Coverage Report

Created: 2024-07-23 06:24

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