blob: 98aa4037b3607f4c9fab96f32aecf69702ae7666 [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
Andrzej Kurek06a00af2022-03-02 10:56:22 -05008#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01009mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
Philippe Antoine499c7352019-06-04 14:14:33 +020010{
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
Gowtham Suresh Kumar186731b2023-07-26 15:47:45 +010016void dummy_init(void)
Philippe Antoine499c7352019-06-04 14:14:33 +020017{
David Horstmannca534592021-11-29 18:57:10 +000018#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010019 mbedtls_platform_set_time(dummy_constant_time);
Philippe Antoine08633822019-06-04 14:03:06 +020020#else
21 fprintf(stderr, "Warning: fuzzing without constant time\n");
22#endif
23}
Philippe Antoine499c7352019-06-04 14:14:33 +020024
Gilles Peskine449bd832023-01-11 14:50:10 +010025int dummy_send(void *ctx, const unsigned char *buf, size_t len)
Philippe Antoine499c7352019-06-04 14:14:33 +020026{
27 //silence warning about unused parameter
28 (void) ctx;
29 (void) buf;
30
31 //pretends we wrote everything ok
Gilles Peskine449bd832023-01-11 14:50:10 +010032 if (len > INT_MAX) {
33 return -1;
Philippe Antoine3e408d52019-07-10 01:09:50 +020034 }
Gilles Peskine449bd832023-01-11 14:50:10 +010035 return (int) len;
Philippe Antoine499c7352019-06-04 14:14:33 +020036}
37
Gilles Peskine449bd832023-01-11 14:50:10 +010038int fuzz_recv(void *ctx, unsigned char *buf, size_t len)
Philippe Antoine499c7352019-06-04 14:14:33 +020039{
40 //reads from the buffer from fuzzer
Gilles Peskine449bd832023-01-11 14:50:10 +010041 fuzzBufferOffset_t *biomemfuzz = (fuzzBufferOffset_t *) ctx;
Philippe Antoine499c7352019-06-04 14:14:33 +020042
Gilles Peskine449bd832023-01-11 14:50:10 +010043 if (biomemfuzz->Offset == biomemfuzz->Size) {
Philippe Antoine499c7352019-06-04 14:14:33 +020044 //EOF
Gilles Peskine449bd832023-01-11 14:50:10 +010045 return 0;
Philippe Antoine499c7352019-06-04 14:14:33 +020046 }
Gilles Peskine449bd832023-01-11 14:50:10 +010047 if (len > INT_MAX) {
48 return -1;
Philippe Antoine23219452019-07-10 08:26:04 +020049 }
Gilles Peskine449bd832023-01-11 14:50:10 +010050 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;
Gilles Peskine449bd832023-01-11 14:50:10 +010056 return (int) len;
Philippe Antoine499c7352019-06-04 14:14:33 +020057}
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059int dummy_random(void *p_rng, unsigned char *output, size_t output_len)
Philippe Antoine499c7352019-06-04 14:14:33 +020060{
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
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (p_rng != NULL) {
Paul Elliotta1dc3e52022-02-14 18:26:21 +000067 //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
Gilles Peskine449bd832023-01-11 14:50:10 +010077 for (i = 0; i < output_len; i++) {
Philippe Antoine499c7352019-06-04 14:14:33 +020078 //replace result with pseudo random
79 output[i] = (unsigned char) rand();
80 }
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return ret;
Philippe Antoine499c7352019-06-04 14:14:33 +020082}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int dummy_entropy(void *data, unsigned char *output, size_t len)
Philippe Antoine499c7352019-06-04 14:14:33 +020085{
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);
Gilles Peskine449bd832023-01-11 14:50:10 +010092 for (i = 0; i < len; i++) {
Philippe Antoine499c7352019-06-04 14:14:33 +020093 //replace result with pseudo random
94 output[i] = (unsigned char) rand();
95 }
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return 0;
Philippe Antoine499c7352019-06-04 14:14:33 +020097}
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099int fuzz_recv_timeout(void *ctx, unsigned char *buf, size_t len,
100 uint32_t timeout)
Philippe Antoine499c7352019-06-04 14:14:33 +0200101{
102 (void) timeout;
103
104 return fuzz_recv(ctx, buf, len);
105}