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

Add ECDSAPrivateKey.ECDH which takes a *ecdh.PublicKey #158

Merged
merged 1 commit into from
Oct 2, 2024

Conversation

AGWA
Copy link
Contributor

@AGWA AGWA commented Oct 2, 2024

The current ECDSAPrivateKey.SharedKey method takes a *ecdsa.PublicKey. However, using crypto/ecdsa and crypto/elliptic for ECDH has been deprecated in the standard library in favor of crypto/ecdh.

This commit adds a new ECDH method to ECDSAPrivateKey which takes a *ecdh.PublicKey. This method has the same signature as ecdh.PrivateKey.ECDH, meaning the following interface can be be used to do ECDH with both standard library private keys and piv-go keys, providing the same flexibility as crypto.Signer and crypto.Decrypter:

interface {
	ECDH(*ecdh.PublicKey) ([]byte, error)
}

ECDSAPrivateKey.SharedKey has been re-implemented as a small wrapper around ECDSAPrivateKey.ECDH. It has not been marked as deprecated, but it could be.

The current ECDSAPrivateKey.SharedKey method takes a *ecdsa.PublicKey.
However, using crypto/ecdsa and crypto/elliptic for ECDH has been
deprecated in the standard library in favor of crypto/ecdh.

This commit adds a new ECDH method to ECDSAPrivateKey which takes
a *ecdh.PublicKey.  This method has the same signature
as ecdh.PrivateKey.ECDH, meaning the following interface can be be used
to do ECDH with both standard library private keys and piv-go keys, providing
the same flexibility as crypto.Signer and crypto.Decrypter:

interface {
	ECDH(*ecdh.PublicKey) ([]byte, error)
}

ECDSAPrivateKey.SharedKey has been re-implemented as a small wrapper around
ECDSAPrivateKey.ECDH.
Copy link
Collaborator

@ericchiang ericchiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@ericchiang ericchiang merged commit 7988525 into go-piv:v2 Oct 2, 2024
4 checks passed
@AGWA AGWA deleted the ecdh branch October 2, 2024 16:04
@maraino
Copy link
Contributor

maraino commented Oct 2, 2024

It's quite easy to convert on both directions crypto/ecdh key to crypto/elliptic. See, for example, this, https://github.com/smallstep/crypto/blob/47febcd8eeae3b35809606fe1f148babe38dc4e0/kms/mackms/mackms.go#L1184-L1208

We can use a method like this to implement ECDH(*ecdh.PublicKey), converting the public key to ecdsa.PublicKey and use SharedKey to perform ECDH.

Broader support in PrivateKey and SetPrivateKeyInsecure is possible too, for example, if we pass as ecdh.P256 key to PrivateKey we can return a type that implements ECDH and returns the ecdh.PublicKey in the Public() crypto.PublicKey method. We can also implement Curve() ecdh.Curve or PublicKey() *ecdh.PublicKey but methods like Equal(x crypto.PrivateKey) bool will be harder to support, we can compare the public keys and assume there are no collisions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants