common.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "common.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "mbedtls/ctr_drbg.h"
  6. mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
  7. {
  8. (void) time;
  9. return 0x5af2a056;
  10. }
  11. void dummy_init()
  12. {
  13. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  14. mbedtls_platform_set_time( dummy_constant_time );
  15. #else
  16. fprintf(stderr, "Warning: fuzzing without constant time\n");
  17. #endif
  18. }
  19. int dummy_send( void *ctx, const unsigned char *buf, size_t len )
  20. {
  21. //silence warning about unused parameter
  22. (void) ctx;
  23. (void) buf;
  24. //pretends we wrote everything ok
  25. if( len > INT_MAX ) {
  26. return( -1 );
  27. }
  28. return( (int) len );
  29. }
  30. int fuzz_recv( void *ctx, unsigned char *buf, size_t len )
  31. {
  32. //reads from the buffer from fuzzer
  33. fuzzBufferOffset_t * biomemfuzz = (fuzzBufferOffset_t *) ctx;
  34. if(biomemfuzz->Offset == biomemfuzz->Size) {
  35. //EOF
  36. return( 0 );
  37. }
  38. if( len > INT_MAX ) {
  39. return( -1 );
  40. }
  41. if( len + biomemfuzz->Offset > biomemfuzz->Size ) {
  42. //do not overflow
  43. len = biomemfuzz->Size - biomemfuzz->Offset;
  44. }
  45. memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
  46. biomemfuzz->Offset += len;
  47. return( (int) len );
  48. }
  49. int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
  50. {
  51. int ret;
  52. size_t i;
  53. #if defined(MBEDTLS_CTR_DRBG_C)
  54. //use mbedtls_ctr_drbg_random to find bugs in it
  55. ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
  56. #else
  57. (void) p_rng;
  58. ret = 0;
  59. #endif
  60. for (i=0; i<output_len; i++) {
  61. //replace result with pseudo random
  62. output[i] = (unsigned char) rand();
  63. }
  64. return( ret );
  65. }
  66. int dummy_entropy( void *data, unsigned char *output, size_t len )
  67. {
  68. size_t i;
  69. (void) data;
  70. //use mbedtls_entropy_func to find bugs in it
  71. //test performance impact of entropy
  72. //ret = mbedtls_entropy_func(data, output, len);
  73. for (i=0; i<len; i++) {
  74. //replace result with pseudo random
  75. output[i] = (unsigned char) rand();
  76. }
  77. return( 0 );
  78. }
  79. int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
  80. uint32_t timeout )
  81. {
  82. (void) timeout;
  83. return fuzz_recv(ctx, buf, len);
  84. }