deprecate engines
[openssl.git] / engines / e_ossltest.c
1 /*
2  * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * This is the OSSLTEST engine. It provides deliberately crippled digest
12  * implementations for test purposes. It is highly insecure and must NOT be
13  * used for any purpose except testing
14  */
15
16 /* We need to use some engine deprecated APIs */
17 #define OPENSSL_SUPPRESS_DEPRECATED
18
19 /*
20  * SHA low level APIs are deprecated for public use, but still ok for
21  * internal use.  Note, that due to symbols not being exported, only the
22  * #defines and type definitions can be accessed, function calls are not
23  * available.  The digest lengths, block sizes and sizeof(CTX) are used herein
24  * for several different digests.
25  */
26 #include "internal/deprecated.h"
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <openssl/engine.h>
32 #include <openssl/sha.h>
33 #include <openssl/md5.h>
34 #include <openssl/rsa.h>
35 #include <openssl/evp.h>
36 #include <openssl/modes.h>
37 #include <openssl/aes.h>
38 #include <openssl/rand.h>
39 #include <openssl/crypto.h>
40
41 #include "e_ossltest_err.c"
42
43 /* Engine Id and Name */
44 static const char *engine_ossltest_id = "ossltest";
45 static const char *engine_ossltest_name = "OpenSSL Test engine support";
46
47
48 /* Engine Lifetime functions */
49 static int ossltest_destroy(ENGINE *e);
50 static int ossltest_init(ENGINE *e);
51 static int ossltest_finish(ENGINE *e);
52 void ENGINE_load_ossltest(void);
53
54
55 /* Set up digests */
56 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
57                           const int **nids, int nid);
58 static const RAND_METHOD *ossltest_rand_method(void);
59
60 /* MD5 */
61 static int digest_md5_init(EVP_MD_CTX *ctx);
62 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
63                              size_t count);
64 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
65
66 static EVP_MD *_hidden_md5_md = NULL;
67 static const EVP_MD *digest_md5(void)
68 {
69     if (_hidden_md5_md == NULL) {
70         EVP_MD *md;
71
72         if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
73             || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
74             || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
75             || !EVP_MD_meth_set_app_datasize(md,
76                                              sizeof(EVP_MD *) + sizeof(MD5_CTX))
77             || !EVP_MD_meth_set_flags(md, 0)
78             || !EVP_MD_meth_set_init(md, digest_md5_init)
79             || !EVP_MD_meth_set_update(md, digest_md5_update)
80             || !EVP_MD_meth_set_final(md, digest_md5_final)) {
81             EVP_MD_meth_free(md);
82             md = NULL;
83         }
84         _hidden_md5_md = md;
85     }
86     return _hidden_md5_md;
87 }
88
89 /* SHA1 */
90 static int digest_sha1_init(EVP_MD_CTX *ctx);
91 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
92                               size_t count);
93 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
94
95 static EVP_MD *_hidden_sha1_md = NULL;
96 static const EVP_MD *digest_sha1(void)
97 {
98     if (_hidden_sha1_md == NULL) {
99         EVP_MD *md;
100
101         if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
102             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
103             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
104             || !EVP_MD_meth_set_app_datasize(md,
105                                              sizeof(EVP_MD *) + sizeof(SHA_CTX))
106             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
107             || !EVP_MD_meth_set_init(md, digest_sha1_init)
108             || !EVP_MD_meth_set_update(md, digest_sha1_update)
109             || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
110             EVP_MD_meth_free(md);
111             md = NULL;
112         }
113         _hidden_sha1_md = md;
114     }
115     return _hidden_sha1_md;
116 }
117
118 /* SHA256 */
119 static int digest_sha256_init(EVP_MD_CTX *ctx);
120 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
121                                 size_t count);
122 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
123
124 static EVP_MD *_hidden_sha256_md = NULL;
125 static const EVP_MD *digest_sha256(void)
126 {
127     if (_hidden_sha256_md == NULL) {
128         EVP_MD *md;
129
130         if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
131             || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
132             || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
133             || !EVP_MD_meth_set_app_datasize(md,
134                                              sizeof(EVP_MD *) + sizeof(SHA256_CTX))
135             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
136             || !EVP_MD_meth_set_init(md, digest_sha256_init)
137             || !EVP_MD_meth_set_update(md, digest_sha256_update)
138             || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
139             EVP_MD_meth_free(md);
140             md = NULL;
141         }
142         _hidden_sha256_md = md;
143     }
144     return _hidden_sha256_md;
145 }
146
147 /* SHA384/SHA512 */
148 static int digest_sha384_init(EVP_MD_CTX *ctx);
149 static int digest_sha384_update(EVP_MD_CTX *ctx, const void *data,
150                                 size_t count);
151 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
152
153 static int digest_sha512_init(EVP_MD_CTX *ctx);
154 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
155                                 size_t count);
156 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
157
158 static EVP_MD *_hidden_sha384_md = NULL;
159 static const EVP_MD *digest_sha384(void)
160 {
161     if (_hidden_sha384_md == NULL) {
162         EVP_MD *md;
163
164         if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
165             || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
166             || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
167             || !EVP_MD_meth_set_app_datasize(md,
168                                              sizeof(EVP_MD *) + sizeof(SHA512_CTX))
169             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
170             || !EVP_MD_meth_set_init(md, digest_sha384_init)
171             || !EVP_MD_meth_set_update(md, digest_sha384_update)
172             || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
173             EVP_MD_meth_free(md);
174             md = NULL;
175         }
176         _hidden_sha384_md = md;
177     }
178     return _hidden_sha384_md;
179 }
180 static EVP_MD *_hidden_sha512_md = NULL;
181 static const EVP_MD *digest_sha512(void)
182 {
183     if (_hidden_sha512_md == NULL) {
184         EVP_MD *md;
185
186         if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
187             || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
188             || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
189             || !EVP_MD_meth_set_app_datasize(md,
190                                              sizeof(EVP_MD *) + sizeof(SHA512_CTX))
191             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
192             || !EVP_MD_meth_set_init(md, digest_sha512_init)
193             || !EVP_MD_meth_set_update(md, digest_sha512_update)
194             || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
195             EVP_MD_meth_free(md);
196             md = NULL;
197         }
198         _hidden_sha512_md = md;
199     }
200     return _hidden_sha512_md;
201 }
202 static void destroy_digests(void)
203 {
204     EVP_MD_meth_free(_hidden_md5_md);
205     _hidden_md5_md = NULL;
206     EVP_MD_meth_free(_hidden_sha1_md);
207     _hidden_sha1_md = NULL;
208     EVP_MD_meth_free(_hidden_sha256_md);
209     _hidden_sha256_md = NULL;
210     EVP_MD_meth_free(_hidden_sha384_md);
211     _hidden_sha384_md = NULL;
212     EVP_MD_meth_free(_hidden_sha512_md);
213     _hidden_sha512_md = NULL;
214 }
215 static int ossltest_digest_nids(const int **nids)
216 {
217     static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
218     static int pos = 0;
219     static int init = 0;
220
221     if (!init) {
222         const EVP_MD *md;
223         if ((md = digest_md5()) != NULL)
224             digest_nids[pos++] = EVP_MD_type(md);
225         if ((md = digest_sha1()) != NULL)
226             digest_nids[pos++] = EVP_MD_type(md);
227         if ((md = digest_sha256()) != NULL)
228             digest_nids[pos++] = EVP_MD_type(md);
229         if ((md = digest_sha384()) != NULL)
230             digest_nids[pos++] = EVP_MD_type(md);
231         if ((md = digest_sha512()) != NULL)
232             digest_nids[pos++] = EVP_MD_type(md);
233         digest_nids[pos] = 0;
234         init = 1;
235     }
236     *nids = digest_nids;
237     return pos;
238 }
239
240 /* Setup ciphers */
241 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
242                             const int **, int);
243
244 static int ossltest_cipher_nids[] = {
245     NID_aes_128_cbc, NID_aes_128_gcm, 0
246 };
247
248 /* AES128 */
249
250 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
251                              const unsigned char *iv, int enc);
252 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
253                                const unsigned char *in, size_t inl);
254 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
255                              const unsigned char *iv, int enc);
256 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
257                                const unsigned char *in, size_t inl);
258 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
259                                     void *ptr);
260
261 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
262 static const EVP_CIPHER *ossltest_aes_128_cbc(void)
263 {
264     if (_hidden_aes_128_cbc == NULL
265         && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
266                                                        16 /* block size */,
267                                                        16 /* key len */)) == NULL
268             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
269             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
270                                           EVP_CIPH_FLAG_DEFAULT_ASN1
271                                           | EVP_CIPH_CBC_MODE)
272             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
273                                          ossltest_aes128_init_key)
274             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
275                                               ossltest_aes128_cbc_cipher)
276             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
277                                                   EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
278         EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
279         _hidden_aes_128_cbc = NULL;
280     }
281     return _hidden_aes_128_cbc;
282 }
283 static EVP_CIPHER *_hidden_aes_128_gcm = NULL;
284
285 #define AES_GCM_FLAGS   (EVP_CIPH_FLAG_DEFAULT_ASN1 \
286                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
287                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
288                 | EVP_CIPH_CUSTOM_COPY |EVP_CIPH_FLAG_AEAD_CIPHER \
289                 | EVP_CIPH_GCM_MODE)
290
291 static const EVP_CIPHER *ossltest_aes_128_gcm(void)
292 {
293     if (_hidden_aes_128_gcm == NULL
294         && ((_hidden_aes_128_gcm = EVP_CIPHER_meth_new(NID_aes_128_gcm,
295                                                        1 /* block size */,
296                                                        16 /* key len */)) == NULL
297             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_gcm,12)
298             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_gcm, AES_GCM_FLAGS)
299             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_gcm,
300                                          ossltest_aes128_gcm_init_key)
301             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_gcm,
302                                               ossltest_aes128_gcm_cipher)
303             || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_gcm,
304                                               ossltest_aes128_gcm_ctrl)
305             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_gcm,
306                               EVP_CIPHER_impl_ctx_size(EVP_aes_128_gcm())))) {
307         EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
308         _hidden_aes_128_gcm = NULL;
309     }
310     return _hidden_aes_128_gcm;
311 }
312
313 static void destroy_ciphers(void)
314 {
315     EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
316     EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
317     _hidden_aes_128_cbc = NULL;
318 }
319
320 static int bind_ossltest(ENGINE *e)
321 {
322     /* Ensure the ossltest error handling is set up */
323     ERR_load_OSSLTEST_strings();
324
325     if (!ENGINE_set_id(e, engine_ossltest_id)
326         || !ENGINE_set_name(e, engine_ossltest_name)
327         || !ENGINE_set_digests(e, ossltest_digests)
328         || !ENGINE_set_ciphers(e, ossltest_ciphers)
329         || !ENGINE_set_RAND(e, ossltest_rand_method())
330         || !ENGINE_set_destroy_function(e, ossltest_destroy)
331         || !ENGINE_set_init_function(e, ossltest_init)
332         || !ENGINE_set_finish_function(e, ossltest_finish)) {
333         OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
334         return 0;
335     }
336
337     return 1;
338 }
339
340 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
341 static int bind_helper(ENGINE *e, const char *id)
342 {
343     if (id && (strcmp(id, engine_ossltest_id) != 0))
344         return 0;
345     if (!bind_ossltest(e))
346         return 0;
347     return 1;
348 }
349
350 IMPLEMENT_DYNAMIC_CHECK_FN()
351     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
352 #endif
353
354 static ENGINE *engine_ossltest(void)
355 {
356     ENGINE *ret = ENGINE_new();
357     if (ret == NULL)
358         return NULL;
359     if (!bind_ossltest(ret)) {
360         ENGINE_free(ret);
361         return NULL;
362     }
363     return ret;
364 }
365
366 void ENGINE_load_ossltest(void)
367 {
368     /* Copied from eng_[openssl|dyn].c */
369     ENGINE *toadd = engine_ossltest();
370     if (!toadd)
371         return;
372     ENGINE_add(toadd);
373     ENGINE_free(toadd);
374     ERR_clear_error();
375 }
376
377
378 static int ossltest_init(ENGINE *e)
379 {
380     return 1;
381 }
382
383
384 static int ossltest_finish(ENGINE *e)
385 {
386     return 1;
387 }
388
389
390 static int ossltest_destroy(ENGINE *e)
391 {
392     destroy_digests();
393     destroy_ciphers();
394     ERR_unload_OSSLTEST_strings();
395     return 1;
396 }
397
398 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
399                           const int **nids, int nid)
400 {
401     int ok = 1;
402     if (!digest) {
403         /* We are returning a list of supported nids */
404         return ossltest_digest_nids(nids);
405     }
406     /* We are being asked for a specific digest */
407     switch (nid) {
408     case NID_md5:
409         *digest = digest_md5();
410         break;
411     case NID_sha1:
412         *digest = digest_sha1();
413         break;
414     case NID_sha256:
415         *digest = digest_sha256();
416         break;
417     case NID_sha384:
418         *digest = digest_sha384();
419         break;
420     case NID_sha512:
421         *digest = digest_sha512();
422         break;
423     default:
424         ok = 0;
425         *digest = NULL;
426         break;
427     }
428     return ok;
429 }
430
431 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
432                           const int **nids, int nid)
433 {
434     int ok = 1;
435     if (!cipher) {
436         /* We are returning a list of supported nids */
437         *nids = ossltest_cipher_nids;
438         return (sizeof(ossltest_cipher_nids) - 1)
439                / sizeof(ossltest_cipher_nids[0]);
440     }
441     /* We are being asked for a specific cipher */
442     switch (nid) {
443     case NID_aes_128_cbc:
444         *cipher = ossltest_aes_128_cbc();
445         break;
446     case NID_aes_128_gcm:
447         *cipher = ossltest_aes_128_gcm();
448         break;
449     default:
450         ok = 0;
451         *cipher = NULL;
452         break;
453     }
454     return ok;
455 }
456
457 static void fill_known_data(unsigned char *md, unsigned int len)
458 {
459     unsigned int i;
460
461     for (i=0; i<len; i++) {
462         md[i] = (unsigned char)(i & 0xff);
463     }
464 }
465
466 /*
467  * MD5 implementation. We go through the motions of doing MD5 by deferring to
468  * the standard implementation. Then we overwrite the result with a will defined
469  * value, so that all "MD5" digests using the test engine always end up with
470  * the same value.
471  */
472 static int digest_md5_init(EVP_MD_CTX *ctx)
473 {
474    return EVP_MD_meth_get_init(EVP_md5())(ctx);
475 }
476
477 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
478                              size_t count)
479 {
480     return EVP_MD_meth_get_update(EVP_md5())(ctx, data, count);
481 }
482
483 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
484 {
485     int ret = EVP_MD_meth_get_final(EVP_md5())(ctx, md);
486
487     if (ret > 0) {
488         fill_known_data(md, MD5_DIGEST_LENGTH);
489     }
490     return ret;
491 }
492
493 /*
494  * SHA1 implementation.
495  */
496 static int digest_sha1_init(EVP_MD_CTX *ctx)
497 {
498     return EVP_MD_meth_get_init(EVP_sha1())(ctx);
499 }
500
501 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
502                               size_t count)
503 {
504     return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
505 }
506
507 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
508 {
509     int ret = EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
510
511     if (ret > 0) {
512         fill_known_data(md, SHA_DIGEST_LENGTH);
513     }
514     return ret;
515 }
516
517 /*
518  * SHA256 implementation.
519  */
520 static int digest_sha256_init(EVP_MD_CTX *ctx)
521 {
522     return EVP_MD_meth_get_init(EVP_sha256())(ctx);
523 }
524
525 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
526                                 size_t count)
527 {
528     return EVP_MD_meth_get_update(EVP_sha256())(ctx, data, count);
529 }
530
531 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
532 {
533     int ret = EVP_MD_meth_get_final(EVP_sha256())(ctx, md);
534
535     if (ret > 0) {
536         fill_known_data(md, SHA256_DIGEST_LENGTH);
537     }
538     return ret;
539 }
540
541 /*
542  * SHA384 implementation.
543  */
544 static int digest_sha384_init(EVP_MD_CTX *ctx)
545 {
546     return EVP_MD_meth_get_init(EVP_sha384())(ctx);
547 }
548
549 static int digest_sha384_update(EVP_MD_CTX *ctx, const void *data,
550                                 size_t count)
551 {
552     return EVP_MD_meth_get_update(EVP_sha384())(ctx, data, count);
553 }
554
555 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
556 {
557     int ret = EVP_MD_meth_get_final(EVP_sha384())(ctx, md);
558
559     if (ret > 0) {
560         fill_known_data(md, SHA384_DIGEST_LENGTH);
561     }
562     return ret;
563 }
564
565 /*
566  * SHA512 implementation.
567  */
568 static int digest_sha512_init(EVP_MD_CTX *ctx)
569 {
570     return EVP_MD_meth_get_init(EVP_sha512())(ctx);
571 }
572
573 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
574                                 size_t count)
575 {
576     return EVP_MD_meth_get_update(EVP_sha512())(ctx, data, count);
577 }
578
579 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
580 {
581     int ret = EVP_MD_meth_get_final(EVP_sha512())(ctx, md);
582
583     if (ret > 0) {
584         fill_known_data(md, SHA512_DIGEST_LENGTH);
585     }
586     return ret;
587 }
588
589 /*
590  * AES128 Implementation
591  */
592
593 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
594                              const unsigned char *iv, int enc)
595 {
596     return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
597 }
598
599 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
600                                const unsigned char *in, size_t inl)
601 {
602     unsigned char *tmpbuf;
603     int ret;
604
605     tmpbuf = OPENSSL_malloc(inl);
606
607     /* OPENSSL_malloc will return NULL if inl == 0 */
608     if (tmpbuf == NULL && inl > 0)
609         return -1;
610
611     /* Remember what we were asked to encrypt */
612     if (tmpbuf != NULL)
613         memcpy(tmpbuf, in, inl);
614
615     /* Go through the motions of encrypting it */
616     ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
617
618     /* Throw it all away and just use the plaintext as the output */
619     if (tmpbuf != NULL)
620         memcpy(out, tmpbuf, inl);
621     OPENSSL_free(tmpbuf);
622
623     return ret;
624 }
625
626 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
627                              const unsigned char *iv, int enc)
628 {
629     return EVP_CIPHER_meth_get_init(EVP_aes_128_gcm()) (ctx, key, iv, enc);
630 }
631
632
633 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
634                                const unsigned char *in, size_t inl)
635 {
636     unsigned char *tmpbuf = OPENSSL_malloc(inl);
637
638     /* OPENSSL_malloc will return NULL if inl == 0 */
639     if (tmpbuf == NULL && inl > 0)
640         return -1;
641
642     /* Remember what we were asked to encrypt */
643     if (tmpbuf != NULL)
644         memcpy(tmpbuf, in, inl);
645
646     /* Go through the motions of encrypting it */
647     EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_gcm())(ctx, out, in, inl);
648
649     /* Throw it all away and just use the plaintext as the output */
650     if (tmpbuf != NULL && out != NULL)
651         memcpy(out, tmpbuf, inl);
652     OPENSSL_free(tmpbuf);
653
654     return inl;
655 }
656
657 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
658                                     void *ptr)
659 {
660     /* Pass the ctrl down */
661     int ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_gcm())(ctx, type, arg, ptr);
662
663     if (ret <= 0)
664         return ret;
665
666     switch(type) {
667     case EVP_CTRL_AEAD_GET_TAG:
668         /* Always give the same tag */
669         memset(ptr, 0, EVP_GCM_TLS_TAG_LEN);
670         break;
671
672     default:
673         break;
674     }
675
676     return 1;
677 }
678
679 static int ossltest_rand_bytes(unsigned char *buf, int num)
680 {
681     unsigned char val = 1;
682
683     while (--num >= 0)
684         *buf++ = val++;
685     return 1;
686 }
687
688 static int ossltest_rand_status(void)
689 {
690     return 1;
691 }
692
693 static const RAND_METHOD *ossltest_rand_method(void)
694 {
695
696     static RAND_METHOD osslt_rand_meth = {
697         NULL,
698         ossltest_rand_bytes,
699         NULL,
700         NULL,
701         ossltest_rand_bytes,
702         ossltest_rand_status
703     };
704
705     return &osslt_rand_meth;
706 }