| 1 | // Use of this source code is governed by a license |
| 2 | // that can be found in the LICENSE file. |
| 3 | |
| 4 | // Package ecdh implements the Diffie-Hellman key exchange |
| 5 | // with elliptic curves. This package directly implements |
| 6 | // generic curves imlementing elliptic.Curve |
| 7 | // (the NIST curves P224, P256, P384 and P521) |
| 8 | // and Bernstein's Curve25519. Other curves can be used |
| 9 | // by implementing the KeyExchange interface. |
| 10 | // |
| 11 | // For generic curves this implementation of ECDH |
| 12 | // only uses the x-coordinate as the computed secret. |
| 13 | package ecdh |
| 14 | |
| 15 | import "io" |
| 16 | |
| 17 | // PublicKey is the type of ECDH public keys. |
| 18 | type PublicKey []byte |
| 19 | |
| 20 | // PrivateKey is the type of ECDH private keys. |
| 21 | type PrivateKey []byte |
| 22 | |
| 23 | // KeyExchange is the interface defining all functions |
| 24 | // necessary for ECDH. |
| 25 | type KeyExchange interface { |
| 26 | // GenerateKey generates a private/public key pair using entropy from rand. |
| 27 | // If rand is nil, crypto/rand.Reader will be used. |
| 28 | GenerateKey(rand io.Reader) (private PrivateKey, public PublicKey, err error) |
| 29 | |
| 30 | // PublicKey returns the public key corresponding to the given private one. |
| 31 | PublicKey(private PrivateKey) (public PublicKey) |
| 32 | |
| 33 | // Check returns a non-nil error if the peers public key cannot used for the |
| 34 | // key exchange (e.g. the public key isn't a point of the elliptic curve) |
| 35 | // It's recommended to check peer's public key before computing the secret. |
| 36 | Check(peersPublic PublicKey) (err error) |
| 37 | |
| 38 | // ComputeSecret returns the secret value computed from the given private key |
| 39 | // and the peers public key. |
| 40 | ComputeSecret(private PrivateKey, peersPublic PublicKey) (secret []byte) |
| 41 | } |