Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-crypto.c

raw symbols SCIP
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
22 #include <pjlib.h>
23 #include <pjlib-util.h>
24
25 #define OPENSSL_SUPPRESS_DEPRECATED 1
26 #include <openssl/bio.h>
27 #include <openssl/evp.h>
28 #include <openssl/sha.h>
29 #include <openssl/md5.h>
30 #include <zlib.h>
31
32 #define kMinInputLength 10
33 #define kMaxInputLength 1024
34
35 #define MAXSIZE 5120
36
37 void encode_base64_differential(const uint8_t *Data, size_t Size) {
38
39 //PJSIP
40 char pj_output[MAXSIZE];
41 int pj_output_len = MAXSIZE;
42
43 memset(pj_output, 0, MAXSIZE);
44 pj_base64_encode(Data, Size, pj_output, &pj_output_len);
45
46 //OPENSSL
47 BIO *bio, *bio_mem;
48 char *ssl_output = NULL;
49 int ssl_output_len;
50
51 bio = BIO_new(BIO_f_base64());
52 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
53
54 bio_mem = BIO_new(BIO_s_mem());
55 BIO_push(bio, bio_mem);
56
57 BIO_write(bio, Data, Size);
58 BIO_flush(bio);
59
60 ssl_output_len = BIO_get_mem_data(bio_mem, &ssl_output);
61 if (ssl_output_len <= 0) {
62 abort();
63 }
64
65 //Differential
66 int result = memcmp(pj_output, ssl_output, ssl_output_len);
67 if(result != 0){
68 abort();
69 }
70 BIO_free_all(bio);
71
72 //PJSIP Decode After encode.
73 pj_str_t pj_input;
74 uint8_t pj_output_dec[MAXSIZE];
75 int pj_output_dec_len = MAXSIZE;
76
77 pj_input.ptr = pj_output;
78 pj_input.slen = ssl_output_len;
79
80 memset(pj_output_dec, 0, MAXSIZE);
81 pj_base64_decode(&pj_input, pj_output_dec, &pj_output_dec_len);
82
83 //Differential
84 int result_dec = memcmp(pj_output_dec, Data, Size);
85 if(result_dec != 0) {
86 abort();
87 }
88 }
89
90 void decode_base64_differential(const uint8_t *Data, size_t Size) {
91
92 //PJSIP
93 pj_str_t pj_input;
94 uint8_t pj_output[MAXSIZE];
95 int pj_output_len = MAXSIZE;
96
97 pj_input.ptr = (char *)Data;
98 pj_input.slen = Size;
99
100 memset(pj_output, 0, MAXSIZE);
101 pj_base64_decode(&pj_input, pj_output, &pj_output_len);
102 }
103
104 void md5_differential(const uint8_t *Data, size_t Size) {
105
106 //PJSIP
107 pj_md5_context ctx;
108 pj_uint8_t pj_md5_hash[MD5_DIGEST_LENGTH];
109
110 pj_md5_init(&ctx);
111 pj_md5_update(&ctx, Data,Size);
112 pj_md5_final(&ctx, pj_md5_hash);
113
114 //OPENSSL
115 uint8_t ssl_md5_hash[MD5_DIGEST_LENGTH] = {};
116 MD5(Data, Size, ssl_md5_hash);
117
118 //Differential
119 int result = memcmp(pj_md5_hash, ssl_md5_hash, MD5_DIGEST_LENGTH);
120 if(result != 0){
121 abort();
122 }
123 }
124
125 void sha1_differential(const uint8_t *Data, size_t Size) {
126
127 //PJSIP
128 pj_sha1_context pj_sha;
129 pj_uint8_t pj_sha_hash[SHA_DIGEST_LENGTH];
130
131 pj_sha1_init(&pj_sha);
132 pj_sha1_update(&pj_sha,Data,Size);
133 pj_sha1_final(&pj_sha,pj_sha_hash);
134
135 //OPENSSL
136 uint8_t ssl_sha_hash[SHA_DIGEST_LENGTH] = {};
137
138 SHA_CTX ctx;
139 SHA1_Init(&ctx);
140 SHA1_Update(&ctx,Data,Size);
141 SHA1_Final(ssl_sha_hash, &ctx);
142
143 //Differential
144 int result = memcmp(pj_sha_hash, ssl_sha_hash, SHA_DIGEST_LENGTH);
145 if(result != 0){
146 abort();
147 }
148 }
149
150 void crc32_differential(const uint8_t *Data, size_t Size) {
151
152 //PJSIP
153 pj_uint32_t pj_crc;
154 pj_crc = pj_crc32_calc(Data, Size);
155
156 //zlib
157 uint32_t zlib_crc;
158 zlib_crc = crc32(0L, Data, Size);
159
160 //Differential
161 if (pj_crc != zlib_crc) {
162 abort();
163 }
164 }
165
166 extern int
167 LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
168 {
169
170 if (Size < kMinInputLength || Size > kMaxInputLength) {
171 return 1;
172 }
173
174 encode_base64_differential(Data, Size);
175 decode_base64_differential(Data, Size);
176 md5_differential(Data, Size);
177 sha1_differential(Data, Size);
178 crc32_differential(Data, Size);
179
180 return 0;
181 }