Transfer parameter handling and key comparison to algorithm methods.
[openssl.git] / crypto / ec / ec_ameth.c
1 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
2  * project 2006.
3  */
4 /* ====================================================================
5  * Copyright (c) 2005 The OpenSSL Project.  All rights reserved.
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
12  *    notice, this list of conditions and the following disclaimer. 
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
58 #include <stdio.h>
59 #include "cryptlib.h"
60 #include <openssl/x509.h>
61 #include <openssl/ec.h>
62
63 static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key)
64         {
65         const EC_GROUP  *group;
66         int nid;
67         if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) 
68         {
69                 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
70                 return 0;
71         }
72         if (EC_GROUP_get_asn1_flag(group)
73                      && (nid = EC_GROUP_get_curve_name(group)))
74                 /* we have a 'named curve' => just set the OID */
75                 {
76                 *ppval = OBJ_nid2obj(nid);
77                 *pptype = V_ASN1_OBJECT;
78                 }
79         else    /* explicit parameters */
80                 {
81                 ASN1_STRING *pstr = NULL;
82                 pstr = ASN1_STRING_new();
83                 if (!pstr)
84                         return 0;
85                 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
86                 if (pstr->length < 0)
87                         {
88                         ASN1_STRING_free(pstr);
89                         ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
90                         return 0;
91                         }
92                 *ppval = pstr;
93                 *pptype = V_ASN1_SEQUENCE;
94                 }
95         return 1;
96         }
97
98 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
99         {
100         EC_KEY *ec_key = pkey->pkey.ec;
101         void *pval = NULL;
102         int ptype;
103         unsigned char *penc = NULL, *p;
104         int penclen;
105
106         if (!eckey_param2type(&ptype, &pval, ec_key))
107                 {
108                 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
109                 return 0;
110                 }
111         penclen = i2o_ECPublicKey(ec_key, NULL);
112         if (penclen <= 0)
113                 goto err;
114         penc = OPENSSL_malloc(penclen);
115         if (!penc)
116                 goto err;
117         p = penc;
118         penclen = i2o_ECPublicKey(ec_key, &p);
119         if (penclen <= 0)
120                 goto err;
121         if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA),
122                                 ptype, pval, penc, penclen))
123                 return 1;
124         err:
125         if (ptype == V_ASN1_OBJECT)
126                 ASN1_OBJECT_free(pval);
127         else
128                 ASN1_STRING_free(pval);
129         if (penc)
130                 OPENSSL_free(penc);
131         return 0;
132         }
133
134 static EC_KEY *eckey_type2param(int ptype, void *pval)
135         {
136         EC_KEY *eckey = NULL;
137         if (ptype == V_ASN1_SEQUENCE)
138                 {
139                 ASN1_STRING *pstr = pval;
140                 const unsigned char *pm = NULL;
141                 int pmlen;
142                 pm = pstr->data;
143                 pmlen = pstr->length;
144                 if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen)))
145                         {
146                         ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
147                         goto ecerr;
148                         }
149                 }
150         else if (ptype == V_ASN1_OBJECT)
151                 {
152                 ASN1_OBJECT *poid = pval;
153                 EC_GROUP *group;
154
155                 /* type == V_ASN1_OBJECT => the parameters are given
156                  * by an asn1 OID
157                  */
158                 if ((eckey = EC_KEY_new()) == NULL)
159                         {
160                         ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
161                         goto ecerr;
162                         }
163                 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
164                 if (group == NULL)
165                         goto ecerr;
166                 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
167                 if (EC_KEY_set_group(eckey, group) == 0)
168                         goto ecerr;
169                 EC_GROUP_free(group);
170                 }
171         else
172                 {
173                 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
174                 goto ecerr;
175                 }
176
177         return eckey;
178
179         ecerr:
180         if (eckey)
181                 EC_KEY_free(eckey);
182         return NULL;
183         }
184
185 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
186         {
187         const unsigned char *p = NULL;
188         void *pval;
189         int ptype, pklen;
190         EC_KEY *eckey = NULL;
191         X509_ALGOR *palg;
192
193         if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
194                 return 0;
195         X509_ALGOR_get0(NULL, &ptype, &pval, palg);
196
197         eckey = eckey_type2param(ptype, pval);
198
199         if (!eckey)
200                 {
201                 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
202                 return 0;
203                 }
204
205         /* We have parameters now set public key */
206         if (!o2i_ECPublicKey(&eckey, &p, pklen))
207                 {
208                 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
209                 goto ecerr;
210                 }
211
212         EVP_PKEY_assign_EC_KEY(pkey, eckey);
213         return 1;
214
215         ecerr:
216         if (eckey)
217                 EC_KEY_free(eckey);
218         return 0;
219         }
220
221 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
222         {
223         int  r;
224         const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
225         const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
226                        *pb = EC_KEY_get0_public_key(b->pkey.ec);
227         r = EC_POINT_cmp(group, pa, pb, NULL);
228         if (r == 0)
229                 return 1;
230         if (r == 1)
231                 return 0;
232         return -2;
233         }
234
235 static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
236         {
237         const unsigned char *p = NULL;
238         void *pval;
239         int ptype, pklen;
240         EC_KEY *eckey = NULL;
241         X509_ALGOR *palg;
242
243         if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
244                 return 0;
245         X509_ALGOR_get0(NULL, &ptype, &pval, palg);
246
247         eckey = eckey_type2param(ptype, pval);
248
249         if (!eckey)
250                 goto ecliberr;
251
252         /* We have parameters now set private key */
253         if (!d2i_ECPrivateKey(&eckey, &p, pklen))
254                 {
255                 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
256                 goto ecerr;
257                 }
258
259         /* calculate public key (if necessary) */
260         if (EC_KEY_get0_public_key(eckey) == NULL)
261                 {
262                 const BIGNUM *priv_key;
263                 const EC_GROUP *group;
264                 EC_POINT *pub_key;
265                 /* the public key was not included in the SEC1 private
266                  * key => calculate the public key */
267                 group   = EC_KEY_get0_group(eckey);
268                 pub_key = EC_POINT_new(group);
269                 if (pub_key == NULL)
270                         {
271                         ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
272                         goto ecliberr;
273                         }
274                 if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group)))
275                         {
276                         EC_POINT_free(pub_key);
277                         ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
278                         goto ecliberr;
279                         }
280                 priv_key = EC_KEY_get0_private_key(eckey);
281                 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL))
282                         {
283                         EC_POINT_free(pub_key);
284                         ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
285                         goto ecliberr;
286                         }
287                 if (EC_KEY_set_public_key(eckey, pub_key) == 0)
288                         {
289                         EC_POINT_free(pub_key);
290                         ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
291                         goto ecliberr;
292                         }
293                 EC_POINT_free(pub_key);
294                 }
295
296         EVP_PKEY_assign_EC_KEY(pkey, eckey);
297         return 1;
298
299         ecliberr:
300         ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
301         ecerr:
302         if (eckey)
303                 EC_KEY_free(eckey);
304         return 0;
305         }
306
307 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
308 {
309         EC_KEY          *ec_key;
310         unsigned char   *ep, *p;
311         int             eplen, ptype;
312         void            *pval;
313         unsigned int    tmp_flags, old_flags;
314
315         ec_key = pkey->pkey.ec;
316
317         if (!eckey_param2type(&ptype, &pval, ec_key))
318                 {
319                 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
320                 return 0;
321                 }
322
323         /* set the private key */
324
325         /* do not include the parameters in the SEC1 private key
326          * see PKCS#11 12.11 */
327         old_flags = EC_KEY_get_enc_flags(ec_key);
328         tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
329         EC_KEY_set_enc_flags(ec_key, tmp_flags);
330         eplen = i2d_ECPrivateKey(ec_key, NULL);
331         if (!eplen)
332         {
333                 EC_KEY_set_enc_flags(ec_key, old_flags);
334                 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
335                 return 0;
336         }
337         ep = (unsigned char *) OPENSSL_malloc(eplen);
338         if (!ep)
339         {
340                 EC_KEY_set_enc_flags(ec_key, old_flags);
341                 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
342                 return 0;
343         }
344         p = ep;
345         if (!i2d_ECPrivateKey(ec_key, &p))
346         {
347                 EC_KEY_set_enc_flags(ec_key, old_flags);
348                 OPENSSL_free(ep);
349                 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
350         }
351         /* restore old encoding flags */
352         EC_KEY_set_enc_flags(ec_key, old_flags);
353
354         if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
355                                 ptype, pval, ep, eplen))
356                 return 0;
357
358         return 1;
359 }
360
361 static int int_ec_size(const EVP_PKEY *pkey)
362         {
363         return ECDSA_size(pkey->pkey.ec);
364         }
365
366 static int ec_bits(const EVP_PKEY *pkey)
367         {
368         BIGNUM *order = BN_new();
369         const EC_GROUP *group;
370         int ret;
371
372         if (!order)
373                 {
374                 ERR_clear_error();
375                 return 0;
376                 }
377         group = EC_KEY_get0_group(pkey->pkey.ec);
378         if (!EC_GROUP_get_order(group, order, NULL))
379                 {
380                 ERR_clear_error();
381                 return 0;
382                 }
383
384         ret = BN_num_bits(order);
385         BN_free(order);
386         return ret;
387         }
388
389 static int ec_missing_parameters(const EVP_PKEY *pkey)
390         {
391         if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
392                 return 1;
393         return 0;
394         }
395
396 int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
397         {
398         EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
399         if (group == NULL)
400                 return 0;
401         if (EC_KEY_set_group(to->pkey.ec, group) == 0)
402                 return 0;
403         EC_GROUP_free(group);
404         return 1;
405         }
406
407 int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
408         {
409         const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
410                        *group_b = EC_KEY_get0_group(b->pkey.ec);
411         if (EC_GROUP_cmp(group_a, group_b, NULL))
412                 return 0;
413         else
414                 return 1;
415         }
416
417 static void int_ec_free(EVP_PKEY *pkey)
418         {
419         EC_KEY_free(pkey->pkey.ec);
420         }
421
422 EVP_PKEY_ASN1_METHOD eckey_asn1_meth = 
423         {
424         EVP_PKEY_EC,
425         0,
426         0,
427
428         eckey_pub_decode,
429         eckey_pub_encode,
430         eckey_pub_cmp,
431         0,
432
433         eckey_priv_decode,
434         eckey_priv_encode,
435         0,
436
437         int_ec_size,
438         ec_bits,
439
440         0,0,    
441         ec_missing_parameters,
442         ec_copy_parameters,
443         ec_cmp_parameters,
444         0,
445
446         int_ec_free,
447         0
448         };