blob: 70a53807582ea065728f7b3ac159ca357026a079 [file] [log] [blame]
Philippe Antoine08633822019-06-04 14:03:06 +02001#include "common.h"
Philippe Antoine499c7352019-06-04 14:14:33 +02002#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5#include "mbedtls/ctr_drbg.h"
Philippe Antoine08633822019-06-04 14:03:06 +02006
Philippe Antoine499c7352019-06-04 14:14:33 +02007mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
8{
Philippe Antoine08633822019-06-04 14:03:06 +02009 (void) time;
10 return 0x5af2a056;
11}
12
Philippe Antoine499c7352019-06-04 14:14:33 +020013void dummy_init()
14{
Philippe Antoine08633822019-06-04 14:03:06 +020015#if defined(MBEDTLS_PLATFORM_TIME_ALT)
16 mbedtls_platform_set_time( dummy_constant_time );
17#else
18 fprintf(stderr, "Warning: fuzzing without constant time\n");
19#endif
20}
Philippe Antoine499c7352019-06-04 14:14:33 +020021
22int dummy_send( void *ctx, const unsigned char *buf, size_t len )
23{
24 //silence warning about unused parameter
25 (void) ctx;
26 (void) buf;
27
28 //pretends we wrote everything ok
Philippe Antoinea82fdd42019-07-10 13:53:40 +020029 if( len > INT_MAX ) {
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020030 return -1 ;
Philippe Antoine3e408d52019-07-10 01:09:50 +020031 }
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020032 return (int) len ;
Philippe Antoine499c7352019-06-04 14:14:33 +020033}
34
35int fuzz_recv( void *ctx, unsigned char *buf, size_t len )
36{
37 //reads from the buffer from fuzzer
38 fuzzBufferOffset_t * biomemfuzz = (fuzzBufferOffset_t *) ctx;
39
Philippe Antoinea82fdd42019-07-10 13:53:40 +020040 if(biomemfuzz->Offset == biomemfuzz->Size) {
Philippe Antoine499c7352019-06-04 14:14:33 +020041 //EOF
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020042 return 0 ;
Philippe Antoine499c7352019-06-04 14:14:33 +020043 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020044 if( len > INT_MAX ) {
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020045 return -1 ;
Philippe Antoine23219452019-07-10 08:26:04 +020046 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020047 if( len + biomemfuzz->Offset > biomemfuzz->Size ) {
Philippe Antoine499c7352019-06-04 14:14:33 +020048 //do not overflow
49 len = biomemfuzz->Size - biomemfuzz->Offset;
50 }
51 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
52 biomemfuzz->Offset += len;
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020053 return (int) len ;
Philippe Antoine499c7352019-06-04 14:14:33 +020054}
55
56int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
57{
58 int ret;
59 size_t i;
60
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020061#if defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine499c7352019-06-04 14:14:33 +020062 //use mbedtls_ctr_drbg_random to find bugs in it
63 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020064#else
65 (void) p_rng;
66 ret = 0;
67#endif
Philippe Antoine499c7352019-06-04 14:14:33 +020068 for (i=0; i<output_len; i++) {
69 //replace result with pseudo random
70 output[i] = (unsigned char) rand();
71 }
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020072 return ret ;
Philippe Antoine499c7352019-06-04 14:14:33 +020073}
74
75int dummy_entropy( void *data, unsigned char *output, size_t len )
76{
77 size_t i;
78 (void) data;
79
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();
86 }
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020087 return 0 ;
Philippe Antoine499c7352019-06-04 14:14:33 +020088}
89
90int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
91 uint32_t timeout )
92{
93 (void) timeout;
94
95 return fuzz_recv(ctx, buf, len);
96}