Improve use of the test framework in the SM2 internal tests
[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             if (!SM2_ciphertext_size(ec, EVP_get_digestbynid(md_type), inlen,
227                                      outlen))
228                 ret = -1;
229             else
230                 ret = 1;
231         }
232         else {
233             ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
234                               in, inlen, out, outlen);
235         }
236 # endif
237     } else {
238         /* standard ECIES not implemented */
239         ret = -1;
240     }
241
242     return ret;
243 }
244
245 static int pkey_ecies_decrypt(EVP_PKEY_CTX *ctx,
246                               unsigned char *out, size_t *outlen,
247                               const unsigned char *in, size_t inlen)
248 {
249     int ret;
250     EC_KEY *ec = ctx->pkey->pkey.ec;
251     const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
252
253     if (ec_nid == NID_sm2) {
254 # if defined(OPENSSL_NO_SM2)
255         ret = -1;
256 # else
257         int md_type;
258         EC_PKEY_CTX *dctx = ctx->data;
259
260         if (dctx->md)
261             md_type = EVP_MD_type(dctx->md);
262         else
263             md_type = NID_sm3;
264
265         if (out == NULL) {
266             if (!SM2_plaintext_size(ec, EVP_get_digestbynid(md_type), inlen,
267                                     outlen))
268                 ret = -1;
269             else
270                 ret = 1;
271         }
272         else {
273             ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
274                               in, inlen, out, outlen);
275         }
276 # endif
277     } else {
278         /* standard ECIES not implemented */
279         ret = -1;
280     }
281
282     return ret;
283 }
284
285 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
286                               unsigned char *key, size_t *keylen)
287 {
288     EC_PKEY_CTX *dctx = ctx->data;
289     unsigned char *ktmp = NULL;
290     size_t ktmplen;
291     int rv = 0;
292     if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
293         return pkey_ec_derive(ctx, key, keylen);
294     if (!key) {
295         *keylen = dctx->kdf_outlen;
296         return 1;
297     }
298     if (*keylen != dctx->kdf_outlen)
299         return 0;
300     if (!pkey_ec_derive(ctx, NULL, &ktmplen))
301         return 0;
302     if ((ktmp = OPENSSL_malloc(ktmplen)) == NULL) {
303         ECerr(EC_F_PKEY_EC_KDF_DERIVE, ERR_R_MALLOC_FAILURE);
304         return 0;
305     }
306     if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
307         goto err;
308     /* Do KDF stuff */
309     if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen,
310                         dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
311         goto err;
312     rv = 1;
313
314  err:
315     OPENSSL_clear_free(ktmp, ktmplen);
316     return rv;
317 }
318 #endif
319
320 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
321 {
322     EC_PKEY_CTX *dctx = ctx->data;
323     EC_GROUP *group;
324     switch (type) {
325     case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
326         group = EC_GROUP_new_by_curve_name(p1);
327         if (group == NULL) {
328             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
329             return 0;
330         }
331         EC_GROUP_free(dctx->gen_group);
332         dctx->gen_group = group;
333         return 1;
334
335     case EVP_PKEY_CTRL_EC_PARAM_ENC:
336         if (!dctx->gen_group) {
337             ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
338             return 0;
339         }
340         EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
341         return 1;
342
343 #ifndef OPENSSL_NO_EC
344     case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
345         if (p1 == -2) {
346             if (dctx->cofactor_mode != -1)
347                 return dctx->cofactor_mode;
348             else {
349                 EC_KEY *ec_key = ctx->pkey->pkey.ec;
350                 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
351             }
352         } else if (p1 < -1 || p1 > 1)
353             return -2;
354         dctx->cofactor_mode = p1;
355         if (p1 != -1) {
356             EC_KEY *ec_key = ctx->pkey->pkey.ec;
357             if (!ec_key->group)
358                 return -2;
359             /* If cofactor is 1 cofactor mode does nothing */
360             if (BN_is_one(ec_key->group->cofactor))
361                 return 1;
362             if (!dctx->co_key) {
363                 dctx->co_key = EC_KEY_dup(ec_key);
364                 if (!dctx->co_key)
365                     return 0;
366             }
367             if (p1)
368                 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
369             else
370                 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
371         } else {
372             EC_KEY_free(dctx->co_key);
373             dctx->co_key = NULL;
374         }
375         return 1;
376 #endif
377
378     case EVP_PKEY_CTRL_EC_KDF_TYPE:
379         if (p1 == -2)
380             return dctx->kdf_type;
381         if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
382             return -2;
383         dctx->kdf_type = p1;
384         return 1;
385
386     case EVP_PKEY_CTRL_EC_KDF_MD:
387         dctx->kdf_md = p2;
388         return 1;
389
390     case EVP_PKEY_CTRL_GET_EC_KDF_MD:
391         *(const EVP_MD **)p2 = dctx->kdf_md;
392         return 1;
393
394     case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
395         if (p1 <= 0)
396             return -2;
397         dctx->kdf_outlen = (size_t)p1;
398         return 1;
399
400     case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
401         *(int *)p2 = dctx->kdf_outlen;
402         return 1;
403
404     case EVP_PKEY_CTRL_EC_KDF_UKM:
405         OPENSSL_free(dctx->kdf_ukm);
406         dctx->kdf_ukm = p2;
407         if (p2)
408             dctx->kdf_ukmlen = p1;
409         else
410             dctx->kdf_ukmlen = 0;
411         return 1;
412
413     case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
414         *(unsigned char **)p2 = dctx->kdf_ukm;
415         return dctx->kdf_ukmlen;
416
417     case EVP_PKEY_CTRL_MD:
418         if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
419             EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
420             EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
421             EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
422             EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
423             EVP_MD_type((const EVP_MD *)p2) != NID_sha512 &&
424             EVP_MD_type((const EVP_MD *)p2) != NID_sm3) {
425             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
426             return 0;
427         }
428         dctx->md = p2;
429         return 1;
430
431     case EVP_PKEY_CTRL_GET_MD:
432         *(const EVP_MD **)p2 = dctx->md;
433         return 1;
434
435     case EVP_PKEY_CTRL_PEER_KEY:
436         /* Default behaviour is OK */
437     case EVP_PKEY_CTRL_DIGESTINIT:
438     case EVP_PKEY_CTRL_PKCS7_SIGN:
439     case EVP_PKEY_CTRL_CMS_SIGN:
440         return 1;
441
442     default:
443         return -2;
444
445     }
446 }
447
448 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
449                             const char *type, const char *value)
450 {
451     if (strcmp(type, "ec_paramgen_curve") == 0) {
452         int nid;
453         nid = EC_curve_nist2nid(value);
454         if (nid == NID_undef)
455             nid = OBJ_sn2nid(value);
456         if (nid == NID_undef)
457             nid = OBJ_ln2nid(value);
458         if (nid == NID_undef) {
459             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
460             return 0;
461         }
462         return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
463     } else if (strcmp(type, "ec_param_enc") == 0) {
464         int param_enc;
465         if (strcmp(value, "explicit") == 0)
466             param_enc = 0;
467         else if (strcmp(value, "named_curve") == 0)
468             param_enc = OPENSSL_EC_NAMED_CURVE;
469         else
470             return -2;
471         return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
472     } else if (strcmp(type, "ecdh_kdf_md") == 0) {
473         const EVP_MD *md;
474         if ((md = EVP_get_digestbyname(value)) == NULL) {
475             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
476             return 0;
477         }
478         return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
479     } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
480         int co_mode;
481         co_mode = atoi(value);
482         return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
483     }
484
485     return -2;
486 }
487
488 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
489 {
490     EC_KEY *ec = NULL;
491     EC_PKEY_CTX *dctx = ctx->data;
492     int ret = 0;
493     if (dctx->gen_group == NULL) {
494         ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
495         return 0;
496     }
497     ec = EC_KEY_new();
498     if (ec == NULL)
499         return 0;
500     ret = EC_KEY_set_group(ec, dctx->gen_group);
501     if (ret)
502         EVP_PKEY_assign_EC_KEY(pkey, ec);
503     else
504         EC_KEY_free(ec);
505     return ret;
506 }
507
508 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
509 {
510     EC_KEY *ec = NULL;
511     EC_PKEY_CTX *dctx = ctx->data;
512     if (ctx->pkey == NULL && dctx->gen_group == NULL) {
513         ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
514         return 0;
515     }
516     ec = EC_KEY_new();
517     if (!ec)
518         return 0;
519     EVP_PKEY_assign_EC_KEY(pkey, ec);
520     if (ctx->pkey) {
521         /* Note: if error return, pkey is freed by parent routine */
522         if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
523             return 0;
524     } else {
525         if (!EC_KEY_set_group(ec, dctx->gen_group))
526             return 0;
527     }
528     return EC_KEY_generate_key(pkey->pkey.ec);
529 }
530
531 const EVP_PKEY_METHOD ec_pkey_meth = {
532     EVP_PKEY_EC,
533     0,
534     pkey_ec_init,
535     pkey_ec_copy,
536     pkey_ec_cleanup,
537
538     0,
539     pkey_ec_paramgen,
540
541     0,
542     pkey_ec_keygen,
543
544     0,
545     pkey_ec_sign,
546
547     0,
548     pkey_ec_verify,
549
550     0, 0,
551
552     0, 0, 0, 0,
553
554     0,
555     pkey_ecies_encrypt,
556
557     0,
558     pkey_ecies_decrypt,
559
560     0,
561 #ifndef OPENSSL_NO_EC
562     pkey_ec_kdf_derive,
563 #else
564     0,
565 #endif
566     pkey_ec_ctrl,
567     pkey_ec_ctrl_str
568 };