aes_test.c (9981B)
1 /********************************************************************* 2 * Filename: aes_test.c 3 * Author: Brad Conte (brad AT bradconte.com) 4 * Copyright: 5 * Disclaimer: This code is presented "as is" without any guarantees. 6 * Details: Performs known-answer tests on the corresponding AES 7 implementation. These tests do not encompass the full 8 range of available test vectors and are not sufficient 9 for FIPS-140 certification. However, if the tests pass 10 it is very, very likely that the code is correct and was 11 compiled properly. This code also serves as 12 example usage of the functions. 13 *********************************************************************/ 14 15 /*************************** HEADER FILES ***************************/ 16 #include <stdio.h> 17 #include <memory.h> 18 #include "aes.h" 19 20 /*********************** FUNCTION DEFINITIONS ***********************/ 21 void print_hex(BYTE str[], int len) 22 { 23 int idx; 24 25 for(idx = 0; idx < len; idx++) 26 printf("%02x", str[idx]); 27 } 28 29 int aes_ecb_test() 30 { 31 WORD key_schedule[60], idx; 32 BYTE enc_buf[128]; 33 BYTE plaintext[2][16] = { 34 {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a}, 35 {0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51} 36 }; 37 BYTE ciphertext[2][16] = { 38 {0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8}, 39 {0x59,0x1c,0xcb,0x10,0xd4,0x10,0xed,0x26,0xdc,0x5b,0xa7,0x4a,0x31,0x36,0x28,0x70} 40 }; 41 BYTE key[1][32] = { 42 {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4} 43 }; 44 int pass = 1; 45 46 // Raw ECB mode. 47 //printf("* ECB mode:\n"); 48 aes_key_setup(key[0], key_schedule, 256); 49 //printf( "Key : "); 50 //print_hex(key[0], 32); 51 52 for(idx = 0; idx < 2; idx++) { 53 aes_encrypt(plaintext[idx], enc_buf, key_schedule, 256); 54 //printf("\nPlaintext : "); 55 //print_hex(plaintext[idx], 16); 56 //printf("\n-encrypted to: "); 57 //print_hex(enc_buf, 16); 58 pass = pass && !memcmp(enc_buf, ciphertext[idx], 16); 59 60 aes_decrypt(ciphertext[idx], enc_buf, key_schedule, 256); 61 //printf("\nCiphertext : "); 62 //print_hex(ciphertext[idx], 16); 63 //printf("\n-decrypted to: "); 64 //print_hex(enc_buf, 16); 65 pass = pass && !memcmp(enc_buf, plaintext[idx], 16); 66 67 //printf("\n\n"); 68 } 69 70 return(pass); 71 } 72 73 int aes_cbc_test() 74 { 75 WORD key_schedule[60]; 76 BYTE enc_buf[128]; 77 BYTE plaintext[1][32] = { 78 {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51} 79 }; 80 BYTE ciphertext[1][32] = { 81 {0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6,0x9c,0xfc,0x4e,0x96,0x7e,0xdb,0x80,0x8d,0x67,0x9f,0x77,0x7b,0xc6,0x70,0x2c,0x7d} 82 }; 83 BYTE iv[1][16] = { 84 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f} 85 }; 86 BYTE key[1][32] = { 87 {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4} 88 }; 89 int pass = 1; 90 91 //printf("* CBC mode:\n"); 92 aes_key_setup(key[0], key_schedule, 256); 93 94 //printf( "Key : "); 95 //print_hex(key[0], 32); 96 //printf("\nIV : "); 97 //print_hex(iv[0], 16); 98 99 aes_encrypt_cbc(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]); 100 //printf("\nPlaintext : "); 101 //print_hex(plaintext[0], 32); 102 //printf("\n-encrypted to: "); 103 //print_hex(enc_buf, 32); 104 //printf("\nCiphertext : "); 105 //print_hex(ciphertext[0], 32); 106 pass = pass && !memcmp(enc_buf, ciphertext[0], 32); 107 108 aes_decrypt_cbc(ciphertext[0], 32, enc_buf, key_schedule, 256, iv[0]); 109 //printf("\nCiphertext : "); 110 //print_hex(ciphertext[0], 32); 111 //printf("\n-decrypted to: "); 112 //print_hex(enc_buf, 32); 113 //printf("\nPlaintext : "); 114 //print_hex(plaintext[0], 32); 115 pass = pass && !memcmp(enc_buf, plaintext[0], 32); 116 117 //printf("\n\n"); 118 return(pass); 119 } 120 121 int aes_ctr_test() 122 { 123 WORD key_schedule[60]; 124 BYTE enc_buf[128]; 125 BYTE plaintext[1][32] = { 126 {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51} 127 }; 128 BYTE ciphertext[1][32] = { 129 {0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5} 130 }; 131 BYTE iv[1][16] = { 132 {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff}, 133 }; 134 BYTE key[1][32] = { 135 {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4} 136 }; 137 int pass = 1; 138 139 //printf("* CTR mode:\n"); 140 aes_key_setup(key[0], key_schedule, 256); 141 142 //printf( "Key : "); 143 //print_hex(key[0], 32); 144 //printf("\nIV : "); 145 //print_hex(iv[0], 16); 146 147 aes_encrypt_ctr(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]); 148 //printf("\nPlaintext : "); 149 //print_hex(plaintext[0], 32); 150 //printf("\n-encrypted to: "); 151 //print_hex(enc_buf, 32); 152 pass = pass && !memcmp(enc_buf, ciphertext[0], 32); 153 154 aes_decrypt_ctr(ciphertext[0], 32, enc_buf, key_schedule, 256, iv[0]); 155 //printf("\nCiphertext : "); 156 //print_hex(ciphertext[0], 32); 157 //printf("\n-decrypted to: "); 158 //print_hex(enc_buf, 32); 159 pass = pass && !memcmp(enc_buf, plaintext[0], 32); 160 161 //printf("\n\n"); 162 return(pass); 163 } 164 165 int aes_ccm_test() 166 { 167 int mac_auth; 168 WORD enc_buf_len; 169 BYTE enc_buf[128]; 170 BYTE plaintext[3][32] = { 171 {0x20,0x21,0x22,0x23}, 172 {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f}, 173 {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37} 174 }; 175 BYTE assoc[3][32] = { 176 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}, 177 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}, 178 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13} 179 }; 180 BYTE ciphertext[3][32 + 16] = { 181 {0x71,0x62,0x01,0x5b,0x4d,0xac,0x25,0x5d}, 182 {0xd2,0xa1,0xf0,0xe0,0x51,0xea,0x5f,0x62,0x08,0x1a,0x77,0x92,0x07,0x3d,0x59,0x3d,0x1f,0xc6,0x4f,0xbf,0xac,0xcd}, 183 {0xe3,0xb2,0x01,0xa9,0xf5,0xb7,0x1a,0x7a,0x9b,0x1c,0xea,0xec,0xcd,0x97,0xe7,0x0b,0x61,0x76,0xaa,0xd9,0xa4,0x42,0x8a,0xa5,0x48,0x43,0x92,0xfb,0xc1,0xb0,0x99,0x51} 184 }; 185 BYTE iv[3][16] = { 186 {0x10,0x11,0x12,0x13,0x14,0x15,0x16}, 187 {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17}, 188 {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b} 189 }; 190 BYTE key[1][32] = { 191 {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f} 192 }; 193 int pass = 1; 194 195 //printf("* CCM mode:\n"); 196 //printf("Key : "); 197 //print_hex(key[0], 16); 198 199 //print_hex(plaintext[0], 4); 200 //print_hex(assoc[0], 8); 201 //print_hex(ciphertext[0], 8); 202 //print_hex(iv[0], 7); 203 //print_hex(key[0], 16); 204 205 aes_encrypt_ccm(plaintext[0], 4, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, key[0], 128); 206 //printf("\nNONCE : "); 207 //print_hex(iv[0], 7); 208 //printf("\nAssoc. Data : "); 209 //print_hex(assoc[0], 8); 210 //printf("\nPayload : "); 211 //print_hex(plaintext[0], 4); 212 //printf("\n-encrypted to: "); 213 //print_hex(enc_buf, enc_buf_len); 214 pass = pass && !memcmp(enc_buf, ciphertext[0], enc_buf_len); 215 216 aes_decrypt_ccm(ciphertext[0], 8, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, &mac_auth, key[0], 128); 217 //printf("\n-Ciphertext : "); 218 //print_hex(ciphertext[0], 8); 219 //printf("\n-decrypted to: "); 220 //print_hex(enc_buf, enc_buf_len); 221 //printf("\nAuthenticated: %d ", mac_auth); 222 pass = pass && !memcmp(enc_buf, plaintext[0], enc_buf_len) && mac_auth; 223 224 225 aes_encrypt_ccm(plaintext[1], 16, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, key[0], 128); 226 //printf("\n\nNONCE : "); 227 //print_hex(iv[1], 8); 228 //printf("\nAssoc. Data : "); 229 //print_hex(assoc[1], 16); 230 //printf("\nPayload : "); 231 //print_hex(plaintext[1], 16); 232 //printf("\n-encrypted to: "); 233 //print_hex(enc_buf, enc_buf_len); 234 pass = pass && !memcmp(enc_buf, ciphertext[1], enc_buf_len); 235 236 aes_decrypt_ccm(ciphertext[1], 22, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, &mac_auth, key[0], 128); 237 //printf("\n-Ciphertext : "); 238 //print_hex(ciphertext[1], 22); 239 //printf("\n-decrypted to: "); 240 //print_hex(enc_buf, enc_buf_len); 241 //printf("\nAuthenticated: %d ", mac_auth); 242 pass = pass && !memcmp(enc_buf, plaintext[1], enc_buf_len) && mac_auth; 243 244 245 aes_encrypt_ccm(plaintext[2], 24, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, key[0], 128); 246 //printf("\n\nNONCE : "); 247 //print_hex(iv[2], 12); 248 //printf("\nAssoc. Data : "); 249 //print_hex(assoc[2], 20); 250 //printf("\nPayload : "); 251 //print_hex(plaintext[2], 24); 252 //printf("\n-encrypted to: "); 253 //print_hex(enc_buf, enc_buf_len); 254 pass = pass && !memcmp(enc_buf, ciphertext[2], enc_buf_len); 255 256 aes_decrypt_ccm(ciphertext[2], 32, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, &mac_auth, key[0], 128); 257 //printf("\n-Ciphertext : "); 258 //print_hex(ciphertext[2], 32); 259 //printf("\n-decrypted to: "); 260 //print_hex(enc_buf, enc_buf_len); 261 //printf("\nAuthenticated: %d ", mac_auth); 262 pass = pass && !memcmp(enc_buf, plaintext[2], enc_buf_len) && mac_auth; 263 264 //printf("\n\n"); 265 return(pass); 266 } 267 268 int aes_test() 269 { 270 int pass = 1; 271 272 pass = pass && aes_ecb_test(); 273 pass = pass && aes_cbc_test(); 274 pass = pass && aes_ctr_test(); 275 pass = pass && aes_ccm_test(); 276 277 return(pass); 278 } 279 280 int main(int argc, char *argv[]) 281 { 282 printf("AES Tests: %s\n", aes_test() ? "SUCCEEDED" : "FAILED"); 283 284 return(0); 285 }