1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "AES.c"
16 #include "common.h"
17 #include <fuzzer/FuzzedDataProvider.h>
18
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)19 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
20
21 if (!size)
22 return 0;
23
24 enum KeySize { AES128 = 16, AES192 = 24, AES256 = 32, kMaxValue = AES256 };
25
26 FuzzedDataProvider stream(data, size);
27 const KeySize keySize = stream.ConsumeEnum<KeySize>();
28 if (stream.remaining_bytes() < keySize)
29 return 0;
30
31 std::vector<uint8_t> keyBuf = stream.ConsumeBytes<uint8_t>(keySize);
32 const uint8_t *key = keyBuf.data();
33
34 BlockBase *state;
35 if (AES_start_operation(key, keySize, reinterpret_cast<AES_State **>(&state)))
36 return 0;
37
38 uint8_t outEnc[size];
39 uint8_t outDec[size];
40
41 AES_encrypt(reinterpret_cast<BlockBase *>(state), data, outEnc, size);
42 AES_decrypt(reinterpret_cast<BlockBase *>(state), data, outDec, size);
43
44 AES_stop_operation(reinterpret_cast<BlockBase *>(state));
45
46 return 0;
47 }
48