Path: blob/main/crypto/krb5/src/tests/fuzzing/fuzz_aes.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/fuzzing/fuzz_aes.c - fuzzing harness for AES encryption/decryption */2/*3* Copyright (C) 2024 by Arjun. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS20* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE21* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,22* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR24* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,26* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED28* OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031#include "autoconf.h"32#include <k5-int.h>33#include <crypto_int.h>3435#define kMinInputLength 4836#define kMaxInputLength 5123738extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);3940static void41fuzz_aes(const uint8_t *data, size_t size, size_t key_size, krb5_enctype etype)42{43krb5_error_code ret;44krb5_keyblock keyblock;45krb5_crypto_iov iov;46krb5_key key = NULL;47char *aeskey = NULL, *data_in = NULL;48char encivbuf[16] = { 0 }, decivbuf[16] = { 0 };49krb5_data enciv = make_data(encivbuf, 16), deciv = make_data(decivbuf, 16);5051aeskey = k5memdup(data, key_size, &ret);52if (ret)53return;5455data_in = k5memdup(data + key_size, size - key_size, &ret);56if (ret)57goto cleanup;5859keyblock.contents = (krb5_octet *)aeskey;60keyblock.length = key_size;61keyblock.enctype = etype;6263ret = krb5_k_create_key(NULL, &keyblock, &key);64if (ret)65goto cleanup;6667iov.flags = KRB5_CRYPTO_TYPE_DATA;68iov.data = make_data(data_in, size - key_size);6970/* iov.data.data is input and output buffer */71ret = krb5int_aes_encrypt(key, &enciv, &iov, 1);72if (ret)73goto cleanup;7475ret = krb5int_aes_decrypt(key, &deciv, &iov, 1);76if (ret)77goto cleanup;7879/* Check that decryption result matches original plaintext. */80ret = memcmp(data_in, data + key_size, size - key_size);81if (ret)82abort();8384(void)krb5int_aes_decrypt(key, &deciv, &iov, 1);8586cleanup:87free(aeskey);88free(data_in);89krb5_k_free_key(NULL, key);90}9192int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)93{94if (size < kMinInputLength || size > kMaxInputLength)95return 0;9697fuzz_aes(data, size, 16, ENCTYPE_AES128_CTS_HMAC_SHA1_96);98fuzz_aes(data, size, 16, ENCTYPE_AES256_CTS_HMAC_SHA1_96);99fuzz_aes(data, size, 32, ENCTYPE_AES128_CTS_HMAC_SHA1_96);100fuzz_aes(data, size, 32, ENCTYPE_AES256_CTS_HMAC_SHA1_96);101102return 0;103}104105106