Skip to content

Commit 2c2b580

Browse files
committed
Add support for 256 bit AES block size encryption
RSA 3072, RSA 4096, ECDSA P384 and ECDSA P512 EKs use a different AES block size for credential activation.
1 parent 2306d5b commit 2c2b580

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

attest/activation.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ const (
2323
// activationSecretLen is the size in bytes of the generated secret
2424
// which is generated for credential activation.
2525
activationSecretLen = 32
26-
// symBlockSize is the block size used for symmetric ciphers used
27-
// when generating the credential activation challenge.
28-
symBlockSize = 16
26+
// defaultSymBlockSize is the block size used for symmetric ciphers
27+
// used when generating the credential activation challenge.
28+
defaultSymBlockSize = 16
2929
// tpm20GeneratedMagic is a magic tag when can only be present on a
3030
// TPM structure if the structure was generated wholly by the TPM.
3131
tpm20GeneratedMagic = 0xff544347
@@ -239,7 +239,8 @@ func (p *ActivationParameters) generateChallengeTPM20(secret []byte) (*Encrypted
239239
if att.AttestedCreationInfo.Name.Digest == nil {
240240
return nil, fmt.Errorf("attestation creation info name has no digest")
241241
}
242-
cred, encSecret, err := credactivation.Generate(att.AttestedCreationInfo.Name.Digest, p.EK, symBlockSize, secret)
242+
243+
cred, encSecret, err := credactivation.Generate(att.AttestedCreationInfo.Name.Digest, p.EK, symBlockSizeForEK(p.EK), secret)
243244
if err != nil {
244245
return nil, fmt.Errorf("credactivation.Generate() failed: %v", err)
245246
}

attest/certification.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package attest
1717
import (
1818
"bytes"
1919
"crypto"
20+
"crypto/ecdsa"
21+
"crypto/elliptic"
2022
"crypto/rand"
2123
"crypto/rsa"
2224
"errors"
@@ -191,6 +193,23 @@ func (p *CertificationParameters) Verify(opts VerifyOpts) error {
191193
return nil
192194
}
193195

196+
func symBlockSizeForEK(ek crypto.PublicKey) int {
197+
// see TCG EK Credential Profile Version 2.6, December 4, 2024, B.4.4 Storage EK Template
198+
symBlockSize := defaultSymBlockSize // default to 16 bytes; 128 bits
199+
switch pk := ek.(type) {
200+
case *rsa.PublicKey:
201+
if pk.Size() >= 384 { // RSA 3072, RSA 4096
202+
symBlockSize = 32 // 32 bytes; 256 bits
203+
}
204+
case *ecdsa.PublicKey:
205+
if pk.Curve == elliptic.P384() || pk.Curve == elliptic.P521() {
206+
symBlockSize = 32 // 32 bytes; 256 bits
207+
}
208+
}
209+
210+
return symBlockSize
211+
}
212+
194213
// Generate returns a credential activation challenge, which can be provided
195214
// to the TPM to verify the AK parameters given are authentic & the AK
196215
// is present on the same TPM as the EK.
@@ -225,7 +244,7 @@ func (p *CertificationParameters) Generate(rnd io.Reader, verifyOpts VerifyOpts,
225244
return nil, nil, fmt.Errorf("attestation does not apply to certify data, got %x", att.Type)
226245
}
227246

228-
cred, encSecret, err := credactivation.Generate(activateOpts.VerifierKeyNameDigest, activateOpts.EK, symBlockSize, secret)
247+
cred, encSecret, err := credactivation.Generate(activateOpts.VerifierKeyNameDigest, activateOpts.EK, symBlockSizeForEK(activateOpts.EK), secret)
229248
if err != nil {
230249
return nil, nil, fmt.Errorf("credactivation.Generate() failed: %v", err)
231250
}

attest/certification_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ package attest
2222
import (
2323
"bytes"
2424
"crypto"
25+
"crypto/ecdsa"
26+
"crypto/elliptic"
2527
"crypto/rand"
2628
"crypto/rsa"
2729
"testing"
@@ -397,3 +399,91 @@ func TestKeyActivationTPM20(t *testing.T) {
397399
})
398400
}
399401
}
402+
403+
func Test_symBlockSizeForEK(t *testing.T) {
404+
t.Parallel()
405+
406+
t.Run("rsa-2048", func(t *testing.T) {
407+
t.Parallel()
408+
409+
k, err := rsa.GenerateKey(rand.Reader, 2048)
410+
if err != nil {
411+
t.Fatalf("unexpected error: %v", err)
412+
}
413+
414+
symBlockSize := symBlockSizeForEK(k.Public())
415+
if symBlockSize != 16 {
416+
t.Errorf("unexpected symBlockSize %d; expected 16", symBlockSize)
417+
}
418+
})
419+
420+
t.Run("rsa-3072", func(t *testing.T) {
421+
t.Parallel()
422+
423+
k, err := rsa.GenerateKey(rand.Reader, 3072)
424+
if err != nil {
425+
t.Fatalf("unexpected error: %v", err)
426+
}
427+
428+
symBlockSize := symBlockSizeForEK(k.Public())
429+
if symBlockSize != 32 {
430+
t.Errorf("unexpected symBlockSize %d; expected 32", symBlockSize)
431+
}
432+
})
433+
434+
t.Run("rsa-4096", func(t *testing.T) {
435+
t.Parallel()
436+
437+
k, err := rsa.GenerateKey(rand.Reader, 4096)
438+
if err != nil {
439+
t.Fatalf("unexpected error: %v", err)
440+
}
441+
442+
symBlockSize := symBlockSizeForEK(k.Public())
443+
if symBlockSize != 32 {
444+
t.Errorf("unexpected symBlockSize %d; expected 32", symBlockSize)
445+
}
446+
})
447+
448+
t.Run("ecdsa-P256", func(t *testing.T) {
449+
t.Parallel()
450+
451+
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
452+
if err != nil {
453+
t.Fatalf("unexpected error: %v", err)
454+
}
455+
456+
symBlockSize := symBlockSizeForEK(k.Public())
457+
if symBlockSize != 16 {
458+
t.Errorf("unexpected symBlockSize %d; expected 16", symBlockSize)
459+
}
460+
})
461+
462+
t.Run("ecdsa-P384", func(t *testing.T) {
463+
t.Parallel()
464+
465+
k, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
466+
if err != nil {
467+
t.Fatalf("unexpected error: %v", err)
468+
}
469+
470+
symBlockSize := symBlockSizeForEK(k.Public())
471+
if symBlockSize != 32 {
472+
t.Errorf("unexpected symBlockSize %d; expected 32", symBlockSize)
473+
}
474+
})
475+
476+
t.Run("ecdsa-P521", func(t *testing.T) {
477+
t.Parallel()
478+
479+
k, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
480+
if err != nil {
481+
t.Fatalf("unexpected error: %v", err)
482+
}
483+
484+
symBlockSize := symBlockSizeForEK(k.Public())
485+
if symBlockSize != 32 {
486+
t.Errorf("unexpected symBlockSize %d; expected 32", symBlockSize)
487+
}
488+
})
489+
}

0 commit comments

Comments
 (0)