...
Package x25519
Package x25519 provides Diffie-Hellman functions as specified in RFC-7748.
Validation of public keys.
The Diffie-Hellman function, as described in RFC-7748 [1], works for any
public key. However, if a different protocol requires contributory
behaviour [2,3], then the public keys must be validated against low-order
points [3,4]. To do that, the Shared function performs this validation
internally and returns false when the public key is invalid (i.e., it
is a low-order point).
References:
▾ Example (X25519)
Code:
var AliceSecret, BobSecret,
AlicePublic, BobPublic,
AliceShared, BobShared Key
_, _ = io.ReadFull(rand.Reader, AliceSecret[:])
KeyGen(&AlicePublic, &AliceSecret)
_, _ = io.ReadFull(rand.Reader, BobSecret[:])
KeyGen(&BobPublic, &BobSecret)
okA := Shared(&AliceShared, &AliceSecret, &BobPublic)
okB := Shared(&BobShared, &BobSecret, &AlicePublic)
fmt.Println(AliceShared == BobShared && okA && okB)
Output:
true
Constants
Size is the length in bytes of a X25519 key.
const Size = 32
func KeyGen(public, secret *Key)
KeyGen obtains a public key given a secret key.
func Shared(shared, secret, public *Key) bool
Shared calculates Alice's shared key from Alice's secret key and Bob's
public key returning true on success. A failure case happens when the public
key is a low-order point, thus the shared key is all-zeros and the function
returns false.
Key represents a X25519 key.
type Key [Size]byte