@Alpha public final class X25519 extends Object
This implementation is based on curve255-donna.
Do not use this API or any other APIs including fields and methods marked with the @Alpha annotation. They can be modified in any way, or even removed, at any time. They are in the package, but not for official, production release, but only for testing.
Alice: byte[] privateKeyA = X25519.generatePrivateKey(); byte[] publicKeyA = X25519.publicFromPrivate(privateKeyA); Bob: byte[] privateKeyB = X25519.generatePrivateKey(); byte[] publicKeyB = X25519.publicFromPrivate(privateKeyB); Alice sends publicKeyA to Bob and Bob sends publicKeyB to Alice. Alice: byte[] sharedSecretA = X25519.computeSharedSecret(privateKeyA, publicKeyB); Bob: byte[] sharedSecretB = X25519.computeSharedSecret(privateKeyB, publicKeyA); such that sharedSecretA == sharedSecretB.
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
computeSharedSecret(byte[] privateKey,
byte[] peersPublicValue)
Returns the 32-byte shared key (i.e., privateKey·peersPublicValue on the curve).
|
static byte[] |
generatePrivateKey()
Returns a 32-byte private key for Curve25519.
|
static byte[] |
publicFromPrivate(byte[] privateKey)
Returns the 32-byte Diffie-Hellman public value based on the given
privateKey (i.e.,
privateKey·[9] on the curve). |
public static byte[] generatePrivateKey()
Note from BoringSSL: All X25519 implementations should decode scalars correctly (see https://tools.ietf.org/html/rfc7748#section-5). However, if an implementation doesn't then it might interoperate with random keys a fraction of the time because they'll, randomly, happen to be correctly formed.
Thus we do the opposite of the masking here to make sure that our private keys are never correctly masked and so, hopefully, any incorrect implementations are deterministically broken.
This does not affect security because, although we're throwing away entropy, a valid implementation of computeSharedSecret should throw away the exact same bits anyway.
public static byte[] computeSharedSecret(byte[] privateKey,
byte[] peersPublicValue)
throws InvalidKeyException
privateKey - 32-byte private keypeersPublicValue - 32-byte public valueInvalidKeyException - when privateKey is not 32-byte or peersPublicValue
is invalid.public static byte[] publicFromPrivate(byte[] privateKey)
throws InvalidKeyException
privateKey (i.e.,
privateKey·[9] on the curve).privateKey - 32-byte private keyInvalidKeyException - when the privateKey is not 32 bytes.