common.c 2.2 KB

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