/src/krb5/src/tests/fuzzing/fuzz_aes.c
Line | Count | Source |
1 | | /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | | /* tests/fuzzing/fuzz_aes.c - fuzzing harness for AES encryption/decryption */ |
3 | | /* |
4 | | * Copyright (C) 2024 by Arjun. All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * |
10 | | * * Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * |
13 | | * * Redistributions in binary form must reproduce the above copyright |
14 | | * notice, this list of conditions and the following disclaimer in |
15 | | * the documentation and/or other materials provided with the |
16 | | * distribution. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
21 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
22 | | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
23 | | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
24 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
25 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
27 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
29 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | | */ |
31 | | |
32 | | #include "autoconf.h" |
33 | | #include <k5-int.h> |
34 | | #include <crypto_int.h> |
35 | | |
36 | 2.35k | #define kMinInputLength 48 |
37 | 1.16k | #define kMaxInputLength 512 |
38 | | |
39 | | extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
40 | | |
41 | | static void |
42 | | fuzz_aes(const uint8_t *data, size_t size, size_t key_size, krb5_enctype etype) |
43 | 152 | { |
44 | 152 | krb5_error_code ret; |
45 | 152 | krb5_keyblock keyblock; |
46 | 152 | krb5_crypto_iov iov; |
47 | 152 | krb5_key key = NULL; |
48 | 152 | char *aeskey = NULL, *data_in = NULL; |
49 | 152 | char encivbuf[16] = { 0 }, decivbuf[16] = { 0 }; |
50 | 152 | krb5_data enciv = make_data(encivbuf, 16), deciv = make_data(decivbuf, 16); |
51 | | |
52 | 152 | aeskey = k5memdup(data, key_size, &ret); |
53 | 152 | if (ret) |
54 | 0 | return; |
55 | | |
56 | 152 | data_in = k5memdup(data + key_size, size - key_size, &ret); |
57 | 152 | if (ret) |
58 | 0 | goto cleanup; |
59 | | |
60 | 152 | keyblock.contents = (krb5_octet *)aeskey; |
61 | 152 | keyblock.length = key_size; |
62 | 152 | keyblock.enctype = etype; |
63 | | |
64 | 152 | ret = krb5_k_create_key(NULL, &keyblock, &key); |
65 | 152 | if (ret) |
66 | 0 | goto cleanup; |
67 | | |
68 | 152 | iov.flags = KRB5_CRYPTO_TYPE_DATA; |
69 | 152 | iov.data = make_data(data_in, size - key_size); |
70 | | |
71 | | /* iov.data.data is input and output buffer */ |
72 | 152 | ret = krb5int_aes_encrypt(key, &enciv, &iov, 1); |
73 | 152 | if (ret) |
74 | 0 | goto cleanup; |
75 | | |
76 | 152 | ret = krb5int_aes_decrypt(key, &deciv, &iov, 1); |
77 | 152 | if (ret) |
78 | 0 | goto cleanup; |
79 | | |
80 | | /* Check that decryption result matches original plaintext. */ |
81 | 152 | ret = memcmp(data_in, data + key_size, size - key_size); |
82 | 152 | if (ret) |
83 | 0 | abort(); |
84 | | |
85 | 152 | (void)krb5int_aes_decrypt(key, &deciv, &iov, 1); |
86 | | |
87 | 152 | cleanup: |
88 | 152 | free(aeskey); |
89 | 152 | free(data_in); |
90 | 152 | krb5_k_free_key(NULL, key); |
91 | 152 | } |
92 | | |
93 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
94 | 1.17k | { |
95 | 1.17k | if (size < kMinInputLength || size > kMaxInputLength) |
96 | 88 | return 0; |
97 | | |
98 | 1.08k | fuzz_aes(data, size, 16, ENCTYPE_AES128_CTS_HMAC_SHA1_96); |
99 | 1.08k | fuzz_aes(data, size, 16, ENCTYPE_AES256_CTS_HMAC_SHA1_96); |
100 | 1.08k | fuzz_aes(data, size, 32, ENCTYPE_AES128_CTS_HMAC_SHA1_96); |
101 | 1.08k | fuzz_aes(data, size, 32, ENCTYPE_AES256_CTS_HMAC_SHA1_96); |
102 | | |
103 | 1.08k | return 0; |
104 | 1.17k | } |