| 1 |
package tpm2test |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ecdh" |
| 5 |
"crypto/rand" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/google/go-cmp/cmp" |
| 9 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 10 |
. "github.com/google/go-tpm/tpm2" |
| 11 |
"github.com/google/go-tpm/tpm2/transport/simulator" |
| 12 |
) |
| 13 |
|
| 14 |
func TestECDH(t *testing.T) { |
| 15 |
thetpm, err := simulator.OpenSimulator() |
| 16 |
if err != nil { |
| 17 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 18 |
} |
| 19 |
defer thetpm.Close() |
| 20 |
|
| 21 |
// Create a TPM ECDH key |
| 22 |
tpmCreate := CreatePrimary{ |
| 23 |
PrimaryHandle: TPMRHOwner, |
| 24 |
InPublic: New2B(TPMTPublic{ |
| 25 |
Type: TPMAlgECC, |
| 26 |
NameAlg: TPMAlgSHA256, |
| 27 |
ObjectAttributes: TPMAObject{ |
| 28 |
FixedTPM: true, |
| 29 |
STClear: false, |
| 30 |
FixedParent: true, |
| 31 |
SensitiveDataOrigin: true, |
| 32 |
UserWithAuth: true, |
| 33 |
AdminWithPolicy: false, |
| 34 |
NoDA: true, |
| 35 |
EncryptedDuplication: false, |
| 36 |
Restricted: false, |
| 37 |
Decrypt: true, |
| 38 |
SignEncrypt: false, |
| 39 |
X509Sign: false, |
| 40 |
}, |
| 41 |
Parameters: NewTPMUPublicParms( |
| 42 |
TPMAlgECC, |
| 43 |
&TPMSECCParms{ |
| 44 |
CurveID: TPMECCNistP256, |
| 45 |
Scheme: TPMTECCScheme{ |
| 46 |
Scheme: TPMAlgECDH, |
| 47 |
Details: NewTPMUAsymScheme( |
| 48 |
TPMAlgECDH, |
| 49 |
&TPMSKeySchemeECDH{ |
| 50 |
HashAlg: TPMAlgSHA256, |
| 51 |
}, |
| 52 |
), |
| 53 |
}, |
| 54 |
}, |
| 55 |
), |
| 56 |
}), |
| 57 |
} |
| 58 |
|
| 59 |
// Use NIST P-256 |
| 60 |
curve := ecdh.P256() |
| 61 |
|
| 62 |
tpmCreateRsp, err := tpmCreate.Execute(thetpm) |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("could not create the TPM key: %v", err) |
| 65 |
} |
| 66 |
outPub, err := tpmCreateRsp.OutPublic.Contents() |
| 67 |
if err != nil { |
| 68 |
t.Fatalf("%v", err) |
| 69 |
} |
| 70 |
tpmPub, err := outPub.Unique.ECC() |
| 71 |
if err != nil { |
| 72 |
t.Fatalf("%v", err) |
| 73 |
} |
| 74 |
eccParms, err := outPub.Parameters.ECCDetail() |
| 75 |
if err != nil { |
| 76 |
t.Fatalf("%v", err) |
| 77 |
} |
| 78 |
tpmPubKey, err := ECDHPub(eccParms, tpmPub) |
| 79 |
if err != nil { |
| 80 |
t.Fatalf("could not unmarshal pubkey: %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
// Create a SW ECDH key |
| 84 |
swPriv, err := curve.GenerateKey(rand.Reader) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("could not create the SW key: %v", err) |
| 87 |
} |
| 88 |
x, y, err := ECCPoint(swPriv.PublicKey()) |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("could not get SW key point: %v", err) |
| 91 |
} |
| 92 |
swPub := TPMSECCPoint{ |
| 93 |
X: TPM2BECCParameter{Buffer: x.FillBytes(make([]byte, 32))}, |
| 94 |
Y: TPM2BECCParameter{Buffer: y.FillBytes(make([]byte, 32))}, |
| 95 |
} |
| 96 |
|
| 97 |
// Calculate Z based on the SW priv * TPM pub |
| 98 |
zx, err := swPriv.ECDH(tpmPubKey) |
| 99 |
if err != nil { |
| 100 |
t.Fatalf("ecdh exchange: %v", err) |
| 101 |
} |
| 102 |
|
| 103 |
z := TPMSECCPoint{ |
| 104 |
X: TPM2BECCParameter{Buffer: zx}, |
| 105 |
} |
| 106 |
|
| 107 |
// Calculate Z based on TPM priv * SW pub |
| 108 |
ecdh := ECDHZGen{ |
| 109 |
KeyHandle: AuthHandle{ |
| 110 |
Handle: tpmCreateRsp.ObjectHandle, |
| 111 |
Name: tpmCreateRsp.Name, |
| 112 |
Auth: PasswordAuth(nil), |
| 113 |
}, |
| 114 |
InPoint: New2B(swPub), |
| 115 |
} |
| 116 |
ecdhRsp, err := ecdh.Execute(thetpm) |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("ECDH_ZGen failed: %v", err) |
| 119 |
} |
| 120 |
|
| 121 |
outPoint, err := ecdhRsp.OutPoint.Contents() |
| 122 |
if err != nil { |
| 123 |
t.Fatalf("%v", err) |
| 124 |
} |
| 125 |
if !cmp.Equal(z.X, outPoint.X, cmpopts.IgnoreUnexported(z.X)) { |
| 126 |
t.Errorf("want %x got %x", z, outPoint) |
| 127 |
} |
| 128 |
} |