]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_fn.c
PKEY: adapt the export_to_provider funtions to handle domain params too
[thirdparty/openssl.git] / crypto / evp / pmeth_fn.c
1 /*
2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
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
19 static EVP_SIGNATURE *evp_signature_new(OSSL_PROVIDER *prov)
20 {
21 EVP_SIGNATURE *signature = OPENSSL_zalloc(sizeof(EVP_SIGNATURE));
22
23 signature->lock = CRYPTO_THREAD_lock_new();
24 if (signature->lock == NULL) {
25 OPENSSL_free(signature);
26 return NULL;
27 }
28 signature->prov = prov;
29 ossl_provider_up_ref(prov);
30 signature->refcnt = 1;
31
32 return signature;
33 }
34
35 static void *evp_signature_from_dispatch(int name_id,
36 const OSSL_DISPATCH *fns,
37 OSSL_PROVIDER *prov,
38 void *vkeymgmt_data)
39 {
40 /*
41 * Signature functions cannot work without a key, and key management
42 * from the same provider to manage its keys. We therefore fetch
43 * a key management method using the same algorithm and properties
44 * and pass that down to evp_generic_fetch to be passed on to our
45 * evp_signature_from_dispatch, which will attach the key management
46 * method to the newly created key exchange method as long as the
47 * provider matches.
48 */
49 struct keymgmt_data_st *keymgmt_data = vkeymgmt_data;
50 EVP_KEYMGMT *keymgmt =
51 evp_keymgmt_fetch_by_number(keymgmt_data->ctx, name_id,
52 keymgmt_data->properties);
53 EVP_SIGNATURE *signature = NULL;
54 int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0;
55 int digsignfncnt = 0, digverifyfncnt = 0;
56 int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0;
57
58 if (keymgmt == NULL || EVP_KEYMGMT_provider(keymgmt) != prov) {
59 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEYMGMT_AVAILABLE);
60 goto err;
61 }
62
63 if ((signature = evp_signature_new(prov)) == NULL) {
64 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
65 goto err;
66 }
67
68 signature->name_id = name_id;
69 signature->keymgmt = keymgmt;
70 keymgmt = NULL; /* avoid double free on failure below */
71
72 for (; fns->function_id != 0; fns++) {
73 switch (fns->function_id) {
74 case OSSL_FUNC_SIGNATURE_NEWCTX:
75 if (signature->newctx != NULL)
76 break;
77 signature->newctx = OSSL_get_OP_signature_newctx(fns);
78 ctxfncnt++;
79 break;
80 case OSSL_FUNC_SIGNATURE_SIGN_INIT:
81 if (signature->sign_init != NULL)
82 break;
83 signature->sign_init = OSSL_get_OP_signature_sign_init(fns);
84 signfncnt++;
85 break;
86 case OSSL_FUNC_SIGNATURE_SIGN:
87 if (signature->sign != NULL)
88 break;
89 signature->sign = OSSL_get_OP_signature_sign(fns);
90 signfncnt++;
91 break;
92 case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
93 if (signature->verify_init != NULL)
94 break;
95 signature->verify_init = OSSL_get_OP_signature_verify_init(fns);
96 verifyfncnt++;
97 break;
98 case OSSL_FUNC_SIGNATURE_VERIFY:
99 if (signature->verify != NULL)
100 break;
101 signature->verify = OSSL_get_OP_signature_verify(fns);
102 verifyfncnt++;
103 break;
104 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
105 if (signature->verify_recover_init != NULL)
106 break;
107 signature->verify_recover_init
108 = OSSL_get_OP_signature_verify_recover_init(fns);
109 verifyrecfncnt++;
110 break;
111 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
112 if (signature->verify_recover != NULL)
113 break;
114 signature->verify_recover
115 = OSSL_get_OP_signature_verify_recover(fns);
116 verifyrecfncnt++;
117 break;
118 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT:
119 if (signature->digest_sign_init != NULL)
120 break;
121 signature->digest_sign_init
122 = OSSL_get_OP_signature_digest_sign_init(fns);
123 digsignfncnt++;
124 break;
125 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
126 if (signature->digest_sign_update != NULL)
127 break;
128 signature->digest_sign_update
129 = OSSL_get_OP_signature_digest_sign_update(fns);
130 digsignfncnt++;
131 break;
132 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL:
133 if (signature->digest_sign_final != NULL)
134 break;
135 signature->digest_sign_final
136 = OSSL_get_OP_signature_digest_sign_final(fns);
137 digsignfncnt++;
138 break;
139 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
140 if (signature->digest_verify_init != NULL)
141 break;
142 signature->digest_verify_init
143 = OSSL_get_OP_signature_digest_verify_init(fns);
144 digverifyfncnt++;
145 break;
146 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
147 if (signature->digest_verify_update != NULL)
148 break;
149 signature->digest_verify_update
150 = OSSL_get_OP_signature_digest_verify_update(fns);
151 digverifyfncnt++;
152 break;
153 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL:
154 if (signature->digest_verify_final != NULL)
155 break;
156 signature->digest_verify_final
157 = OSSL_get_OP_signature_digest_verify_final(fns);
158 digverifyfncnt++;
159 break;
160 case OSSL_FUNC_SIGNATURE_FREECTX:
161 if (signature->freectx != NULL)
162 break;
163 signature->freectx = OSSL_get_OP_signature_freectx(fns);
164 ctxfncnt++;
165 break;
166 case OSSL_FUNC_SIGNATURE_DUPCTX:
167 if (signature->dupctx != NULL)
168 break;
169 signature->dupctx = OSSL_get_OP_signature_dupctx(fns);
170 break;
171 case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
172 if (signature->get_ctx_params != NULL)
173 break;
174 signature->get_ctx_params
175 = OSSL_get_OP_signature_get_ctx_params(fns);
176 gparamfncnt++;
177 break;
178 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
179 if (signature->gettable_ctx_params != NULL)
180 break;
181 signature->gettable_ctx_params
182 = OSSL_get_OP_signature_gettable_ctx_params(fns);
183 gparamfncnt++;
184 break;
185 case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
186 if (signature->set_ctx_params != NULL)
187 break;
188 signature->set_ctx_params
189 = OSSL_get_OP_signature_set_ctx_params(fns);
190 sparamfncnt++;
191 break;
192 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
193 if (signature->settable_ctx_params != NULL)
194 break;
195 signature->settable_ctx_params
196 = OSSL_get_OP_signature_settable_ctx_params(fns);
197 sparamfncnt++;
198 break;
199 case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS:
200 if (signature->get_ctx_md_params != NULL)
201 break;
202 signature->get_ctx_md_params
203 = OSSL_get_OP_signature_get_ctx_md_params(fns);
204 gmdparamfncnt++;
205 break;
206 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS:
207 if (signature->gettable_ctx_md_params != NULL)
208 break;
209 signature->gettable_ctx_md_params
210 = OSSL_get_OP_signature_gettable_ctx_md_params(fns);
211 gmdparamfncnt++;
212 break;
213 case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS:
214 if (signature->set_ctx_md_params != NULL)
215 break;
216 signature->set_ctx_md_params
217 = OSSL_get_OP_signature_set_ctx_md_params(fns);
218 smdparamfncnt++;
219 break;
220 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS:
221 if (signature->settable_ctx_md_params != NULL)
222 break;
223 signature->settable_ctx_md_params
224 = OSSL_get_OP_signature_settable_ctx_md_params(fns);
225 smdparamfncnt++;
226 break;
227 }
228 }
229 if (ctxfncnt != 2
230 || (signfncnt == 0
231 && verifyfncnt == 0
232 && verifyrecfncnt == 0
233 && digsignfncnt == 0
234 && digverifyfncnt == 0)
235 || (signfncnt != 0 && signfncnt != 2)
236 || (verifyfncnt != 0 && verifyfncnt != 2)
237 || (verifyrecfncnt != 0 && verifyrecfncnt != 2)
238 || (digsignfncnt != 0 && digsignfncnt != 3)
239 || (digverifyfncnt != 0 && digverifyfncnt != 3)
240 || (gparamfncnt != 0 && gparamfncnt != 2)
241 || (sparamfncnt != 0 && sparamfncnt != 2)
242 || (gmdparamfncnt != 0 && gmdparamfncnt != 2)
243 || (smdparamfncnt != 0 && smdparamfncnt != 2)) {
244 /*
245 * In order to be a consistent set of functions we must have at least
246 * a set of context functions (newctx and freectx) as well as a set of
247 * "signature" functions:
248 * (sign_init, sign) or
249 * (verify_init verify) or
250 * (verify_recover_init, verify_recover) or
251 * (digest_sign_init, digest_sign_update, digest_sign_final) or
252 * (digest_verify_init, digest_verify_update, digest_verify_final).
253 *
254 * set_ctx_params and settable_ctx_params are optional, but if one of
255 * them is present then the other one must also be present. The same
256 * applies to get_ctx_params and gettable_ctx_params. The same rules
257 * apply to the "md_params" functions. The dupctx function is optional.
258 */
259 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
260 goto err;
261 }
262
263 return signature;
264 err:
265 EVP_SIGNATURE_free(signature);
266 EVP_KEYMGMT_free(keymgmt);
267 return NULL;
268 }
269
270 void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
271 {
272 if (signature != NULL) {
273 int i;
274
275 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
276 if (i > 0)
277 return;
278 EVP_KEYMGMT_free(signature->keymgmt);
279 ossl_provider_free(signature->prov);
280 CRYPTO_THREAD_lock_free(signature->lock);
281 OPENSSL_free(signature);
282 }
283 }
284
285 int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
286 {
287 int ref = 0;
288
289 CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
290 return 1;
291 }
292
293 OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
294 {
295 return signature->prov;
296 }
297
298 EVP_SIGNATURE *EVP_SIGNATURE_fetch(OPENSSL_CTX *ctx, const char *algorithm,
299 const char *properties)
300 {
301 struct keymgmt_data_st keymgmt_data;
302
303 /*
304 * A signature operation cannot work without a key, so we need key
305 * management from the same provider to manage its keys.
306 */
307 keymgmt_data.ctx = ctx;
308 keymgmt_data.properties = properties;
309 return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
310 evp_signature_from_dispatch, &keymgmt_data,
311 (int (*)(void *))EVP_SIGNATURE_up_ref,
312 (void (*)(void *))EVP_SIGNATURE_free);
313 }
314
315 int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name)
316 {
317 return evp_is_a(signature->prov, signature->name_id, name);
318 }
319
320 int EVP_SIGNATURE_number(const EVP_SIGNATURE *signature)
321 {
322 return signature->name_id;
323 }
324
325 void EVP_SIGNATURE_do_all_provided(OPENSSL_CTX *libctx,
326 void (*fn)(EVP_SIGNATURE *signature,
327 void *arg),
328 void *arg)
329 {
330 struct keymgmt_data_st keymgmt_data;
331
332 keymgmt_data.ctx = libctx;
333 keymgmt_data.properties = NULL;
334 evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
335 (void (*)(void *, void *))fn, arg,
336 evp_signature_from_dispatch, &keymgmt_data,
337 (void (*)(void *))EVP_SIGNATURE_free);
338 }
339
340
341 void EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
342 void (*fn)(const char *name, void *data),
343 void *data)
344 {
345 if (signature->prov != NULL)
346 evp_names_do_all(signature->prov, signature->name_id, fn, data);
347 }
348
349 static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature,
350 int operation)
351 {
352 int ret = 0;
353 void *provkey = NULL;
354
355 if (ctx == NULL) {
356 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
357 return -2;
358 }
359
360 evp_pkey_ctx_free_old_ops(ctx);
361 ctx->operation = operation;
362
363 if (ctx->engine != NULL)
364 goto legacy;
365
366 if (signature != NULL) {
367 if (!EVP_SIGNATURE_up_ref(signature))
368 goto err;
369 } else {
370 int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
371
372 /*
373 * TODO(3.0): Check for legacy handling. Remove this once all all
374 * algorithms are moved to providers.
375 */
376 if (ctx->pkey != NULL) {
377 switch (ctx->pkey->type) {
378 case NID_dsa:
379 break;
380 default:
381 goto legacy;
382 }
383 signature = EVP_SIGNATURE_fetch(NULL, OBJ_nid2sn(nid), NULL);
384 } else {
385 goto legacy;
386 }
387
388 if (signature == NULL) {
389 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
390 goto err;
391 }
392 }
393
394 ctx->op.sig.signature = signature;
395 if (ctx->pkey != NULL) {
396 provkey =
397 evp_keymgmt_export_to_provider(ctx->pkey, signature->keymgmt, 0);
398 if (provkey == NULL) {
399 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
400 goto err;
401 }
402 }
403 ctx->op.sig.sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov));
404 if (ctx->op.sig.sigprovctx == NULL) {
405 /* The provider key can stay in the cache */
406 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
407 goto err;
408 }
409
410 switch (operation) {
411 case EVP_PKEY_OP_SIGN:
412 if (signature->sign_init == NULL) {
413 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
414 ret = -2;
415 goto err;
416 }
417 ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey);
418 break;
419 case EVP_PKEY_OP_VERIFY:
420 if (signature->verify_init == NULL) {
421 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
422 ret = -2;
423 goto err;
424 }
425 ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey);
426 break;
427 case EVP_PKEY_OP_VERIFYRECOVER:
428 if (signature->verify_recover_init == NULL) {
429 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
430 ret = -2;
431 goto err;
432 }
433 ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey);
434 break;
435 default:
436 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
437 goto err;
438 }
439
440 if (ret <= 0) {
441 signature->freectx(ctx->op.sig.sigprovctx);
442 ctx->op.sig.sigprovctx = NULL;
443 goto err;
444 }
445 return 1;
446
447 legacy:
448 if (ctx->pmeth == NULL
449 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
450 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
451 || (operation == EVP_PKEY_OP_VERIFYRECOVER
452 && ctx->pmeth->verify_recover == NULL)) {
453 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
454 return -2;
455 }
456
457 switch (operation) {
458 case EVP_PKEY_OP_SIGN:
459 if (ctx->pmeth->sign_init == NULL)
460 return 1;
461 ret = ctx->pmeth->sign_init(ctx);
462 break;
463 case EVP_PKEY_OP_VERIFY:
464 if (ctx->pmeth->verify_init == NULL)
465 return 1;
466 ret = ctx->pmeth->verify_init(ctx);
467 break;
468 case EVP_PKEY_OP_VERIFYRECOVER:
469 if (ctx->pmeth->verify_recover_init == NULL)
470 return 1;
471 ret = ctx->pmeth->verify_recover_init(ctx);
472 break;
473 default:
474 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
475 goto err;
476 }
477 if (ret <= 0)
478 goto err;
479 return ret;
480
481 err:
482 ctx->operation = EVP_PKEY_OP_UNDEFINED;
483 return ret;
484 }
485
486 int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
487 {
488 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_SIGN);
489 }
490
491 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
492 {
493 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN);
494 }
495
496 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
497 unsigned char *sig, size_t *siglen,
498 const unsigned char *tbs, size_t tbslen)
499 {
500 int ret;
501
502 if (ctx == NULL) {
503 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
504 return -2;
505 }
506
507 if (ctx->operation != EVP_PKEY_OP_SIGN) {
508 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
509 return -1;
510 }
511
512 if (ctx->op.sig.sigprovctx == NULL)
513 goto legacy;
514
515 ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
516 SIZE_MAX, tbs, tbslen);
517
518 return ret;
519 legacy:
520
521 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
522 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
523 return -2;
524 }
525
526 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
527 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
528 }
529
530 int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
531 {
532 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFY);
533 }
534
535 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
536 {
537 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY);
538 }
539
540 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
541 const unsigned char *sig, size_t siglen,
542 const unsigned char *tbs, size_t tbslen)
543 {
544 int ret;
545
546 if (ctx == NULL) {
547 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
548 return -2;
549 }
550
551 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
552 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
553 return -1;
554 }
555
556 if (ctx->op.sig.sigprovctx == NULL)
557 goto legacy;
558
559 ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
560 tbs, tbslen);
561
562 return ret;
563 legacy:
564 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
565 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
566 return -2;
567 }
568
569 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
570 }
571
572 int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
573 {
574 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFYRECOVER);
575 }
576
577 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
578 {
579 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER);
580 }
581
582 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
583 unsigned char *rout, size_t *routlen,
584 const unsigned char *sig, size_t siglen)
585 {
586 int ret;
587
588 if (ctx == NULL) {
589 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
590 return -2;
591 }
592
593 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
594 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
595 return -1;
596 }
597
598 if (ctx->op.sig.sigprovctx == NULL)
599 goto legacy;
600
601 ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
602 routlen,
603 (rout == NULL ? 0 : *routlen),
604 sig, siglen);
605 return ret;
606 legacy:
607 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
608 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
609 return -2;
610 }
611 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
612 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
613 }
614
615 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
616 {
617 int ret;
618 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
619 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
620 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
621 return -2;
622 }
623 ctx->operation = EVP_PKEY_OP_ENCRYPT;
624 if (!ctx->pmeth->encrypt_init)
625 return 1;
626 ret = ctx->pmeth->encrypt_init(ctx);
627 if (ret <= 0)
628 ctx->operation = EVP_PKEY_OP_UNDEFINED;
629 return ret;
630 }
631
632 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
633 unsigned char *out, size_t *outlen,
634 const unsigned char *in, size_t inlen)
635 {
636 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
637 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
638 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
639 return -2;
640 }
641 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
642 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
643 return -1;
644 }
645 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
646 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
647 }
648
649 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
650 {
651 int ret;
652 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
653 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
654 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
655 return -2;
656 }
657 ctx->operation = EVP_PKEY_OP_DECRYPT;
658 if (!ctx->pmeth->decrypt_init)
659 return 1;
660 ret = ctx->pmeth->decrypt_init(ctx);
661 if (ret <= 0)
662 ctx->operation = EVP_PKEY_OP_UNDEFINED;
663 return ret;
664 }
665
666 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
667 unsigned char *out, size_t *outlen,
668 const unsigned char *in, size_t inlen)
669 {
670 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
671 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
672 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
673 return -2;
674 }
675 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
676 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
677 return -1;
678 }
679 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
680 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
681 }