Path: blob/main/crypto/krb5/src/tests/fuzzing/fuzz_des.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/fuzzing/fuzz_des.c - fuzzing harness for DES functions */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 <des_int.h>3435#include <f_cbc.c>3637#define kMinInputLength 3238#define kMaxInputLength 1283940extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);4142uint8_t default_ivec[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };4344static void45fuzz_des(uint8_t *input, mit_des_key_schedule sched)46{47uint8_t encrypt[8], decrypt[8];4849mit_des_cbc_encrypt((const mit_des_cblock *)input,50(mit_des_cblock *)encrypt, 8,51sched, default_ivec, MIT_DES_ENCRYPT);5253mit_des_cbc_encrypt((const mit_des_cblock *)encrypt,54(mit_des_cblock *)decrypt, 8,55sched, default_ivec, MIT_DES_DECRYPT);5657if (memcmp(input, decrypt, 8) != 0)58abort();59}6061static void62fuzz_decrypt(uint8_t *input, mit_des_key_schedule sched)63{64uint8_t output[8];6566mit_des_cbc_encrypt((const mit_des_cblock *)input,67(mit_des_cblock *)output, 8,68sched, default_ivec, MIT_DES_DECRYPT);69}7071static void72fuzz_cksum(uint8_t *input, mit_des_key_schedule sched)73{74uint8_t output[8];7576mit_des_cbc_cksum(input, output, 8, sched, default_ivec);77}7879int80LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)81{82krb5_error_code ret;83mit_des_key_schedule sched;84uint8_t *data_in, input[8];8586if (size < kMinInputLength || size > kMaxInputLength)87return 0;8889memcpy(input, data, 8);90ret = mit_des_key_sched(input, sched);91if (ret)92return 0;9394memcpy(input, data + 8, 8);95fuzz_des(input, sched);9697memcpy(input, data + 16, 8);98fuzz_decrypt(input, sched);99100data_in = k5memdup(data + 24, size - 24, &ret);101if (ret)102return 0;103104fuzz_cksum(data_in, sched);105free(data_in);106107return 0;108}109110111