blob: 269c2e3e1ee2894512eeb8098b1efc822f6aa57f [file] [log] [blame]
Philippe Antoine08633822019-06-04 14:03:06 +02001#include "common.h"
Tom Cosgrove58efe612021-11-15 09:59: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 Axtens301db662020-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 Axtens301db662020-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{
David Horstmann11d0a6f2021-11-29 18:57:10 +000018#if 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)
Philippe Antoine499c7352019-06-04 14:14:33 +020065 //use mbedtls_ctr_drbg_random to find bugs in it
66 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020067#else
68 (void) p_rng;
69 ret = 0;
70#endif
Philippe Antoine499c7352019-06-04 14:14:33 +020071 for (i=0; i<output_len; i++) {
72 //replace result with pseudo random
73 output[i] = (unsigned char) rand();
74 }
75 return( ret );
76}
77
78int dummy_entropy( void *data, unsigned char *output, size_t len )
79{
80 size_t i;
81 (void) data;
82
83 //use mbedtls_entropy_func to find bugs in it
84 //test performance impact of entropy
85 //ret = mbedtls_entropy_func(data, output, len);
86 for (i=0; i<len; i++) {
87 //replace result with pseudo random
88 output[i] = (unsigned char) rand();
89 }
90 return( 0 );
91}
92
93int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
94 uint32_t timeout )
95{
96 (void) timeout;
97
98 return fuzz_recv(ctx, buf, len);
99}