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