| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/hmac" |
| 6 |
"crypto/rand" |
| 7 |
"crypto/sha256" |
| 8 |
"crypto/subtle" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
|
| 12 |
tpb "github.com/google/go-tpm-tools/proto/tpm" |
| 13 |
"github.com/google/go-tpm/tpm2" |
| 14 |
) |
| 15 |
|
| 16 |
// This file aims to implement the verifier side of https://trustedcomputinggroup.org/wp-content/uploads/EK-Based-Key-Attestation-with-TPM-Firmware-Version-V1-RC1_9July2025.pdf#page=8 |
| 17 |
// For reference: https://github.com/TrustedComputingGroup/tpm-fw-attestation-reference-code |
| 18 |
|
| 19 |
var ( |
| 20 |
errCertifiedWrongName = errors.New("incorrect name") |
| 21 |
errWrongHashAlg = errors.New("wrong hash algorithm") |
| 22 |
errInvalidHMAC = errors.New("invalid HMAC") |
| 23 |
errInvalidAttestation = errors.New("attestation statement was invalid") |
| 24 |
) |
| 25 |
|
| 26 |
// CreateRestrictedHMACBlob generates a new HMAC key and wraps it to the given EK. |
| 27 |
func CreateRestrictedHMACBlob(tPublic *tpm2.TPMTPublic) (*tpb.ImportBlob, []byte, error) { |
| 28 |
encap, err := tpm2.ImportEncapsulationKey(tPublic) |
| 29 |
if err != nil { |
| 30 |
return nil, nil, err |
| 31 |
} |
| 32 |
|
| 33 |
hmacKey := make([]byte, 32) |
| 34 |
pub, sensitive := generateRestrictedHMACKey(hmacKey) |
| 35 |
name, err := tpm2.ObjectName(pub) |
| 36 |
if err != nil { |
| 37 |
return nil, nil, err |
| 38 |
} |
| 39 |
|
| 40 |
duplicate, inSymSeed, err := tpm2.CreateDuplicate(rand.Reader, encap, name.Buffer, tpm2.Marshal(sensitive)) |
| 41 |
if err != nil { |
| 42 |
return nil, nil, err |
| 43 |
} |
| 44 |
|
| 45 |
return &tpb.ImportBlob{ |
| 46 |
PublicArea: tpm2.Marshal(pub), |
| 47 |
Duplicate: duplicate, |
| 48 |
EncryptedSeed: inSymSeed, |
| 49 |
}, hmacKey, nil |
| 50 |
} |
| 51 |
|
| 52 |
// VerifyCertifiedAKBlob verifies the blob against a secret HMAC. |
| 53 |
func VerifyCertifiedAKBlob(req *tpb.CertifiedBlob, secret []byte) error { |
| 54 |
akPub, err := tpm2.Unmarshal[tpm2.TPMTPublic](req.GetPubArea()) |
| 55 |
if err != nil { |
| 56 |
return err |
| 57 |
} |
| 58 |
|
| 59 |
akName, err := tpm2.ObjectName(akPub) |
| 60 |
if err != nil { |
| 61 |
return err |
| 62 |
} |
| 63 |
|
| 64 |
signature, err := tpm2.Unmarshal[tpm2.TPMTSignature](req.GetRawSig()) |
| 65 |
if err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
|
| 69 |
hmac, err := signature.Signature.HMAC() |
| 70 |
if err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
|
| 74 |
if err := verifyHMAC(secret, req.GetCertifyInfo(), hmac); err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
|
| 78 |
attest, err := tpm2.Unmarshal[tpm2.TPMSAttest](req.GetCertifyInfo()) |
| 79 |
if err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
|
| 83 |
if err := verifyAttestCertify(attest); err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
|
| 87 |
certify, err := attest.Attested.Certify() |
| 88 |
if err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
|
| 92 |
if err := verifyCertifyInfo(akName, certify); err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
|
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
// generateRestrictedHMACKey writes a new hmac to the input parameter and produces the pub/priv tpm2 structures |
| 100 |
func generateRestrictedHMACKey(hmacKey []byte) (*tpm2.TPMTPublic, *tpm2.TPMTSensitive) { |
| 101 |
// Generate the random obfuscation value and key |
| 102 |
obfuscate := make([]byte, 32) |
| 103 |
rand.Read(obfuscate) |
| 104 |
rand.Read(hmacKey[:]) |
| 105 |
|
| 106 |
// Unique for a KEYEDHASH object is H_nameAlg(obfuscate | key) |
| 107 |
// See Part 1, "Public Area Creation" |
| 108 |
h := sha256.New() |
| 109 |
h.Write(obfuscate) |
| 110 |
h.Write(hmacKey[:]) |
| 111 |
|
| 112 |
pub := &tpm2.TPMTPublic{ |
| 113 |
Type: tpm2.TPMAlgKeyedHash, |
| 114 |
NameAlg: tpm2.TPMAlgSHA256, |
| 115 |
ObjectAttributes: tpm2.TPMAObject{ |
| 116 |
UserWithAuth: true, |
| 117 |
NoDA: true, |
| 118 |
Restricted: true, |
| 119 |
SignEncrypt: true, |
| 120 |
}, |
| 121 |
Parameters: tpm2.NewTPMUPublicParms(tpm2.TPMAlgKeyedHash, &tpm2.TPMSKeyedHashParms{ |
| 122 |
Scheme: tpm2.TPMTKeyedHashScheme{ |
| 123 |
Scheme: tpm2.TPMAlgHMAC, |
| 124 |
Details: tpm2.NewTPMUSchemeKeyedHash(tpm2.TPMAlgHMAC, &tpm2.TPMSSchemeHMAC{ |
| 125 |
HashAlg: tpm2.TPMAlgSHA256, |
| 126 |
}), |
| 127 |
}, |
| 128 |
}), |
| 129 |
Unique: tpm2.NewTPMUPublicID(tpm2.TPMAlgKeyedHash, &tpm2.TPM2BDigest{ |
| 130 |
Buffer: h.Sum(nil), |
| 131 |
}), |
| 132 |
} |
| 133 |
|
| 134 |
priv := &tpm2.TPMTSensitive{ |
| 135 |
SensitiveType: tpm2.TPMAlgKeyedHash, |
| 136 |
SeedValue: tpm2.TPM2BDigest{ |
| 137 |
Buffer: obfuscate, |
| 138 |
}, |
| 139 |
Sensitive: tpm2.NewTPMUSensitiveComposite(tpm2.TPMAlgKeyedHash, &tpm2.TPM2BSensitiveData{ |
| 140 |
Buffer: hmacKey[:], |
| 141 |
}), |
| 142 |
} |
| 143 |
|
| 144 |
return pub, priv |
| 145 |
} |
| 146 |
|
| 147 |
// verifyHMAC checks the MAC on the given message. |
| 148 |
func verifyHMAC(hmacKey []byte, message []byte, ha *tpm2.TPMTHA) error { |
| 149 |
if ha.HashAlg != tpm2.TPMAlgSHA256 { |
| 150 |
return fmt.Errorf("%w %v (expected SHA256)", errWrongHashAlg, ha.HashAlg) |
| 151 |
} |
| 152 |
digest := sha256.Sum256(message) |
| 153 |
h := hmac.New(sha256.New, hmacKey[:]) |
| 154 |
h.Write(digest[:]) |
| 155 |
if subtle.ConstantTimeCompare(ha.Digest, h.Sum(nil)) != 1 { |
| 156 |
return errInvalidHMAC |
| 157 |
} |
| 158 |
return nil |
| 159 |
} |
| 160 |
|
| 161 |
// verifyAttestCertify checks that the attestation structure has valid data |
| 162 |
func verifyAttestCertify(attest *tpm2.TPMSAttest) error { |
| 163 |
if attest.Type != tpm2.TPMSTAttestCertify { |
| 164 |
return fmt.Errorf("expected attest type TPMSTAttestCertify, got %v", attest.Type) |
| 165 |
} |
| 166 |
if attest.Magic != tpm2.TPMGeneratedValue { |
| 167 |
return fmt.Errorf("%w: unexpected prefix %0x", errInvalidAttestation, attest.Magic) |
| 168 |
} |
| 169 |
|
| 170 |
return nil |
| 171 |
} |
| 172 |
|
| 173 |
// verifyCertifyInfo checks the certifyInfo against the given name. |
| 174 |
func verifyCertifyInfo(name *tpm2.TPM2BName, certifyInfo *tpm2.TPMSCertifyInfo) error { |
| 175 |
// Check that the certified Name is the same as we expected. |
| 176 |
if !bytes.Equal(name.Buffer, certifyInfo.Name.Buffer) { |
| 177 |
return fmt.Errorf("%w: expected Name %x, certified Name was %x", errCertifiedWrongName, name.Buffer, certifyInfo.Name.Buffer) |
| 178 |
} |
| 179 |
|
| 180 |
// We can't really check the QualifiedName here, since we don't have any |
| 181 |
// information about the object's parent. As a paranoid consistency check, |
| 182 |
// just make sure that QualifiedName doesn't match Name for some reason. |
| 183 |
if bytes.Equal(certifyInfo.QualifiedName.Buffer, certifyInfo.Name.Buffer) { |
| 184 |
return fmt.Errorf("%w: QualifiedName unexpectedly matched Name", errCertifiedWrongName) |
| 185 |
} |
| 186 |
|
| 187 |
return nil |
| 188 |
} |