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