]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ecdsa_ossl.c
Copyright consolidation 05/10
[thirdparty/openssl.git] / crypto / ec / ecdsa_ossl.c
1 /*
2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <string.h>
11 #include <openssl/err.h>
12 #include <openssl/obj_mac.h>
13 #include <openssl/bn.h>
14 #include <openssl/rand.h>
15 #include <openssl/ec.h>
16 #include "ec_lcl.h"
17
18 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
19 unsigned char *sig, unsigned int *siglen,
20 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
21 {
22 ECDSA_SIG *s;
23 RAND_seed(dgst, dlen);
24 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
25 if (s == NULL) {
26 *siglen = 0;
27 return 0;
28 }
29 *siglen = i2d_ECDSA_SIG(s, &sig);
30 ECDSA_SIG_free(s);
31 return 1;
32 }
33
34 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
35 BIGNUM **kinvp, BIGNUM **rp,
36 const unsigned char *dgst, int dlen)
37 {
38 BN_CTX *ctx = NULL;
39 BIGNUM *k = NULL, *r = NULL, *X = NULL;
40 const BIGNUM *order;
41 EC_POINT *tmp_point = NULL;
42 const EC_GROUP *group;
43 int ret = 0;
44
45 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
46 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
47 return 0;
48 }
49
50 if (!EC_KEY_can_sign(eckey)) {
51 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
52 return 0;
53 }
54
55 if (ctx_in == NULL) {
56 if ((ctx = BN_CTX_new()) == NULL) {
57 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
58 return 0;
59 }
60 } else
61 ctx = ctx_in;
62
63 k = BN_new(); /* this value is later returned in *kinvp */
64 r = BN_new(); /* this value is later returned in *rp */
65 X = BN_new();
66 if (k == NULL || r == NULL || X == NULL) {
67 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
68 goto err;
69 }
70 if ((tmp_point = EC_POINT_new(group)) == NULL) {
71 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
72 goto err;
73 }
74 order = EC_GROUP_get0_order(group);
75 if (order == NULL) {
76 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
77 goto err;
78 }
79
80 do {
81 /* get random k */
82 do
83 if (dgst != NULL) {
84 if (!BN_generate_dsa_nonce
85 (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,
86 ctx)) {
87 ECerr(EC_F_ECDSA_SIGN_SETUP,
88 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
89 goto err;
90 }
91 } else {
92 if (!BN_rand_range(k, order)) {
93 ECerr(EC_F_ECDSA_SIGN_SETUP,
94 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
95 goto err;
96 }
97 }
98 while (BN_is_zero(k));
99
100 /*
101 * We do not want timing information to leak the length of k, so we
102 * compute G*k using an equivalent scalar of fixed bit-length.
103 */
104
105 if (!BN_add(k, k, order))
106 goto err;
107 if (BN_num_bits(k) <= BN_num_bits(order))
108 if (!BN_add(k, k, order))
109 goto err;
110
111 /* compute r the x-coordinate of generator * k */
112 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
113 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
114 goto err;
115 }
116 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
117 NID_X9_62_prime_field) {
118 if (!EC_POINT_get_affine_coordinates_GFp
119 (group, tmp_point, X, NULL, ctx)) {
120 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
121 goto err;
122 }
123 }
124 #ifndef OPENSSL_NO_EC2M
125 else { /* NID_X9_62_characteristic_two_field */
126
127 if (!EC_POINT_get_affine_coordinates_GF2m(group,
128 tmp_point, X, NULL,
129 ctx)) {
130 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
131 goto err;
132 }
133 }
134 #endif
135 if (!BN_nnmod(r, X, order, ctx)) {
136 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
137 goto err;
138 }
139 }
140 while (BN_is_zero(r));
141
142 /* compute the inverse of k */
143 if (EC_GROUP_get_mont_data(group) != NULL) {
144 /*
145 * We want inverse in constant time, therefore we utilize the fact
146 * order must be prime and use Fermats Little Theorem instead.
147 */
148 if (!BN_set_word(X, 2)) {
149 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
150 goto err;
151 }
152 if (!BN_mod_sub(X, order, X, order, ctx)) {
153 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
154 goto err;
155 }
156 BN_set_flags(X, BN_FLG_CONSTTIME);
157 if (!BN_mod_exp_mont_consttime
158 (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {
159 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
160 goto err;
161 }
162 } else {
163 if (!BN_mod_inverse(k, k, order, ctx)) {
164 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
165 goto err;
166 }
167 }
168
169 /* clear old values if necessary */
170 BN_clear_free(*rp);
171 BN_clear_free(*kinvp);
172 /* save the pre-computed values */
173 *rp = r;
174 *kinvp = k;
175 ret = 1;
176 err:
177 if (!ret) {
178 BN_clear_free(k);
179 BN_clear_free(r);
180 }
181 if (ctx != ctx_in)
182 BN_CTX_free(ctx);
183 EC_POINT_free(tmp_point);
184 BN_clear_free(X);
185 return (ret);
186 }
187
188 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
189 BIGNUM **rp)
190 {
191 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
192 }
193
194 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
195 const BIGNUM *in_kinv, const BIGNUM *in_r,
196 EC_KEY *eckey)
197 {
198 int ok = 0, i;
199 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
200 const BIGNUM *order, *ckinv;
201 BN_CTX *ctx = NULL;
202 const EC_GROUP *group;
203 ECDSA_SIG *ret;
204 const BIGNUM *priv_key;
205
206 group = EC_KEY_get0_group(eckey);
207 priv_key = EC_KEY_get0_private_key(eckey);
208
209 if (group == NULL || priv_key == NULL) {
210 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
211 return NULL;
212 }
213
214 if (!EC_KEY_can_sign(eckey)) {
215 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
216 return NULL;
217 }
218
219 ret = ECDSA_SIG_new();
220 if (ret == NULL) {
221 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
222 return NULL;
223 }
224 s = ret->s;
225
226 if ((ctx = BN_CTX_new()) == NULL ||
227 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
228 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
229 goto err;
230 }
231
232 order = EC_GROUP_get0_order(group);
233 if (order == NULL) {
234 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
235 goto err;
236 }
237 i = BN_num_bits(order);
238 /*
239 * Need to truncate digest if it is too long: first truncate whole bytes.
240 */
241 if (8 * dgst_len > i)
242 dgst_len = (i + 7) / 8;
243 if (!BN_bin2bn(dgst, dgst_len, m)) {
244 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
245 goto err;
246 }
247 /* If still too long truncate remaining bits with a shift */
248 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
249 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
250 goto err;
251 }
252 do {
253 if (in_kinv == NULL || in_r == NULL) {
254 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
255 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
256 goto err;
257 }
258 ckinv = kinv;
259 } else {
260 ckinv = in_kinv;
261 if (BN_copy(ret->r, in_r) == NULL) {
262 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
263 goto err;
264 }
265 }
266
267 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
268 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
269 goto err;
270 }
271 if (!BN_mod_add_quick(s, tmp, m, order)) {
272 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
273 goto err;
274 }
275 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
276 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
277 goto err;
278 }
279 if (BN_is_zero(s)) {
280 /*
281 * if kinv and r have been supplied by the caller don't to
282 * generate new kinv and r values
283 */
284 if (in_kinv != NULL && in_r != NULL) {
285 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
286 goto err;
287 }
288 } else
289 /* s != 0 => we have a valid signature */
290 break;
291 }
292 while (1);
293
294 ok = 1;
295 err:
296 if (!ok) {
297 ECDSA_SIG_free(ret);
298 ret = NULL;
299 }
300 BN_CTX_free(ctx);
301 BN_clear_free(m);
302 BN_clear_free(tmp);
303 BN_clear_free(kinv);
304 return ret;
305 }
306
307 /*-
308 * returns
309 * 1: correct signature
310 * 0: incorrect signature
311 * -1: error
312 */
313 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
314 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
315 {
316 ECDSA_SIG *s;
317 const unsigned char *p = sigbuf;
318 unsigned char *der = NULL;
319 int derlen = -1;
320 int ret = -1;
321
322 s = ECDSA_SIG_new();
323 if (s == NULL)
324 return (ret);
325 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
326 goto err;
327 /* Ensure signature uses DER and doesn't have trailing garbage */
328 derlen = i2d_ECDSA_SIG(s, &der);
329 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
330 goto err;
331 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
332 err:
333 OPENSSL_clear_free(der, derlen);
334 ECDSA_SIG_free(s);
335 return (ret);
336 }
337
338 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
339 const ECDSA_SIG *sig, EC_KEY *eckey)
340 {
341 int ret = -1, i;
342 BN_CTX *ctx;
343 const BIGNUM *order;
344 BIGNUM *u1, *u2, *m, *X;
345 EC_POINT *point = NULL;
346 const EC_GROUP *group;
347 const EC_POINT *pub_key;
348
349 /* check input values */
350 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
351 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
352 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
353 return -1;
354 }
355
356 if (!EC_KEY_can_sign(eckey)) {
357 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
358 return -1;
359 }
360
361 ctx = BN_CTX_new();
362 if (ctx == NULL) {
363 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
364 return -1;
365 }
366 BN_CTX_start(ctx);
367 u1 = BN_CTX_get(ctx);
368 u2 = BN_CTX_get(ctx);
369 m = BN_CTX_get(ctx);
370 X = BN_CTX_get(ctx);
371 if (X == NULL) {
372 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
373 goto err;
374 }
375
376 order = EC_GROUP_get0_order(group);
377 if (order == NULL) {
378 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
379 goto err;
380 }
381
382 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
383 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
384 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
385 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
386 ret = 0; /* signature is invalid */
387 goto err;
388 }
389 /* calculate tmp1 = inv(S) mod order */
390 if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
391 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
392 goto err;
393 }
394 /* digest -> m */
395 i = BN_num_bits(order);
396 /*
397 * Need to truncate digest if it is too long: first truncate whole bytes.
398 */
399 if (8 * dgst_len > i)
400 dgst_len = (i + 7) / 8;
401 if (!BN_bin2bn(dgst, dgst_len, m)) {
402 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
403 goto err;
404 }
405 /* If still too long truncate remaining bits with a shift */
406 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
407 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
408 goto err;
409 }
410 /* u1 = m * tmp mod order */
411 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
412 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
413 goto err;
414 }
415 /* u2 = r * w mod q */
416 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
417 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
418 goto err;
419 }
420
421 if ((point = EC_POINT_new(group)) == NULL) {
422 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
423 goto err;
424 }
425 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
426 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
427 goto err;
428 }
429 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
430 NID_X9_62_prime_field) {
431 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
432 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
433 goto err;
434 }
435 }
436 #ifndef OPENSSL_NO_EC2M
437 else { /* NID_X9_62_characteristic_two_field */
438
439 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
440 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
441 goto err;
442 }
443 }
444 #endif
445 if (!BN_nnmod(u1, X, order, ctx)) {
446 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
447 goto err;
448 }
449 /* if the signature is correct u1 is equal to sig->r */
450 ret = (BN_ucmp(u1, sig->r) == 0);
451 err:
452 BN_CTX_end(ctx);
453 BN_CTX_free(ctx);
454 EC_POINT_free(point);
455 return ret;
456 }