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