aes_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /* ! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <atf-c.h>
00024 
00025 #include <stdio.h>
00026 #include <string.h>
00027 
00028 #include <isc/aes.h>
00029 #include <isc/buffer.h>
00030 #include <isc/hex.h>
00031 #include <isc/platform.h>
00032 #include <isc/region.h>
00033 #include <isc/string.h>
00034 #include <isc/util.h>
00035 
00036 #ifdef ISC_PLATFORM_WANTAES
00037 
00038 /*
00039  * Test data from NIST KAT
00040  */
00041 
00042 isc_result_t
00043 tohexstr(unsigned char *d, char *out);
00044 
00045 size_t
00046 fromhexstr(const char *in, unsigned char *d);
00047 
00048 unsigned char plaintext[3 * ISC_AES_BLOCK_LENGTH];
00049 unsigned char ciphertext[ISC_AES_BLOCK_LENGTH];
00050 char str[2 * ISC_AES_BLOCK_LENGTH + 1];
00051 unsigned char key[ISC_AES256_KEYLENGTH + 1];
00052 size_t len;
00053 
00054 isc_result_t
00055 tohexstr(unsigned char *d, char *out) {
00056         isc_buffer_t b;
00057         isc_region_t r;
00058 
00059         isc_buffer_init(&b, out, 2 * ISC_AES_BLOCK_LENGTH + 1);
00060         r.base = d;
00061         r.length = ISC_AES_BLOCK_LENGTH;
00062         return (isc_hex_totext(&r, 0, "", &b));
00063 }
00064 
00065 size_t
00066 fromhexstr(const char *in, unsigned char *d)
00067 {
00068         isc_buffer_t b;
00069         isc_result_t ret;
00070 
00071         isc_buffer_init(&b, d, ISC_AES256_KEYLENGTH + 1);
00072         ret = isc_hex_decodestring(in, &b);
00073         if (ret != ISC_R_SUCCESS)
00074                 return 0;
00075         return isc_buffer_usedlength(&b);
00076 }
00077 
00078 typedef struct aes_testcase {
00079         const char *key;
00080         const char *input;
00081         const char *result;
00082 } aes_testcase_t;
00083 
00084 
00085 ATF_TC(isc_aes128);
00086 ATF_TC_HEAD(isc_aes128, tc) {
00087         atf_tc_set_md_var(tc, "descr", "AES 128 test vectors");
00088 }
00089 ATF_TC_BODY(isc_aes128, tc) {
00090         UNUSED(tc);
00091 
00092         aes_testcase_t testcases[] = {
00093                 /* Test 1 (KAT ECBVarTxt128 #3) */
00094                 {
00095                         "00000000000000000000000000000000",
00096                         "F0000000000000000000000000000000",
00097                         "96D9FD5CC4F07441727DF0F33E401A36"
00098                 },
00099                 /* Test 2 (KAT ECBVarTxt128 #123) */
00100                 {
00101                         "00000000000000000000000000000000",
00102                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0",
00103                         "F9B0FDA0C4A898F5B9E6F661C4CE4D07"
00104                 },
00105                 /* Test 3 (KAT ECBVarKey128 #3) */
00106                 {
00107                         "F0000000000000000000000000000000",
00108                         "00000000000000000000000000000000",
00109                         "970014D634E2B7650777E8E84D03CCD8"
00110                 },
00111                 /* Test 4 (KAT ECBVarKey128 #123) */
00112                 {
00113                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0",
00114                         "00000000000000000000000000000000",
00115                         "41C78C135ED9E98C096640647265DA1E"
00116                 },
00117                 /* Test 5 (KAT ECBGFSbox128 #3) */
00118                 {
00119                         "00000000000000000000000000000000",
00120                         "6A118A874519E64E9963798A503F1D35",
00121                         "DC43BE40BE0E53712F7E2BF5CA707209"
00122                 },
00123                 /* Test 6 (KAT ECBKeySbox128 #3) */
00124                 {
00125                         "B6364AC4E1DE1E285EAF144A2415F7A0",
00126                         "00000000000000000000000000000000",
00127                         "5D9B05578FC944B3CF1CCF0E746CD581"
00128                 },
00129                 { NULL, NULL, NULL }
00130         };
00131 
00132         aes_testcase_t *testcase = testcases;
00133 
00134         while (testcase->key != NULL) {
00135                 len = fromhexstr(testcase->key, key);
00136                 ATF_CHECK_EQ(len, ISC_AES128_KEYLENGTH);
00137                 len = fromhexstr(testcase->input, plaintext);
00138                 ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH);
00139                 isc_aes128_crypt(key, plaintext, ciphertext);
00140                 ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS);
00141                 ATF_CHECK_STREQ(str, testcase->result);
00142 
00143                 testcase++;
00144         }
00145 }
00146 
00147 ATF_TC(isc_aes192);
00148 ATF_TC_HEAD(isc_aes192, tc) {
00149         atf_tc_set_md_var(tc, "descr", "AES 192 test vectors");
00150 }
00151 ATF_TC_BODY(isc_aes192, tc) {
00152         UNUSED(tc);
00153 
00154         aes_testcase_t testcases[] = {
00155                 /* Test 1 (KAT ECBVarTxt192 #3) */
00156                 {
00157                         "000000000000000000000000000000000000000000000000",
00158                         "F0000000000000000000000000000000",
00159                         "2A560364CE529EFC21788779568D5555"
00160                 },
00161                 /* Test 2 (KAT ECBVarTxt192 #123) */
00162                 {
00163                         "000000000000000000000000000000000000000000000000",
00164                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0",
00165                         "2AABB999F43693175AF65C6C612C46FB"
00166                 },
00167                 /* Test 3 (KAT ECBVarKey192 #3) */
00168                 {
00169                         "F00000000000000000000000000000000000000000000000",
00170                         "00000000000000000000000000000000",
00171                         "180B09F267C45145DB2F826C2582D35C"
00172                 },
00173                 /* Test 4 (KAT ECBVarKey192 #187) */
00174                 {
00175                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0",
00176                         "00000000000000000000000000000000",
00177                         "EACF1E6C4224EFB38900B185AB1DFD42"
00178                 },
00179                 /* Test 5 (KAT ECBGFSbox192 #3) */
00180                 {
00181                         "000000000000000000000000000000000000000000000000",
00182                         "51719783D3185A535BD75ADC65071CE1",
00183                         "4F354592FF7C8847D2D0870CA9481B7C"
00184                 },
00185                 /* Test 6 (KAT ECBKeySbox192 #3) */
00186                 {
00187                         "CD62376D5EBB414917F0C78F05266433DC9192A1EC943300",
00188                         "00000000000000000000000000000000",
00189                         "7F6C25FF41858561BB62F36492E93C29"
00190                 },
00191                 { NULL, NULL, NULL }
00192         };
00193 
00194         aes_testcase_t *testcase = testcases;
00195 
00196         while (testcase->key != NULL) {
00197                 len = fromhexstr(testcase->key, key);
00198                 ATF_CHECK_EQ(len, ISC_AES192_KEYLENGTH);
00199                 len = fromhexstr(testcase->input, plaintext);
00200                 ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH);
00201                 isc_aes192_crypt(key, plaintext, ciphertext);
00202                 ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS);
00203                 ATF_CHECK_STREQ(str, testcase->result);
00204 
00205                 testcase++;
00206         }
00207 }
00208 
00209 ATF_TC(isc_aes256);
00210 ATF_TC_HEAD(isc_aes256, tc) {
00211         atf_tc_set_md_var(tc, "descr", "AES 256 test vectors");
00212 }
00213 ATF_TC_BODY(isc_aes256, tc) {
00214         UNUSED(tc);
00215 
00216         aes_testcase_t testcases[] = {
00217                 /* Test 1 (KAT ECBVarTxt256 #3) */
00218                 {
00219                         "00000000000000000000000000000000"
00220                         "00000000000000000000000000000000",
00221                         "F0000000000000000000000000000000",
00222                         "7F2C5ECE07A98D8BEE13C51177395FF7"
00223                 },
00224                 /* Test 2 (KAT ECBVarTxt256 #123) */
00225                 {
00226                         "00000000000000000000000000000000"
00227                         "00000000000000000000000000000000",
00228                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0",
00229                         "7240E524BC51D8C4D440B1BE55D1062C"
00230                 },
00231                 /* Test 3 (KAT ECBVarKey256 #3) */
00232                 {
00233                         "F0000000000000000000000000000000"
00234                         "00000000000000000000000000000000",
00235                         "00000000000000000000000000000000",
00236                         "1C777679D50037C79491A94DA76A9A35"
00237                 },
00238                 /* Test 4 (KAT ECBVarKey256 #251) */
00239                 {
00240                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
00241                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0",
00242                         "00000000000000000000000000000000",
00243                         "03720371A04962EAEA0A852E69972858"
00244                 },
00245                 /* Test 5 (KAT ECBGFSbox256 #3) */
00246                 {
00247                         "00000000000000000000000000000000"
00248                         "00000000000000000000000000000000",
00249                         "8A560769D605868AD80D819BDBA03771",
00250                         "38F2C7AE10612415D27CA190D27DA8B4"
00251                 },
00252                 /* Test 6 (KAT ECBKeySbox256 #3) */
00253                 {
00254                         "984CA75F4EE8D706F46C2D98C0BF4A45"
00255                         "F5B00D791C2DFEB191B5ED8E420FD627",
00256                         "00000000000000000000000000000000",
00257                         "4307456A9E67813B452E15FA8FFFE398"
00258                 },
00259                 { NULL, NULL, NULL }
00260         };
00261 
00262         aes_testcase_t *testcase = testcases;
00263 
00264         while (testcase->key != NULL) {
00265                 len = fromhexstr(testcase->key, key);
00266                 ATF_CHECK_EQ(len, ISC_AES256_KEYLENGTH);
00267                 len = fromhexstr(testcase->input, plaintext);
00268                 ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH);
00269                 isc_aes256_crypt(key, plaintext, ciphertext);
00270                 ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS);
00271                 ATF_CHECK_STREQ(str, testcase->result);
00272 
00273                 testcase++;
00274         }
00275 }
00276 #else
00277 ATF_TC(untested);
00278 ATF_TC_HEAD(untested, tc) {
00279         atf_tc_set_md_var(tc, "descr", "skipping aes test");
00280 }
00281 ATF_TC_BODY(untested, tc) {
00282         UNUSED(tc);
00283         atf_tc_skip("AES not available");
00284 }
00285 #endif
00286 
00287 /*
00288  * Main
00289  */
00290 ATF_TP_ADD_TCS(tp) {
00291 #ifdef ISC_PLATFORM_WANTAES
00292         ATF_TP_ADD_TC(tp, isc_aes128);
00293         ATF_TP_ADD_TC(tp, isc_aes192);
00294         ATF_TP_ADD_TC(tp, isc_aes256);
00295 #else
00296         ATF_TP_ADD_TC(tp, untested);
00297 #endif
00298         return (atf_no_error());
00299 }
00300 

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha