1 /*
2 * Copyright (c) 1995 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * 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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <k5-int.h>
35 #include "crypto_int.h"
36
37 #define ONE_MILLION_A "one million a's"
38
39 struct test {
40 char *str;
41 unsigned char hash[64];
42 };
43
44 struct test sha256_tests[] = {
45 { "abc",
46 { 0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,
47 0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
48 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,
49 0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad }},
50 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
51 { 0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,
52 0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39,
53 0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,
54 0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1 }},
55 { ONE_MILLION_A,
56 { 0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92,
57 0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67,
58 0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e,
59 0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0 }},
60 { NULL }
61 };
62
63 struct test sha384_tests[] = {
64 { "abc",
65 { 0xcb,0x00,0x75,0x3f,0x45,0xa3,0x5e,0x8b,
66 0xb5,0xa0,0x3d,0x69,0x9a,0xc6,0x50,0x07,
67 0x27,0x2c,0x32,0xab,0x0e,0xde,0xd1,0x63,
68 0x1a,0x8b,0x60,0x5a,0x43,0xff,0x5b,0xed,
69 0x80,0x86,0x07,0x2b,0xa1,0xe7,0xcc,0x23,
70 0x58,0xba,0xec,0xa1,0x34,0xc8,0x25,0xa7 }},
71 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
72 "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
73 { 0x09,0x33,0x0c,0x33,0xf7,0x11,0x47,0xe8,
74 0x3d,0x19,0x2f,0xc7,0x82,0xcd,0x1b,0x47,
75 0x53,0x11,0x1b,0x17,0x3b,0x3b,0x05,0xd2,
76 0x2f,0xa0,0x80,0x86,0xe3,0xb0,0xf7,0x12,
77 0xfc,0xc7,0xc7,0x1a,0x55,0x7e,0x2d,0xb9,
78 0x66,0xc3,0xe9,0xfa,0x91,0x74,0x60,0x39 }},
79 { ONE_MILLION_A,
80 { 0x9d,0x0e,0x18,0x09,0x71,0x64,0x74,0xcb,
81 0x08,0x6e,0x83,0x4e,0x31,0x0a,0x4a,0x1c,
82 0xed,0x14,0x9e,0x9c,0x00,0xf2,0x48,0x52,
83 0x79,0x72,0xce,0xc5,0x70,0x4c,0x2a,0x5b,
84 0x07,0xb8,0xb3,0xdc,0x38,0xec,0xc4,0xeb,
85 0xae,0x97,0xdd,0xd8,0x7f,0x3d,0x89,0x85 }},
86 { NULL }
87 };
88
89 static int
hash_test(const struct krb5_hash_provider * hash,struct test * tests)90 hash_test(const struct krb5_hash_provider *hash, struct test *tests)
91 {
92 struct test *t;
93 krb5_crypto_iov iov, *iovs;
94 krb5_data hval;
95 size_t i;
96
97 if (alloc_data(&hval, hash->hashsize))
98 abort();
99 for (t = tests; t->str; ++t) {
100 if (strcmp(t->str, ONE_MILLION_A) == 0) {
101 /* Hash a million 'a's using a thousand iovs. */
102 iovs = calloc(1000, sizeof(*iovs));
103 assert(iovs != NULL);
104 for (i = 0; i < 1000; i++) {
105 iovs[i].flags = KRB5_CRYPTO_TYPE_DATA;
106 if (alloc_data(&iovs[i].data, 1000) != 0)
107 abort();
108 memset(iovs[i].data.data, 'a', 1000);
109 }
110 if (hash->hash(iovs, 1000, &hval) != 0)
111 abort();
112 if (memcmp(hval.data, t->hash, hval.length) != 0)
113 abort();
114 for (i = 0; i < 1000; i++)
115 free(iovs[i].data.data);
116 free(iovs);
117 } else {
118 /* Hash the input in the test. */
119 iov.flags = KRB5_CRYPTO_TYPE_DATA;
120 iov.data = string2data(t->str);
121 if (hash->hash(&iov, 1, &hval) != 0)
122 abort();
123 if (memcmp(hval.data, t->hash, hval.length) != 0)
124 abort();
125
126 if (hash == &krb5int_hash_sha256) {
127 /* Try again using k5_sha256(). */
128 if (k5_sha256(&iov.data, 1, (uint8_t *)hval.data) != 0)
129 abort();
130 if (memcmp(hval.data, t->hash, hval.length) != 0)
131 abort();
132 }
133 }
134 }
135 free(hval.data);
136 return 0;
137 }
138
139 int
main(void)140 main(void)
141 {
142 hash_test(&krb5int_hash_sha256, sha256_tests);
143 hash_test(&krb5int_hash_sha384, sha384_tests);
144 return 0;
145 }
146