summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec_pmeth.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ec/ec_pmeth.c')
-rw-r--r--src/lib/libcrypto/ec/ec_pmeth.c545
1 files changed, 0 insertions, 545 deletions
diff --git a/src/lib/libcrypto/ec/ec_pmeth.c b/src/lib/libcrypto/ec/ec_pmeth.c
deleted file mode 100644
index 85ac4822d1..0000000000
--- a/src/lib/libcrypto/ec/ec_pmeth.c
+++ /dev/null
@@ -1,545 +0,0 @@
1/* $OpenBSD: ec_pmeth.c,v 1.26 2025/03/13 10:39:51 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 <stdlib.h>
61#include <string.h>
62
63#include <openssl/asn1t.h>
64#include <openssl/ec.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 "evp_local.h"
72
73/* EC pkey context structure */
74
75typedef struct {
76 /* Key and paramgen group */
77 EC_GROUP *gen_group;
78 /* message digest */
79 const EVP_MD *md;
80 /* Duplicate key if custom cofactor needed */
81 EC_KEY *co_key;
82 /* Cofactor mode */
83 signed char cofactor_mode;
84 /* KDF (if any) to use for ECDH */
85 char kdf_type;
86 /* Message digest to use for key derivation */
87 const EVP_MD *kdf_md;
88 /* User key material */
89 unsigned char *kdf_ukm;
90 size_t kdf_ukmlen;
91 /* KDF output length */
92 size_t kdf_outlen;
93} EC_PKEY_CTX;
94
95static int
96pkey_ec_init(EVP_PKEY_CTX *ctx)
97{
98 EC_PKEY_CTX *dctx;
99
100 if ((dctx = calloc(1, sizeof(EC_PKEY_CTX))) == NULL) {
101 ECerror(ERR_R_MALLOC_FAILURE);
102 return 0;
103 }
104
105 dctx->cofactor_mode = -1;
106 dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
107
108 ctx->data = dctx;
109
110 return 1;
111}
112
113static int
114pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
115{
116 EC_PKEY_CTX *dctx, *sctx;
117 if (!pkey_ec_init(dst))
118 return 0;
119 sctx = src->data;
120 dctx = dst->data;
121 if (sctx->gen_group) {
122 dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
123 if (!dctx->gen_group)
124 return 0;
125 }
126 dctx->md = sctx->md;
127
128 if (sctx->co_key) {
129 dctx->co_key = EC_KEY_dup(sctx->co_key);
130 if (!dctx->co_key)
131 return 0;
132 }
133 dctx->kdf_type = sctx->kdf_type;
134 dctx->kdf_md = sctx->kdf_md;
135 dctx->kdf_outlen = sctx->kdf_outlen;
136 if (sctx->kdf_ukm) {
137 if ((dctx->kdf_ukm = calloc(1, sctx->kdf_ukmlen)) == NULL)
138 return 0;
139 memcpy(dctx->kdf_ukm, sctx->kdf_ukm, sctx->kdf_ukmlen);
140 } else
141 dctx->kdf_ukm = NULL;
142
143 dctx->kdf_ukmlen = sctx->kdf_ukmlen;
144
145 return 1;
146}
147
148static void
149pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
150{
151 EC_PKEY_CTX *dctx = ctx->data;
152
153 if (dctx != NULL) {
154 EC_GROUP_free(dctx->gen_group);
155 EC_KEY_free(dctx->co_key);
156 free(dctx->kdf_ukm);
157 free(dctx);
158 ctx->data = NULL;
159 }
160}
161
162static int
163pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
164 const unsigned char *tbs, size_t tbslen)
165{
166 int ret, type;
167 unsigned int sltmp;
168 EC_PKEY_CTX *dctx = ctx->data;
169 EC_KEY *ec = ctx->pkey->pkey.ec;
170
171 if (!sig) {
172 *siglen = ECDSA_size(ec);
173 return 1;
174 } else if (*siglen < (size_t) ECDSA_size(ec)) {
175 ECerror(EC_R_BUFFER_TOO_SMALL);
176 return 0;
177 }
178 if (dctx->md)
179 type = EVP_MD_type(dctx->md);
180 else
181 type = NID_sha1;
182
183 ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
184 if (ret <= 0)
185 return ret;
186 *siglen = (size_t) sltmp;
187 return 1;
188}
189
190static int
191pkey_ec_verify(EVP_PKEY_CTX *ctx,
192 const unsigned char *sig, size_t siglen,
193 const unsigned char *tbs, size_t tbslen)
194{
195 int ret, type;
196 EC_PKEY_CTX *dctx = ctx->data;
197 EC_KEY *ec = ctx->pkey->pkey.ec;
198
199 if (dctx->md)
200 type = EVP_MD_type(dctx->md);
201 else
202 type = NID_sha1;
203
204 ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
205
206 return ret;
207}
208
209static int
210pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
211{
212 int ret;
213 size_t outlen;
214 const EC_POINT *pubkey = NULL;
215 EC_KEY *eckey;
216 EC_PKEY_CTX *dctx = ctx->data;
217
218 if (!ctx->pkey || !ctx->peerkey) {
219 ECerror(EC_R_KEYS_NOT_SET);
220 return 0;
221 }
222
223 eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
224 if (key == NULL) {
225 *keylen = BN_num_bytes(eckey->group->p);
226 return 1;
227 }
228 pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
229
230 /*
231 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is
232 * not an error, the result is truncated.
233 */
234
235 outlen = *keylen;
236
237 ret = ECDH_compute_key(key, outlen, pubkey, eckey, NULL);
238 if (ret <= 0)
239 return 0;
240
241 *keylen = ret;
242
243 return 1;
244}
245
246static int
247pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
248{
249 EC_PKEY_CTX *dctx = ctx->data;
250 unsigned char *ktmp = NULL;
251 size_t ktmplen;
252 int rv = 0;
253
254 if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
255 return pkey_ec_derive(ctx, key, keylen);
256
257 if (!key) {
258 *keylen = dctx->kdf_outlen;
259 return 1;
260 }
261 if (*keylen != dctx->kdf_outlen)
262 return 0;
263 if (!pkey_ec_derive(ctx, NULL, &ktmplen))
264 return 0;
265 if ((ktmp = calloc(1, ktmplen)) == NULL) {
266 ECerror(ERR_R_MALLOC_FAILURE);
267 return 0;
268 }
269 if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
270 goto err;
271 /* Do KDF stuff */
272 if (!ecdh_KDF_X9_63(key, *keylen, ktmp, ktmplen, dctx->kdf_ukm,
273 dctx->kdf_ukmlen, dctx->kdf_md))
274 goto err;
275 rv = 1;
276
277 err:
278 freezero(ktmp, ktmplen);
279
280 return rv;
281}
282
283static int
284pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
285{
286 EC_PKEY_CTX *dctx = ctx->data;
287 EC_GROUP *group;
288
289 switch (type) {
290 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
291 group = EC_GROUP_new_by_curve_name(p1);
292 if (group == NULL) {
293 ECerror(EC_R_INVALID_CURVE);
294 return 0;
295 }
296 EC_GROUP_free(dctx->gen_group);
297 dctx->gen_group = group;
298 return 1;
299
300 case EVP_PKEY_CTRL_EC_PARAM_ENC:
301 if (!dctx->gen_group) {
302 ECerror(EC_R_NO_PARAMETERS_SET);
303 return 0;
304 }
305 EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
306 return 1;
307
308 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
309 if (p1 == -2) {
310 if (dctx->cofactor_mode != -1)
311 return dctx->cofactor_mode;
312 else {
313 EC_KEY *ec_key = ctx->pkey->pkey.ec;
314 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
315 }
316 } else if (p1 < -1 || p1 > 1)
317 return -2;
318 dctx->cofactor_mode = p1;
319 if (p1 != -1) {
320 EC_KEY *ec_key = ctx->pkey->pkey.ec;
321 if (!ec_key->group)
322 return -2;
323 /* If cofactor is 1 cofactor mode does nothing */
324 if (BN_is_one(ec_key->group->cofactor))
325 return 1;
326 if (!dctx->co_key) {
327 dctx->co_key = EC_KEY_dup(ec_key);
328 if (!dctx->co_key)
329 return 0;
330 }
331 if (p1)
332 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
333 else
334 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
335 } else {
336 EC_KEY_free(dctx->co_key);
337 dctx->co_key = NULL;
338 }
339 return 1;
340
341 case EVP_PKEY_CTRL_EC_KDF_TYPE:
342 if (p1 == -2)
343 return dctx->kdf_type;
344 if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_63)
345 return -2;
346 dctx->kdf_type = p1;
347 return 1;
348
349 case EVP_PKEY_CTRL_EC_KDF_MD:
350 dctx->kdf_md = p2;
351 return 1;
352
353 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
354 *(const EVP_MD **)p2 = dctx->kdf_md;
355 return 1;
356
357 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
358 if (p1 <= 0)
359 return -2;
360 dctx->kdf_outlen = (size_t)p1;
361 return 1;
362
363 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
364 *(int *)p2 = dctx->kdf_outlen;
365 return 1;
366
367 case EVP_PKEY_CTRL_EC_KDF_UKM:
368 free(dctx->kdf_ukm);
369 dctx->kdf_ukm = p2;
370 if (p2)
371 dctx->kdf_ukmlen = p1;
372 else
373 dctx->kdf_ukmlen = 0;
374 return 1;
375
376 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
377 *(unsigned char **)p2 = dctx->kdf_ukm;
378 return dctx->kdf_ukmlen;
379
380 case EVP_PKEY_CTRL_MD:
381 /* RFC 3279, RFC 5758 and NIST CSOR. */
382 switch (EVP_MD_type(p2)) {
383 case NID_sha1:
384 case NID_ecdsa_with_SHA1:
385 case NID_sha224:
386 case NID_sha256:
387 case NID_sha384:
388 case NID_sha512:
389 case NID_sha3_224:
390 case NID_sha3_256:
391 case NID_sha3_384:
392 case NID_sha3_512:
393 break;
394 default:
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
418static int
419pkey_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 cofactor_mode;
451 const char *errstr;
452
453 cofactor_mode = strtonum(value, -1, 1, &errstr);
454 if (errstr != NULL)
455 return -2;
456 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, cofactor_mode);
457 }
458
459 return -2;
460}
461
462static int
463pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
464{
465 EC_KEY *ec = NULL;
466 EC_PKEY_CTX *dctx = ctx->data;
467 int ret = 0;
468
469 if (dctx->gen_group == NULL) {
470 ECerror(EC_R_NO_PARAMETERS_SET);
471 goto err;
472 }
473
474 if ((ec = EC_KEY_new()) == NULL)
475 goto err;
476 if (!EC_KEY_set_group(ec, dctx->gen_group))
477 goto err;
478 if (!EVP_PKEY_assign_EC_KEY(pkey, ec))
479 goto err;
480 ec = NULL;
481
482 ret = 1;
483
484 err:
485 EC_KEY_free(ec);
486
487 return ret;
488}
489
490static int
491pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
492{
493 EC_KEY *ec = NULL;
494 EC_PKEY_CTX *dctx = ctx->data;
495 int ret = 0;
496
497 if (ctx->pkey == NULL && dctx->gen_group == NULL) {
498 ECerror(EC_R_NO_PARAMETERS_SET);
499 goto err;
500 }
501
502 if ((ec = EC_KEY_new()) == NULL)
503 goto err;
504 if (!EVP_PKEY_set1_EC_KEY(pkey, ec))
505 goto err;
506
507 if (ctx->pkey != NULL) {
508 if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
509 goto err;
510 } else {
511 if (!EC_KEY_set_group(ec, dctx->gen_group))
512 goto err;
513 }
514
515 if (!EC_KEY_generate_key(ec))
516 goto err;
517
518 ret = 1;
519
520 err:
521 EC_KEY_free(ec);
522
523 return ret;
524}
525
526const EVP_PKEY_METHOD ec_pkey_meth = {
527 .pkey_id = EVP_PKEY_EC,
528
529 .init = pkey_ec_init,
530 .copy = pkey_ec_copy,
531 .cleanup = pkey_ec_cleanup,
532
533 .paramgen = pkey_ec_paramgen,
534
535 .keygen = pkey_ec_keygen,
536
537 .sign = pkey_ec_sign,
538
539 .verify = pkey_ec_verify,
540
541 .derive = pkey_ec_kdf_derive,
542
543 .ctrl = pkey_ec_ctrl,
544 .ctrl_str = pkey_ec_ctrl_str
545};