xref: /openbsd-src/lib/libcrypto/ec/ec_pmeth.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /* $OpenBSD: ec_pmeth.c,v 1.17 2023/04/25 15:48:48 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_local.h"
70 #include "ec_local.h"
71 #include "ech_local.h"
72 #include "evp_local.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 		/* RFC 3279, RFC 5758 and NIST CSOR. */
385 		if (EVP_MD_type((const EVP_MD *) p2) != NID_sha1 &&
386 		    EVP_MD_type((const EVP_MD *) p2) != NID_ecdsa_with_SHA1 &&
387 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha224 &&
388 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha256 &&
389 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha384 &&
390 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha512 &&
391 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha3_224 &&
392 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha3_256 &&
393 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha3_384 &&
394 		    EVP_MD_type((const EVP_MD *) p2) != NID_sha3_512) {
395 			ECerror(EC_R_INVALID_DIGEST_TYPE);
396 			return 0;
397 		}
398 		dctx->md = p2;
399 		return 1;
400 
401 	case EVP_PKEY_CTRL_GET_MD:
402 		*(const EVP_MD **)p2 = dctx->md;
403 		return 1;
404 
405 	case EVP_PKEY_CTRL_PEER_KEY:
406 		/* Default behaviour is OK */
407 	case EVP_PKEY_CTRL_DIGESTINIT:
408 	case EVP_PKEY_CTRL_PKCS7_SIGN:
409 	case EVP_PKEY_CTRL_CMS_SIGN:
410 		return 1;
411 
412 	default:
413 		return -2;
414 
415 	}
416 }
417 
418 static int
419 pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
420 {
421 	if (!strcmp(type, "ec_paramgen_curve")) {
422 		int nid;
423 		nid = EC_curve_nist2nid(value);
424 		if (nid == NID_undef)
425 			nid = OBJ_sn2nid(value);
426 		if (nid == NID_undef)
427 			nid = OBJ_ln2nid(value);
428 		if (nid == NID_undef) {
429 			ECerror(EC_R_INVALID_CURVE);
430 			return 0;
431 		}
432 		return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
433 	} else if (strcmp(type, "ec_param_enc") == 0) {
434 		int param_enc;
435 		if (strcmp(value, "explicit") == 0)
436 			param_enc = 0;
437 		else if (strcmp(value, "named_curve") == 0)
438 			param_enc = OPENSSL_EC_NAMED_CURVE;
439 		else
440 			return -2;
441 		return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
442 	} else if (strcmp(type, "ecdh_kdf_md") == 0) {
443 		const EVP_MD *md;
444 		if ((md = EVP_get_digestbyname(value)) == NULL) {
445 			ECerror(EC_R_INVALID_DIGEST);
446 			return 0;
447 		}
448 		return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
449 	} else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
450 		int co_mode;
451 		co_mode = atoi(value);
452 		return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
453 	}
454 	return -2;
455 }
456 
457 static int
458 pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
459 {
460 	EC_KEY *ec = NULL;
461 	EC_PKEY_CTX *dctx = ctx->data;
462 	int ret = 0;
463 	if (dctx->gen_group == NULL) {
464 		ECerror(EC_R_NO_PARAMETERS_SET);
465 		return 0;
466 	}
467 	ec = EC_KEY_new();
468 	if (!ec)
469 		return 0;
470 	ret = EC_KEY_set_group(ec, dctx->gen_group);
471 	if (ret)
472 		EVP_PKEY_assign_EC_KEY(pkey, ec);
473 	else
474 		EC_KEY_free(ec);
475 	return ret;
476 }
477 
478 static int
479 pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
480 {
481 	EC_KEY *ec = NULL;
482 	EC_PKEY_CTX *dctx = ctx->data;
483 
484 	if (ctx->pkey == NULL && dctx->gen_group == NULL) {
485 		ECerror(EC_R_NO_PARAMETERS_SET);
486 		return 0;
487 	}
488 	ec = EC_KEY_new();
489 	if (ec == NULL)
490 		return 0;
491 	if (!EVP_PKEY_assign_EC_KEY(pkey, ec)) {
492 		EC_KEY_free(ec);
493 		return 0;
494 	}
495 	/* Note: if error is returned, we count on caller to free pkey->pkey.ec */
496 	if (ctx->pkey != NULL) {
497 		if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
498 			return 0;
499 	} else {
500 		if (!EC_KEY_set_group(ec, dctx->gen_group))
501 			return 0;
502 	}
503 
504 	return EC_KEY_generate_key(ec);
505 }
506 
507 const EVP_PKEY_METHOD ec_pkey_meth = {
508 	.pkey_id = EVP_PKEY_EC,
509 
510 	.init = pkey_ec_init,
511 	.copy = pkey_ec_copy,
512 	.cleanup = pkey_ec_cleanup,
513 
514 	.paramgen = pkey_ec_paramgen,
515 
516 	.keygen = pkey_ec_keygen,
517 
518 	.sign = pkey_ec_sign,
519 
520 	.verify = pkey_ec_verify,
521 
522 	.derive = pkey_ec_kdf_derive,
523 
524 	.ctrl = pkey_ec_ctrl,
525 	.ctrl_str = pkey_ec_ctrl_str
526 };
527