Skip to content

Commit

Permalink
Unify ECC public key helpers on ECDHPub and ECDSAPub (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfenner authored Dec 16, 2024
1 parent 49968d8 commit 6208188
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
24 changes: 0 additions & 24 deletions tpm2/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,6 @@ func ECDHPub(parms *TPMSECCParms, pub *TPMSECCPoint) (*ecdh.PublicKey, error) {
return pubKey.ECDH()
}

// ECDHPubKey converts a TPM ECC public key into one recognized by the ecdh package
func ECDHPubKey(curve ecdh.Curve, pub *TPMSECCPoint) (*ecdh.PublicKey, error) {

var c elliptic.Curve
switch curve {
case ecdh.P256():
c = elliptic.P256()
case ecdh.P384():
c = elliptic.P384()
case ecdh.P521():
c = elliptic.P521()
default:
return nil, fmt.Errorf("unknown curve: %v", curve)
}

pubKey := ecdsa.PublicKey{
Curve: c,
X: big.NewInt(0).SetBytes(pub.X.Buffer),
Y: big.NewInt(0).SetBytes(pub.Y.Buffer),
}

return pubKey.ECDH()
}

// ECCPoint returns an uncompressed ECC Point
func ECCPoint(pubKey *ecdh.PublicKey) (*big.Int, *big.Int, error) {
b := pubKey.Bytes()
Expand Down
4 changes: 2 additions & 2 deletions tpm2/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@ func getEncryptedSaltECC(nameAlg TPMIAlgHash, parms *TPMSECCParms, pub *TPMSECCP
if err != nil {
return nil, nil, fmt.Errorf("ecc salt: param curve: %w", err)
}
eccPub, err := ECDHPubKey(curve, pub)
eccPub, err := ECDHPub(parms, pub)
if err != nil {
return nil, nil, fmt.Errorf("ecc salt: unmarshalling tpm ecc key: %w", err)
return nil, nil, fmt.Errorf("ecc salt: unmarshaling tpm ecc key: %w", err)
}

// Generate new ECDH key
Expand Down
8 changes: 6 additions & 2 deletions tpm2/test/ecdh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ func TestECDH(t *testing.T) {
if err != nil {
t.Fatalf("%v", err)
}
tpmPubKey, err := ECDHPubKey(curve, tpmPub)
eccParms, err := outPub.Parameters.ECCDetail()
if err != nil {
t.Fatalf("could not unmarshall pubkey: %v", err)
t.Fatalf("%v", err)
}
tpmPubKey, err := ECDHPub(eccParms, tpmPub)
if err != nil {
t.Fatalf("could not unmarshal pubkey: %v", err)
}

// Create a SW ECDH key
Expand Down

0 comments on commit 6208188

Please sign in to comment.