Make the EVP Key Exchange code provider aware
[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 "internal/evp_int.h"
16 #include "evp_locl.h"
17
18 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
19 {
20     int ret;
21     if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
22         EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
23                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
24         return -2;
25     }
26     ctx->operation = EVP_PKEY_OP_SIGN;
27     if (!ctx->pmeth->sign_init)
28         return 1;
29     ret = ctx->pmeth->sign_init(ctx);
30     if (ret <= 0)
31         ctx->operation = EVP_PKEY_OP_UNDEFINED;
32     return ret;
33 }
34
35 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
36                   unsigned char *sig, size_t *siglen,
37                   const unsigned char *tbs, size_t tbslen)
38 {
39     if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
40         EVPerr(EVP_F_EVP_PKEY_SIGN,
41                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
42         return -2;
43     }
44     if (ctx->operation != EVP_PKEY_OP_SIGN) {
45         EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
46         return -1;
47     }
48     M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
49         return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
50 }
51
52 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
53 {
54     int ret;
55     if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
56         EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
57                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
58         return -2;
59     }
60     ctx->operation = EVP_PKEY_OP_VERIFY;
61     if (!ctx->pmeth->verify_init)
62         return 1;
63     ret = ctx->pmeth->verify_init(ctx);
64     if (ret <= 0)
65         ctx->operation = EVP_PKEY_OP_UNDEFINED;
66     return ret;
67 }
68
69 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
70                     const unsigned char *sig, size_t siglen,
71                     const unsigned char *tbs, size_t tbslen)
72 {
73     if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
74         EVPerr(EVP_F_EVP_PKEY_VERIFY,
75                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
76         return -2;
77     }
78     if (ctx->operation != EVP_PKEY_OP_VERIFY) {
79         EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
80         return -1;
81     }
82     return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
83 }
84
85 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
86 {
87     int ret;
88     if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
89         EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
90                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
91         return -2;
92     }
93     ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
94     if (!ctx->pmeth->verify_recover_init)
95         return 1;
96     ret = ctx->pmeth->verify_recover_init(ctx);
97     if (ret <= 0)
98         ctx->operation = EVP_PKEY_OP_UNDEFINED;
99     return ret;
100 }
101
102 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
103                             unsigned char *rout, size_t *routlen,
104                             const unsigned char *sig, size_t siglen)
105 {
106     if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
107         EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
108                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
109         return -2;
110     }
111     if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
112         EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
113         return -1;
114     }
115     M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
116         return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
117 }
118
119 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
120 {
121     int ret;
122     if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
123         EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
124                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
125         return -2;
126     }
127     ctx->operation = EVP_PKEY_OP_ENCRYPT;
128     if (!ctx->pmeth->encrypt_init)
129         return 1;
130     ret = ctx->pmeth->encrypt_init(ctx);
131     if (ret <= 0)
132         ctx->operation = EVP_PKEY_OP_UNDEFINED;
133     return ret;
134 }
135
136 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
137                      unsigned char *out, size_t *outlen,
138                      const unsigned char *in, size_t inlen)
139 {
140     if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
141         EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
142                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
143         return -2;
144     }
145     if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
146         EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
147         return -1;
148     }
149     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
150         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
151 }
152
153 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
154 {
155     int ret;
156     if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
157         EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
158                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
159         return -2;
160     }
161     ctx->operation = EVP_PKEY_OP_DECRYPT;
162     if (!ctx->pmeth->decrypt_init)
163         return 1;
164     ret = ctx->pmeth->decrypt_init(ctx);
165     if (ret <= 0)
166         ctx->operation = EVP_PKEY_OP_UNDEFINED;
167     return ret;
168 }
169
170 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
171                      unsigned char *out, size_t *outlen,
172                      const unsigned char *in, size_t inlen)
173 {
174     if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
175         EVPerr(EVP_F_EVP_PKEY_DECRYPT,
176                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
177         return -2;
178     }
179     if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
180         EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
181         return -1;
182     }
183     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
184         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
185 }