| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 3 | |
| 4 | package tpm |
| 5 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 6 | // This file is adapted from github.com/google/go-tpm/tpm2/credactivation which |
| 7 | // outputs broken challenges for unknown reasons. They use u16 length-delimited |
| 8 | // outputs for the challenge blobs which is incorrect. Rather than rewriting |
| 9 | // the routine, we only applied minimal fixes to it and skip the ECC part of |
| 10 | // the issue (because we would rather trust the proprietary RSA |
| 11 | // implementation). |
| Leopold Schabel | 8fba0f8 | 2020-01-22 18:46:25 +0100 | [diff] [blame] | 12 | // |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 13 | // TODO(lorenz): I'll eventually deal with this upstream, but for now just fix |
| 14 | // it here (it's not that) much code after all. |
| 15 | // https://github.com/google/go-tpm/issues/121 |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "crypto/aes" |
| 19 | "crypto/cipher" |
| 20 | "crypto/hmac" |
| 21 | "crypto/rsa" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | |
| 25 | "github.com/google/go-tpm/tpm2" |
| 26 | "github.com/google/go-tpm/tpmutil" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | labelIdentity = "IDENTITY" |
| 31 | labelStorage = "STORAGE" |
| 32 | labelIntegrity = "INTEGRITY" |
| 33 | ) |
| 34 | |
| 35 | func generateRSA(aik *tpm2.HashValue, pub *rsa.PublicKey, symBlockSize int, secret []byte, rnd io.Reader) ([]byte, []byte, error) { |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 36 | aikHash, err := aik.Alg.Hash() |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 37 | if err != nil { |
| 38 | return nil, nil, err |
| 39 | } |
| 40 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 41 | // The seed length should match the keysize used by the EKs symmetric |
| 42 | // cipher. |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 43 | // For typical RSA EKs, this will be 128 bits (16 bytes). |
| 44 | // Spec: TCG 2.0 EK Credential Profile revision 14, section 2.1.5.1. |
| 45 | seed := make([]byte, symBlockSize) |
| 46 | if _, err := io.ReadFull(rnd, seed); err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 47 | return nil, nil, fmt.Errorf("generating seed: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Encrypt the seed value using the provided public key. |
| 51 | // See annex B, section 10.4 of the TPM specification revision 2 part 1. |
| 52 | label := append([]byte(labelIdentity), 0) |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 53 | encSecret, err := rsa.EncryptOAEP(aikHash.New(), rnd, pub, seed, label) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 54 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 55 | return nil, nil, fmt.Errorf("generating encrypted seed: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 58 | // Generate the encrypted credential by convolving the seed with the digest |
| 59 | // of the AIK, and using the result as the key to encrypt the secret. |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 60 | // See section 24.4 of TPM 2.0 specification, part 1. |
| 61 | aikNameEncoded, err := aik.Encode() |
| 62 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 63 | return nil, nil, fmt.Errorf("encoding aikName: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 64 | } |
| 65 | symmetricKey, err := tpm2.KDFa(aik.Alg, seed, labelStorage, aikNameEncoded, nil, len(seed)*8) |
| 66 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 67 | return nil, nil, fmt.Errorf("generating symmetric key: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 68 | } |
| 69 | c, err := aes.NewCipher(symmetricKey) |
| 70 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 71 | return nil, nil, fmt.Errorf("symmetric cipher setup: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 72 | } |
| 73 | cv, err := tpmutil.Pack(tpmutil.U16Bytes(secret)) |
| 74 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 75 | return nil, nil, fmt.Errorf("generating cv (TPM2B_Digest): %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // IV is all null bytes. encIdentity represents the encrypted credential. |
| 79 | encIdentity := make([]byte, len(cv)) |
| 80 | cipher.NewCFBEncrypter(c, make([]byte, len(symmetricKey))).XORKeyStream(encIdentity, cv) |
| 81 | |
| 82 | // Generate the integrity HMAC, which is used to protect the integrity of the |
| 83 | // encrypted structure. |
| 84 | // See section 24.5 of the TPM specification revision 2 part 1. |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 85 | macKey, err := tpm2.KDFa(aik.Alg, seed, labelIntegrity, nil, nil, aikHash.Size()*8) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 86 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 87 | return nil, nil, fmt.Errorf("generating HMAC key: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 90 | mac := hmac.New(aikHash.New, macKey) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 91 | mac.Write(encIdentity) |
| 92 | mac.Write(aikNameEncoded) |
| 93 | integrityHMAC := mac.Sum(nil) |
| 94 | |
| 95 | idObject := &tpm2.IDObject{ |
| 96 | IntegrityHMAC: integrityHMAC, |
| 97 | EncIdentity: encIdentity, |
| 98 | } |
| 99 | id, err := tpmutil.Pack(idObject) |
| 100 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 101 | return nil, nil, fmt.Errorf("encoding IDObject: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | packedID, err := tpmutil.Pack(id) |
| 105 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 106 | return nil, nil, fmt.Errorf("packing id: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 107 | } |
| 108 | packedEncSecret, err := tpmutil.Pack(encSecret) |
| 109 | if err != nil { |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 110 | return nil, nil, fmt.Errorf("packing encSecret: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return packedID, packedEncSecret, nil |
| 114 | } |