xref: /freebsd/crypto/krb5/src/lib/crypto/crypto_tests/t_short.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_short.c */
3 /*
4  * Copyright (C) 2009 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 /*
28  * Tests the outcome of decrypting overly short tokens.  This program can be
29  * run under a tool like valgrind to detect bad memory accesses; when run
30  * normally by the test suite, it verifies that each operation returns
31  * KRB5_BAD_MSIZE.
32  */
33 
34 #include "k5-int.h"
35 
36 krb5_enctype interesting_enctypes[] = {
37     ENCTYPE_DES3_CBC_SHA1,
38     ENCTYPE_ARCFOUR_HMAC,
39     ENCTYPE_ARCFOUR_HMAC_EXP,
40     ENCTYPE_AES256_CTS_HMAC_SHA1_96,
41     ENCTYPE_AES128_CTS_HMAC_SHA1_96,
42     ENCTYPE_CAMELLIA128_CTS_CMAC,
43     ENCTYPE_CAMELLIA256_CTS_CMAC,
44     ENCTYPE_AES128_CTS_HMAC_SHA256_128,
45     ENCTYPE_AES256_CTS_HMAC_SHA384_192,
46     0
47 };
48 
49 /* Abort if an operation unexpectedly fails. */
50 static void
x(krb5_error_code code)51 x(krb5_error_code code)
52 {
53     if (code != 0)
54         abort();
55 }
56 
57 /* Abort if a decrypt operation doesn't have the expected result. */
58 static void
check_decrypt_result(krb5_error_code code,size_t len,size_t min_len)59 check_decrypt_result(krb5_error_code code, size_t len, size_t min_len)
60 {
61     if (len < min_len) {
62         /* Undersized tokens should always result in BAD_MSIZE. */
63         if (code != KRB5_BAD_MSIZE)
64             abort();
65     } else {
66         /* Min-size tokens should succeed or fail the integrity check. */
67         if (code != 0 && code != KRB5KRB_AP_ERR_BAD_INTEGRITY)
68             abort();
69     }
70 }
71 
72 static void
test_enctype(krb5_enctype enctype)73 test_enctype(krb5_enctype enctype)
74 {
75     krb5_error_code ret;
76     krb5_keyblock keyblock;
77     krb5_enc_data input;
78     krb5_data output;
79     krb5_crypto_iov iov[2];
80     unsigned int dummy;
81     size_t min_len, len;
82 
83     printf("Testing enctype %d\n", (int) enctype);
84     x(krb5_c_encrypt_length(NULL, enctype, 0, &min_len));
85     x(krb5_c_make_random_key(NULL, enctype, &keyblock));
86     input.enctype = enctype;
87 
88     /* Try each length up to the minimum length. */
89     for (len = 0; len <= min_len; len++) {
90         input.ciphertext.data = calloc(len, 1);
91         input.ciphertext.length = len;
92         output.data = calloc(len, 1);
93         output.length = len;
94 
95         /* Attempt a normal decryption. */
96         ret = krb5_c_decrypt(NULL, &keyblock, 0, NULL, &input, &output);
97         check_decrypt_result(ret, len, min_len);
98 
99         if (krb5_c_crypto_length(NULL, enctype, KRB5_CRYPTO_TYPE_HEADER,
100                                  &dummy) == 0) {
101             /* Attempt an IOV stream decryption. */
102             iov[0].flags = KRB5_CRYPTO_TYPE_STREAM;
103             iov[0].data = input.ciphertext;
104             iov[1].flags = KRB5_CRYPTO_TYPE_DATA;
105             iov[1].data.data = NULL;
106             iov[1].data.length = 0;
107             ret = krb5_c_decrypt_iov(NULL, &keyblock, 0, NULL, iov, 2);
108             check_decrypt_result(ret, len, min_len);
109         }
110 
111         free(input.ciphertext.data);
112         free(output.data);
113     }
114     krb5int_c_free_keyblock_contents (NULL, &keyblock);
115 
116 }
117 
118 int
main(int argc,char ** argv)119 main(int argc, char **argv)
120 {
121     int i;
122     krb5_data notrandom;
123 
124     notrandom.data = "notrandom";
125     notrandom.length = 9;
126     krb5_c_random_seed(NULL, &notrandom);
127     for (i = 0; interesting_enctypes[i]; i++)
128         test_enctype(interesting_enctypes[i]);
129     return 0;
130 }
131