Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify ECC public key helpers on ECDHPub and ECDSAPub #380

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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