]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_fn.c
If <operatio>_init function is zero interpret as noop.
[thirdparty/openssl.git] / crypto / evp / pmeth_fn.c
1 /* pmeth_fn.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2006.
4 */
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <openssl/objects.h>
62 #include "cryptlib.h"
63 #include <openssl/evp.h>
64 #include "evp_locl.h"
65
66 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
67 {
68 int ret;
69 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign)
70 {
71 EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
72 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
73 return -2;
74 }
75 ctx->operation = EVP_PKEY_OP_SIGN;
76 if (!ctx->pmeth->sign_init)
77 return 1;
78 ret = ctx->pmeth->sign_init(ctx);
79 if (ret <= 0)
80 ctx->operation = EVP_PKEY_OP_UNDEFINED;
81 return ret;
82 }
83
84 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
85 unsigned char *sig, int *siglen,
86 unsigned char *tbs, int tbslen)
87 {
88 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign)
89 {
90 EVPerr(EVP_F_EVP_PKEY_SIGN,
91 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
92 return -2;
93 }
94 if (ctx->operation != EVP_PKEY_OP_SIGN)
95 {
96 EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
97 return -1;
98 }
99 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
100 }
101
102 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
103 {
104 int ret;
105 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
106 {
107 EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
108 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
109 return -2;
110 }
111 ctx->operation = EVP_PKEY_OP_VERIFY;
112 if (!ctx->pmeth->verify_init)
113 return 1;
114 ret = ctx->pmeth->verify_init(ctx);
115 if (ret <= 0)
116 ctx->operation = EVP_PKEY_OP_UNDEFINED;
117 return ret;
118 }
119
120 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
121 unsigned char *sig, int siglen,
122 unsigned char *tbs, int tbslen)
123 {
124 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
125 {
126 EVPerr(EVP_F_EVP_PKEY_VERIFY,
127 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
128 return -2;
129 }
130 if (ctx->operation != EVP_PKEY_OP_VERIFY)
131 {
132 EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
133 return -1;
134 }
135 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
136 }
137
138 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
139 {
140 int ret;
141 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
142 {
143 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
144 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
145 return -2;
146 }
147 ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
148 if (!ctx->pmeth->verify_recover_init)
149 return 1;
150 ret = ctx->pmeth->verify_recover_init(ctx);
151 if (ret <= 0)
152 ctx->operation = EVP_PKEY_OP_UNDEFINED;
153 return ret;
154 }
155
156 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
157 unsigned char *rout, int *routlen,
158 unsigned char *sig, int siglen)
159 {
160 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
161 {
162 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
163 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
164 return -2;
165 }
166 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER)
167 {
168 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
169 return -1;
170 }
171 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
172 }
173
174 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
175 {
176 int ret;
177 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
178 {
179 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
180 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
181 return -2;
182 }
183 ctx->operation = EVP_PKEY_OP_ENCRYPT;
184 if (!ctx->pmeth->encrypt_init)
185 return 1;
186 ret = ctx->pmeth->encrypt_init(ctx);
187 if (ret <= 0)
188 ctx->operation = EVP_PKEY_OP_UNDEFINED;
189 return ret;
190 }
191
192 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
193 unsigned char *out, int *outlen,
194 unsigned char *in, int inlen)
195 {
196 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
197 {
198 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
199 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
200 return -2;
201 }
202 if (ctx->operation != EVP_PKEY_OP_ENCRYPT)
203 {
204 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
205 return -1;
206 }
207 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
208 }
209
210 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
211 {
212 int ret;
213 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
214 {
215 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
216 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
217 return -2;
218 }
219 ctx->operation = EVP_PKEY_OP_DECRYPT;
220 if (!ctx->pmeth->decrypt_init)
221 return 1;
222 ret = ctx->pmeth->decrypt_init(ctx);
223 if (ret <= 0)
224 ctx->operation = EVP_PKEY_OP_UNDEFINED;
225 return ret;
226 }
227
228 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
229 unsigned char *out, int *outlen,
230 unsigned char *in, int inlen)
231 {
232 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
233 {
234 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
235 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
236 return -2;
237 }
238 if (ctx->operation != EVP_PKEY_OP_DECRYPT)
239 {
240 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
241 return -1;
242 }
243 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
244 }
245