08dda12eeb5e26becd748331bd2a2e14a18704f2
[openssl.git] / crypto / ec / ec_pmeth.c
1 /*
2  * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/ec.h>
15 #include "ec_lcl.h"
16 #include <openssl/evp.h>
17 #include "internal/evp_int.h"
18
19 #if !defined(OPENSSL_NO_SM2)
20 # include <openssl/sm2.h>
21 #endif
22
23 /* EC pkey context structure */
24
25 typedef struct {
26     /* Key and paramgen group */
27     EC_GROUP *gen_group;
28     /* message digest */
29     const EVP_MD *md;
30     /* Duplicate key if custom cofactor needed */
31     EC_KEY *co_key;
32     /* Cofactor mode */
33     signed char cofactor_mode;
34     /* KDF (if any) to use for ECDH */
35     char kdf_type;
36     /* Message digest to use for key derivation */
37     const EVP_MD *kdf_md;
38     /* User key material */
39     unsigned char *kdf_ukm;
40     size_t kdf_ukmlen;
41     /* KDF output length */
42     size_t kdf_outlen;
43 } EC_PKEY_CTX;
44
45 static int pkey_ec_init(EVP_PKEY_CTX *ctx)
46 {
47     EC_PKEY_CTX *dctx;
48
49     dctx = OPENSSL_zalloc(sizeof(*dctx));
50     if (dctx == NULL)
51         return 0;
52
53     dctx->cofactor_mode = -1;
54     dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
55     ctx->data = dctx;
56     return 1;
57 }
58
59 static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
60 {
61     EC_PKEY_CTX *dctx, *sctx;
62     if (!pkey_ec_init(dst))
63         return 0;
64     sctx = src->data;
65     dctx = dst->data;
66     if (sctx->gen_group) {
67         dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
68         if (!dctx->gen_group)
69             return 0;
70     }
71     dctx->md = sctx->md;
72
73     if (sctx->co_key) {
74         dctx->co_key = EC_KEY_dup(sctx->co_key);
75         if (!dctx->co_key)
76             return 0;
77     }
78     dctx->kdf_type = sctx->kdf_type;
79     dctx->kdf_md = sctx->kdf_md;
80     dctx->kdf_outlen = sctx->kdf_outlen;
81     if (sctx->kdf_ukm) {
82         dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
83         if (!dctx->kdf_ukm)
84             return 0;
85     } else
86         dctx->kdf_ukm = NULL;
87     dctx->kdf_ukmlen = sctx->kdf_ukmlen;
88     return 1;
89 }
90
91 static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
92 {
93     EC_PKEY_CTX *dctx = ctx->data;
94     if (dctx) {
95         EC_GROUP_free(dctx->gen_group);
96         EC_KEY_free(dctx->co_key);
97         OPENSSL_free(dctx->kdf_ukm);
98         OPENSSL_free(dctx);
99     }
100 }
101
102 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
103                         const unsigned char *tbs, size_t tbslen)
104 {
105     int ret, type;
106     unsigned int sltmp;
107     EC_PKEY_CTX *dctx = ctx->data;
108     EC_KEY *ec = ctx->pkey->pkey.ec;
109     const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
110
111     if (!sig) {
112         *siglen = ECDSA_size(ec);
113         return 1;
114     } else if (*siglen < (size_t)ECDSA_size(ec)) {
115         ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
116         return 0;
117     }
118
119     if (dctx->md)
120         type = EVP_MD_type(dctx->md);
121     else
122         type = NID_sha1;
123
124     if (ec_nid == NID_sm2) {
125 #if defined(OPENSSL_NO_SM2)
126         ret = -1;
127 #else
128         ret = SM2_sign(type, tbs, tbslen, sig, &sltmp, ec);
129 #endif
130     } else {
131         ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
132     }
133
134     if (ret <= 0)
135         return ret;
136     *siglen = (size_t)sltmp;
137     return 1;
138 }
139
140 static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
141                           const unsigned char *sig, size_t siglen,
142                           const unsigned char *tbs, size_t tbslen)
143 {
144     int ret, type;
145     EC_PKEY_CTX *dctx = ctx->data;
146     EC_KEY *ec = ctx->pkey->pkey.ec;
147     const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
148
149     if (dctx->md)
150         type = EVP_MD_type(dctx->md);
151     else
152         type = NID_sha1;
153
154     if (ec_nid == NID_sm2) {
155 #if defined(OPENSSL_NO_SM2)
156         ret = -1;
157 #else
158         ret = SM2_verify(type, tbs, tbslen, sig, siglen, ec);
159 #endif
160     } else {
161         ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
162     }
163
164     return ret;
165 }
166
167 #ifndef OPENSSL_NO_EC
168 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
169 {
170     int ret;
171     size_t outlen;
172     const EC_POINT *pubkey = NULL;
173     EC_KEY *eckey;
174     EC_PKEY_CTX *dctx = ctx->data;
175     if (!ctx->pkey || !ctx->peerkey) {
176         ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
177         return 0;
178     }
179
180     eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
181
182     if (!key) {
183         const EC_GROUP *group;
184         group = EC_KEY_get0_group(eckey);
185         *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
186         return 1;
187     }
188     pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
189
190     /*
191      * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
192      * an error, the result is truncated.
193      */
194
195     outlen = *keylen;
196
197     ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
198     if (ret <= 0)
199         return 0;
200     *keylen = ret;
201     return 1;
202 }
203
204 static int pkey_ecies_encrypt(EVP_PKEY_CTX *ctx,
205                               unsigned char *out, size_t *outlen,
206                               const unsigned char *in, size_t inlen)
207 {
208     int ret, md_type;
209     EC_PKEY_CTX *dctx = ctx->data;
210     EC_KEY *ec = ctx->pkey->pkey.ec;
211     const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
212
213     if (dctx->md)
214         md_type = EVP_MD_type(dctx->md);
215     else if (ec_nid == NID_sm2)
216         md_type = NID_sm3;
217     else
218         md_type = NID_sha256;
219
220     if (ec_nid == NID_sm2) {
221 # if defined(OPENSSL_NO_SM2)
222         ret = -1;
223 # else
224         if (out == NULL) {
225             *outlen = SM2_ciphertext_size(ec, EVP_get_digestbynid(md_type), inlen);
226             ret = 1;
227         }
228         else {
229             ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
230                               in, inlen, out, outlen);
231         }
232 # endif
233     } else {
234         /* standard ECIES not implemented */
235         ret = -1;
236     }
237
238     return ret;
239 }
240
241 static int pkey_ecies_decrypt(EVP_PKEY_CTX *ctx,
242                               unsigned char *out, size_t *outlen,
243                               const unsigned char *in, size_t inlen)
244 {
245     int ret, md_type;
246     EC_PKEY_CTX *dctx = ctx->data;
247     EC_KEY *ec = ctx->pkey->pkey.ec;
248     const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
249
250     if (dctx->md)
251         md_type = EVP_MD_type(dctx->md);
252     else if (ec_nid == NID_sm2)
253         md_type = NID_sm3;
254     else
255         md_type = NID_sha256;
256
257     if (ec_nid == NID_sm2) {
258 # if defined(OPENSSL_NO_SM2)
259         ret = -1;
260 # else
261         if (out == NULL) {
262             *outlen = SM2_plaintext_size(ec, EVP_get_digestbynid(md_type), inlen);
263             ret = 1;
264         }
265         else {
266             ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
267                               in, inlen, out, outlen);
268         }
269 # endif
270     } else {
271         /* standard ECIES not implemented */
272         ret = -1;
273     }
274
275     return ret;
276 }
277
278 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
279                               unsigned char *key, size_t *keylen)
280 {
281     EC_PKEY_CTX *dctx = ctx->data;
282     unsigned char *ktmp = NULL;
283     size_t ktmplen;
284     int rv = 0;
285     if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
286         return pkey_ec_derive(ctx, key, keylen);
287     if (!key) {
288         *keylen = dctx->kdf_outlen;
289         return 1;
290     }
291     if (*keylen != dctx->kdf_outlen)
292         return 0;
293     if (!pkey_ec_derive(ctx, NULL, &ktmplen))
294         return 0;
295     ktmp = OPENSSL_malloc(ktmplen);
296     if (ktmp == NULL)
297         return 0;
298     if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
299         goto err;
300     /* Do KDF stuff */
301     if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen,
302                         dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
303         goto err;
304     rv = 1;
305
306  err:
307     OPENSSL_clear_free(ktmp, ktmplen);
308     return rv;
309 }
310 #endif
311
312 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
313 {
314     EC_PKEY_CTX *dctx = ctx->data;
315     EC_GROUP *group;
316     switch (type) {
317     case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
318         group = EC_GROUP_new_by_curve_name(p1);
319         if (group == NULL) {
320             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
321             return 0;
322         }
323         EC_GROUP_free(dctx->gen_group);
324         dctx->gen_group = group;
325         return 1;
326
327     case EVP_PKEY_CTRL_EC_PARAM_ENC:
328         if (!dctx->gen_group) {
329             ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
330             return 0;
331         }
332         EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
333         return 1;
334
335 #ifndef OPENSSL_NO_EC
336     case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
337         if (p1 == -2) {
338             if (dctx->cofactor_mode != -1)
339                 return dctx->cofactor_mode;
340             else {
341                 EC_KEY *ec_key = ctx->pkey->pkey.ec;
342                 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
343             }
344         } else if (p1 < -1 || p1 > 1)
345             return -2;
346         dctx->cofactor_mode = p1;
347         if (p1 != -1) {
348             EC_KEY *ec_key = ctx->pkey->pkey.ec;
349             if (!ec_key->group)
350                 return -2;
351             /* If cofactor is 1 cofactor mode does nothing */
352             if (BN_is_one(ec_key->group->cofactor))
353                 return 1;
354             if (!dctx->co_key) {
355                 dctx->co_key = EC_KEY_dup(ec_key);
356                 if (!dctx->co_key)
357                     return 0;
358             }
359             if (p1)
360                 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
361             else
362                 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
363         } else {
364             EC_KEY_free(dctx->co_key);
365             dctx->co_key = NULL;
366         }
367         return 1;
368 #endif
369
370     case EVP_PKEY_CTRL_EC_KDF_TYPE:
371         if (p1 == -2)
372             return dctx->kdf_type;
373         if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
374             return -2;
375         dctx->kdf_type = p1;
376         return 1;
377
378     case EVP_PKEY_CTRL_EC_KDF_MD:
379         dctx->kdf_md = p2;
380         return 1;
381
382     case EVP_PKEY_CTRL_GET_EC_KDF_MD:
383         *(const EVP_MD **)p2 = dctx->kdf_md;
384         return 1;
385
386     case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
387         if (p1 <= 0)
388             return -2;
389         dctx->kdf_outlen = (size_t)p1;
390         return 1;
391
392     case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
393         *(int *)p2 = dctx->kdf_outlen;
394         return 1;
395
396     case EVP_PKEY_CTRL_EC_KDF_UKM:
397         OPENSSL_free(dctx->kdf_ukm);
398         dctx->kdf_ukm = p2;
399         if (p2)
400             dctx->kdf_ukmlen = p1;
401         else
402             dctx->kdf_ukmlen = 0;
403         return 1;
404
405     case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
406         *(unsigned char **)p2 = dctx->kdf_ukm;
407         return dctx->kdf_ukmlen;
408
409     case EVP_PKEY_CTRL_MD:
410         if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
411             EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
412             EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
413             EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
414             EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
415             EVP_MD_type((const EVP_MD *)p2) != NID_sha512 &&
416             EVP_MD_type((const EVP_MD *)p2) != NID_sm3) {
417             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
418             return 0;
419         }
420         dctx->md = p2;
421         return 1;
422
423     case EVP_PKEY_CTRL_GET_MD:
424         *(const EVP_MD **)p2 = dctx->md;
425         return 1;
426
427     case EVP_PKEY_CTRL_PEER_KEY:
428         /* Default behaviour is OK */
429     case EVP_PKEY_CTRL_DIGESTINIT:
430     case EVP_PKEY_CTRL_PKCS7_SIGN:
431     case EVP_PKEY_CTRL_CMS_SIGN:
432         return 1;
433
434     default:
435         return -2;
436
437     }
438 }
439
440 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
441                             const char *type, const char *value)
442 {
443     if (strcmp(type, "ec_paramgen_curve") == 0) {
444         int nid;
445         nid = EC_curve_nist2nid(value);
446         if (nid == NID_undef)
447             nid = OBJ_sn2nid(value);
448         if (nid == NID_undef)
449             nid = OBJ_ln2nid(value);
450         if (nid == NID_undef) {
451             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
452             return 0;
453         }
454         return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
455     } else if (strcmp(type, "ec_param_enc") == 0) {
456         int param_enc;
457         if (strcmp(value, "explicit") == 0)
458             param_enc = 0;
459         else if (strcmp(value, "named_curve") == 0)
460             param_enc = OPENSSL_EC_NAMED_CURVE;
461         else
462             return -2;
463         return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
464     } else if (strcmp(type, "ecdh_kdf_md") == 0) {
465         const EVP_MD *md;
466         if ((md = EVP_get_digestbyname(value)) == NULL) {
467             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
468             return 0;
469         }
470         return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
471     } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
472         int co_mode;
473         co_mode = atoi(value);
474         return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
475     }
476
477     return -2;
478 }
479
480 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
481 {
482     EC_KEY *ec = NULL;
483     EC_PKEY_CTX *dctx = ctx->data;
484     int ret = 0;
485     if (dctx->gen_group == NULL) {
486         ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
487         return 0;
488     }
489     ec = EC_KEY_new();
490     if (ec == NULL)
491         return 0;
492     ret = EC_KEY_set_group(ec, dctx->gen_group);
493     if (ret)
494         EVP_PKEY_assign_EC_KEY(pkey, ec);
495     else
496         EC_KEY_free(ec);
497     return ret;
498 }
499
500 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
501 {
502     EC_KEY *ec = NULL;
503     EC_PKEY_CTX *dctx = ctx->data;
504     if (ctx->pkey == NULL && dctx->gen_group == NULL) {
505         ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
506         return 0;
507     }
508     ec = EC_KEY_new();
509     if (!ec)
510         return 0;
511     EVP_PKEY_assign_EC_KEY(pkey, ec);
512     if (ctx->pkey) {
513         /* Note: if error return, pkey is freed by parent routine */
514         if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
515             return 0;
516     } else {
517         if (!EC_KEY_set_group(ec, dctx->gen_group))
518             return 0;
519     }
520     return EC_KEY_generate_key(pkey->pkey.ec);
521 }
522
523 const EVP_PKEY_METHOD ec_pkey_meth = {
524     EVP_PKEY_EC,
525     0,
526     pkey_ec_init,
527     pkey_ec_copy,
528     pkey_ec_cleanup,
529
530     0,
531     pkey_ec_paramgen,
532
533     0,
534     pkey_ec_keygen,
535
536     0,
537     pkey_ec_sign,
538
539     0,
540     pkey_ec_verify,
541
542     0, 0,
543
544     0, 0, 0, 0,
545
546     0,
547     pkey_ecies_encrypt,
548
549     0,
550     pkey_ecies_decrypt,
551
552     0,
553 #ifndef OPENSSL_NO_EC
554     pkey_ec_kdf_derive,
555 #else
556     0,
557 #endif
558     pkey_ec_ctrl,
559     pkey_ec_ctrl_str
560 };