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