xref: /freebsd/crypto/krb5/src/tests/fuzzing/fuzz_des.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1*f1c4c3daSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*f1c4c3daSCy Schubert /* tests/fuzzing/fuzz_des.c - fuzzing harness for DES functions */
3*f1c4c3daSCy Schubert /*
4*f1c4c3daSCy Schubert  * Copyright (C) 2024 by Arjun. All rights reserved.
5*f1c4c3daSCy Schubert  *
6*f1c4c3daSCy Schubert  * Redistribution and use in source and binary forms, with or without
7*f1c4c3daSCy Schubert  * modification, are permitted provided that the following conditions
8*f1c4c3daSCy Schubert  * are met:
9*f1c4c3daSCy Schubert  *
10*f1c4c3daSCy Schubert  * * Redistributions of source code must retain the above copyright
11*f1c4c3daSCy Schubert  *   notice, this list of conditions and the following disclaimer.
12*f1c4c3daSCy Schubert  *
13*f1c4c3daSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
14*f1c4c3daSCy Schubert  *   notice, this list of conditions and the following disclaimer in
15*f1c4c3daSCy Schubert  *   the documentation and/or other materials provided with the
16*f1c4c3daSCy Schubert  *   distribution.
17*f1c4c3daSCy Schubert  *
18*f1c4c3daSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*f1c4c3daSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*f1c4c3daSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*f1c4c3daSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22*f1c4c3daSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23*f1c4c3daSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*f1c4c3daSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25*f1c4c3daSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*f1c4c3daSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27*f1c4c3daSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*f1c4c3daSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29*f1c4c3daSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
30*f1c4c3daSCy Schubert  */
31*f1c4c3daSCy Schubert 
32*f1c4c3daSCy Schubert #include "autoconf.h"
33*f1c4c3daSCy Schubert #include <k5-int.h>
34*f1c4c3daSCy Schubert #include <des_int.h>
35*f1c4c3daSCy Schubert 
36*f1c4c3daSCy Schubert #include <f_cbc.c>
37*f1c4c3daSCy Schubert 
38*f1c4c3daSCy Schubert #define kMinInputLength 32
39*f1c4c3daSCy Schubert #define kMaxInputLength 128
40*f1c4c3daSCy Schubert 
41*f1c4c3daSCy Schubert extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
42*f1c4c3daSCy Schubert 
43*f1c4c3daSCy Schubert uint8_t default_ivec[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
44*f1c4c3daSCy Schubert 
45*f1c4c3daSCy Schubert static void
fuzz_des(uint8_t * input,mit_des_key_schedule sched)46*f1c4c3daSCy Schubert fuzz_des(uint8_t *input, mit_des_key_schedule sched)
47*f1c4c3daSCy Schubert {
48*f1c4c3daSCy Schubert     uint8_t encrypt[8], decrypt[8];
49*f1c4c3daSCy Schubert 
50*f1c4c3daSCy Schubert     mit_des_cbc_encrypt((const mit_des_cblock *)input,
51*f1c4c3daSCy Schubert                         (mit_des_cblock *)encrypt, 8,
52*f1c4c3daSCy Schubert                         sched, default_ivec, MIT_DES_ENCRYPT);
53*f1c4c3daSCy Schubert 
54*f1c4c3daSCy Schubert     mit_des_cbc_encrypt((const mit_des_cblock *)encrypt,
55*f1c4c3daSCy Schubert                         (mit_des_cblock *)decrypt, 8,
56*f1c4c3daSCy Schubert                         sched, default_ivec, MIT_DES_DECRYPT);
57*f1c4c3daSCy Schubert 
58*f1c4c3daSCy Schubert     if (memcmp(input, decrypt, 8) != 0)
59*f1c4c3daSCy Schubert         abort();
60*f1c4c3daSCy Schubert }
61*f1c4c3daSCy Schubert 
62*f1c4c3daSCy Schubert static void
fuzz_decrypt(uint8_t * input,mit_des_key_schedule sched)63*f1c4c3daSCy Schubert fuzz_decrypt(uint8_t *input, mit_des_key_schedule sched)
64*f1c4c3daSCy Schubert {
65*f1c4c3daSCy Schubert     uint8_t output[8];
66*f1c4c3daSCy Schubert 
67*f1c4c3daSCy Schubert     mit_des_cbc_encrypt((const mit_des_cblock *)input,
68*f1c4c3daSCy Schubert                         (mit_des_cblock *)output, 8,
69*f1c4c3daSCy Schubert                         sched, default_ivec, MIT_DES_DECRYPT);
70*f1c4c3daSCy Schubert }
71*f1c4c3daSCy Schubert 
72*f1c4c3daSCy Schubert static void
fuzz_cksum(uint8_t * input,mit_des_key_schedule sched)73*f1c4c3daSCy Schubert fuzz_cksum(uint8_t *input, mit_des_key_schedule sched)
74*f1c4c3daSCy Schubert {
75*f1c4c3daSCy Schubert     uint8_t output[8];
76*f1c4c3daSCy Schubert 
77*f1c4c3daSCy Schubert     mit_des_cbc_cksum(input, output, 8, sched, default_ivec);
78*f1c4c3daSCy Schubert }
79*f1c4c3daSCy Schubert 
80*f1c4c3daSCy Schubert int
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81*f1c4c3daSCy Schubert LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82*f1c4c3daSCy Schubert {
83*f1c4c3daSCy Schubert     krb5_error_code ret;
84*f1c4c3daSCy Schubert     mit_des_key_schedule sched;
85*f1c4c3daSCy Schubert     uint8_t *data_in, input[8];
86*f1c4c3daSCy Schubert 
87*f1c4c3daSCy Schubert     if (size < kMinInputLength || size > kMaxInputLength)
88*f1c4c3daSCy Schubert         return 0;
89*f1c4c3daSCy Schubert 
90*f1c4c3daSCy Schubert     memcpy(input, data, 8);
91*f1c4c3daSCy Schubert     ret = mit_des_key_sched(input, sched);
92*f1c4c3daSCy Schubert     if (ret)
93*f1c4c3daSCy Schubert         return 0;
94*f1c4c3daSCy Schubert 
95*f1c4c3daSCy Schubert     memcpy(input, data + 8, 8);
96*f1c4c3daSCy Schubert     fuzz_des(input, sched);
97*f1c4c3daSCy Schubert 
98*f1c4c3daSCy Schubert     memcpy(input, data + 16, 8);
99*f1c4c3daSCy Schubert     fuzz_decrypt(input, sched);
100*f1c4c3daSCy Schubert 
101*f1c4c3daSCy Schubert     data_in = k5memdup(data + 24, size - 24, &ret);
102*f1c4c3daSCy Schubert     if (ret)
103*f1c4c3daSCy Schubert         return 0;
104*f1c4c3daSCy Schubert 
105*f1c4c3daSCy Schubert     fuzz_cksum(data_in, sched);
106*f1c4c3daSCy Schubert     free(data_in);
107*f1c4c3daSCy Schubert 
108*f1c4c3daSCy Schubert     return 0;
109*f1c4c3daSCy Schubert }
110