/src/libhmac/ossfuzz/md5_context_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libhmac MD5 context type |
3 | | * |
4 | | * Copyright (C) 2011-2026, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <stddef.h> |
23 | | #include <stdint.h> |
24 | | |
25 | | /* Note that some of the OSS-Fuzz engines use C++ |
26 | | */ |
27 | | extern "C" { |
28 | | |
29 | | #include "ossfuzz_libhmac.h" |
30 | | |
31 | | int LLVMFuzzerTestOneInput( |
32 | | const uint8_t *data, |
33 | | size_t size ) |
34 | 675 | { |
35 | 675 | uint8_t md5_hash[ LIBHMAC_MD5_HASH_SIZE ]; |
36 | 675 | uint8_t small_hash[ 1 ]; |
37 | | |
38 | 675 | libhmac_md5_context_t *context = NULL; |
39 | 675 | size_t maximum_update_size = 0; |
40 | 675 | size_t offset = 0; |
41 | 675 | size_t update_size = 0; |
42 | | |
43 | 675 | libhmac_md5_calculate( |
44 | 675 | data, |
45 | 675 | size, |
46 | 675 | md5_hash, |
47 | 675 | LIBHMAC_MD5_HASH_SIZE, |
48 | 675 | NULL ); |
49 | | |
50 | | /* Exercise invalid argument handling |
51 | | */ |
52 | 675 | libhmac_md5_context_initialize( |
53 | 675 | NULL, |
54 | 675 | NULL ); |
55 | | |
56 | 675 | libhmac_md5_context_update( |
57 | 675 | NULL, |
58 | 675 | data, |
59 | 675 | size, |
60 | 675 | NULL ); |
61 | | |
62 | 675 | libhmac_md5_context_finalize( |
63 | 675 | NULL, |
64 | 675 | md5_hash, |
65 | 675 | LIBHMAC_MD5_HASH_SIZE, |
66 | 675 | NULL ); |
67 | | |
68 | 675 | libhmac_md5_context_free( |
69 | 675 | NULL, |
70 | 675 | NULL ); |
71 | | |
72 | 675 | if( libhmac_md5_context_initialize( |
73 | 675 | &context, |
74 | 675 | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | return( 0 ); |
77 | 0 | } |
78 | 25.1M | while( offset < size ) |
79 | 25.1M | { |
80 | 25.1M | maximum_update_size = 1 + ( size_t ) ( data[ offset ] & 0x7f ) * 2; |
81 | | |
82 | 25.1M | update_size = size - offset; |
83 | | |
84 | 25.1M | if( update_size > maximum_update_size ) |
85 | 25.1M | { |
86 | 25.1M | update_size = maximum_update_size; |
87 | 25.1M | } |
88 | 25.1M | libhmac_md5_context_update( |
89 | 25.1M | context, |
90 | 25.1M | &( data[ offset ] ), |
91 | 25.1M | update_size, |
92 | 25.1M | NULL ); |
93 | | |
94 | 25.1M | offset += update_size; |
95 | 25.1M | } |
96 | | /* Exercise invalid argument handling |
97 | | */ |
98 | 675 | libhmac_md5_context_finalize( |
99 | 675 | context, |
100 | 675 | small_hash, |
101 | 675 | sizeof( small_hash ), |
102 | 675 | NULL ); |
103 | | |
104 | 675 | libhmac_md5_context_finalize( |
105 | 675 | context, |
106 | 675 | md5_hash, |
107 | 675 | LIBHMAC_MD5_HASH_SIZE, |
108 | 675 | NULL ); |
109 | | |
110 | 675 | libhmac_md5_context_free( |
111 | 675 | &context, |
112 | 675 | NULL ); |
113 | | |
114 | 675 | return( 0 ); |
115 | 675 | } |
116 | | |
117 | | } /* extern "C" */ |
118 | | |