forked from luochaolun/ecdh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecdh.cpp
More file actions
128 lines (103 loc) · 2.97 KB
/
ecdh.cpp
File metadata and controls
128 lines (103 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// ecdh.cpp : 定义 DLL 应用程序的导出函数。
//
//#include "stdafx.h"
#include "openssl/ssl.h"
#include "openssl/aes.h"
#include "openssl/rsa.h"
#include "openssl/ec.h"
#include "openssl/ecdh.h"
#include "openssl/md5.h"
#define TRUE 1
#define FALSE 0
extern "C" bool GenEcdh(int nid, unsigned char *szPriKey, int *pLenPri, unsigned char *szPubKey, int *pLenPub);
extern "C" bool DoEcdh(int nid, unsigned char * szServerPubKey, int nLenServerPub, unsigned char * szLocalPriKey, int nLenLocalPri, unsigned char * szShareKey, int *pLenShareKey);
#define MD5_DIGEST_LENGTH 16
extern "C" bool GenEcdh(int nid, unsigned char *szPriKey, int *pLenPri, unsigned char *szPubKey, int *pLenPub)
{
if (!szPriKey || !pLenPri || !szPubKey || !pLenPub) return FALSE;
EC_KEY *ec_key = EC_KEY_new_by_curve_name(nid);
if (!ec_key) return FALSE;
int ret = EC_KEY_generate_key(ec_key);
if (1 != ret)
{
EC_KEY_free(ec_key);
ec_key = NULL;
return FALSE;
}
int nLenPub = i2o_ECPublicKey(ec_key, NULL);
unsigned char *pub_key_buf = NULL;
ret = i2o_ECPublicKey(ec_key, &pub_key_buf);
if (!ret)
{
EC_KEY_free(ec_key);
ec_key = NULL;
return FALSE;
}
memcpy(szPubKey, pub_key_buf, nLenPub);
*pLenPub = nLenPub;
int nLenPri = i2d_ECPrivateKey(ec_key, NULL);
unsigned char *pri_key_buf = NULL;
ret = i2d_ECPrivateKey(ec_key, &pri_key_buf);
if (!ret)
{
EC_KEY_free(ec_key);
ec_key = NULL;
return FALSE;
}
memcpy(szPriKey, pri_key_buf, nLenPri);
*pLenPri = nLenPri;
if (ec_key)
{
EC_KEY_free(ec_key);
ec_key = NULL;
}
if (pub_key_buf)
{
OPENSSL_free(pub_key_buf);
}
if (pri_key_buf)
{
OPENSSL_free(pri_key_buf);
}
return TRUE;
}
void *KDF_MD5(const void *in, size_t inlen, void *out, size_t *outlen)
{
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, in, inlen);
MD5_Final((unsigned char*)out, &ctx);
*outlen = MD5_DIGEST_LENGTH;
return out;
}
extern "C" bool DoEcdh(int nid, unsigned char * szServerPubKey, int nLenServerPub, unsigned char * szLocalPriKey, int nLenLocalPri, unsigned char * szShareKey, int *pLenShareKey)
{
const unsigned char *public_material = (const unsigned char *)szServerPubKey;
const unsigned char *private_material = (const unsigned char *)szLocalPriKey;
EC_KEY *pub_ec_key = EC_KEY_new_by_curve_name(nid);
if (!pub_ec_key) return FALSE;
pub_ec_key = o2i_ECPublicKey(&pub_ec_key, &public_material, nLenServerPub);
if (!pub_ec_key) return FALSE;
EC_KEY *pri_ec_key = EC_KEY_new_by_curve_name(nid);
if (!pri_ec_key) return FALSE;
pri_ec_key = d2i_ECPrivateKey(&pri_ec_key, &private_material, nLenLocalPri);
if (!pri_ec_key) return FALSE;
if (MD5_DIGEST_LENGTH != ECDH_compute_key((void *)szShareKey, MD5_DIGEST_LENGTH, EC_KEY_get0_public_key(pub_ec_key), pri_ec_key, KDF_MD5))
{
EC_KEY_free(pub_ec_key);
EC_KEY_free(pri_ec_key);
return FALSE;
}
*pLenShareKey = MD5_DIGEST_LENGTH;
if (pub_ec_key)
{
EC_KEY_free(pub_ec_key);
pub_ec_key = NULL;
}
if (pri_ec_key)
{
EC_KEY_free(pri_ec_key);
pri_ec_key = NULL;
}
return TRUE;
}