blob: 96a24f7575e62c429443b68d2b67f3f6e5a99abb [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
Andrzej Kurek448cf482022-03-02 10:56:22 -05008#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Axtens301db662020-05-28 11:43:41 +100014#endif
Philippe Antoine08633822019-06-04 14:03:06 +020015
Gowtham Suresh Kumar34d8bd32023-07-26 17:18:55 +010016void dummy_init(void)
Philippe Antoine499c7352019-06-04 14:14:33 +020017{
David Horstmann11d0a6f2021-11-29 18:57:10 +000018#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +010032 if (len > INT_MAX) {
33 return -1;
Philippe Antoine3e408d52019-07-10 01:09:50 +020034 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035 return (int) len;
Philippe Antoine499c7352019-06-04 14:14:33 +020036}
37
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +010041 fuzzBufferOffset_t *biomemfuzz = (fuzzBufferOffset_t *) ctx;
Philippe Antoine499c7352019-06-04 14:14:33 +020042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043 if (biomemfuzz->Offset == biomemfuzz->Size) {
Philippe Antoine499c7352019-06-04 14:14:33 +020044 //EOF
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 return 0;
Philippe Antoine499c7352019-06-04 14:14:33 +020046 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047 if (len > INT_MAX) {
48 return -1;
Philippe Antoine23219452019-07-10 08:26:04 +020049 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +010056 return (int) len;
Philippe Antoine499c7352019-06-04 14:14:33 +020057}
58
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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)
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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 for (i = 0; i < output_len; i++) {
Philippe Antoine499c7352019-06-04 14:14:33 +020072 //replace result with pseudo random
73 output[i] = (unsigned char) rand();
74 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 return ret;
Philippe Antoine499c7352019-06-04 14:14:33 +020076}
77
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078int dummy_entropy(void *data, unsigned char *output, size_t len)
Philippe Antoine499c7352019-06-04 14:14:33 +020079{
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);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 for (i = 0; i < len; i++) {
Philippe Antoine499c7352019-06-04 14:14:33 +020087 //replace result with pseudo random
88 output[i] = (unsigned char) rand();
89 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 return 0;
Philippe Antoine499c7352019-06-04 14:14:33 +020091}
92
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093int fuzz_recv_timeout(void *ctx, unsigned char *buf, size_t len,
94 uint32_t timeout)
Philippe Antoine499c7352019-06-04 14:14:33 +020095{
96 (void) timeout;
97
98 return fuzz_recv(ctx, buf, len);
99}