]> git.ipfire.org Git - thirdparty/openssl.git/blob - engines/e_ossltest.c
Add some missing committers to the AUTHORS list
[thirdparty/openssl.git] / engines / e_ossltest.c
1 /*
2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /*
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
14 */
15
16 /* We need to use some engine deprecated APIs */
17 #define OPENSSL_SUPPRESS_DEPRECATED
18
19 /*
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.
25 */
26 #include "internal/deprecated.h"
27
28 #include <stdio.h>
29 #include <string.h>
30
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
42 #include "e_ossltest_err.c"
43
44 #ifdef _WIN32
45 # define strncasecmp _strnicmp
46 #endif
47
48 /* Engine Id and Name */
49 static const char *engine_ossltest_id = "ossltest";
50 static const char *engine_ossltest_name = "OpenSSL Test engine support";
51
52
53 /* Engine Lifetime functions */
54 static int ossltest_destroy(ENGINE *e);
55 static int ossltest_init(ENGINE *e);
56 static int ossltest_finish(ENGINE *e);
57 void ENGINE_load_ossltest(void);
58
59
60 /* Set up digests */
61 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
62 const int **nids, int nid);
63 static const RAND_METHOD *ossltest_rand_method(void);
64
65 /* MD5 */
66 static int digest_md5_init(EVP_MD_CTX *ctx);
67 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
68 size_t count);
69 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
70
71 static EVP_MD *_hidden_md5_md = NULL;
72 static const EVP_MD *digest_md5(void)
73 {
74 if (_hidden_md5_md == NULL) {
75 EVP_MD *md;
76
77 if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
78 || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
79 || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
80 || !EVP_MD_meth_set_app_datasize(md,
81 sizeof(EVP_MD *) + sizeof(MD5_CTX))
82 || !EVP_MD_meth_set_flags(md, 0)
83 || !EVP_MD_meth_set_init(md, digest_md5_init)
84 || !EVP_MD_meth_set_update(md, digest_md5_update)
85 || !EVP_MD_meth_set_final(md, digest_md5_final)) {
86 EVP_MD_meth_free(md);
87 md = NULL;
88 }
89 _hidden_md5_md = md;
90 }
91 return _hidden_md5_md;
92 }
93
94 /* SHA1 */
95 static int digest_sha1_init(EVP_MD_CTX *ctx);
96 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
97 size_t count);
98 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
99
100 static EVP_MD *_hidden_sha1_md = NULL;
101 static const EVP_MD *digest_sha1(void)
102 {
103 if (_hidden_sha1_md == NULL) {
104 EVP_MD *md;
105
106 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
107 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
108 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
109 || !EVP_MD_meth_set_app_datasize(md,
110 sizeof(EVP_MD *) + sizeof(SHA_CTX))
111 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
112 || !EVP_MD_meth_set_init(md, digest_sha1_init)
113 || !EVP_MD_meth_set_update(md, digest_sha1_update)
114 || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
115 EVP_MD_meth_free(md);
116 md = NULL;
117 }
118 _hidden_sha1_md = md;
119 }
120 return _hidden_sha1_md;
121 }
122
123 /* SHA256 */
124 static int digest_sha256_init(EVP_MD_CTX *ctx);
125 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
126 size_t count);
127 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
128
129 static EVP_MD *_hidden_sha256_md = NULL;
130 static const EVP_MD *digest_sha256(void)
131 {
132 if (_hidden_sha256_md == NULL) {
133 EVP_MD *md;
134
135 if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
136 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
137 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
138 || !EVP_MD_meth_set_app_datasize(md,
139 sizeof(EVP_MD *) + sizeof(SHA256_CTX))
140 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
141 || !EVP_MD_meth_set_init(md, digest_sha256_init)
142 || !EVP_MD_meth_set_update(md, digest_sha256_update)
143 || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
144 EVP_MD_meth_free(md);
145 md = NULL;
146 }
147 _hidden_sha256_md = md;
148 }
149 return _hidden_sha256_md;
150 }
151
152 /* SHA384/SHA512 */
153 static int digest_sha384_init(EVP_MD_CTX *ctx);
154 static int digest_sha384_update(EVP_MD_CTX *ctx, const void *data,
155 size_t count);
156 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
157
158 static int digest_sha512_init(EVP_MD_CTX *ctx);
159 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
160 size_t count);
161 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
162
163 static EVP_MD *_hidden_sha384_md = NULL;
164 static const EVP_MD *digest_sha384(void)
165 {
166 if (_hidden_sha384_md == NULL) {
167 EVP_MD *md;
168
169 if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
170 || !EVP_MD_meth_set_result_size(md, SHA384_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_sha384_init)
176 || !EVP_MD_meth_set_update(md, digest_sha384_update)
177 || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
178 EVP_MD_meth_free(md);
179 md = NULL;
180 }
181 _hidden_sha384_md = md;
182 }
183 return _hidden_sha384_md;
184 }
185 static EVP_MD *_hidden_sha512_md = NULL;
186 static const EVP_MD *digest_sha512(void)
187 {
188 if (_hidden_sha512_md == NULL) {
189 EVP_MD *md;
190
191 if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
192 || !EVP_MD_meth_set_result_size(md, SHA512_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_sha512_init)
198 || !EVP_MD_meth_set_update(md, digest_sha512_update)
199 || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
200 EVP_MD_meth_free(md);
201 md = NULL;
202 }
203 _hidden_sha512_md = md;
204 }
205 return _hidden_sha512_md;
206 }
207 static void destroy_digests(void)
208 {
209 EVP_MD_meth_free(_hidden_md5_md);
210 _hidden_md5_md = NULL;
211 EVP_MD_meth_free(_hidden_sha1_md);
212 _hidden_sha1_md = NULL;
213 EVP_MD_meth_free(_hidden_sha256_md);
214 _hidden_sha256_md = NULL;
215 EVP_MD_meth_free(_hidden_sha384_md);
216 _hidden_sha384_md = NULL;
217 EVP_MD_meth_free(_hidden_sha512_md);
218 _hidden_sha512_md = NULL;
219 }
220 static int ossltest_digest_nids(const int **nids)
221 {
222 static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
223 static int pos = 0;
224 static int init = 0;
225
226 if (!init) {
227 const EVP_MD *md;
228 if ((md = digest_md5()) != NULL)
229 digest_nids[pos++] = EVP_MD_type(md);
230 if ((md = digest_sha1()) != NULL)
231 digest_nids[pos++] = EVP_MD_type(md);
232 if ((md = digest_sha256()) != NULL)
233 digest_nids[pos++] = EVP_MD_type(md);
234 if ((md = digest_sha384()) != NULL)
235 digest_nids[pos++] = EVP_MD_type(md);
236 if ((md = digest_sha512()) != NULL)
237 digest_nids[pos++] = EVP_MD_type(md);
238 digest_nids[pos] = 0;
239 init = 1;
240 }
241 *nids = digest_nids;
242 return pos;
243 }
244
245 /* Setup ciphers */
246 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
247 const int **, int);
248
249 static int ossltest_cipher_nids[] = {
250 NID_aes_128_cbc, NID_aes_128_gcm, 0
251 };
252
253 /* AES128 */
254
255 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
256 const unsigned char *iv, int enc);
257 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
258 const unsigned char *in, size_t inl);
259 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
260 const unsigned char *iv, int enc);
261 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
262 const unsigned char *in, size_t inl);
263 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
264 void *ptr);
265
266 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
267 static const EVP_CIPHER *ossltest_aes_128_cbc(void)
268 {
269 if (_hidden_aes_128_cbc == NULL
270 && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
271 16 /* block size */,
272 16 /* key len */)) == NULL
273 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
274 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
275 EVP_CIPH_FLAG_DEFAULT_ASN1
276 | EVP_CIPH_CBC_MODE)
277 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
278 ossltest_aes128_init_key)
279 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
280 ossltest_aes128_cbc_cipher)
281 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
282 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
283 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
284 _hidden_aes_128_cbc = NULL;
285 }
286 return _hidden_aes_128_cbc;
287 }
288 static EVP_CIPHER *_hidden_aes_128_gcm = NULL;
289
290 #define AES_GCM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
291 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
292 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
293 | EVP_CIPH_CUSTOM_COPY |EVP_CIPH_FLAG_AEAD_CIPHER \
294 | EVP_CIPH_GCM_MODE)
295
296 static const EVP_CIPHER *ossltest_aes_128_gcm(void)
297 {
298 if (_hidden_aes_128_gcm == NULL
299 && ((_hidden_aes_128_gcm = EVP_CIPHER_meth_new(NID_aes_128_gcm,
300 1 /* block size */,
301 16 /* key len */)) == NULL
302 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_gcm,12)
303 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_gcm, AES_GCM_FLAGS)
304 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_gcm,
305 ossltest_aes128_gcm_init_key)
306 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_gcm,
307 ossltest_aes128_gcm_cipher)
308 || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_gcm,
309 ossltest_aes128_gcm_ctrl)
310 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_gcm,
311 EVP_CIPHER_impl_ctx_size(EVP_aes_128_gcm())))) {
312 EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
313 _hidden_aes_128_gcm = NULL;
314 }
315 return _hidden_aes_128_gcm;
316 }
317
318 static void destroy_ciphers(void)
319 {
320 EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
321 EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
322 _hidden_aes_128_cbc = NULL;
323 }
324
325 /* Key loading */
326 static EVP_PKEY *load_key(ENGINE *eng, const char *key_id, int pub,
327 UI_METHOD *ui_method, void *ui_data)
328 {
329 BIO *in;
330 EVP_PKEY *key;
331
332 if (strncasecmp(key_id, "ot:", 3) != 0)
333 return NULL;
334 key_id += 3;
335
336 fprintf(stderr, "[ossltest]Loading %s key %s\n",
337 pub ? "Public" : "Private", key_id);
338 in = BIO_new_file(key_id, "r");
339 if (!in)
340 return NULL;
341 if (pub)
342 key = PEM_read_bio_PUBKEY(in, NULL, 0, NULL);
343 else
344 key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
345 BIO_free(in);
346 return key;
347 }
348
349 static EVP_PKEY *ossltest_load_privkey(ENGINE *eng, const char *key_id,
350 UI_METHOD *ui_method, void *ui_data)
351 {
352 return load_key(eng, key_id, 0, ui_method, ui_data);
353 }
354
355 static EVP_PKEY *ossltest_load_pubkey(ENGINE *eng, const char *key_id,
356 UI_METHOD *ui_method, void *ui_data)
357 {
358 return load_key(eng, key_id, 1, ui_method, ui_data);
359 }
360
361
362 static int bind_ossltest(ENGINE *e)
363 {
364 /* Ensure the ossltest error handling is set up */
365 ERR_load_OSSLTEST_strings();
366
367 if (!ENGINE_set_id(e, engine_ossltest_id)
368 || !ENGINE_set_name(e, engine_ossltest_name)
369 || !ENGINE_set_digests(e, ossltest_digests)
370 || !ENGINE_set_ciphers(e, ossltest_ciphers)
371 || !ENGINE_set_RAND(e, ossltest_rand_method())
372 || !ENGINE_set_destroy_function(e, ossltest_destroy)
373 || !ENGINE_set_load_privkey_function(e, ossltest_load_privkey)
374 || !ENGINE_set_load_pubkey_function(e, ossltest_load_pubkey)
375 || !ENGINE_set_init_function(e, ossltest_init)
376 || !ENGINE_set_finish_function(e, ossltest_finish)) {
377 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
378 return 0;
379 }
380
381 return 1;
382 }
383
384 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
385 static int bind_helper(ENGINE *e, const char *id)
386 {
387 if (id && (strcmp(id, engine_ossltest_id) != 0))
388 return 0;
389 if (!bind_ossltest(e))
390 return 0;
391 return 1;
392 }
393
394 IMPLEMENT_DYNAMIC_CHECK_FN()
395 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
396 #endif
397
398 static ENGINE *engine_ossltest(void)
399 {
400 ENGINE *ret = ENGINE_new();
401 if (ret == NULL)
402 return NULL;
403 if (!bind_ossltest(ret)) {
404 ENGINE_free(ret);
405 return NULL;
406 }
407 return ret;
408 }
409
410 void ENGINE_load_ossltest(void)
411 {
412 /* Copied from eng_[openssl|dyn].c */
413 ENGINE *toadd = engine_ossltest();
414 if (!toadd)
415 return;
416 ENGINE_add(toadd);
417 ENGINE_free(toadd);
418 ERR_clear_error();
419 }
420
421
422 static int ossltest_init(ENGINE *e)
423 {
424 return 1;
425 }
426
427
428 static int ossltest_finish(ENGINE *e)
429 {
430 return 1;
431 }
432
433
434 static int ossltest_destroy(ENGINE *e)
435 {
436 destroy_digests();
437 destroy_ciphers();
438 ERR_unload_OSSLTEST_strings();
439 return 1;
440 }
441
442 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
443 const int **nids, int nid)
444 {
445 int ok = 1;
446 if (!digest) {
447 /* We are returning a list of supported nids */
448 return ossltest_digest_nids(nids);
449 }
450 /* We are being asked for a specific digest */
451 switch (nid) {
452 case NID_md5:
453 *digest = digest_md5();
454 break;
455 case NID_sha1:
456 *digest = digest_sha1();
457 break;
458 case NID_sha256:
459 *digest = digest_sha256();
460 break;
461 case NID_sha384:
462 *digest = digest_sha384();
463 break;
464 case NID_sha512:
465 *digest = digest_sha512();
466 break;
467 default:
468 ok = 0;
469 *digest = NULL;
470 break;
471 }
472 return ok;
473 }
474
475 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
476 const int **nids, int nid)
477 {
478 int ok = 1;
479 if (!cipher) {
480 /* We are returning a list of supported nids */
481 *nids = ossltest_cipher_nids;
482 return (sizeof(ossltest_cipher_nids) - 1)
483 / sizeof(ossltest_cipher_nids[0]);
484 }
485 /* We are being asked for a specific cipher */
486 switch (nid) {
487 case NID_aes_128_cbc:
488 *cipher = ossltest_aes_128_cbc();
489 break;
490 case NID_aes_128_gcm:
491 *cipher = ossltest_aes_128_gcm();
492 break;
493 default:
494 ok = 0;
495 *cipher = NULL;
496 break;
497 }
498 return ok;
499 }
500
501 static void fill_known_data(unsigned char *md, unsigned int len)
502 {
503 unsigned int i;
504
505 for (i=0; i<len; i++) {
506 md[i] = (unsigned char)(i & 0xff);
507 }
508 }
509
510 /*
511 * MD5 implementation. We go through the motions of doing MD5 by deferring to
512 * the standard implementation. Then we overwrite the result with a will defined
513 * value, so that all "MD5" digests using the test engine always end up with
514 * the same value.
515 */
516 static int digest_md5_init(EVP_MD_CTX *ctx)
517 {
518 return EVP_MD_meth_get_init(EVP_md5())(ctx);
519 }
520
521 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
522 size_t count)
523 {
524 return EVP_MD_meth_get_update(EVP_md5())(ctx, data, count);
525 }
526
527 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
528 {
529 int ret = EVP_MD_meth_get_final(EVP_md5())(ctx, md);
530
531 if (ret > 0) {
532 fill_known_data(md, MD5_DIGEST_LENGTH);
533 }
534 return ret;
535 }
536
537 /*
538 * SHA1 implementation.
539 */
540 static int digest_sha1_init(EVP_MD_CTX *ctx)
541 {
542 return EVP_MD_meth_get_init(EVP_sha1())(ctx);
543 }
544
545 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
546 size_t count)
547 {
548 return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
549 }
550
551 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
552 {
553 int ret = EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
554
555 if (ret > 0) {
556 fill_known_data(md, SHA_DIGEST_LENGTH);
557 }
558 return ret;
559 }
560
561 /*
562 * SHA256 implementation.
563 */
564 static int digest_sha256_init(EVP_MD_CTX *ctx)
565 {
566 return EVP_MD_meth_get_init(EVP_sha256())(ctx);
567 }
568
569 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
570 size_t count)
571 {
572 return EVP_MD_meth_get_update(EVP_sha256())(ctx, data, count);
573 }
574
575 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
576 {
577 int ret = EVP_MD_meth_get_final(EVP_sha256())(ctx, md);
578
579 if (ret > 0) {
580 fill_known_data(md, SHA256_DIGEST_LENGTH);
581 }
582 return ret;
583 }
584
585 /*
586 * SHA384 implementation.
587 */
588 static int digest_sha384_init(EVP_MD_CTX *ctx)
589 {
590 return EVP_MD_meth_get_init(EVP_sha384())(ctx);
591 }
592
593 static int digest_sha384_update(EVP_MD_CTX *ctx, const void *data,
594 size_t count)
595 {
596 return EVP_MD_meth_get_update(EVP_sha384())(ctx, data, count);
597 }
598
599 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
600 {
601 int ret = EVP_MD_meth_get_final(EVP_sha384())(ctx, md);
602
603 if (ret > 0) {
604 fill_known_data(md, SHA384_DIGEST_LENGTH);
605 }
606 return ret;
607 }
608
609 /*
610 * SHA512 implementation.
611 */
612 static int digest_sha512_init(EVP_MD_CTX *ctx)
613 {
614 return EVP_MD_meth_get_init(EVP_sha512())(ctx);
615 }
616
617 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
618 size_t count)
619 {
620 return EVP_MD_meth_get_update(EVP_sha512())(ctx, data, count);
621 }
622
623 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
624 {
625 int ret = EVP_MD_meth_get_final(EVP_sha512())(ctx, md);
626
627 if (ret > 0) {
628 fill_known_data(md, SHA512_DIGEST_LENGTH);
629 }
630 return ret;
631 }
632
633 /*
634 * AES128 Implementation
635 */
636
637 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
638 const unsigned char *iv, int enc)
639 {
640 return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
641 }
642
643 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
644 const unsigned char *in, size_t inl)
645 {
646 unsigned char *tmpbuf;
647 int ret;
648
649 tmpbuf = OPENSSL_malloc(inl);
650
651 /* OPENSSL_malloc will return NULL if inl == 0 */
652 if (tmpbuf == NULL && inl > 0)
653 return -1;
654
655 /* Remember what we were asked to encrypt */
656 if (tmpbuf != NULL)
657 memcpy(tmpbuf, in, inl);
658
659 /* Go through the motions of encrypting it */
660 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
661
662 /* Throw it all away and just use the plaintext as the output */
663 if (tmpbuf != NULL)
664 memcpy(out, tmpbuf, inl);
665 OPENSSL_free(tmpbuf);
666
667 return ret;
668 }
669
670 int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
671 const unsigned char *iv, int enc)
672 {
673 return EVP_CIPHER_meth_get_init(EVP_aes_128_gcm()) (ctx, key, iv, enc);
674 }
675
676
677 int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
678 const unsigned char *in, size_t inl)
679 {
680 unsigned char *tmpbuf = OPENSSL_malloc(inl);
681
682 /* OPENSSL_malloc will return NULL if inl == 0 */
683 if (tmpbuf == NULL && inl > 0)
684 return -1;
685
686 /* Remember what we were asked to encrypt */
687 if (tmpbuf != NULL)
688 memcpy(tmpbuf, in, inl);
689
690 /* Go through the motions of encrypting it */
691 EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_gcm())(ctx, out, in, inl);
692
693 /* Throw it all away and just use the plaintext as the output */
694 if (tmpbuf != NULL && out != NULL)
695 memcpy(out, tmpbuf, inl);
696 OPENSSL_free(tmpbuf);
697
698 return inl;
699 }
700
701 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
702 void *ptr)
703 {
704 /* Pass the ctrl down */
705 int ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_gcm())(ctx, type, arg, ptr);
706
707 if (ret <= 0)
708 return ret;
709
710 switch(type) {
711 case EVP_CTRL_AEAD_GET_TAG:
712 /* Always give the same tag */
713 memset(ptr, 0, EVP_GCM_TLS_TAG_LEN);
714 break;
715
716 default:
717 break;
718 }
719
720 return 1;
721 }
722
723 static int ossltest_rand_bytes(unsigned char *buf, int num)
724 {
725 unsigned char val = 1;
726
727 while (--num >= 0)
728 *buf++ = val++;
729 return 1;
730 }
731
732 static int ossltest_rand_status(void)
733 {
734 return 1;
735 }
736
737 static const RAND_METHOD *ossltest_rand_method(void)
738 {
739
740 static RAND_METHOD osslt_rand_meth = {
741 NULL,
742 ossltest_rand_bytes,
743 NULL,
744 NULL,
745 ossltest_rand_bytes,
746 ossltest_rand_status
747 };
748
749 return &osslt_rand_meth;
750 }