| 1 |
package tpm2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
) |
| 6 |
|
| 7 |
var ( |
| 8 |
labelIdentity = "IDENTITY" |
| 9 |
) |
| 10 |
|
| 11 |
// CreateCredential creates an encrypted secret that can be recovered using ActivateCredential as part of a key-attestation flow. |
| 12 |
func CreateCredential(rand io.Reader, pub LabeledEncapsulationKey, name []byte, credentialValue []byte) (idObject []byte, encSecret []byte, err error) { |
| 13 |
secret, ciphertext, err := pub.Encapsulate(rand, labelIdentity) |
| 14 |
if err != nil { |
| 15 |
return nil, nil, err |
| 16 |
} |
| 17 |
|
| 18 |
// Marshal the credentialValue as a TPM2B_DIGEST before encrypting it. |
| 19 |
// See Part 1, "Credential Protection", and Part 2, "TPMS_ID_OBJECT". |
| 20 |
credential2B := Marshal(TPM2BDigest{Buffer: credentialValue}) |
| 21 |
|
| 22 |
// Encrypt the credentialValue as encIdentity. |
| 23 |
encIdentity, err := deriveAndEncrypt(pub, secret, name, credential2B) |
| 24 |
if err != nil { |
| 25 |
return nil, nil, err |
| 26 |
} |
| 27 |
|
| 28 |
// Compute the HMAC of (encIdentity || name) |
| 29 |
identityHMAC, err := deriveAndHMAC(pub, secret, nil, encIdentity, name) |
| 30 |
if err != nil { |
| 31 |
return nil, nil, err |
| 32 |
} |
| 33 |
|
| 34 |
// Marshal the virtual TPMS_ID_OBJECT ourselves. We have to do this since encIdentity's size is encrypted. |
| 35 |
idObject = make([]byte, 0, 2+len(identityHMAC)+len(encIdentity)) |
| 36 |
idObject = append(idObject, Marshal(TPM2BDigest{Buffer: identityHMAC})...) |
| 37 |
idObject = append(idObject, encIdentity...) |
| 38 |
|
| 39 |
return idObject, ciphertext, nil |
| 40 |
} |