1 /* $NetBSD: aes_test.c,v 1.1.1.4 2014/12/10 03:34:44 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC") 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* Id */ 20 21 /* ! \file */ 22 23 #include <config.h> 24 25 #include <atf-c.h> 26 27 #include <stdio.h> 28 #include <string.h> 29 30 #include <isc/aes.h> 31 #include <isc/buffer.h> 32 #include <isc/hex.h> 33 #include <isc/platform.h> 34 #include <isc/region.h> 35 #include <isc/string.h> 36 #include <isc/util.h> 37 38 #ifdef ISC_PLATFORM_WANTAES 39 40 /* 41 * Test data from NIST KAT 42 */ 43 44 isc_result_t 45 tohexstr(unsigned char *d, char *out); 46 47 size_t 48 fromhexstr(const char *in, unsigned char *d); 49 50 unsigned char plaintext[3 * ISC_AES_BLOCK_LENGTH]; 51 unsigned char ciphertext[ISC_AES_BLOCK_LENGTH]; 52 char str[2 * ISC_AES_BLOCK_LENGTH + 1]; 53 unsigned char key[ISC_AES256_KEYLENGTH + 1]; 54 size_t len; 55 56 isc_result_t 57 tohexstr(unsigned char *d, char *out) { 58 isc_buffer_t b; 59 isc_region_t r; 60 61 isc_buffer_init(&b, out, 2 * ISC_AES_BLOCK_LENGTH + 1); 62 r.base = d; 63 r.length = ISC_AES_BLOCK_LENGTH; 64 return (isc_hex_totext(&r, 0, "", &b)); 65 } 66 67 size_t 68 fromhexstr(const char *in, unsigned char *d) 69 { 70 isc_buffer_t b; 71 isc_result_t ret; 72 73 isc_buffer_init(&b, d, ISC_AES256_KEYLENGTH + 1); 74 ret = isc_hex_decodestring(in, &b); 75 if (ret != ISC_R_SUCCESS) 76 return 0; 77 return isc_buffer_usedlength(&b); 78 } 79 80 typedef struct aes_testcase { 81 const char *key; 82 const char *input; 83 const char *result; 84 } aes_testcase_t; 85 86 87 ATF_TC(isc_aes128); 88 ATF_TC_HEAD(isc_aes128, tc) { 89 atf_tc_set_md_var(tc, "descr", "AES 128 test vectors"); 90 } 91 ATF_TC_BODY(isc_aes128, tc) { 92 UNUSED(tc); 93 94 aes_testcase_t testcases[] = { 95 /* Test 1 (KAT ECBVarTxt128 #3) */ 96 { 97 "00000000000000000000000000000000", 98 "F0000000000000000000000000000000", 99 "96D9FD5CC4F07441727DF0F33E401A36" 100 }, 101 /* Test 2 (KAT ECBVarTxt128 #123) */ 102 { 103 "00000000000000000000000000000000", 104 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0", 105 "F9B0FDA0C4A898F5B9E6F661C4CE4D07" 106 }, 107 /* Test 3 (KAT ECBVarKey128 #3) */ 108 { 109 "F0000000000000000000000000000000", 110 "00000000000000000000000000000000", 111 "970014D634E2B7650777E8E84D03CCD8" 112 }, 113 /* Test 4 (KAT ECBVarKey128 #123) */ 114 { 115 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0", 116 "00000000000000000000000000000000", 117 "41C78C135ED9E98C096640647265DA1E" 118 }, 119 /* Test 5 (KAT ECBGFSbox128 #3) */ 120 { 121 "00000000000000000000000000000000", 122 "6A118A874519E64E9963798A503F1D35", 123 "DC43BE40BE0E53712F7E2BF5CA707209" 124 }, 125 /* Test 6 (KAT ECBKeySbox128 #3) */ 126 { 127 "B6364AC4E1DE1E285EAF144A2415F7A0", 128 "00000000000000000000000000000000", 129 "5D9B05578FC944B3CF1CCF0E746CD581" 130 }, 131 { NULL, NULL, NULL } 132 }; 133 134 aes_testcase_t *testcase = testcases; 135 136 while (testcase->key != NULL) { 137 len = fromhexstr(testcase->key, key); 138 ATF_CHECK_EQ(len, ISC_AES128_KEYLENGTH); 139 len = fromhexstr(testcase->input, plaintext); 140 ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH); 141 isc_aes128_crypt(key, plaintext, ciphertext); 142 ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS); 143 ATF_CHECK_STREQ(str, testcase->result); 144 145 testcase++; 146 } 147 } 148 149 ATF_TC(isc_aes192); 150 ATF_TC_HEAD(isc_aes192, tc) { 151 atf_tc_set_md_var(tc, "descr", "AES 192 test vectors"); 152 } 153 ATF_TC_BODY(isc_aes192, tc) { 154 UNUSED(tc); 155 156 aes_testcase_t testcases[] = { 157 /* Test 1 (KAT ECBVarTxt192 #3) */ 158 { 159 "000000000000000000000000000000000000000000000000", 160 "F0000000000000000000000000000000", 161 "2A560364CE529EFC21788779568D5555" 162 }, 163 /* Test 2 (KAT ECBVarTxt192 #123) */ 164 { 165 "000000000000000000000000000000000000000000000000", 166 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0", 167 "2AABB999F43693175AF65C6C612C46FB" 168 }, 169 /* Test 3 (KAT ECBVarKey192 #3) */ 170 { 171 "F00000000000000000000000000000000000000000000000", 172 "00000000000000000000000000000000", 173 "180B09F267C45145DB2F826C2582D35C" 174 }, 175 /* Test 4 (KAT ECBVarKey192 #187) */ 176 { 177 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0", 178 "00000000000000000000000000000000", 179 "EACF1E6C4224EFB38900B185AB1DFD42" 180 }, 181 /* Test 5 (KAT ECBGFSbox192 #3) */ 182 { 183 "000000000000000000000000000000000000000000000000", 184 "51719783D3185A535BD75ADC65071CE1", 185 "4F354592FF7C8847D2D0870CA9481B7C" 186 }, 187 /* Test 6 (KAT ECBKeySbox192 #3) */ 188 { 189 "CD62376D5EBB414917F0C78F05266433DC9192A1EC943300", 190 "00000000000000000000000000000000", 191 "7F6C25FF41858561BB62F36492E93C29" 192 }, 193 { NULL, NULL, NULL } 194 }; 195 196 aes_testcase_t *testcase = testcases; 197 198 while (testcase->key != NULL) { 199 len = fromhexstr(testcase->key, key); 200 ATF_CHECK_EQ(len, ISC_AES192_KEYLENGTH); 201 len = fromhexstr(testcase->input, plaintext); 202 ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH); 203 isc_aes192_crypt(key, plaintext, ciphertext); 204 ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS); 205 ATF_CHECK_STREQ(str, testcase->result); 206 207 testcase++; 208 } 209 } 210 211 ATF_TC(isc_aes256); 212 ATF_TC_HEAD(isc_aes256, tc) { 213 atf_tc_set_md_var(tc, "descr", "AES 256 test vectors"); 214 } 215 ATF_TC_BODY(isc_aes256, tc) { 216 UNUSED(tc); 217 218 aes_testcase_t testcases[] = { 219 /* Test 1 (KAT ECBVarTxt256 #3) */ 220 { 221 "00000000000000000000000000000000" 222 "00000000000000000000000000000000", 223 "F0000000000000000000000000000000", 224 "7F2C5ECE07A98D8BEE13C51177395FF7" 225 }, 226 /* Test 2 (KAT ECBVarTxt256 #123) */ 227 { 228 "00000000000000000000000000000000" 229 "00000000000000000000000000000000", 230 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0", 231 "7240E524BC51D8C4D440B1BE55D1062C" 232 }, 233 /* Test 3 (KAT ECBVarKey256 #3) */ 234 { 235 "F0000000000000000000000000000000" 236 "00000000000000000000000000000000", 237 "00000000000000000000000000000000", 238 "1C777679D50037C79491A94DA76A9A35" 239 }, 240 /* Test 4 (KAT ECBVarKey256 #251) */ 241 { 242 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 243 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0", 244 "00000000000000000000000000000000", 245 "03720371A04962EAEA0A852E69972858" 246 }, 247 /* Test 5 (KAT ECBGFSbox256 #3) */ 248 { 249 "00000000000000000000000000000000" 250 "00000000000000000000000000000000", 251 "8A560769D605868AD80D819BDBA03771", 252 "38F2C7AE10612415D27CA190D27DA8B4" 253 }, 254 /* Test 6 (KAT ECBKeySbox256 #3) */ 255 { 256 "984CA75F4EE8D706F46C2D98C0BF4A45" 257 "F5B00D791C2DFEB191B5ED8E420FD627", 258 "00000000000000000000000000000000", 259 "4307456A9E67813B452E15FA8FFFE398" 260 }, 261 { NULL, NULL, NULL } 262 }; 263 264 aes_testcase_t *testcase = testcases; 265 266 while (testcase->key != NULL) { 267 len = fromhexstr(testcase->key, key); 268 ATF_CHECK_EQ(len, ISC_AES256_KEYLENGTH); 269 len = fromhexstr(testcase->input, plaintext); 270 ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH); 271 isc_aes256_crypt(key, plaintext, ciphertext); 272 ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS); 273 ATF_CHECK_STREQ(str, testcase->result); 274 275 testcase++; 276 } 277 } 278 #else 279 ATF_TC(untested); 280 ATF_TC_HEAD(untested, tc) { 281 atf_tc_set_md_var(tc, "descr", "skipping aes test"); 282 } 283 ATF_TC_BODY(untested, tc) { 284 UNUSED(tc); 285 atf_tc_skip("AES not available"); 286 } 287 #endif 288 289 /* 290 * Main 291 */ 292 ATF_TP_ADD_TCS(tp) { 293 #ifdef ISC_PLATFORM_WANTAES 294 ATF_TP_ADD_TC(tp, isc_aes128); 295 ATF_TP_ADD_TC(tp, isc_aes192); 296 ATF_TP_ADD_TC(tp, isc_aes256); 297 #else 298 ATF_TP_ADD_TC(tp, untested); 299 #endif 300 return (atf_no_error()); 301 } 302 303