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