]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecdsa_ossl.c
s390x assembly pack: accelerate scalar multiplication
[thirdparty/openssl.git] / crypto / ec / ecdsa_ossl.c
CommitLineData
c6700d27 1/*
3c7d0945 2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
4d94ae00 3 *
a7f182b7 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
4d94ae00 8 */
0bee0e62 9
a200a817 10#include <string.h>
0bee0e62 11#include <openssl/err.h>
14a7cfb3 12#include <openssl/obj_mac.h>
8a99cb29 13#include <openssl/rand.h>
37132c97 14#include "internal/bn_int.h"
6a47db45 15#include "ec_lcl.h"
190c615d 16
a200a817
DSH
17int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
18 unsigned char *sig, unsigned int *siglen,
19 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
20{
21 ECDSA_SIG *s;
75e2c877 22
a200a817
DSH
23 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
24 if (s == NULL) {
25 *siglen = 0;
26 return 0;
27 }
28 *siglen = i2d_ECDSA_SIG(s, &sig);
29 ECDSA_SIG_free(s);
30 return 1;
31}
32
8d6a75dc 33static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
0f113f3e
MC
34 BIGNUM **kinvp, BIGNUM **rp,
35 const unsigned char *dgst, int dlen)
4d94ae00 36{
0f113f3e 37 BN_CTX *ctx = NULL;
be2e334f
DSH
38 BIGNUM *k = NULL, *r = NULL, *X = NULL;
39 const BIGNUM *order;
0f113f3e
MC
40 EC_POINT *tmp_point = NULL;
41 const EC_GROUP *group;
42 int ret = 0;
4a089bbd 43 int order_bits;
7408f675 44 const BIGNUM *priv_key;
9dd84053 45
0f113f3e 46 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
6a47db45 47 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
48 return 0;
49 }
7408f675
DO
50 if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
51 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_MISSING_PRIVATE_KEY);
52 return 0;
53 }
a74333f9 54
4b0555ec
DSH
55 if (!EC_KEY_can_sign(eckey)) {
56 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
57 return 0;
58 }
59
fff7a0dc 60 if ((ctx = ctx_in) == NULL) {
a9612d6c 61 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
6a47db45 62 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
63 return 0;
64 }
fff7a0dc 65 }
4d94ae00 66
bb315ca7 67 k = BN_secure_new(); /* this value is later returned in *kinvp */
0f113f3e 68 r = BN_new(); /* this value is later returned in *rp */
0f113f3e 69 X = BN_new();
be2e334f 70 if (k == NULL || r == NULL || X == NULL) {
6a47db45 71 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
72 goto err;
73 }
74 if ((tmp_point = EC_POINT_new(group)) == NULL) {
6a47db45 75 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
76 goto err;
77 }
be2e334f 78 order = EC_GROUP_get0_order(group);
cac4fb58 79
4a089bbd
P
80 /* Preallocate space */
81 order_bits = BN_num_bits(order);
82 if (!BN_set_bit(k, order_bits)
83 || !BN_set_bit(r, order_bits)
84 || !BN_set_bit(X, order_bits))
85 goto err;
86
0f113f3e
MC
87 do {
88 /* get random k */
fff7a0dc 89 do {
0f113f3e 90 if (dgst != NULL) {
7408f675 91 if (!BN_generate_dsa_nonce(k, order, priv_key,
fff7a0dc 92 dgst, dlen, ctx)) {
6a47db45 93 ECerr(EC_F_ECDSA_SIGN_SETUP,
fff7a0dc 94 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
0f113f3e
MC
95 goto err;
96 }
474e469b 97 } else {
a9612d6c 98 if (!BN_priv_rand_range_ex(k, order, ctx)) {
6a47db45 99 ECerr(EC_F_ECDSA_SIGN_SETUP,
fff7a0dc 100 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
0f113f3e
MC
101 goto err;
102 }
103 }
fff7a0dc 104 } while (BN_is_zero(k));
4d94ae00 105
0f113f3e
MC
106 /* compute r the x-coordinate of generator * k */
107 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
6a47db45 108 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
109 goto err;
110 }
9cc570d4
MC
111
112 if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
113 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
114 goto err;
0f113f3e 115 }
9cc570d4 116
0f113f3e 117 if (!BN_nnmod(r, X, order, ctx)) {
6a47db45 118 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
0f113f3e
MC
119 goto err;
120 }
fff7a0dc 121 } while (BN_is_zero(r));
4d94ae00 122
c11d372b 123 /* compute the inverse of k */
792546eb 124 if (!ec_group_do_inverse_ord(group, k, k, ctx)) {
c11d372b
BB
125 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
126 goto err;
0f113f3e 127 }
f54be179 128
0f113f3e 129 /* clear old values if necessary */
23a1d5e9
RS
130 BN_clear_free(*rp);
131 BN_clear_free(*kinvp);
0f113f3e
MC
132 /* save the pre-computed values */
133 *rp = r;
134 *kinvp = k;
135 ret = 1;
136 err:
137 if (!ret) {
23a1d5e9
RS
138 BN_clear_free(k);
139 BN_clear_free(r);
0f113f3e 140 }
23a1d5e9 141 if (ctx != ctx_in)
0f113f3e 142 BN_CTX_free(ctx);
8fdc3734 143 EC_POINT_free(tmp_point);
23a1d5e9 144 BN_clear_free(X);
26a7d938 145 return ret;
4d94ae00
BM
146}
147
6a47db45
DSH
148int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
149 BIGNUM **rp)
150{
151 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
152}
153
154ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
155 const BIGNUM *in_kinv, const BIGNUM *in_r,
156 EC_KEY *eckey)
4d94ae00 157{
0f113f3e 158 int ok = 0, i;
37132c97 159 BIGNUM *kinv = NULL, *s, *m = NULL;
be2e334f 160 const BIGNUM *order, *ckinv;
0f113f3e
MC
161 BN_CTX *ctx = NULL;
162 const EC_GROUP *group;
163 ECDSA_SIG *ret;
0f113f3e
MC
164 const BIGNUM *priv_key;
165
0f113f3e
MC
166 group = EC_KEY_get0_group(eckey);
167 priv_key = EC_KEY_get0_private_key(eckey);
14a7cfb3 168
7408f675 169 if (group == NULL) {
6a47db45 170 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
171 return NULL;
172 }
7408f675
DO
173 if (priv_key == NULL) {
174 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_MISSING_PRIVATE_KEY);
175 return NULL;
176 }
4d94ae00 177
4b0555ec
DSH
178 if (!EC_KEY_can_sign(eckey)) {
179 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
180 return NULL;
181 }
182
0f113f3e 183 ret = ECDSA_SIG_new();
90945fa3 184 if (ret == NULL) {
6a47db45 185 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
186 return NULL;
187 }
8cc44d97
DSH
188 ret->r = BN_new();
189 ret->s = BN_new();
190 if (ret->r == NULL || ret->s == NULL) {
191 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
192 goto err;
193 }
0f113f3e 194 s = ret->s;
c6700d27 195
a9612d6c 196 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
37132c97 197 || (m = BN_new()) == NULL) {
6a47db45 198 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
199 goto err;
200 }
4d94ae00 201
be2e334f 202 order = EC_GROUP_get0_order(group);
0f113f3e
MC
203 i = BN_num_bits(order);
204 /*
205 * Need to truncate digest if it is too long: first truncate whole bytes.
206 */
207 if (8 * dgst_len > i)
208 dgst_len = (i + 7) / 8;
209 if (!BN_bin2bn(dgst, dgst_len, m)) {
6a47db45 210 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
211 goto err;
212 }
fff7a0dc 213 /* If still too long, truncate remaining bits with a shift */
0f113f3e 214 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
6a47db45 215 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
216 goto err;
217 }
218 do {
219 if (in_kinv == NULL || in_r == NULL) {
220 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
6a47db45 221 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
0f113f3e
MC
222 goto err;
223 }
224 ckinv = kinv;
225 } else {
226 ckinv = in_kinv;
227 if (BN_copy(ret->r, in_r) == NULL) {
6a47db45 228 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
229 goto err;
230 }
231 }
4d94ae00 232
37132c97
AP
233 /*
234 * With only one multiplicant being in Montgomery domain
235 * multiplication yields real result without post-conversion.
236 * Also note that all operations but last are performed with
237 * zero-padded vectors. Last operation, BN_mod_mul_montgomery
238 * below, returns user-visible value with removed zero padding.
239 */
240 if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
241 || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
a3e9d5aa
MC
242 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
243 goto err;
244 }
37132c97 245 if (!bn_mod_add_fixed_top(s, s, m, order)) {
6a47db45 246 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
247 goto err;
248 }
37132c97
AP
249 /*
250 * |s| can still be larger than modulus, because |m| can be. In
251 * such case we count on Montgomery reduction to tie it up.
252 */
253 if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
254 || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
a3e9d5aa
MC
255 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
256 goto err;
257 }
37132c97 258
0f113f3e
MC
259 if (BN_is_zero(s)) {
260 /*
532b1183 261 * if kinv and r have been supplied by the caller, don't
0f113f3e
MC
262 * generate new kinv and r values
263 */
264 if (in_kinv != NULL && in_r != NULL) {
6a47db45 265 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
0f113f3e
MC
266 goto err;
267 }
fff7a0dc 268 } else {
0f113f3e
MC
269 /* s != 0 => we have a valid signature */
270 break;
fff7a0dc
AP
271 }
272 } while (1);
4d94ae00 273
0f113f3e
MC
274 ok = 1;
275 err:
276 if (!ok) {
277 ECDSA_SIG_free(ret);
278 ret = NULL;
279 }
23a1d5e9 280 BN_CTX_free(ctx);
3fc7a9b9 281 BN_clear_free(m);
23a1d5e9 282 BN_clear_free(kinv);
0f113f3e 283 return ret;
4d94ae00
BM
284}
285
a200a817
DSH
286/*-
287 * returns
288 * 1: correct signature
289 * 0: incorrect signature
290 * -1: error
291 */
292int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
293 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
294{
295 ECDSA_SIG *s;
296 const unsigned char *p = sigbuf;
297 unsigned char *der = NULL;
298 int derlen = -1;
299 int ret = -1;
300
301 s = ECDSA_SIG_new();
302 if (s == NULL)
26a7d938 303 return ret;
a200a817
DSH
304 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
305 goto err;
306 /* Ensure signature uses DER and doesn't have trailing garbage */
307 derlen = i2d_ECDSA_SIG(s, &der);
91e7bcc2 308 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
a200a817
DSH
309 goto err;
310 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
311 err:
312 OPENSSL_clear_free(der, derlen);
313 ECDSA_SIG_free(s);
26a7d938 314 return ret;
a200a817
DSH
315}
316
6a47db45
DSH
317int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
318 const ECDSA_SIG *sig, EC_KEY *eckey)
4d94ae00 319{
0f113f3e
MC
320 int ret = -1, i;
321 BN_CTX *ctx;
be2e334f
DSH
322 const BIGNUM *order;
323 BIGNUM *u1, *u2, *m, *X;
0f113f3e
MC
324 EC_POINT *point = NULL;
325 const EC_GROUP *group;
326 const EC_POINT *pub_key;
9dd84053 327
0f113f3e
MC
328 /* check input values */
329 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
330 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
6a47db45 331 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
0f113f3e
MC
332 return -1;
333 }
4d94ae00 334
4b0555ec
DSH
335 if (!EC_KEY_can_sign(eckey)) {
336 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
337 return -1;
338 }
339
a9612d6c 340 ctx = BN_CTX_new_ex(eckey->libctx);
90945fa3 341 if (ctx == NULL) {
6a47db45 342 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
343 return -1;
344 }
345 BN_CTX_start(ctx);
0f113f3e
MC
346 u1 = BN_CTX_get(ctx);
347 u2 = BN_CTX_get(ctx);
348 m = BN_CTX_get(ctx);
349 X = BN_CTX_get(ctx);
91e7bcc2 350 if (X == NULL) {
6a47db45 351 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
352 goto err;
353 }
c6700d27 354
be2e334f
DSH
355 order = EC_GROUP_get0_order(group);
356 if (order == NULL) {
6a47db45 357 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
0f113f3e
MC
358 goto err;
359 }
4d94ae00 360
0f113f3e
MC
361 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
362 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
363 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
6a47db45 364 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
0f113f3e
MC
365 ret = 0; /* signature is invalid */
366 goto err;
367 }
368 /* calculate tmp1 = inv(S) mod order */
792546eb 369 if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
c11d372b
BB
370 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
371 goto err;
0f113f3e
MC
372 }
373 /* digest -> m */
374 i = BN_num_bits(order);
375 /*
376 * Need to truncate digest if it is too long: first truncate whole bytes.
377 */
378 if (8 * dgst_len > i)
379 dgst_len = (i + 7) / 8;
380 if (!BN_bin2bn(dgst, dgst_len, m)) {
6a47db45 381 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
382 goto err;
383 }
384 /* If still too long truncate remaining bits with a shift */
385 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
6a47db45 386 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
387 goto err;
388 }
389 /* u1 = m * tmp mod order */
390 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
6a47db45 391 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
392 goto err;
393 }
394 /* u2 = r * w mod q */
395 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
6a47db45 396 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
397 goto err;
398 }
399
400 if ((point = EC_POINT_new(group)) == NULL) {
6a47db45 401 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
402 goto err;
403 }
404 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
6a47db45 405 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
0f113f3e
MC
406 goto err;
407 }
0f113f3e 408
9cc570d4
MC
409 if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
410 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
411 goto err;
0f113f3e 412 }
9cc570d4 413
0f113f3e 414 if (!BN_nnmod(u1, X, order, ctx)) {
6a47db45 415 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
416 goto err;
417 }
418 /* if the signature is correct u1 is equal to sig->r */
419 ret = (BN_ucmp(u1, sig->r) == 0);
420 err:
421 BN_CTX_end(ctx);
422 BN_CTX_free(ctx);
8fdc3734 423 EC_POINT_free(point);
0f113f3e 424 return ret;
4d94ae00 425}