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 #include "e_ossltest_err.c"
30 /* Engine Id and Name */
31 static const char *engine_ossltest_id
= "ossltest";
32 static const char *engine_ossltest_name
= "OpenSSL Test engine support";
35 /* Engine Lifetime functions */
36 static int ossltest_destroy(ENGINE
*e
);
37 static int ossltest_init(ENGINE
*e
);
38 static int ossltest_finish(ENGINE
*e
);
39 void ENGINE_load_ossltest(void);
43 static int ossltest_digests(ENGINE
*e
, const EVP_MD
**digest
,
44 const int **nids
, int nid
);
47 static int digest_md5_init(EVP_MD_CTX
*ctx
);
48 static int digest_md5_update(EVP_MD_CTX
*ctx
, const void *data
,
50 static int digest_md5_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
52 static EVP_MD
*_hidden_md5_md
= NULL
;
53 static const EVP_MD
*digest_md5(void)
55 if (_hidden_md5_md
== NULL
) {
58 if ((md
= EVP_MD_meth_new(NID_md5
, NID_md5WithRSAEncryption
)) == NULL
59 || !EVP_MD_meth_set_result_size(md
, MD5_DIGEST_LENGTH
)
60 || !EVP_MD_meth_set_input_blocksize(md
, MD5_CBLOCK
)
61 || !EVP_MD_meth_set_app_datasize(md
,
62 sizeof(EVP_MD
*) + sizeof(MD5_CTX
))
63 || !EVP_MD_meth_set_flags(md
, 0)
64 || !EVP_MD_meth_set_init(md
, digest_md5_init
)
65 || !EVP_MD_meth_set_update(md
, digest_md5_update
)
66 || !EVP_MD_meth_set_final(md
, digest_md5_final
)) {
72 return _hidden_md5_md
;
76 static int digest_sha1_init(EVP_MD_CTX
*ctx
);
77 static int digest_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
79 static int digest_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
81 static EVP_MD
*_hidden_sha1_md
= NULL
;
82 static const EVP_MD
*digest_sha1(void)
84 if (_hidden_sha1_md
== NULL
) {
87 if ((md
= EVP_MD_meth_new(NID_sha1
, NID_sha1WithRSAEncryption
)) == NULL
88 || !EVP_MD_meth_set_result_size(md
, SHA_DIGEST_LENGTH
)
89 || !EVP_MD_meth_set_input_blocksize(md
, SHA_CBLOCK
)
90 || !EVP_MD_meth_set_app_datasize(md
,
91 sizeof(EVP_MD
*) + sizeof(SHA_CTX
))
92 || !EVP_MD_meth_set_flags(md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
93 || !EVP_MD_meth_set_init(md
, digest_sha1_init
)
94 || !EVP_MD_meth_set_update(md
, digest_sha1_update
)
95 || !EVP_MD_meth_set_final(md
, digest_sha1_final
)) {
101 return _hidden_sha1_md
;
105 static int digest_sha256_init(EVP_MD_CTX
*ctx
);
106 static int digest_sha256_update(EVP_MD_CTX
*ctx
, const void *data
,
108 static int digest_sha256_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
110 static EVP_MD
*_hidden_sha256_md
= NULL
;
111 static const EVP_MD
*digest_sha256(void)
113 if (_hidden_sha256_md
== NULL
) {
116 if ((md
= EVP_MD_meth_new(NID_sha256
, NID_sha256WithRSAEncryption
)) == NULL
117 || !EVP_MD_meth_set_result_size(md
, SHA256_DIGEST_LENGTH
)
118 || !EVP_MD_meth_set_input_blocksize(md
, SHA256_CBLOCK
)
119 || !EVP_MD_meth_set_app_datasize(md
,
120 sizeof(EVP_MD
*) + sizeof(SHA256_CTX
))
121 || !EVP_MD_meth_set_flags(md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
122 || !EVP_MD_meth_set_init(md
, digest_sha256_init
)
123 || !EVP_MD_meth_set_update(md
, digest_sha256_update
)
124 || !EVP_MD_meth_set_final(md
, digest_sha256_final
)) {
125 EVP_MD_meth_free(md
);
128 _hidden_sha256_md
= md
;
130 return _hidden_sha256_md
;
134 static int digest_sha384_init(EVP_MD_CTX
*ctx
);
135 static int digest_sha512_init(EVP_MD_CTX
*ctx
);
136 static int digest_sha512_update(EVP_MD_CTX
*ctx
, const void *data
,
138 static int digest_sha384_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
139 static int digest_sha512_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
141 static EVP_MD
*_hidden_sha384_md
= NULL
;
142 static const EVP_MD
*digest_sha384(void)
144 if (_hidden_sha384_md
== NULL
) {
147 if ((md
= EVP_MD_meth_new(NID_sha384
, NID_sha384WithRSAEncryption
)) == NULL
148 || !EVP_MD_meth_set_result_size(md
, SHA384_DIGEST_LENGTH
)
149 || !EVP_MD_meth_set_input_blocksize(md
, SHA512_CBLOCK
)
150 || !EVP_MD_meth_set_app_datasize(md
,
151 sizeof(EVP_MD
*) + sizeof(SHA512_CTX
))
152 || !EVP_MD_meth_set_flags(md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
153 || !EVP_MD_meth_set_init(md
, digest_sha384_init
)
154 || !EVP_MD_meth_set_update(md
, digest_sha512_update
)
155 || !EVP_MD_meth_set_final(md
, digest_sha384_final
)) {
156 EVP_MD_meth_free(md
);
159 _hidden_sha384_md
= md
;
161 return _hidden_sha384_md
;
163 static EVP_MD
*_hidden_sha512_md
= NULL
;
164 static const EVP_MD
*digest_sha512(void)
166 if (_hidden_sha512_md
== NULL
) {
169 if ((md
= EVP_MD_meth_new(NID_sha512
, NID_sha512WithRSAEncryption
)) == NULL
170 || !EVP_MD_meth_set_result_size(md
, SHA512_DIGEST_LENGTH
)
171 || !EVP_MD_meth_set_input_blocksize(md
, SHA512_CBLOCK
)
172 || !EVP_MD_meth_set_app_datasize(md
,
173 sizeof(EVP_MD
*) + sizeof(SHA512_CTX
))
174 || !EVP_MD_meth_set_flags(md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
175 || !EVP_MD_meth_set_init(md
, digest_sha512_init
)
176 || !EVP_MD_meth_set_update(md
, digest_sha512_update
)
177 || !EVP_MD_meth_set_final(md
, digest_sha512_final
)) {
178 EVP_MD_meth_free(md
);
181 _hidden_sha512_md
= md
;
183 return _hidden_sha512_md
;
185 static void destroy_digests(void)
187 EVP_MD_meth_free(_hidden_md5_md
);
188 _hidden_md5_md
= NULL
;
189 EVP_MD_meth_free(_hidden_sha1_md
);
190 _hidden_sha1_md
= NULL
;
191 EVP_MD_meth_free(_hidden_sha256_md
);
192 _hidden_sha256_md
= NULL
;
193 EVP_MD_meth_free(_hidden_sha384_md
);
194 _hidden_sha384_md
= NULL
;
195 EVP_MD_meth_free(_hidden_sha512_md
);
196 _hidden_sha512_md
= NULL
;
198 static int ossltest_digest_nids(const int **nids
)
200 static int digest_nids
[6] = { 0, 0, 0, 0, 0, 0 };
206 if ((md
= digest_md5()) != NULL
)
207 digest_nids
[pos
++] = EVP_MD_type(md
);
208 if ((md
= digest_sha1()) != NULL
)
209 digest_nids
[pos
++] = EVP_MD_type(md
);
210 if ((md
= digest_sha256()) != NULL
)
211 digest_nids
[pos
++] = EVP_MD_type(md
);
212 if ((md
= digest_sha384()) != NULL
)
213 digest_nids
[pos
++] = EVP_MD_type(md
);
214 if ((md
= digest_sha512()) != NULL
)
215 digest_nids
[pos
++] = EVP_MD_type(md
);
216 digest_nids
[pos
] = 0;
224 static int ossltest_ciphers(ENGINE
*, const EVP_CIPHER
**,
227 static int ossltest_cipher_nids
[] = {
228 NID_aes_128_cbc
, NID_aes_128_gcm
, 0
233 int ossltest_aes128_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
234 const unsigned char *iv
, int enc
);
235 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
236 const unsigned char *in
, size_t inl
);
237 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
238 const unsigned char *iv
, int enc
);
239 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
240 const unsigned char *in
, size_t inl
);
241 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
244 static EVP_CIPHER
*_hidden_aes_128_cbc
= NULL
;
245 static const EVP_CIPHER
*ossltest_aes_128_cbc(void)
247 if (_hidden_aes_128_cbc
== NULL
248 && ((_hidden_aes_128_cbc
= EVP_CIPHER_meth_new(NID_aes_128_cbc
,
250 16 /* key len */)) == NULL
251 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc
,16)
252 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc
,
253 EVP_CIPH_FLAG_DEFAULT_ASN1
255 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc
,
256 ossltest_aes128_init_key
)
257 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc
,
258 ossltest_aes128_cbc_cipher
)
259 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc
,
260 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
261 EVP_CIPHER_meth_free(_hidden_aes_128_cbc
);
262 _hidden_aes_128_cbc
= NULL
;
264 return _hidden_aes_128_cbc
;
266 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 \
274 static const EVP_CIPHER
*ossltest_aes_128_gcm(void)
276 if (_hidden_aes_128_gcm
== NULL
277 && ((_hidden_aes_128_gcm
= EVP_CIPHER_meth_new(NID_aes_128_gcm
,
279 16 /* key len */)) == NULL
280 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_gcm
,12)
281 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_gcm
, AES_GCM_FLAGS
)
282 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_gcm
,
283 ossltest_aes128_gcm_init_key
)
284 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_gcm
,
285 ossltest_aes128_gcm_cipher
)
286 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_gcm
,
287 ossltest_aes128_gcm_ctrl
)
288 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_gcm
,
289 EVP_CIPHER_impl_ctx_size(EVP_aes_128_gcm())))) {
290 EVP_CIPHER_meth_free(_hidden_aes_128_gcm
);
291 _hidden_aes_128_gcm
= NULL
;
293 return _hidden_aes_128_gcm
;
296 static void destroy_ciphers(void)
298 EVP_CIPHER_meth_free(_hidden_aes_128_cbc
);
299 EVP_CIPHER_meth_free(_hidden_aes_128_gcm
);
300 _hidden_aes_128_cbc
= NULL
;
303 static int bind_ossltest(ENGINE
*e
)
305 /* Ensure the ossltest error handling is set up */
306 ERR_load_OSSLTEST_strings();
308 if (!ENGINE_set_id(e
, engine_ossltest_id
)
309 || !ENGINE_set_name(e
, engine_ossltest_name
)
310 || !ENGINE_set_digests(e
, ossltest_digests
)
311 || !ENGINE_set_ciphers(e
, ossltest_ciphers
)
312 || !ENGINE_set_destroy_function(e
, ossltest_destroy
)
313 || !ENGINE_set_init_function(e
, ossltest_init
)
314 || !ENGINE_set_finish_function(e
, ossltest_finish
)) {
315 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST
, OSSLTEST_R_INIT_FAILED
);
322 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
323 static int bind_helper(ENGINE
*e
, const char *id
)
325 if (id
&& (strcmp(id
, engine_ossltest_id
) != 0))
327 if (!bind_ossltest(e
))
332 IMPLEMENT_DYNAMIC_CHECK_FN()
333 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
336 static ENGINE
*engine_ossltest(void)
338 ENGINE
*ret
= ENGINE_new();
341 if (!bind_ossltest(ret
)) {
348 void ENGINE_load_ossltest(void)
350 /* Copied from eng_[openssl|dyn].c */
351 ENGINE
*toadd
= engine_ossltest();
360 static int ossltest_init(ENGINE
*e
)
366 static int ossltest_finish(ENGINE
*e
)
372 static int ossltest_destroy(ENGINE
*e
)
376 ERR_unload_OSSLTEST_strings();
380 static int ossltest_digests(ENGINE
*e
, const EVP_MD
**digest
,
381 const int **nids
, int nid
)
385 /* We are returning a list of supported nids */
386 return ossltest_digest_nids(nids
);
388 /* We are being asked for a specific digest */
391 *digest
= digest_md5();
394 *digest
= digest_sha1();
397 *digest
= digest_sha256();
400 *digest
= digest_sha384();
403 *digest
= digest_sha512();
413 static int ossltest_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
414 const int **nids
, int nid
)
418 /* We are returning a list of supported nids */
419 *nids
= ossltest_cipher_nids
;
420 return (sizeof(ossltest_cipher_nids
) - 1)
421 / sizeof(ossltest_cipher_nids
[0]);
423 /* We are being asked for a specific cipher */
425 case NID_aes_128_cbc
:
426 *cipher
= ossltest_aes_128_cbc();
428 case NID_aes_128_gcm
:
429 *cipher
= ossltest_aes_128_gcm();
439 static void fill_known_data(unsigned char *md
, unsigned int len
)
443 for (i
=0; i
<len
; i
++) {
444 md
[i
] = (unsigned char)(i
& 0xff);
449 * MD5 implementation. We go through the motions of doing MD5 by deferring to
450 * the standard implementation. Then we overwrite the result with a will defined
451 * value, so that all "MD5" digests using the test engine always end up with
455 #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx))
456 static int digest_md5_init(EVP_MD_CTX
*ctx
)
458 return MD5_Init(data(ctx
));
461 static int digest_md5_update(EVP_MD_CTX
*ctx
, const void *data
,
464 return MD5_Update(data(ctx
), data
, (size_t)count
);
467 static int digest_md5_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
470 ret
= MD5_Final(md
, data(ctx
));
473 fill_known_data(md
, MD5_DIGEST_LENGTH
);
479 * SHA1 implementation.
482 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
483 static int digest_sha1_init(EVP_MD_CTX
*ctx
)
485 return SHA1_Init(data(ctx
));
488 static int digest_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
491 return SHA1_Update(data(ctx
), data
, (size_t)count
);
494 static int digest_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
497 ret
= SHA1_Final(md
, data(ctx
));
500 fill_known_data(md
, SHA_DIGEST_LENGTH
);
506 * SHA256 implementation.
509 #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx))
510 static int digest_sha256_init(EVP_MD_CTX
*ctx
)
512 return SHA256_Init(data(ctx
));
515 static int digest_sha256_update(EVP_MD_CTX
*ctx
, const void *data
,
518 return SHA256_Update(data(ctx
), data
, (size_t)count
);
521 static int digest_sha256_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
524 ret
= SHA256_Final(md
, data(ctx
));
527 fill_known_data(md
, SHA256_DIGEST_LENGTH
);
533 * SHA384/512 implementation.
536 #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx))
537 static int digest_sha384_init(EVP_MD_CTX
*ctx
)
539 return SHA384_Init(data(ctx
));
542 static int digest_sha512_init(EVP_MD_CTX
*ctx
)
544 return SHA512_Init(data(ctx
));
547 static int digest_sha512_update(EVP_MD_CTX
*ctx
, const void *data
,
550 return SHA512_Update(data(ctx
), data
, (size_t)count
);
553 static int digest_sha384_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
556 /* Actually uses SHA512_Final! */
557 ret
= SHA512_Final(md
, data(ctx
));
560 fill_known_data(md
, SHA384_DIGEST_LENGTH
);
565 static int digest_sha512_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
568 ret
= SHA512_Final(md
, data(ctx
));
571 fill_known_data(md
, SHA512_DIGEST_LENGTH
);
577 * AES128 Implementation
580 int ossltest_aes128_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
581 const unsigned char *iv
, int enc
)
583 return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx
, key
, iv
, enc
);
586 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
587 const unsigned char *in
, size_t inl
)
589 unsigned char *tmpbuf
;
592 tmpbuf
= OPENSSL_malloc(inl
);
596 /* Remember what we were asked to encrypt */
597 memcpy(tmpbuf
, in
, inl
);
599 /* Go through the motions of encrypting it */
600 ret
= EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx
, out
, in
, inl
);
602 /* Throw it all away and just use the plaintext as the output */
603 memcpy(out
, tmpbuf
, inl
);
604 OPENSSL_free(tmpbuf
);
609 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
610 const unsigned char *iv
, int enc
)
612 return EVP_CIPHER_meth_get_init(EVP_aes_128_gcm()) (ctx
, key
, iv
, enc
);
616 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
617 const unsigned char *in
, size_t inl
)
619 unsigned char *tmpbuf
= OPENSSL_malloc(inl
);
621 /* OPENSSL_malloc will return NULL if inl == 0 */
622 if (tmpbuf
== NULL
&& inl
> 0)
625 /* Remember what we were asked to encrypt */
626 memcpy(tmpbuf
, in
, inl
);
628 /* Go through the motions of encrypting it */
629 EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_gcm())(ctx
, out
, in
, inl
);
631 /* Throw it all away and just use the plaintext as the output */
632 memcpy(out
, tmpbuf
, inl
);
633 OPENSSL_free(tmpbuf
);
638 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
641 /* Pass the ctrl down */
642 int ret
= EVP_CIPHER_meth_get_ctrl(EVP_aes_128_gcm())(ctx
, type
, arg
, ptr
);
648 case EVP_CTRL_AEAD_GET_TAG
:
649 /* Always give the same tag */
650 memset(ptr
, 0, EVP_GCM_TLS_TAG_LEN
);