您的位置:首页 > 其它

SM2算法第十四篇:ECDH秘钥交换的C程序

2016-05-19 16:28 1366 查看
参考:ECDH秘钥交换的C程序


ECDH秘钥交换与SM2秘钥交换类似,下面是我从网上下到的ECDH秘钥交换的C程序

#include <openssl/ssl.h>
#define ECDH_SIZE 33

void handleErrors()
{
printf("Error occurred.\n");
}
static void disp(const char *str, const void *pbuf, const int size)
{
int i=0;
if(str != NULL){
printf("%s:\n", str);
}
if(pbuf != NULL && size > 0){
for(i=0;i<size;i++)
printf("%02x ", *((unsigned char *)pbuf+i));
putchar('\n');
}
putchar('\n');
}
int main() {
/* alice */
EC_KEY *ecdh = EC_KEY_new();
EC_POINT *point = NULL;
EC_POINT *point2c;
EC_GROUP *group;
unsigned char pubkey[ECDH_SIZE];
unsigned char shared[ECDH_SIZE];
int len;

//Generate Public
ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1
EC_KEY_generate_key(ecdh);
point = EC_KEY_get0_public_key(ecdh);
group = EC_KEY_get0_group(ecdh);

if(0 == (len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL))) handleErrors();
printf("len=%d\n",len);

/* bob */
EC_KEY *ecdh2 = EC_KEY_new();
EC_POINT *point2 = NULL;
EC_POINT *pointc;
EC_GROUP *group2;
unsigned char pubkey2[ECDH_SIZE];
unsigned char shared2[ECDH_SIZE];
int len2;

//Generate Public
ecdh2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1
EC_KEY_generate_key(ecdh2);
point2 = EC_KEY_get0_public_key(ecdh2);
group2 = EC_KEY_get0_group(ecdh2);

if(0 == (len2 = EC_POINT_point2oct(group2, point2, POINT_CONVERSION_COMPRESSED, pubkey2, ECDH_SIZE, NULL))) handleErrors();
printf("len2=%d\n",len);

/* alice */
//ComputeKey
point2c = EC_POINT_new(group);
EC_POINT_oct2point(group, point2c, pubkey2, ECDH_SIZE, NULL);

if (0 != EC_POINT_cmp(group, point2, point2c, NULL)) handleErrors();
if(0 == (len = ECDH_compute_key(shared, ECDH_SIZE, point2c, ecdh, NULL))) handleErrors();
printf("len=%d\n",len);
disp("shared", shared, len);

/* bob */
//ComputeKey
pointc = EC_POINT_new(group2);
EC_POINT_oct2point(group2, pointc, pubkey, ECDH_SIZE, NULL);

if (0 != EC_POINT_cmp(group2, point, pointc, NULL)) handleErrors();
if(0 == (len2 = ECDH_compute_key(shared2, ECDH_SIZE, pointc, ecdh2, NULL))) handleErrors();
printf("len2=%d\n",len2);
disp("shared2", shared2, len2);

/* alice */
EC_POINT_free(pointc);
EC_KEY_free(ecdh);
/* bob */
EC_POINT_free(point2c);
EC_KEY_free(ecdh2);

printf("To the end\n");

return 0;
}


在VC++ 6中编译(compile),报错信息如下




出错原因在这篇博客“容易混淆的const”一文中说得很清楚,据此解决方案就是将34、35、52、53的右侧表达式进行强制类型转换,经编译没有报错,修改后完整代码如下

#include <openssl/ssl.h>
#define ECDH_SIZE 33

void handleErrors()
{
printf("Error occurred.\n");
}
static void disp(const char *str, const void *pbuf, const int size)
{
int i=0;
if(str != NULL){
printf("%s:\n", str);
}
if(pbuf != NULL && size > 0){
for(i=0;i<size;i++)
printf("%02x ", *((unsigned char *)pbuf+i));
putchar('\n');
}
putchar('\n');
}
int main() {
/* alice */
EC_KEY *ecdh = EC_KEY_new();
EC_POINT *point = NULL;
EC_POINT *point2c;
EC_GROUP *group;
unsigned char pubkey[ECDH_SIZE];
unsigned char shared[ECDH_SIZE];
int len;

//Generate Public
ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1
EC_KEY_generate_key(ecdh);
point = (struct ec_point_st *)EC_KEY_get0_public_key(ecdh);
group = (struct ec_group_st *)EC_KEY_get0_group(ecdh);

if(0 == (len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL))) handleErrors();
printf("len=%d\n",len);

/* bob */
EC_KEY *ecdh2 = EC_KEY_new();
EC_POINT *point2 = NULL;
EC_POINT *pointc;
EC_GROUP *group2;
unsigned char pubkey2[ECDH_SIZE];
unsigned char shared2[ECDH_SIZE];
int len2;

//Generate Public
ecdh2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1
EC_KEY_generate_key(ecdh2);
point2 = (struct ec_point_st *)EC_KEY_get0_public_key(ecdh2);
group2 = (struct ec_group_st *)EC_KEY_get0_group(ecdh2);

if(0 == (len2 = EC_POINT_point2oct(group2, point2, POINT_CONVERSION_COMPRESSED, pubkey2, ECDH_SIZE, NULL))) handleErrors();
printf("len2=%d\n",len);

/* alice */
//ComputeKey
point2c = EC_POINT_new(group);
EC_POINT_oct2point(group, point2c, pubkey2, ECDH_SIZE, NULL);

if (0 != EC_POINT_cmp(group, point2, point2c, NULL)) handleErrors();
if(0 == (len = ECDH_compute_key(shared, ECDH_SIZE, point2c, ecdh, NULL))) handleErrors();
printf("len=%d\n",len);
disp("shared", shared, len);

/* bob */
//ComputeKey
pointc = EC_POINT_new(group2);
EC_POINT_oct2point(group2, pointc, pubkey, ECDH_SIZE, NULL);

if (0 != EC_POINT_cmp(group2, point, pointc, NULL)) handleErrors();
if(0 == (len2 = ECDH_compute_key(shared2, ECDH_SIZE, pointc, ecdh2, NULL))) handleErrors();
printf("len2=%d\n",len2);
disp("shared2", shared2, len2);

/* alice */
EC_POINT_free(pointc);
EC_KEY_free(ecdh);
/* bob */
EC_POINT_free(point2c);
EC_KEY_free(ecdh2);

printf("To the end\n");

return 0;
}


组建(build),报错如下




出错原因在“ 解决error LNK2001 unresolved external symbol”一文中说得很清楚,缺少必要的库,添加库的方法为:工程—>设置—>连接—>对象/模块库—>添加ssleay32.lib
 libeay32.lib,如下



在组建(build),成功了,运行(Ctrl+F5),结果如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ECDH C