Work around Travis "virtual memory exhausted" error
[openssl.git] / crypto / ec / ec_key.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 /* ====================================================================
11  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12  * Portions originally developed by SUN MICROSYSTEMS, INC., and
13  * contributed to the OpenSSL project.
14  */
15
16 #include <internal/cryptlib.h>
17 #include <string.h>
18 #include "ec_lcl.h"
19 #include <openssl/err.h>
20 #include <openssl/engine.h>
21
22 EC_KEY *EC_KEY_new(void)
23 {
24     return EC_KEY_new_method(NULL);
25 }
26
27 EC_KEY *EC_KEY_new_by_curve_name(int nid)
28 {
29     EC_KEY *ret = EC_KEY_new();
30     if (ret == NULL)
31         return NULL;
32     ret->group = EC_GROUP_new_by_curve_name(nid);
33     if (ret->group == NULL) {
34         EC_KEY_free(ret);
35         return NULL;
36     }
37     if (ret->meth->set_group != NULL
38         && ret->meth->set_group(ret, ret->group) == 0) {
39         EC_KEY_free(ret);
40         return NULL;
41     }
42     return ret;
43 }
44
45 void EC_KEY_free(EC_KEY *r)
46 {
47     int i;
48
49     if (r == NULL)
50         return;
51
52     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
53     REF_PRINT_COUNT("EC_KEY", r);
54     if (i > 0)
55         return;
56     REF_ASSERT_ISNT(i < 0);
57
58     if (r->meth->finish != NULL)
59         r->meth->finish(r);
60
61 #ifndef OPENSSL_NO_ENGINE
62     ENGINE_finish(r->engine);
63 #endif
64
65     if (r->group && r->group->meth->keyfinish)
66         r->group->meth->keyfinish(r);
67
68     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
69     CRYPTO_THREAD_lock_free(r->lock);
70     EC_GROUP_free(r->group);
71     EC_POINT_free(r->pub_key);
72     BN_clear_free(r->priv_key);
73
74     OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
75 }
76
77 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
78 {
79     if (dest == NULL || src == NULL) {
80         ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
81         return NULL;
82     }
83     if (src->meth != dest->meth) {
84         if (dest->meth->finish != NULL)
85             dest->meth->finish(dest);
86         if (dest->group && dest->group->meth->keyfinish)
87             dest->group->meth->keyfinish(dest);
88 #ifndef OPENSSL_NO_ENGINE
89         if (ENGINE_finish(dest->engine) == 0)
90             return 0;
91         dest->engine = NULL;
92 #endif
93     }
94     /* copy the parameters */
95     if (src->group != NULL) {
96         const EC_METHOD *meth = EC_GROUP_method_of(src->group);
97         /* clear the old group */
98         EC_GROUP_free(dest->group);
99         dest->group = EC_GROUP_new(meth);
100         if (dest->group == NULL)
101             return NULL;
102         if (!EC_GROUP_copy(dest->group, src->group))
103             return NULL;
104
105         /*  copy the public key */
106         if (src->pub_key != NULL) {
107             EC_POINT_free(dest->pub_key);
108             dest->pub_key = EC_POINT_new(src->group);
109             if (dest->pub_key == NULL)
110                 return NULL;
111             if (!EC_POINT_copy(dest->pub_key, src->pub_key))
112                 return NULL;
113         }
114         /* copy the private key */
115         if (src->priv_key != NULL) {
116             if (dest->priv_key == NULL) {
117                 dest->priv_key = BN_new();
118                 if (dest->priv_key == NULL)
119                     return NULL;
120             }
121             if (!BN_copy(dest->priv_key, src->priv_key))
122                 return NULL;
123             if (src->group->meth->keycopy
124                 && src->group->meth->keycopy(dest, src) == 0)
125                 return NULL;
126         }
127     }
128
129
130     /* copy the rest */
131     dest->enc_flag = src->enc_flag;
132     dest->conv_form = src->conv_form;
133     dest->version = src->version;
134     dest->flags = src->flags;
135     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
136                             &dest->ex_data, &src->ex_data))
137         return NULL;
138
139     if (src->meth != dest->meth) {
140 #ifndef OPENSSL_NO_ENGINE
141         if (src->engine != NULL && ENGINE_init(src->engine) == 0)
142             return NULL;
143         dest->engine = src->engine;
144 #endif
145         dest->meth = src->meth;
146     }
147
148     if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
149         return NULL;
150
151     return dest;
152 }
153
154 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
155 {
156     EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
157
158     if (ret == NULL)
159         return NULL;
160
161     if (EC_KEY_copy(ret, ec_key) == NULL) {
162         EC_KEY_free(ret);
163         return NULL;
164     }
165     return ret;
166 }
167
168 int EC_KEY_up_ref(EC_KEY *r)
169 {
170     int i;
171
172     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
173         return 0;
174
175     REF_PRINT_COUNT("EC_KEY", r);
176     REF_ASSERT_ISNT(i < 2);
177     return ((i > 1) ? 1 : 0);
178 }
179
180 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
181 {
182     return eckey->engine;
183 }
184
185 int EC_KEY_generate_key(EC_KEY *eckey)
186 {
187     if (eckey == NULL || eckey->group == NULL) {
188         ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
189         return 0;
190     }
191     if (eckey->meth->keygen != NULL)
192         return eckey->meth->keygen(eckey);
193     ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
194     return 0;
195 }
196
197 int ossl_ec_key_gen(EC_KEY *eckey)
198 {
199     OPENSSL_assert(eckey->group->meth->keygen != NULL);
200     return eckey->group->meth->keygen(eckey);
201 }
202
203 int ec_key_simple_generate_key(EC_KEY *eckey)
204 {
205     int ok = 0;
206     BN_CTX *ctx = NULL;
207     BIGNUM *priv_key = NULL;
208     const BIGNUM *order = NULL;
209     EC_POINT *pub_key = NULL;
210
211     if ((ctx = BN_CTX_new()) == NULL)
212         goto err;
213
214     if (eckey->priv_key == NULL) {
215         priv_key = BN_new();
216         if (priv_key == NULL)
217             goto err;
218     } else
219         priv_key = eckey->priv_key;
220
221     order = EC_GROUP_get0_order(eckey->group);
222     if (order == NULL)
223         goto err;
224
225     do
226         if (!BN_rand_range(priv_key, order))
227             goto err;
228     while (BN_is_zero(priv_key)) ;
229
230     if (eckey->pub_key == NULL) {
231         pub_key = EC_POINT_new(eckey->group);
232         if (pub_key == NULL)
233             goto err;
234     } else
235         pub_key = eckey->pub_key;
236
237     if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
238         goto err;
239
240     eckey->priv_key = priv_key;
241     eckey->pub_key = pub_key;
242
243     ok = 1;
244
245  err:
246     if (eckey->pub_key == NULL)
247         EC_POINT_free(pub_key);
248     if (eckey->priv_key != priv_key)
249         BN_free(priv_key);
250     BN_CTX_free(ctx);
251     return ok;
252 }
253
254 int ec_key_simple_generate_public_key(EC_KEY *eckey)
255 {
256     return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
257                         NULL, NULL);
258 }
259
260 int EC_KEY_check_key(const EC_KEY *eckey)
261 {
262     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
263         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
264         return 0;
265     }
266
267     if (eckey->group->meth->keycheck == NULL) {
268         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
269         return 0;
270     }
271
272     return eckey->group->meth->keycheck(eckey);
273 }
274
275 int ec_key_simple_check_key(const EC_KEY *eckey)
276 {
277     int ok = 0;
278     BN_CTX *ctx = NULL;
279     const BIGNUM *order = NULL;
280     EC_POINT *point = NULL;
281
282     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
283         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
284         return 0;
285     }
286
287     if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
288         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
289         goto err;
290     }
291
292     if ((ctx = BN_CTX_new()) == NULL)
293         goto err;
294     if ((point = EC_POINT_new(eckey->group)) == NULL)
295         goto err;
296
297     /* testing whether the pub_key is on the elliptic curve */
298     if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
299         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
300         goto err;
301     }
302     /* testing whether pub_key * order is the point at infinity */
303     order = eckey->group->order;
304     if (BN_is_zero(order)) {
305         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
306         goto err;
307     }
308     if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
309         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
310         goto err;
311     }
312     if (!EC_POINT_is_at_infinity(eckey->group, point)) {
313         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
314         goto err;
315     }
316     /*
317      * in case the priv_key is present : check if generator * priv_key ==
318      * pub_key
319      */
320     if (eckey->priv_key != NULL) {
321         if (BN_cmp(eckey->priv_key, order) >= 0) {
322             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
323             goto err;
324         }
325         if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
326                           NULL, NULL, ctx)) {
327             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
328             goto err;
329         }
330         if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
331             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
332             goto err;
333         }
334     }
335     ok = 1;
336  err:
337     BN_CTX_free(ctx);
338     EC_POINT_free(point);
339     return ok;
340 }
341
342 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
343                                              BIGNUM *y)
344 {
345     BN_CTX *ctx = NULL;
346     BIGNUM *tx, *ty;
347     EC_POINT *point = NULL;
348     int ok = 0;
349 #ifndef OPENSSL_NO_EC2M
350     int tmp_nid, is_char_two = 0;
351 #endif
352
353     if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
354         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
355               ERR_R_PASSED_NULL_PARAMETER);
356         return 0;
357     }
358     ctx = BN_CTX_new();
359     if (ctx == NULL)
360         return 0;
361
362     BN_CTX_start(ctx);
363     point = EC_POINT_new(key->group);
364
365     if (point == NULL)
366         goto err;
367
368     tx = BN_CTX_get(ctx);
369     ty = BN_CTX_get(ctx);
370     if (ty == NULL)
371         goto err;
372
373 #ifndef OPENSSL_NO_EC2M
374     tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
375
376     if (tmp_nid == NID_X9_62_characteristic_two_field)
377         is_char_two = 1;
378
379     if (is_char_two) {
380         if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
381                                                   x, y, ctx))
382             goto err;
383         if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
384                                                   tx, ty, ctx))
385             goto err;
386     } else
387 #endif
388     {
389         if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
390                                                  x, y, ctx))
391             goto err;
392         if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
393                                                  tx, ty, ctx))
394             goto err;
395     }
396     /*
397      * Check if retrieved coordinates match originals and are less than field
398      * order: if not values are out of range.
399      */
400     if (BN_cmp(x, tx) || BN_cmp(y, ty)
401         || (BN_cmp(x, key->group->field) >= 0)
402         || (BN_cmp(y, key->group->field) >= 0)) {
403         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
404               EC_R_COORDINATES_OUT_OF_RANGE);
405         goto err;
406     }
407
408     if (!EC_KEY_set_public_key(key, point))
409         goto err;
410
411     if (EC_KEY_check_key(key) == 0)
412         goto err;
413
414     ok = 1;
415
416  err:
417     BN_CTX_end(ctx);
418     BN_CTX_free(ctx);
419     EC_POINT_free(point);
420     return ok;
421
422 }
423
424 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
425 {
426     return key->group;
427 }
428
429 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
430 {
431     if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
432         return 0;
433     EC_GROUP_free(key->group);
434     key->group = EC_GROUP_dup(group);
435     return (key->group == NULL) ? 0 : 1;
436 }
437
438 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
439 {
440     return key->priv_key;
441 }
442
443 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
444 {
445     if (key->group == NULL || key->group->meth == NULL)
446         return 0;
447     if (key->group->meth->set_private != NULL
448         && key->group->meth->set_private(key, priv_key) == 0)
449         return 0;
450     if (key->meth->set_private != NULL
451         && key->meth->set_private(key, priv_key) == 0)
452         return 0;
453     BN_clear_free(key->priv_key);
454     key->priv_key = BN_dup(priv_key);
455     return (key->priv_key == NULL) ? 0 : 1;
456 }
457
458 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
459 {
460     return key->pub_key;
461 }
462
463 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
464 {
465     if (key->meth->set_public != NULL
466         && key->meth->set_public(key, pub_key) == 0)
467         return 0;
468     EC_POINT_free(key->pub_key);
469     key->pub_key = EC_POINT_dup(pub_key, key->group);
470     return (key->pub_key == NULL) ? 0 : 1;
471 }
472
473 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
474 {
475     return key->enc_flag;
476 }
477
478 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
479 {
480     key->enc_flag = flags;
481 }
482
483 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
484 {
485     return key->conv_form;
486 }
487
488 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
489 {
490     key->conv_form = cform;
491     if (key->group != NULL)
492         EC_GROUP_set_point_conversion_form(key->group, cform);
493 }
494
495 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
496 {
497     if (key->group != NULL)
498         EC_GROUP_set_asn1_flag(key->group, flag);
499 }
500
501 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
502 {
503     if (key->group == NULL)
504         return 0;
505     return EC_GROUP_precompute_mult(key->group, ctx);
506 }
507
508 int EC_KEY_get_flags(const EC_KEY *key)
509 {
510     return key->flags;
511 }
512
513 void EC_KEY_set_flags(EC_KEY *key, int flags)
514 {
515     key->flags |= flags;
516 }
517
518 void EC_KEY_clear_flags(EC_KEY *key, int flags)
519 {
520     key->flags &= ~flags;
521 }
522
523 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
524                         unsigned char **pbuf, BN_CTX *ctx)
525 {
526     if (key == NULL || key->pub_key == NULL || key->group == NULL)
527         return 0;
528     return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
529 }
530
531 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
532                    BN_CTX *ctx)
533 {
534     if (key == NULL || key->group == NULL)
535         return 0;
536     if (key->pub_key == NULL)
537         key->pub_key = EC_POINT_new(key->group);
538     if (key->pub_key == NULL)
539         return 0;
540     if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
541         return 0;
542     /*
543      * Save the point conversion form.
544      * For non-custom curves the first octet of the buffer (excluding
545      * the last significant bit) contains the point conversion form.
546      * EC_POINT_oct2point() has already performed sanity checking of
547      * the buffer so we know it is valid.
548      */
549     if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
550         key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
551     return 1;
552 }
553
554 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
555                        unsigned char *buf, size_t len)
556 {
557     if (eckey->group == NULL || eckey->group->meth == NULL)
558         return 0;
559     if (eckey->group->meth->priv2oct == NULL) {
560         ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
561         return 0;
562     }
563
564     return eckey->group->meth->priv2oct(eckey, buf, len);
565 }
566
567 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
568                               unsigned char *buf, size_t len)
569 {
570     size_t buf_len;
571
572     buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
573     if (eckey->priv_key == NULL)
574         return 0;
575     if (buf == NULL)
576         return buf_len;
577     else if (len < buf_len)
578         return 0;
579
580     /* Octetstring may need leading zeros if BN is to short */
581
582     if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
583         ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
584         return 0;
585     }
586
587     return buf_len;
588 }
589
590 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
591 {
592     if (eckey->group == NULL || eckey->group->meth == NULL)
593         return 0;
594     if (eckey->group->meth->oct2priv == NULL) {
595         ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
596         return 0;
597     }
598     return eckey->group->meth->oct2priv(eckey, buf, len);
599 }
600
601 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
602 {
603     if (eckey->priv_key == NULL)
604         eckey->priv_key = BN_secure_new();
605     if (eckey->priv_key == NULL) {
606         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
607         return 0;
608     }
609     eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
610     if (eckey->priv_key == NULL) {
611         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
612         return 0;
613     }
614     return 1;
615 }
616
617 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
618 {
619     size_t len;
620     unsigned char *buf;
621     len = EC_KEY_priv2oct(eckey, NULL, 0);
622     if (len == 0)
623         return 0;
624     buf = OPENSSL_malloc(len);
625     if (buf == NULL)
626         return 0;
627     len = EC_KEY_priv2oct(eckey, buf, len);
628     if (len == 0) {
629         OPENSSL_free(buf);
630         return 0;
631     }
632     *pbuf = buf;
633     return len;
634 }
635
636 int EC_KEY_can_sign(const EC_KEY *eckey)
637 {
638     if (eckey->group == NULL || eckey->group->meth == NULL
639         || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
640         return 0;
641     return 1;
642 }