2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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
17 * SHA low level APIs are deprecated for public use, but still ok for
18 * internal use. Note, that due to symbols not being exported, only the
19 * #defines and type definitions can be accessed, function calls are not
20 * available. The digest lengths, block sizes and sizeof(CTX) are used herein
21 * for several different digests.
23 #include "internal/deprecated.h"
28 #include <openssl/engine.h>
29 #include <openssl/sha.h>
30 #include <openssl/md5.h>
31 #include <openssl/rsa.h>
32 #include <openssl/evp.h>
33 #include <openssl/modes.h>
34 #include <openssl/aes.h>
35 #include <openssl/rand.h>
36 #include <openssl/crypto.h>
38 #include "e_ossltest_err.c"
40 /* Engine Id and Name */
41 static const char *engine_ossltest_id = "ossltest";
42 static const char *engine_ossltest_name = "OpenSSL Test engine support";
45 /* Engine Lifetime functions */
46 static int ossltest_destroy(ENGINE *e);
47 static int ossltest_init(ENGINE *e);
48 static int ossltest_finish(ENGINE *e);
49 void ENGINE_load_ossltest(void);
53 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
54 const int **nids, int nid);
55 static const RAND_METHOD *ossltest_rand_method(void);
58 static int digest_md5_init(EVP_MD_CTX *ctx);
59 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
61 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
63 static EVP_MD *_hidden_md5_md = NULL;
64 static const EVP_MD *digest_md5(void)
66 if (_hidden_md5_md == NULL) {
69 if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
70 || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
71 || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
72 || !EVP_MD_meth_set_app_datasize(md,
73 sizeof(EVP_MD *) + sizeof(MD5_CTX))
74 || !EVP_MD_meth_set_flags(md, 0)
75 || !EVP_MD_meth_set_init(md, digest_md5_init)
76 || !EVP_MD_meth_set_update(md, digest_md5_update)
77 || !EVP_MD_meth_set_final(md, digest_md5_final)) {
83 return _hidden_md5_md;
87 static int digest_sha1_init(EVP_MD_CTX *ctx);
88 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
90 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
92 static EVP_MD *_hidden_sha1_md = NULL;
93 static const EVP_MD *digest_sha1(void)
95 if (_hidden_sha1_md == NULL) {
98 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
99 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
100 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
101 || !EVP_MD_meth_set_app_datasize(md,
102 sizeof(EVP_MD *) + sizeof(SHA_CTX))
103 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
104 || !EVP_MD_meth_set_init(md, digest_sha1_init)
105 || !EVP_MD_meth_set_update(md, digest_sha1_update)
106 || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
107 EVP_MD_meth_free(md);
110 _hidden_sha1_md = md;
112 return _hidden_sha1_md;
116 static int digest_sha256_init(EVP_MD_CTX *ctx);
117 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
119 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
121 static EVP_MD *_hidden_sha256_md = NULL;
122 static const EVP_MD *digest_sha256(void)
124 if (_hidden_sha256_md == NULL) {
127 if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
128 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
129 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
130 || !EVP_MD_meth_set_app_datasize(md,
131 sizeof(EVP_MD *) + sizeof(SHA256_CTX))
132 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
133 || !EVP_MD_meth_set_init(md, digest_sha256_init)
134 || !EVP_MD_meth_set_update(md, digest_sha256_update)
135 || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
136 EVP_MD_meth_free(md);
139 _hidden_sha256_md = md;
141 return _hidden_sha256_md;
145 static int digest_sha384_init(EVP_MD_CTX *ctx);
146 static int digest_sha384_update(EVP_MD_CTX *ctx, const void *data,
148 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
150 static int digest_sha512_init(EVP_MD_CTX *ctx);
151 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
153 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
155 static EVP_MD *_hidden_sha384_md = NULL;
156 static const EVP_MD *digest_sha384(void)
158 if (_hidden_sha384_md == NULL) {
161 if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
162 || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
163 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
164 || !EVP_MD_meth_set_app_datasize(md,
165 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
166 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
167 || !EVP_MD_meth_set_init(md, digest_sha384_init)
168 || !EVP_MD_meth_set_update(md, digest_sha384_update)
169 || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
170 EVP_MD_meth_free(md);
173 _hidden_sha384_md = md;
175 return _hidden_sha384_md;
177 static EVP_MD *_hidden_sha512_md = NULL;
178 static const EVP_MD *digest_sha512(void)
180 if (_hidden_sha512_md == NULL) {
183 if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
184 || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
185 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
186 || !EVP_MD_meth_set_app_datasize(md,
187 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
188 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
189 || !EVP_MD_meth_set_init(md, digest_sha512_init)
190 || !EVP_MD_meth_set_update(md, digest_sha512_update)
191 || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
192 EVP_MD_meth_free(md);
195 _hidden_sha512_md = md;
197 return _hidden_sha512_md;
199 static void destroy_digests(void)
201 EVP_MD_meth_free(_hidden_md5_md);
202 _hidden_md5_md = NULL;
203 EVP_MD_meth_free(_hidden_sha1_md);
204 _hidden_sha1_md = NULL;
205 EVP_MD_meth_free(_hidden_sha256_md);
206 _hidden_sha256_md = NULL;
207 EVP_MD_meth_free(_hidden_sha384_md);
208 _hidden_sha384_md = NULL;
209 EVP_MD_meth_free(_hidden_sha512_md);
210 _hidden_sha512_md = NULL;
212 static int ossltest_digest_nids(const int **nids)
214 static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
220 if ((md = digest_md5()) != NULL)
221 digest_nids[pos++] = EVP_MD_type(md);
222 if ((md = digest_sha1()) != NULL)
223 digest_nids[pos++] = EVP_MD_type(md);
224 if ((md = digest_sha256()) != NULL)
225 digest_nids[pos++] = EVP_MD_type(md);
226 if ((md = digest_sha384()) != NULL)
227 digest_nids[pos++] = EVP_MD_type(md);
228 if ((md = digest_sha512()) != NULL)
229 digest_nids[pos++] = EVP_MD_type(md);
230 digest_nids[pos] = 0;
238 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
241 static int ossltest_cipher_nids[] = {
242 NID_aes_128_cbc, NID_aes_128_gcm, 0
247 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
248 const unsigned char *iv, int enc);
249 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
250 const unsigned char *in, size_t inl);
251 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
252 const unsigned char *iv, int enc);
253 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
254 const unsigned char *in, size_t inl);
255 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
258 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
259 static const EVP_CIPHER *ossltest_aes_128_cbc(void)
261 if (_hidden_aes_128_cbc == NULL
262 && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
264 16 /* key len */)) == NULL
265 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
266 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
267 EVP_CIPH_FLAG_DEFAULT_ASN1
269 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
270 ossltest_aes128_init_key)
271 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
272 ossltest_aes128_cbc_cipher)
273 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
274 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
275 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
276 _hidden_aes_128_cbc = NULL;
278 return _hidden_aes_128_cbc;
280 static EVP_CIPHER *_hidden_aes_128_gcm = NULL;
282 #define AES_GCM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
283 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
284 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
285 | EVP_CIPH_CUSTOM_COPY |EVP_CIPH_FLAG_AEAD_CIPHER \
288 static const EVP_CIPHER *ossltest_aes_128_gcm(void)
290 if (_hidden_aes_128_gcm == NULL
291 && ((_hidden_aes_128_gcm = EVP_CIPHER_meth_new(NID_aes_128_gcm,
293 16 /* key len */)) == NULL
294 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_gcm,12)
295 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_gcm, AES_GCM_FLAGS)
296 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_gcm,
297 ossltest_aes128_gcm_init_key)
298 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_gcm,
299 ossltest_aes128_gcm_cipher)
300 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_gcm,
301 ossltest_aes128_gcm_ctrl)
302 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_gcm,
303 EVP_CIPHER_impl_ctx_size(EVP_aes_128_gcm())))) {
304 EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
305 _hidden_aes_128_gcm = NULL;
307 return _hidden_aes_128_gcm;
310 static void destroy_ciphers(void)
312 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
313 EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
314 _hidden_aes_128_cbc = NULL;
317 static int bind_ossltest(ENGINE *e)
319 /* Ensure the ossltest error handling is set up */
320 ERR_load_OSSLTEST_strings();
322 if (!ENGINE_set_id(e, engine_ossltest_id)
323 || !ENGINE_set_name(e, engine_ossltest_name)
324 || !ENGINE_set_digests(e, ossltest_digests)
325 || !ENGINE_set_ciphers(e, ossltest_ciphers)
326 || !ENGINE_set_RAND(e, ossltest_rand_method())
327 || !ENGINE_set_destroy_function(e, ossltest_destroy)
328 || !ENGINE_set_init_function(e, ossltest_init)
329 || !ENGINE_set_finish_function(e, ossltest_finish)) {
330 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
337 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
338 static int bind_helper(ENGINE *e, const char *id)
340 if (id && (strcmp(id, engine_ossltest_id) != 0))
342 if (!bind_ossltest(e))
347 IMPLEMENT_DYNAMIC_CHECK_FN()
348 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
351 static ENGINE *engine_ossltest(void)
353 ENGINE *ret = ENGINE_new();
356 if (!bind_ossltest(ret)) {
363 void ENGINE_load_ossltest(void)
365 /* Copied from eng_[openssl|dyn].c */
366 ENGINE *toadd = engine_ossltest();
375 static int ossltest_init(ENGINE *e)
381 static int ossltest_finish(ENGINE *e)
387 static int ossltest_destroy(ENGINE *e)
391 ERR_unload_OSSLTEST_strings();
395 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
396 const int **nids, int nid)
400 /* We are returning a list of supported nids */
401 return ossltest_digest_nids(nids);
403 /* We are being asked for a specific digest */
406 *digest = digest_md5();
409 *digest = digest_sha1();
412 *digest = digest_sha256();
415 *digest = digest_sha384();
418 *digest = digest_sha512();
428 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
429 const int **nids, int nid)
433 /* We are returning a list of supported nids */
434 *nids = ossltest_cipher_nids;
435 return (sizeof(ossltest_cipher_nids) - 1)
436 / sizeof(ossltest_cipher_nids[0]);
438 /* We are being asked for a specific cipher */
440 case NID_aes_128_cbc:
441 *cipher = ossltest_aes_128_cbc();
443 case NID_aes_128_gcm:
444 *cipher = ossltest_aes_128_gcm();
454 static void fill_known_data(unsigned char *md, unsigned int len)
458 for (i=0; i<len; i++) {
459 md[i] = (unsigned char)(i & 0xff);
464 * MD5 implementation. We go through the motions of doing MD5 by deferring to
465 * the standard implementation. Then we overwrite the result with a will defined
466 * value, so that all "MD5" digests using the test engine always end up with
469 static int digest_md5_init(EVP_MD_CTX *ctx)
471 return EVP_MD_meth_get_init(EVP_md5())(ctx);
474 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
477 return EVP_MD_meth_get_update(EVP_md5())(ctx, data, count);
480 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
482 int ret = EVP_MD_meth_get_final(EVP_md5())(ctx, md);
485 fill_known_data(md, MD5_DIGEST_LENGTH);
491 * SHA1 implementation.
493 static int digest_sha1_init(EVP_MD_CTX *ctx)
495 return EVP_MD_meth_get_init(EVP_sha1())(ctx);
498 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
501 return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
504 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
506 int ret = EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
509 fill_known_data(md, SHA_DIGEST_LENGTH);
515 * SHA256 implementation.
517 static int digest_sha256_init(EVP_MD_CTX *ctx)
519 return EVP_MD_meth_get_init(EVP_sha256())(ctx);
522 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
525 return EVP_MD_meth_get_update(EVP_sha256())(ctx, data, count);
528 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
530 int ret = EVP_MD_meth_get_final(EVP_sha256())(ctx, md);
533 fill_known_data(md, SHA256_DIGEST_LENGTH);
539 * SHA384 implementation.
541 static int digest_sha384_init(EVP_MD_CTX *ctx)
543 return EVP_MD_meth_get_init(EVP_sha384())(ctx);
546 static int digest_sha384_update(EVP_MD_CTX *ctx, const void *data,
549 return EVP_MD_meth_get_update(EVP_sha384())(ctx, data, count);
552 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
554 int ret = EVP_MD_meth_get_final(EVP_sha384())(ctx, md);
557 fill_known_data(md, SHA384_DIGEST_LENGTH);
563 * SHA512 implementation.
565 static int digest_sha512_init(EVP_MD_CTX *ctx)
567 return EVP_MD_meth_get_init(EVP_sha512())(ctx);
570 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
573 return EVP_MD_meth_get_update(EVP_sha512())(ctx, data, count);
576 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
578 int ret = EVP_MD_meth_get_final(EVP_sha512())(ctx, md);
581 fill_known_data(md, SHA512_DIGEST_LENGTH);
587 * AES128 Implementation
590 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
591 const unsigned char *iv, int enc)
593 return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
596 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
597 const unsigned char *in, size_t inl)
599 unsigned char *tmpbuf;
602 tmpbuf = OPENSSL_malloc(inl);
604 /* OPENSSL_malloc will return NULL if inl == 0 */
605 if (tmpbuf == NULL && inl > 0)
608 /* Remember what we were asked to encrypt */
610 memcpy(tmpbuf, in, inl);
612 /* Go through the motions of encrypting it */
613 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
615 /* Throw it all away and just use the plaintext as the output */
617 memcpy(out, tmpbuf, inl);
618 OPENSSL_free(tmpbuf);
623 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
624 const unsigned char *iv, int enc)
626 return EVP_CIPHER_meth_get_init(EVP_aes_128_gcm()) (ctx, key, iv, enc);
630 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
631 const unsigned char *in, size_t inl)
633 unsigned char *tmpbuf = OPENSSL_malloc(inl);
635 /* OPENSSL_malloc will return NULL if inl == 0 */
636 if (tmpbuf == NULL && inl > 0)
639 /* Remember what we were asked to encrypt */
641 memcpy(tmpbuf, in, inl);
643 /* Go through the motions of encrypting it */
644 EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_gcm())(ctx, out, in, inl);
646 /* Throw it all away and just use the plaintext as the output */
647 if (tmpbuf != NULL && out != NULL)
648 memcpy(out, tmpbuf, inl);
649 OPENSSL_free(tmpbuf);
654 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
657 /* Pass the ctrl down */
658 int ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_gcm())(ctx, type, arg, ptr);
664 case EVP_CTRL_AEAD_GET_TAG:
665 /* Always give the same tag */
666 memset(ptr, 0, EVP_GCM_TLS_TAG_LEN);
676 static int ossltest_rand_bytes(unsigned char *buf, int num)
678 unsigned char val = 1;
685 static int ossltest_rand_status(void)
690 static const RAND_METHOD *ossltest_rand_method(void)
693 static RAND_METHOD osslt_rand_meth = {
702 return &osslt_rand_meth;