blob: 2f18d07db86b4802765a42614c2fc20abb95cb9d [file] [log] [blame]
Matt Caswella14e9ff2015-02-13 23:25:33 +00001/*
2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
3 */
4/* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 */
52
53#include <stdio.h>
54#include <string.h>
55
56#include <openssl/engine.h>
57#include <openssl/sha.h>
58#include <openssl/rsa.h>
59#include <openssl/evp.h>
60#include <openssl/async.h>
61#include <openssl/bn.h>
62
63#define DASYNC_LIB_NAME "DASYNC"
64#include "e_dasync_err.c"
65
66/* Engine Id and Name */
67static const char *engine_dasync_id = "dasync";
68static const char *engine_dasync_name = "Dummy Async engine support";
69
70
71/* Engine Lifetime functions */
72static int dasync_destroy(ENGINE *e);
73static int dasync_init(ENGINE *e);
74static int dasync_finish(ENGINE *e);
75void ENGINE_load_dasync(void);
76
77
78/* Set up digests. Just SHA1 for now */
79static int dasync_digests(ENGINE *e, const EVP_MD **digest,
80 const int **nids, int nid);
81
Matt Caswellf4da39d2015-07-24 08:15:31 +010082static void dummy_pause_job(void);
Matt Caswella14e9ff2015-02-13 23:25:33 +000083
84/* SHA1 */
Matt Caswell46a283c2015-10-09 16:45:25 +010085static int dasync_sha1_init(EVP_MD_CTX *ctx);
86static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
Kurt Roeckx652d4a82015-11-21 17:58:12 +010087 size_t count);
Matt Caswell46a283c2015-10-09 16:45:25 +010088static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
Matt Caswella14e9ff2015-02-13 23:25:33 +000089
Richard Levittecddcea82015-11-30 10:24:12 +010090static EVP_MD *_hidden_sha1_md = NULL;
91static const EVP_MD *dasync_sha1(void)
92{
93 if (_hidden_sha1_md == NULL) {
94 EVP_MD *md;
95
96 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
97 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
98 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
99 || !EVP_MD_meth_set_app_datasize(md,
100 sizeof(EVP_MD *) + sizeof(SHA_CTX))
101 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
102 || !EVP_MD_meth_set_init(md, dasync_sha1_init)
103 || !EVP_MD_meth_set_update(md, dasync_sha1_update)
104 || !EVP_MD_meth_set_final(md, dasync_sha1_final)) {
105 EVP_MD_meth_free(md);
106 md = NULL;
107 }
108 _hidden_sha1_md = md;
109 }
110 return _hidden_sha1_md;
111}
112static void destroy_digests(void)
113{
114 EVP_MD_meth_free(_hidden_sha1_md);
115 _hidden_sha1_md = NULL;
116}
117static int dasync_digest_nids(const int **nids)
118{
119 static int digest_nids[2] = { 0, 0 };
120 static int pos = 0;
121 static int init = 0;
122
123 if (!init) {
124 const EVP_MD *md;
125 if ((md = dasync_sha1()) != NULL)
126 digest_nids[pos++] = EVP_MD_type(md);
127 digest_nids[pos] = 0;
128 init = 1;
129 }
130 *nids = digest_nids;
131 return pos;
132}
Matt Caswella14e9ff2015-02-13 23:25:33 +0000133
134/* RSA */
135
136static int dasync_pub_enc(int flen, const unsigned char *from,
137 unsigned char *to, RSA *rsa, int padding);
138static int dasync_pub_dec(int flen, const unsigned char *from,
139 unsigned char *to, RSA *rsa, int padding);
140static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
141 unsigned char *to, RSA *rsa, int padding);
142static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
143 unsigned char *to, RSA *rsa, int padding);
144static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
145 BN_CTX *ctx);
146
147static int dasync_rsa_init(RSA *rsa);
148static int dasync_rsa_finish(RSA *rsa);
149
150static RSA_METHOD dasync_rsa_method = {
151 "Dummy Async RSA method",
152 dasync_pub_enc, /* pub_enc */
153 dasync_pub_dec, /* pub_dec */
154 dasync_rsa_priv_enc, /* priv_enc */
155 dasync_rsa_priv_dec, /* priv_dec */
156 dasync_rsa_mod_exp, /* rsa_mod_exp */
157 BN_mod_exp_mont, /* bn_mod_exp */
158 dasync_rsa_init, /* init */
159 dasync_rsa_finish, /* finish */
160 0, /* flags */
161 NULL, /* app_data */
162 0, /* rsa_sign */
163 0, /* rsa_verify */
164 NULL /* rsa_keygen */
165};
166
167
168static int bind_dasync(ENGINE *e)
169{
170 /* Ensure the dasync error handling is set up */
171 ERR_load_DASYNC_strings();
172
173 if (!ENGINE_set_id(e, engine_dasync_id)
174 || !ENGINE_set_name(e, engine_dasync_name)
175 || !ENGINE_set_RSA(e, &dasync_rsa_method)
176 || !ENGINE_set_digests(e, dasync_digests)
177 || !ENGINE_set_destroy_function(e, dasync_destroy)
178 || !ENGINE_set_init_function(e, dasync_init)
179 || !ENGINE_set_finish_function(e, dasync_finish)) {
180 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
181 return 0;
182 }
183
184 return 1;
185}
186
187# ifndef OPENSSL_NO_DYNAMIC_ENGINE
188static int bind_helper(ENGINE *e, const char *id)
189{
190 if (id && (strcmp(id, engine_dasync_id) != 0))
191 return 0;
192 if (!bind_dasync(e))
193 return 0;
194 return 1;
195}
196
197IMPLEMENT_DYNAMIC_CHECK_FN()
198 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
199# endif
200
201static ENGINE *engine_dasync(void)
202{
203 ENGINE *ret = ENGINE_new();
204 if (!ret)
205 return NULL;
206 if (!bind_dasync(ret)) {
207 ENGINE_free(ret);
208 return NULL;
209 }
210 return ret;
211}
212
213void ENGINE_load_dasync(void)
214{
215 ENGINE *toadd = engine_dasync();
216 if (!toadd)
217 return;
218 ENGINE_add(toadd);
219 ENGINE_free(toadd);
220 ERR_clear_error();
221}
222
223static int dasync_init(ENGINE *e)
224{
225 return 1;
226}
227
228
229static int dasync_finish(ENGINE *e)
230{
231 return 1;
232}
233
234
235static int dasync_destroy(ENGINE *e)
236{
Richard Levittecddcea82015-11-30 10:24:12 +0100237 destroy_digests();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000238 ERR_unload_DASYNC_strings();
239 return 1;
240}
241
242static int dasync_digests(ENGINE *e, const EVP_MD **digest,
243 const int **nids, int nid)
244{
245 int ok = 1;
246 if (!digest) {
247 /* We are returning a list of supported nids */
Richard Levittecddcea82015-11-30 10:24:12 +0100248 return dasync_digest_nids(nids);
Matt Caswella14e9ff2015-02-13 23:25:33 +0000249 }
250 /* We are being asked for a specific digest */
251 switch (nid) {
252 case NID_sha1:
Richard Levittecddcea82015-11-30 10:24:12 +0100253 *digest = dasync_sha1();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000254 break;
255 default:
256 ok = 0;
257 *digest = NULL;
258 break;
259 }
260 return ok;
261}
262
Matt Caswellf4da39d2015-07-24 08:15:31 +0100263static void dummy_pause_job(void) {
264 ASYNC_JOB *job;
265
266 if ((job = ASYNC_get_current_job()) == NULL)
267 return;
268
269 /*
270 * In the Dummy async engine we are cheating. We signal that the job
271 * is complete by waking it before the call to ASYNC_pause_job(). A real
272 * async engine would only wake when the job was actually complete
273 */
274 ASYNC_wake(job);
275
276 /* Ignore errors - we carry on anyway */
277 ASYNC_pause_job();
278
279 ASYNC_clear_wake(job);
280}
281
Matt Caswella14e9ff2015-02-13 23:25:33 +0000282
283/*
284 * SHA1 implementation. At the moment we just defer to the standard
285 * implementation
286 */
287#undef data
Richard Levitte6e59a892015-11-27 14:02:12 +0100288#define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
Matt Caswell46a283c2015-10-09 16:45:25 +0100289static int dasync_sha1_init(EVP_MD_CTX *ctx)
Matt Caswella14e9ff2015-02-13 23:25:33 +0000290{
Matt Caswellf4da39d2015-07-24 08:15:31 +0100291 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000292
293 return SHA1_Init(data(ctx));
294}
295
Matt Caswell46a283c2015-10-09 16:45:25 +0100296static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
Kurt Roeckx652d4a82015-11-21 17:58:12 +0100297 size_t count)
Matt Caswella14e9ff2015-02-13 23:25:33 +0000298{
Matt Caswellf4da39d2015-07-24 08:15:31 +0100299 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000300
301 return SHA1_Update(data(ctx), data, (size_t)count);
302}
303
Matt Caswell46a283c2015-10-09 16:45:25 +0100304static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
Matt Caswella14e9ff2015-02-13 23:25:33 +0000305{
Matt Caswellf4da39d2015-07-24 08:15:31 +0100306 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000307
308 return SHA1_Final(md, data(ctx));
309}
310
311/*
312 * RSA implementation
313 */
314
315static int dasync_pub_enc(int flen, const unsigned char *from,
316 unsigned char *to, RSA *rsa, int padding) {
317 /* Ignore errors - we carry on anyway */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100318 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000319 return RSA_PKCS1_OpenSSL()->rsa_pub_enc(flen, from, to, rsa, padding);
320}
321
322static int dasync_pub_dec(int flen, const unsigned char *from,
323 unsigned char *to, RSA *rsa, int padding) {
324 /* Ignore errors - we carry on anyway */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100325 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000326 return RSA_PKCS1_OpenSSL()->rsa_pub_dec(flen, from, to, rsa, padding);
327}
328
329static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
330 unsigned char *to, RSA *rsa, int padding)
331{
332 /* Ignore errors - we carry on anyway */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100333 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000334 return RSA_PKCS1_OpenSSL()->rsa_priv_enc(flen, from, to, rsa, padding);
335}
336
337static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
338 unsigned char *to, RSA *rsa, int padding)
339{
340 /* Ignore errors - we carry on anyway */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100341 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000342 return RSA_PKCS1_OpenSSL()->rsa_priv_dec(flen, from, to, rsa, padding);
343}
344
345static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
346{
347 /* Ignore errors - we carry on anyway */
Matt Caswellf4da39d2015-07-24 08:15:31 +0100348 dummy_pause_job();
Matt Caswella14e9ff2015-02-13 23:25:33 +0000349 return RSA_PKCS1_OpenSSL()->rsa_mod_exp(r0, I, rsa, ctx);
350}
351
352static int dasync_rsa_init(RSA *rsa)
353{
354 return RSA_PKCS1_OpenSSL()->init(rsa);
355}
356static int dasync_rsa_finish(RSA *rsa)
357{
358 return RSA_PKCS1_OpenSSL()->finish(rsa);
359}