Class Ed25519

java.lang.Object
com.iizix.license.v2.Ed25519

public final class Ed25519 extends Object
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 Details

    • ALGORITHM

      public static final String ALGORITHM
      The JCA algorithm name: "Ed25519".
      See Also:
    • PUBLIC_KEY_LENGTH

      public static final int PUBLIC_KEY_LENGTH
      The length of an encoded public key: 32 bytes.
      See Also:
    • SEED_LENGTH

      public static final int SEED_LENGTH
      The length of a private key seed: 32 bytes.
      See Also:
    • SIGNATURE_LENGTH

      public static final int SIGNATURE_LENGTH
      The length of a signature: 64 bytes.
      See Also:
  • Method Details

    • generateKeyPair

      public static KeyPair generateKeyPair()
      Generates a key pair.
      Returns:
      A new pair.
    • privateKeyFromSeed

      public static PrivateKey privateKeyFromSeed(byte[] seed)
      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

      public static byte[] seed(PrivateKey key)
      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

      public 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.
      Parameters:
      key - The public key.
      Returns:
      The 32 bytes.
      Throws:
      IllegalArgumentException - If the key is not an Ed25519 key.
    • decodePublicKey

      public static PublicKey decodePublicKey(byte[] encoded)
      Decodes a public key from its 32-byte form.
      Parameters:
      encoded - The 32 bytes of encodePublicKey(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

      public static String publicKeyToBase64Url(PublicKey key)
      Encodes a public key as text: the 32 bytes of encodePublicKey(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

      public static PublicKey publicKeyFromBase64Url(String text)
      Decodes a public key from its text.
      Parameters:
      text - The text of publicKeyToBase64Url(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

      public static byte[] sign(PrivateKey key, byte[] message)
      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

      public static boolean verify(PublicKey key, byte[] message, byte[] signature)
      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 of decodePublicKey(byte[]) always is).