fuzz-crypto.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2023 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <pjlib.h>
  22. #include <pjlib-util.h>
  23. #define OPENSSL_SUPPRESS_DEPRECATED 1
  24. #include <openssl/bio.h>
  25. #include <openssl/evp.h>
  26. #include <openssl/sha.h>
  27. #include <openssl/md5.h>
  28. #include <zlib.h>
  29. #define kMinInputLength 10
  30. #define kMaxInputLength 1024
  31. #define MAXSIZE 5120
  32. void encode_base64_differential(const uint8_t *Data, size_t Size) {
  33. //PJSIP
  34. char pj_output[MAXSIZE];
  35. int pj_output_len = MAXSIZE;
  36. memset(pj_output, 0, MAXSIZE);
  37. pj_base64_encode(Data, Size, pj_output, &pj_output_len);
  38. //OPENSSL
  39. BIO *bio, *bio_mem;
  40. char *ssl_output = NULL;
  41. int ssl_output_len;
  42. bio = BIO_new(BIO_f_base64());
  43. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  44. bio_mem = BIO_new(BIO_s_mem());
  45. BIO_push(bio, bio_mem);
  46. BIO_write(bio, Data, Size);
  47. BIO_flush(bio);
  48. ssl_output_len = BIO_get_mem_data(bio_mem, &ssl_output);
  49. if (ssl_output_len <= 0) {
  50. abort();
  51. }
  52. //Differential
  53. int result = memcmp(pj_output, ssl_output, ssl_output_len);
  54. if(result != 0){
  55. abort();
  56. }
  57. BIO_free_all(bio);
  58. //PJSIP Decode After encode.
  59. pj_str_t pj_input;
  60. uint8_t pj_output_dec[MAXSIZE];
  61. int pj_output_dec_len = MAXSIZE;
  62. pj_input.ptr = pj_output;
  63. pj_input.slen = ssl_output_len;
  64. memset(pj_output_dec, 0, MAXSIZE);
  65. pj_base64_decode(&pj_input, pj_output_dec, &pj_output_dec_len);
  66. //Differential
  67. int result_dec = memcmp(pj_output_dec, Data, Size);
  68. if(result_dec != 0) {
  69. abort();
  70. }
  71. }
  72. void decode_base64_differential(const uint8_t *Data, size_t Size) {
  73. //PJSIP
  74. pj_str_t pj_input;
  75. uint8_t pj_output[MAXSIZE];
  76. int pj_output_len = MAXSIZE;
  77. pj_input.ptr = (char *)Data;
  78. pj_input.slen = Size;
  79. memset(pj_output, 0, MAXSIZE);
  80. pj_base64_decode(&pj_input, pj_output, &pj_output_len);
  81. }
  82. void md5_differential(const uint8_t *Data, size_t Size) {
  83. //PJSIP
  84. pj_md5_context ctx;
  85. pj_uint8_t pj_md5_hash[MD5_DIGEST_LENGTH];
  86. pj_md5_init(&ctx);
  87. pj_md5_update(&ctx, Data,Size);
  88. pj_md5_final(&ctx, pj_md5_hash);
  89. //OPENSSL
  90. uint8_t ssl_md5_hash[MD5_DIGEST_LENGTH] = {};
  91. MD5(Data, Size, ssl_md5_hash);
  92. //Differential
  93. int result = memcmp(pj_md5_hash, ssl_md5_hash, MD5_DIGEST_LENGTH);
  94. if(result != 0){
  95. abort();
  96. }
  97. }
  98. void sha1_differential(const uint8_t *Data, size_t Size) {
  99. //PJSIP
  100. pj_sha1_context pj_sha;
  101. pj_uint8_t pj_sha_hash[SHA_DIGEST_LENGTH];
  102. pj_sha1_init(&pj_sha);
  103. pj_sha1_update(&pj_sha,Data,Size);
  104. pj_sha1_final(&pj_sha,pj_sha_hash);
  105. //OPENSSL
  106. uint8_t ssl_sha_hash[SHA_DIGEST_LENGTH] = {};
  107. SHA_CTX ctx;
  108. SHA1_Init(&ctx);
  109. SHA1_Update(&ctx,Data,Size);
  110. SHA1_Final(ssl_sha_hash, &ctx);
  111. //Differential
  112. int result = memcmp(pj_sha_hash, ssl_sha_hash, SHA_DIGEST_LENGTH);
  113. if(result != 0){
  114. abort();
  115. }
  116. }
  117. void crc32_differential(const uint8_t *Data, size_t Size) {
  118. //PJSIP
  119. pj_uint32_t pj_crc;
  120. pj_crc = pj_crc32_calc(Data, Size);
  121. //zlib
  122. uint32_t zlib_crc;
  123. zlib_crc = crc32(0L, Data, Size);
  124. //Differential
  125. if (pj_crc != zlib_crc) {
  126. abort();
  127. }
  128. }
  129. extern int
  130. LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  131. {
  132. if (Size < kMinInputLength || Size > kMaxInputLength) {
  133. return 1;
  134. }
  135. encode_base64_differential(Data, Size);
  136. decode_base64_differential(Data, Size);
  137. md5_differential(Data, Size);
  138. sha1_differential(Data, Size);
  139. crc32_differential(Data, Size);
  140. return 0;
  141. }