Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 1 | #include "common.h" |
Dave Rodgman | 351c71b | 2021-12-06 17:50:53 +0000 | [diff] [blame] | 2 | #include <limits.h> |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include "mbedtls/ctr_drbg.h" |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 7 | |
Andrzej Kurek | 06a00af | 2022-03-02 10:56:22 -0500 | [diff] [blame] | 8 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9 | mbedtls_time_t dummy_constant_time(mbedtls_time_t *time) |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 10 | { |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 11 | (void) time; |
| 12 | return 0x5af2a056; |
| 13 | } |
Daniel Axtens | f071024 | 2020-05-28 11:43:41 +1000 | [diff] [blame] | 14 | #endif |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 15 | |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 16 | void dummy_init() |
| 17 | { |
David Horstmann | ca53459 | 2021-11-29 18:57:10 +0000 | [diff] [blame] | 18 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 19 | mbedtls_platform_set_time(dummy_constant_time); |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 20 | #else |
| 21 | fprintf(stderr, "Warning: fuzzing without constant time\n"); |
| 22 | #endif |
| 23 | } |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 24 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 25 | int dummy_send(void *ctx, const unsigned char *buf, size_t len) |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 26 | { |
| 27 | //silence warning about unused parameter |
| 28 | (void) ctx; |
| 29 | (void) buf; |
| 30 | |
| 31 | //pretends we wrote everything ok |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 32 | if (len > INT_MAX) { |
| 33 | return -1; |
Philippe Antoine | 3e408d5 | 2019-07-10 01:09:50 +0200 | [diff] [blame] | 34 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | return (int) len; |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | int fuzz_recv(void *ctx, unsigned char *buf, size_t len) |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 39 | { |
| 40 | //reads from the buffer from fuzzer |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 41 | fuzzBufferOffset_t *biomemfuzz = (fuzzBufferOffset_t *) ctx; |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 42 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | if (biomemfuzz->Offset == biomemfuzz->Size) { |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 44 | //EOF |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | return 0; |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 46 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | if (len > INT_MAX) { |
| 48 | return -1; |
Philippe Antoine | 2321945 | 2019-07-10 08:26:04 +0200 | [diff] [blame] | 49 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | if (len + biomemfuzz->Offset > biomemfuzz->Size) { |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 51 | //do not overflow |
| 52 | len = biomemfuzz->Size - biomemfuzz->Offset; |
| 53 | } |
| 54 | memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len); |
| 55 | biomemfuzz->Offset += len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | return (int) len; |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | int dummy_random(void *p_rng, unsigned char *output, size_t output_len) |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 60 | { |
| 61 | int ret; |
| 62 | size_t i; |
| 63 | |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 64 | #if defined(MBEDTLS_CTR_DRBG_C) |
Paul Elliott | a1dc3e5 | 2022-02-14 18:26:21 +0000 | [diff] [blame] | 65 | //mbedtls_ctr_drbg_random requires a valid mbedtls_ctr_drbg_context in p_rng |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | if (p_rng != NULL) { |
Paul Elliott | a1dc3e5 | 2022-02-14 18:26:21 +0000 | [diff] [blame] | 67 | //use mbedtls_ctr_drbg_random to find bugs in it |
| 68 | ret = mbedtls_ctr_drbg_random(p_rng, output, output_len); |
Paul Elliott | 5d7e61f | 2022-02-15 16:05:17 +0000 | [diff] [blame] | 69 | } else { |
| 70 | //fall through to pseudo-random |
| 71 | ret = 0; |
Paul Elliott | a1dc3e5 | 2022-02-14 18:26:21 +0000 | [diff] [blame] | 72 | } |
Manuel Pégourié-Gonnard | a89040c | 2020-05-20 10:35:01 +0200 | [diff] [blame] | 73 | #else |
| 74 | (void) p_rng; |
| 75 | ret = 0; |
| 76 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | for (i = 0; i < output_len; i++) { |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 78 | //replace result with pseudo random |
| 79 | output[i] = (unsigned char) rand(); |
| 80 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | return ret; |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | int dummy_entropy(void *data, unsigned char *output, size_t len) |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 85 | { |
| 86 | size_t i; |
| 87 | (void) data; |
| 88 | |
| 89 | //use mbedtls_entropy_func to find bugs in it |
| 90 | //test performance impact of entropy |
| 91 | //ret = mbedtls_entropy_func(data, output, len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | for (i = 0; i < len; i++) { |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 93 | //replace result with pseudo random |
| 94 | output[i] = (unsigned char) rand(); |
| 95 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | return 0; |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | int fuzz_recv_timeout(void *ctx, unsigned char *buf, size_t len, |
| 100 | uint32_t timeout) |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 101 | { |
| 102 | (void) timeout; |
| 103 | |
| 104 | return fuzz_recv(ctx, buf, len); |
| 105 | } |