blob: 4d5840228326686c91134c5e9b05baffec80a587 [file] [log] [blame]
Philippe Antoine08633822019-06-04 14:03:06 +02001#include "common.h"
Dave Rodgman351c71b2021-12-06 17:50:53 +00002#include <limits.h>
Philippe Antoine499c7352019-06-04 14:14:33 +02003#include <stdio.h>
4#include <string.h>
5#include <stdlib.h>
6#include "mbedtls/ctr_drbg.h"
Philippe Antoine08633822019-06-04 14:03:06 +02007
Philippe Antoine499c7352019-06-04 14:14:33 +02008mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
9{
Philippe Antoine08633822019-06-04 14:03:06 +020010 (void) time;
11 return 0x5af2a056;
12}
13
Philippe Antoine499c7352019-06-04 14:14:33 +020014void dummy_init()
15{
Philippe Antoine08633822019-06-04 14:03:06 +020016#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 Antoine499c7352019-06-04 14:14:33 +020022
23int 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 Antoinea82fdd42019-07-10 13:53:40 +020030 if( len > INT_MAX ) {
31 return( -1 );
Philippe Antoine3e408d52019-07-10 01:09:50 +020032 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020033 return( (int) len );
Philippe Antoine499c7352019-06-04 14:14:33 +020034}
35
36int 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 Antoinea82fdd42019-07-10 13:53:40 +020041 if(biomemfuzz->Offset == biomemfuzz->Size) {
Philippe Antoine499c7352019-06-04 14:14:33 +020042 //EOF
Philippe Antoinea82fdd42019-07-10 13:53:40 +020043 return( 0 );
Philippe Antoine499c7352019-06-04 14:14:33 +020044 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020045 if( len > INT_MAX ) {
46 return( -1 );
Philippe Antoine23219452019-07-10 08:26:04 +020047 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020048 if( len + biomemfuzz->Offset > biomemfuzz->Size ) {
Philippe Antoine499c7352019-06-04 14:14:33 +020049 //do not overflow
50 len = biomemfuzz->Size - biomemfuzz->Offset;
51 }
52 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
53 biomemfuzz->Offset += len;
Philippe Antoinea82fdd42019-07-10 13:53:40 +020054 return( (int) len );
Philippe Antoine499c7352019-06-04 14:14:33 +020055}
56
57int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
58{
59 int ret;
60 size_t i;
61
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020062#if defined(MBEDTLS_CTR_DRBG_C)
Paul Elliotta1dc3e52022-02-14 18:26:21 +000063 //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 Elliott5d7e61f2022-02-15 16:05:17 +000067 } else {
68 //fall through to pseudo-random
69 ret = 0;
Paul Elliotta1dc3e52022-02-14 18:26:21 +000070 }
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020071#else
72 (void) p_rng;
73 ret = 0;
74#endif
Philippe Antoine499c7352019-06-04 14:14:33 +020075 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
82int 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
97int 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}