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 #define kMinInputLength 48
37 #define kMaxInputLength 512
38
39 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
40
41 static void
fuzz_aes(const uint8_t * data,size_t size,size_t key_size,krb5_enctype etype)42 fuzz_aes(const uint8_t *data, size_t size, size_t key_size, krb5_enctype etype)
43 {
44 krb5_error_code ret;
45 krb5_keyblock keyblock;
46 krb5_crypto_iov iov;
47 krb5_key key = NULL;
48 char *aeskey = NULL, *data_in = NULL;
49 char encivbuf[16] = { 0 }, decivbuf[16] = { 0 };
50 krb5_data enciv = make_data(encivbuf, 16), deciv = make_data(decivbuf, 16);
51
52 aeskey = k5memdup(data, key_size, &ret);
53 if (ret)
54 return;
55
56 data_in = k5memdup(data + key_size, size - key_size, &ret);
57 if (ret)
58 goto cleanup;
59
60 keyblock.contents = (krb5_octet *)aeskey;
61 keyblock.length = key_size;
62 keyblock.enctype = etype;
63
64 ret = krb5_k_create_key(NULL, &keyblock, &key);
65 if (ret)
66 goto cleanup;
67
68 iov.flags = KRB5_CRYPTO_TYPE_DATA;
69 iov.data = make_data(data_in, size - key_size);
70
71 /* iov.data.data is input and output buffer */
72 ret = krb5int_aes_encrypt(key, &enciv, &iov, 1);
73 if (ret)
74 goto cleanup;
75
76 ret = krb5int_aes_decrypt(key, &deciv, &iov, 1);
77 if (ret)
78 goto cleanup;
79
80 /* Check that decryption result matches original plaintext. */
81 ret = memcmp(data_in, data + key_size, size - key_size);
82 if (ret)
83 abort();
84
85 (void)krb5int_aes_decrypt(key, &deciv, &iov, 1);
86
87 cleanup:
88 free(aeskey);
89 free(data_in);
90 krb5_k_free_key(NULL, key);
91 }
92
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
94 {
95 if (size < kMinInputLength || size > kMaxInputLength)
96 return 0;
97
98 fuzz_aes(data, size, 16, ENCTYPE_AES128_CTS_HMAC_SHA1_96);
99 fuzz_aes(data, size, 16, ENCTYPE_AES256_CTS_HMAC_SHA1_96);
100 fuzz_aes(data, size, 32, ENCTYPE_AES128_CTS_HMAC_SHA1_96);
101 fuzz_aes(data, size, 32, ENCTYPE_AES256_CTS_HMAC_SHA1_96);
102
103 return 0;
104 }
105