Add SM2 signature and ECIES schemes
[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     }
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     }
162     else {
163        ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
164     }
165
166
167     return ret;
168 }
169
170 #ifndef OPENSSL_NO_EC
171 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
172                           size_t *keylen)
173 {
174     int ret;
175     size_t outlen;
176     const EC_POINT *pubkey = NULL;
177     EC_KEY *eckey;
178     EC_PKEY_CTX *dctx = ctx->data;
179     if (!ctx->pkey || !ctx->peerkey) {
180         ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
181         return 0;
182     }
183
184     eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
185
186     if (!key) {
187         const EC_GROUP *group;
188         group = EC_KEY_get0_group(eckey);
189         *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
190         return 1;
191     }
192     pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
193
194     /*
195      * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
196      * an error, the result is truncated.
197      */
198
199     outlen = *keylen;
200
201     ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
202     if (ret <= 0)
203         return 0;
204     *keylen = ret;
205     return 1;
206 }
207
208 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
209                               unsigned char *key, size_t *keylen)
210 {
211     EC_PKEY_CTX *dctx = ctx->data;
212     unsigned char *ktmp = NULL;
213     size_t ktmplen;
214     int rv = 0;
215     if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
216         return pkey_ec_derive(ctx, key, keylen);
217     if (!key) {
218         *keylen = dctx->kdf_outlen;
219         return 1;
220     }
221     if (*keylen != dctx->kdf_outlen)
222         return 0;
223     if (!pkey_ec_derive(ctx, NULL, &ktmplen))
224         return 0;
225     ktmp = OPENSSL_malloc(ktmplen);
226     if (ktmp == NULL)
227         return 0;
228     if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
229         goto err;
230     /* Do KDF stuff */
231     if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen,
232                         dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
233         goto err;
234     rv = 1;
235
236  err:
237     OPENSSL_clear_free(ktmp, ktmplen);
238     return rv;
239 }
240 #endif
241
242 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
243 {
244     EC_PKEY_CTX *dctx = ctx->data;
245     EC_GROUP *group;
246     switch (type) {
247     case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
248         group = EC_GROUP_new_by_curve_name(p1);
249         if (group == NULL) {
250             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
251             return 0;
252         }
253         EC_GROUP_free(dctx->gen_group);
254         dctx->gen_group = group;
255         return 1;
256
257     case EVP_PKEY_CTRL_EC_PARAM_ENC:
258         if (!dctx->gen_group) {
259             ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
260             return 0;
261         }
262         EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
263         return 1;
264
265 #ifndef OPENSSL_NO_EC
266     case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
267         if (p1 == -2) {
268             if (dctx->cofactor_mode != -1)
269                 return dctx->cofactor_mode;
270             else {
271                 EC_KEY *ec_key = ctx->pkey->pkey.ec;
272                 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 :
273                     0;
274             }
275         } else if (p1 < -1 || p1 > 1)
276             return -2;
277         dctx->cofactor_mode = p1;
278         if (p1 != -1) {
279             EC_KEY *ec_key = ctx->pkey->pkey.ec;
280             if (!ec_key->group)
281                 return -2;
282             /* If cofactor is 1 cofactor mode does nothing */
283             if (BN_is_one(ec_key->group->cofactor))
284                 return 1;
285             if (!dctx->co_key) {
286                 dctx->co_key = EC_KEY_dup(ec_key);
287                 if (!dctx->co_key)
288                     return 0;
289             }
290             if (p1)
291                 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
292             else
293                 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
294         } else {
295             EC_KEY_free(dctx->co_key);
296             dctx->co_key = NULL;
297         }
298         return 1;
299 #endif
300
301     case EVP_PKEY_CTRL_EC_KDF_TYPE:
302         if (p1 == -2)
303             return dctx->kdf_type;
304         if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
305             return -2;
306         dctx->kdf_type = p1;
307         return 1;
308
309     case EVP_PKEY_CTRL_EC_KDF_MD:
310         dctx->kdf_md = p2;
311         return 1;
312
313     case EVP_PKEY_CTRL_GET_EC_KDF_MD:
314         *(const EVP_MD **)p2 = dctx->kdf_md;
315         return 1;
316
317     case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
318         if (p1 <= 0)
319             return -2;
320         dctx->kdf_outlen = (size_t)p1;
321         return 1;
322
323     case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
324         *(int *)p2 = dctx->kdf_outlen;
325         return 1;
326
327     case EVP_PKEY_CTRL_EC_KDF_UKM:
328         OPENSSL_free(dctx->kdf_ukm);
329         dctx->kdf_ukm = p2;
330         if (p2)
331             dctx->kdf_ukmlen = p1;
332         else
333             dctx->kdf_ukmlen = 0;
334         return 1;
335
336     case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
337         *(unsigned char **)p2 = dctx->kdf_ukm;
338         return dctx->kdf_ukmlen;
339
340     case EVP_PKEY_CTRL_MD:
341         if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
342             EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
343             EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
344             EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
345             EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
346             EVP_MD_type((const EVP_MD *)p2) != NID_sha512 &&
347             EVP_MD_type((const EVP_MD *)p2) != NID_sm3) {
348             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
349             return 0;
350         }
351         dctx->md = p2;
352         return 1;
353
354     case EVP_PKEY_CTRL_GET_MD:
355         *(const EVP_MD **)p2 = dctx->md;
356         return 1;
357
358     case EVP_PKEY_CTRL_PEER_KEY:
359         /* Default behaviour is OK */
360     case EVP_PKEY_CTRL_DIGESTINIT:
361     case EVP_PKEY_CTRL_PKCS7_SIGN:
362     case EVP_PKEY_CTRL_CMS_SIGN:
363         return 1;
364
365     default:
366         return -2;
367
368     }
369 }
370
371 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
372                             const char *type, const char *value)
373 {
374     if (strcmp(type, "ec_paramgen_curve") == 0) {
375         int nid;
376         nid = EC_curve_nist2nid(value);
377         if (nid == NID_undef)
378             nid = OBJ_sn2nid(value);
379         if (nid == NID_undef)
380             nid = OBJ_ln2nid(value);
381         if (nid == NID_undef) {
382             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
383             return 0;
384         }
385         return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
386     } else if (strcmp(type, "ec_param_enc") == 0) {
387         int param_enc;
388         if (strcmp(value, "explicit") == 0)
389             param_enc = 0;
390         else if (strcmp(value, "named_curve") == 0)
391             param_enc = OPENSSL_EC_NAMED_CURVE;
392         else
393             return -2;
394         return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
395     } else if (strcmp(type, "ecdh_kdf_md") == 0) {
396         const EVP_MD *md;
397         if ((md = EVP_get_digestbyname(value)) == NULL) {
398             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
399             return 0;
400         }
401         return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
402     } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
403         int co_mode;
404         co_mode = atoi(value);
405         return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
406     }
407
408     return -2;
409 }
410
411 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
412 {
413     EC_KEY *ec = NULL;
414     EC_PKEY_CTX *dctx = ctx->data;
415     int ret = 0;
416     if (dctx->gen_group == NULL) {
417         ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
418         return 0;
419     }
420     ec = EC_KEY_new();
421     if (ec == NULL)
422         return 0;
423     ret = EC_KEY_set_group(ec, dctx->gen_group);
424     if (ret)
425         EVP_PKEY_assign_EC_KEY(pkey, ec);
426     else
427         EC_KEY_free(ec);
428     return ret;
429 }
430
431 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
432 {
433     EC_KEY *ec = NULL;
434     EC_PKEY_CTX *dctx = ctx->data;
435     if (ctx->pkey == NULL && dctx->gen_group == NULL) {
436         ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
437         return 0;
438     }
439     ec = EC_KEY_new();
440     if (!ec)
441         return 0;
442     EVP_PKEY_assign_EC_KEY(pkey, ec);
443     if (ctx->pkey) {
444         /* Note: if error return, pkey is freed by parent routine */
445         if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
446             return 0;
447     } else {
448         if (!EC_KEY_set_group(ec, dctx->gen_group))
449             return 0;
450     }
451     return EC_KEY_generate_key(pkey->pkey.ec);
452 }
453
454 const EVP_PKEY_METHOD ec_pkey_meth = {
455     EVP_PKEY_EC,
456     0,
457     pkey_ec_init,
458     pkey_ec_copy,
459     pkey_ec_cleanup,
460
461     0,
462     pkey_ec_paramgen,
463
464     0,
465     pkey_ec_keygen,
466
467     0,
468     pkey_ec_sign,
469
470     0,
471     pkey_ec_verify,
472
473     0, 0,
474
475     0, 0, 0, 0,
476
477     0, 0,
478
479     0, 0,
480
481     0,
482 #ifndef OPENSSL_NO_EC
483     pkey_ec_kdf_derive,
484 #else
485     0,
486 #endif
487     pkey_ec_ctrl,
488     pkey_ec_ctrl_str
489 };