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