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