Class Ed25519
java.lang.Object
com.iizix.license.v2.Ed25519
Ed25519 for the licence formats of
LICENSE-V2-DESIGN.md (sections 2.3 and 2.4, ruling LV-2): the JDK's Signature.getInstance("Ed25519") of java.base (RFC 8032, in the JDK since 15), so the mangled licence client, the MLS and the hub need no library. Nothing in the tree signed with Ed25519 before this class; the Push Notification keys are ECDSA P-256 in com.iizix.server, which the licence client cannot reach (design 2.1).Key text. A public key travels as the 32-byte encoding of RFC 8032 section 5.1.2 (the y coordinate little-endian, the low bit of x in the top bit of the last byte), base64url without padding (publicKeyToBase64Url(PublicKey)): the ikey of a licence document, the key of a key certificate, a pinned root. A private key is its 32-byte seed (seed(PrivateKey), privateKeyFromSeed(byte[])); where a seed is kept is its owner's business (the local licence file, LV-5; the MLS's PKCS#8 file, 2.4).
A signature is 64 bytes. Signing is deterministic: the same key and message always give the same signature, which the RFC 8032 section 7.1 vectors test relies on.
- Author:
- Christopher Mindus
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe JCA algorithm name: "Ed25519".static final intThe length of an encoded public key: 32 bytes.static final intThe length of a private key seed: 32 bytes.static final intThe length of a signature: 64 bytes.Method Summary
Modifier and TypeMethodDescriptionstatic PublicKeydecodePublicKey(byte[] encoded) Decodes a public key from its 32-byte form.static byte[]encodePublicKey(PublicKey key) Encodes a public key as RFC 8032 section 5.1.2 does: 32 bytes, the y coordinate little-endian with the low bit of x in the top bit of the last byte.static KeyPairGenerates a key pair.static PrivateKeyprivateKeyFromSeed(byte[] seed) Loads a private key from its seed.static PublicKeypublicKeyFromBase64Url(String text) Decodes a public key from its text.static StringEncodes a public key as text: the 32 bytes ofencodePublicKey(PublicKey), base64url without padding.static byte[]seed(PrivateKey key) Gets the seed of a private key.static byte[]sign(PrivateKey key, byte[] message) Signs a message.static booleanVerifies a signature.
Field Details
ALGORITHM
PUBLIC_KEY_LENGTH
public static final int PUBLIC_KEY_LENGTHThe length of an encoded public key: 32 bytes.- See Also:
SEED_LENGTH
public static final int SEED_LENGTHThe length of a private key seed: 32 bytes.- See Also:
SIGNATURE_LENGTH
public static final int SIGNATURE_LENGTHThe length of a signature: 64 bytes.- See Also:
Method Details
generateKeyPair
privateKeyFromSeed
Loads a private key from its seed.- Parameters:
seed- The 32-byte seed.- Returns:
- The private key.
- Throws:
IllegalArgumentException- If the seed is not 32 bytes.
seed
Gets the seed of a private key.- Parameters:
key- The private key.- Returns:
- A copy of its 32-byte seed.
- Throws:
IllegalArgumentException- If the key is not an Ed25519 key or does not expose its seed.
encodePublicKey
Encodes a public key as RFC 8032 section 5.1.2 does: 32 bytes, the y coordinate little-endian with the low bit of x in the top bit of the last byte.- Parameters:
key- The public key.- Returns:
- The 32 bytes.
- Throws:
IllegalArgumentException- If the key is not an Ed25519 key.
decodePublicKey
Decodes a public key from its 32-byte form.- Parameters:
encoded- The 32 bytes ofencodePublicKey(PublicKey).- Returns:
- The public key, a point on the curve.
- Throws:
IllegalArgumentException- If the length is not 32, the y coordinate is not below the field prime, or the bytes are not a point on the curve.
publicKeyToBase64Url
Encodes a public key as text: the 32 bytes ofencodePublicKey(PublicKey), base64url without padding.- Parameters:
key- The public key.- Returns:
- The text, 43 characters.
- Throws:
IllegalArgumentException- If the key is not an Ed25519 key.
publicKeyFromBase64Url
Decodes a public key from its text.- Parameters:
text- The text ofpublicKeyToBase64Url(PublicKey).- Returns:
- The public key.
- Throws:
IllegalArgumentException- If the text is not canonical base64url of 32 bytes, or the bytes are not a point on the curve.
sign
Signs a message. Deterministic: the same key and message give the same signature.- Parameters:
key- The private key.message- The message.- Returns:
- The 64-byte signature.
- Throws:
IllegalArgumentException- If the key is not an Ed25519 private key.
verify
Verifies a signature. A signature of the wrong length or one the algorithm rejects as malformed is simply false, never an exception.- Parameters:
key- The public key.message- The message.signature- The signature, may be null.- Returns:
- true when the signature is the key's signature over the message, false otherwise.
- Throws:
IllegalArgumentException- If the key is not an Ed25519 public key or not a point on the curve (a key ofdecodePublicKey(byte[])always is).