]> git.ipfire.org Git - thirdparty/openssl.git/blame - engines/e_dasync.c
Copyright consolidation 01/10
[thirdparty/openssl.git] / engines / e_dasync.c
CommitLineData
a14e9ff7
MC
1/*
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>
2f2c9caa 58#include <openssl/aes.h>
a14e9ff7
MC
59#include <openssl/rsa.h>
60#include <openssl/evp.h>
61#include <openssl/async.h>
62#include <openssl/bn.h>
7b9f8f7f 63#include <openssl/crypto.h>
98ee7543
MC
64#include <openssl/ssl.h>
65#include <openssl/modes.h>
a14e9ff7 66
ff75a257
MC
67#if (defined(OPENSSL_SYS_UNIX) || defined(OPENSSL_SYS_CYGWIN)) && defined(OPENSSL_THREADS)
68# undef ASYNC_POSIX
69# define ASYNC_POSIX
70# include <unistd.h>
71#elif defined(_WIN32)
72# undef ASYNC_WIN
73# define ASYNC_WIN
74# include <windows.h>
75#endif
76
a14e9ff7
MC
77#define DASYNC_LIB_NAME "DASYNC"
78#include "e_dasync_err.c"
79
80/* Engine Id and Name */
81static const char *engine_dasync_id = "dasync";
82static const char *engine_dasync_name = "Dummy Async engine support";
83
84
85/* Engine Lifetime functions */
86static int dasync_destroy(ENGINE *e);
87static int dasync_init(ENGINE *e);
88static int dasync_finish(ENGINE *e);
b3599dbb 89void engine_load_dasync_int(void);
a14e9ff7
MC
90
91
92/* Set up digests. Just SHA1 for now */
93static int dasync_digests(ENGINE *e, const EVP_MD **digest,
94 const int **nids, int nid);
95
f4da39d2 96static void dummy_pause_job(void);
a14e9ff7
MC
97
98/* SHA1 */
46a283c0
MC
99static int dasync_sha1_init(EVP_MD_CTX *ctx);
100static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
652d4a8c 101 size_t count);
46a283c0 102static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
a14e9ff7 103
11780ac3
MC
104/*
105 * Holds the EVP_MD object for sha1 in this engine. Set up once only during
106 * engine bind and can then be reused many times.
107 */
cddcea8c
RL
108static EVP_MD *_hidden_sha1_md = NULL;
109static const EVP_MD *dasync_sha1(void)
110{
cddcea8c
RL
111 return _hidden_sha1_md;
112}
113static void destroy_digests(void)
114{
115 EVP_MD_meth_free(_hidden_sha1_md);
116 _hidden_sha1_md = NULL;
117}
11780ac3 118
cddcea8c
RL
119static int dasync_digest_nids(const int **nids)
120{
121 static int digest_nids[2] = { 0, 0 };
122 static int pos = 0;
123 static int init = 0;
124
125 if (!init) {
126 const EVP_MD *md;
127 if ((md = dasync_sha1()) != NULL)
128 digest_nids[pos++] = EVP_MD_type(md);
129 digest_nids[pos] = 0;
130 init = 1;
131 }
132 *nids = digest_nids;
133 return pos;
134}
a14e9ff7
MC
135
136/* RSA */
137
138static int dasync_pub_enc(int flen, const unsigned char *from,
139 unsigned char *to, RSA *rsa, int padding);
140static int dasync_pub_dec(int flen, const unsigned char *from,
141 unsigned char *to, RSA *rsa, int padding);
142static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
143 unsigned char *to, RSA *rsa, int padding);
144static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
145 unsigned char *to, RSA *rsa, int padding);
146static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
147 BN_CTX *ctx);
148
149static int dasync_rsa_init(RSA *rsa);
150static int dasync_rsa_finish(RSA *rsa);
151
b72c9121 152static RSA_METHOD *dasync_rsa_method = NULL;
a14e9ff7 153
98ee7543
MC
154/* AES */
155
156static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
157 void *ptr);
98ee7543
MC
158static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
159 const unsigned char *iv, int enc);
98ee7543
MC
160static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
161 const unsigned char *in, size_t inl);
98ee7543
MC
162static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
163
2f2c9caa
MC
164static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
165 int arg, void *ptr);
166static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
167 const unsigned char *key,
168 const unsigned char *iv,
169 int enc);
170static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
171 unsigned char *out,
172 const unsigned char *in,
173 size_t inl);
174static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
175
e38c2e85 176struct dasync_pipeline_ctx {
98ee7543 177 void *inner_cipher_data;
98ee7543
MC
178 unsigned int numpipes;
179 unsigned char **inbufs;
180 unsigned char **outbufs;
181 size_t *lens;
2f2c9caa
MC
182 int enc;
183 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
184 unsigned int aadctr;
98ee7543
MC
185};
186
11780ac3
MC
187/*
188 * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
189 * during engine bind and can then be reused many times.
190 */
98ee7543
MC
191static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
192static const EVP_CIPHER *dasync_aes_128_cbc(void)
193{
11780ac3
MC
194 return _hidden_aes_128_cbc;
195}
196
197/*
198 * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
199 * once only during engine bind and can then be reused many times.
200 */
201static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
202static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
203{
204 return _hidden_aes_128_cbc_hmac_sha1;
205}
206
207static void destroy_ciphers(void)
208{
209 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
210 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
211 _hidden_aes_128_cbc = NULL;
212 _hidden_aes_128_cbc_hmac_sha1 = NULL;
213}
214
215static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
216 const int **nids, int nid);
217
218static int dasync_cipher_nids[] = {
219 NID_aes_128_cbc,
220 NID_aes_128_cbc_hmac_sha1,
221 0
222};
223
224static int bind_dasync(ENGINE *e)
225{
b72c9121
RL
226 /* Setup RSA_METHOD */
227 if ((dasync_rsa_method = RSA_meth_new("Dummy Async RSA method", 0)) == NULL
228 || RSA_meth_set_pub_enc(dasync_rsa_method, dasync_pub_enc) == 0
229 || RSA_meth_set_pub_dec(dasync_rsa_method, dasync_pub_dec) == 0
230 || RSA_meth_set_priv_enc(dasync_rsa_method, dasync_rsa_priv_enc) == 0
8aac5d2e 231 || RSA_meth_set_priv_dec(dasync_rsa_method, dasync_rsa_priv_dec) == 0
b72c9121
RL
232 || RSA_meth_set_mod_exp(dasync_rsa_method, dasync_rsa_mod_exp) == 0
233 || RSA_meth_set_bn_mod_exp(dasync_rsa_method, BN_mod_exp_mont) == 0
234 || RSA_meth_set_init(dasync_rsa_method, dasync_rsa_init) == 0
235 || RSA_meth_set_finish(dasync_rsa_method, dasync_rsa_finish) == 0) {
236 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
237 return 0;
238 }
239
11780ac3
MC
240 /* Ensure the dasync error handling is set up */
241 ERR_load_DASYNC_strings();
242
243 if (!ENGINE_set_id(e, engine_dasync_id)
244 || !ENGINE_set_name(e, engine_dasync_name)
b72c9121 245 || !ENGINE_set_RSA(e, dasync_rsa_method)
11780ac3
MC
246 || !ENGINE_set_digests(e, dasync_digests)
247 || !ENGINE_set_ciphers(e, dasync_ciphers)
248 || !ENGINE_set_destroy_function(e, dasync_destroy)
249 || !ENGINE_set_init_function(e, dasync_init)
250 || !ENGINE_set_finish_function(e, dasync_finish)) {
251 DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
252 return 0;
253 }
254
255 /*
256 * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
257 * supplied by this engine
258 */
259 _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
260 if (_hidden_sha1_md == NULL
261 || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
262 || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
263 || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
264 sizeof(EVP_MD *) + sizeof(SHA_CTX))
265 || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
266 || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
267 || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
268 || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
269 EVP_MD_meth_free(_hidden_sha1_md);
270 _hidden_sha1_md = NULL;
271 }
272
273 _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
274 16 /* block size */,
275 16 /* key len */);
98ee7543
MC
276 if (_hidden_aes_128_cbc == NULL
277 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
278 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
279 EVP_CIPH_FLAG_DEFAULT_ASN1
280 | EVP_CIPH_CBC_MODE
281 | EVP_CIPH_FLAG_PIPELINE)
282 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
283 dasync_aes128_init_key)
284 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
285 dasync_aes128_cbc_cipher)
286 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
287 dasync_aes128_cbc_cleanup)
288 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
289 dasync_aes128_cbc_ctrl)
290 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
e38c2e85 291 sizeof(struct dasync_pipeline_ctx))) {
98ee7543
MC
292 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
293 _hidden_aes_128_cbc = NULL;
294 }
98ee7543 295
11780ac3
MC
296 _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
297 NID_aes_128_cbc_hmac_sha1,
298 16 /* block size */,
299 16 /* key len */);
2f2c9caa
MC
300 if (_hidden_aes_128_cbc_hmac_sha1 == NULL
301 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1,16)
302 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
303 EVP_CIPH_CBC_MODE
304 | EVP_CIPH_FLAG_DEFAULT_ASN1
305 | EVP_CIPH_FLAG_AEAD_CIPHER
306 | EVP_CIPH_FLAG_PIPELINE)
307 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
308 dasync_aes128_cbc_hmac_sha1_init_key)
309 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
310 dasync_aes128_cbc_hmac_sha1_cipher)
311 || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
312 dasync_aes128_cbc_hmac_sha1_cleanup)
313 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
314 dasync_aes128_cbc_hmac_sha1_ctrl)
315 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
e38c2e85 316 sizeof(struct dasync_pipeline_ctx))) {
2f2c9caa
MC
317 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
318 _hidden_aes_128_cbc_hmac_sha1 = NULL;
319 }
a14e9ff7
MC
320
321 return 1;
322}
323
324# ifndef OPENSSL_NO_DYNAMIC_ENGINE
325static int bind_helper(ENGINE *e, const char *id)
326{
327 if (id && (strcmp(id, engine_dasync_id) != 0))
328 return 0;
329 if (!bind_dasync(e))
330 return 0;
331 return 1;
332}
333
334IMPLEMENT_DYNAMIC_CHECK_FN()
335 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
336# endif
337
338static ENGINE *engine_dasync(void)
339{
340 ENGINE *ret = ENGINE_new();
341 if (!ret)
342 return NULL;
343 if (!bind_dasync(ret)) {
344 ENGINE_free(ret);
345 return NULL;
346 }
347 return ret;
348}
349
b3599dbb 350void engine_load_dasync_int(void)
a14e9ff7
MC
351{
352 ENGINE *toadd = engine_dasync();
353 if (!toadd)
354 return;
355 ENGINE_add(toadd);
356 ENGINE_free(toadd);
357 ERR_clear_error();
358}
359
360static int dasync_init(ENGINE *e)
361{
362 return 1;
363}
364
365
366static int dasync_finish(ENGINE *e)
367{
368 return 1;
369}
370
371
372static int dasync_destroy(ENGINE *e)
373{
cddcea8c 374 destroy_digests();
11780ac3 375 destroy_ciphers();
b72c9121 376 RSA_meth_free(dasync_rsa_method);
a14e9ff7
MC
377 ERR_unload_DASYNC_strings();
378 return 1;
379}
380
381static int dasync_digests(ENGINE *e, const EVP_MD **digest,
382 const int **nids, int nid)
383{
384 int ok = 1;
385 if (!digest) {
386 /* We are returning a list of supported nids */
cddcea8c 387 return dasync_digest_nids(nids);
a14e9ff7
MC
388 }
389 /* We are being asked for a specific digest */
390 switch (nid) {
391 case NID_sha1:
cddcea8c 392 *digest = dasync_sha1();
a14e9ff7
MC
393 break;
394 default:
395 ok = 0;
396 *digest = NULL;
397 break;
398 }
399 return ok;
400}
401
98ee7543
MC
402static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
403 const int **nids, int nid)
404{
405 int ok = 1;
11780ac3 406 if (cipher == NULL) {
98ee7543
MC
407 /* We are returning a list of supported nids */
408 *nids = dasync_cipher_nids;
409 return (sizeof(dasync_cipher_nids) -
410 1) / sizeof(dasync_cipher_nids[0]);
411 }
412 /* We are being asked for a specific cipher */
413 switch (nid) {
414 case NID_aes_128_cbc:
415 *cipher = dasync_aes_128_cbc();
416 break;
2f2c9caa
MC
417 case NID_aes_128_cbc_hmac_sha1:
418 *cipher = dasync_aes_128_cbc_hmac_sha1();
419 break;
98ee7543
MC
420 default:
421 ok = 0;
422 *cipher = NULL;
423 break;
424 }
425 return ok;
426}
427
ff75a257
MC
428static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
429 OSSL_ASYNC_FD readfd, void *pvwritefd)
430{
431 OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
432#if defined(ASYNC_WIN)
433 CloseHandle(readfd);
434 CloseHandle(*pwritefd);
435#elif defined(ASYNC_POSIX)
436 close(readfd);
437 close(*pwritefd);
438#endif
439 OPENSSL_free(pwritefd);
440}
441
442#define DUMMY_CHAR 'X'
443
f4da39d2
MC
444static void dummy_pause_job(void) {
445 ASYNC_JOB *job;
ff75a257
MC
446 ASYNC_WAIT_CTX *waitctx;
447 OSSL_ASYNC_FD pipefds[2] = {0, 0};
448 OSSL_ASYNC_FD *writefd;
449#if defined(ASYNC_WIN)
450 DWORD numwritten, numread;
451 char buf = DUMMY_CHAR;
452#elif defined(ASYNC_POSIX)
453 char buf = DUMMY_CHAR;
454#endif
f4da39d2
MC
455
456 if ((job = ASYNC_get_current_job()) == NULL)
457 return;
458
ff75a257
MC
459 waitctx = ASYNC_get_wait_ctx(job);
460
461 if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
462 (void **)&writefd)) {
463 pipefds[1] = *writefd;
464 } else {
465 writefd = OPENSSL_malloc(sizeof(*writefd));
466 if (writefd == NULL)
467 return;
468#if defined(ASYNC_WIN)
469 if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
470 OPENSSL_free(writefd);
471 return;
472 }
473#elif defined(ASYNC_POSIX)
474 if (pipe(pipefds) != 0) {
475 OPENSSL_free(writefd);
476 return;
477 }
478#endif
479 *writefd = pipefds[1];
480
481 if(!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
482 writefd, wait_cleanup)) {
483 wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
484 return;
485 }
486 }
f4da39d2
MC
487 /*
488 * In the Dummy async engine we are cheating. We signal that the job
489 * is complete by waking it before the call to ASYNC_pause_job(). A real
490 * async engine would only wake when the job was actually complete
491 */
ff75a257
MC
492#if defined(ASYNC_WIN)
493 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
494#elif defined(ASYNC_POSIX)
b8972eda
AG
495 if (write(pipefds[1], &buf, 1) < 0)
496 return;
ff75a257 497#endif
f4da39d2
MC
498
499 /* Ignore errors - we carry on anyway */
500 ASYNC_pause_job();
501
ff75a257
MC
502 /* Clear the wake signal */
503#if defined(ASYNC_WIN)
504 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
505#elif defined(ASYNC_POSIX)
b8972eda
AG
506 if (read(pipefds[0], &buf, 1) < 0)
507 return;
ff75a257 508#endif
f4da39d2
MC
509}
510
a14e9ff7
MC
511/*
512 * SHA1 implementation. At the moment we just defer to the standard
513 * implementation
514 */
515#undef data
6e59a892 516#define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
46a283c0 517static int dasync_sha1_init(EVP_MD_CTX *ctx)
a14e9ff7 518{
f4da39d2 519 dummy_pause_job();
a14e9ff7
MC
520
521 return SHA1_Init(data(ctx));
522}
523
46a283c0 524static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
652d4a8c 525 size_t count)
a14e9ff7 526{
f4da39d2 527 dummy_pause_job();
a14e9ff7
MC
528
529 return SHA1_Update(data(ctx), data, (size_t)count);
530}
531
46a283c0 532static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
a14e9ff7 533{
f4da39d2 534 dummy_pause_job();
a14e9ff7
MC
535
536 return SHA1_Final(md, data(ctx));
537}
538
539/*
540 * RSA implementation
541 */
542
543static int dasync_pub_enc(int flen, const unsigned char *from,
544 unsigned char *to, RSA *rsa, int padding) {
545 /* Ignore errors - we carry on anyway */
f4da39d2 546 dummy_pause_job();
b72c9121
RL
547 return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
548 (flen, from, to, rsa, padding);
a14e9ff7
MC
549}
550
551static int dasync_pub_dec(int flen, const unsigned char *from,
552 unsigned char *to, RSA *rsa, int padding) {
553 /* Ignore errors - we carry on anyway */
f4da39d2 554 dummy_pause_job();
b72c9121
RL
555 return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
556 (flen, from, to, rsa, padding);
a14e9ff7
MC
557}
558
559static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
560 unsigned char *to, RSA *rsa, int padding)
561{
562 /* Ignore errors - we carry on anyway */
f4da39d2 563 dummy_pause_job();
b72c9121
RL
564 return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
565 (flen, from, to, rsa, padding);
a14e9ff7
MC
566}
567
568static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
569 unsigned char *to, RSA *rsa, int padding)
570{
571 /* Ignore errors - we carry on anyway */
f4da39d2 572 dummy_pause_job();
b72c9121
RL
573 return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
574 (flen, from, to, rsa, padding);
a14e9ff7
MC
575}
576
577static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
578{
579 /* Ignore errors - we carry on anyway */
f4da39d2 580 dummy_pause_job();
b72c9121 581 return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
a14e9ff7
MC
582}
583
584static int dasync_rsa_init(RSA *rsa)
585{
b72c9121 586 return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
a14e9ff7
MC
587}
588static int dasync_rsa_finish(RSA *rsa)
589{
b72c9121 590 return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
a14e9ff7 591}
98ee7543 592
e38c2e85 593/* Cipher helper functions */
98ee7543 594
e38c2e85
MC
595static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
596 void *ptr, int aeadcapable)
98ee7543
MC
597{
598 int ret;
e38c2e85
MC
599 struct dasync_pipeline_ctx *pipe_ctx =
600 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
2f2c9caa
MC
601
602 if (pipe_ctx == NULL)
603 return 0;
604
605 switch (type) {
606 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
607 pipe_ctx->numpipes = arg;
608 pipe_ctx->outbufs = (unsigned char **)ptr;
609 break;
610
611 case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
612 pipe_ctx->numpipes = arg;
613 pipe_ctx->inbufs = (unsigned char **)ptr;
614 break;
615
616 case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
617 pipe_ctx->numpipes = arg;
618 pipe_ctx->lens = (size_t *)ptr;
619 break;
620
621 case EVP_CTRL_AEAD_SET_MAC_KEY:
e38c2e85
MC
622 if (!aeadcapable)
623 return -1;
2f2c9caa
MC
624 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
625 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())
626 (ctx, type, arg, ptr);
627 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
628 return ret;
629
630 case EVP_CTRL_AEAD_TLS1_AAD:
631 {
632 unsigned char *p = ptr;
633 unsigned int len;
634
e38c2e85 635 if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
2f2c9caa
MC
636 return -1;
637
638 if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
639 return -1;
640
641 memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
642 EVP_AEAD_TLS1_AAD_LEN);
643 pipe_ctx->aadctr++;
644
645 len = p[arg - 2] << 8 | p[arg - 1];
646
647 if (pipe_ctx->enc) {
648 if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
649 len -= AES_BLOCK_SIZE;
650 }
651
652 return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
653 & -AES_BLOCK_SIZE) - len;
654 } else {
655 return SHA_DIGEST_LENGTH;
656 }
657 }
658
2f2c9caa
MC
659 default:
660 return 0;
661 }
662
663 return 1;
664}
665
e38c2e85
MC
666static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
667 const unsigned char *key,
668 const unsigned char *iv, int enc,
669 const EVP_CIPHER *cipher)
2f2c9caa
MC
670{
671 int ret;
e38c2e85
MC
672 struct dasync_pipeline_ctx *pipe_ctx =
673 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
2f2c9caa
MC
674
675 if (pipe_ctx->inner_cipher_data == NULL
e38c2e85
MC
676 && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
677 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
678 EVP_CIPHER_impl_ctx_size(cipher));
2f2c9caa 679 if (pipe_ctx->inner_cipher_data == NULL) {
2f781956 680 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
2f2c9caa
MC
681 ERR_R_MALLOC_FAILURE);
682 return 0;
683 }
684 }
685
686 pipe_ctx->numpipes = 0;
e38c2e85 687 pipe_ctx->aadctr = 0;
2f2c9caa
MC
688
689 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
e38c2e85 690 ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
2f2c9caa
MC
691 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
692
693 return ret;
694}
695
e38c2e85
MC
696static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
697 const unsigned char *in, size_t inl,
698 const EVP_CIPHER *cipher)
2f2c9caa
MC
699{
700 int ret = 1;
701 unsigned int i, pipes;
e38c2e85
MC
702 struct dasync_pipeline_ctx *pipe_ctx =
703 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
2f2c9caa
MC
704
705 pipes = pipe_ctx->numpipes;
706 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
707 if (pipes == 0) {
708 if (pipe_ctx->aadctr != 0) {
709 if (pipe_ctx->aadctr != 1)
710 return -1;
e38c2e85 711 EVP_CIPHER_meth_get_ctrl(cipher)
2f2c9caa
MC
712 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
713 EVP_AEAD_TLS1_AAD_LEN,
714 pipe_ctx->tlsaad[0]);
715 }
e38c2e85 716 ret = EVP_CIPHER_meth_get_do_cipher(cipher)
2f2c9caa
MC
717 (ctx, out, in, inl);
718 } else {
719 if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
720 return -1;
721 for (i = 0; i < pipes; i++) {
722 if (pipe_ctx->aadctr > 0) {
e38c2e85 723 EVP_CIPHER_meth_get_ctrl(cipher)
2f2c9caa
MC
724 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
725 EVP_AEAD_TLS1_AAD_LEN,
726 pipe_ctx->tlsaad[i]);
727 }
e38c2e85 728 ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
2f2c9caa
MC
729 (ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i],
730 pipe_ctx->lens[i]);
731 }
732 pipe_ctx->numpipes = 0;
733 }
734 pipe_ctx->aadctr = 0;
735 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
736 return ret;
737}
738
e38c2e85
MC
739static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
740 const EVP_CIPHER *cipher)
2f2c9caa 741{
e38c2e85
MC
742 struct dasync_pipeline_ctx *pipe_ctx =
743 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
2f2c9caa
MC
744
745 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
e38c2e85 746 EVP_CIPHER_impl_ctx_size(cipher));
2f2c9caa
MC
747
748 return 1;
749}
e38c2e85
MC
750
751/*
752 * AES128 CBC Implementation
753 */
754
755static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
756 void *ptr)
757{
758 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0);
759}
760
761static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
762 const unsigned char *iv, int enc)
763{
764 return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
765}
766
767static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
768 const unsigned char *in, size_t inl)
769{
770 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
771}
772
773static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
774{
775 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
776}
777
778
779/*
780 * AES128 CBC HMAC SHA1 Implementation
781 */
782
783static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
784 int arg, void *ptr)
785{
786 return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1);
787}
788
789static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
790 const unsigned char *key,
791 const unsigned char *iv,
792 int enc)
793{
794 return dasync_cipher_init_key_helper(ctx, key, iv, enc,
795 EVP_aes_128_cbc_hmac_sha1());
796}
797
798static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
799 unsigned char *out,
800 const unsigned char *in,
801 size_t inl)
802{
803 return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
804}
805
806static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
807{
808 return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
809}