2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
54 * This is the OSSLTEST engine. It provides deliberately crippled digest
55 * implementations for test purposes. It is highly insecure and must NOT be
56 * used for any purpose except testing
62 #include <openssl/engine.h>
63 #include <openssl/sha.h>
64 #include <openssl/md5.h>
65 #include <openssl/rsa.h>
66 #include <openssl/evp.h>
67 #include <openssl/modes.h>
68 #include <openssl/aes.h>
70 #define OSSLTEST_LIB_NAME "OSSLTEST"
71 #include "e_ossltest_err.c"
73 /* Engine Id and Name */
74 static const char *engine_ossltest_id = "ossltest";
75 static const char *engine_ossltest_name = "OpenSSL Test engine support";
78 /* Engine Lifetime functions */
79 static int ossltest_destroy(ENGINE *e);
80 static int ossltest_init(ENGINE *e);
81 static int ossltest_finish(ENGINE *e);
82 void ENGINE_load_ossltest(void);
86 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
87 const int **nids, int nid);
90 static int digest_md5_init(EVP_MD_CTX *ctx);
91 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
93 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
95 static EVP_MD *_hidden_md5_md = NULL;
96 static const EVP_MD *digest_md5(void)
98 if (_hidden_md5_md == NULL) {
101 if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
102 || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
103 || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
104 || !EVP_MD_meth_set_app_datasize(md,
105 sizeof(EVP_MD *) + sizeof(MD5_CTX))
106 || !EVP_MD_meth_set_flags(md, 0)
107 || !EVP_MD_meth_set_init(md, digest_md5_init)
108 || !EVP_MD_meth_set_update(md, digest_md5_update)
109 || !EVP_MD_meth_set_final(md, digest_md5_final)) {
110 EVP_MD_meth_free(md);
115 return _hidden_md5_md;
119 static int digest_sha1_init(EVP_MD_CTX *ctx);
120 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
122 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
124 static EVP_MD *_hidden_sha1_md = NULL;
125 static const EVP_MD *digest_sha1(void)
127 if (_hidden_sha1_md == NULL) {
130 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
131 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
132 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
133 || !EVP_MD_meth_set_app_datasize(md,
134 sizeof(EVP_MD *) + sizeof(SHA_CTX))
135 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
136 || !EVP_MD_meth_set_init(md, digest_sha1_init)
137 || !EVP_MD_meth_set_update(md, digest_sha1_update)
138 || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
139 EVP_MD_meth_free(md);
142 _hidden_sha1_md = md;
144 return _hidden_sha1_md;
148 static int digest_sha256_init(EVP_MD_CTX *ctx);
149 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
151 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
153 static EVP_MD *_hidden_sha256_md = NULL;
154 static const EVP_MD *digest_sha256(void)
156 if (_hidden_sha256_md == NULL) {
159 if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
160 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
161 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
162 || !EVP_MD_meth_set_app_datasize(md,
163 sizeof(EVP_MD *) + sizeof(SHA256_CTX))
164 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
165 || !EVP_MD_meth_set_init(md, digest_sha256_init)
166 || !EVP_MD_meth_set_update(md, digest_sha256_update)
167 || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
168 EVP_MD_meth_free(md);
171 _hidden_sha256_md = md;
173 return _hidden_sha256_md;
177 static int digest_sha384_init(EVP_MD_CTX *ctx);
178 static int digest_sha512_init(EVP_MD_CTX *ctx);
179 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
181 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
182 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
184 static EVP_MD *_hidden_sha384_md = NULL;
185 static const EVP_MD *digest_sha384(void)
187 if (_hidden_sha384_md == NULL) {
190 if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
191 || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
192 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
193 || !EVP_MD_meth_set_app_datasize(md,
194 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
195 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
196 || !EVP_MD_meth_set_init(md, digest_sha384_init)
197 || !EVP_MD_meth_set_update(md, digest_sha512_update)
198 || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
199 EVP_MD_meth_free(md);
202 _hidden_sha384_md = md;
204 return _hidden_sha384_md;
206 static EVP_MD *_hidden_sha512_md = NULL;
207 static const EVP_MD *digest_sha512(void)
209 if (_hidden_sha512_md == NULL) {
212 if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
213 || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
214 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
215 || !EVP_MD_meth_set_app_datasize(md,
216 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
217 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
218 || !EVP_MD_meth_set_init(md, digest_sha512_init)
219 || !EVP_MD_meth_set_update(md, digest_sha512_update)
220 || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
221 EVP_MD_meth_free(md);
224 _hidden_sha512_md = md;
226 return _hidden_sha512_md;
228 static void destroy_digests(void)
230 EVP_MD_meth_free(_hidden_md5_md);
231 _hidden_md5_md = NULL;
232 EVP_MD_meth_free(_hidden_sha1_md);
233 _hidden_sha1_md = NULL;
234 EVP_MD_meth_free(_hidden_sha256_md);
235 _hidden_sha256_md = NULL;
236 EVP_MD_meth_free(_hidden_sha384_md);
237 _hidden_sha384_md = NULL;
238 EVP_MD_meth_free(_hidden_sha512_md);
239 _hidden_sha512_md = NULL;
241 static int ossltest_digest_nids(const int **nids)
243 static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
249 if ((md = digest_md5()) != NULL)
250 digest_nids[pos++] = EVP_MD_type(md);
251 if ((md = digest_sha1()) != NULL)
252 digest_nids[pos++] = EVP_MD_type(md);
253 if ((md = digest_sha256()) != NULL)
254 digest_nids[pos++] = EVP_MD_type(md);
255 if ((md = digest_sha384()) != NULL)
256 digest_nids[pos++] = EVP_MD_type(md);
257 if ((md = digest_sha512()) != NULL)
258 digest_nids[pos++] = EVP_MD_type(md);
259 digest_nids[pos] = 0;
267 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
270 static int ossltest_cipher_nids[] = {
276 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
277 const unsigned char *iv, int enc);
278 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
279 const unsigned char *in, size_t inl);
281 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
282 static const EVP_CIPHER *ossltest_aes_128_cbc(void)
284 if (_hidden_aes_128_cbc == NULL
285 && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
287 16 /* key len */)) == NULL
288 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
289 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
290 EVP_CIPH_FLAG_DEFAULT_ASN1
292 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
293 ossltest_aes128_init_key)
294 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
295 ossltest_aes128_cbc_cipher)
296 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
297 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
298 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
299 _hidden_aes_128_cbc = NULL;
301 return _hidden_aes_128_cbc;
303 static void destroy_ciphers(void)
305 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
306 _hidden_aes_128_cbc = NULL;
309 static int bind_ossltest(ENGINE *e)
311 /* Ensure the ossltest error handling is set up */
312 ERR_load_OSSLTEST_strings();
314 if (!ENGINE_set_id(e, engine_ossltest_id)
315 || !ENGINE_set_name(e, engine_ossltest_name)
316 || !ENGINE_set_digests(e, ossltest_digests)
317 || !ENGINE_set_ciphers(e, ossltest_ciphers)
318 || !ENGINE_set_destroy_function(e, ossltest_destroy)
319 || !ENGINE_set_init_function(e, ossltest_init)
320 || !ENGINE_set_finish_function(e, ossltest_finish)) {
321 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
328 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
329 static int bind_helper(ENGINE *e, const char *id)
331 if (id && (strcmp(id, engine_ossltest_id) != 0))
333 if (!bind_ossltest(e))
338 IMPLEMENT_DYNAMIC_CHECK_FN()
339 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
342 static ENGINE *engine_ossltest(void)
344 ENGINE *ret = ENGINE_new();
347 if (!bind_ossltest(ret)) {
354 void ENGINE_load_ossltest(void)
356 /* Copied from eng_[openssl|dyn].c */
357 ENGINE *toadd = engine_ossltest();
366 static int ossltest_init(ENGINE *e)
372 static int ossltest_finish(ENGINE *e)
378 static int ossltest_destroy(ENGINE *e)
382 ERR_unload_OSSLTEST_strings();
386 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
387 const int **nids, int nid)
391 /* We are returning a list of supported nids */
392 return ossltest_digest_nids(nids);
394 /* We are being asked for a specific digest */
397 *digest = digest_md5();
400 *digest = digest_sha1();
403 *digest = digest_sha256();
406 *digest = digest_sha384();
409 *digest = digest_sha512();
419 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
420 const int **nids, int nid)
424 /* We are returning a list of supported nids */
425 *nids = ossltest_cipher_nids;
426 return (sizeof(ossltest_cipher_nids) - 1)
427 / sizeof(ossltest_cipher_nids[0]);
429 /* We are being asked for a specific cipher */
431 case NID_aes_128_cbc:
432 *cipher = ossltest_aes_128_cbc();
442 static void fill_known_data(unsigned char *md, unsigned int len)
446 for (i=0; i<len; i++) {
447 md[i] = (unsigned char)(i & 0xff);
452 * MD5 implementation. We go through the motions of doing MD5 by deferring to
453 * the standard implementation. Then we overwrite the result with a will defined
454 * value, so that all "MD5" digests using the test engine always end up with
458 #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx))
459 static int digest_md5_init(EVP_MD_CTX *ctx)
461 return MD5_Init(data(ctx));
464 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
467 return MD5_Update(data(ctx), data, (size_t)count);
470 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
473 ret = MD5_Final(md, data(ctx));
476 fill_known_data(md, MD5_DIGEST_LENGTH);
482 * SHA1 implementation.
485 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
486 static int digest_sha1_init(EVP_MD_CTX *ctx)
488 return SHA1_Init(data(ctx));
491 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
494 return SHA1_Update(data(ctx), data, (size_t)count);
497 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
500 ret = SHA1_Final(md, data(ctx));
503 fill_known_data(md, SHA_DIGEST_LENGTH);
509 * SHA256 implementation.
512 #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx))
513 static int digest_sha256_init(EVP_MD_CTX *ctx)
515 return SHA256_Init(data(ctx));
518 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
521 return SHA256_Update(data(ctx), data, (size_t)count);
524 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
527 ret = SHA256_Final(md, data(ctx));
530 fill_known_data(md, SHA256_DIGEST_LENGTH);
536 * SHA384/512 implementation.
539 #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx))
540 static int digest_sha384_init(EVP_MD_CTX *ctx)
542 return SHA384_Init(data(ctx));
545 static int digest_sha512_init(EVP_MD_CTX *ctx)
547 return SHA512_Init(data(ctx));
550 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
553 return SHA512_Update(data(ctx), data, (size_t)count);
556 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
559 /* Actually uses SHA512_Final! */
560 ret = SHA512_Final(md, data(ctx));
563 fill_known_data(md, SHA384_DIGEST_LENGTH);
568 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
571 ret = SHA512_Final(md, data(ctx));
574 fill_known_data(md, SHA512_DIGEST_LENGTH);
580 * AES128 Implementation
583 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
584 const unsigned char *iv, int enc)
586 return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
589 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
590 const unsigned char *in, size_t inl)
592 unsigned char *tmpbuf;
595 tmpbuf = OPENSSL_malloc(inl);
599 /* Remember what we were asked to encrypt */
600 memcpy(tmpbuf, in, inl);
602 /* Go through the motions of encrypting it */
603 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
605 /* Throw it all away and just use the plaintext as the output */
606 memcpy(out, tmpbuf, inl);
607 OPENSSL_free(tmpbuf);