forked from frankmorgner/openpace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
295 lines (250 loc) · 8.08 KB
/
misc.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright (c) 2010-2012 Frank Morgner and Dominik Oepen
*
* This file is part of OpenPACE.
*
* OpenPACE is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* OpenPACE is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* OpenPACE. If not, see <http://www.gnu.org/licenses/>.
*
* Additional permission under GNU GPL version 3 section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with OpenSSL (or a modified version of that library), containing
* parts covered by the terms of OpenSSL's license, the licensors of
* this Program grant you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination shall include
* the source code for the parts of OpenSSL used as well as that of the
* covered work.
*
* If you modify this Program, or any covered work, by linking or combining it
* with OpenSC (or a modified version of that library), containing
* parts covered by the terms of OpenSC's license, the licensors of
* this Program grant you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination shall include
* the source code for the parts of OpenSC used as well as that of the
* covered work.
*/
/**
* @file misc.c
* @brief Miscellaneous functions used in OpenPACE
*
* @author Frank Morgner <[email protected]>
* @author Dominik Oepen <[email protected]>
*/
#include "eac_err.h"
#include "misc.h"
#include <limits.h>
#include <openssl/ecdh.h>
#include <openssl/ecdsa.h>
#include <openssl/ossl_typ.h>
#include <stdint.h>
#include <string.h>
static int ecdh_compute_key_point(void *out, size_t outlen, const EC_POINT *pub_key,
EC_KEY *ecdh,
void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen));
static int new_ecdh_compute_key_point(unsigned char **psec, size_t *pseclen,
const EC_POINT *pub_key, const EC_KEY *ecdh);
struct ec_key_method_st {
const char *name;
int32_t flags;
int (*init)(EC_KEY *key);
void (*finish)(EC_KEY *key);
int (*copy)(EC_KEY *dest, const EC_KEY *src);
int (*set_group)(EC_KEY *key, const EC_GROUP *grp);
int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
int (*keygen)(EC_KEY *key);
int (*compute_key)(unsigned char **pout, size_t *poutlen,
const EC_POINT *pub_key, const EC_KEY *ecdh);
int (*sign)(int type, const unsigned char *dgst, int dlen, unsigned char
*sig, unsigned int *siglen, const BIGNUM *kinv,
const BIGNUM *r, EC_KEY *eckey);
int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
BIGNUM **rp);
ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgst_len,
const BIGNUM *in_kinv, const BIGNUM *in_r,
EC_KEY *eckey);
int (*verify)(int type, const unsigned char *dgst, int dgst_len,
const unsigned char *sigbuf, int sig_len, EC_KEY *eckey);
int (*verify_sig)(const unsigned char *dgst, int dgst_len,
const ECDSA_SIG *sig, EC_KEY *eckey);
};
struct ecdh_method
{
const char *name;
int (*compute_key)(void *key, size_t outlen, const EC_POINT *pub_key, EC_KEY *ecdh,
void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen));
#if 0
int (*init)(EC_KEY *eckey);
int (*finish)(EC_KEY *eckey);
#endif
int flags;
char *app_data;
};
static const EC_KEY_METHOD openssl_ec_key_meth_point = {
"OpenSSL EC_KEY method with Point",
0,
0,0,0,0,0,0,
NULL,
new_ecdh_compute_key_point,
NULL,
NULL,
NULL,
NULL,
NULL
};
const EC_KEY_METHOD *EC_KEY_OpenSSL_Point(void)
{
return &openssl_ec_key_meth_point;
}
int new_ecdh_compute_key_point(unsigned char **psec, size_t *pseclen, const
EC_POINT *pub_key, const EC_KEY *ecdh)
{
/* The new API requires us to allocate the memory for the output buffer */
int ret= -1;
/* should be enough to hold an uncompressed point of a 528 bit curve
* (e.g. secp521r1, which is the biggest curve of BSI TR-03110) */
*psec = OPENSSL_malloc(133);
check(*psec, "Out of memory");
*pseclen = 133;
ret = ecdh_compute_key_point(*psec, *pseclen, pub_key, (EC_KEY *) ecdh, NULL);
err:
if (ret <= 0) {
OPENSSL_free(*psec);
*psec = NULL;
*pseclen = 0;
ret = 0;
} else {
*pseclen = ret;
ret = 1;
}
return ret;
}
int ecdh_compute_key_point(void *out, size_t outlen, const EC_POINT *pub_key,
EC_KEY *ecdh,
void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen))
{
/* The old API allocates the memory for us */
BN_CTX *ctx = NULL;
EC_POINT *tmp=NULL;
const BIGNUM *priv_key;
const EC_GROUP* group;
int ret= -1;
size_t buflen;
unsigned char *buf=NULL;
check((outlen < INT_MAX), "out of memory"); /* sort of, anyway */
if ((ctx = BN_CTX_new()) == NULL) goto err;
BN_CTX_start(ctx);
priv_key = EC_KEY_get0_private_key(ecdh);
check(priv_key, "No pivate key");
group = EC_KEY_get0_group(ecdh);
tmp = EC_POINT_new(group);
check(tmp, "Out of memory");
check((EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)),
"Arithmetic error");
buflen = EC_POINT_point2oct(group, tmp, EC_KEY_get_conv_form(ecdh), NULL,
0, ctx);
check((buflen != 0), "Failed to convert point to hex");
buf = OPENSSL_malloc(buflen);
check(buf, "Out of memory");
check((buflen == EC_POINT_point2oct(group, tmp, EC_KEY_get_conv_form(ecdh),
buf, buflen, ctx)), "Failed to convert point to hex");
if (KDF != 0)
{
check((KDF(buf, buflen, out, &outlen) != NULL),
"Key derivation function failed");
ret = outlen;
}
else
{
/* no KDF, just copy as much as we can */
if (outlen > buflen)
outlen = buflen;
memcpy(out, buf, outlen);
ret = outlen;
}
err:
if (tmp) EC_POINT_free(tmp);
if (ctx) BN_CTX_end(ctx);
if (ctx) BN_CTX_free(ctx);
if (buf) OPENSSL_free(buf);
return(ret);
}
BUF_MEM *
BUF_MEM_create(size_t len)
{
BUF_MEM *out = BUF_MEM_new();
if (!out)
return NULL;
if (len == 0)
return out;
if (!BUF_MEM_grow(out, len)) {
BUF_MEM_free(out);
return NULL;
}
return out;
}
BUF_MEM *
BUF_MEM_create_init(const void *buf, size_t len)
{
BUF_MEM *out;
out = BUF_MEM_create(len);
if (!out)
return NULL;
memcpy(out->data, buf, len);
return out;
}
BUF_MEM *
BUF_MEM_dup(const BUF_MEM * in)
{
BUF_MEM * out = NULL;
if (!in)
return NULL;
out = BUF_MEM_create(in->length);
check(out, "Failed to allocate memory");
memcpy(out->data, in->data, in->length);
out->max = in->max;
err:
return out;
}
BUF_MEM *
BN_bn2buf(const BIGNUM *bn)
{
BUF_MEM * out;
if (!bn)
return NULL;
out = BUF_MEM_create(BN_num_bytes(bn));
if (!out)
return NULL;
out->length = BN_bn2bin(bn, (unsigned char *) out->data);
return out;
}
BUF_MEM *
EC_POINT_point2mem(const EC_KEY * ecdh, BN_CTX * bn_ctx, const EC_POINT * ecp)
{
size_t len;
BUF_MEM * out;
if (!ecp)
return NULL;
len = EC_POINT_point2oct(EC_KEY_get0_group(ecdh), ecp,
EC_KEY_get_conv_form(ecdh), NULL, 0, bn_ctx);
if (len == 0)
return NULL;
out = BUF_MEM_create(len);
if (!out)
return NULL;
out->length = EC_POINT_point2oct(EC_KEY_get0_group(ecdh), ecp,
EC_KEY_get_conv_form(ecdh), (unsigned char *) out->data, out->max,
bn_ctx);
return out;
}