'Elliptic Curve DSA on JavaCard - what else do I need to initialise?

I am trying to implement ECDSA on JavaCard. So far I have this code:

Signature sig = Signature.getInstance(Signature.ALG_ECDSA_SHA_256, false);
KeyPair key = new KeyPair(KeyPair.ALG_EC_FP, (short)256);
key.genKeyPair();
sig.init(key.Private(), Signature.MODE_SIGN);
sig.sign(data, (short)0, dataLen, outputBuf, (short)0);

When I try to retire K using the getK() method so that I can send the public key in an APDU I get the error code 6F 00.

According to the documentation getK() can throw CryptoException.UNINITIALIZED_KEY if the 'cofactor of the order of the fixed point G of the curve of the key has not been successfully initialized'

Is there something else I need to initialise when setting up the keys/signature?



Solution 1:[1]

You need to initialise the domain parameters: A,B, G, R, K and the field. For a list of NIST recommended curves you can look here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LozCodes