#include <limits.h>
#include <openssl/ec.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <openssl/ecdsa.h>
#include <openssl/ecdh.h>
#include <openssl/obj_mac.h>
extern char *param_p;
extern char *param_a;
extern char *param_b;
extern char *param_gx;
extern char *param_gy;
extern char *param_n;
extern char *param_pri;
extern char *param_sign_k;
extern int ECC_Y_BIT;
extern void BNPrintf(BIGNUM* bn);
static const int KDF1_SHA1_len = 20;
static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
{
return SHA1(in, inlen, out);
}
int test_ecdh_curve(void)
{
BN_CTX *ctx = NULL;
BIGNUM *p = NULL;
BIGNUM *a = NULL;
BIGNUM *b = NULL;
BIGNUM *x = NULL;
BIGNUM *y = NULL;
BIGNUM *x_a = NULL;
BIGNUM *y_a = NULL;
BIGNUM *x_b = NULL;
BIGNUM *y_b = NULL;
BIGNUM *order = NULL;
EC_GROUP *group = NULL ;
EC_POINT *P = NULL;
EC_POINT *Q = NULL;
EC_POINT *R = NULL;
EC_KEY *eckey_a = NULL;
EC_KEY *eckey_b = NULL;
unsigned char *abuff = NULL;
unsigned char *bbuff = NULL;
int iret = 0;
ctx = BN_CTX_new();
if (NULL == ctx)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
return 0;
}
p = BN_new();
if (NULL == p)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
a = BN_new();
if (NULL == a)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
b = BN_new();
if (NULL == b)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
* so that the library gets to choose the EC_METHOD */
if (NULL == group)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!BN_hex2bn(&p, param_p))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!BN_hex2bn(&a, param_a))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!BN_hex2bn(&b, param_b))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
P = EC_POINT_new(group);
if (NULL == P)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
Q = EC_POINT_new(group);
if (NULL == Q)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
R = EC_POINT_new(group);
if (NULL == R)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
x = BN_new();
if (NULL == x)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
y = BN_new();
if (NULL == y)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
x_a = BN_new();
if (NULL == x_a)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
y_a = BN_new();
if (NULL == y_a)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
x_b = BN_new();
if (NULL == x_b)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
y_b = BN_new();
if (NULL == y_b)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
order = BN_new();
if (NULL == order)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!BN_hex2bn(&x, param_gx))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, ECC_Y_BIT, ctx))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!EC_POINT_is_on_curve(group, P, ctx))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!BN_hex2bn(&order, param_n))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!EC_GROUP_set_generator(group, P, order, BN_value_one()))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
fprintf(stdout, "\n Generator:\n x = 0x");
BNPrintf(x);
fprintf(stdout, "\n y = 0x");
BNPrintf(y);
fprintf(stdout, "\n");
if ((eckey_a = EC_KEY_new()) == NULL)
{
goto errhandl;
}
if (EC_KEY_set_group(eckey_a, group) == 0)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!EC_KEY_generate_key(eckey_a))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if ((eckey_b = EC_KEY_new()) == NULL)
{
goto errhandl;
}
if (EC_KEY_set_group(eckey_b, group) == 0)
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!EC_KEY_generate_key(eckey_b))
{
printf("[%s %d] failed\n", __FILE__, __LINE__);
goto errhandl;
}
if (!EC_POINT_get_affine_coordinates_GFp(group, EC_KEY_get0_public_key(eckey_a), x_a, y_a, ctx))
goto errhandl;
fprintf(stdout, "\n ECKEY a:\n x = 0x");
BNPrintf(x_a);
fprintf(stdout, "\n y = 0x");
BNPrintf(y_a);
fprintf(stdout, "\n");
if (!EC_POINT_get_affine_coordinates_GFp(group, EC_KEY_get0_public_key(eckey_b), x_b, y_b, ctx))
goto errhandl;
fprintf(stdout, "\n ECKEY b:\n x = 0x");
BNPrintf(x_b);
fprintf(stdout, "\n y = 0x");
BNPrintf(y_b);
fprintf(stdout, "\n");
int alen = KDF1_SHA1_len;
abuff = (unsigned char *)OPENSSL_malloc(alen);
int aout =ECDH_compute_key(abuff, alen, EC_KEY_get0_public_key(eckey_b), eckey_a, KDF1_SHA1);
printf( "key1 =");
int i = 0;
for (i = 0; i < aout; i++)
{
printf("%02X", abuff[i]);
}
printf("\n");
int blen = KDF1_SHA1_len;
bbuff = (unsigned char *)OPENSSL_malloc(blen);
int bout =ECDH_compute_key(bbuff, blen, EC_KEY_get0_public_key(eckey_a), eckey_b, KDF1_SHA1);
printf("key2 =");
for (i = 0; i < bout; i++)
{
printf("%02X", abuff[i]);
}
printf("\n");
if ((aout < 4) || (bout != aout) || (memcmp(abuff, bbuff, aout) != 0))
{
printf("key a:\n");
printf("private key: ");
BNPrintf(EC_KEY_get0_private_key(a));
printf("\n");
printf("public key (x,y): ");
BNPrintf(x);
printf( ",");
BNPrintf(y_a);
printf("\nkey b:\n");
printf("private key: ");
BNPrintf(EC_KEY_get0_private_key(b));
printf("\n");
printf("public key (x,y): ");
BNPrintf(x_b);
printf( ",");
BNPrintf(y_b);
printf("\n");
}
errhandl:
EC_POINT_free(P);
EC_POINT_free(Q);
EC_POINT_free(R);
}
其中:
void BNPrintf(BIGNUM* bn)
{
char *p=NULL;
p=BN_bn2hex(bn);
printf("%s",p);
OPENSSL_free(p);
}
char *param_p = NULL;
char *param_a = NULL;
char *param_b = NULL;
char *param_gx = NULL;
char *param_gy = NULL;
char *param_n = NULL;
char *param_pri = NULL;
char *param_sign_k = NULL;
#define ECC_256R1_p "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF";
#define ECC_256R1_a "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC";
#define ECC_256R1_b "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B";
#define ECC_256R1_gx "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
#define ECC_256R1_gy "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
#define ECC_256R1_n "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551";
#define ECC_256R1_pri "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
#define ECC_256R1_sign_k "6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F";
int ECC_Y_BIT = 0;
void SetParamFlag(int flag)
{
if (1 == flag)
{
param_p = SM2_256_p;
param_a = SM2_256_a;
param_b = SM2_256_b;
param_gx = SM2_256_gx;
param_gy = SM2_256_gy;
param_n = SM2_256_n;
param_pri = SM2_256_pri;
param_sign_k = SM2_256_sign_k;
ECC_Y_BIT = 0;
}
else if (2 == flag)
{
param_p = ECC_256R1_p;
param_a = ECC_256R1_a;
param_b = ECC_256R1_b;
param_gx = ECC_256R1_gx;
param_gy = ECC_256R1_gy;
param_n = ECC_256R1_n;
param_pri = ECC_256R1_pri;
param_sign_k = ECC_256R1_sign_k;
ECC_Y_BIT = 1;
}
else
{
param_p = OTHER_EC_p;
param_a = OTHER_EC_a;
param_b = OTHER_EC_b;
param_gx = OTHER_EC_gx;
param_gy = OTHER_EC_gy;
param_n = OTHER_EC_n;
param_pri = OTHER_EC_pri;
param_sign_k = OTHER_EC_sign_k;
ECC_Y_BIT = 0;
}
}