Coverage Report

Created: 2025-08-03 06:03

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