]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_asn1.c
Ignore ORDINALS in build.info files, and remove its documentation
[thirdparty/openssl.git] / crypto / ec / ec_asn1.c
CommitLineData
7e31164a 1/*
aa6bb135 2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
012c86ab 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
012c86ab
BM
8 */
9
5454829a 10#include <string.h>
012c86ab 11#include "ec_lcl.h"
0bee0e62 12#include <openssl/err.h>
012c86ab
BM
13#include <openssl/asn1t.h>
14#include <openssl/objects.h>
677963e5 15#include "internal/nelem.h"
012c86ab 16
34f1f2a8 17int EC_GROUP_get_basis_type(const EC_GROUP *group)
0f113f3e 18{
57f48f93 19 int i;
0f113f3e
MC
20
21 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
22 NID_X9_62_characteristic_two_field)
23 /* everything else is currently not supported */
24 return 0;
25
57f48f93
RS
26 /* Find the last non-zero element of group->poly[] */
27 for (i = 0;
50799f35 28 i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
57f48f93
RS
29 i++)
30 continue;
0f113f3e
MC
31
32 if (i == 4)
33 return NID_X9_62_ppBasis;
34 else if (i == 2)
35 return NID_X9_62_tpBasis;
36 else
37 /* everything else is currently not supported */
38 return 0;
39}
40
b3310161 41#ifndef OPENSSL_NO_EC2M
34f1f2a8 42int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
0f113f3e
MC
43{
44 if (group == NULL)
45 return 0;
46
47 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
48 NID_X9_62_characteristic_two_field
49 || !((group->poly[0] != 0) && (group->poly[1] != 0)
50 && (group->poly[2] == 0))) {
51 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
52 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
53 return 0;
54 }
55
56 if (k)
57 *k = group->poly[1];
58
59 return 1;
60}
61
34f1f2a8 62int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
0f113f3e
MC
63 unsigned int *k2, unsigned int *k3)
64{
65 if (group == NULL)
66 return 0;
67
68 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
69 NID_X9_62_characteristic_two_field
70 || !((group->poly[0] != 0) && (group->poly[1] != 0)
71 && (group->poly[2] != 0) && (group->poly[3] != 0)
72 && (group->poly[4] == 0))) {
73 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
74 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
75 return 0;
76 }
77
78 if (k1)
79 *k1 = group->poly[3];
80 if (k2)
81 *k2 = group->poly[2];
82 if (k3)
83 *k3 = group->poly[1];
84
85 return 1;
86}
b3310161 87#endif
8aefe253 88
012c86ab 89/* some structures needed for the asn1 encoding */
012c86ab 90typedef struct x9_62_pentanomial_st {
6a32a3c0
RL
91 int32_t k1;
92 int32_t k2;
93 int32_t k3;
0f113f3e 94} X9_62_PENTANOMIAL;
012c86ab 95
37af03d3 96typedef struct x9_62_characteristic_two_st {
6a32a3c0 97 int32_t m;
0f113f3e
MC
98 ASN1_OBJECT *type;
99 union {
100 char *ptr;
101 /* NID_X9_62_onBasis */
102 ASN1_NULL *onBasis;
103 /* NID_X9_62_tpBasis */
104 ASN1_INTEGER *tpBasis;
105 /* NID_X9_62_ppBasis */
106 X9_62_PENTANOMIAL *ppBasis;
107 /* anything else */
108 ASN1_TYPE *other;
109 } p;
110} X9_62_CHARACTERISTIC_TWO;
37af03d3
GT
111
112typedef struct x9_62_fieldid_st {
0f113f3e
MC
113 ASN1_OBJECT *fieldType;
114 union {
115 char *ptr;
116 /* NID_X9_62_prime_field */
117 ASN1_INTEGER *prime;
118 /* NID_X9_62_characteristic_two_field */
119 X9_62_CHARACTERISTIC_TWO *char_two;
120 /* anything else */
121 ASN1_TYPE *other;
122 } p;
123} X9_62_FIELDID;
37af03d3 124
012c86ab 125typedef struct x9_62_curve_st {
0f113f3e
MC
126 ASN1_OCTET_STRING *a;
127 ASN1_OCTET_STRING *b;
128 ASN1_BIT_STRING *seed;
129} X9_62_CURVE;
012c86ab 130
03f880e4 131struct ec_parameters_st {
6a32a3c0 132 int32_t version;
0f113f3e
MC
133 X9_62_FIELDID *fieldID;
134 X9_62_CURVE *curve;
135 ASN1_OCTET_STRING *base;
136 ASN1_INTEGER *order;
137 ASN1_INTEGER *cofactor;
03f880e4 138} /* ECPARAMETERS */ ;
012c86ab
BM
139
140struct ecpk_parameters_st {
0f113f3e
MC
141 int type;
142 union {
143 ASN1_OBJECT *named_curve;
144 ECPARAMETERS *parameters;
145 ASN1_NULL *implicitlyCA;
146 } value;
147} /* ECPKPARAMETERS */ ;
012c86ab 148
14a7cfb3
BM
149/* SEC1 ECPrivateKey */
150typedef struct ec_privatekey_st {
6a32a3c0 151 int32_t version;
0f113f3e
MC
152 ASN1_OCTET_STRING *privateKey;
153 ECPKPARAMETERS *parameters;
154 ASN1_BIT_STRING *publicKey;
155} EC_PRIVATEKEY;
14a7cfb3 156
37af03d3
GT
157/* the OpenSSL ASN.1 definitions */
158ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
9612e157
RL
159 ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32),
160 ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32),
161 ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32)
df2ee0e2 162} static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL)
012c86ab 163
37af03d3
GT
164DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
165IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL)
166
167ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY);
012c86ab 168
37af03d3 169ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = {
0f113f3e
MC
170 ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)),
171 ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)),
172 ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL))
37af03d3 173} ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL);
012c86ab
BM
174
175ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
9612e157 176 ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32),
0f113f3e
MC
177 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
178 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
df2ee0e2 179} static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
012c86ab 180
37af03d3
GT
181DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
182IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
012c86ab 183
37af03d3
GT
184ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
185
186ASN1_ADB(X9_62_FIELDID) = {
0f113f3e
MC
187 ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)),
188 ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
37af03d3 189} ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL);
012c86ab 190
37af03d3 191ASN1_SEQUENCE(X9_62_FIELDID) = {
0f113f3e
MC
192 ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT),
193 ASN1_ADB_OBJECT(X9_62_FIELDID)
df2ee0e2 194} static_ASN1_SEQUENCE_END(X9_62_FIELDID)
012c86ab
BM
195
196ASN1_SEQUENCE(X9_62_CURVE) = {
0f113f3e
MC
197 ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING),
198 ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING),
199 ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING)
df2ee0e2 200} static_ASN1_SEQUENCE_END(X9_62_CURVE)
012c86ab 201
012c86ab 202ASN1_SEQUENCE(ECPARAMETERS) = {
9612e157 203 ASN1_EMBED(ECPARAMETERS, version, INT32),
0f113f3e
MC
204 ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID),
205 ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
206 ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
207 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
208 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
60b350a3 209} ASN1_SEQUENCE_END(ECPARAMETERS)
012c86ab 210
37af03d3
GT
211DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
212IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
012c86ab
BM
213
214ASN1_CHOICE(ECPKPARAMETERS) = {
0f113f3e
MC
215 ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT),
216 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
217 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
60b350a3 218} ASN1_CHOICE_END(ECPKPARAMETERS)
012c86ab
BM
219
220DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
14a7cfb3 221DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
012c86ab
BM
222IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
223
0bee0e62 224ASN1_SEQUENCE(EC_PRIVATEKEY) = {
9612e157 225 ASN1_EMBED(EC_PRIVATEKEY, version, INT32),
0f113f3e
MC
226 ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
227 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
228 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
df2ee0e2 229} static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
0bee0e62 230
14a7cfb3
BM
231DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
232DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
0bee0e62
BM
233IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
234
7e31164a 235/* some declarations of internal function */
012c86ab 236
0f113f3e 237/* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
37af03d3 238static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
0f113f3e 239/* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
37af03d3 240static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
7e31164a
BM
241
242/* the function definitions */
012c86ab 243
37af03d3 244static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
0f113f3e
MC
245{
246 int ok = 0, nid;
247 BIGNUM *tmp = NULL;
248
249 if (group == NULL || field == NULL)
250 return 0;
251
252 /* clear the old values (if necessary) */
0dfb9398 253 ASN1_OBJECT_free(field->fieldType);
2ace7450 254 ASN1_TYPE_free(field->p.other);
0f113f3e
MC
255
256 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
257 /* set OID for the field */
258 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
259 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
260 goto err;
261 }
262
263 if (nid == NID_X9_62_prime_field) {
264 if ((tmp = BN_new()) == NULL) {
265 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
266 goto err;
267 }
268 /* the parameters are specified by the prime number p */
269 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
270 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
271 goto err;
272 }
273 /* set the prime number */
274 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
275 if (field->p.prime == NULL) {
276 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
277 goto err;
278 }
6903e2e7 279 } else if (nid == NID_X9_62_characteristic_two_field)
b3310161 280#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
281 {
282 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
283 goto err;
284 }
b3310161 285#else
0f113f3e
MC
286 {
287 int field_type;
288 X9_62_CHARACTERISTIC_TWO *char_two;
289
290 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
291 char_two = field->p.char_two;
292
293 if (char_two == NULL) {
294 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
295 goto err;
296 }
297
298 char_two->m = (long)EC_GROUP_get_degree(group);
299
300 field_type = EC_GROUP_get_basis_type(group);
301
302 if (field_type == 0) {
303 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
304 goto err;
305 }
306 /* set base type OID */
307 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
308 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
309 goto err;
310 }
311
312 if (field_type == NID_X9_62_tpBasis) {
313 unsigned int k;
314
315 if (!EC_GROUP_get_trinomial_basis(group, &k))
316 goto err;
317
318 char_two->p.tpBasis = ASN1_INTEGER_new();
90945fa3 319 if (char_two->p.tpBasis == NULL) {
0f113f3e
MC
320 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
321 goto err;
322 }
323 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
324 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
325 goto err;
326 }
327 } else if (field_type == NID_X9_62_ppBasis) {
328 unsigned int k1, k2, k3;
329
330 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
331 goto err;
332
333 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
90945fa3 334 if (char_two->p.ppBasis == NULL) {
0f113f3e
MC
335 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
336 goto err;
337 }
338
339 /* set k? values */
340 char_two->p.ppBasis->k1 = (long)k1;
341 char_two->p.ppBasis->k2 = (long)k2;
342 char_two->p.ppBasis->k3 = (long)k3;
343 } else { /* field_type == NID_X9_62_onBasis */
344
345 /* for ONB the parameters are (asn1) NULL */
346 char_two->p.onBasis = ASN1_NULL_new();
90945fa3 347 if (char_two->p.onBasis == NULL) {
0f113f3e
MC
348 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
349 goto err;
350 }
351 }
352 }
b3310161 353#endif
6903e2e7
DSH
354 else {
355 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
356 goto err;
357 }
012c86ab 358
0f113f3e 359 ok = 1;
012c86ab 360
23a1d5e9
RS
361 err:
362 BN_free(tmp);
26a7d938 363 return ok;
012c86ab
BM
364}
365
37af03d3 366static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
0f113f3e
MC
367{
368 int ok = 0, nid;
369 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
370 unsigned char *buffer_1 = NULL, *buffer_2 = NULL,
371 *a_buf = NULL, *b_buf = NULL;
372 size_t len_1, len_2;
373 unsigned char char_zero = 0;
374
375 if (!group || !curve || !curve->a || !curve->b)
376 return 0;
377
378 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
379 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
380 goto err;
381 }
382
383 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
384
385 /* get a and b */
386 if (nid == NID_X9_62_prime_field) {
387 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
388 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
389 goto err;
390 }
391 }
b3310161 392#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
393 else { /* nid == NID_X9_62_characteristic_two_field */
394
395 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
396 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
397 goto err;
398 }
399 }
b3310161 400#endif
0f113f3e
MC
401 len_1 = (size_t)BN_num_bytes(tmp_1);
402 len_2 = (size_t)BN_num_bytes(tmp_2);
403
404 if (len_1 == 0) {
405 /* len_1 == 0 => a == 0 */
406 a_buf = &char_zero;
407 len_1 = 1;
408 } else {
409 if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) {
410 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
411 goto err;
412 }
413 if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
414 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
415 goto err;
416 }
417 a_buf = buffer_1;
418 }
419
420 if (len_2 == 0) {
421 /* len_2 == 0 => b == 0 */
422 b_buf = &char_zero;
423 len_2 = 1;
424 } else {
425 if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) {
426 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
427 goto err;
428 }
429 if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
430 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
431 goto err;
432 }
433 b_buf = buffer_2;
434 }
435
436 /* set a and b */
f422a514
DSH
437 if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
438 !ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
0f113f3e
MC
439 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
440 goto err;
441 }
442
443 /* set the seed (optional) */
444 if (group->seed) {
445 if (!curve->seed)
446 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
447 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
448 goto err;
449 }
450 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
451 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
452 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
453 (int)group->seed_len)) {
454 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
455 goto err;
456 }
457 } else {
2ace7450
RS
458 ASN1_BIT_STRING_free(curve->seed);
459 curve->seed = NULL;
0f113f3e
MC
460 }
461
462 ok = 1;
463
23a1d5e9 464 err:
b548a1f1
RS
465 OPENSSL_free(buffer_1);
466 OPENSSL_free(buffer_2);
23a1d5e9
RS
467 BN_free(tmp_1);
468 BN_free(tmp_2);
26a7d938 469 return ok;
0f113f3e 470}
012c86ab 471
60b350a3
RS
472ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
473 ECPARAMETERS *params)
0f113f3e 474{
0f113f3e
MC
475 size_t len = 0;
476 ECPARAMETERS *ret = NULL;
be2e334f 477 const BIGNUM *tmp;
0f113f3e
MC
478 unsigned char *buffer = NULL;
479 const EC_POINT *point = NULL;
480 point_conversion_form_t form;
481
60b350a3 482 if (params == NULL) {
0f113f3e 483 if ((ret = ECPARAMETERS_new()) == NULL) {
60b350a3 484 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
485 goto err;
486 }
487 } else
60b350a3 488 ret = params;
0f113f3e
MC
489
490 /* set the version (always one) */
491 ret->version = (long)0x1;
492
493 /* set the fieldID */
494 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
60b350a3 495 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
496 goto err;
497 }
498
499 /* set the curve */
500 if (!ec_asn1_group2curve(group, ret->curve)) {
60b350a3 501 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
502 goto err;
503 }
504
505 /* set the base point */
506 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
60b350a3 507 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
0f113f3e
MC
508 goto err;
509 }
510
511 form = EC_GROUP_get_point_conversion_form(group);
512
981bd8a2 513 len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
0f113f3e 514 if (len == 0) {
60b350a3 515 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
516 goto err;
517 }
0f113f3e 518 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
0110a470 519 OPENSSL_free(buffer);
60b350a3 520 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
521 goto err;
522 }
0110a470 523 ASN1_STRING_set0(ret->base, buffer, len);
0f113f3e
MC
524
525 /* set the order */
be2e334f
DSH
526 tmp = EC_GROUP_get0_order(group);
527 if (tmp == NULL) {
60b350a3 528 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
529 goto err;
530 }
531 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
532 if (ret->order == NULL) {
60b350a3 533 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
534 goto err;
535 }
536
537 /* set the cofactor (optional) */
be2e334f
DSH
538 tmp = EC_GROUP_get0_cofactor(group);
539 if (tmp != NULL) {
0f113f3e
MC
540 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
541 if (ret->cofactor == NULL) {
60b350a3 542 ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
543 goto err;
544 }
545 }
546
23a1d5e9 547 return ret;
0f113f3e 548
23a1d5e9 549 err:
60b350a3 550 if (params == NULL)
23a1d5e9 551 ECPARAMETERS_free(ret);
23a1d5e9 552 return NULL;
0f113f3e
MC
553}
554
60b350a3
RS
555ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
556 ECPKPARAMETERS *params)
0f113f3e
MC
557{
558 int ok = 1, tmp;
559 ECPKPARAMETERS *ret = params;
560
561 if (ret == NULL) {
562 if ((ret = ECPKPARAMETERS_new()) == NULL) {
60b350a3 563 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
564 return NULL;
565 }
566 } else {
0dfb9398 567 if (ret->type == 0)
0f113f3e
MC
568 ASN1_OBJECT_free(ret->value.named_curve);
569 else if (ret->type == 1 && ret->value.parameters)
570 ECPARAMETERS_free(ret->value.parameters);
571 }
572
573 if (EC_GROUP_get_asn1_flag(group)) {
574 /*
575 * use the asn1 OID to describe the the elliptic curve parameters
576 */
577 tmp = EC_GROUP_get_curve_name(group);
578 if (tmp) {
579 ret->type = 0;
580 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
581 ok = 0;
582 } else
0d4fb843 583 /* we don't know the nid => ERROR */
0f113f3e
MC
584 ok = 0;
585 } else {
586 /* use the ECPARAMETERS structure */
587 ret->type = 1;
588 if ((ret->value.parameters =
60b350a3 589 EC_GROUP_get_ecparameters(group, NULL)) == NULL)
0f113f3e
MC
590 ok = 0;
591 }
592
593 if (!ok) {
594 ECPKPARAMETERS_free(ret);
595 return NULL;
596 }
597 return ret;
598}
012c86ab 599
60b350a3 600EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)
0f113f3e
MC
601{
602 int ok = 0, tmp;
603 EC_GROUP *ret = NULL;
604 BIGNUM *p = NULL, *a = NULL, *b = NULL;
605 EC_POINT *point = NULL;
606 long field_bits;
607
608 if (!params->fieldID || !params->fieldID->fieldType ||
609 !params->fieldID->p.ptr) {
60b350a3 610 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
611 goto err;
612 }
613
614 /* now extract the curve parameters a and b */
615 if (!params->curve || !params->curve->a ||
616 !params->curve->a->data || !params->curve->b ||
617 !params->curve->b->data) {
60b350a3 618 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
619 goto err;
620 }
621 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
622 if (a == NULL) {
60b350a3 623 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
0f113f3e
MC
624 goto err;
625 }
626 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
627 if (b == NULL) {
60b350a3 628 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
0f113f3e
MC
629 goto err;
630 }
631
632 /* get the field parameters */
633 tmp = OBJ_obj2nid(params->fieldID->fieldType);
634 if (tmp == NID_X9_62_characteristic_two_field)
b3310161 635#ifdef OPENSSL_NO_EC2M
0f113f3e 636 {
60b350a3 637 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
0f113f3e
MC
638 goto err;
639 }
b3310161 640#else
0f113f3e
MC
641 {
642 X9_62_CHARACTERISTIC_TWO *char_two;
643
644 char_two = params->fieldID->p.char_two;
645
646 field_bits = char_two->m;
647 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
60b350a3 648 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
0f113f3e
MC
649 goto err;
650 }
651
652 if ((p = BN_new()) == NULL) {
60b350a3 653 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
654 goto err;
655 }
656
657 /* get the base type */
658 tmp = OBJ_obj2nid(char_two->type);
659
660 if (tmp == NID_X9_62_tpBasis) {
661 long tmp_long;
662
663 if (!char_two->p.tpBasis) {
60b350a3 664 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
665 goto err;
666 }
667
668 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
669
670 if (!(char_two->m > tmp_long && tmp_long > 0)) {
60b350a3 671 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
0f113f3e
MC
672 EC_R_INVALID_TRINOMIAL_BASIS);
673 goto err;
674 }
675
676 /* create the polynomial */
677 if (!BN_set_bit(p, (int)char_two->m))
678 goto err;
679 if (!BN_set_bit(p, (int)tmp_long))
680 goto err;
681 if (!BN_set_bit(p, 0))
682 goto err;
683 } else if (tmp == NID_X9_62_ppBasis) {
684 X9_62_PENTANOMIAL *penta;
685
686 penta = char_two->p.ppBasis;
687 if (!penta) {
60b350a3 688 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
689 goto err;
690 }
691
692 if (!
693 (char_two->m > penta->k3 && penta->k3 > penta->k2
694 && penta->k2 > penta->k1 && penta->k1 > 0)) {
60b350a3 695 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
0f113f3e
MC
696 EC_R_INVALID_PENTANOMIAL_BASIS);
697 goto err;
698 }
699
700 /* create the polynomial */
701 if (!BN_set_bit(p, (int)char_two->m))
702 goto err;
703 if (!BN_set_bit(p, (int)penta->k1))
704 goto err;
705 if (!BN_set_bit(p, (int)penta->k2))
706 goto err;
707 if (!BN_set_bit(p, (int)penta->k3))
708 goto err;
709 if (!BN_set_bit(p, 0))
710 goto err;
711 } else if (tmp == NID_X9_62_onBasis) {
60b350a3 712 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
0f113f3e
MC
713 goto err;
714 } else { /* error */
715
60b350a3 716 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
717 goto err;
718 }
719
720 /* create the EC_GROUP structure */
721 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
722 }
b3310161 723#endif
0f113f3e
MC
724 else if (tmp == NID_X9_62_prime_field) {
725 /* we have a curve over a prime field */
726 /* extract the prime number */
727 if (!params->fieldID->p.prime) {
60b350a3 728 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
729 goto err;
730 }
731 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
732 if (p == NULL) {
60b350a3 733 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
734 goto err;
735 }
736
737 if (BN_is_negative(p) || BN_is_zero(p)) {
60b350a3 738 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
0f113f3e
MC
739 goto err;
740 }
741
742 field_bits = BN_num_bits(p);
743 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
60b350a3 744 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
0f113f3e
MC
745 goto err;
746 }
747
748 /* create the EC_GROUP structure */
749 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
750 } else {
60b350a3 751 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
0f113f3e
MC
752 goto err;
753 }
754
755 if (ret == NULL) {
60b350a3 756 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
757 goto err;
758 }
759
760 /* extract seed (optional) */
761 if (params->curve->seed != NULL) {
b548a1f1 762 OPENSSL_free(ret->seed);
75ebbd9a 763 if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
60b350a3 764 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
765 goto err;
766 }
767 memcpy(ret->seed, params->curve->seed->data,
768 params->curve->seed->length);
769 ret->seed_len = params->curve->seed->length;
770 }
771
772 if (!params->order || !params->base || !params->base->data) {
60b350a3 773 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
774 goto err;
775 }
776
777 if ((point = EC_POINT_new(ret)) == NULL)
778 goto err;
779
780 /* set the point conversion form */
781 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
782 (params->base->data[0] & ~0x01));
783
784 /* extract the ec point */
785 if (!EC_POINT_oct2point(ret, point, params->base->data,
786 params->base->length, NULL)) {
60b350a3 787 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
788 goto err;
789 }
790
791 /* extract the order */
792 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
60b350a3 793 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
794 goto err;
795 }
796 if (BN_is_negative(a) || BN_is_zero(a)) {
60b350a3 797 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
0f113f3e
MC
798 goto err;
799 }
800 if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
60b350a3 801 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
0f113f3e
MC
802 goto err;
803 }
804
805 /* extract the cofactor (optional) */
806 if (params->cofactor == NULL) {
23a1d5e9
RS
807 BN_free(b);
808 b = NULL;
0f113f3e 809 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
60b350a3 810 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
0f113f3e
MC
811 goto err;
812 }
813 /* set the generator, order and cofactor (if present) */
814 if (!EC_GROUP_set_generator(ret, point, a, b)) {
60b350a3 815 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
816 goto err;
817 }
818
819 ok = 1;
820
8fdc3734
RS
821 err:
822 if (!ok) {
823 EC_GROUP_clear_free(ret);
0f113f3e
MC
824 ret = NULL;
825 }
826
23a1d5e9
RS
827 BN_free(p);
828 BN_free(a);
829 BN_free(b);
8fdc3734 830 EC_POINT_free(point);
26a7d938 831 return ret;
012c86ab
BM
832}
833
60b350a3 834EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params)
0f113f3e
MC
835{
836 EC_GROUP *ret = NULL;
837 int tmp = 0;
838
839 if (params == NULL) {
60b350a3 840 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
0f113f3e
MC
841 return NULL;
842 }
843
844 if (params->type == 0) { /* the curve is given by an OID */
845 tmp = OBJ_obj2nid(params->value.named_curve);
846 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
60b350a3 847 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
0f113f3e
MC
848 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
849 return NULL;
850 }
851 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
852 } else if (params->type == 1) { /* the parameters are given by a
853 * ECPARAMETERS structure */
60b350a3 854 ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
0f113f3e 855 if (!ret) {
60b350a3 856 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
0f113f3e
MC
857 return NULL;
858 }
859 EC_GROUP_set_asn1_flag(ret, 0x0);
860 } else if (params->type == 2) { /* implicitlyCA */
861 return NULL;
862 } else {
60b350a3 863 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
0f113f3e
MC
864 return NULL;
865 }
866
867 return ret;
868}
012c86ab 869
14a7cfb3 870/* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
012c86ab 871
6343829a 872EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)
0f113f3e
MC
873{
874 EC_GROUP *group = NULL;
875 ECPKPARAMETERS *params = NULL;
a46c9789 876 const unsigned char *p = *in;
0f113f3e 877
a46c9789 878 if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
0f113f3e
MC
879 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
880 ECPKPARAMETERS_free(params);
881 return NULL;
882 }
883
60b350a3 884 if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
0f113f3e
MC
885 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
886 ECPKPARAMETERS_free(params);
887 return NULL;
888 }
889
8fdc3734 890 if (a) {
0f113f3e 891 EC_GROUP_clear_free(*a);
0f113f3e 892 *a = group;
8fdc3734 893 }
0f113f3e
MC
894
895 ECPKPARAMETERS_free(params);
a46c9789 896 *in = p;
26a7d938 897 return group;
0f113f3e 898}
012c86ab 899
14a7cfb3 900int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out)
0f113f3e
MC
901{
902 int ret = 0;
60b350a3 903 ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
0f113f3e
MC
904 if (tmp == NULL) {
905 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
906 return 0;
907 }
908 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
909 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
910 ECPKPARAMETERS_free(tmp);
911 return 0;
912 }
913 ECPKPARAMETERS_free(tmp);
26a7d938 914 return ret;
0f113f3e 915}
012c86ab 916
14a7cfb3
BM
917/* some EC_KEY functions */
918
6343829a 919EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)
0f113f3e 920{
0f113f3e
MC
921 EC_KEY *ret = NULL;
922 EC_PRIVATEKEY *priv_key = NULL;
a46c9789 923 const unsigned char *p = *in;
0f113f3e 924
a46c9789 925 if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
0f113f3e 926 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
0f113f3e
MC
927 return NULL;
928 }
929
930 if (a == NULL || *a == NULL) {
931 if ((ret = EC_KEY_new()) == NULL) {
932 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
933 goto err;
934 }
0f113f3e
MC
935 } else
936 ret = *a;
937
938 if (priv_key->parameters) {
8fdc3734 939 EC_GROUP_clear_free(ret->group);
60b350a3 940 ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters);
0f113f3e
MC
941 }
942
943 if (ret->group == NULL) {
944 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
945 goto err;
946 }
947
948 ret->version = priv_key->version;
949
950 if (priv_key->privateKey) {
d810700b 951 ASN1_OCTET_STRING *pkey = priv_key->privateKey;
17ebf85a 952 if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey),
d810700b 953 ASN1_STRING_length(pkey)) == 0)
0f113f3e 954 goto err;
0f113f3e
MC
955 } else {
956 ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
957 goto err;
958 }
959
8fdc3734 960 EC_POINT_clear_free(ret->pub_key);
0f113f3e
MC
961 ret->pub_key = EC_POINT_new(ret->group);
962 if (ret->pub_key == NULL) {
963 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
964 goto err;
965 }
966
967 if (priv_key->publicKey) {
968 const unsigned char *pub_oct;
969 int pub_oct_len;
970
17ebf85a 971 pub_oct = ASN1_STRING_get0_data(priv_key->publicKey);
f422a514 972 pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
6ea04154 973 if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
0f113f3e
MC
974 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
975 goto err;
976 }
977 } else {
77470e98
DSH
978 if (ret->group->meth->keygenpub == NULL
979 || ret->group->meth->keygenpub(ret) == 0)
6903e2e7 980 goto err;
0f113f3e
MC
981 /* Remember the original private-key-only encoding. */
982 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
983 }
984
9e442d48
MC
985 if (a)
986 *a = ret;
25aaa98a 987 EC_PRIVATEKEY_free(priv_key);
a46c9789 988 *in = p;
26a7d938 989 return ret;
25aaa98a
RS
990
991 err:
992 if (a == NULL || *a != ret)
993 EC_KEY_free(ret);
994 EC_PRIVATEKEY_free(priv_key);
995 return NULL;
0f113f3e
MC
996}
997
998int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
999{
1000 int ret = 0, ok = 0;
7fc7d1a7 1001 unsigned char *priv= NULL, *pub= NULL;
8a41fa6f 1002 size_t privlen = 0, publen = 0;
7fc7d1a7 1003
0f113f3e
MC
1004 EC_PRIVATEKEY *priv_key = NULL;
1005
d810700b 1006 if (a == NULL || a->group == NULL ||
0f113f3e
MC
1007 (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
1008 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
1009 goto err;
1010 }
1011
1012 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1013 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1014 goto err;
1015 }
1016
1017 priv_key->version = a->version;
1018
7fc7d1a7 1019 privlen = EC_KEY_priv2buf(a, &priv);
30cd4ff2 1020
7fc7d1a7 1021 if (privlen == 0) {
d810700b 1022 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
30cd4ff2
DE
1023 goto err;
1024 }
1025
7fc7d1a7
DSH
1026 ASN1_STRING_set0(priv_key->privateKey, priv, privlen);
1027 priv = NULL;
0f113f3e
MC
1028
1029 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1030 if ((priv_key->parameters =
60b350a3 1031 EC_GROUP_get_ecpkparameters(a->group,
0f113f3e
MC
1032 priv_key->parameters)) == NULL) {
1033 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1034 goto err;
1035 }
1036 }
1037
1038 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
f422a514 1039 priv_key->publicKey = ASN1_BIT_STRING_new();
0f113f3e
MC
1040 if (priv_key->publicKey == NULL) {
1041 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1042 goto err;
1043 }
1044
7fc7d1a7 1045 publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL);
0f113f3e 1046
7fc7d1a7 1047 if (publen == 0) {
0f113f3e
MC
1048 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1049 goto err;
1050 }
1051
1052 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1053 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
7fc7d1a7
DSH
1054 ASN1_STRING_set0(priv_key->publicKey, pub, publen);
1055 pub = NULL;
0f113f3e
MC
1056 }
1057
1058 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1059 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1060 goto err;
1061 }
1062 ok = 1;
1063 err:
7fc7d1a7
DSH
1064 OPENSSL_clear_free(priv, privlen);
1065 OPENSSL_free(pub);
25aaa98a 1066 EC_PRIVATEKEY_free(priv_key);
0f113f3e
MC
1067 return (ok ? ret : 0);
1068}
14a7cfb3
BM
1069
1070int i2d_ECParameters(EC_KEY *a, unsigned char **out)
0f113f3e
MC
1071{
1072 if (a == NULL) {
1073 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1074 return 0;
1075 }
1076 return i2d_ECPKParameters(a->group, out);
1077}
14a7cfb3
BM
1078
1079EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
0f113f3e
MC
1080{
1081 EC_KEY *ret;
1082
1083 if (in == NULL || *in == NULL) {
1084 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1085 return NULL;
1086 }
1087
1088 if (a == NULL || *a == NULL) {
1089 if ((ret = EC_KEY_new()) == NULL) {
1090 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1091 return NULL;
1092 }
0f113f3e
MC
1093 } else
1094 ret = *a;
1095
1096 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1097 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
5e5d53d3
MC
1098 if (a == NULL || *a != ret)
1099 EC_KEY_free(ret);
0f113f3e
MC
1100 return NULL;
1101 }
1102
5e5d53d3
MC
1103 if (a)
1104 *a = ret;
1105
0f113f3e
MC
1106 return ret;
1107}
14a7cfb3 1108
62e3163b 1109EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
0f113f3e
MC
1110{
1111 EC_KEY *ret = NULL;
1112
1113 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1114 /*
0d4fb843 1115 * sorry, but a EC_GROUP-structure is necessary to set the public key
0f113f3e
MC
1116 */
1117 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1118 return 0;
1119 }
1120 ret = *a;
6ea04154 1121 if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
0f113f3e
MC
1122 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1123 return 0;
1124 }
0f113f3e
MC
1125 *in += len;
1126 return ret;
1127}
14a7cfb3 1128
60c25873 1129int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out)
0f113f3e
MC
1130{
1131 size_t buf_len = 0;
1132 int new_buffer = 0;
1133
1134 if (a == NULL) {
1135 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1136 return 0;
1137 }
1138
1139 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1140 a->conv_form, NULL, 0, NULL);
1141
1142 if (out == NULL || buf_len == 0)
1143 /* out == NULL => just return the length of the octet string */
1144 return buf_len;
1145
1146 if (*out == NULL) {
1147 if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
1148 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1149 return 0;
1150 }
1151 new_buffer = 1;
1152 }
1153 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1154 *out, buf_len, NULL)) {
1155 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1156 if (new_buffer) {
1157 OPENSSL_free(*out);
1158 *out = NULL;
1159 }
1160 return 0;
1161 }
1162 if (!new_buffer)
1163 *out += buf_len;
1164 return buf_len;
1165}
2d3d00dc 1166
2d3d00dc
DSH
1167ASN1_SEQUENCE(ECDSA_SIG) = {
1168 ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
1169 ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
1170} static_ASN1_SEQUENCE_END(ECDSA_SIG)
1171
1172DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
1173DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
8cc44d97
DSH
1174IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSA_SIG, ECDSA_SIG, ECDSA_SIG)
1175
1176ECDSA_SIG *ECDSA_SIG_new(void)
1177{
1178 ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
1179 if (sig == NULL)
1180 ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
1181 return sig;
1182}
1183
1184void ECDSA_SIG_free(ECDSA_SIG *sig)
1185{
1186 if (sig == NULL)
1187 return;
1188 BN_clear_free(sig->r);
1189 BN_clear_free(sig->s);
1190 OPENSSL_free(sig);
1191}
7236e3c8 1192
9267c11b 1193void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
7236e3c8 1194{
91e7bcc2 1195 if (pr != NULL)
7236e3c8 1196 *pr = sig->r;
91e7bcc2 1197 if (ps != NULL)
7236e3c8
DSH
1198 *ps = sig->s;
1199}
cf517a6d 1200
7ca3ea22 1201int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
6a571a18 1202{
bbaa9dd8
TS
1203 if (r == NULL || s == NULL)
1204 return 0;
6a571a18
TS
1205 BN_clear_free(sig->r);
1206 BN_clear_free(sig->s);
1207 sig->r = r;
1208 sig->s = s;
1209 return 1;
1210}
1211
cf517a6d
DSH
1212int ECDSA_size(const EC_KEY *r)
1213{
1214 int ret, i;
1215 ASN1_INTEGER bs;
cf517a6d
DSH
1216 unsigned char buf[4];
1217 const EC_GROUP *group;
1218
1219 if (r == NULL)
1220 return 0;
1221 group = EC_KEY_get0_group(r);
1222 if (group == NULL)
1223 return 0;
1224
be2e334f
DSH
1225 i = EC_GROUP_order_bits(group);
1226 if (i == 0)
cf517a6d 1227 return 0;
cf517a6d
DSH
1228 bs.length = (i + 7) / 8;
1229 bs.data = buf;
1230 bs.type = V_ASN1_INTEGER;
1231 /* If the top bit is set the asn1 encoding is 1 larger. */
1232 buf[0] = 0xff;
1233
1234 i = i2d_ASN1_INTEGER(&bs, NULL);
1235 i += i; /* r and s */
1236 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
26a7d938 1237 return ret;
cf517a6d 1238}