]> git.ipfire.org Git - thirdparty/openssl.git/blob - engines/e_dasync.c
2ee7d6363d073cd71c7536788d54536dd86cf415
[thirdparty/openssl.git] / engines / e_dasync.c
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>
58 #include <openssl/aes.h>
59 #include <openssl/rsa.h>
60 #include <openssl/evp.h>
61 #include <openssl/async.h>
62 #include <openssl/bn.h>
63 #include <openssl/crypto.h>
64 #include <openssl/ssl.h>
65 #include <openssl/modes.h>
66
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
77 #define DASYNC_LIB_NAME "DASYNC"
78 #include "e_dasync_err.c"
79
80 /* Engine Id and Name */
81 static const char *engine_dasync_id = "dasync";
82 static const char *engine_dasync_name = "Dummy Async engine support";
83
84
85 /* Engine Lifetime functions */
86 static int dasync_destroy(ENGINE *e);
87 static int dasync_init(ENGINE *e);
88 static int dasync_finish(ENGINE *e);
89 void engine_load_dasync_internal(void);
90
91
92 /* Set up digests. Just SHA1 for now */
93 static int dasync_digests(ENGINE *e, const EVP_MD **digest,
94 const int **nids, int nid);
95
96 static void dummy_pause_job(void);
97
98 /* SHA1 */
99 static int dasync_sha1_init(EVP_MD_CTX *ctx);
100 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
101 size_t count);
102 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
103
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 */
108 static EVP_MD *_hidden_sha1_md = NULL;
109 static const EVP_MD *dasync_sha1(void)
110 {
111 return _hidden_sha1_md;
112 }
113 static void destroy_digests(void)
114 {
115 EVP_MD_meth_free(_hidden_sha1_md);
116 _hidden_sha1_md = NULL;
117 }
118
119 static 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 }
135
136 /* RSA */
137
138 static int dasync_pub_enc(int flen, const unsigned char *from,
139 unsigned char *to, RSA *rsa, int padding);
140 static int dasync_pub_dec(int flen, const unsigned char *from,
141 unsigned char *to, RSA *rsa, int padding);
142 static int dasync_rsa_priv_enc(int flen, const unsigned char *from,
143 unsigned char *to, RSA *rsa, int padding);
144 static int dasync_rsa_priv_dec(int flen, const unsigned char *from,
145 unsigned char *to, RSA *rsa, int padding);
146 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
147 BN_CTX *ctx);
148
149 static int dasync_rsa_init(RSA *rsa);
150 static int dasync_rsa_finish(RSA *rsa);
151
152 static RSA_METHOD *dasync_rsa_method = NULL;
153
154 /* AES */
155
156 static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
157 void *ptr);
158 static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
159 const unsigned char *iv, int enc);
160 static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
161 const unsigned char *in, size_t inl);
162 static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
163
164 static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
165 int arg, void *ptr);
166 static 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);
170 static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
171 unsigned char *out,
172 const unsigned char *in,
173 size_t inl);
174 static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
175
176 struct dasync_pipeline_ctx {
177 void *inner_cipher_data;
178 unsigned int numpipes;
179 unsigned char **inbufs;
180 unsigned char **outbufs;
181 size_t *lens;
182 int enc;
183 unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
184 unsigned int aadctr;
185 };
186
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 */
191 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
192 static const EVP_CIPHER *dasync_aes_128_cbc(void)
193 {
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 */
201 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
202 static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
203 {
204 return _hidden_aes_128_cbc_hmac_sha1;
205 }
206
207 static 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
215 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
216 const int **nids, int nid);
217
218 static int dasync_cipher_nids[] = {
219 NID_aes_128_cbc,
220 NID_aes_128_cbc_hmac_sha1,
221 0
222 };
223
224 static int bind_dasync(ENGINE *e)
225 {
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
231 || RSA_meth_set_priv_enc(dasync_rsa_method, dasync_rsa_priv_dec) == 0
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
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)
245 || !ENGINE_set_RSA(e, dasync_rsa_method)
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 */);
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,
291 sizeof(struct dasync_pipeline_ctx))) {
292 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
293 _hidden_aes_128_cbc = NULL;
294 }
295
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 */);
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,
316 sizeof(struct dasync_pipeline_ctx))) {
317 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
318 _hidden_aes_128_cbc_hmac_sha1 = NULL;
319 }
320
321 return 1;
322 }
323
324 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
325 static 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
334 IMPLEMENT_DYNAMIC_CHECK_FN()
335 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
336 # endif
337
338 static 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
350 void engine_load_dasync_internal(void)
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
360 static int dasync_init(ENGINE *e)
361 {
362 return 1;
363 }
364
365
366 static int dasync_finish(ENGINE *e)
367 {
368 return 1;
369 }
370
371
372 static int dasync_destroy(ENGINE *e)
373 {
374 destroy_digests();
375 destroy_ciphers();
376 RSA_meth_free(dasync_rsa_method);
377 ERR_unload_DASYNC_strings();
378 return 1;
379 }
380
381 static 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 */
387 return dasync_digest_nids(nids);
388 }
389 /* We are being asked for a specific digest */
390 switch (nid) {
391 case NID_sha1:
392 *digest = dasync_sha1();
393 break;
394 default:
395 ok = 0;
396 *digest = NULL;
397 break;
398 }
399 return ok;
400 }
401
402 static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
403 const int **nids, int nid)
404 {
405 int ok = 1;
406 if (cipher == NULL) {
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;
417 case NID_aes_128_cbc_hmac_sha1:
418 *cipher = dasync_aes_128_cbc_hmac_sha1();
419 break;
420 default:
421 ok = 0;
422 *cipher = NULL;
423 break;
424 }
425 return ok;
426 }
427
428 static 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
444 static void dummy_pause_job(void) {
445 ASYNC_JOB *job;
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
455
456 if ((job = ASYNC_get_current_job()) == NULL)
457 return;
458
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 }
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 */
492 #if defined(ASYNC_WIN)
493 WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
494 #elif defined(ASYNC_POSIX)
495 if (write(pipefds[1], &buf, 1) < 0)
496 return;
497 #endif
498
499 /* Ignore errors - we carry on anyway */
500 ASYNC_pause_job();
501
502 /* Clear the wake signal */
503 #if defined(ASYNC_WIN)
504 ReadFile(pipefds[0], &buf, 1, &numread, NULL);
505 #elif defined(ASYNC_POSIX)
506 if (read(pipefds[0], &buf, 1) < 0)
507 return;
508 #endif
509 }
510
511 /*
512 * SHA1 implementation. At the moment we just defer to the standard
513 * implementation
514 */
515 #undef data
516 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
517 static int dasync_sha1_init(EVP_MD_CTX *ctx)
518 {
519 dummy_pause_job();
520
521 return SHA1_Init(data(ctx));
522 }
523
524 static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
525 size_t count)
526 {
527 dummy_pause_job();
528
529 return SHA1_Update(data(ctx), data, (size_t)count);
530 }
531
532 static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
533 {
534 dummy_pause_job();
535
536 return SHA1_Final(md, data(ctx));
537 }
538
539 /*
540 * RSA implementation
541 */
542
543 static 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 */
546 dummy_pause_job();
547 return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
548 (flen, from, to, rsa, padding);
549 }
550
551 static 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 */
554 dummy_pause_job();
555 return RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
556 (flen, from, to, rsa, padding);
557 }
558
559 static 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 */
563 dummy_pause_job();
564 return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
565 (flen, from, to, rsa, padding);
566 }
567
568 static 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 */
572 dummy_pause_job();
573 return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
574 (flen, from, to, rsa, padding);
575 }
576
577 static int dasync_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
578 {
579 /* Ignore errors - we carry on anyway */
580 dummy_pause_job();
581 return RSA_meth_get_mod_exp(RSA_PKCS1_OpenSSL())(r0, I, rsa, ctx);
582 }
583
584 static int dasync_rsa_init(RSA *rsa)
585 {
586 return RSA_meth_get_init(RSA_PKCS1_OpenSSL())(rsa);
587 }
588 static int dasync_rsa_finish(RSA *rsa)
589 {
590 return RSA_meth_get_finish(RSA_PKCS1_OpenSSL())(rsa);
591 }
592
593 /* Cipher helper functions */
594
595 static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
596 void *ptr, int aeadcapable)
597 {
598 int ret;
599 struct dasync_pipeline_ctx *pipe_ctx =
600 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
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:
622 if (!aeadcapable)
623 return -1;
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
635 if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
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
659 default:
660 return 0;
661 }
662
663 return 1;
664 }
665
666 static 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)
670 {
671 int ret;
672 struct dasync_pipeline_ctx *pipe_ctx =
673 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
674
675 if (pipe_ctx->inner_cipher_data == NULL
676 && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
677 pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
678 EVP_CIPHER_impl_ctx_size(cipher));
679 if (pipe_ctx->inner_cipher_data == NULL) {
680 DASYNCerr(DASYNC_F_DASYNC_CIPHER_INIT_KEY_HELPER,
681 ERR_R_MALLOC_FAILURE);
682 return 0;
683 }
684 }
685
686 pipe_ctx->numpipes = 0;
687 pipe_ctx->aadctr = 0;
688
689 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
690 ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
691 EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
692
693 return ret;
694 }
695
696 static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
697 const unsigned char *in, size_t inl,
698 const EVP_CIPHER *cipher)
699 {
700 int ret = 1;
701 unsigned int i, pipes;
702 struct dasync_pipeline_ctx *pipe_ctx =
703 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
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;
711 EVP_CIPHER_meth_get_ctrl(cipher)
712 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
713 EVP_AEAD_TLS1_AAD_LEN,
714 pipe_ctx->tlsaad[0]);
715 }
716 ret = EVP_CIPHER_meth_get_do_cipher(cipher)
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) {
723 EVP_CIPHER_meth_get_ctrl(cipher)
724 (ctx, EVP_CTRL_AEAD_TLS1_AAD,
725 EVP_AEAD_TLS1_AAD_LEN,
726 pipe_ctx->tlsaad[i]);
727 }
728 ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)
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
739 static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
740 const EVP_CIPHER *cipher)
741 {
742 struct dasync_pipeline_ctx *pipe_ctx =
743 (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
744
745 OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
746 EVP_CIPHER_impl_ctx_size(cipher));
747
748 return 1;
749 }
750
751 /*
752 * AES128 CBC Implementation
753 */
754
755 static 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
761 static 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
767 static 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
773 static 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
783 static 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
789 static 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
798 static 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
806 static 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 }