Coverage Report

Created: 2025-12-14 06:09

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