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