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