]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/signature.c
Standard style for all EVP_xxx_free routines
[thirdparty/openssl.git] / crypto / evp / signature.c
CommitLineData
e683582b 1/*
8020d79b 2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
e683582b
SL
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#include <stdio.h>
11#include <stdlib.h>
12#include <openssl/objects.h>
13#include <openssl/evp.h>
14#include "internal/cryptlib.h"
15#include "crypto/evp.h"
16#include "internal/provider.h"
17#include "evp_local.h"
18
19static EVP_SIGNATURE *evp_signature_new(OSSL_PROVIDER *prov)
20{
21 EVP_SIGNATURE *signature = OPENSSL_zalloc(sizeof(EVP_SIGNATURE));
22
23 if (signature == NULL) {
24 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
25 return NULL;
26 }
27
28 signature->lock = CRYPTO_THREAD_lock_new();
29 if (signature->lock == NULL) {
30 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
31 OPENSSL_free(signature);
32 return NULL;
33 }
34 signature->prov = prov;
35 ossl_provider_up_ref(prov);
36 signature->refcnt = 1;
37
38 return signature;
39}
40
309a78aa
RL
41static void *evp_signature_from_algorithm(int name_id,
42 const OSSL_ALGORITHM *algodef,
43 OSSL_PROVIDER *prov)
e683582b 44{
309a78aa 45 const OSSL_DISPATCH *fns = algodef->implementation;
e683582b
SL
46 EVP_SIGNATURE *signature = NULL;
47 int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0;
48 int digsignfncnt = 0, digverifyfncnt = 0;
49 int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0;
50
51 if ((signature = evp_signature_new(prov)) == NULL) {
52 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
53 goto err;
54 }
55
56 signature->name_id = name_id;
309a78aa 57 signature->description = algodef->algorithm_description;
e683582b
SL
58
59 for (; fns->function_id != 0; fns++) {
60 switch (fns->function_id) {
61 case OSSL_FUNC_SIGNATURE_NEWCTX:
62 if (signature->newctx != NULL)
63 break;
363b1e5d 64 signature->newctx = OSSL_FUNC_signature_newctx(fns);
e683582b
SL
65 ctxfncnt++;
66 break;
67 case OSSL_FUNC_SIGNATURE_SIGN_INIT:
68 if (signature->sign_init != NULL)
69 break;
363b1e5d 70 signature->sign_init = OSSL_FUNC_signature_sign_init(fns);
e683582b
SL
71 signfncnt++;
72 break;
73 case OSSL_FUNC_SIGNATURE_SIGN:
74 if (signature->sign != NULL)
75 break;
363b1e5d 76 signature->sign = OSSL_FUNC_signature_sign(fns);
e683582b
SL
77 signfncnt++;
78 break;
79 case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
80 if (signature->verify_init != NULL)
81 break;
363b1e5d 82 signature->verify_init = OSSL_FUNC_signature_verify_init(fns);
e683582b
SL
83 verifyfncnt++;
84 break;
85 case OSSL_FUNC_SIGNATURE_VERIFY:
86 if (signature->verify != NULL)
87 break;
363b1e5d 88 signature->verify = OSSL_FUNC_signature_verify(fns);
e683582b
SL
89 verifyfncnt++;
90 break;
91 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
92 if (signature->verify_recover_init != NULL)
93 break;
94 signature->verify_recover_init
363b1e5d 95 = OSSL_FUNC_signature_verify_recover_init(fns);
e683582b
SL
96 verifyrecfncnt++;
97 break;
98 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
99 if (signature->verify_recover != NULL)
100 break;
101 signature->verify_recover
363b1e5d 102 = OSSL_FUNC_signature_verify_recover(fns);
e683582b
SL
103 verifyrecfncnt++;
104 break;
105 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT:
106 if (signature->digest_sign_init != NULL)
107 break;
108 signature->digest_sign_init
363b1e5d 109 = OSSL_FUNC_signature_digest_sign_init(fns);
e683582b
SL
110 break;
111 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
112 if (signature->digest_sign_update != NULL)
113 break;
114 signature->digest_sign_update
363b1e5d 115 = OSSL_FUNC_signature_digest_sign_update(fns);
e683582b
SL
116 digsignfncnt++;
117 break;
118 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL:
119 if (signature->digest_sign_final != NULL)
120 break;
121 signature->digest_sign_final
363b1e5d 122 = OSSL_FUNC_signature_digest_sign_final(fns);
e683582b
SL
123 digsignfncnt++;
124 break;
eea1e780
MC
125 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN:
126 if (signature->digest_sign != NULL)
127 break;
128 signature->digest_sign
363b1e5d 129 = OSSL_FUNC_signature_digest_sign(fns);
eea1e780 130 break;
e683582b
SL
131 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
132 if (signature->digest_verify_init != NULL)
133 break;
134 signature->digest_verify_init
363b1e5d 135 = OSSL_FUNC_signature_digest_verify_init(fns);
e683582b
SL
136 break;
137 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
138 if (signature->digest_verify_update != NULL)
139 break;
140 signature->digest_verify_update
363b1e5d 141 = OSSL_FUNC_signature_digest_verify_update(fns);
e683582b
SL
142 digverifyfncnt++;
143 break;
144 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL:
145 if (signature->digest_verify_final != NULL)
146 break;
147 signature->digest_verify_final
363b1e5d 148 = OSSL_FUNC_signature_digest_verify_final(fns);
e683582b
SL
149 digverifyfncnt++;
150 break;
eea1e780
MC
151 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY:
152 if (signature->digest_verify != NULL)
153 break;
154 signature->digest_verify
363b1e5d 155 = OSSL_FUNC_signature_digest_verify(fns);
eea1e780 156 break;
e683582b
SL
157 case OSSL_FUNC_SIGNATURE_FREECTX:
158 if (signature->freectx != NULL)
159 break;
363b1e5d 160 signature->freectx = OSSL_FUNC_signature_freectx(fns);
e683582b
SL
161 ctxfncnt++;
162 break;
163 case OSSL_FUNC_SIGNATURE_DUPCTX:
164 if (signature->dupctx != NULL)
165 break;
363b1e5d 166 signature->dupctx = OSSL_FUNC_signature_dupctx(fns);
e683582b
SL
167 break;
168 case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
169 if (signature->get_ctx_params != NULL)
170 break;
171 signature->get_ctx_params
363b1e5d 172 = OSSL_FUNC_signature_get_ctx_params(fns);
e683582b
SL
173 gparamfncnt++;
174 break;
175 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
176 if (signature->gettable_ctx_params != NULL)
177 break;
178 signature->gettable_ctx_params
363b1e5d 179 = OSSL_FUNC_signature_gettable_ctx_params(fns);
e683582b
SL
180 gparamfncnt++;
181 break;
182 case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
183 if (signature->set_ctx_params != NULL)
184 break;
185 signature->set_ctx_params
363b1e5d 186 = OSSL_FUNC_signature_set_ctx_params(fns);
e683582b
SL
187 sparamfncnt++;
188 break;
189 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
190 if (signature->settable_ctx_params != NULL)
191 break;
192 signature->settable_ctx_params
363b1e5d 193 = OSSL_FUNC_signature_settable_ctx_params(fns);
e683582b
SL
194 sparamfncnt++;
195 break;
196 case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS:
197 if (signature->get_ctx_md_params != NULL)
198 break;
199 signature->get_ctx_md_params
363b1e5d 200 = OSSL_FUNC_signature_get_ctx_md_params(fns);
e683582b
SL
201 gmdparamfncnt++;
202 break;
203 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS:
204 if (signature->gettable_ctx_md_params != NULL)
205 break;
206 signature->gettable_ctx_md_params
363b1e5d 207 = OSSL_FUNC_signature_gettable_ctx_md_params(fns);
e683582b
SL
208 gmdparamfncnt++;
209 break;
210 case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS:
211 if (signature->set_ctx_md_params != NULL)
212 break;
213 signature->set_ctx_md_params
363b1e5d 214 = OSSL_FUNC_signature_set_ctx_md_params(fns);
e683582b
SL
215 smdparamfncnt++;
216 break;
217 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS:
218 if (signature->settable_ctx_md_params != NULL)
219 break;
220 signature->settable_ctx_md_params
363b1e5d 221 = OSSL_FUNC_signature_settable_ctx_md_params(fns);
e683582b
SL
222 smdparamfncnt++;
223 break;
224 }
225 }
226 if (ctxfncnt != 2
227 || (signfncnt == 0
228 && verifyfncnt == 0
229 && verifyrecfncnt == 0
230 && digsignfncnt == 0
eea1e780
MC
231 && digverifyfncnt == 0
232 && signature->digest_sign == NULL
233 && signature->digest_verify == NULL)
e683582b
SL
234 || (signfncnt != 0 && signfncnt != 2)
235 || (verifyfncnt != 0 && verifyfncnt != 2)
236 || (verifyrecfncnt != 0 && verifyrecfncnt != 2)
eea1e780
MC
237 || (digsignfncnt != 0 && digsignfncnt != 2)
238 || (digsignfncnt == 2 && signature->digest_sign_init == NULL)
239 || (digverifyfncnt != 0 && digverifyfncnt != 2)
240 || (digverifyfncnt == 2 && signature->digest_verify_init == NULL)
241 || (signature->digest_sign != NULL
242 && signature->digest_sign_init == NULL)
243 || (signature->digest_verify != NULL
244 && signature->digest_verify_init == NULL)
e683582b
SL
245 || (gparamfncnt != 0 && gparamfncnt != 2)
246 || (sparamfncnt != 0 && sparamfncnt != 2)
247 || (gmdparamfncnt != 0 && gmdparamfncnt != 2)
248 || (smdparamfncnt != 0 && smdparamfncnt != 2)) {
249 /*
250 * In order to be a consistent set of functions we must have at least
251 * a set of context functions (newctx and freectx) as well as a set of
252 * "signature" functions:
253 * (sign_init, sign) or
254 * (verify_init verify) or
255 * (verify_recover_init, verify_recover) or
256 * (digest_sign_init, digest_sign_update, digest_sign_final) or
eea1e780
MC
257 * (digest_verify_init, digest_verify_update, digest_verify_final) or
258 * (digest_sign_init, digest_sign) or
259 * (digest_verify_init, digest_verify).
e683582b
SL
260 *
261 * set_ctx_params and settable_ctx_params are optional, but if one of
262 * them is present then the other one must also be present. The same
263 * applies to get_ctx_params and gettable_ctx_params. The same rules
264 * apply to the "md_params" functions. The dupctx function is optional.
265 */
266 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
267 goto err;
268 }
269
270 return signature;
271 err:
272 EVP_SIGNATURE_free(signature);
273 return NULL;
274}
275
276void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
277{
543e740b 278 int i;
e683582b 279
543e740b
RS
280 if (signature == NULL)
281 return;
282 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
283 if (i > 0)
284 return;
285 ossl_provider_free(signature->prov);
286 CRYPTO_THREAD_lock_free(signature->lock);
287 OPENSSL_free(signature);
e683582b
SL
288}
289
290int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
291{
292 int ref = 0;
293
294 CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
295 return 1;
296}
297
298OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
299{
300 return signature->prov;
301}
302
b4250010 303EVP_SIGNATURE *EVP_SIGNATURE_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
e683582b
SL
304 const char *properties)
305{
306 return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
309a78aa 307 evp_signature_from_algorithm,
e683582b
SL
308 (int (*)(void *))EVP_SIGNATURE_up_ref,
309 (void (*)(void *))EVP_SIGNATURE_free);
310}
311
312int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name)
313{
e4a1d023 314 return evp_is_a(signature->prov, signature->name_id, NULL, name);
e683582b
SL
315}
316
317int EVP_SIGNATURE_number(const EVP_SIGNATURE *signature)
318{
319 return signature->name_id;
320}
321
03888233
RL
322const char *EVP_SIGNATURE_description(const EVP_SIGNATURE *signature)
323{
324 return signature->description;
325}
326
b4250010 327void EVP_SIGNATURE_do_all_provided(OSSL_LIB_CTX *libctx,
e683582b
SL
328 void (*fn)(EVP_SIGNATURE *signature,
329 void *arg),
330 void *arg)
331{
332 evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
333 (void (*)(void *, void *))fn, arg,
309a78aa 334 evp_signature_from_algorithm,
e683582b
SL
335 (void (*)(void *))EVP_SIGNATURE_free);
336}
337
338
d84f5515
MC
339int EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
340 void (*fn)(const char *name, void *data),
341 void *data)
e683582b
SL
342{
343 if (signature->prov != NULL)
d84f5515
MC
344 return evp_names_do_all(signature->prov, signature->name_id, fn, data);
345
346 return 1;
e683582b
SL
347}
348
e3efe7a5
SL
349const OSSL_PARAM *EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig)
350{
351 void *provctx;
352
353 if (sig == NULL || sig->gettable_ctx_params == NULL)
354 return NULL;
355
356 provctx = ossl_provider_ctx(EVP_SIGNATURE_provider(sig));
fb67126e 357 return sig->gettable_ctx_params(NULL, provctx);
e3efe7a5
SL
358}
359
360const OSSL_PARAM *EVP_SIGNATURE_settable_ctx_params(const EVP_SIGNATURE *sig)
361{
362 void *provctx;
363
364 if (sig == NULL || sig->settable_ctx_params == NULL)
365 return NULL;
366
367 provctx = ossl_provider_ctx(EVP_SIGNATURE_provider(sig));
fb67126e 368 return sig->settable_ctx_params(NULL, provctx);
e3efe7a5
SL
369}
370
4b58d9b4
P
371static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation,
372 const OSSL_PARAM params[])
e683582b
SL
373{
374 int ret = 0;
375 void *provkey = NULL;
376 EVP_SIGNATURE *signature = NULL;
f6aa5774
RL
377 EVP_KEYMGMT *tmp_keymgmt = NULL;
378 const char *supported_sig = NULL;
e683582b
SL
379
380 if (ctx == NULL) {
9311d0c4 381 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
382 return -2;
383 }
384
385 evp_pkey_ctx_free_old_ops(ctx);
386 ctx->operation = operation;
387
0b9dd384
RL
388 /*
389 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
390 * calls can be removed.
391 */
392 ERR_set_mark();
393
f21c9c64 394 if (evp_pkey_ctx_is_legacy(ctx))
e683582b
SL
395 goto legacy;
396
3c6ed955
RL
397 /*
398 * Ensure that the key is provided, either natively, or as a cached export.
399 * If not, go legacy
400 */
f6aa5774 401 tmp_keymgmt = ctx->keymgmt;
3c6ed955
RL
402 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
403 &tmp_keymgmt, ctx->propquery);
d0ddf9b4 404 if (tmp_keymgmt == NULL)
f6aa5774
RL
405 goto legacy;
406 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
0b9dd384 407 ERR_clear_last_mark();
f6aa5774
RL
408 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
409 goto err;
e683582b 410 }
f6aa5774
RL
411 EVP_KEYMGMT_free(ctx->keymgmt);
412 ctx->keymgmt = tmp_keymgmt;
413
414 if (ctx->keymgmt->query_operation_name != NULL)
415 supported_sig = ctx->keymgmt->query_operation_name(OSSL_OP_SIGNATURE);
416
417 /*
418 * If we didn't get a supported sig, assume there is one with the
419 * same name as the key type.
420 */
421 if (supported_sig == NULL)
422 supported_sig = ctx->keytype;
423
424 /*
425 * Because we cleared out old ops, we shouldn't need to worry about
426 * checking if signature is already there.
427 */
428 signature =
429 EVP_SIGNATURE_fetch(ctx->libctx, supported_sig, ctx->propquery);
430
431 if (signature == NULL
e683582b
SL
432 || (EVP_KEYMGMT_provider(ctx->keymgmt)
433 != EVP_SIGNATURE_provider(signature))) {
434 /*
0b9dd384
RL
435 * We don't need to free ctx->keymgmt here, as it's not necessarily
436 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
e683582b
SL
437 */
438 EVP_SIGNATURE_free(signature);
439 goto legacy;
440 }
441
0b9dd384
RL
442 /*
443 * TODO remove this when legacy is gone
444 * If we don't have the full support we need with provided methods,
445 * let's go see if legacy does.
446 */
447 ERR_pop_to_mark();
448
449 /* No more legacy from here down to legacy: */
450
e683582b 451 ctx->op.sig.signature = signature;
2c6094ba
RL
452 ctx->op.sig.sigprovctx =
453 signature->newctx(ossl_provider_ctx(signature->prov), ctx->propquery);
e683582b
SL
454 if (ctx->op.sig.sigprovctx == NULL) {
455 /* The provider key can stay in the cache */
9311d0c4 456 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
457 goto err;
458 }
459
460 switch (operation) {
461 case EVP_PKEY_OP_SIGN:
462 if (signature->sign_init == NULL) {
9311d0c4 463 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
464 ret = -2;
465 goto err;
466 }
4b58d9b4 467 ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey, params);
e683582b
SL
468 break;
469 case EVP_PKEY_OP_VERIFY:
470 if (signature->verify_init == NULL) {
9311d0c4 471 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
472 ret = -2;
473 goto err;
474 }
4b58d9b4 475 ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey, params);
e683582b
SL
476 break;
477 case EVP_PKEY_OP_VERIFYRECOVER:
478 if (signature->verify_recover_init == NULL) {
9311d0c4 479 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
480 ret = -2;
481 goto err;
482 }
4b58d9b4
P
483 ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey,
484 params);
e683582b
SL
485 break;
486 default:
9311d0c4 487 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
488 goto err;
489 }
490
491 if (ret <= 0) {
492 signature->freectx(ctx->op.sig.sigprovctx);
493 ctx->op.sig.sigprovctx = NULL;
494 goto err;
495 }
86df26b3 496 goto end;
e683582b
SL
497
498 legacy:
0b9dd384
RL
499 /*
500 * TODO remove this when legacy is gone
501 * If we don't have the full support we need with provided methods,
502 * let's go see if legacy does.
503 */
504 ERR_pop_to_mark();
505
e683582b
SL
506 if (ctx->pmeth == NULL
507 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
508 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
509 || (operation == EVP_PKEY_OP_VERIFYRECOVER
510 && ctx->pmeth->verify_recover == NULL)) {
9311d0c4 511 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
512 return -2;
513 }
514
515 switch (operation) {
516 case EVP_PKEY_OP_SIGN:
517 if (ctx->pmeth->sign_init == NULL)
518 return 1;
519 ret = ctx->pmeth->sign_init(ctx);
520 break;
521 case EVP_PKEY_OP_VERIFY:
522 if (ctx->pmeth->verify_init == NULL)
523 return 1;
524 ret = ctx->pmeth->verify_init(ctx);
525 break;
526 case EVP_PKEY_OP_VERIFYRECOVER:
527 if (ctx->pmeth->verify_recover_init == NULL)
528 return 1;
529 ret = ctx->pmeth->verify_recover_init(ctx);
530 break;
531 default:
9311d0c4 532 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
533 goto err;
534 }
535 if (ret <= 0)
536 goto err;
86df26b3
RL
537 end:
538#ifndef FIPS_MODULE
539 if (ret > 0)
540 ret = evp_pkey_ctx_use_cached_data(ctx);
541#endif
e683582b 542
86df26b3 543 return ret;
e683582b 544 err:
c7fa9297 545 evp_pkey_ctx_free_old_ops(ctx);
e683582b
SL
546 ctx->operation = EVP_PKEY_OP_UNDEFINED;
547 return ret;
548}
549
550int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
551{
4b58d9b4
P
552 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN, NULL);
553}
554
555int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
556{
557 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN, params);
e683582b
SL
558}
559
560int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
561 unsigned char *sig, size_t *siglen,
562 const unsigned char *tbs, size_t tbslen)
563{
564 int ret;
565
566 if (ctx == NULL) {
9311d0c4 567 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
568 return -2;
569 }
570
571 if (ctx->operation != EVP_PKEY_OP_SIGN) {
bf23b9a1 572 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
e683582b
SL
573 return -1;
574 }
575
576 if (ctx->op.sig.sigprovctx == NULL)
577 goto legacy;
578
579 ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
580 SIZE_MAX, tbs, tbslen);
581
582 return ret;
583 legacy:
584
585 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
9311d0c4 586 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
587 return -2;
588 }
589
590 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
591 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
592}
593
594int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
595{
4b58d9b4
P
596 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY, NULL);
597}
598
599int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
600{
601 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY, params);
e683582b
SL
602}
603
604int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
605 const unsigned char *sig, size_t siglen,
606 const unsigned char *tbs, size_t tbslen)
607{
608 int ret;
609
610 if (ctx == NULL) {
9311d0c4 611 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
612 return -2;
613 }
614
615 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
bf23b9a1 616 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
e683582b
SL
617 return -1;
618 }
619
620 if (ctx->op.sig.sigprovctx == NULL)
621 goto legacy;
622
623 ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
624 tbs, tbslen);
625
626 return ret;
627 legacy:
628 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
9311d0c4 629 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
630 return -2;
631 }
632
633 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
634}
635
636int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
637{
4b58d9b4
P
638 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER, NULL);
639}
640
641int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx,
642 const OSSL_PARAM params[])
643{
644 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER, params);
e683582b
SL
645}
646
647int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
648 unsigned char *rout, size_t *routlen,
649 const unsigned char *sig, size_t siglen)
650{
651 int ret;
652
653 if (ctx == NULL) {
9311d0c4 654 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
655 return -2;
656 }
657
658 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
bf23b9a1 659 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
e683582b
SL
660 return -1;
661 }
662
663 if (ctx->op.sig.sigprovctx == NULL)
664 goto legacy;
665
666 ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
667 routlen,
668 (rout == NULL ? 0 : *routlen),
669 sig, siglen);
670 return ret;
671 legacy:
672 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
9311d0c4 673 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
674 return -2;
675 }
676 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
677 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
678}