| 1 |
// Copyright 2026 Google Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 |
// use this file except in compliance with the License. You may obtain a copy of |
| 5 |
// 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, WITHOUT |
| 11 |
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
// License for the specific language governing permissions and limitations under |
| 13 |
// the License. |
| 14 |
|
| 15 |
package attest |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto" |
| 20 |
"crypto/ecdsa" |
| 21 |
"crypto/elliptic" |
| 22 |
"crypto/hmac" |
| 23 |
"crypto/rand" |
| 24 |
"crypto/rsa" |
| 25 |
"crypto/sha256" |
| 26 |
"crypto/subtle" |
| 27 |
"fmt" |
| 28 |
|
| 29 |
"github.com/google/go-tpm/tpm2" |
| 30 |
) |
| 31 |
|
| 32 |
// HMAC represents a SHA-256 HMAC digest, strictly bound to the SHA-256 output size (32 bytes). |
| 33 |
type HMAC [sha256.Size]byte |
| 34 |
|
| 35 |
func generateRestrictedHMACKey() (tpm2.TPMTPublic, tpm2.TPMTSensitive, HMAC) { |
| 36 |
var hmacKeyBytes HMAC |
| 37 |
rand.Read(hmacKeyBytes[:]) |
| 38 |
|
| 39 |
// The size is sha256.Size. |
| 40 |
obfuscate := make([]byte, 32) |
| 41 |
rand.Read(obfuscate) |
| 42 |
|
| 43 |
h := sha256.New() |
| 44 |
h.Write(obfuscate) |
| 45 |
h.Write(hmacKeyBytes[:]) |
| 46 |
|
| 47 |
hmacPub := tpm2.TPMTPublic{ |
| 48 |
Type: tpm2.TPMAlgKeyedHash, |
| 49 |
NameAlg: tpm2.TPMAlgSHA256, |
| 50 |
ObjectAttributes: tpm2.TPMAObject{ |
| 51 |
UserWithAuth: true, |
| 52 |
NoDA: true, |
| 53 |
Restricted: true, |
| 54 |
SignEncrypt: true, |
| 55 |
}, |
| 56 |
Parameters: tpm2.NewTPMUPublicParms(tpm2.TPMAlgKeyedHash, |
| 57 |
&tpm2.TPMSKeyedHashParms{ |
| 58 |
Scheme: tpm2.TPMTKeyedHashScheme{ |
| 59 |
Scheme: tpm2.TPMAlgHMAC, |
| 60 |
Details: tpm2.NewTPMUSchemeKeyedHash(tpm2.TPMAlgHMAC, |
| 61 |
&tpm2.TPMSSchemeHMAC{ |
| 62 |
HashAlg: tpm2.TPMAlgSHA256, |
| 63 |
}), |
| 64 |
}, |
| 65 |
}), |
| 66 |
Unique: tpm2.NewTPMUPublicID(tpm2.TPMAlgKeyedHash, &tpm2.TPM2BDigest{Buffer: h.Sum(nil)}), |
| 67 |
} |
| 68 |
|
| 69 |
sensitive := tpm2.TPMTSensitive{ |
| 70 |
SensitiveType: tpm2.TPMAlgKeyedHash, |
| 71 |
SeedValue: tpm2.TPM2BDigest{ |
| 72 |
Buffer: obfuscate, |
| 73 |
}, |
| 74 |
Sensitive: tpm2.NewTPMUSensitiveComposite(tpm2.TPMAlgKeyedHash, &tpm2.TPM2BSensitiveData{ |
| 75 |
Buffer: hmacKeyBytes[:], |
| 76 |
}), |
| 77 |
} |
| 78 |
|
| 79 |
return hmacPub, sensitive, hmacKeyBytes |
| 80 |
} |
| 81 |
|
| 82 |
// GenerateEkChallenge generates a challenge that can only be solved by a TPM |
| 83 |
// possessing the private key corresponding to the provided Endorsement Key (EK). |
| 84 |
// |
| 85 |
// It generates an ephemeral restricted HMAC key, duplicates it under the EK's |
| 86 |
// public key. |
| 87 |
// |
| 88 |
// The returned DecryptionEkChallenge should be sent to the client. The returned |
| 89 |
// HMAC key bytes (32 bytes) must be retained by the challenger (CA) to verify |
| 90 |
// // the client's response later. |
| 91 |
func GenerateEkChallenge(ekPublic crypto.PublicKey) (*DecryptionEkChallenge, *HMAC, error) { |
| 92 |
// Reconstruct EK public area in v0/tpm2 format from pub (crypto.PublicKey) |
| 93 |
var ekPubContents tpm2.TPMTPublic |
| 94 |
|
| 95 |
switch ekPub := ekPublic.(type) { |
| 96 |
case *rsa.PublicKey: |
| 97 |
u := tpm2.NewTPMUPublicID( |
| 98 |
tpm2.TPMAlgRSA, |
| 99 |
&tpm2.TPM2BPublicKeyRSA{ |
| 100 |
Buffer: ekPub.N.Bytes(), |
| 101 |
}, |
| 102 |
) |
| 103 |
ekPubContents = tpm2.RSAEKTemplate |
| 104 |
ekPubContents.Unique = u |
| 105 |
|
| 106 |
case *ecdsa.PublicKey: |
| 107 |
var size int |
| 108 |
var curveID tpm2.TPMECCCurve |
| 109 |
switch ekPub.Curve { |
| 110 |
case elliptic.P256(): |
| 111 |
size = 32 |
| 112 |
curveID = tpm2.TPMECCNistP256 |
| 113 |
case elliptic.P384(): |
| 114 |
size = 48 |
| 115 |
curveID = tpm2.TPMECCNistP384 |
| 116 |
case elliptic.P521(): |
| 117 |
size = 66 |
| 118 |
curveID = tpm2.TPMECCNistP521 |
| 119 |
default: |
| 120 |
return nil, nil, fmt.Errorf("unsupported ECC curve: %v", ekPub.Curve.Params().Name) |
| 121 |
} |
| 122 |
xBytes := make([]byte, size) |
| 123 |
yBytes := make([]byte, size) |
| 124 |
ekPub.X.FillBytes(xBytes) |
| 125 |
ekPub.Y.FillBytes(yBytes) |
| 126 |
|
| 127 |
u := tpm2.NewTPMUPublicID( |
| 128 |
tpm2.TPMAlgECC, |
| 129 |
&tpm2.TPMSECCPoint{ |
| 130 |
X: tpm2.TPM2BECCParameter{Buffer: xBytes}, |
| 131 |
Y: tpm2.TPM2BECCParameter{Buffer: yBytes}, |
| 132 |
}, |
| 133 |
) |
| 134 |
ekPubContents = tpm2.ECCEKTemplate |
| 135 |
ekPubContents.Parameters = tpm2.NewTPMUPublicParms( |
| 136 |
tpm2.TPMAlgECC, |
| 137 |
&tpm2.TPMSECCParms{ |
| 138 |
Symmetric: tpm2.TPMTSymDefObject{ |
| 139 |
Algorithm: tpm2.TPMAlgAES, |
| 140 |
KeyBits: tpm2.NewTPMUSymKeyBits( |
| 141 |
tpm2.TPMAlgAES, |
| 142 |
tpm2.TPMKeyBits(128), |
| 143 |
), |
| 144 |
Mode: tpm2.NewTPMUSymMode( |
| 145 |
tpm2.TPMAlgAES, |
| 146 |
tpm2.TPMAlgCFB, |
| 147 |
), |
| 148 |
}, |
| 149 |
CurveID: curveID, |
| 150 |
}, |
| 151 |
) |
| 152 |
ekPubContents.Unique = u |
| 153 |
|
| 154 |
default: |
| 155 |
return nil, nil, fmt.Errorf("unsupported EK public key type: %T", ekPublic) |
| 156 |
} |
| 157 |
|
| 158 |
// Import EK public key as encapsulation key |
| 159 |
encap, err := tpm2.ImportEncapsulationKey(&ekPubContents) |
| 160 |
if err != nil { |
| 161 |
return nil, nil, fmt.Errorf("ImportEncapsulationKey() failed: %v", err) |
| 162 |
} |
| 163 |
|
| 164 |
hmacPub, sensitive, hmacKeyBytes := generateRestrictedHMACKey() |
| 165 |
|
| 166 |
hmacName, err := tpm2.ObjectName(&hmacPub) |
| 167 |
if err != nil { |
| 168 |
return nil, nil, fmt.Errorf("ObjectName() failed: %v", err) |
| 169 |
} |
| 170 |
|
| 171 |
// Duplicate the HMAC key under EK |
| 172 |
duplicate, inSymSeed, err := tpm2.CreateDuplicate(rand.Reader, encap, hmacName.Buffer, tpm2.Marshal(sensitive)) |
| 173 |
if err != nil { |
| 174 |
return nil, nil, fmt.Errorf("CreateDuplicate() failed: %v", err) |
| 175 |
} |
| 176 |
|
| 177 |
challenge := &DecryptionEkChallenge{ |
| 178 |
ObjectPublic: tpm2.Marshal(hmacPub), |
| 179 |
Duplicate: duplicate, |
| 180 |
InSymSeed: inSymSeed, |
| 181 |
} |
| 182 |
return challenge, &hmacKeyBytes, nil |
| 183 |
} |
| 184 |
|
| 185 |
// VerifySolvedDecryptionEkChallenge verifies that the client has successfully solved the EK |
| 186 |
// decryption challenge and certified the correct Attestation Key (AK). |
| 187 |
// |
| 188 |
// It unmarshals the AK public area, verifies the HMAC signature over the attestation data using |
| 189 |
// the challenge HMAC key, and checks that the certified name matches the AK's name. |
| 190 |
// |
| 191 |
// akPubData is the marshaled TPMT_PUBLIC representation of the AK. |
| 192 |
// certParams contains the attestation data and signature returned by the client. |
| 193 |
// challengeHMACKey is the raw 32-byte HMAC key generated by GenerateEkChallenge. |
| 194 |
func VerifySolvedDecryptionEkChallenge(akPubData []byte, certParams *CertificationParameters, challengeHMACKey HMAC) error { |
| 195 |
if certParams == nil { |
| 196 |
return fmt.Errorf("certParams is nil") |
| 197 |
} |
| 198 |
akPub, err := tpm2.Unmarshal[tpm2.TPMTPublic](akPubData) |
| 199 |
if err != nil { |
| 200 |
return fmt.Errorf("failed to unmarshal AK public area: %v", err) |
| 201 |
} |
| 202 |
|
| 203 |
akName, err := tpm2.ObjectName(akPub) |
| 204 |
if err != nil { |
| 205 |
return fmt.Errorf("failed to get AK name: %v", err) |
| 206 |
} |
| 207 |
|
| 208 |
signature, err := tpm2.Unmarshal[tpm2.TPMTSignature](certParams.CreateSignature) |
| 209 |
if err != nil { |
| 210 |
return fmt.Errorf("failed to unmarshal signature: %v", err) |
| 211 |
} |
| 212 |
|
| 213 |
hmac, err := signature.Signature.HMAC() |
| 214 |
if err != nil { |
| 215 |
return fmt.Errorf("failed to get signature: %v", err) |
| 216 |
} |
| 217 |
|
| 218 |
if err := verifyHMAC(certParams.CreateAttestation, hmac, challengeHMACKey); err != nil { |
| 219 |
return fmt.Errorf("failed to verify HMAC: %v", err) |
| 220 |
} |
| 221 |
|
| 222 |
attest, err := tpm2.Unmarshal[tpm2.TPMSAttest](certParams.CreateAttestation) |
| 223 |
if err != nil { |
| 224 |
return fmt.Errorf("failed to unmarshal attestation: %v", err) |
| 225 |
} |
| 226 |
|
| 227 |
if err := verifyAttest(attest); err != nil { |
| 228 |
return fmt.Errorf("failed to verify attestation: %v", err) |
| 229 |
} |
| 230 |
|
| 231 |
certify, err := attest.Attested.Certify() |
| 232 |
if err != nil { |
| 233 |
return fmt.Errorf("failed to certify: %v", err) |
| 234 |
} |
| 235 |
|
| 236 |
if err := verifyCertify(akName, certify); err != nil { |
| 237 |
return fmt.Errorf("failed to certify: %v", err) |
| 238 |
} |
| 239 |
return nil |
| 240 |
} |
| 241 |
|
| 242 |
func verifyHMAC(message []byte, ha *tpm2.TPMTHA, challengeHMACKey HMAC) error { |
| 243 |
if ha.HashAlg != tpm2.TPMAlgSHA256 { |
| 244 |
return fmt.Errorf("%v (expected SHA256)", ha.HashAlg) |
| 245 |
} |
| 246 |
|
| 247 |
if len(message) == 0 { |
| 248 |
return fmt.Errorf("message is empty") |
| 249 |
} |
| 250 |
// The HMAC is over SHA256(message). |
| 251 |
digest := sha256.Sum256(message) |
| 252 |
|
| 253 |
h := hmac.New(sha256.New, challengeHMACKey[:]) |
| 254 |
h.Write(digest[:]) |
| 255 |
if subtle.ConstantTimeCompare(ha.Digest, h.Sum(nil)) != 1 { |
| 256 |
return fmt.Errorf("invalid HMAC") |
| 257 |
} |
| 258 |
return nil |
| 259 |
} |
| 260 |
|
| 261 |
// verifyAttest checks that the attestation structure has valid data |
| 262 |
func verifyAttest(attest *tpm2.TPMSAttest) error { |
| 263 |
if attest.Magic != tpm2.TPMGeneratedValue { |
| 264 |
return fmt.Errorf("unexpected prefix %0x", attest.Magic) |
| 265 |
} |
| 266 |
if attest.Type != tpm2.TPMSTAttestCertify { |
| 267 |
return fmt.Errorf("unexpected type %0x", attest.Type) |
| 268 |
} |
| 269 |
return nil |
| 270 |
} |
| 271 |
|
| 272 |
func verifyCertify(name *tpm2.TPM2BName, certifyInfo *tpm2.TPMSCertifyInfo) error { |
| 273 |
// Check that the certified Name is the same as we expected. |
| 274 |
if subtle.ConstantTimeCompare(name.Buffer, certifyInfo.Name.Buffer) == 0 { |
| 275 |
return fmt.Errorf("expected Name %x, certified Name was %x", name.Buffer, certifyInfo.Name.Buffer) |
| 276 |
} |
| 277 |
|
| 278 |
// We can't really check the QualifiedName here, since we don't have any |
| 279 |
// information about the object's parent. As a paranoid consistency check, |
| 280 |
// just make sure that QualifiedName doesn't match Name for some reason. |
| 281 |
if bytes.Equal(certifyInfo.QualifiedName.Buffer, certifyInfo.Name.Buffer) { |
| 282 |
return fmt.Errorf("QualifiedName unexpectedly matched Name") |
| 283 |
} |
| 284 |
|
| 285 |
return nil |
| 286 |
} |