1 /* 2 * Test program for SHA256 3 * Copyright (c) 2006, Jouni Malinen <j (at) w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "includes.h" 16 17 #include "common.h" 18 #include "sha256.h" 19 #include "crypto.h" 20 21 struct { 22 char *data; 23 u8 hash[32]; 24 } tests[] = { 25 { 26 "abc", 27 { 28 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 29 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 30 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 31 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad 32 } 33 }, 34 { 35 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 36 { 37 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 38 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 39 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 40 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 41 } 42 } 43 }; 44 45 struct hmac_test { 46 u8 key[80]; 47 size_t key_len; 48 u8 data[128]; 49 size_t data_len; 50 u8 hash[32]; 51 } hmac_tests[] = { 52 /* draft-ietf-ipsec-ciph-sha-256-01.txt */ 53 { 54 { 55 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 56 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 57 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 58 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20 59 }, 60 32, 61 "abc", 3, 62 { 63 0xa2, 0x1b, 0x1f, 0x5d, 0x4c, 0xf4, 0xf7, 0x3a, 64 0x4d, 0xd9, 0x39, 0x75, 0x0f, 0x7a, 0x06, 0x6a, 65 0x7f, 0x98, 0xcc, 0x13, 0x1c, 0xb1, 0x6a, 0x66, 66 0x92, 0x75, 0x90, 0x21, 0xcf, 0xab, 0x81, 0x81 67 } 68 }, 69 { 70 { 71 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 72 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 73 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 74 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20 75 }, 76 32, 77 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 78 56, 79 { 80 0x10, 0x4f, 0xdc, 0x12, 0x57, 0x32, 0x8f, 0x08, 81 0x18, 0x4b, 0xa7, 0x31, 0x31, 0xc5, 0x3c, 0xae, 82 0xe6, 0x98, 0xe3, 0x61, 0x19, 0x42, 0x11, 0x49, 83 0xea, 0x8c, 0x71, 0x24, 0x56, 0x69, 0x7d, 0x30 84 } 85 }, 86 { 87 { 88 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 89 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 90 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 91 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20 92 }, 93 32, 94 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 95 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 96 112, 97 { 98 0x47, 0x03, 0x05, 0xfc, 0x7e, 0x40, 0xfe, 0x34, 99 0xd3, 0xee, 0xb3, 0xe7, 0x73, 0xd9, 0x5a, 0xab, 100 0x73, 0xac, 0xf0, 0xfd, 0x06, 0x04, 0x47, 0xa5, 101 0xeb, 0x45, 0x95, 0xbf, 0x33, 0xa9, 0xd1, 0xa3 102 } 103 }, 104 { 105 { 106 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 107 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 108 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 109 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 110 }, 111 32, 112 "Hi There", 113 8, 114 { 115 0x19, 0x8a, 0x60, 0x7e, 0xb4, 0x4b, 0xfb, 0xc6, 116 0x99, 0x03, 0xa0, 0xf1, 0xcf, 0x2b, 0xbd, 0xc5, 117 0xba, 0x0a, 0xa3, 0xf3, 0xd9, 0xae, 0x3c, 0x1c, 118 0x7a, 0x3b, 0x16, 0x96, 0xa0, 0xb6, 0x8c, 0xf7 119 } 120 }, 121 { 122 "Jefe", 123 4, 124 "what do ya want for nothing?", 125 28, 126 { 127 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 128 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7, 129 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, 130 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 131 } 132 }, 133 { 134 { 135 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 136 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 137 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 138 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa 139 }, 140 32, 141 { 142 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 143 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 144 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 145 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 146 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 147 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 148 0xdd, 0xdd 149 }, 150 50, 151 { 152 0xcd, 0xcb, 0x12, 0x20, 0xd1, 0xec, 0xcc, 0xea, 153 0x91, 0xe5, 0x3a, 0xba, 0x30, 0x92, 0xf9, 0x62, 154 0xe5, 0x49, 0xfe, 0x6c, 0xe9, 0xed, 0x7f, 0xdc, 155 0x43, 0x19, 0x1f, 0xbd, 0xe4, 0x5c, 0x30, 0xb0 156 } 157 }, 158 { 159 { 160 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 161 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 162 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 163 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 164 0x21, 0x22, 0x23, 0x24, 0x25 165 }, 166 37, 167 { 168 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 169 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 170 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 171 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 172 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 173 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 174 0xcd, 0xcd 175 }, 176 50, 177 { 178 0xd4, 0x63, 0x3c, 0x17, 0xf6, 0xfb, 0x8d, 0x74, 179 0x4c, 0x66, 0xde, 0xe0, 0xf8, 0xf0, 0x74, 0x55, 180 0x6e, 0xc4, 0xaf, 0x55, 0xef, 0x07, 0x99, 0x85, 181 0x41, 0x46, 0x8e, 0xb4, 0x9b, 0xd2, 0xe9, 0x17 182 } 183 }, 184 { 185 { 186 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 187 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 188 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 189 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c 190 }, 191 32, 192 "Test With Truncation", 193 20, 194 { 195 0x75, 0x46, 0xaf, 0x01, 0x84, 0x1f, 0xc0, 0x9b, 196 0x1a, 0xb9, 0xc3, 0x74, 0x9a, 0x5f, 0x1c, 0x17, 197 0xd4, 0xf5, 0x89, 0x66, 0x8a, 0x58, 0x7b, 0x27, 198 0x00, 0xa9, 0xc9, 0x7c, 0x11, 0x93, 0xcf, 0x42 199 } 200 }, 201 { 202 { 203 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 204 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 205 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 206 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 207 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 208 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 209 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 210 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 211 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 212 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa 213 }, 214 80, 215 "Test Using Larger Than Block-Size Key - Hash Key First", 216 54, 217 { 218 0x69, 0x53, 0x02, 0x5e, 0xd9, 0x6f, 0x0c, 0x09, 219 0xf8, 0x0a, 0x96, 0xf7, 0x8e, 0x65, 0x38, 0xdb, 220 0xe2, 0xe7, 0xb8, 0x20, 0xe3, 0xdd, 0x97, 0x0e, 221 0x7d, 0xdd, 0x39, 0x09, 0x1b, 0x32, 0x35, 0x2f 222 } 223 }, 224 { 225 { 226 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 227 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 228 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 229 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 230 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 231 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 232 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 233 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 234 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 235 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa 236 }, 237 80, 238 "Test Using Larger Than Block-Size Key and Larger Than One " 239 "Block-Size Data", 240 73, 241 { 242 0x63, 0x55, 0xac, 0x22, 0xe8, 0x90, 0xd0, 0xa3, 243 0xc8, 0x48, 0x1a, 0x5c, 0xa4, 0x82, 0x5b, 0xc8, 244 0x84, 0xd3, 0xe7, 0xa1, 0xff, 0x98, 0xa2, 0xfc, 245 0x2a, 0xc7, 0xd8, 0xe0, 0x64, 0xc3, 0xb2, 0xe6 246 } 247 } 248 }; 249 250 251 int main(int argc, char *argv[]) 252 { 253 254 unsigned int i; 255 u8 hash[32]; 256 const u8 *addr[2]; 257 size_t len[2]; 258 int errors = 0; 259 260 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { 261 printf("SHA256 test case %d:", i + 1); 262 263 addr[0] = (u8 *) tests[i].data; 264 len[0] = strlen(tests[i].data); 265 sha256_vector(1, addr, len, hash); 266 if (memcmp(hash, tests[i].hash, 32) != 0) { 267 printf(" FAIL"); 268 errors++; 269 } else 270 printf(" OK"); 271 272 if (len[0]) { 273 addr[0] = (u8 *) tests[i].data; 274 len[0] = 1; 275 addr[1] = (u8 *) tests[i].data + 1; 276 len[1] = strlen(tests[i].data) - 1; 277 sha256_vector(2, addr, len, hash); 278 if (memcmp(hash, tests[i].hash, 32) != 0) { 279 printf(" FAIL"); 280 errors++; 281 } else 282 printf(" OK"); 283 } 284 285 printf("\n"); 286 } 287 288 for (i = 0; i < sizeof(hmac_tests) / sizeof(hmac_tests[0]); i++) { 289 struct hmac_test *t = &hmac_tests[i]; 290 printf("HMAC-SHA256 test case %d:", i + 1); 291 292 hmac_sha256(t->key, t->key_len, t->data, t->data_len, hash); 293 if (memcmp(hash, t->hash, 32) != 0) { 294 printf(" FAIL"); 295 errors++; 296 } else 297 printf(" OK"); 298 299 addr[0] = t->data; 300 len[0] = t->data_len; 301 hmac_sha256_vector(t->key, t->key_len, 1, addr, len, hash); 302 if (memcmp(hash, t->hash, 32) != 0) { 303 printf(" FAIL"); 304 errors++; 305 } else 306 printf(" OK"); 307 308 if (len[0]) { 309 addr[0] = t->data; 310 len[0] = 1; 311 addr[1] = t->data + 1; 312 len[1] = t->data_len - 1; 313 hmac_sha256_vector(t->key, t->key_len, 2, addr, len, 314 hash); 315 if (memcmp(hash, t->hash, 32) != 0) { 316 printf(" FAIL"); 317 errors++; 318 } else 319 printf(" OK"); 320 } 321 322 printf("\n"); 323 } 324 325 printf("Test IEEE 802.11r KDF\n"); 326 sha256_prf((u8 *) "abc", 3, "KDF test", (u8 *) "data", 4, 327 hash, sizeof(hash)); 328 /* TODO: add proper test case for this */ 329 330 return errors; 331 } 332