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