/src/libfvde/ossfuzz/encryption_context_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfvde encryption context type |
3 | | * |
4 | | * Copyright (C) 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 | | #include <string.h> |
25 | | |
26 | | /* Note that some of the OSS-Fuzz engines use C++ |
27 | | */ |
28 | | extern "C" { |
29 | | |
30 | | #include "ossfuzz_libbfio.h" |
31 | | #include "ossfuzz_libfvde.h" |
32 | | |
33 | | #include "../libfvde/libfvde_definitions.h" |
34 | | #include "../libfvde/libfvde_encryption_context.h" |
35 | | |
36 | | int LLVMFuzzerTestOneInput( |
37 | | const uint8_t *data, |
38 | | size_t size ) |
39 | 65 | { |
40 | 65 | uint8_t input_data[ 256 ]; |
41 | 65 | uint8_t output_data[ 256 ]; |
42 | 65 | uint8_t unwrapped_data[ 64 ]; |
43 | | |
44 | | /* The AES-128 key |
45 | | */ |
46 | 65 | uint8_t key[ 16 ] = |
47 | 65 | { |
48 | 65 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
49 | 65 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 |
50 | 65 | }; |
51 | | |
52 | | /* The AES-128 tweak key |
53 | | */ |
54 | 65 | uint8_t tweak_key[ 16 ] = |
55 | 65 | { |
56 | 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
57 | 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
58 | 65 | }; |
59 | | |
60 | 65 | libfvde_encryption_context_t *encryption_context = NULL; |
61 | 65 | size_t data_size = 0; |
62 | | |
63 | 65 | if( size > 256 ) |
64 | 27 | { |
65 | 27 | data_size = 256; |
66 | 27 | } |
67 | 38 | else |
68 | 38 | { |
69 | 38 | data_size = size; |
70 | 38 | } |
71 | 65 | if( data_size > 0 ) |
72 | 65 | { |
73 | 65 | memcpy( input_data, data, data_size ); |
74 | 65 | } |
75 | | /* Note the AES-XTS encryption requires the data size |
76 | | * to be a multiple of the block size (16 bytes). |
77 | | */ |
78 | 65 | data_size = ( data_size / 16 ) * 16; |
79 | | |
80 | 65 | if( libfvde_encryption_context_initialize( |
81 | 65 | &encryption_context, |
82 | 65 | LIBFVDE_ENCRYPTION_METHOD_AES_128_XTS, |
83 | 65 | NULL ) != 1 ) |
84 | 0 | { |
85 | 0 | return( 0 ); |
86 | 0 | } |
87 | 65 | if( libfvde_encryption_context_set_keys( |
88 | 65 | encryption_context, |
89 | 65 | key, |
90 | 65 | 16, |
91 | 65 | tweak_key, |
92 | 65 | 16, |
93 | 65 | NULL ) != 1 ) |
94 | 0 | { |
95 | 0 | goto on_error_libfvde_encryption_context; |
96 | 0 | } |
97 | 65 | if( data_size > 0 ) |
98 | 59 | { |
99 | 59 | libfvde_encryption_context_crypt( |
100 | 59 | encryption_context, |
101 | 59 | LIBFVDE_ENCRYPTION_CRYPT_MODE_DECRYPT, |
102 | 59 | input_data, |
103 | 59 | data_size, |
104 | 59 | output_data, |
105 | 59 | data_size, |
106 | 59 | 0, |
107 | 59 | NULL ); |
108 | | |
109 | 59 | libfvde_encryption_context_crypt( |
110 | 59 | encryption_context, |
111 | 59 | LIBFVDE_ENCRYPTION_CRYPT_MODE_DECRYPT, |
112 | 59 | input_data, |
113 | 59 | data_size, |
114 | 59 | output_data, |
115 | 59 | data_size, |
116 | 59 | 1, |
117 | 59 | NULL ); |
118 | 59 | } |
119 | 65 | libfvde_encryption_context_crypt( |
120 | 65 | encryption_context, |
121 | 65 | LIBFVDE_ENCRYPTION_CRYPT_MODE_ENCRYPT, |
122 | 65 | input_data, |
123 | 65 | 16, |
124 | 65 | output_data, |
125 | 65 | 16, |
126 | 65 | 0, |
127 | 65 | NULL ); |
128 | 65 | on_error_libfvde_encryption_context: |
129 | 65 | libfvde_encryption_context_free( |
130 | 65 | &encryption_context, |
131 | 65 | NULL ); |
132 | | |
133 | | /* Unwraps a wrapped key using AES |
134 | | */ |
135 | 65 | data_size = ( size > 48 ) ? 48 : size; |
136 | 65 | data_size = ( data_size / 8 ) * 8; |
137 | | |
138 | 65 | if( data_size > 8 ) |
139 | 59 | { |
140 | 59 | libfvde_encryption_aes_key_unwrap( |
141 | 59 | key, |
142 | 59 | 128, |
143 | 59 | data, |
144 | 59 | data_size, |
145 | 59 | unwrapped_data, |
146 | 59 | sizeof( unwrapped_data ), |
147 | | NULL ); |
148 | 59 | } |
149 | 65 | return( 0 ); |
150 | 65 | } |
151 | | |
152 | | } /* extern "C" */ |