2 * Copyright 2015-2021 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
16 /* We need to use some engine deprecated APIs */
17 #define OPENSSL_SUPPRESS_DEPRECATED
20 * SHA low level APIs are deprecated for public use, but still ok for
21 * internal use. Note, that due to symbols not being exported, only the
22 * #defines and type definitions can be accessed, function calls are not
23 * available. The digest lengths, block sizes and sizeof(CTX) are used herein
24 * for several different digests.
26 #include "internal/deprecated.h"
31 #include <openssl/engine.h>
32 #include <openssl/sha.h>
33 #include <openssl/md5.h>
34 #include <openssl/rsa.h>
35 #include <openssl/evp.h>
36 #include <openssl/modes.h>
37 #include <openssl/aes.h>
38 #include <openssl/rand.h>
39 #include <openssl/crypto.h>
40 #include <openssl/pem.h>
41 #include <crypto/evp.h>
43 #include "e_ossltest_err.c"
46 # define strncasecmp _strnicmp
49 /* Engine Id and Name */
50 static const char *engine_ossltest_id
= "ossltest";
51 static const char *engine_ossltest_name
= "OpenSSL Test engine support";
54 /* Engine Lifetime functions */
55 static int ossltest_destroy(ENGINE
*e
);
56 static int ossltest_init(ENGINE
*e
);
57 static int ossltest_finish(ENGINE
*e
);
58 void ENGINE_load_ossltest(void);
62 static int ossltest_digests(ENGINE
*e
, const EVP_MD
**digest
,
63 const int **nids
, int nid
);
64 static const RAND_METHOD
*ossltest_rand_method(void);
67 static int digest_md5_init(EVP_MD_CTX
*ctx
);
68 static int digest_md5_update(EVP_MD_CTX
*ctx
, const void *data
,
70 static int digest_md5_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
72 static EVP_MD
*_hidden_md5_md
= NULL
;
73 static const EVP_MD
*digest_md5(void)
75 if (_hidden_md5_md
== NULL
) {
78 if ((md
= EVP_MD_meth_new(NID_md5
, NID_md5WithRSAEncryption
)) == NULL
79 || !EVP_MD_meth_set_result_size(md
, MD5_DIGEST_LENGTH
)
80 || !EVP_MD_meth_set_input_blocksize(md
, MD5_CBLOCK
)
81 || !EVP_MD_meth_set_app_datasize(md
,
82 sizeof(EVP_MD
*) + sizeof(MD5_CTX
))
83 || !EVP_MD_meth_set_flags(md
, 0)
84 || !EVP_MD_meth_set_init(md
, digest_md5_init
)
85 || !EVP_MD_meth_set_update(md
, digest_md5_update
)
86 || !EVP_MD_meth_set_final(md
, digest_md5_final
)) {
92 return _hidden_md5_md
;
96 static int digest_sha1_init(EVP_MD_CTX
*ctx
);
97 static int digest_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
99 static int digest_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
101 static EVP_MD
*_hidden_sha1_md
= NULL
;
102 static const EVP_MD
*digest_sha1(void)
104 if (_hidden_sha1_md
== NULL
) {
107 if ((md
= EVP_MD_meth_new(NID_sha1
, NID_sha1WithRSAEncryption
)) == NULL
108 || !EVP_MD_meth_set_result_size(md
, SHA_DIGEST_LENGTH
)
109 || !EVP_MD_meth_set_input_blocksize(md
, SHA_CBLOCK
)
110 || !EVP_MD_meth_set_app_datasize(md
,
111 sizeof(EVP_MD
*) + sizeof(SHA_CTX
))
112 || !EVP_MD_meth_set_flags(md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
113 || !EVP_MD_meth_set_init(md
, digest_sha1_init
)
114 || !EVP_MD_meth_set_update(md
, digest_sha1_update
)
115 || !EVP_MD_meth_set_final(md
, digest_sha1_final
)) {
116 EVP_MD_meth_free(md
);
119 _hidden_sha1_md
= md
;
121 return _hidden_sha1_md
;
125 static int digest_sha256_init(EVP_MD_CTX
*ctx
);
126 static int digest_sha256_update(EVP_MD_CTX
*ctx
, const void *data
,
128 static int digest_sha256_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
130 static EVP_MD
*_hidden_sha256_md
= NULL
;
131 static const EVP_MD
*digest_sha256(void)
133 if (_hidden_sha256_md
== NULL
) {
136 if ((md
= EVP_MD_meth_new(NID_sha256
, NID_sha256WithRSAEncryption
)) == NULL
137 || !EVP_MD_meth_set_result_size(md
, SHA256_DIGEST_LENGTH
)
138 || !EVP_MD_meth_set_input_blocksize(md
, SHA256_CBLOCK
)
139 || !EVP_MD_meth_set_app_datasize(md
,
140 sizeof(EVP_MD
*) + sizeof(SHA256_CTX
))
141 || !EVP_MD_meth_set_flags(md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
142 || !EVP_MD_meth_set_init(md
, digest_sha256_init
)
143 || !EVP_MD_meth_set_update(md
, digest_sha256_update
)
144 || !EVP_MD_meth_set_final(md
, digest_sha256_final
)) {
145 EVP_MD_meth_free(md
);
148 _hidden_sha256_md
= md
;
150 return _hidden_sha256_md
;
154 static int digest_sha384_init(EVP_MD_CTX
*ctx
);
155 static int digest_sha384_update(EVP_MD_CTX
*ctx
, const void *data
,
157 static int digest_sha384_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
159 static int digest_sha512_init(EVP_MD_CTX
*ctx
);
160 static int digest_sha512_update(EVP_MD_CTX
*ctx
, const void *data
,
162 static int digest_sha512_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
164 static EVP_MD
*_hidden_sha384_md
= NULL
;
165 static const EVP_MD
*digest_sha384(void)
167 if (_hidden_sha384_md
== NULL
) {
170 if ((md
= EVP_MD_meth_new(NID_sha384
, NID_sha384WithRSAEncryption
)) == NULL
171 || !EVP_MD_meth_set_result_size(md
, SHA384_DIGEST_LENGTH
)
172 || !EVP_MD_meth_set_input_blocksize(md
, SHA512_CBLOCK
)
173 || !EVP_MD_meth_set_app_datasize(md
,
174 sizeof(EVP_MD
*) + sizeof(SHA512_CTX
))
175 || !EVP_MD_meth_set_flags(md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
176 || !EVP_MD_meth_set_init(md
, digest_sha384_init
)
177 || !EVP_MD_meth_set_update(md
, digest_sha384_update
)
178 || !EVP_MD_meth_set_final(md
, digest_sha384_final
)) {
179 EVP_MD_meth_free(md
);
182 _hidden_sha384_md
= md
;
184 return _hidden_sha384_md
;
186 static EVP_MD
*_hidden_sha512_md
= NULL
;
187 static const EVP_MD
*digest_sha512(void)
189 if (_hidden_sha512_md
== NULL
) {
192 if ((md
= EVP_MD_meth_new(NID_sha512
, NID_sha512WithRSAEncryption
)) == NULL
193 || !EVP_MD_meth_set_result_size(md
, SHA512_DIGEST_LENGTH
)
194 || !EVP_MD_meth_set_input_blocksize(md
, SHA512_CBLOCK
)
195 || !EVP_MD_meth_set_app_datasize(md
,
196 sizeof(EVP_MD
*) + sizeof(SHA512_CTX
))
197 || !EVP_MD_meth_set_flags(md
, EVP_MD_FLAG_DIGALGID_ABSENT
)
198 || !EVP_MD_meth_set_init(md
, digest_sha512_init
)
199 || !EVP_MD_meth_set_update(md
, digest_sha512_update
)
200 || !EVP_MD_meth_set_final(md
, digest_sha512_final
)) {
201 EVP_MD_meth_free(md
);
204 _hidden_sha512_md
= md
;
206 return _hidden_sha512_md
;
208 static void destroy_digests(void)
210 EVP_MD_meth_free(_hidden_md5_md
);
211 _hidden_md5_md
= NULL
;
212 EVP_MD_meth_free(_hidden_sha1_md
);
213 _hidden_sha1_md
= NULL
;
214 EVP_MD_meth_free(_hidden_sha256_md
);
215 _hidden_sha256_md
= NULL
;
216 EVP_MD_meth_free(_hidden_sha384_md
);
217 _hidden_sha384_md
= NULL
;
218 EVP_MD_meth_free(_hidden_sha512_md
);
219 _hidden_sha512_md
= NULL
;
221 static int ossltest_digest_nids(const int **nids
)
223 static int digest_nids
[6] = { 0, 0, 0, 0, 0, 0 };
229 if ((md
= digest_md5()) != NULL
)
230 digest_nids
[pos
++] = EVP_MD_get_type(md
);
231 if ((md
= digest_sha1()) != NULL
)
232 digest_nids
[pos
++] = EVP_MD_get_type(md
);
233 if ((md
= digest_sha256()) != NULL
)
234 digest_nids
[pos
++] = EVP_MD_get_type(md
);
235 if ((md
= digest_sha384()) != NULL
)
236 digest_nids
[pos
++] = EVP_MD_get_type(md
);
237 if ((md
= digest_sha512()) != NULL
)
238 digest_nids
[pos
++] = EVP_MD_get_type(md
);
239 digest_nids
[pos
] = 0;
247 static int ossltest_ciphers(ENGINE
*, const EVP_CIPHER
**,
250 static int ossltest_cipher_nids
[] = {
251 NID_aes_128_cbc
, NID_aes_128_gcm
,
252 NID_aes_128_cbc_hmac_sha1
, 0
257 static int ossltest_aes128_init_key(EVP_CIPHER_CTX
*ctx
,
258 const unsigned char *key
,
259 const unsigned char *iv
, int enc
);
260 static int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
261 const unsigned char *in
, size_t inl
);
262 static int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX
*ctx
,
263 const unsigned char *key
,
264 const unsigned char *iv
, int enc
);
265 static int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
266 const unsigned char *in
, size_t inl
);
267 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
269 static int ossltest_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX
*ctx
,
270 const unsigned char *key
,
271 const unsigned char *iv
,
273 static int ossltest_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX
*ctx
,
275 const unsigned char *in
,
277 static int ossltest_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX
*ctx
, int type
,
281 size_t payload_length
; /* AAD length in decrypt case */
282 unsigned int tls_ver
;
285 static EVP_CIPHER
*_hidden_aes_128_cbc
= NULL
;
286 static const EVP_CIPHER
*ossltest_aes_128_cbc(void)
288 if (_hidden_aes_128_cbc
== NULL
289 && ((_hidden_aes_128_cbc
= EVP_CIPHER_meth_new(NID_aes_128_cbc
,
291 16 /* key len */)) == NULL
292 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc
,16)
293 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc
,
294 EVP_CIPH_FLAG_DEFAULT_ASN1
296 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc
,
297 ossltest_aes128_init_key
)
298 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc
,
299 ossltest_aes128_cbc_cipher
)
300 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc
,
301 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
302 EVP_CIPHER_meth_free(_hidden_aes_128_cbc
);
303 _hidden_aes_128_cbc
= NULL
;
305 return _hidden_aes_128_cbc
;
308 static EVP_CIPHER
*_hidden_aes_128_gcm
= NULL
;
310 #define AES_GCM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
311 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
312 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
313 | EVP_CIPH_CUSTOM_COPY |EVP_CIPH_FLAG_AEAD_CIPHER \
316 static const EVP_CIPHER
*ossltest_aes_128_gcm(void)
318 if (_hidden_aes_128_gcm
== NULL
319 && ((_hidden_aes_128_gcm
= EVP_CIPHER_meth_new(NID_aes_128_gcm
,
321 16 /* key len */)) == NULL
322 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_gcm
,12)
323 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_gcm
, AES_GCM_FLAGS
)
324 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_gcm
,
325 ossltest_aes128_gcm_init_key
)
326 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_gcm
,
327 ossltest_aes128_gcm_cipher
)
328 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_gcm
,
329 ossltest_aes128_gcm_ctrl
)
330 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_gcm
,
331 EVP_CIPHER_impl_ctx_size(EVP_aes_128_gcm())))) {
332 EVP_CIPHER_meth_free(_hidden_aes_128_gcm
);
333 _hidden_aes_128_gcm
= NULL
;
335 return _hidden_aes_128_gcm
;
338 static EVP_CIPHER
*_hidden_aes_128_cbc_hmac_sha1
= NULL
;
340 static const EVP_CIPHER
*ossltest_aes_128_cbc_hmac_sha1(void)
342 if (_hidden_aes_128_cbc_hmac_sha1
== NULL
343 && ((_hidden_aes_128_cbc_hmac_sha1
344 = EVP_CIPHER_meth_new(NID_aes_128_cbc_hmac_sha1
,
346 16 /* key len */)) == NULL
347 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1
,16)
348 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1
,
349 EVP_CIPH_CBC_MODE
| EVP_CIPH_FLAG_DEFAULT_ASN1
|
350 EVP_CIPH_FLAG_AEAD_CIPHER
)
351 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1
,
352 ossltest_aes128_cbc_hmac_sha1_init_key
)
353 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1
,
354 ossltest_aes128_cbc_hmac_sha1_cipher
)
355 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1
,
356 ossltest_aes128_cbc_hmac_sha1_ctrl
)
357 || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_128_cbc_hmac_sha1
,
358 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_set_asn1_iv
)
359 || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_128_cbc_hmac_sha1
,
360 EVP_CIPH_FLAG_DEFAULT_ASN1
? NULL
: EVP_CIPHER_get_asn1_iv
)
361 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1
,
362 sizeof(EVP_AES_HMAC_SHA1
)))) {
363 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1
);
364 _hidden_aes_128_cbc_hmac_sha1
= NULL
;
366 return _hidden_aes_128_cbc_hmac_sha1
;
369 static void destroy_ciphers(void)
371 EVP_CIPHER_meth_free(_hidden_aes_128_cbc
);
372 EVP_CIPHER_meth_free(_hidden_aes_128_gcm
);
373 EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1
);
374 _hidden_aes_128_cbc
= NULL
;
375 _hidden_aes_128_gcm
= NULL
;
376 _hidden_aes_128_cbc_hmac_sha1
= NULL
;
380 static EVP_PKEY
*load_key(ENGINE
*eng
, const char *key_id
, int pub
,
381 UI_METHOD
*ui_method
, void *ui_data
)
386 if (strncasecmp(key_id
, "ot:", 3) != 0)
390 fprintf(stderr
, "[ossltest]Loading %s key %s\n",
391 pub
? "Public" : "Private", key_id
);
392 in
= BIO_new_file(key_id
, "r");
396 key
= PEM_read_bio_PUBKEY(in
, NULL
, 0, NULL
);
398 key
= PEM_read_bio_PrivateKey(in
, NULL
, 0, NULL
);
403 static EVP_PKEY
*ossltest_load_privkey(ENGINE
*eng
, const char *key_id
,
404 UI_METHOD
*ui_method
, void *ui_data
)
406 return load_key(eng
, key_id
, 0, ui_method
, ui_data
);
409 static EVP_PKEY
*ossltest_load_pubkey(ENGINE
*eng
, const char *key_id
,
410 UI_METHOD
*ui_method
, void *ui_data
)
412 return load_key(eng
, key_id
, 1, ui_method
, ui_data
);
416 static int bind_ossltest(ENGINE
*e
)
418 /* Ensure the ossltest error handling is set up */
419 ERR_load_OSSLTEST_strings();
421 if (!ENGINE_set_id(e
, engine_ossltest_id
)
422 || !ENGINE_set_name(e
, engine_ossltest_name
)
423 || !ENGINE_set_digests(e
, ossltest_digests
)
424 || !ENGINE_set_ciphers(e
, ossltest_ciphers
)
425 || !ENGINE_set_RAND(e
, ossltest_rand_method())
426 || !ENGINE_set_destroy_function(e
, ossltest_destroy
)
427 || !ENGINE_set_load_privkey_function(e
, ossltest_load_privkey
)
428 || !ENGINE_set_load_pubkey_function(e
, ossltest_load_pubkey
)
429 || !ENGINE_set_init_function(e
, ossltest_init
)
430 || !ENGINE_set_finish_function(e
, ossltest_finish
)) {
431 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST
, OSSLTEST_R_INIT_FAILED
);
438 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
439 static int bind_helper(ENGINE
*e
, const char *id
)
441 if (id
&& (strcmp(id
, engine_ossltest_id
) != 0))
443 if (!bind_ossltest(e
))
448 IMPLEMENT_DYNAMIC_CHECK_FN()
449 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
452 static ENGINE
*engine_ossltest(void)
454 ENGINE
*ret
= ENGINE_new();
457 if (!bind_ossltest(ret
)) {
464 void ENGINE_load_ossltest(void)
466 /* Copied from eng_[openssl|dyn].c */
467 ENGINE
*toadd
= engine_ossltest();
476 static int ossltest_init(ENGINE
*e
)
482 static int ossltest_finish(ENGINE
*e
)
488 static int ossltest_destroy(ENGINE
*e
)
492 ERR_unload_OSSLTEST_strings();
496 static int ossltest_digests(ENGINE
*e
, const EVP_MD
**digest
,
497 const int **nids
, int nid
)
501 /* We are returning a list of supported nids */
502 return ossltest_digest_nids(nids
);
504 /* We are being asked for a specific digest */
507 *digest
= digest_md5();
510 *digest
= digest_sha1();
513 *digest
= digest_sha256();
516 *digest
= digest_sha384();
519 *digest
= digest_sha512();
529 static int ossltest_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
530 const int **nids
, int nid
)
534 /* We are returning a list of supported nids */
535 *nids
= ossltest_cipher_nids
;
536 return (sizeof(ossltest_cipher_nids
) - 1)
537 / sizeof(ossltest_cipher_nids
[0]);
539 /* We are being asked for a specific cipher */
541 case NID_aes_128_cbc
:
542 *cipher
= ossltest_aes_128_cbc();
544 case NID_aes_128_gcm
:
545 *cipher
= ossltest_aes_128_gcm();
547 case NID_aes_128_cbc_hmac_sha1
:
548 *cipher
= ossltest_aes_128_cbc_hmac_sha1();
558 static void fill_known_data(unsigned char *md
, unsigned int len
)
562 for (i
=0; i
<len
; i
++) {
563 md
[i
] = (unsigned char)(i
& 0xff);
568 * MD5 implementation. We go through the motions of doing MD5 by deferring to
569 * the standard implementation. Then we overwrite the result with a will defined
570 * value, so that all "MD5" digests using the test engine always end up with
573 static int digest_md5_init(EVP_MD_CTX
*ctx
)
575 return EVP_MD_meth_get_init(EVP_md5())(ctx
);
578 static int digest_md5_update(EVP_MD_CTX
*ctx
, const void *data
,
581 return EVP_MD_meth_get_update(EVP_md5())(ctx
, data
, count
);
584 static int digest_md5_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
586 int ret
= EVP_MD_meth_get_final(EVP_md5())(ctx
, md
);
589 fill_known_data(md
, MD5_DIGEST_LENGTH
);
595 * SHA1 implementation.
597 static int digest_sha1_init(EVP_MD_CTX
*ctx
)
599 return EVP_MD_meth_get_init(EVP_sha1())(ctx
);
602 static int digest_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
605 return EVP_MD_meth_get_update(EVP_sha1())(ctx
, data
, count
);
608 static int digest_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
610 int ret
= EVP_MD_meth_get_final(EVP_sha1())(ctx
, md
);
613 fill_known_data(md
, SHA_DIGEST_LENGTH
);
619 * SHA256 implementation.
621 static int digest_sha256_init(EVP_MD_CTX
*ctx
)
623 return EVP_MD_meth_get_init(EVP_sha256())(ctx
);
626 static int digest_sha256_update(EVP_MD_CTX
*ctx
, const void *data
,
629 return EVP_MD_meth_get_update(EVP_sha256())(ctx
, data
, count
);
632 static int digest_sha256_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
634 int ret
= EVP_MD_meth_get_final(EVP_sha256())(ctx
, md
);
637 fill_known_data(md
, SHA256_DIGEST_LENGTH
);
643 * SHA384 implementation.
645 static int digest_sha384_init(EVP_MD_CTX
*ctx
)
647 return EVP_MD_meth_get_init(EVP_sha384())(ctx
);
650 static int digest_sha384_update(EVP_MD_CTX
*ctx
, const void *data
,
653 return EVP_MD_meth_get_update(EVP_sha384())(ctx
, data
, count
);
656 static int digest_sha384_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
658 int ret
= EVP_MD_meth_get_final(EVP_sha384())(ctx
, md
);
661 fill_known_data(md
, SHA384_DIGEST_LENGTH
);
667 * SHA512 implementation.
669 static int digest_sha512_init(EVP_MD_CTX
*ctx
)
671 return EVP_MD_meth_get_init(EVP_sha512())(ctx
);
674 static int digest_sha512_update(EVP_MD_CTX
*ctx
, const void *data
,
677 return EVP_MD_meth_get_update(EVP_sha512())(ctx
, data
, count
);
680 static int digest_sha512_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
682 int ret
= EVP_MD_meth_get_final(EVP_sha512())(ctx
, md
);
685 fill_known_data(md
, SHA512_DIGEST_LENGTH
);
691 * AES128 Implementation
694 static int ossltest_aes128_init_key(EVP_CIPHER_CTX
*ctx
,
695 const unsigned char *key
,
696 const unsigned char *iv
, int enc
)
698 return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx
, key
, iv
, enc
);
701 static int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
702 const unsigned char *in
, size_t inl
)
704 unsigned char *tmpbuf
;
707 tmpbuf
= OPENSSL_malloc(inl
);
709 /* OPENSSL_malloc will return NULL if inl == 0 */
710 if (tmpbuf
== NULL
&& inl
> 0)
713 /* Remember what we were asked to encrypt */
715 memcpy(tmpbuf
, in
, inl
);
717 /* Go through the motions of encrypting it */
718 ret
= EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx
, out
, in
, inl
);
720 /* Throw it all away and just use the plaintext as the output */
722 memcpy(out
, tmpbuf
, inl
);
723 OPENSSL_free(tmpbuf
);
728 static int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX
*ctx
,
729 const unsigned char *key
,
730 const unsigned char *iv
, int enc
)
732 return EVP_CIPHER_meth_get_init(EVP_aes_128_gcm()) (ctx
, key
, iv
, enc
);
735 static int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
736 const unsigned char *in
, size_t inl
)
738 unsigned char *tmpbuf
= OPENSSL_malloc(inl
);
740 /* OPENSSL_malloc will return NULL if inl == 0 */
741 if (tmpbuf
== NULL
&& inl
> 0)
744 /* Remember what we were asked to encrypt */
746 memcpy(tmpbuf
, in
, inl
);
748 /* Go through the motions of encrypting it */
749 EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_gcm())(ctx
, out
, in
, inl
);
751 /* Throw it all away and just use the plaintext as the output */
752 if (tmpbuf
!= NULL
&& out
!= NULL
)
753 memcpy(out
, tmpbuf
, inl
);
754 OPENSSL_free(tmpbuf
);
759 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
,
762 /* Pass the ctrl down */
763 int ret
= EVP_CIPHER_meth_get_ctrl(EVP_aes_128_gcm())(ctx
, type
, arg
, ptr
);
769 case EVP_CTRL_AEAD_GET_TAG
:
770 /* Always give the same tag */
771 memset(ptr
, 0, EVP_GCM_TLS_TAG_LEN
);
781 #define NO_PAYLOAD_LENGTH ((size_t)-1)
782 # define data(ctx) ((EVP_AES_HMAC_SHA1 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
784 static int ossltest_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX
*ctx
,
785 const unsigned char *inkey
,
786 const unsigned char *iv
,
789 EVP_AES_HMAC_SHA1
*key
= data(ctx
);
790 key
->payload_length
= NO_PAYLOAD_LENGTH
;
794 static int ossltest_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX
*ctx
,
796 const unsigned char *in
,
799 EVP_AES_HMAC_SHA1
*key
= data(ctx
);
801 size_t plen
= key
->payload_length
;
803 key
->payload_length
= NO_PAYLOAD_LENGTH
;
805 if (len
% AES_BLOCK_SIZE
)
808 if (EVP_CIPHER_CTX_is_encrypting(ctx
)) {
809 if (plen
== NO_PAYLOAD_LENGTH
)
812 ((plen
+ SHA_DIGEST_LENGTH
+
813 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
))
816 memmove(out
, in
, plen
);
818 if (plen
!= len
) { /* "TLS" mode of operation */
819 /* calculate HMAC and append it to payload */
820 fill_known_data(out
+ plen
, SHA_DIGEST_LENGTH
);
822 /* pad the payload|hmac */
823 plen
+= SHA_DIGEST_LENGTH
;
824 for (l
= len
- plen
- 1; plen
< len
; plen
++)
828 /* decrypt HMAC|padding at once */
829 memmove(out
, in
, len
);
831 if (plen
!= NO_PAYLOAD_LENGTH
) { /* "TLS" mode of operation */
832 unsigned int maxpad
, pad
;
834 if (key
->tls_ver
>= TLS1_1_VERSION
) {
835 if (len
< (AES_BLOCK_SIZE
+ SHA_DIGEST_LENGTH
+ 1))
838 /* omit explicit iv */
839 in
+= AES_BLOCK_SIZE
;
840 out
+= AES_BLOCK_SIZE
;
841 len
-= AES_BLOCK_SIZE
;
842 } else if (len
< (SHA_DIGEST_LENGTH
+ 1))
845 /* figure out payload length */
847 maxpad
= len
- (SHA_DIGEST_LENGTH
+ 1);
850 for (plen
= len
- pad
- 1; plen
< len
; plen
++)
851 if (out
[plen
] != pad
)
859 static int ossltest_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX
*ctx
, int type
,
862 EVP_AES_HMAC_SHA1
*key
= data(ctx
);
865 case EVP_CTRL_AEAD_SET_MAC_KEY
:
868 case EVP_CTRL_AEAD_TLS1_AAD
:
870 unsigned char *p
= ptr
;
873 if (arg
!= EVP_AEAD_TLS1_AAD_LEN
)
876 len
= p
[arg
- 2] << 8 | p
[arg
- 1];
877 key
->tls_ver
= p
[arg
- 4] << 8 | p
[arg
- 3];
879 if (EVP_CIPHER_CTX_is_encrypting(ctx
)) {
880 key
->payload_length
= len
;
881 if (key
->tls_ver
>= TLS1_1_VERSION
) {
882 if (len
< AES_BLOCK_SIZE
)
884 len
-= AES_BLOCK_SIZE
;
885 p
[arg
- 2] = len
>> 8;
889 return (int)(((len
+ SHA_DIGEST_LENGTH
+
890 AES_BLOCK_SIZE
) & -AES_BLOCK_SIZE
)
893 key
->payload_length
= arg
;
895 return SHA_DIGEST_LENGTH
;
903 static int ossltest_rand_bytes(unsigned char *buf
, int num
)
905 unsigned char val
= 1;
912 static int ossltest_rand_status(void)
917 static const RAND_METHOD
*ossltest_rand_method(void)
920 static RAND_METHOD osslt_rand_meth
= {
929 return &osslt_rand_meth
;