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