Hello list,
I have been trying to study the documentation on ECDSA, and header files for
ec.h but I have not managed to make it do what I need. I am currently trying
to convert some existing Java code (using bouncycastle) to C and OpenSSL.
Would it be possible to get someone who knows more than me to steer me along
the right path? Just throw me a bone or two!
My apologies that this email will be large..
--- Java cut down code ---
// define the curve parameters a, b and
q
EllipticCurve ec = new EllipticCurve(
new ECFieldFp( new BigInteger( constants.ECDSA_qStr, 16 ) )
,
new BigInteger( constants.ECDSA_aStr, 16 ),
new BigInteger( constants.ECDSA_bStr, 16
)
);
// define the base point on the
curve
ECPoint basePointP = new ECPoint(
new BigInteger( constants.ECDSA_xGStr, 16 ), //
x_G
new BigInteger( constants.ECDSA_ySGtr, 16 ) //
y_G
);
// setup the elliptic
curve
ECParameterSpec ecSpec = new ECParameterSpec(
ec,
basePointP,
new BigInteger( constants.ECDSA_nStr, 16 ), //
n
1
);
privKey = new BigInteger( constants.ECDSA_dStr_0, 16 );
ECPrivateKeySpec privKeySpec = new ECPrivateKeySpec(
privKey, //
d_A
ecSpec
);
Signature ecdsa = Signature.getInstance("ECDSA","BC");
KeyFactory fact = KeyFactory.getInstance("ECDSA","BC");
PrivateKey ecPrivKey = fact.generatePrivate(privKeySpec);
ecdsa.initSign( ecPrivKey );
ecdsa.update( message ); // message is uint8 of lensize+16
byte[] sig = ecdsa.sign();
----- end of Java version -----
My first attempt at C version is as follows, showing no freeing of
variables, or error checking etc. It is clearly wrong, but compiles.
----- C OpenSSL version -----
q = BN_new();
a = BN_new();
b = BN_new();
x = BN_new();
y = BN_new();
n = BN_new();
d0_A = BN_new();
x0_Q = BN_new();
y0_Q = BN_new();
BN_hex2bn(&q, "9660........................"); // Big hex number here
20bytes
// BN_hex2bn() calls for a,b,x,y,n,d0,x0,y0
ec = EC_GROUP_new_curve_GFp(q, a, b, ctx);
g = EC_POINT_new(ec);
EC_POINT_set_affine_coordinates_GFp(ec, n, x, y, ctx); // n?
printf("point is on curve: %d\n", EC_POINT_is_on_curve(ec, g, ctx));
privKey = d0_A;
pk = EC_KEY_new();
k = EC_KEY_new();
EC_KEY_set_private_key(pk, privKey);
EC_KEY_set_group(pk, ec);
EC_KEY_generate_key(k); // Whats
this?
ECDSA_sign(0, message, srcLen + 16,
sig, &siglen, pk);
printf("signature is %d long\n", siglen);
----- end of C OpenSSL version ----
Output is "point is on curve: 1" and "signature is 0 long".
Any help is much appreciated!