Remove legacy sign/verify from EVP_MD.
[openssl.git] / engines / e_dasync.c
1 /* engines/e_dasync.c */
2 /*
3  * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2015 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
54 #include <stdio.h>
55 #include <string.h>
56
57 #include <openssl/engine.h>
58 #include <openssl/sha.h>
59 #include <openssl/rsa.h>
60 #include <openssl/evp.h>
61 #include <openssl/async.h>
62 #include <openssl/bn.h>
63
64 #define DASYNC_LIB_NAME "DASYNC"
65 #include "e_dasync_err.c"
66
67 /* Engine Id and Name */
68 static const char *engine_dasync_id = "dasync";
69 static const char *engine_dasync_name = "Dummy Async engine support";
70
71
72 /* Engine Lifetime functions */
73 static int dasync_destroy(ENGINE *e);
74 static int dasync_init(ENGINE *e);
75 static int dasync_finish(ENGINE *e);
76 void ENGINE_load_dasync(void);
77
78
79 /* Set up digests. Just SHA1 for now */
80 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
81                           const int **nids, int nid);
82
83 static int dasync_digest_nids[] = { NID_sha1, 0 };
84
85 static void dummy_pause_job(void);
86
87 /* SHA1 */
88 static int dasync_sha1_init(EVP_MD_CTX *ctx);
89 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
90                              size_t count);
91 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
92
93 static const EVP_MD dasync_sha1 = {
94     NID_sha1,
95     NID_sha1WithRSAEncryption,
96     SHA_DIGEST_LENGTH,
97     EVP_MD_FLAG_DIGALGID_ABSENT,
98     dasync_sha1_init,
99     dasync_sha1_update,
100     dasync_sha1_final,
101     NULL,
102     NULL,
103     SHA_CBLOCK,
104     sizeof(EVP_MD *) + sizeof(SHA_CTX),
105 };
106
107 /* RSA */
108
109 static int dasync_pub_enc(int flen, const unsigned char *from,
110                     unsigned char *to, RSA *rsa, int padding);
111 static int dasync_pub_dec(int flen, const unsigned char *from,
112                     unsigned char *to, RSA *rsa, int padding);
113 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
114                       unsigned char *to, RSA *rsa, int padding);
115 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
116                       unsigned char *to, RSA *rsa, int padding);
117 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
118                               BN_CTX *ctx);
119
120 static int dasync_rsa_init(RSA *rsa);
121 static int dasync_rsa_finish(RSA *rsa);
122
123 static RSA_METHOD dasync_rsa_method = {
124     "Dummy Async RSA method",
125     dasync_pub_enc,             /* pub_enc */
126     dasync_pub_dec,             /* pub_dec */
127     dasync_rsa_priv_enc,        /* priv_enc */
128     dasync_rsa_priv_dec,        /* priv_dec */
129     dasync_rsa_mod_exp,         /* rsa_mod_exp */
130     BN_mod_exp_mont,            /* bn_mod_exp */
131     dasync_rsa_init,            /* init */
132     dasync_rsa_finish,          /* finish */
133     0,                          /* flags */
134     NULL,                       /* app_data */
135     0,                          /* rsa_sign */
136     0,                          /* rsa_verify */
137     NULL                        /* rsa_keygen */
138 };
139
140
141 static int bind_dasync(ENGINE *e)
142 {
143     /* Ensure the dasync error handling is set up */
144     ERR_load_DASYNC_strings();
145
146     if (!ENGINE_set_id(e, engine_dasync_id)
147         || !ENGINE_set_name(e, engine_dasync_name)
148         || !ENGINE_set_RSA(e, &dasync_rsa_method)
149         || !ENGINE_set_digests(e, dasync_digests)
150         || !ENGINE_set_destroy_function(e, dasync_destroy)
151         || !ENGINE_set_init_function(e, dasync_init)
152         || !ENGINE_set_finish_function(e, dasync_finish)) {
153         DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
154         return 0;
155     }
156
157     return 1;
158 }
159
160 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
161 static int bind_helper(ENGINE *e, const char *id)
162 {
163     if (id && (strcmp(id, engine_dasync_id) != 0))
164         return 0;
165     if (!bind_dasync(e))
166         return 0;
167     return 1;
168 }
169
170 IMPLEMENT_DYNAMIC_CHECK_FN()
171     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
172 # endif
173
174 static ENGINE *engine_dasync(void)
175 {
176     ENGINE *ret = ENGINE_new();
177     if (!ret)
178         return NULL;
179     if (!bind_dasync(ret)) {
180         ENGINE_free(ret);
181         return NULL;
182     }
183     return ret;
184 }
185
186 void ENGINE_load_dasync(void)
187 {
188     ENGINE *toadd = engine_dasync();
189     if (!toadd)
190         return;
191     ENGINE_add(toadd);
192     ENGINE_free(toadd);
193     ERR_clear_error();
194 }
195
196 static int dasync_init(ENGINE *e)
197 {
198     return 1;
199 }
200
201
202 static int dasync_finish(ENGINE *e)
203 {
204     return 1;
205 }
206
207
208 static int dasync_destroy(ENGINE *e)
209 {
210     ERR_unload_DASYNC_strings();
211     return 1;
212 }
213
214 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
215                           const int **nids, int nid)
216 {
217     int ok = 1;
218     if (!digest) {
219         /* We are returning a list of supported nids */
220         *nids = dasync_digest_nids;
221         return (sizeof(dasync_digest_nids) -
222                 1) / sizeof(dasync_digest_nids[0]);
223     }
224     /* We are being asked for a specific digest */
225     switch (nid) {
226     case NID_sha1:
227         *digest = &dasync_sha1;
228         break;
229     default:
230         ok = 0;
231         *digest = NULL;
232         break;
233     }
234     return ok;
235 }
236
237 static void dummy_pause_job(void) {
238     ASYNC_JOB *job;
239
240     if ((job = ASYNC_get_current_job()) == NULL)
241         return;
242
243     /*
244      * In the Dummy async engine we are cheating. We signal that the job
245      * is complete by waking it before the call to ASYNC_pause_job(). A real
246      * async engine would only wake when the job was actually complete
247      */
248     ASYNC_wake(job);
249
250     /* Ignore errors - we carry on anyway */
251     ASYNC_pause_job();
252
253     ASYNC_clear_wake(job);
254 }
255
256
257 /*
258  * SHA1 implementation. At the moment we just defer to the standard
259  * implementation
260  */
261 #undef data
262 #define data(ctx) ((SHA_CTX *)(ctx)->md_data)
263 static int dasync_sha1_init(EVP_MD_CTX *ctx)
264 {
265     dummy_pause_job();
266
267     return SHA1_Init(data(ctx));
268 }
269
270 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
271                              size_t count)
272 {
273     dummy_pause_job();
274
275     return SHA1_Update(data(ctx), data, (size_t)count);
276 }
277
278 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
279 {
280     dummy_pause_job();
281
282     return SHA1_Final(md, data(ctx));
283 }
284
285 /*
286  * RSA implementation
287  */
288
289 static int dasync_pub_enc(int flen, const unsigned char *from,
290                     unsigned char *to, RSA *rsa, int padding) {
291     /* Ignore errors - we carry on anyway */
292     dummy_pause_job();
293     return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
294 }
295
296 static int dasync_pub_dec(int flen, const unsigned char *from,
297                     unsigned char *to, RSA *rsa, int padding) {
298     /* Ignore errors - we carry on anyway */
299     dummy_pause_job();
300     return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
301 }
302
303 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
304                       unsigned char *to, RSA *rsa, int padding)
305 {
306     /* Ignore errors - we carry on anyway */
307     dummy_pause_job();
308     return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
309 }
310
311 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
312                       unsigned char *to, RSA *rsa, int padding)
313 {
314     /* Ignore errors - we carry on anyway */
315     dummy_pause_job();
316     return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
317 }
318
319 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
320 {
321     /* Ignore errors - we carry on anyway */
322     dummy_pause_job();
323     return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
324 }
325
326 static int dasync_rsa_init(RSA *rsa)
327 {
328     return RSA_PKCS1_OpenSSL()->init(rsa);
329 }
330 static int dasync_rsa_finish(RSA *rsa)
331 {
332     return RSA_PKCS1_OpenSSL()->finish(rsa);
333 }