blob: 1af2446dbdfee7eda0013493486f1ef817252313 [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
Daniel Axtensf0710242020-05-28 11:43:41 +10008#if defined(MBEDTLS_HAVE_TIME)
Philippe Antoine499c7352019-06-04 14:14:33 +02009mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
10{
Philippe Antoine08633822019-06-04 14:03:06 +020011 (void) time;
12 return 0x5af2a056;
13}
Daniel Axtensf0710242020-05-28 11:43:41 +100014#endif
Philippe Antoine08633822019-06-04 14:03:06 +020015
Philippe Antoine499c7352019-06-04 14:14:33 +020016void dummy_init()
17{
Daniel Axtensf0710242020-05-28 11:43:41 +100018#if (defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_PLATFORM_TIME_ALT))
Philippe Antoine08633822019-06-04 14:03:06 +020019 mbedtls_platform_set_time( dummy_constant_time );
20#else
21 fprintf(stderr, "Warning: fuzzing without constant time\n");
22#endif
23}
Philippe Antoine499c7352019-06-04 14:14:33 +020024
25int dummy_send( void *ctx, const unsigned char *buf, size_t len )
26{
27 //silence warning about unused parameter
28 (void) ctx;
29 (void) buf;
30
31 //pretends we wrote everything ok
Philippe Antoinea82fdd42019-07-10 13:53:40 +020032 if( len > INT_MAX ) {
33 return( -1 );
Philippe Antoine3e408d52019-07-10 01:09:50 +020034 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020035 return( (int) len );
Philippe Antoine499c7352019-06-04 14:14:33 +020036}
37
38int fuzz_recv( void *ctx, unsigned char *buf, size_t len )
39{
40 //reads from the buffer from fuzzer
41 fuzzBufferOffset_t * biomemfuzz = (fuzzBufferOffset_t *) ctx;
42
Philippe Antoinea82fdd42019-07-10 13:53:40 +020043 if(biomemfuzz->Offset == biomemfuzz->Size) {
Philippe Antoine499c7352019-06-04 14:14:33 +020044 //EOF
Philippe Antoinea82fdd42019-07-10 13:53:40 +020045 return( 0 );
Philippe Antoine499c7352019-06-04 14:14:33 +020046 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020047 if( len > INT_MAX ) {
48 return( -1 );
Philippe Antoine23219452019-07-10 08:26:04 +020049 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020050 if( len + biomemfuzz->Offset > biomemfuzz->Size ) {
Philippe Antoine499c7352019-06-04 14:14:33 +020051 //do not overflow
52 len = biomemfuzz->Size - biomemfuzz->Offset;
53 }
54 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
55 biomemfuzz->Offset += len;
Philippe Antoinea82fdd42019-07-10 13:53:40 +020056 return( (int) len );
Philippe Antoine499c7352019-06-04 14:14:33 +020057}
58
59int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
60{
61 int ret;
62 size_t i;
63
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020064#if defined(MBEDTLS_CTR_DRBG_C)
Paul Elliotta1dc3e52022-02-14 18:26:21 +000065 //mbedtls_ctr_drbg_random requires a valid mbedtls_ctr_drbg_context in p_rng
66 if( p_rng != NULL ) {
67 //use mbedtls_ctr_drbg_random to find bugs in it
68 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
Paul Elliott5d7e61f2022-02-15 16:05:17 +000069 } else {
70 //fall through to pseudo-random
71 ret = 0;
Paul Elliotta1dc3e52022-02-14 18:26:21 +000072 }
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020073#else
74 (void) p_rng;
75 ret = 0;
76#endif
Philippe Antoine499c7352019-06-04 14:14:33 +020077 for (i=0; i<output_len; i++) {
78 //replace result with pseudo random
79 output[i] = (unsigned char) rand();
80 }
81 return( ret );
82}
83
84int dummy_entropy( void *data, unsigned char *output, size_t len )
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);
92 for (i=0; i<len; i++) {
93 //replace result with pseudo random
94 output[i] = (unsigned char) rand();
95 }
96 return( 0 );
97}
98
99int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
100 uint32_t timeout )
101{
102 (void) timeout;
103
104 return fuzz_recv(ctx, buf, len);
105}