2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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
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>
28 #define OSSLTEST_LIB_NAME "OSSLTEST"
29 #include "e_ossltest_err.c"
31 /* Engine Id and Name */
32 static const char *engine_ossltest_id = "ossltest";
33 static const char *engine_ossltest_name = "OpenSSL Test engine support";
36 /* Engine Lifetime functions */
37 static int ossltest_destroy(ENGINE *e);
38 static int ossltest_init(ENGINE *e);
39 static int ossltest_finish(ENGINE *e);
40 void ENGINE_load_ossltest(void);
44 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
45 const int **nids, int nid);
48 static int digest_md5_init(EVP_MD_CTX *ctx);
49 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
51 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
53 static EVP_MD *_hidden_md5_md = NULL;
54 static const EVP_MD *digest_md5(void)
56 if (_hidden_md5_md == NULL) {
59 if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
60 || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
61 || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
62 || !EVP_MD_meth_set_app_datasize(md,
63 sizeof(EVP_MD *) + sizeof(MD5_CTX))
64 || !EVP_MD_meth_set_flags(md, 0)
65 || !EVP_MD_meth_set_init(md, digest_md5_init)
66 || !EVP_MD_meth_set_update(md, digest_md5_update)
67 || !EVP_MD_meth_set_final(md, digest_md5_final)) {
73 return _hidden_md5_md;
77 static int digest_sha1_init(EVP_MD_CTX *ctx);
78 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
80 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
82 static EVP_MD *_hidden_sha1_md = NULL;
83 static const EVP_MD *digest_sha1(void)
85 if (_hidden_sha1_md == NULL) {
88 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
89 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
90 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
91 || !EVP_MD_meth_set_app_datasize(md,
92 sizeof(EVP_MD *) + sizeof(SHA_CTX))
93 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
94 || !EVP_MD_meth_set_init(md, digest_sha1_init)
95 || !EVP_MD_meth_set_update(md, digest_sha1_update)
96 || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
100 _hidden_sha1_md = md;
102 return _hidden_sha1_md;
106 static int digest_sha256_init(EVP_MD_CTX *ctx);
107 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
109 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
111 static EVP_MD *_hidden_sha256_md = NULL;
112 static const EVP_MD *digest_sha256(void)
114 if (_hidden_sha256_md == NULL) {
117 if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
118 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
119 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
120 || !EVP_MD_meth_set_app_datasize(md,
121 sizeof(EVP_MD *) + sizeof(SHA256_CTX))
122 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
123 || !EVP_MD_meth_set_init(md, digest_sha256_init)
124 || !EVP_MD_meth_set_update(md, digest_sha256_update)
125 || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
126 EVP_MD_meth_free(md);
129 _hidden_sha256_md = md;
131 return _hidden_sha256_md;
135 static int digest_sha384_init(EVP_MD_CTX *ctx);
136 static int digest_sha512_init(EVP_MD_CTX *ctx);
137 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
139 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
140 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
142 static EVP_MD *_hidden_sha384_md = NULL;
143 static const EVP_MD *digest_sha384(void)
145 if (_hidden_sha384_md == NULL) {
148 if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
149 || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
150 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
151 || !EVP_MD_meth_set_app_datasize(md,
152 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
153 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
154 || !EVP_MD_meth_set_init(md, digest_sha384_init)
155 || !EVP_MD_meth_set_update(md, digest_sha512_update)
156 || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
157 EVP_MD_meth_free(md);
160 _hidden_sha384_md = md;
162 return _hidden_sha384_md;
164 static EVP_MD *_hidden_sha512_md = NULL;
165 static const EVP_MD *digest_sha512(void)
167 if (_hidden_sha512_md == NULL) {
170 if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
171 || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
172 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
173 || !EVP_MD_meth_set_app_datasize(md,
174 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
175 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
176 || !EVP_MD_meth_set_init(md, digest_sha512_init)
177 || !EVP_MD_meth_set_update(md, digest_sha512_update)
178 || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
179 EVP_MD_meth_free(md);
182 _hidden_sha512_md = md;
184 return _hidden_sha512_md;
186 static void destroy_digests(void)
188 EVP_MD_meth_free(_hidden_md5_md);
189 _hidden_md5_md = NULL;
190 EVP_MD_meth_free(_hidden_sha1_md);
191 _hidden_sha1_md = NULL;
192 EVP_MD_meth_free(_hidden_sha256_md);
193 _hidden_sha256_md = NULL;
194 EVP_MD_meth_free(_hidden_sha384_md);
195 _hidden_sha384_md = NULL;
196 EVP_MD_meth_free(_hidden_sha512_md);
197 _hidden_sha512_md = NULL;
199 static int ossltest_digest_nids(const int **nids)
201 static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
207 if ((md = digest_md5()) != NULL)
208 digest_nids[pos++] = EVP_MD_type(md);
209 if ((md = digest_sha1()) != NULL)
210 digest_nids[pos++] = EVP_MD_type(md);
211 if ((md = digest_sha256()) != NULL)
212 digest_nids[pos++] = EVP_MD_type(md);
213 if ((md = digest_sha384()) != NULL)
214 digest_nids[pos++] = EVP_MD_type(md);
215 if ((md = digest_sha512()) != NULL)
216 digest_nids[pos++] = EVP_MD_type(md);
217 digest_nids[pos] = 0;
225 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
228 static int ossltest_cipher_nids[] = {
229 NID_aes_128_cbc, NID_aes_128_gcm, 0
234 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
235 const unsigned char *iv, int enc);
236 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
237 const unsigned char *in, size_t inl);
238 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
239 const unsigned char *iv, int enc);
240 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
241 const unsigned char *in, size_t inl);
242 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
245 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
246 static const EVP_CIPHER *ossltest_aes_128_cbc(void)
248 if (_hidden_aes_128_cbc == NULL
249 && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
251 16 /* key len */)) == NULL
252 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
253 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
254 EVP_CIPH_FLAG_DEFAULT_ASN1
256 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
257 ossltest_aes128_init_key)
258 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
259 ossltest_aes128_cbc_cipher)
260 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
261 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
262 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
263 _hidden_aes_128_cbc = NULL;
265 return _hidden_aes_128_cbc;
267 static EVP_CIPHER *_hidden_aes_128_gcm = NULL;
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 \
273 static const EVP_CIPHER *ossltest_aes_128_gcm(void)
275 if (_hidden_aes_128_gcm == NULL
276 && ((_hidden_aes_128_gcm = EVP_CIPHER_meth_new(NID_aes_128_gcm,
278 16 /* key len */)) == NULL
279 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_gcm,12)
280 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_gcm, AES_GCM_FLAGS)
281 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_gcm,
282 ossltest_aes128_gcm_init_key)
283 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_gcm,
284 ossltest_aes128_gcm_cipher)
285 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_gcm,
286 ossltest_aes128_gcm_ctrl)
287 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_gcm,
288 EVP_CIPHER_impl_ctx_size(EVP_aes_128_gcm())))) {
289 EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
290 _hidden_aes_128_gcm = NULL;
292 return _hidden_aes_128_gcm;
295 static void destroy_ciphers(void)
297 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
298 EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
299 _hidden_aes_128_cbc = NULL;
302 static int bind_ossltest(ENGINE *e)
304 /* Ensure the ossltest error handling is set up */
305 ERR_load_OSSLTEST_strings();
307 if (!ENGINE_set_id(e, engine_ossltest_id)
308 || !ENGINE_set_name(e, engine_ossltest_name)
309 || !ENGINE_set_digests(e, ossltest_digests)
310 || !ENGINE_set_ciphers(e, ossltest_ciphers)
311 || !ENGINE_set_destroy_function(e, ossltest_destroy)
312 || !ENGINE_set_init_function(e, ossltest_init)
313 || !ENGINE_set_finish_function(e, ossltest_finish)) {
314 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
321 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
322 static int bind_helper(ENGINE *e, const char *id)
324 if (id && (strcmp(id, engine_ossltest_id) != 0))
326 if (!bind_ossltest(e))
331 IMPLEMENT_DYNAMIC_CHECK_FN()
332 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
335 static ENGINE *engine_ossltest(void)
337 ENGINE *ret = ENGINE_new();
340 if (!bind_ossltest(ret)) {
347 void ENGINE_load_ossltest(void)
349 /* Copied from eng_[openssl|dyn].c */
350 ENGINE *toadd = engine_ossltest();
359 static int ossltest_init(ENGINE *e)
365 static int ossltest_finish(ENGINE *e)
371 static int ossltest_destroy(ENGINE *e)
375 ERR_unload_OSSLTEST_strings();
379 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
380 const int **nids, int nid)
384 /* We are returning a list of supported nids */
385 return ossltest_digest_nids(nids);
387 /* We are being asked for a specific digest */
390 *digest = digest_md5();
393 *digest = digest_sha1();
396 *digest = digest_sha256();
399 *digest = digest_sha384();
402 *digest = digest_sha512();
412 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
413 const int **nids, int nid)
417 /* We are returning a list of supported nids */
418 *nids = ossltest_cipher_nids;
419 return (sizeof(ossltest_cipher_nids) - 1)
420 / sizeof(ossltest_cipher_nids[0]);
422 /* We are being asked for a specific cipher */
424 case NID_aes_128_cbc:
425 *cipher = ossltest_aes_128_cbc();
427 case NID_aes_128_gcm:
428 *cipher = ossltest_aes_128_gcm();
438 static void fill_known_data(unsigned char *md, unsigned int len)
442 for (i=0; i<len; i++) {
443 md[i] = (unsigned char)(i & 0xff);
448 * MD5 implementation. We go through the motions of doing MD5 by deferring to
449 * the standard implementation. Then we overwrite the result with a will defined
450 * value, so that all "MD5" digests using the test engine always end up with
454 #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx))
455 static int digest_md5_init(EVP_MD_CTX *ctx)
457 return MD5_Init(data(ctx));
460 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
463 return MD5_Update(data(ctx), data, (size_t)count);
466 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
469 ret = MD5_Final(md, data(ctx));
472 fill_known_data(md, MD5_DIGEST_LENGTH);
478 * SHA1 implementation.
481 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
482 static int digest_sha1_init(EVP_MD_CTX *ctx)
484 return SHA1_Init(data(ctx));
487 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
490 return SHA1_Update(data(ctx), data, (size_t)count);
493 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
496 ret = SHA1_Final(md, data(ctx));
499 fill_known_data(md, SHA_DIGEST_LENGTH);
505 * SHA256 implementation.
508 #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx))
509 static int digest_sha256_init(EVP_MD_CTX *ctx)
511 return SHA256_Init(data(ctx));
514 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
517 return SHA256_Update(data(ctx), data, (size_t)count);
520 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
523 ret = SHA256_Final(md, data(ctx));
526 fill_known_data(md, SHA256_DIGEST_LENGTH);
532 * SHA384/512 implementation.
535 #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx))
536 static int digest_sha384_init(EVP_MD_CTX *ctx)
538 return SHA384_Init(data(ctx));
541 static int digest_sha512_init(EVP_MD_CTX *ctx)
543 return SHA512_Init(data(ctx));
546 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
549 return SHA512_Update(data(ctx), data, (size_t)count);
552 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
555 /* Actually uses SHA512_Final! */
556 ret = SHA512_Final(md, data(ctx));
559 fill_known_data(md, SHA384_DIGEST_LENGTH);
564 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
567 ret = SHA512_Final(md, data(ctx));
570 fill_known_data(md, SHA512_DIGEST_LENGTH);
576 * AES128 Implementation
579 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
580 const unsigned char *iv, int enc)
582 return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
585 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
586 const unsigned char *in, size_t inl)
588 unsigned char *tmpbuf;
591 tmpbuf = OPENSSL_malloc(inl);
595 /* Remember what we were asked to encrypt */
596 memcpy(tmpbuf, in, inl);
598 /* Go through the motions of encrypting it */
599 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
601 /* Throw it all away and just use the plaintext as the output */
602 memcpy(out, tmpbuf, inl);
603 OPENSSL_free(tmpbuf);
608 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
609 const unsigned char *iv, int enc)
611 return EVP_CIPHER_meth_get_init(EVP_aes_128_gcm()) (ctx, key, iv, enc);
615 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
616 const unsigned char *in, size_t inl)
618 unsigned char *tmpbuf;
619 const size_t datalen = inl - EVP_GCM_TLS_EXPLICIT_IV_LEN
620 - EVP_GCM_TLS_TAG_LEN;
622 tmpbuf = OPENSSL_malloc(datalen);
626 /* Remember what we were asked to encrypt */
627 memcpy(tmpbuf, in + EVP_GCM_TLS_EXPLICIT_IV_LEN, datalen);
629 /* Go through the motions of encrypting it */
630 EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_gcm())(ctx, out, in, inl);
633 * Throw it all away and just use the plaintext as the output with empty
637 memcpy(out + EVP_GCM_TLS_EXPLICIT_IV_LEN, tmpbuf, datalen);
638 OPENSSL_free(tmpbuf);
643 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
648 /* Pass the ctrl down */
649 ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_gcm())(ctx, type, arg, ptr);