Add default operations to EC_METHOD
[openssl.git] / crypto / ec / ec_pmeth.c
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 2006.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include "internal/cryptlib.h"
61 #include <openssl/asn1t.h>
62 #include <openssl/x509.h>
63 #include <openssl/ec.h>
64 #include "ec_lcl.h"
65 #include <openssl/evp.h>
66 #include "internal/evp_int.h"
67
68 /* EC pkey context structure */
69
70 typedef struct {
71     /* Key and paramgen group */
72     EC_GROUP *gen_group;
73     /* message digest */
74     const EVP_MD *md;
75     /* Duplicate key if custom cofactor needed */
76     EC_KEY *co_key;
77     /* Cofactor mode */
78     signed char cofactor_mode;
79     /* KDF (if any) to use for ECDH */
80     char kdf_type;
81     /* Message digest to use for key derivation */
82     const EVP_MD *kdf_md;
83     /* User key material */
84     unsigned char *kdf_ukm;
85     size_t kdf_ukmlen;
86     /* KDF output length */
87     size_t kdf_outlen;
88 } EC_PKEY_CTX;
89
90 static int pkey_ec_init(EVP_PKEY_CTX *ctx)
91 {
92     EC_PKEY_CTX *dctx;
93
94     dctx = OPENSSL_zalloc(sizeof(*dctx));
95     if (dctx == NULL)
96         return 0;
97
98     dctx->cofactor_mode = -1;
99     dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
100     ctx->data = dctx;
101     return 1;
102 }
103
104 static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
105 {
106     EC_PKEY_CTX *dctx, *sctx;
107     if (!pkey_ec_init(dst))
108         return 0;
109     sctx = src->data;
110     dctx = dst->data;
111     if (sctx->gen_group) {
112         dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
113         if (!dctx->gen_group)
114             return 0;
115     }
116     dctx->md = sctx->md;
117
118     if (sctx->co_key) {
119         dctx->co_key = EC_KEY_dup(sctx->co_key);
120         if (!dctx->co_key)
121             return 0;
122     }
123     dctx->kdf_type = sctx->kdf_type;
124     dctx->kdf_md = sctx->kdf_md;
125     dctx->kdf_outlen = sctx->kdf_outlen;
126     if (sctx->kdf_ukm) {
127         dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
128         if (!dctx->kdf_ukm)
129             return 0;
130     } else
131         dctx->kdf_ukm = NULL;
132     dctx->kdf_ukmlen = sctx->kdf_ukmlen;
133     return 1;
134 }
135
136 static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
137 {
138     EC_PKEY_CTX *dctx = ctx->data;
139     if (dctx) {
140         EC_GROUP_free(dctx->gen_group);
141         EC_KEY_free(dctx->co_key);
142         OPENSSL_free(dctx->kdf_ukm);
143         OPENSSL_free(dctx);
144     }
145 }
146
147 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
148                         const unsigned char *tbs, size_t tbslen)
149 {
150     int ret, type;
151     unsigned int sltmp;
152     EC_PKEY_CTX *dctx = ctx->data;
153     EC_KEY *ec = ctx->pkey->pkey.ec;
154
155     if (!sig) {
156         *siglen = ECDSA_size(ec);
157         return 1;
158     } else if (*siglen < (size_t)ECDSA_size(ec)) {
159         ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
160         return 0;
161     }
162
163     if (dctx->md)
164         type = EVP_MD_type(dctx->md);
165     else
166         type = NID_sha1;
167
168     ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
169
170     if (ret <= 0)
171         return ret;
172     *siglen = (size_t)sltmp;
173     return 1;
174 }
175
176 static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
177                           const unsigned char *sig, size_t siglen,
178                           const unsigned char *tbs, size_t tbslen)
179 {
180     int ret, type;
181     EC_PKEY_CTX *dctx = ctx->data;
182     EC_KEY *ec = ctx->pkey->pkey.ec;
183
184     if (dctx->md)
185         type = EVP_MD_type(dctx->md);
186     else
187         type = NID_sha1;
188
189     ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
190
191     return ret;
192 }
193
194 #ifndef OPENSSL_NO_EC
195 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
196                           size_t *keylen)
197 {
198     int ret;
199     size_t outlen;
200     const EC_POINT *pubkey = NULL;
201     EC_KEY *eckey;
202     EC_PKEY_CTX *dctx = ctx->data;
203     if (!ctx->pkey || !ctx->peerkey) {
204         ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
205         return 0;
206     }
207
208     eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
209
210     if (!key) {
211         const EC_GROUP *group;
212         group = EC_KEY_get0_group(eckey);
213         *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
214         return 1;
215     }
216     pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
217
218     /*
219      * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
220      * an error, the result is truncated.
221      */
222
223     outlen = *keylen;
224
225     ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
226     if (ret <= 0)
227         return 0;
228     *keylen = ret;
229     return 1;
230 }
231
232 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
233                               unsigned char *key, size_t *keylen)
234 {
235     EC_PKEY_CTX *dctx = ctx->data;
236     unsigned char *ktmp = NULL;
237     size_t ktmplen;
238     int rv = 0;
239     if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
240         return pkey_ec_derive(ctx, key, keylen);
241     if (!key) {
242         *keylen = dctx->kdf_outlen;
243         return 1;
244     }
245     if (*keylen != dctx->kdf_outlen)
246         return 0;
247     if (!pkey_ec_derive(ctx, NULL, &ktmplen))
248         return 0;
249     ktmp = OPENSSL_malloc(ktmplen);
250     if (ktmp == NULL)
251         return 0;
252     if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
253         goto err;
254     /* Do KDF stuff */
255     if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen,
256                         dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
257         goto err;
258     rv = 1;
259
260  err:
261     OPENSSL_clear_free(ktmp, ktmplen);
262     return rv;
263 }
264 #endif
265
266 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
267 {
268     EC_PKEY_CTX *dctx = ctx->data;
269     EC_GROUP *group;
270     switch (type) {
271     case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
272         group = EC_GROUP_new_by_curve_name(p1);
273         if (group == NULL) {
274             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
275             return 0;
276         }
277         EC_GROUP_free(dctx->gen_group);
278         dctx->gen_group = group;
279         return 1;
280
281     case EVP_PKEY_CTRL_EC_PARAM_ENC:
282         if (!dctx->gen_group) {
283             ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
284             return 0;
285         }
286         EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
287         return 1;
288
289 #ifndef OPENSSL_NO_EC
290     case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
291         if (p1 == -2) {
292             if (dctx->cofactor_mode != -1)
293                 return dctx->cofactor_mode;
294             else {
295                 EC_KEY *ec_key = ctx->pkey->pkey.ec;
296                 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 :
297                     0;
298             }
299         } else if (p1 < -1 || p1 > 1)
300             return -2;
301         dctx->cofactor_mode = p1;
302         if (p1 != -1) {
303             EC_KEY *ec_key = ctx->pkey->pkey.ec;
304             if (!ec_key->group)
305                 return -2;
306             /* If cofactor is 1 cofactor mode does nothing */
307             if (BN_is_one(ec_key->group->cofactor))
308                 return 1;
309             if (!dctx->co_key) {
310                 dctx->co_key = EC_KEY_dup(ec_key);
311                 if (!dctx->co_key)
312                     return 0;
313             }
314             if (p1)
315                 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
316             else
317                 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
318         } else {
319             EC_KEY_free(dctx->co_key);
320             dctx->co_key = NULL;
321         }
322         return 1;
323 #endif
324
325     case EVP_PKEY_CTRL_EC_KDF_TYPE:
326         if (p1 == -2)
327             return dctx->kdf_type;
328         if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
329             return -2;
330         dctx->kdf_type = p1;
331         return 1;
332
333     case EVP_PKEY_CTRL_EC_KDF_MD:
334         dctx->kdf_md = p2;
335         return 1;
336
337     case EVP_PKEY_CTRL_GET_EC_KDF_MD:
338         *(const EVP_MD **)p2 = dctx->kdf_md;
339         return 1;
340
341     case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
342         if (p1 <= 0)
343             return -2;
344         dctx->kdf_outlen = (size_t)p1;
345         return 1;
346
347     case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
348         *(int *)p2 = dctx->kdf_outlen;
349         return 1;
350
351     case EVP_PKEY_CTRL_EC_KDF_UKM:
352         OPENSSL_free(dctx->kdf_ukm);
353         dctx->kdf_ukm = p2;
354         if (p2)
355             dctx->kdf_ukmlen = p1;
356         else
357             dctx->kdf_ukmlen = 0;
358         return 1;
359
360     case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
361         *(unsigned char **)p2 = dctx->kdf_ukm;
362         return dctx->kdf_ukmlen;
363
364     case EVP_PKEY_CTRL_MD:
365         if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
366             EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
367             EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
368             EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
369             EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
370             EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
371             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
372             return 0;
373         }
374         dctx->md = p2;
375         return 1;
376
377     case EVP_PKEY_CTRL_GET_MD:
378         *(const EVP_MD **)p2 = dctx->md;
379         return 1;
380
381     case EVP_PKEY_CTRL_PEER_KEY:
382         /* Default behaviour is OK */
383     case EVP_PKEY_CTRL_DIGESTINIT:
384     case EVP_PKEY_CTRL_PKCS7_SIGN:
385     case EVP_PKEY_CTRL_CMS_SIGN:
386         return 1;
387
388     default:
389         return -2;
390
391     }
392 }
393
394 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
395                             const char *type, const char *value)
396 {
397     if (strcmp(type, "ec_paramgen_curve") == 0) {
398         int nid;
399         nid = EC_curve_nist2nid(value);
400         if (nid == NID_undef)
401             nid = OBJ_sn2nid(value);
402         if (nid == NID_undef)
403             nid = OBJ_ln2nid(value);
404         if (nid == NID_undef) {
405             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
406             return 0;
407         }
408         return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
409     } else if (strcmp(type, "ec_param_enc") == 0) {
410         int param_enc;
411         if (strcmp(value, "explicit") == 0)
412             param_enc = 0;
413         else if (strcmp(value, "named_curve") == 0)
414             param_enc = OPENSSL_EC_NAMED_CURVE;
415         else
416             return -2;
417         return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
418     } else if (strcmp(type, "ecdh_kdf_md") == 0) {
419         const EVP_MD *md;
420         if ((md = EVP_get_digestbyname(value)) == NULL) {
421             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
422             return 0;
423         }
424         return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
425     } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
426         int co_mode;
427         co_mode = atoi(value);
428         return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
429     }
430
431     return -2;
432 }
433
434 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
435 {
436     EC_KEY *ec = NULL;
437     EC_PKEY_CTX *dctx = ctx->data;
438     int ret = 0;
439     if (dctx->gen_group == NULL) {
440         ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
441         return 0;
442     }
443     ec = EC_KEY_new();
444     if (ec == NULL)
445         return 0;
446     ret = EC_KEY_set_group(ec, dctx->gen_group);
447     if (ret)
448         EVP_PKEY_assign_EC_KEY(pkey, ec);
449     else
450         EC_KEY_free(ec);
451     return ret;
452 }
453
454 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
455 {
456     EC_KEY *ec = NULL;
457     EC_PKEY_CTX *dctx = ctx->data;
458     if (ctx->pkey == NULL && dctx->gen_group == NULL) {
459         ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
460         return 0;
461     }
462     ec = EC_KEY_new();
463     if (!ec)
464         return 0;
465     EVP_PKEY_assign_EC_KEY(pkey, ec);
466     if (ctx->pkey) {
467         /* Note: if error return, pkey is freed by parent routine */
468         if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
469             return 0;
470     } else {
471         if (!EC_KEY_set_group(ec, dctx->gen_group))
472             return 0;
473     }
474     return EC_KEY_generate_key(pkey->pkey.ec);
475 }
476
477 const EVP_PKEY_METHOD ec_pkey_meth = {
478     EVP_PKEY_EC,
479     0,
480     pkey_ec_init,
481     pkey_ec_copy,
482     pkey_ec_cleanup,
483
484     0,
485     pkey_ec_paramgen,
486
487     0,
488     pkey_ec_keygen,
489
490     0,
491     pkey_ec_sign,
492
493     0,
494     pkey_ec_verify,
495
496     0, 0,
497
498     0, 0, 0, 0,
499
500     0, 0,
501
502     0, 0,
503
504     0,
505 #ifndef OPENSSL_NO_EC
506     pkey_ec_kdf_derive,
507 #else
508     0,
509 #endif
510     pkey_ec_ctrl,
511     pkey_ec_ctrl_str
512 };