Search code examples
javakotlincryptographyecdsaed25519

Generating ed25519 keys in Java/Kotlin


I am looking to generate ed25519 keys in java/kotlin. The key pair should be deterministic in that if one can provide some seed information used during the generation, they can generate the same pair again.

I understand one can achieve this using a mnemonic. Please advise me how to approach this. I am looking for a reputed library that can help me achieve this in java.


Solution

  • You should use libsodium, here's a link to the documentation.

    libsodium is the most actively maintained implementation of DJB's NaCl, thus if you are using ed25519 or curve25519 elliptic curve crypto, you should be using libsodium.

    There are a number of bindings in Java/Kotlin.

    It's really easy to use and it's safe, for example, it performs scalar multiplication in constant time.

    To answer your question on determinism, libsodium provides a mechanism to generate deterministic keys from a seed. Note, you need to ensure your seed has sufficient entropy to be secure.

    You should call int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, const unsigned char *seed); to generate an ed25519 keypair from a seed.

    Here is a library that provides a binding to that function in Java:

        /**
         * Deterministically generate a public and secret key.
         * Store the seed somewhere if you want to generate these
         * keys again.
         * @param publicKey Public key will be populated here of size {@link #PUBLICKEYBYTES}.
         * @param secretKey Secret key will be populated here of size {@link #SECRETKEYBYTES}.
         * @param seed A random seed of size {@link #SEEDBYTES}.
         * @return True if generated successfully.
         */
        boolean cryptoKxSeedKeypair(byte[] publicKey, byte[] secretKey, byte[] seed);