]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/signature.c
CORE: Add an algorithm_description field to OSSL_ALGORITHM
[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{
278 if (signature != NULL) {
279 int i;
280
281 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
282 if (i > 0)
283 return;
284 ossl_provider_free(signature->prov);
285 CRYPTO_THREAD_lock_free(signature->lock);
286 OPENSSL_free(signature);
287 }
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
b4250010 322void EVP_SIGNATURE_do_all_provided(OSSL_LIB_CTX *libctx,
e683582b
SL
323 void (*fn)(EVP_SIGNATURE *signature,
324 void *arg),
325 void *arg)
326{
327 evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
328 (void (*)(void *, void *))fn, arg,
309a78aa 329 evp_signature_from_algorithm,
e683582b
SL
330 (void (*)(void *))EVP_SIGNATURE_free);
331}
332
333
d84f5515
MC
334int EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
335 void (*fn)(const char *name, void *data),
336 void *data)
e683582b
SL
337{
338 if (signature->prov != NULL)
d84f5515
MC
339 return evp_names_do_all(signature->prov, signature->name_id, fn, data);
340
341 return 1;
e683582b
SL
342}
343
e3efe7a5
SL
344const OSSL_PARAM *EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig)
345{
346 void *provctx;
347
348 if (sig == NULL || sig->gettable_ctx_params == NULL)
349 return NULL;
350
351 provctx = ossl_provider_ctx(EVP_SIGNATURE_provider(sig));
fb67126e 352 return sig->gettable_ctx_params(NULL, provctx);
e3efe7a5
SL
353}
354
355const OSSL_PARAM *EVP_SIGNATURE_settable_ctx_params(const EVP_SIGNATURE *sig)
356{
357 void *provctx;
358
359 if (sig == NULL || sig->settable_ctx_params == NULL)
360 return NULL;
361
362 provctx = ossl_provider_ctx(EVP_SIGNATURE_provider(sig));
fb67126e 363 return sig->settable_ctx_params(NULL, provctx);
e3efe7a5
SL
364}
365
4b58d9b4
P
366static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation,
367 const OSSL_PARAM params[])
e683582b
SL
368{
369 int ret = 0;
370 void *provkey = NULL;
371 EVP_SIGNATURE *signature = NULL;
f6aa5774
RL
372 EVP_KEYMGMT *tmp_keymgmt = NULL;
373 const char *supported_sig = NULL;
e683582b
SL
374
375 if (ctx == NULL) {
9311d0c4 376 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
377 return -2;
378 }
379
380 evp_pkey_ctx_free_old_ops(ctx);
381 ctx->operation = operation;
382
0b9dd384
RL
383 /*
384 * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
385 * calls can be removed.
386 */
387 ERR_set_mark();
388
f21c9c64 389 if (evp_pkey_ctx_is_legacy(ctx))
e683582b
SL
390 goto legacy;
391
3c6ed955
RL
392 /*
393 * Ensure that the key is provided, either natively, or as a cached export.
394 * If not, go legacy
395 */
f6aa5774 396 tmp_keymgmt = ctx->keymgmt;
3c6ed955
RL
397 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
398 &tmp_keymgmt, ctx->propquery);
d0ddf9b4 399 if (tmp_keymgmt == NULL)
f6aa5774
RL
400 goto legacy;
401 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
0b9dd384 402 ERR_clear_last_mark();
f6aa5774
RL
403 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
404 goto err;
e683582b 405 }
f6aa5774
RL
406 EVP_KEYMGMT_free(ctx->keymgmt);
407 ctx->keymgmt = tmp_keymgmt;
408
409 if (ctx->keymgmt->query_operation_name != NULL)
410 supported_sig = ctx->keymgmt->query_operation_name(OSSL_OP_SIGNATURE);
411
412 /*
413 * If we didn't get a supported sig, assume there is one with the
414 * same name as the key type.
415 */
416 if (supported_sig == NULL)
417 supported_sig = ctx->keytype;
418
419 /*
420 * Because we cleared out old ops, we shouldn't need to worry about
421 * checking if signature is already there.
422 */
423 signature =
424 EVP_SIGNATURE_fetch(ctx->libctx, supported_sig, ctx->propquery);
425
426 if (signature == NULL
e683582b
SL
427 || (EVP_KEYMGMT_provider(ctx->keymgmt)
428 != EVP_SIGNATURE_provider(signature))) {
429 /*
0b9dd384
RL
430 * We don't need to free ctx->keymgmt here, as it's not necessarily
431 * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
e683582b
SL
432 */
433 EVP_SIGNATURE_free(signature);
434 goto legacy;
435 }
436
0b9dd384
RL
437 /*
438 * TODO remove this when legacy is gone
439 * If we don't have the full support we need with provided methods,
440 * let's go see if legacy does.
441 */
442 ERR_pop_to_mark();
443
444 /* No more legacy from here down to legacy: */
445
e683582b 446 ctx->op.sig.signature = signature;
2c6094ba
RL
447 ctx->op.sig.sigprovctx =
448 signature->newctx(ossl_provider_ctx(signature->prov), ctx->propquery);
e683582b
SL
449 if (ctx->op.sig.sigprovctx == NULL) {
450 /* The provider key can stay in the cache */
9311d0c4 451 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
452 goto err;
453 }
454
455 switch (operation) {
456 case EVP_PKEY_OP_SIGN:
457 if (signature->sign_init == NULL) {
9311d0c4 458 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
459 ret = -2;
460 goto err;
461 }
4b58d9b4 462 ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey, params);
e683582b
SL
463 break;
464 case EVP_PKEY_OP_VERIFY:
465 if (signature->verify_init == NULL) {
9311d0c4 466 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
467 ret = -2;
468 goto err;
469 }
4b58d9b4 470 ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey, params);
e683582b
SL
471 break;
472 case EVP_PKEY_OP_VERIFYRECOVER:
473 if (signature->verify_recover_init == NULL) {
9311d0c4 474 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
475 ret = -2;
476 goto err;
477 }
4b58d9b4
P
478 ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey,
479 params);
e683582b
SL
480 break;
481 default:
9311d0c4 482 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
483 goto err;
484 }
485
486 if (ret <= 0) {
487 signature->freectx(ctx->op.sig.sigprovctx);
488 ctx->op.sig.sigprovctx = NULL;
489 goto err;
490 }
86df26b3 491 goto end;
e683582b
SL
492
493 legacy:
0b9dd384
RL
494 /*
495 * TODO remove this when legacy is gone
496 * If we don't have the full support we need with provided methods,
497 * let's go see if legacy does.
498 */
499 ERR_pop_to_mark();
500
e683582b
SL
501 if (ctx->pmeth == NULL
502 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
503 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
504 || (operation == EVP_PKEY_OP_VERIFYRECOVER
505 && ctx->pmeth->verify_recover == NULL)) {
9311d0c4 506 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
507 return -2;
508 }
509
510 switch (operation) {
511 case EVP_PKEY_OP_SIGN:
512 if (ctx->pmeth->sign_init == NULL)
513 return 1;
514 ret = ctx->pmeth->sign_init(ctx);
515 break;
516 case EVP_PKEY_OP_VERIFY:
517 if (ctx->pmeth->verify_init == NULL)
518 return 1;
519 ret = ctx->pmeth->verify_init(ctx);
520 break;
521 case EVP_PKEY_OP_VERIFYRECOVER:
522 if (ctx->pmeth->verify_recover_init == NULL)
523 return 1;
524 ret = ctx->pmeth->verify_recover_init(ctx);
525 break;
526 default:
9311d0c4 527 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
e683582b
SL
528 goto err;
529 }
530 if (ret <= 0)
531 goto err;
86df26b3
RL
532 end:
533#ifndef FIPS_MODULE
534 if (ret > 0)
535 ret = evp_pkey_ctx_use_cached_data(ctx);
536#endif
e683582b 537
86df26b3 538 return ret;
e683582b 539 err:
c7fa9297 540 evp_pkey_ctx_free_old_ops(ctx);
e683582b
SL
541 ctx->operation = EVP_PKEY_OP_UNDEFINED;
542 return ret;
543}
544
545int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
546{
4b58d9b4
P
547 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN, NULL);
548}
549
550int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
551{
552 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN, params);
e683582b
SL
553}
554
555int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
556 unsigned char *sig, size_t *siglen,
557 const unsigned char *tbs, size_t tbslen)
558{
559 int ret;
560
561 if (ctx == NULL) {
9311d0c4 562 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
563 return -2;
564 }
565
566 if (ctx->operation != EVP_PKEY_OP_SIGN) {
bf23b9a1 567 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
e683582b
SL
568 return -1;
569 }
570
571 if (ctx->op.sig.sigprovctx == NULL)
572 goto legacy;
573
574 ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
575 SIZE_MAX, tbs, tbslen);
576
577 return ret;
578 legacy:
579
580 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
9311d0c4 581 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
582 return -2;
583 }
584
585 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
586 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
587}
588
589int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
590{
4b58d9b4
P
591 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY, NULL);
592}
593
594int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
595{
596 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY, params);
e683582b
SL
597}
598
599int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
600 const unsigned char *sig, size_t siglen,
601 const unsigned char *tbs, size_t tbslen)
602{
603 int ret;
604
605 if (ctx == NULL) {
9311d0c4 606 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
607 return -2;
608 }
609
610 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
bf23b9a1 611 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
e683582b
SL
612 return -1;
613 }
614
615 if (ctx->op.sig.sigprovctx == NULL)
616 goto legacy;
617
618 ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
619 tbs, tbslen);
620
621 return ret;
622 legacy:
623 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
9311d0c4 624 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
625 return -2;
626 }
627
628 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
629}
630
631int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
632{
4b58d9b4
P
633 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER, NULL);
634}
635
636int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx,
637 const OSSL_PARAM params[])
638{
639 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER, params);
e683582b
SL
640}
641
642int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
643 unsigned char *rout, size_t *routlen,
644 const unsigned char *sig, size_t siglen)
645{
646 int ret;
647
648 if (ctx == NULL) {
9311d0c4 649 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
650 return -2;
651 }
652
653 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
bf23b9a1 654 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
e683582b
SL
655 return -1;
656 }
657
658 if (ctx->op.sig.sigprovctx == NULL)
659 goto legacy;
660
661 ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
662 routlen,
663 (rout == NULL ? 0 : *routlen),
664 sig, siglen);
665 return ret;
666 legacy:
667 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
9311d0c4 668 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
e683582b
SL
669 return -2;
670 }
671 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
672 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
673}