Hi all,
I developed EC-Elgamal crypto schema, work fine till I use NIST
Prime-Curve, but when I try to work on NIST Binary-Curve crypted point
is egual to decrypted poit.
This is source code, pls help me :(
#include stdio.h
#include stdlib.h
#include string.h
#include ../e_os.h
#include openssl/opensslconf.h/* for OPENSSL_NO_ECDH */
#include openssl/crypto.h
#include openssl/bio.h
#include openssl/bn.h
#include openssl/objects.h
#include openssl/rand.h
#include openssl/sha.h
#include openssl/err.h
#ifdef OPENSSL_NO_ECDH
int main(int argc, char *argv[]) {
printf(No ECDH support\n);
return(0);
}
#else
#include openssl/ec.h
#include openssl/ecdh.h
static const char rnd_seed[] = 21o4h32rfon4d3ornou53gnwqpegbnng;
static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO
*out) {
EC_KEY *a=NULL;
EC_KEY *b=NULL;
BIGNUM *x_a=NULL, *y_a=NULL,
*x_b=NULL, *y_b=NULL;
int ret=0;
const EC_GROUP *group;
EC_POINT *M = NULL, *P = NULL, *R = NULL, *Q = NULL, *A = NULL, *B =
NULL;
a = EC_KEY_new_by_curve_name(nid);
b = EC_KEY_new_by_curve_name(nid);
if (a == NULL || b == NULL)
goto err;
group = EC_KEY_get0_group(a);
if ((x_a=BN_new()) == NULL) goto err;
if ((y_a=BN_new()) == NULL) goto err;
if ((x_b=BN_new()) == NULL) goto err;
if ((y_b=BN_new()) == NULL) goto err;
BIO_puts(out, Testing key generation with );
BIO_puts(out, text);
BIO_puts(out, \n);
if (!EC_KEY_generate_key(a)) goto err;
if (!EC_KEY_generate_key(b)) goto err;
P = EC_POINT_new(group);
Q = EC_POINT_new(group);
R = EC_POINT_new(group);
A = EC_POINT_new(group);
B = EC_POINT_new(group);
M = EC_POINT_new(group);
EC_POINT_copy(P, EC_KEY_get0_public_key(a));
EC_POINT_copy(Q, EC_KEY_get0_public_key(a));
EC_POINT_copy(R, EC_KEY_get0_public_key(a));
EC_POINT_copy(A, EC_KEY_get0_public_key(a));
EC_POINT_copy(B, EC_KEY_get0_public_key(a));
EC_POINT_copy(M, EC_KEY_get0_public_key(a));
/*
* Q = a * P
*/
EC_POINT_mul(group, Q, NULL, P, EC_KEY_get0_private_key(a), ctx);
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(group, P, x_a, y_a,
ctx)) goto err;
}else {
if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x_a, y_a,
ctx)) goto err;
}
BIO_printf(out, Point P (x,y): );
BN_print(out, x_a);
BIO_printf(out, ,);
BN_print(out, y_a);
BIO_printf(out, \nkey a:\n);
BIO_printf(out, private key: );
BN_print(out, EC_KEY_get0_private_key(a));
BIO_printf(out, \n);
BIO_printf(out, \nkey b:\n);
BIO_printf(out, private key: );
BN_print(out, EC_KEY_get0_private_key(b));
BIO_printf(out, \n);
/*
* Encrypting message P because message must be in E
*/
/*
* R = b * P
*/
EC_POINT_mul(group, R, NULL, P, EC_KEY_get0_private_key(b), ctx);
/*
* B = [b * a] * P
*/
EC_POINT_mul(group, B, NULL, Q, EC_KEY_get0_private_key(b), ctx);
/*
* B = P + [b * a] * P
*/
EC_POINT_add(group, B, P, B, ctx);
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(group, B, x_a, y_a,
ctx)) goto err;
}else {
if (!EC_POINT_get_affine_coordinates_GF2m(group, B, x_a, y_a,
ctx)) goto err;
}
BIO_printf(out, Encrypted Point P (x,y): );
BN_print(out, x_a);
BIO_printf(out, ,);
BN_print(out, y_a);
BIO_printf(out, \n);
/*
* Decrypting message B = (bP, P + abP)
*/
EC_POINT_mul(group, R, NULL, R, EC_KEY_get0_private_key(a), ctx);
EC_POINT_invert(group, R, ctx);
EC_POINT_add(group, B, B, R, ctx);
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(group, B, x_b, y_b,
ctx)) goto err;
}else {
if (!EC_POINT_get_affine_coordinates_GF2m(group, B, x_b, y_b,
ctx)) goto err;
}
BIO_printf(out, Decrypted point P (x,y): );
BN_print(out, x_b);
BIO_printf(out, ,);
BN_print(out, y_b);
BIO_printf(out, \n);
ret=1;
err:
ERR_print_errors_fp(stderr);
if (y_a) BN_free(y_a);
if (x_b) BN_free(x_b);
if (y_b) BN_free(y_b);
if (b) EC_KEY_free(b);
if (a) EC_KEY_free(a);
return(ret);
}
int main(int argc, char *argv[]) {
BN_CTX *ctx=NULL;
int ret=1;
BIO *out;
RAND_seed(rnd_seed, sizeof rnd_seed);
out=BIO_new(BIO_s_file());
FILE* fp;
if((fp=fopen(keys, w))==NULL) {
printf(Error in fopen!\n);
return 0;
}
if (out == NULL) EXIT(1);