java.lang.Object
net.i2p.crypto.eddsa.EdDSAPrivateKey
All Implemented Interfaces:
Serializable, AsymmetricKey, BinaryEncodablePREVIEW, Key, PrivateKey, Destroyable, EdDSAKey

public class EdDSAPrivateKeyextends Object implements EdDSAKey, PrivateKey
An EdDSA private key.

For compatibility with older releases, decoding supports both RFC 8410 and an older draft specifications.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final byte[]
    The private scalar, the first 32 bytes of H.
    private final GroupElement
    The public key point.
    private final byte[]
    The encoded public key (Abyte).
    private final EdDSAParameterSpec
    Ed dsa spec.
    private final byte[]
    The 64-byte hash of the seed.
    private static final int
    Offset of the identifier length in the encoded key.
    private static final int
    Offset of the object identifier in the encoded key.
    private static final int
    Object identifier for Ed25519.
    private static final int
    First object identifier in the 1.3.101.xxx range.
    private final byte[]
    The 32-byte seed, or null if constructed from H.
    private static final long
    Pinned for serialized-form compatibility.

    Fields inherited from interface EdDSAKey

    KEY_ALGORITHM
    Modifier and Type
    Field
    Description
    static final String
    The reported key algorithm for all EdDSA keys.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create from PKCS#8.
    Unpacks a private key specification, encoding the public point as Abyte.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static byte[]
    decode(byte[] d)
    Extracts the private key bytes from the provided encoding.
    boolean
    Whether this key is equal to the given key.
    byte[]
    The private scalar, the first 32 bytes of H.
    The public key point.
    byte[]
    The encoded public key.
    The reported algorithm name for all EdDSA keys.
    byte[]
    Returns the private key in its canonical encoding.
    The encoding format of the key.
    byte[]
    The 64-byte hash of the seed.
    The EdDSA domain parameters for the key.
    byte[]
    The 32-byte seed.
    int
    Hash code of the seed, for use in hash tables.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface Destroyable

    destroy, isDestroyed
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Pinned for serialized-form compatibility.
      See Also:
    • seed

      private final byte[] seed
      The 32-byte seed, or null if constructed from H.
    • h

      private final byte[] h
      The 64-byte hash of the seed.
    • a

      private final byte[] a
      The private scalar, the first 32 bytes of H.
    • A

      private final GroupElement A
      The public key point.
    • Abyte

      private final byte[] Abyte
      The encoded public key (Abyte).
    • edDsaSpec

      private final EdDSAParameterSpec edDsaSpec
      Ed dsa spec.
    • OID_OLD

      private static final int OID_OLD
      First object identifier in the 1.3.101.xxx range.
      See Also:
    • OID_ED25519

      private static final int OID_ED25519
      Object identifier for Ed25519.
      See Also:
    • OID_BYTE

      private static final int OID_BYTE
      Offset of the object identifier in the encoded key.
      See Also:
    • IDLEN_BYTE

      private static final int IDLEN_BYTE
      Offset of the identifier length in the encoded key.
      See Also:
  • Constructor Details

  • Method Details

    • getAlgorithm

      public String getAlgorithm()
      The reported algorithm name for all EdDSA keys.
      Specified by:
      getAlgorithm in interface Key
      Returns:
      the algorithm name EdDSA, the same for every curve
    • getFormat

      public String getFormat()
      The encoding format of the key.
      Specified by:
      getFormat in interface Key
      Returns:
      the encoding name PKCS#8, matching getEncoded()
    • getEncoded

      public byte[] getEncoded()
      Returns the private key in its canonical encoding.

      This implements the following specs:

      • General encoding: https://tools.ietf.org/html/rfc8410
      • Key encoding: https://tools.ietf.org/html/rfc8032

      This encodes the seed. It will return null if constructed from a spec which was directly constructed from H, in which case seed is null.

      For keys in older formats, decoding and then re-encoding is sufficient to migrate them to the canonical encoding.

      Relevant spec quotes:

           OneAsymmetricKey ::= SEQUENCE {
             version Version,
             privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
             privateKey PrivateKey,
             attributes [0] IMPLICIT Attributes OPTIONAL,
             ...,
             [[2: publicKey [1] IMPLICIT PublicKey OPTIONAL ]],
             ...
           }
      
           Version ::= INTEGER
           PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
           PrivateKey ::= OCTET STRING
           PublicKey ::= BIT STRING
           Attributes ::= SET OF Attribute
      
           ... when encoding a OneAsymmetricKey object, the private key is wrapped
           in a CurvePrivateKey object and wrapped by the OCTET STRING of the
           "privateKey" field.
      
           CurvePrivateKey ::= OCTET STRING
      
           AlgorithmIdentifier  ::=  SEQUENCE  {
             algorithm   OBJECT IDENTIFIER,
             parameters  ANY DEFINED BY algorithm OPTIONAL
           }
      
           For all of the OIDs, the parameters MUST be absent.
      
           id-Ed25519   OBJECT IDENTIFIER ::= { 1 3 101 112 }
      
      Specified by:
      getEncoded in interface Key
      Returns:
      48 bytes for Ed25519, null for other curves
    • decode

      private static byte[] decode(byte[] d) throws InvalidKeySpecException
      Extracts the private key bytes from the provided encoding.

      This will decode data conforming to RFC 8410 or as inferred from the older draft spec at https://tools.ietf.org/html/draft-josefsson-pkix-eddsa-04.

      Per RFC 8410 section 3, this function WILL accept a parameter value of NULL, as it is required for interoperability with the default Java keystore. Other implementations MUST NOT copy this behaviour from here unless they also need to read keys from the default Java keystore.

      This is really dumb for now. It does not use a general-purpose ASN.1 decoder. See also getEncoded().

      Parameters:
      d - the DER-encoded private key bytes to decode
      Returns:
      32 bytes for Ed25519, throws for other curves
      Throws:
      InvalidKeySpecException - if the encoding is invalid or the curve is not Ed25519
    • getParams

      public EdDSAParameterSpec getParams()
      The EdDSA domain parameters for the key.
      Specified by:
      getParams in interface AsymmetricKey
      Specified by:
      getParams in interface EdDSAKey
      Returns:
      the curve parameters this key was built with, never null
    • getSeed

      public byte[] getSeed()
      The 32-byte seed.
      Returns:
      Will be null if constructed from a spec which was directly constructed from H.
    • getH

      public byte[] getH()
      The 64-byte hash of the seed.
      Returns:
      The hash of the seed.
    • geta

      public byte[] geta()
      The private scalar, the first 32 bytes of H.
      Returns:
      The private key.
    • getA

      public GroupElement getA()
      The public key point.
      Returns:
      The public key.
    • getAbyte

      public byte[] getAbyte()
      The encoded public key.
      Returns:
      The public key.
    • hashCode

      public int hashCode()
      Hash code of the seed, for use in hash tables.
      Overrides:
      hashCode in class Object
      Returns:
      whether h code is present
    • equals

      public boolean equals(Object o)
      Whether this key is equal to the given key.
      Overrides:
      equals in class Object
      Returns:
      true if equal