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