Debian Codegraph @

tpm2/labeled_kem_test.go

raw
1 package tpm2
2
3 // Note: this unit test is in the tpm2 package instead of in the tpm2test package.
4 // This is to allow calling encapsulateDerandomized from the test without exporting it.
5
6 import (
7 "bytes"
8 "testing"
9
10 "github.com/google/go-tpm/tpm2/test/testvectors"
11 )
12
13 func TestECCLabeledEncapsulation(t *testing.T) {
14 for _, testcase := range testvectors.ECCLabeledEncapsulation(t) {
15 t.Run(testcase.Name, func(t *testing.T) {
16 pub, err := Unmarshal[TPMTPublic](testcase.PublicKey)
17 if err != nil {
18 t.Fatalf("Unmarshal() = %v", err)
19 }
20 encapsPub, err := importECCEncapsulationKey(pub)
21 if err != nil {
22 t.Fatalf("importECCEncapsulationKey() = %v", err)
23 }
24 ephPriv, err := encapsPub.eccPub.Curve().NewPrivateKey(testcase.EphemeralPrivate)
25 if err != nil {
26 t.Fatalf("NewPrivateKey() = %v", err)
27 }
28 secret, ciphertext, err := encapsPub.encapsulateDerandomized(ephPriv, testcase.Label)
29 if err != nil {
30 t.Fatalf("encapsulateDerandomized() = %v", err)
31 }
32
33 if !bytes.Equal(testcase.Secret, secret) {
34 t.Errorf("want %x got %x", testcase.Secret, secret)
35 }
36 if !bytes.Equal(testcase.Ciphertext, ciphertext) {
37 t.Errorf("want %x got %x", testcase.Ciphertext, ciphertext)
38 }
39 })
40 }
41 }
42
43 func TestRSALabeledEncapsulation(t *testing.T) {
44 for _, testcase := range testvectors.RSALabeledEncapsulation(t) {
45 t.Run(testcase.Name, func(t *testing.T) {
46 pub, err := Unmarshal[TPMTPublic](testcase.PublicKey)
47 if err != nil {
48 t.Fatalf("Unmarshal() = %v", err)
49 }
50 encapsPub, err := importRSAEncapsulationKey(pub)
51 if err != nil {
52 t.Fatalf("importRSAEncapsulationKey() = %v", err)
53 }
54
55 ciphertext, err := encapsPub.encapsulateDerandomized(bytes.NewReader(testcase.OAEPSalt), testcase.Secret, testcase.Label)
56 if err != nil {
57 t.Fatalf("encapsulateDerandomized() = %v", err)
58 }
59
60 if !bytes.Equal(testcase.Ciphertext, ciphertext) {
61 t.Errorf("want %x got %x", testcase.Ciphertext, ciphertext)
62 }
63 })
64 }
65 }