xref: /dragonfly/crypto/libressl/crypto/ec/ec_pmeth.c (revision 6f5ec8b5)
1 /* $OpenBSD: ec_pmeth.c,v 1.13 2021/12/04 16:08:32 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2006.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <stdio.h>
60 #include <string.h>
61 
62 #include <openssl/asn1t.h>
63 #include <openssl/ec.h>
64 #include <openssl/ecdsa.h>
65 #include <openssl/err.h>
66 #include <openssl/evp.h>
67 #include <openssl/x509.h>
68 
69 #include "bn_lcl.h"
70 #include "ec_lcl.h"
71 #include "ech_locl.h"
72 #include "evp_locl.h"
73 
74 /* EC pkey context structure */
75 
76 typedef struct {
77 	/* Key and paramgen group */
78 	EC_GROUP *gen_group;
79 	/* message digest */
80 	const EVP_MD *md;
81 	/* Duplicate key if custom cofactor needed */
82 	EC_KEY *co_key;
83 	/* Cofactor mode */
84 	signed char cofactor_mode;
85 	/* KDF (if any) to use for ECDH */
86 	char kdf_type;
87 	/* Message digest to use for key derivation */
88 	const EVP_MD *kdf_md;
89 	/* User key material */
90 	unsigned char *kdf_ukm;
91 	size_t kdf_ukmlen;
92 	/* KDF output length */
93 	size_t kdf_outlen;
94 } EC_PKEY_CTX;
95 
96 static int
97 pkey_ec_init(EVP_PKEY_CTX * ctx)
98 {
99 	EC_PKEY_CTX *dctx;
100 
101 	if ((dctx = calloc(1, sizeof(EC_PKEY_CTX))) == NULL) {
102 		ECerror(ERR_R_MALLOC_FAILURE);
103 		return 0;
104 	}
105 
106 	dctx->cofactor_mode = -1;
107 	dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
108 
109 	ctx->data = dctx;
110 
111 	return 1;
112 }
113 
114 static int
115 pkey_ec_copy(EVP_PKEY_CTX * dst, EVP_PKEY_CTX * src)
116 {
117 	EC_PKEY_CTX *dctx, *sctx;
118 	if (!pkey_ec_init(dst))
119 		return 0;
120 	sctx = src->data;
121 	dctx = dst->data;
122 	if (sctx->gen_group) {
123 		dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
124 		if (!dctx->gen_group)
125 			return 0;
126 	}
127 	dctx->md = sctx->md;
128 
129 	if (sctx->co_key) {
130 		dctx->co_key = EC_KEY_dup(sctx->co_key);
131 		if (!dctx->co_key)
132 			return 0;
133 	}
134 	dctx->kdf_type = sctx->kdf_type;
135 	dctx->kdf_md = sctx->kdf_md;
136 	dctx->kdf_outlen = sctx->kdf_outlen;
137 	if (sctx->kdf_ukm) {
138 		if ((dctx->kdf_ukm = calloc(1, sctx->kdf_ukmlen)) == NULL)
139 			return 0;
140 		memcpy(dctx->kdf_ukm, sctx->kdf_ukm, sctx->kdf_ukmlen);
141 	} else
142 		dctx->kdf_ukm = NULL;
143 
144 	dctx->kdf_ukmlen = sctx->kdf_ukmlen;
145 
146 	return 1;
147 }
148 
149 static void
150 pkey_ec_cleanup(EVP_PKEY_CTX * ctx)
151 {
152 	EC_PKEY_CTX *dctx = ctx->data;
153 
154 	if (dctx != NULL) {
155 		EC_GROUP_free(dctx->gen_group);
156 		EC_KEY_free(dctx->co_key);
157 		free(dctx->kdf_ukm);
158 		free(dctx);
159 		ctx->data = NULL;
160 	}
161 }
162 
163 static int
164 pkey_ec_sign(EVP_PKEY_CTX * ctx, unsigned char *sig, size_t * siglen,
165     const unsigned char *tbs, size_t tbslen)
166 {
167 	int ret, type;
168 	unsigned int sltmp;
169 	EC_PKEY_CTX *dctx = ctx->data;
170 	EC_KEY *ec = ctx->pkey->pkey.ec;
171 
172 	if (!sig) {
173 		*siglen = ECDSA_size(ec);
174 		return 1;
175 	} else if (*siglen < (size_t) ECDSA_size(ec)) {
176 		ECerror(EC_R_BUFFER_TOO_SMALL);
177 		return 0;
178 	}
179 	if (dctx->md)
180 		type = EVP_MD_type(dctx->md);
181 	else
182 		type = NID_sha1;
183 
184 	ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
185 	if (ret <= 0)
186 		return ret;
187 	*siglen = (size_t) sltmp;
188 	return 1;
189 }
190 
191 static int
192 pkey_ec_verify(EVP_PKEY_CTX * ctx,
193     const unsigned char *sig, size_t siglen,
194     const unsigned char *tbs, size_t tbslen)
195 {
196 	int ret, type;
197 	EC_PKEY_CTX *dctx = ctx->data;
198 	EC_KEY *ec = ctx->pkey->pkey.ec;
199 
200 	if (dctx->md)
201 		type = EVP_MD_type(dctx->md);
202 	else
203 		type = NID_sha1;
204 
205 	ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
206 
207 	return ret;
208 }
209 
210 static int
211 pkey_ec_derive(EVP_PKEY_CTX * ctx, unsigned char *key, size_t * keylen)
212 {
213 	int ret;
214 	size_t outlen;
215 	const EC_POINT *pubkey = NULL;
216 	EC_KEY *eckey;
217 	EC_PKEY_CTX *dctx = ctx->data;
218 
219 	if (!ctx->pkey || !ctx->peerkey) {
220 		ECerror(EC_R_KEYS_NOT_SET);
221 		return 0;
222 	}
223 
224 	eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
225 	if (!key) {
226 		const EC_GROUP *group;
227 		group = EC_KEY_get0_group(eckey);
228 		*keylen = (EC_GROUP_get_degree(group) + 7) / 8;
229 		return 1;
230 	}
231 	pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
232 
233 	/*
234 	 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is
235 	 * not an error, the result is truncated.
236 	 */
237 
238 	outlen = *keylen;
239 
240 	ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
241 	if (ret <= 0)
242 		return 0;
243 
244 	*keylen = ret;
245 
246 	return 1;
247 }
248 
249 static int
250 pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
251 {
252 	EC_PKEY_CTX *dctx = ctx->data;
253 	unsigned char *ktmp = NULL;
254 	size_t ktmplen;
255 	int rv = 0;
256 
257 	if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
258 		return pkey_ec_derive(ctx, key, keylen);
259 
260 	if (!key) {
261 		*keylen = dctx->kdf_outlen;
262 		return 1;
263 	}
264 	if (*keylen != dctx->kdf_outlen)
265 		return 0;
266 	if (!pkey_ec_derive(ctx, NULL, &ktmplen))
267 		return 0;
268 	if ((ktmp = calloc(1, ktmplen)) == NULL) {
269 		ECerror(ERR_R_MALLOC_FAILURE);
270 		return 0;
271 	}
272 	if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
273 		goto err;
274 	/* Do KDF stuff */
275 	if (!ecdh_KDF_X9_63(key, *keylen, ktmp, ktmplen, dctx->kdf_ukm,
276 	    dctx->kdf_ukmlen, dctx->kdf_md))
277 		goto err;
278 	rv = 1;
279 
280  err:
281 	freezero(ktmp, ktmplen);
282 
283 	return rv;
284 }
285 
286 static int
287 pkey_ec_ctrl(EVP_PKEY_CTX * ctx, int type, int p1, void *p2)
288 {
289 	EC_PKEY_CTX *dctx = ctx->data;
290 	EC_GROUP *group;
291 
292 	switch (type) {
293 	case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
294 		group = EC_GROUP_new_by_curve_name(p1);
295 		if (group == NULL) {
296 			ECerror(EC_R_INVALID_CURVE);
297 			return 0;
298 		}
299 		EC_GROUP_free(dctx->gen_group);
300 		dctx->gen_group = group;
301 		return 1;
302 
303 	case EVP_PKEY_CTRL_EC_PARAM_ENC:
304 		if (!dctx->gen_group) {
305 			ECerror(EC_R_NO_PARAMETERS_SET);
306 			return 0;
307 		}
308 		EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
309 		return 1;
310 
311 	case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
312 		if (p1 == -2) {
313 			if (dctx->cofactor_mode != -1)
314 				return dctx->cofactor_mode;
315 			else {
316 				EC_KEY *ec_key = ctx->pkey->pkey.ec;
317 				return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
318 			}
319 		} else if (p1 < -1 || p1 > 1)
320 			return -2;
321 		dctx->cofactor_mode = p1;
322 		if (p1 != -1) {
323 			EC_KEY *ec_key = ctx->pkey->pkey.ec;
324 			if (!ec_key->group)
325 				return -2;
326 			/* If cofactor is 1 cofactor mode does nothing */
327 			if (BN_is_one(&ec_key->group->cofactor))
328 				return 1;
329 			if (!dctx->co_key) {
330 				dctx->co_key = EC_KEY_dup(ec_key);
331 				if (!dctx->co_key)
332 					return 0;
333 			}
334 			if (p1)
335 				EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
336 			else
337 				EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
338 		} else {
339 			EC_KEY_free(dctx->co_key);
340 			dctx->co_key = NULL;
341 		}
342 		return 1;
343 
344 	case EVP_PKEY_CTRL_EC_KDF_TYPE:
345 		if (p1 == -2)
346 			return dctx->kdf_type;
347 		if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_63)
348 			return -2;
349 		dctx->kdf_type = p1;
350 		return 1;
351 
352 	case EVP_PKEY_CTRL_EC_KDF_MD:
353 		dctx->kdf_md = p2;
354 		return 1;
355 
356 	case EVP_PKEY_CTRL_GET_EC_KDF_MD:
357 		*(const EVP_MD **)p2 = dctx->kdf_md;
358 		return 1;
359 
360 	case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
361 		if (p1 <= 0)
362 			return -2;
363 		dctx->kdf_outlen = (size_t)p1;
364 		return 1;
365 
366 	case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
367 		*(int *)p2 = dctx->kdf_outlen;
368 		return 1;
369 
370 	case EVP_PKEY_CTRL_EC_KDF_UKM:
371 		free(dctx->kdf_ukm);
372 		dctx->kdf_ukm = p2;
373 		if (p2)
374 			dctx->kdf_ukmlen = p1;
375 		else
376 			dctx->kdf_ukmlen = 0;
377 		return 1;
378 
379 	case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
380 		*(unsigned char **)p2 = dctx->kdf_ukm;
381 		return dctx->kdf_ukmlen;
382 
383 	case EVP_PKEY_CTRL_MD:
384 		if (EVP_MD_type((const EVP_MD *) p2) != NID_sha1 &&
385 		    EVP_MD_type((const EVP_MD *) p2) != NID_ecdsa_with_SHA1 &&
386 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha224 &&
387 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha256 &&
388 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha384 &&
389 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha512) {
390 			ECerror(EC_R_INVALID_DIGEST_TYPE);
391 			return 0;
392 		}
393 		dctx->md = p2;
394 		return 1;
395 
396 	case EVP_PKEY_CTRL_GET_MD:
397 		*(const EVP_MD **)p2 = dctx->md;
398 		return 1;
399 
400 	case EVP_PKEY_CTRL_PEER_KEY:
401 		/* Default behaviour is OK */
402 	case EVP_PKEY_CTRL_DIGESTINIT:
403 	case EVP_PKEY_CTRL_PKCS7_SIGN:
404 	case EVP_PKEY_CTRL_CMS_SIGN:
405 		return 1;
406 
407 	default:
408 		return -2;
409 
410 	}
411 }
412 
413 static int
414 pkey_ec_ctrl_str(EVP_PKEY_CTX * ctx, const char *type, const char *value)
415 {
416 	if (!strcmp(type, "ec_paramgen_curve")) {
417 		int nid;
418 		nid = EC_curve_nist2nid(value);
419 		if (nid == NID_undef)
420 			nid = OBJ_sn2nid(value);
421 		if (nid == NID_undef)
422 			nid = OBJ_ln2nid(value);
423 		if (nid == NID_undef) {
424 			ECerror(EC_R_INVALID_CURVE);
425 			return 0;
426 		}
427 		return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
428 	} else if (strcmp(type, "ec_param_enc") == 0) {
429 		int param_enc;
430 		if (strcmp(value, "explicit") == 0)
431 			param_enc = 0;
432 		else if (strcmp(value, "named_curve") == 0)
433 			param_enc = OPENSSL_EC_NAMED_CURVE;
434 		else
435 			return -2;
436 		return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
437 	} else if (strcmp(type, "ecdh_kdf_md") == 0) {
438 		const EVP_MD *md;
439 		if ((md = EVP_get_digestbyname(value)) == NULL) {
440 			ECerror(EC_R_INVALID_DIGEST);
441 			return 0;
442 		}
443 		return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
444 	} else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
445 		int co_mode;
446 		co_mode = atoi(value);
447 		return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
448 	}
449 	return -2;
450 }
451 
452 static int
453 pkey_ec_paramgen(EVP_PKEY_CTX * ctx, EVP_PKEY * pkey)
454 {
455 	EC_KEY *ec = NULL;
456 	EC_PKEY_CTX *dctx = ctx->data;
457 	int ret = 0;
458 	if (dctx->gen_group == NULL) {
459 		ECerror(EC_R_NO_PARAMETERS_SET);
460 		return 0;
461 	}
462 	ec = EC_KEY_new();
463 	if (!ec)
464 		return 0;
465 	ret = EC_KEY_set_group(ec, dctx->gen_group);
466 	if (ret)
467 		EVP_PKEY_assign_EC_KEY(pkey, ec);
468 	else
469 		EC_KEY_free(ec);
470 	return ret;
471 }
472 
473 static int
474 pkey_ec_keygen(EVP_PKEY_CTX * ctx, EVP_PKEY * pkey)
475 {
476 	EC_KEY *ec = NULL;
477 	EC_PKEY_CTX *dctx = ctx->data;
478 
479 	if (ctx->pkey == NULL && dctx->gen_group == NULL) {
480 		ECerror(EC_R_NO_PARAMETERS_SET);
481 		return 0;
482 	}
483 	ec = EC_KEY_new();
484 	if (ec == NULL)
485 		return 0;
486 	if (!EVP_PKEY_assign_EC_KEY(pkey, ec)) {
487 		EC_KEY_free(ec);
488 		return 0;
489 	}
490 	/* Note: if error is returned, we count on caller to free pkey->pkey.ec */
491 	if (ctx->pkey != NULL) {
492 		if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
493 			return 0;
494 	} else {
495 		if (!EC_KEY_set_group(ec, dctx->gen_group))
496 			return 0;
497 	}
498 
499 	return EC_KEY_generate_key(ec);
500 }
501 
502 const EVP_PKEY_METHOD ec_pkey_meth = {
503 	.pkey_id = EVP_PKEY_EC,
504 
505 	.init = pkey_ec_init,
506 	.copy = pkey_ec_copy,
507 	.cleanup = pkey_ec_cleanup,
508 
509 	.paramgen = pkey_ec_paramgen,
510 
511 	.keygen = pkey_ec_keygen,
512 
513 	.sign = pkey_ec_sign,
514 
515 	.verify = pkey_ec_verify,
516 
517 	.derive = pkey_ec_kdf_derive,
518 
519 	.ctrl = pkey_ec_ctrl,
520 	.ctrl_str = pkey_ec_ctrl_str
521 };
522