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