1 /* engines/e_ossltest.c */
3 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
55 * This is the OSSLTEST engine. It provides deliberately crippled digest
56 * implementations for test purposes. It is highly insecure and must NOT be
57 * used for any purpose except testing
63 #include <openssl/engine.h>
64 #include <openssl/sha.h>
65 #include <openssl/md5.h>
66 #include <openssl/rsa.h>
67 #include <openssl/evp.h>
68 #include <openssl/modes.h>
69 #include <openssl/aes.h>
71 #define OSSLTEST_LIB_NAME "OSSLTEST"
72 #include "e_ossltest_err.c"
74 /* Engine Id and Name */
75 static const char *engine_ossltest_id = "ossltest";
76 static const char *engine_ossltest_name = "OpenSSL Test engine support";
79 /* Engine Lifetime functions */
80 static int ossltest_destroy(ENGINE *e);
81 static int ossltest_init(ENGINE *e);
82 static int ossltest_finish(ENGINE *e);
83 void ENGINE_load_ossltest(void);
87 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
88 const int **nids, int nid);
91 static int digest_md5_init(EVP_MD_CTX *ctx);
92 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
94 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
96 static EVP_MD *_hidden_md5_md = NULL;
97 static const EVP_MD *digest_md5(void)
99 if (_hidden_md5_md == NULL) {
102 if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
103 || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
104 || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
105 || !EVP_MD_meth_set_app_datasize(md,
106 sizeof(EVP_MD *) + sizeof(MD5_CTX))
107 || !EVP_MD_meth_set_flags(md, 0)
108 || !EVP_MD_meth_set_init(md, digest_md5_init)
109 || !EVP_MD_meth_set_update(md, digest_md5_update)
110 || !EVP_MD_meth_set_final(md, digest_md5_final)) {
111 EVP_MD_meth_free(md);
116 return _hidden_md5_md;
120 static int digest_sha1_init(EVP_MD_CTX *ctx);
121 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
123 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
125 static EVP_MD *_hidden_sha1_md = NULL;
126 static const EVP_MD *digest_sha1(void)
128 if (_hidden_sha1_md == NULL) {
131 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
132 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
133 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
134 || !EVP_MD_meth_set_app_datasize(md,
135 sizeof(EVP_MD *) + sizeof(SHA_CTX))
136 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
137 || !EVP_MD_meth_set_init(md, digest_sha1_init)
138 || !EVP_MD_meth_set_update(md, digest_sha1_update)
139 || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
140 EVP_MD_meth_free(md);
143 _hidden_sha1_md = md;
145 return _hidden_sha1_md;
149 static int digest_sha256_init(EVP_MD_CTX *ctx);
150 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
152 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
154 static EVP_MD *_hidden_sha256_md = NULL;
155 static const EVP_MD *digest_sha256(void)
157 if (_hidden_sha256_md == NULL) {
160 if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
161 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
162 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
163 || !EVP_MD_meth_set_app_datasize(md,
164 sizeof(EVP_MD *) + sizeof(SHA256_CTX))
165 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
166 || !EVP_MD_meth_set_init(md, digest_sha256_init)
167 || !EVP_MD_meth_set_update(md, digest_sha256_update)
168 || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
169 EVP_MD_meth_free(md);
172 _hidden_sha256_md = md;
174 return _hidden_sha256_md;
178 static int digest_sha384_init(EVP_MD_CTX *ctx);
179 static int digest_sha512_init(EVP_MD_CTX *ctx);
180 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
182 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
183 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
185 static EVP_MD *_hidden_sha384_md = NULL;
186 static const EVP_MD *digest_sha384(void)
188 if (_hidden_sha384_md == NULL) {
191 if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
192 || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
193 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
194 || !EVP_MD_meth_set_app_datasize(md,
195 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
196 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
197 || !EVP_MD_meth_set_init(md, digest_sha384_init)
198 || !EVP_MD_meth_set_update(md, digest_sha512_update)
199 || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
200 EVP_MD_meth_free(md);
203 _hidden_sha384_md = md;
205 return _hidden_sha384_md;
207 static EVP_MD *_hidden_sha512_md = NULL;
208 static const EVP_MD *digest_sha512(void)
210 if (_hidden_sha512_md == NULL) {
213 if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
214 || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
215 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
216 || !EVP_MD_meth_set_app_datasize(md,
217 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
218 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
219 || !EVP_MD_meth_set_init(md, digest_sha512_init)
220 || !EVP_MD_meth_set_update(md, digest_sha512_update)
221 || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
222 EVP_MD_meth_free(md);
225 _hidden_sha512_md = md;
227 return _hidden_sha512_md;
229 static void destroy_digests(void)
231 EVP_MD_meth_free(_hidden_md5_md);
232 _hidden_md5_md = NULL;
233 EVP_MD_meth_free(_hidden_sha1_md);
234 _hidden_sha1_md = NULL;
235 EVP_MD_meth_free(_hidden_sha256_md);
236 _hidden_sha256_md = NULL;
237 EVP_MD_meth_free(_hidden_sha384_md);
238 _hidden_sha384_md = NULL;
239 EVP_MD_meth_free(_hidden_sha512_md);
240 _hidden_sha512_md = NULL;
242 static int ossltest_digest_nids(const int **nids)
244 static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
250 if ((md = digest_md5()) != NULL)
251 digest_nids[pos++] = EVP_MD_type(md);
252 if ((md = digest_sha1()) != NULL)
253 digest_nids[pos++] = EVP_MD_type(md);
254 if ((md = digest_sha256()) != NULL)
255 digest_nids[pos++] = EVP_MD_type(md);
256 if ((md = digest_sha384()) != NULL)
257 digest_nids[pos++] = EVP_MD_type(md);
258 if ((md = digest_sha512()) != NULL)
259 digest_nids[pos++] = EVP_MD_type(md);
260 digest_nids[pos] = 0;
268 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
271 static int ossltest_cipher_nids[] = {
277 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
278 const unsigned char *iv, int enc);
279 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
280 const unsigned char *in, size_t inl);
282 static const EVP_CIPHER ossltest_aes_128_cbc = { \
287 EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE,
288 ossltest_aes128_init_key,
289 ossltest_aes128_cbc_cipher,
291 0, /* We don't know the size of cipher_data at compile time */
296 static int bind_ossltest(ENGINE *e)
298 /* Ensure the ossltest error handling is set up */
299 ERR_load_OSSLTEST_strings();
301 if (!ENGINE_set_id(e, engine_ossltest_id)
302 || !ENGINE_set_name(e, engine_ossltest_name)
303 || !ENGINE_set_digests(e, ossltest_digests)
304 || !ENGINE_set_ciphers(e, ossltest_ciphers)
305 || !ENGINE_set_destroy_function(e, ossltest_destroy)
306 || !ENGINE_set_init_function(e, ossltest_init)
307 || !ENGINE_set_finish_function(e, ossltest_finish)) {
308 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
315 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
316 static int bind_helper(ENGINE *e, const char *id)
318 if (id && (strcmp(id, engine_ossltest_id) != 0))
320 if (!bind_ossltest(e))
325 IMPLEMENT_DYNAMIC_CHECK_FN()
326 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
329 static ENGINE *engine_ossltest(void)
331 ENGINE *ret = ENGINE_new();
334 if (!bind_ossltest(ret)) {
341 void ENGINE_load_ossltest(void)
343 /* Copied from eng_[openssl|dyn].c */
344 ENGINE *toadd = engine_ossltest();
353 static int ossltest_init(ENGINE *e)
359 static int ossltest_finish(ENGINE *e)
365 static int ossltest_destroy(ENGINE *e)
368 ERR_unload_OSSLTEST_strings();
372 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
373 const int **nids, int nid)
377 /* We are returning a list of supported nids */
378 return ossltest_digest_nids(nids);
380 /* We are being asked for a specific digest */
383 *digest = digest_md5();
386 *digest = digest_sha1();
389 *digest = digest_sha256();
392 *digest = digest_sha384();
395 *digest = digest_sha512();
405 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
406 const int **nids, int nid)
410 /* We are returning a list of supported nids */
411 *nids = ossltest_cipher_nids;
412 return (sizeof(ossltest_cipher_nids) - 1)
413 / sizeof(ossltest_cipher_nids[0]);
415 /* We are being asked for a specific cipher */
417 case NID_aes_128_cbc:
418 *cipher = &ossltest_aes_128_cbc;
428 static void fill_known_data(unsigned char *md, unsigned int len)
432 for (i=0; i<len; i++) {
433 md[i] = (unsigned char)(i & 0xff);
438 * MD5 implementation. We go through the motions of doing MD5 by deferring to
439 * the standard implementation. Then we overwrite the result with a will defined
440 * value, so that all "MD5" digests using the test engine always end up with
444 #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx))
445 static int digest_md5_init(EVP_MD_CTX *ctx)
447 return MD5_Init(data(ctx));
450 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
453 return MD5_Update(data(ctx), data, (size_t)count);
456 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
459 ret = MD5_Final(md, data(ctx));
462 fill_known_data(md, MD5_DIGEST_LENGTH);
468 * SHA1 implementation.
471 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
472 static int digest_sha1_init(EVP_MD_CTX *ctx)
474 return SHA1_Init(data(ctx));
477 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
480 return SHA1_Update(data(ctx), data, (size_t)count);
483 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
486 ret = SHA1_Final(md, data(ctx));
489 fill_known_data(md, SHA_DIGEST_LENGTH);
495 * SHA256 implementation.
498 #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx))
499 static int digest_sha256_init(EVP_MD_CTX *ctx)
501 return SHA256_Init(data(ctx));
504 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
507 return SHA256_Update(data(ctx), data, (size_t)count);
510 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
513 ret = SHA256_Final(md, data(ctx));
516 fill_known_data(md, SHA256_DIGEST_LENGTH);
522 * SHA384/512 implementation.
525 #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx))
526 static int digest_sha384_init(EVP_MD_CTX *ctx)
528 return SHA384_Init(data(ctx));
531 static int digest_sha512_init(EVP_MD_CTX *ctx)
533 return SHA512_Init(data(ctx));
536 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
539 return SHA512_Update(data(ctx), data, (size_t)count);
542 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
545 /* Actually uses SHA512_Final! */
546 ret = SHA512_Final(md, data(ctx));
549 fill_known_data(md, SHA384_DIGEST_LENGTH);
554 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
557 ret = SHA512_Final(md, data(ctx));
560 fill_known_data(md, SHA512_DIGEST_LENGTH);
566 * AES128 Implementation
569 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
570 const unsigned char *iv, int enc)
572 if (ctx->cipher_data == NULL) {
574 * Normally cipher_data is allocated automatically for an engine but
575 * we don't know the ctx_size as compile time so we have to do it at
578 ctx->cipher_data = OPENSSL_zalloc(EVP_aes_128_cbc()->ctx_size);
579 if (ctx->cipher_data == NULL) {
580 OSSLTESTerr(OSSLTEST_F_OSSLTEST_AES128_INIT_KEY,
581 ERR_R_MALLOC_FAILURE);
585 return EVP_aes_128_cbc()->init(ctx, key, iv, enc);
588 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
589 const unsigned char *in, size_t inl)
591 unsigned char *tmpbuf;
594 tmpbuf = OPENSSL_malloc(inl);
598 /* Remember what we were asked to encrypt */
599 memcpy(tmpbuf, in, inl);
601 /* Go through the motions of encrypting it */
602 ret = EVP_aes_128_cbc()->do_cipher(ctx, out, in, inl);
604 /* Throw it all away and just use the plaintext as the output */
605 memcpy(out, tmpbuf, inl);
606 OPENSSL_free(tmpbuf);