2b4bca5727821e9e12adbab14a849d06b14ca308
[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 void EVP_SIGNATURE_do_all_provided(OPENSSL_CTX *libctx,
321                                    void (*fn)(EVP_SIGNATURE *signature,
322                                               void *arg),
323                                    void *arg)
324 {
325     struct keymgmt_data_st keymgmt_data;
326
327     keymgmt_data.ctx = libctx;
328     keymgmt_data.properties = NULL;
329     evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
330                        (void (*)(void *, void *))fn, arg,
331                        evp_signature_from_dispatch, &keymgmt_data,
332                        (void (*)(void *))EVP_SIGNATURE_free);
333 }
334
335 static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature,
336                                    int operation)
337 {
338     int ret = 0;
339     void *provkey = NULL;
340
341     if (ctx == NULL) {
342         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
343         return -2;
344     }
345
346     evp_pkey_ctx_free_old_ops(ctx);
347     ctx->operation = operation;
348
349     if (ctx->engine != NULL)
350         goto legacy;
351
352     if (signature != NULL) {
353         if (!EVP_SIGNATURE_up_ref(signature))
354             goto err;
355     } else {
356         int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
357
358         /*
359          * TODO(3.0): Check for legacy handling. Remove this once all all
360          * algorithms are moved to providers.
361          */
362         if (ctx->pkey != NULL) {
363             switch (ctx->pkey->type) {
364             case NID_dsa:
365                 break;
366             default:
367                 goto legacy;
368             }
369             signature = EVP_SIGNATURE_fetch(NULL, OBJ_nid2sn(nid), NULL);
370         } else {
371             goto legacy;
372         }
373
374         if (signature == NULL) {
375             EVPerr(0, EVP_R_INITIALIZATION_ERROR);
376             goto err;
377         }
378     }
379
380     ctx->op.sig.signature = signature;
381     if (ctx->pkey != NULL) {
382         provkey = evp_keymgmt_export_to_provider(ctx->pkey, signature->keymgmt);
383         if (provkey == NULL) {
384             EVPerr(0, EVP_R_INITIALIZATION_ERROR);
385             goto err;
386         }
387     }
388     ctx->op.sig.sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov));
389     if (ctx->op.sig.sigprovctx == NULL) {
390         /* The provider key can stay in the cache */
391         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
392         goto err;
393     }
394
395     switch (operation) {
396     case EVP_PKEY_OP_SIGN:
397         if (signature->sign_init == NULL) {
398             EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
399             ret = -2;
400             goto err;
401         }
402         ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey);
403         break;
404     case EVP_PKEY_OP_VERIFY:
405         if (signature->verify_init == NULL) {
406             EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
407             ret = -2;
408             goto err;
409         }
410         ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey);
411         break;
412     case EVP_PKEY_OP_VERIFYRECOVER:
413         if (signature->verify_recover_init == NULL) {
414             EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
415             ret = -2;
416             goto err;
417         }
418         ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey);
419         break;
420     default:
421         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
422         goto err;
423     }
424
425     if (ret <= 0) {
426         signature->freectx(ctx->op.sig.sigprovctx);
427         ctx->op.sig.sigprovctx = NULL;
428         goto err;
429     }
430     return 1;
431
432  legacy:
433     if (ctx->pmeth == NULL
434             || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
435             || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
436             || (operation == EVP_PKEY_OP_VERIFYRECOVER
437                 && ctx->pmeth->verify_recover == NULL)) {
438         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
439         return -2;
440     }
441
442     switch (operation) {
443     case EVP_PKEY_OP_SIGN:
444         if (ctx->pmeth->sign_init == NULL)
445             return 1;
446         ret = ctx->pmeth->sign_init(ctx);
447         break;
448     case EVP_PKEY_OP_VERIFY:
449         if (ctx->pmeth->verify_init == NULL)
450             return 1;
451         ret = ctx->pmeth->verify_init(ctx);
452         break;
453     case EVP_PKEY_OP_VERIFYRECOVER:
454         if (ctx->pmeth->verify_recover_init == NULL)
455             return 1;
456         ret = ctx->pmeth->verify_recover_init(ctx);
457         break;
458     default:
459         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
460         goto err;
461     }
462     if (ret <= 0)
463         goto err;
464     return ret;
465
466  err:
467     ctx->operation = EVP_PKEY_OP_UNDEFINED;
468     return ret;
469 }
470
471 int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
472 {
473     return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_SIGN);
474 }
475
476 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
477 {
478     return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN);
479 }
480
481 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
482                   unsigned char *sig, size_t *siglen,
483                   const unsigned char *tbs, size_t tbslen)
484 {
485     int ret;
486
487     if (ctx == NULL) {
488         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
489         return -2;
490     }
491
492     if (ctx->operation != EVP_PKEY_OP_SIGN) {
493         EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
494         return -1;
495     }
496
497     if (ctx->op.sig.sigprovctx == NULL)
498         goto legacy;
499
500     ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
501                                       SIZE_MAX, tbs, tbslen);
502
503     return ret;
504  legacy:
505  
506     if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
507         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
508         return -2;
509     }
510
511     M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
512         return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
513 }
514
515 int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
516 {
517     return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFY);
518 }
519
520 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
521 {
522     return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY);
523 }
524
525 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
526                     const unsigned char *sig, size_t siglen,
527                     const unsigned char *tbs, size_t tbslen)
528 {
529     int ret;
530
531     if (ctx == NULL) {
532         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
533         return -2;
534     }
535
536     if (ctx->operation != EVP_PKEY_OP_VERIFY) {
537         EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
538         return -1;
539     }
540
541     if (ctx->op.sig.sigprovctx == NULL)
542         goto legacy;
543
544     ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
545                                         tbs, tbslen);
546
547     return ret;
548  legacy:
549     if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
550         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
551         return -2;
552     }
553
554     return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
555 }
556
557 int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
558 {
559     return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFYRECOVER);
560 }
561
562 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
563 {
564     return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER);
565 }
566
567 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
568                             unsigned char *rout, size_t *routlen,
569                             const unsigned char *sig, size_t siglen)
570 {
571     int ret;
572
573     if (ctx == NULL) {
574         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
575         return -2;
576     }
577
578     if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
579         EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
580         return -1;
581     }
582
583     if (ctx->op.sig.sigprovctx == NULL)
584         goto legacy;
585
586     ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
587                                                 routlen,
588                                                 (rout == NULL ? 0 : *routlen),
589                                                 sig, siglen);
590     return ret;
591  legacy:
592     if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
593         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
594         return -2;
595     }
596     M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
597         return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
598 }
599
600 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
601 {
602     int ret;
603     if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
604         EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
605                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
606         return -2;
607     }
608     ctx->operation = EVP_PKEY_OP_ENCRYPT;
609     if (!ctx->pmeth->encrypt_init)
610         return 1;
611     ret = ctx->pmeth->encrypt_init(ctx);
612     if (ret <= 0)
613         ctx->operation = EVP_PKEY_OP_UNDEFINED;
614     return ret;
615 }
616
617 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
618                      unsigned char *out, size_t *outlen,
619                      const unsigned char *in, size_t inlen)
620 {
621     if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
622         EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
623                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
624         return -2;
625     }
626     if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
627         EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
628         return -1;
629     }
630     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
631         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
632 }
633
634 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
635 {
636     int ret;
637     if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
638         EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
639                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
640         return -2;
641     }
642     ctx->operation = EVP_PKEY_OP_DECRYPT;
643     if (!ctx->pmeth->decrypt_init)
644         return 1;
645     ret = ctx->pmeth->decrypt_init(ctx);
646     if (ret <= 0)
647         ctx->operation = EVP_PKEY_OP_UNDEFINED;
648     return ret;
649 }
650
651 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
652                      unsigned char *out, size_t *outlen,
653                      const unsigned char *in, size_t inlen)
654 {
655     if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
656         EVPerr(EVP_F_EVP_PKEY_DECRYPT,
657                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
658         return -2;
659     }
660     if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
661         EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
662         return -1;
663     }
664     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
665         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
666 }