Debian Codegraph @

legacy/tpm2/credactivation/credential_activation.go

raw
1 // Copyright (c) 2018, Google LLC All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package credactivation implements generation of data blobs to be used
16 // when invoking the ActivateCredential command, on a TPM.
17 package credactivation
18
19 import (
20 "crypto"
21 "crypto/aes"
22 "crypto/cipher"
23 "crypto/ecdh"
24 "crypto/ecdsa"
25 "crypto/hmac"
26 "crypto/rand"
27 "crypto/rsa"
28 "errors"
29 "fmt"
30 "io"
31
32 "github.com/google/go-tpm/legacy/tpm2"
33 "github.com/google/go-tpm/tpmutil"
34 )
35
36 // Labels for use in key derivation or OAEP encryption.
37 const (
38 labelIdentity = "IDENTITY"
39 labelStorage = "STORAGE"
40 labelIntegrity = "INTEGRITY"
41 )
42
43 // Generate returns a TPM2B_ID_OBJECT & TPM2B_ENCRYPTED_SECRET for use in
44 // credential activation.
45 // This has been tested on EKs compliant with TCG 2.0 EK Credential Profile
46 // specification, revision 14.
47 // The pub parameter must be a pointer to rsa.PublicKey.
48 // The secret parameter must not be longer than the longest digest size implemented
49 // by the TPM. A 32 byte secret is a safe, recommended default.
50 //
51 // This function implements Credential Protection as defined in section 24 of the TPM
52 // specification revision 2 part 1.
53 // See: https://trustedcomputinggroup.org/resource/tpm-library-specification/
54 func Generate(aik *tpm2.HashValue, pub crypto.PublicKey, symBlockSize int, secret []byte) ([]byte, []byte, error) {
55 return generate(aik, pub, symBlockSize, secret, rand.Reader)
56 }
57
58 func generate(aik *tpm2.HashValue, pub crypto.PublicKey, symBlockSize int, secret []byte, rnd io.Reader) ([]byte, []byte, error) {
59 var seed, encSecret []byte
60 var err error
61 switch ekKey := pub.(type) {
62 case *ecdh.PublicKey:
63 seed, encSecret, err = createECSeed(aik, ekKey, rnd)
64 if err != nil {
65 return nil, nil, fmt.Errorf("creating seed: %v", err)
66 }
67 case *ecdsa.PublicKey:
68 ecdhKey, err := ekKey.ECDH()
69 if err != nil {
70 return nil, nil, fmt.Errorf("transmuting ecdsa key to ecdh key: %v", err)
71 }
72 return generate(aik, ecdhKey, symBlockSize, secret, rnd)
73 case *rsa.PublicKey:
74 seed, encSecret, err = createRSASeed(aik, ekKey, symBlockSize, rnd)
75 if err != nil {
76 return nil, nil, fmt.Errorf("creating seed: %v", err)
77 }
78 default:
79 return nil, nil, errors.New("only RSA and EC public keys are supported for credential activation")
80 }
81
82 // Generate the encrypted credential by convolving the seed with the digest of
83 // the AIK, and using the result as the key to encrypt the secret.
84 // See section 24.4 of TPM 2.0 specification, part 1.
85 aikNameEncoded, err := aik.Encode()
86 if err != nil {
87 return nil, nil, fmt.Errorf("encoding aikName: %v", err)
88 }
89 symmetricKey, err := tpm2.KDFa(aik.Alg, seed, labelStorage, aikNameEncoded, nil, symBlockSize*8)
90 if err != nil {
91 return nil, nil, fmt.Errorf("generating symmetric key: %v", err)
92 }
93 c, err := aes.NewCipher(symmetricKey)
94 if err != nil {
95 return nil, nil, fmt.Errorf("symmetric cipher setup: %v", err)
96 }
97 cv, err := tpmutil.Pack(tpmutil.U16Bytes(secret))
98 if err != nil {
99 return nil, nil, fmt.Errorf("generating cv (TPM2B_Digest): %v", err)
100 }
101
102 // IV is all null bytes. encIdentity represents the encrypted credential.
103 encIdentity := make([]byte, len(cv))
104 cipher.NewCFBEncrypter(c, make([]byte, len(symmetricKey))).XORKeyStream(encIdentity, cv)
105
106 // Generate the integrity HMAC, which is used to protect the integrity of the
107 // encrypted structure.
108 // See section 24.5 of the TPM 2.0 specification.
109 cryptohash, err := aik.Alg.Hash()
110 if err != nil {
111 return nil, nil, err
112 }
113 macKey, err := tpm2.KDFa(aik.Alg, seed, labelIntegrity, nil, nil, cryptohash.Size()*8)
114 if err != nil {
115 return nil, nil, fmt.Errorf("generating HMAC key: %v", err)
116 }
117
118 mac := hmac.New(cryptohash.New, macKey)
119 mac.Write(encIdentity)
120 mac.Write(aikNameEncoded)
121 integrityHMAC := mac.Sum(nil)
122
123 idObject := &tpm2.IDObject{
124 IntegrityHMAC: integrityHMAC,
125 EncIdentity: encIdentity,
126 }
127 id, err := tpmutil.Pack(idObject)
128 if err != nil {
129 return nil, nil, fmt.Errorf("encoding IDObject: %v", err)
130 }
131
132 packedID, err := tpmutil.Pack(tpmutil.U16Bytes(id))
133 if err != nil {
134 return nil, nil, fmt.Errorf("packing id: %v", err)
135 }
136 packedEncSecret, err := tpmutil.Pack(tpmutil.U16Bytes(encSecret))
137 if err != nil {
138 return nil, nil, fmt.Errorf("packing encSecret: %v", err)
139 }
140
141 return packedID, packedEncSecret, nil
142 }
143
144 func createRSASeed(aik *tpm2.HashValue, ek *rsa.PublicKey, symBlockSize int, rnd io.Reader) ([]byte, []byte, error) {
145 crypothash, err := aik.Alg.Hash()
146 if err != nil {
147 return nil, nil, err
148 }
149
150 // The seed length should match the keysize used by the EKs symmetric cipher.
151 // For typical RSA EKs, this will be 128 bits (16 bytes).
152 // Spec: TCG 2.0 EK Credential Profile revision 14, section 2.1.5.1.
153 seed := make([]byte, symBlockSize)
154 if _, err := io.ReadFull(rnd, seed); err != nil {
155 return nil, nil, fmt.Errorf("generating seed: %v", err)
156 }
157
158 // Encrypt the seed value using the provided public key.
159 // See annex B, section 10.4 of the TPM specification revision 2 part 1.
160 label := append([]byte(labelIdentity), 0)
161 encryptedSeed, err := rsa.EncryptOAEP(crypothash.New(), rnd, ek, seed, label)
162 if err != nil {
163 return nil, nil, fmt.Errorf("generating encrypted seed: %v", err)
164 }
165
166 encryptedSeed, err = tpmutil.Pack(encryptedSeed)
167 return seed, encryptedSeed, err
168 }
169
170 func createECSeed(ak *tpm2.HashValue, ek *ecdh.PublicKey, rnd io.Reader) (seed, encryptedSeed []byte, err error) {
171 ephemeralPriv, err := ek.Curve().GenerateKey(rnd)
172 if err != nil {
173 return nil, nil, err
174 }
175 ephemeralX, ephemeralY := deconstructECDHPublicKey(ephemeralPriv.PublicKey())
176
177 z, err := ephemeralPriv.ECDH(ek)
178 if err != nil {
179 return nil, nil, err
180 }
181
182 ekX, _ := deconstructECDHPublicKey(ek)
183
184 crypothash, err := ak.Alg.Hash()
185 if err != nil {
186 return nil, nil, err
187 }
188
189 seed, err = tpm2.KDFe(
190 ak.Alg,
191 z,
192 labelIdentity,
193 ephemeralX,
194 ekX,
195 crypothash.Size()*8)
196 if err != nil {
197 return nil, nil, err
198 }
199 encryptedSeed, err = tpmutil.Pack(tpmutil.U16Bytes(ephemeralX), tpmutil.U16Bytes(ephemeralY))
200 return seed, encryptedSeed, err
201 }
202
203 func deconstructECDHPublicKey(key *ecdh.PublicKey) (x []byte, y []byte) {
204 b := key.Bytes()[1:]
205 return b[:len(b)/2], b[len(b)/2:]
206 }