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);
90 static int ossltest_digest_nids[] = {
91 NID_md5, NID_sha1, NID_sha256, NID_sha384, NID_sha512, 0
95 static int digest_md5_init(EVP_MD_CTX *ctx);
96 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
98 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
100 static const EVP_MD digest_md5 = {
102 NID_md5WithRSAEncryption,
112 sizeof(EVP_MD *) + sizeof(MD5_CTX),
116 static int digest_sha1_init(EVP_MD_CTX *ctx);
117 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
118 unsigned long count);
119 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
121 static const EVP_MD digest_sha1 = {
123 NID_sha1WithRSAEncryption,
125 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
131 EVP_PKEY_NULL_method,
133 sizeof(EVP_MD *) + sizeof(SHA_CTX),
137 static int digest_sha256_init(EVP_MD_CTX *ctx);
138 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
139 unsigned long count);
140 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
142 static const EVP_MD digest_sha256 = {
144 NID_sha256WithRSAEncryption,
145 SHA256_DIGEST_LENGTH,
146 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
148 digest_sha256_update,
152 EVP_PKEY_NULL_method,
154 sizeof(EVP_MD *) + sizeof(SHA256_CTX),
158 static int digest_sha384_init(EVP_MD_CTX *ctx);
159 static int digest_sha512_init(EVP_MD_CTX *ctx);
160 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
161 unsigned long count);
162 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
163 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
165 static const EVP_MD digest_sha384 = {
167 NID_sha384WithRSAEncryption,
168 SHA384_DIGEST_LENGTH,
169 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
171 digest_sha512_update,
175 EVP_PKEY_NULL_method,
177 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
180 static const EVP_MD digest_sha512 = {
182 NID_sha512WithRSAEncryption,
183 SHA512_DIGEST_LENGTH,
184 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
186 digest_sha512_update,
190 EVP_PKEY_NULL_method,
192 sizeof(EVP_MD *) + sizeof(SHA512_CTX),
196 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
199 static int ossltest_cipher_nids[] = {
205 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
206 const unsigned char *iv, int enc);
207 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
208 const unsigned char *in, size_t inl);
211 * Copy of the definition in crypto/evp/e_aes.c. Only used for the "sizeof"
227 static const EVP_CIPHER ossltest_aes_128_cbc = { \
232 EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE,
233 ossltest_aes128_init_key,
234 ossltest_aes128_cbc_cipher,
241 static int bind_ossltest(ENGINE *e)
243 /* Ensure the ossltest error handling is set up */
244 ERR_load_OSSLTEST_strings();
246 if (!ENGINE_set_id(e, engine_ossltest_id)
247 || !ENGINE_set_name(e, engine_ossltest_name)
248 || !ENGINE_set_digests(e, ossltest_digests)
249 || !ENGINE_set_ciphers(e, ossltest_ciphers)
250 || !ENGINE_set_destroy_function(e, ossltest_destroy)
251 || !ENGINE_set_init_function(e, ossltest_init)
252 || !ENGINE_set_finish_function(e, ossltest_finish)) {
253 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
260 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
261 static int bind_helper(ENGINE *e, const char *id)
263 if (id && (strcmp(id, engine_ossltest_id) != 0))
265 if (!bind_ossltest(e))
270 IMPLEMENT_DYNAMIC_CHECK_FN()
271 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
274 static ENGINE *engine_ossltest(void)
276 ENGINE *ret = ENGINE_new();
279 if (!bind_ossltest(ret)) {
286 void ENGINE_load_ossltest(void)
288 /* Copied from eng_[openssl|dyn].c */
289 ENGINE *toadd = engine_ossltest();
298 static int ossltest_init(ENGINE *e)
304 static int ossltest_finish(ENGINE *e)
310 static int ossltest_destroy(ENGINE *e)
312 ERR_unload_OSSLTEST_strings();
316 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
317 const int **nids, int nid)
321 /* We are returning a list of supported nids */
322 *nids = ossltest_digest_nids;
323 return (sizeof(ossltest_digest_nids) -
324 1) / sizeof(ossltest_digest_nids[0]);
326 /* We are being asked for a specific digest */
329 *digest = &digest_md5;
332 *digest = &digest_sha1;
335 *digest = &digest_sha256;
338 *digest = &digest_sha384;
341 *digest = &digest_sha512;
351 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
352 const int **nids, int nid)
356 /* We are returning a list of supported nids */
357 *nids = ossltest_cipher_nids;
358 return (sizeof(ossltest_cipher_nids) - 1)
359 / sizeof(ossltest_cipher_nids[0]);
361 /* We are being asked for a specific cipher */
363 case NID_aes_128_cbc:
364 *cipher = &ossltest_aes_128_cbc;
374 static void fill_known_data(unsigned char *md, unsigned int len)
378 for (i=0; i<len; i++) {
379 md[i] = (unsigned char)(i & 0xff);
384 * MD5 implementation. We go through the motions of doing MD5 by deferring to
385 * the standard implementation. Then we overwrite the result with a will defined
386 * value, so that all "MD5" digests using the test engine always end up with
390 #define data(ctx) ((MD5_CTX *)(ctx)->md_data)
391 static int digest_md5_init(EVP_MD_CTX *ctx)
393 return MD5_Init(data(ctx));
396 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
399 return MD5_Update(data(ctx), data, (size_t)count);
402 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
405 ret = MD5_Final(md, data(ctx));
408 fill_known_data(md, MD5_DIGEST_LENGTH);
414 * SHA1 implementation.
417 #define data(ctx) ((SHA_CTX *)(ctx)->md_data)
418 static int digest_sha1_init(EVP_MD_CTX *ctx)
420 return SHA1_Init(data(ctx));
423 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
426 return SHA1_Update(data(ctx), data, (size_t)count);
429 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
432 ret = SHA1_Final(md, data(ctx));
435 fill_known_data(md, SHA_DIGEST_LENGTH);
441 * SHA256 implementation.
444 #define data(ctx) ((SHA256_CTX *)(ctx)->md_data)
445 static int digest_sha256_init(EVP_MD_CTX *ctx)
447 return SHA256_Init(data(ctx));
450 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
453 return SHA256_Update(data(ctx), data, (size_t)count);
456 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
459 ret = SHA256_Final(md, data(ctx));
462 fill_known_data(md, SHA256_DIGEST_LENGTH);
468 * SHA384/512 implementation.
471 #define data(ctx) ((SHA512_CTX *)(ctx)->md_data)
472 static int digest_sha384_init(EVP_MD_CTX *ctx)
474 return SHA384_Init(data(ctx));
477 static int digest_sha512_init(EVP_MD_CTX *ctx)
479 return SHA512_Init(data(ctx));
482 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
485 return SHA512_Update(data(ctx), data, (size_t)count);
488 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
491 /* Actually uses SHA512_Final! */
492 ret = SHA512_Final(md, data(ctx));
495 fill_known_data(md, SHA384_DIGEST_LENGTH);
500 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
503 ret = SHA512_Final(md, data(ctx));
506 fill_known_data(md, SHA512_DIGEST_LENGTH);
512 * AES128 Implementation
515 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
516 const unsigned char *iv, int enc)
518 return EVP_aes_128_cbc()->init(ctx, key, iv, enc);
521 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
522 const unsigned char *in, size_t inl)
524 unsigned char *tmpbuf;
527 tmpbuf = OPENSSL_malloc(inl);
531 /* Remember what we were asked to encrypt */
532 memcpy(tmpbuf, in, inl);
534 /* Go through the motions of encrypting it */
535 ret = EVP_aes_128_cbc()->do_cipher(ctx, out, in, inl);
537 /* Throw it all away and just use the plaintext as the output */
538 memcpy(out, tmpbuf, inl);
539 OPENSSL_free(tmpbuf);