0

I'm trying to make a ECDSA signature using the ed25519 curve in Java, using the Bouncy Castle library. I have managed to generate the keys and to be able to obtain the public key given the private key. However, I'm struggling to sign some data with it. When I try to do so it says java.security.SignatureException: Curve not supported: java.security.spec.ECParameterSpec@223191a6. I suppose that's because I initialized the curve parameters using CustomNamedCurves. Does somebody know how to make this work?

        // Bouncy Castle Provider
        Security.addProvider(new BouncyCastleProvider());
        
        // Secure random
        SecureRandom secureRandom = new SecureRandom();

        // Key generator
        X9ECParameters curveParams = CustomNamedCurves.getByName("Curve25519");
        ECParameterSpec ecSpec = new ECParameterSpec(curveParams.getCurve(), curveParams.getG(), curveParams.getN(), curveParams.getH(), curveParams.getSeed());
        KeyPairGenerator ecKeyGen = KeyPairGenerator.getInstance("EC", "BC");
        ecKeyGen.initialize(ecSpec);
        ecKeyGen.initialize(ecSpec, secureRandom);
        ecKeyGen.initialize(ecSpec);

        // Signer
        Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");

        // Generate Key pair
        var keypair = ecKeyGen.generateKeyPair();
        var pub = keypair.getPublic();
        var priv = keypair.getPrivate();

        // Signature
        byte[] data = "Some data".getBytes();
        ecdsaSign.initSign(priv);
        ecdsaSign.update(data);
        // java.security.SignatureException: Curve not supported: java.security.spec.ECParameterSpec@6069db5
        byte[] sig = ecdsaSign.sign();

I'm using java 17.0.1 and org.bouncycastle:bcprov-jdk15on:1.70

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • You don't need BouncyCastle for Ed25519. Java supports Ed25519 since Java 15. You can find a code example in [JEP 339](https://openjdk.java.net/jeps/339), section *Description*. But of course BouncyCastle is another option. You can find an example in the [BC source code](https://github.com/bcgit/bc-java/blob/master/core/src/test/java/org/bouncycastle/crypto/test/Ed25519Test.java#L91). Your implementation is not compatible with Ed25519, see also [this post](https://stackoverflow.com/q/70693834/9014097) (though it's for ECDH). – Topaco Mar 17 '22 at 11:32

0 Answers0