blob: c125af2b440179660ecaf0db69a33dc9e6f35976 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Written by Nils Larsch for the OpenSSL project. */
2/* ====================================================================
3 * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com). */
53
54#include <openssl/ec.h>
55
David Benjamin4969cc92016-04-22 15:02:23 -040056#include <limits.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080057#include <string.h>
58
David Benjamin4969cc92016-04-22 15:02:23 -040059#include <openssl/bytestring.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080060#include <openssl/bn.h>
61#include <openssl/err.h>
62#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -040063#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080064
Robert Sloan8ff03552017-06-14 12:40:58 -070065#include "../fipsmodule/ec/internal.h"
David Benjamin4969cc92016-04-22 15:02:23 -040066#include "../bytestring/internal.h"
Robert Sloan69939df2017-01-09 10:53:07 -080067#include "../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080068
69
Robert Sloan99319a12017-11-27 10:32:46 -080070static const unsigned kParametersTag =
David Benjamin4969cc92016-04-22 15:02:23 -040071 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0;
Robert Sloan99319a12017-11-27 10:32:46 -080072static const unsigned kPublicKeyTag =
David Benjamin4969cc92016-04-22 15:02:23 -040073 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -080074
David Benjamin4969cc92016-04-22 15:02:23 -040075EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
76 CBS ec_private_key, private_key;
77 uint64_t version;
78 if (!CBS_get_asn1(cbs, &ec_private_key, CBS_ASN1_SEQUENCE) ||
79 !CBS_get_asn1_uint64(&ec_private_key, &version) ||
80 version != 1 ||
81 !CBS_get_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING)) {
82 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -080083 return NULL;
84 }
85
Robert Sloan8f860b12017-08-28 07:37:06 -070086 // Parse the optional parameters field.
David Benjamin4969cc92016-04-22 15:02:23 -040087 EC_GROUP *inner_group = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -080088 EC_KEY *ret = NULL;
David Benjamin4969cc92016-04-22 15:02:23 -040089 if (CBS_peek_asn1_tag(&ec_private_key, kParametersTag)) {
Robert Sloan8f860b12017-08-28 07:37:06 -070090 // Per SEC 1, as an alternative to omitting it, one is allowed to specify
91 // this field and put in a NULL to mean inheriting this value. This was
92 // omitted in a previous version of this logic without problems, so leave it
93 // unimplemented.
David Benjamin4969cc92016-04-22 15:02:23 -040094 CBS child;
95 if (!CBS_get_asn1(&ec_private_key, &child, kParametersTag)) {
96 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -080097 goto err;
98 }
David Benjamin4969cc92016-04-22 15:02:23 -040099 inner_group = EC_KEY_parse_parameters(&child);
100 if (inner_group == NULL) {
101 goto err;
102 }
103 if (group == NULL) {
104 group = inner_group;
105 } else if (EC_GROUP_cmp(group, inner_group, NULL) != 0) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700106 // If a group was supplied externally, it must match.
David Benjamin4969cc92016-04-22 15:02:23 -0400107 OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
108 goto err;
109 }
110 if (CBS_len(&child) != 0) {
111 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
112 goto err;
113 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800114 }
115
David Benjamin4969cc92016-04-22 15:02:23 -0400116 if (group == NULL) {
117 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800118 goto err;
119 }
120
David Benjamin4969cc92016-04-22 15:02:23 -0400121 ret = EC_KEY_new();
122 if (ret == NULL || !EC_KEY_set_group(ret, group)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123 goto err;
124 }
125
Robert Sloan8f860b12017-08-28 07:37:06 -0700126 // Although RFC 5915 specifies the length of the key, OpenSSL historically
127 // got this wrong, so accept any length. See upstream's
128 // 30cd4ff294252c4b6a4b69cbef6a5b4117705d22.
David Benjamin4969cc92016-04-22 15:02:23 -0400129 ret->priv_key =
130 BN_bin2bn(CBS_data(&private_key), CBS_len(&private_key), NULL);
131 ret->pub_key = EC_POINT_new(group);
132 if (ret->priv_key == NULL || ret->pub_key == NULL) {
133 goto err;
134 }
135
136 if (BN_cmp(ret->priv_key, EC_GROUP_get0_order(group)) >= 0) {
Adam Langley4139edb2016-01-13 15:00:54 -0800137 OPENSSL_PUT_ERROR(EC, EC_R_WRONG_ORDER);
138 goto err;
139 }
140
David Benjamin4969cc92016-04-22 15:02:23 -0400141 if (CBS_peek_asn1_tag(&ec_private_key, kPublicKeyTag)) {
142 CBS child, public_key;
143 uint8_t padding;
144 if (!CBS_get_asn1(&ec_private_key, &child, kPublicKeyTag) ||
145 !CBS_get_asn1(&child, &public_key, CBS_ASN1_BITSTRING) ||
Robert Sloan8f860b12017-08-28 07:37:06 -0700146 // As in a SubjectPublicKeyInfo, the byte-encoded public key is then
147 // encoded as a BIT STRING with bits ordered as in the DER encoding.
David Benjamin4969cc92016-04-22 15:02:23 -0400148 !CBS_get_u8(&public_key, &padding) ||
149 padding != 0 ||
Robert Sloan8f860b12017-08-28 07:37:06 -0700150 // Explicitly check |public_key| is non-empty to save the conversion
151 // form later.
David Benjamin4969cc92016-04-22 15:02:23 -0400152 CBS_len(&public_key) == 0 ||
153 !EC_POINT_oct2point(group, ret->pub_key, CBS_data(&public_key),
154 CBS_len(&public_key), NULL) ||
155 CBS_len(&child) != 0) {
156 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157 goto err;
158 }
David Benjamin4969cc92016-04-22 15:02:23 -0400159
Robert Sloan8f860b12017-08-28 07:37:06 -0700160 // Save the point conversion form.
161 // TODO(davidben): Consider removing this.
Robert Sloan8ff03552017-06-14 12:40:58 -0700162 ret->conv_form =
163 (point_conversion_form_t)(CBS_data(&public_key)[0] & ~0x01);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164 } else {
Robert Sloan8f860b12017-08-28 07:37:06 -0700165 // Compute the public key instead.
David Benjamin4969cc92016-04-22 15:02:23 -0400166 if (!EC_POINT_mul(group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167 goto err;
168 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700169 // Remember the original private-key-only encoding.
170 // TODO(davidben): Consider removing this.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171 ret->enc_flag |= EC_PKEY_NO_PUBKEY;
172 }
173
David Benjamin4969cc92016-04-22 15:02:23 -0400174 if (CBS_len(&ec_private_key) != 0) {
175 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
176 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700177 }
David Benjamin4969cc92016-04-22 15:02:23 -0400178
Robert Sloan8f860b12017-08-28 07:37:06 -0700179 // Ensure the resulting key is valid.
David Benjamin4969cc92016-04-22 15:02:23 -0400180 if (!EC_KEY_check_key(ret)) {
181 goto err;
182 }
183
184 EC_GROUP_free(inner_group);
185 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800186
187err:
David Benjamin4969cc92016-04-22 15:02:23 -0400188 EC_KEY_free(ret);
189 EC_GROUP_free(inner_group);
190 return NULL;
191}
192
193int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
194 unsigned enc_flags) {
195 if (key == NULL || key->group == NULL || key->priv_key == NULL) {
196 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
197 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198 }
199
David Benjamin4969cc92016-04-22 15:02:23 -0400200 CBB ec_private_key, private_key;
201 if (!CBB_add_asn1(cbb, &ec_private_key, CBS_ASN1_SEQUENCE) ||
202 !CBB_add_asn1_uint64(&ec_private_key, 1 /* version */) ||
203 !CBB_add_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING) ||
204 !BN_bn2cbb_padded(&private_key,
205 BN_num_bytes(EC_GROUP_get0_order(key->group)),
206 key->priv_key)) {
207 OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
208 return 0;
209 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210
David Benjamin4969cc92016-04-22 15:02:23 -0400211 if (!(enc_flags & EC_PKEY_NO_PARAMETERS)) {
212 CBB child;
213 if (!CBB_add_asn1(&ec_private_key, &child, kParametersTag) ||
214 !EC_KEY_marshal_curve_name(&child, key->group) ||
215 !CBB_flush(&ec_private_key)) {
216 OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
217 return 0;
218 }
219 }
220
Robert Sloan8f860b12017-08-28 07:37:06 -0700221 // TODO(fork): replace this flexibility with sensible default?
David Benjamin4969cc92016-04-22 15:02:23 -0400222 if (!(enc_flags & EC_PKEY_NO_PUBKEY) && key->pub_key != NULL) {
223 CBB child, public_key;
224 if (!CBB_add_asn1(&ec_private_key, &child, kPublicKeyTag) ||
225 !CBB_add_asn1(&child, &public_key, CBS_ASN1_BITSTRING) ||
Robert Sloan8f860b12017-08-28 07:37:06 -0700226 // As in a SubjectPublicKeyInfo, the byte-encoded public key is then
227 // encoded as a BIT STRING with bits ordered as in the DER encoding.
David Benjamin4969cc92016-04-22 15:02:23 -0400228 !CBB_add_u8(&public_key, 0 /* padding */) ||
229 !EC_POINT_point2cbb(&public_key, key->group, key->pub_key,
230 key->conv_form, NULL) ||
231 !CBB_flush(&ec_private_key)) {
232 OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
233 return 0;
234 }
235 }
236
237 if (!CBB_flush(cbb)) {
238 OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR);
239 return 0;
240 }
241
242 return 1;
243}
244
Robert Sloan8f860b12017-08-28 07:37:06 -0700245// is_unsigned_integer returns one if |cbs| is a valid unsigned DER INTEGER and
246// zero otherwise.
David Benjamin4969cc92016-04-22 15:02:23 -0400247static int is_unsigned_integer(const CBS *cbs) {
248 if (CBS_len(cbs) == 0) {
249 return 0;
250 }
251 uint8_t byte = CBS_data(cbs)[0];
252 if ((byte & 0x80) ||
253 (byte == 0 && CBS_len(cbs) > 1 && (CBS_data(cbs)[1] & 0x80) == 0)) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700254 // Negative or not minimally-encoded.
David Benjamin4969cc92016-04-22 15:02:23 -0400255 return 0;
256 }
257 return 1;
258}
259
Robert Sloan8f860b12017-08-28 07:37:06 -0700260// kPrimeFieldOID is the encoding of 1.2.840.10045.1.1.
David Benjamin4969cc92016-04-22 15:02:23 -0400261static const uint8_t kPrimeField[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01};
262
263static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a,
264 CBS *out_b, CBS *out_base_x,
265 CBS *out_base_y, CBS *out_order) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700266 // See RFC 3279, section 2.3.5. Note that RFC 3279 calls this structure an
267 // ECParameters while RFC 5480 calls it a SpecifiedECDomain.
David Benjamin4969cc92016-04-22 15:02:23 -0400268 CBS params, field_id, field_type, curve, base;
269 uint64_t version;
270 if (!CBS_get_asn1(in, &params, CBS_ASN1_SEQUENCE) ||
271 !CBS_get_asn1_uint64(&params, &version) ||
272 version != 1 ||
273 !CBS_get_asn1(&params, &field_id, CBS_ASN1_SEQUENCE) ||
274 !CBS_get_asn1(&field_id, &field_type, CBS_ASN1_OBJECT) ||
275 CBS_len(&field_type) != sizeof(kPrimeField) ||
Robert Sloan69939df2017-01-09 10:53:07 -0800276 OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) != 0 ||
David Benjamin4969cc92016-04-22 15:02:23 -0400277 !CBS_get_asn1(&field_id, out_prime, CBS_ASN1_INTEGER) ||
278 !is_unsigned_integer(out_prime) ||
279 CBS_len(&field_id) != 0 ||
280 !CBS_get_asn1(&params, &curve, CBS_ASN1_SEQUENCE) ||
281 !CBS_get_asn1(&curve, out_a, CBS_ASN1_OCTETSTRING) ||
282 !CBS_get_asn1(&curve, out_b, CBS_ASN1_OCTETSTRING) ||
Robert Sloan8f860b12017-08-28 07:37:06 -0700283 // |curve| has an optional BIT STRING seed which we ignore.
David Benjamin4969cc92016-04-22 15:02:23 -0400284 !CBS_get_asn1(&params, &base, CBS_ASN1_OCTETSTRING) ||
285 !CBS_get_asn1(&params, out_order, CBS_ASN1_INTEGER) ||
286 !is_unsigned_integer(out_order)) {
287 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
288 return 0;
289 }
290
Robert Sloan8f860b12017-08-28 07:37:06 -0700291 // |params| has an optional cofactor which we ignore. With the optional seed
292 // in |curve|, a group already has arbitrarily many encodings. Parse enough to
293 // uniquely determine the curve.
David Benjamin4969cc92016-04-22 15:02:23 -0400294
Robert Sloan8f860b12017-08-28 07:37:06 -0700295 // Require that the base point use uncompressed form.
David Benjamin4969cc92016-04-22 15:02:23 -0400296 uint8_t form;
297 if (!CBS_get_u8(&base, &form) || form != POINT_CONVERSION_UNCOMPRESSED) {
298 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
299 return 0;
300 }
301
302 if (CBS_len(&base) % 2 != 0) {
303 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
304 return 0;
305 }
306 size_t field_len = CBS_len(&base) / 2;
307 CBS_init(out_base_x, CBS_data(&base), field_len);
308 CBS_init(out_base_y, CBS_data(&base) + field_len, field_len);
309
310 return 1;
311}
312
Robert Sloan8f860b12017-08-28 07:37:06 -0700313// integers_equal returns one if |a| and |b| are equal, up to leading zeros, and
314// zero otherwise.
David Benjamin4969cc92016-04-22 15:02:23 -0400315static int integers_equal(const CBS *a, const uint8_t *b, size_t b_len) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700316 // Remove leading zeros from |a| and |b|.
David Benjamin4969cc92016-04-22 15:02:23 -0400317 CBS a_copy = *a;
318 while (CBS_len(&a_copy) > 0 && CBS_data(&a_copy)[0] == 0) {
319 CBS_skip(&a_copy, 1);
320 }
321 while (b_len > 0 && b[0] == 0) {
322 b++;
323 b_len--;
324 }
325 return CBS_mem_equal(&a_copy, b, b_len);
326}
327
328EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) {
329 CBS named_curve;
330 if (!CBS_get_asn1(cbs, &named_curve, CBS_ASN1_OBJECT)) {
331 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
332 return NULL;
333 }
334
Robert Sloan8f860b12017-08-28 07:37:06 -0700335 // Look for a matching curve.
Robert Sloan8ff03552017-06-14 12:40:58 -0700336 const struct built_in_curves *const curves = OPENSSL_built_in_curves();
337 for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
338 const struct built_in_curve *curve = &curves->curves[i];
David Benjamin4969cc92016-04-22 15:02:23 -0400339 if (CBS_len(&named_curve) == curve->oid_len &&
Robert Sloan8ff03552017-06-14 12:40:58 -0700340 OPENSSL_memcmp(CBS_data(&named_curve), curve->oid, curve->oid_len) ==
341 0) {
David Benjamin4969cc92016-04-22 15:02:23 -0400342 return EC_GROUP_new_by_curve_name(curve->nid);
343 }
344 }
345
346 OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
347 return NULL;
348}
349
350int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group) {
351 int nid = EC_GROUP_get_curve_name(group);
352 if (nid == NID_undef) {
353 OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
354 return 0;
355 }
356
Robert Sloan8ff03552017-06-14 12:40:58 -0700357 const struct built_in_curves *const curves = OPENSSL_built_in_curves();
358 for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
359 const struct built_in_curve *curve = &curves->curves[i];
David Benjamin4969cc92016-04-22 15:02:23 -0400360 if (curve->nid == nid) {
361 CBB child;
362 return CBB_add_asn1(cbb, &child, CBS_ASN1_OBJECT) &&
363 CBB_add_bytes(&child, curve->oid, curve->oid_len) &&
364 CBB_flush(cbb);
365 }
366 }
367
368 OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
369 return 0;
370}
371
372EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) {
373 if (!CBS_peek_asn1_tag(cbs, CBS_ASN1_SEQUENCE)) {
374 return EC_KEY_parse_curve_name(cbs);
375 }
376
Robert Sloan8f860b12017-08-28 07:37:06 -0700377 // OpenSSL sometimes produces ECPrivateKeys with explicitly-encoded versions
378 // of named curves.
379 //
380 // TODO(davidben): Remove support for this.
David Benjamin4969cc92016-04-22 15:02:23 -0400381 CBS prime, a, b, base_x, base_y, order;
382 if (!parse_explicit_prime_curve(cbs, &prime, &a, &b, &base_x, &base_y,
383 &order)) {
384 return NULL;
385 }
386
Robert Sloan8f860b12017-08-28 07:37:06 -0700387 // Look for a matching prime curve.
Robert Sloan8ff03552017-06-14 12:40:58 -0700388 const struct built_in_curves *const curves = OPENSSL_built_in_curves();
389 for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
390 const struct built_in_curve *curve = &curves->curves[i];
Robert Sloane56da3e2017-06-26 08:26:42 -0700391 const unsigned param_len = curve->param_len;
Robert Sloan8f860b12017-08-28 07:37:06 -0700392 // |curve->params| is ordered p, a, b, x, y, order, each component
393 // zero-padded up to the field length. Although SEC 1 states that the
394 // Field-Element-to-Octet-String conversion also pads, OpenSSL mis-encodes
395 // |a| and |b|, so this comparison must allow omitting leading zeros. (This
396 // is relevant for P-521 whose |b| has a leading 0.)
Robert Sloane56da3e2017-06-26 08:26:42 -0700397 if (integers_equal(&prime, curve->params, param_len) &&
398 integers_equal(&a, curve->params + param_len, param_len) &&
399 integers_equal(&b, curve->params + param_len * 2, param_len) &&
400 integers_equal(&base_x, curve->params + param_len * 3, param_len) &&
401 integers_equal(&base_y, curve->params + param_len * 4, param_len) &&
402 integers_equal(&order, curve->params + param_len * 5, param_len)) {
David Benjamin4969cc92016-04-22 15:02:23 -0400403 return EC_GROUP_new_by_curve_name(curve->nid);
404 }
405 }
406
407 OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
408 return NULL;
409}
410
Robert Sloan8ff03552017-06-14 12:40:58 -0700411int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point,
412 point_conversion_form_t form, BN_CTX *ctx) {
413 size_t len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx);
414 if (len == 0) {
415 return 0;
416 }
417 uint8_t *p;
418 return CBB_add_space(out, &p, len) &&
419 EC_POINT_point2oct(group, point, form, p, len, ctx) == len;
420}
421
David Benjamin4969cc92016-04-22 15:02:23 -0400422EC_KEY *d2i_ECPrivateKey(EC_KEY **out, const uint8_t **inp, long len) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700423 // This function treats its |out| parameter differently from other |d2i|
424 // functions. If supplied, take the group from |*out|.
David Benjamin4969cc92016-04-22 15:02:23 -0400425 const EC_GROUP *group = NULL;
426 if (out != NULL && *out != NULL) {
427 group = EC_KEY_get0_group(*out);
428 }
429
430 if (len < 0) {
431 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
432 return NULL;
433 }
434 CBS cbs;
435 CBS_init(&cbs, *inp, (size_t)len);
436 EC_KEY *ret = EC_KEY_parse_private_key(&cbs, group);
437 if (ret == NULL) {
438 return NULL;
439 }
440 if (out != NULL) {
441 EC_KEY_free(*out);
442 *out = ret;
443 }
444 *inp = CBS_data(&cbs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800445 return ret;
446}
447
448int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp) {
David Benjamin4969cc92016-04-22 15:02:23 -0400449 CBB cbb;
450 if (!CBB_init(&cbb, 0) ||
451 !EC_KEY_marshal_private_key(&cbb, key, EC_KEY_get_enc_flags(key))) {
452 CBB_cleanup(&cbb);
453 return -1;
454 }
455 return CBB_finish_i2d(&cbb, outp);
456}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800457
David Benjamin4969cc92016-04-22 15:02:23 -0400458EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, long len) {
459 if (len < 0) {
460 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800461 }
462
David Benjamin4969cc92016-04-22 15:02:23 -0400463 CBS cbs;
464 CBS_init(&cbs, *inp, (size_t)len);
465 EC_GROUP *group = EC_KEY_parse_parameters(&cbs);
466 if (group == NULL) {
467 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800468 }
469
David Benjamin4969cc92016-04-22 15:02:23 -0400470 EC_KEY *ret = EC_KEY_new();
471 if (ret == NULL || !EC_KEY_set_group(ret, group)) {
472 EC_GROUP_free(group);
473 EC_KEY_free(ret);
474 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800475 }
David Benjamin4969cc92016-04-22 15:02:23 -0400476 EC_GROUP_free(group);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800477
David Benjamin4969cc92016-04-22 15:02:23 -0400478 if (out_key != NULL) {
479 EC_KEY_free(*out_key);
480 *out_key = ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800481 }
David Benjamin4969cc92016-04-22 15:02:23 -0400482 *inp = CBS_data(&cbs);
483 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800484}
485
486int i2d_ECParameters(const EC_KEY *key, uint8_t **outp) {
David Benjamin4969cc92016-04-22 15:02:23 -0400487 if (key == NULL || key->group == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000488 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
David Benjamin4969cc92016-04-22 15:02:23 -0400489 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800490 }
491
David Benjamin4969cc92016-04-22 15:02:23 -0400492 CBB cbb;
493 if (!CBB_init(&cbb, 0) ||
494 !EC_KEY_marshal_curve_name(&cbb, key->group)) {
495 CBB_cleanup(&cbb);
496 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800497 }
David Benjamin4969cc92016-04-22 15:02:23 -0400498 return CBB_finish_i2d(&cbb, outp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800499}
500
501EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) {
502 EC_KEY *ret = NULL;
503
504 if (keyp == NULL || *keyp == NULL || (*keyp)->group == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000505 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
David Benjamin4969cc92016-04-22 15:02:23 -0400506 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800507 }
508 ret = *keyp;
509 if (ret->pub_key == NULL &&
510 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000511 OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
David Benjamin4969cc92016-04-22 15:02:23 -0400512 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800513 }
514 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *inp, len, NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000515 OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
David Benjamin4969cc92016-04-22 15:02:23 -0400516 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800517 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700518 // save the point conversion form
Adam Langleyd9e397b2015-01-22 14:27:53 -0800519 ret->conv_form = (point_conversion_form_t)(*inp[0] & ~0x01);
520 *inp += len;
521 return ret;
522}
523
524int i2o_ECPublicKey(const EC_KEY *key, uint8_t **outp) {
525 size_t buf_len = 0;
526 int new_buffer = 0;
527
528 if (key == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000529 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800530 return 0;
531 }
532
533 buf_len = EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, NULL,
534 0, NULL);
535
536 if (outp == NULL || buf_len == 0) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700537 // out == NULL => just return the length of the octet string
Adam Langleyd9e397b2015-01-22 14:27:53 -0800538 return buf_len;
539 }
540
541 if (*outp == NULL) {
542 *outp = OPENSSL_malloc(buf_len);
543 if (*outp == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000544 OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800545 return 0;
546 }
547 new_buffer = 1;
548 }
549 if (!EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, *outp,
550 buf_len, NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000551 OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800552 if (new_buffer) {
553 OPENSSL_free(*outp);
554 *outp = NULL;
555 }
556 return 0;
557 }
558
559 if (!new_buffer) {
560 *outp += buf_len;
561 }
562 return buf_len;
563}