A comprehensive test program for the AES library using official test vectors.

Dependencies:   AES mbed

Committer:
neilt6
Date:
Fri Sep 04 02:10:58 2015 +0000
Revision:
0:a30389f7422b
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:a30389f7422b 1 #include "mbed.h"
neilt6 0:a30389f7422b 2 #include "AES.h"
neilt6 0:a30389f7422b 3
neilt6 0:a30389f7422b 4 const char testVector1[16] = {
neilt6 0:a30389f7422b 5 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
neilt6 0:a30389f7422b 6 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
neilt6 0:a30389f7422b 7 };
neilt6 0:a30389f7422b 8
neilt6 0:a30389f7422b 9 const char testVector2[16] = {
neilt6 0:a30389f7422b 10 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
neilt6 0:a30389f7422b 11 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51
neilt6 0:a30389f7422b 12 };
neilt6 0:a30389f7422b 13
neilt6 0:a30389f7422b 14 const char testVector3[16] = {
neilt6 0:a30389f7422b 15 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
neilt6 0:a30389f7422b 16 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF
neilt6 0:a30389f7422b 17 };
neilt6 0:a30389f7422b 18
neilt6 0:a30389f7422b 19 const char testVector4[16] = {
neilt6 0:a30389f7422b 20 0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17,
neilt6 0:a30389f7422b 21 0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10
neilt6 0:a30389f7422b 22 };
neilt6 0:a30389f7422b 23
neilt6 0:a30389f7422b 24 bool testEncrypt128Ecb()
neilt6 0:a30389f7422b 25 {
neilt6 0:a30389f7422b 26 const char key[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
neilt6 0:a30389f7422b 27 const char expected1[16] = { 0x3A, 0xD7, 0x7B, 0xB4, 0x0D, 0x7A, 0x36, 0x60, 0xA8, 0x9E, 0xCA, 0xF3, 0x24, 0x66, 0xEF, 0x97 };
neilt6 0:a30389f7422b 28 const char expected2[16] = { 0xF5, 0xD3, 0xD5, 0x85, 0x03, 0xB9, 0x69, 0x9D, 0xE7, 0x85, 0x89, 0x5A, 0x96, 0xFD, 0xBA, 0xAF };
neilt6 0:a30389f7422b 29 const char expected3[16] = { 0x43, 0xB1, 0xCD, 0x7F, 0x59, 0x8E, 0xCE, 0x23, 0x88, 0x1B, 0x00, 0xE3, 0xED, 0x03, 0x06, 0x88 };
neilt6 0:a30389f7422b 30 const char expected4[16] = { 0x7B, 0x0C, 0x78, 0x5E, 0x27, 0xE8, 0xAD, 0x3F, 0x82, 0x23, 0x20, 0x71, 0x04, 0x72, 0x5D, 0xD4 };
neilt6 0:a30389f7422b 31 char out[16];
neilt6 0:a30389f7422b 32
neilt6 0:a30389f7422b 33 //Encrypt the test vectors using AES 128-bit ECB mode
neilt6 0:a30389f7422b 34 AES aes(key, AES::KEY_128, AES::MODE_ECB);
neilt6 0:a30389f7422b 35 aes.encrypt(testVector1, out, 16);
neilt6 0:a30389f7422b 36 if (strncmp(out, expected1, 16) != 0)
neilt6 0:a30389f7422b 37 return false;
neilt6 0:a30389f7422b 38 aes.encrypt(testVector2, out, 16);
neilt6 0:a30389f7422b 39 if (strncmp(out, expected2, 16) != 0)
neilt6 0:a30389f7422b 40 return false;
neilt6 0:a30389f7422b 41 aes.encrypt(testVector3, out, 16);
neilt6 0:a30389f7422b 42 if (strncmp(out, expected3, 16) != 0)
neilt6 0:a30389f7422b 43 return false;
neilt6 0:a30389f7422b 44 aes.encrypt(testVector4, out, 16);
neilt6 0:a30389f7422b 45 if (strncmp(out, expected4, 16) != 0)
neilt6 0:a30389f7422b 46 return false;
neilt6 0:a30389f7422b 47
neilt6 0:a30389f7422b 48 //Success!
neilt6 0:a30389f7422b 49 return true;
neilt6 0:a30389f7422b 50 }
neilt6 0:a30389f7422b 51
neilt6 0:a30389f7422b 52 bool testDecrypt128Ecb()
neilt6 0:a30389f7422b 53 {
neilt6 0:a30389f7422b 54 const char key[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
neilt6 0:a30389f7422b 55 const char in1[16] = { 0x3A, 0xD7, 0x7B, 0xB4, 0x0D, 0x7A, 0x36, 0x60, 0xA8, 0x9E, 0xCA, 0xF3, 0x24, 0x66, 0xEF, 0x97 };
neilt6 0:a30389f7422b 56 const char in2[16] = { 0xF5, 0xD3, 0xD5, 0x85, 0x03, 0xB9, 0x69, 0x9D, 0xE7, 0x85, 0x89, 0x5A, 0x96, 0xFD, 0xBA, 0xAF };
neilt6 0:a30389f7422b 57 const char in3[16] = { 0x43, 0xB1, 0xCD, 0x7F, 0x59, 0x8E, 0xCE, 0x23, 0x88, 0x1B, 0x00, 0xE3, 0xED, 0x03, 0x06, 0x88 };
neilt6 0:a30389f7422b 58 const char in4[16] = { 0x7B, 0x0C, 0x78, 0x5E, 0x27, 0xE8, 0xAD, 0x3F, 0x82, 0x23, 0x20, 0x71, 0x04, 0x72, 0x5D, 0xD4 };
neilt6 0:a30389f7422b 59 char out[16];
neilt6 0:a30389f7422b 60
neilt6 0:a30389f7422b 61 //Decrypt the test vectors using AES 128-bit ECB mode
neilt6 0:a30389f7422b 62 AES aes(key, AES::KEY_128, AES::MODE_ECB);
neilt6 0:a30389f7422b 63 aes.decrypt(in1, out, 16);
neilt6 0:a30389f7422b 64 if (strncmp(out, testVector1, 16) != 0)
neilt6 0:a30389f7422b 65 return false;
neilt6 0:a30389f7422b 66 aes.decrypt(in2, out, 16);
neilt6 0:a30389f7422b 67 if (strncmp(out, testVector2, 16) != 0)
neilt6 0:a30389f7422b 68 return false;
neilt6 0:a30389f7422b 69 aes.decrypt(in3, out, 16);
neilt6 0:a30389f7422b 70 if (strncmp(out, testVector3, 16) != 0)
neilt6 0:a30389f7422b 71 return false;
neilt6 0:a30389f7422b 72 aes.decrypt(in4, out, 16);
neilt6 0:a30389f7422b 73 if (strncmp(out, testVector4, 16) != 0)
neilt6 0:a30389f7422b 74 return false;
neilt6 0:a30389f7422b 75
neilt6 0:a30389f7422b 76 //Success!
neilt6 0:a30389f7422b 77 return true;
neilt6 0:a30389f7422b 78 }
neilt6 0:a30389f7422b 79
neilt6 0:a30389f7422b 80 bool testEncrypt192Ecb()
neilt6 0:a30389f7422b 81 {
neilt6 0:a30389f7422b 82 const char key[24] = { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52, 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5, 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B };
neilt6 0:a30389f7422b 83 const char expected1[16] = { 0xBD, 0x33, 0x4F, 0x1D, 0x6E, 0x45, 0xF2, 0x5F, 0xF7, 0x12, 0xA2, 0x14, 0x57, 0x1F, 0xA5, 0xCC };
neilt6 0:a30389f7422b 84 const char expected2[16] = { 0x97, 0x41, 0x04, 0x84, 0x6D, 0x0A, 0xD3, 0xAD, 0x77, 0x34, 0xEC, 0xB3, 0xEC, 0xEE, 0x4E, 0xEF };
neilt6 0:a30389f7422b 85 const char expected3[16] = { 0xEF, 0x7A, 0xFD, 0x22, 0x70, 0xE2, 0xE6, 0x0A, 0xDC, 0xE0, 0xBA, 0x2F, 0xAC, 0xE6, 0x44, 0x4E };
neilt6 0:a30389f7422b 86 const char expected4[16] = { 0x9A, 0x4B, 0x41, 0xBA, 0x73, 0x8D, 0x6C, 0x72, 0xFB, 0x16, 0x69, 0x16, 0x03, 0xC1, 0x8E, 0x0E };
neilt6 0:a30389f7422b 87 char out[16];
neilt6 0:a30389f7422b 88
neilt6 0:a30389f7422b 89 //Encrypt the test vectors using AES 192-bit ECB mode
neilt6 0:a30389f7422b 90 AES aes(key, AES::KEY_192);
neilt6 0:a30389f7422b 91 aes.encrypt(testVector1, out, 16);
neilt6 0:a30389f7422b 92 if (strncmp(out, expected1, 16) != 0)
neilt6 0:a30389f7422b 93 return false;
neilt6 0:a30389f7422b 94 aes.encrypt(testVector2, out, 16);
neilt6 0:a30389f7422b 95 if (strncmp(out, expected2, 16) != 0)
neilt6 0:a30389f7422b 96 return false;
neilt6 0:a30389f7422b 97 aes.encrypt(testVector3, out, 16);
neilt6 0:a30389f7422b 98 if (strncmp(out, expected3, 16) != 0)
neilt6 0:a30389f7422b 99 return false;
neilt6 0:a30389f7422b 100 aes.encrypt(testVector4, out, 16);
neilt6 0:a30389f7422b 101 if (strncmp(out, expected4, 16) != 0)
neilt6 0:a30389f7422b 102 return false;
neilt6 0:a30389f7422b 103
neilt6 0:a30389f7422b 104 //Success!
neilt6 0:a30389f7422b 105 return true;
neilt6 0:a30389f7422b 106 }
neilt6 0:a30389f7422b 107
neilt6 0:a30389f7422b 108 bool testDecrypt192Ecb()
neilt6 0:a30389f7422b 109 {
neilt6 0:a30389f7422b 110 const char key[24] = { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52, 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5, 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B };
neilt6 0:a30389f7422b 111 const char in1[16] = { 0xBD, 0x33, 0x4F, 0x1D, 0x6E, 0x45, 0xF2, 0x5F, 0xF7, 0x12, 0xA2, 0x14, 0x57, 0x1F, 0xA5, 0xCC };
neilt6 0:a30389f7422b 112 const char in2[16] = { 0x97, 0x41, 0x04, 0x84, 0x6D, 0x0A, 0xD3, 0xAD, 0x77, 0x34, 0xEC, 0xB3, 0xEC, 0xEE, 0x4E, 0xEF };
neilt6 0:a30389f7422b 113 const char in3[16] = { 0xEF, 0x7A, 0xFD, 0x22, 0x70, 0xE2, 0xE6, 0x0A, 0xDC, 0xE0, 0xBA, 0x2F, 0xAC, 0xE6, 0x44, 0x4E };
neilt6 0:a30389f7422b 114 const char in4[16] = { 0x9A, 0x4B, 0x41, 0xBA, 0x73, 0x8D, 0x6C, 0x72, 0xFB, 0x16, 0x69, 0x16, 0x03, 0xC1, 0x8E, 0x0E };
neilt6 0:a30389f7422b 115 char out[16];
neilt6 0:a30389f7422b 116
neilt6 0:a30389f7422b 117 //Decrypt the test vectors using AES 192-bit ECB mode
neilt6 0:a30389f7422b 118 AES aes(key, AES::KEY_192);
neilt6 0:a30389f7422b 119 aes.decrypt(in1, out, 16);
neilt6 0:a30389f7422b 120 if (strncmp(out, testVector1, 16) != 0)
neilt6 0:a30389f7422b 121 return false;
neilt6 0:a30389f7422b 122 aes.decrypt(in2, out, 16);
neilt6 0:a30389f7422b 123 if (strncmp(out, testVector2, 16) != 0)
neilt6 0:a30389f7422b 124 return false;
neilt6 0:a30389f7422b 125 aes.decrypt(in3, out, 16);
neilt6 0:a30389f7422b 126 if (strncmp(out, testVector3, 16) != 0)
neilt6 0:a30389f7422b 127 return false;
neilt6 0:a30389f7422b 128 aes.decrypt(in4, out, 16);
neilt6 0:a30389f7422b 129 if (strncmp(out, testVector4, 16) != 0)
neilt6 0:a30389f7422b 130 return false;
neilt6 0:a30389f7422b 131
neilt6 0:a30389f7422b 132 //Success!
neilt6 0:a30389f7422b 133 return true;
neilt6 0:a30389f7422b 134 }
neilt6 0:a30389f7422b 135
neilt6 0:a30389f7422b 136 bool testEncrypt256Ecb()
neilt6 0:a30389f7422b 137 {
neilt6 0:a30389f7422b 138 const char key[32] = { 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 };
neilt6 0:a30389f7422b 139 const char expected1[16] = { 0xF3, 0xEE, 0xD1, 0xBD, 0xB5, 0xD2, 0xA0, 0x3C, 0x06, 0x4B, 0x5A, 0x7E, 0x3D, 0xB1, 0x81, 0xF8 };
neilt6 0:a30389f7422b 140 const char expected2[16] = { 0x59, 0x1C, 0xCB, 0x10, 0xD4, 0x10, 0xED, 0x26, 0xDC, 0x5B, 0xA7, 0x4A, 0x31, 0x36, 0x28, 0x70 };
neilt6 0:a30389f7422b 141 const char expected3[16] = { 0xB6, 0xED, 0x21, 0xB9, 0x9C, 0xA6, 0xF4, 0xF9, 0xF1, 0x53, 0xE7, 0xB1, 0xBE, 0xAF, 0xED, 0x1D };
neilt6 0:a30389f7422b 142 const char expected4[16] = { 0x23, 0x30, 0x4B, 0x7A, 0x39, 0xF9, 0xF3, 0xFF, 0x06, 0x7D, 0x8D, 0x8F, 0x9E, 0x24, 0xEC, 0xC7 };
neilt6 0:a30389f7422b 143 char out[16];
neilt6 0:a30389f7422b 144
neilt6 0:a30389f7422b 145 //Encrypt the test vectors using AES 256-bit ECB mode
neilt6 0:a30389f7422b 146 AES aes(key, AES::KEY_256);
neilt6 0:a30389f7422b 147 aes.encrypt(testVector1, out, 16);
neilt6 0:a30389f7422b 148 if (strncmp(out, expected1, 16) != 0)
neilt6 0:a30389f7422b 149 return false;
neilt6 0:a30389f7422b 150 aes.encrypt(testVector2, out, 16);
neilt6 0:a30389f7422b 151 if (strncmp(out, expected2, 16) != 0)
neilt6 0:a30389f7422b 152 return false;
neilt6 0:a30389f7422b 153 aes.encrypt(testVector3, out, 16);
neilt6 0:a30389f7422b 154 if (strncmp(out, expected3, 16) != 0)
neilt6 0:a30389f7422b 155 return false;
neilt6 0:a30389f7422b 156 aes.encrypt(testVector4, out, 16);
neilt6 0:a30389f7422b 157 if (strncmp(out, expected4, 16) != 0)
neilt6 0:a30389f7422b 158 return false;
neilt6 0:a30389f7422b 159
neilt6 0:a30389f7422b 160 //Success!
neilt6 0:a30389f7422b 161 return true;
neilt6 0:a30389f7422b 162 }
neilt6 0:a30389f7422b 163
neilt6 0:a30389f7422b 164 bool testDecrypt256Ecb()
neilt6 0:a30389f7422b 165 {
neilt6 0:a30389f7422b 166 const char key[32] = { 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 };
neilt6 0:a30389f7422b 167 const char in1[16] = { 0xF3, 0xEE, 0xD1, 0xBD, 0xB5, 0xD2, 0xA0, 0x3C, 0x06, 0x4B, 0x5A, 0x7E, 0x3D, 0xB1, 0x81, 0xF8 };
neilt6 0:a30389f7422b 168 const char in2[16] = { 0x59, 0x1C, 0xCB, 0x10, 0xD4, 0x10, 0xED, 0x26, 0xDC, 0x5B, 0xA7, 0x4A, 0x31, 0x36, 0x28, 0x70 };
neilt6 0:a30389f7422b 169 const char in3[16] = { 0xB6, 0xED, 0x21, 0xB9, 0x9C, 0xA6, 0xF4, 0xF9, 0xF1, 0x53, 0xE7, 0xB1, 0xBE, 0xAF, 0xED, 0x1D };
neilt6 0:a30389f7422b 170 const char in4[16] = { 0x23, 0x30, 0x4B, 0x7A, 0x39, 0xF9, 0xF3, 0xFF, 0x06, 0x7D, 0x8D, 0x8F, 0x9E, 0x24, 0xEC, 0xC7 };
neilt6 0:a30389f7422b 171 char out[16];
neilt6 0:a30389f7422b 172
neilt6 0:a30389f7422b 173 //Decrypt the test vectors using AES 256-bit ECB mode
neilt6 0:a30389f7422b 174 AES aes(key, AES::KEY_256);
neilt6 0:a30389f7422b 175 aes.decrypt(in1, out, 16);
neilt6 0:a30389f7422b 176 if (strncmp(out, testVector1, 16) != 0)
neilt6 0:a30389f7422b 177 return false;
neilt6 0:a30389f7422b 178 aes.decrypt(in2, out, 16);
neilt6 0:a30389f7422b 179 if (strncmp(out, testVector2, 16) != 0)
neilt6 0:a30389f7422b 180 return false;
neilt6 0:a30389f7422b 181 aes.decrypt(in3, out, 16);
neilt6 0:a30389f7422b 182 if (strncmp(out, testVector3, 16) != 0)
neilt6 0:a30389f7422b 183 return false;
neilt6 0:a30389f7422b 184 aes.decrypt(in4, out, 16);
neilt6 0:a30389f7422b 185 if (strncmp(out, testVector4, 16) != 0)
neilt6 0:a30389f7422b 186 return false;
neilt6 0:a30389f7422b 187
neilt6 0:a30389f7422b 188 //Success!
neilt6 0:a30389f7422b 189 return true;
neilt6 0:a30389f7422b 190 }
neilt6 0:a30389f7422b 191
neilt6 0:a30389f7422b 192 bool testEncrypt128Cbc()
neilt6 0:a30389f7422b 193 {
neilt6 0:a30389f7422b 194 const char key[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
neilt6 0:a30389f7422b 195 const char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
neilt6 0:a30389f7422b 196 const char expected1[16] = { 0x76, 0x49, 0xAB, 0xAC, 0x81, 0x19, 0xB2, 0x46, 0xCE, 0xE9, 0x8E, 0x9B, 0x12, 0xE9, 0x19, 0x7D };
neilt6 0:a30389f7422b 197 const char expected2[16] = { 0x50, 0x86, 0xCB, 0x9B, 0x50, 0x72, 0x19, 0xEE, 0x95, 0xDB, 0x11, 0x3A, 0x91, 0x76, 0x78, 0xB2 };
neilt6 0:a30389f7422b 198 const char expected3[16] = { 0x73, 0xBE, 0xD6, 0xB8, 0xE3, 0xC1, 0x74, 0x3B, 0x71, 0x16, 0xE6, 0x9E, 0x22, 0x22, 0x95, 0x16 };
neilt6 0:a30389f7422b 199 const char expected4[16] = { 0x3F, 0xF1, 0xCA, 0xA1, 0x68, 0x1F, 0xAC, 0x09, 0x12, 0x0E, 0xCA, 0x30, 0x75, 0x86, 0xE1, 0xA7 };
neilt6 0:a30389f7422b 200 char out[16];
neilt6 0:a30389f7422b 201
neilt6 0:a30389f7422b 202 //Encrypt the test vectors using AES 128-bit CBC mode
neilt6 0:a30389f7422b 203 AES aes(key, AES::KEY_128, AES::MODE_CBC, iv);
neilt6 0:a30389f7422b 204 aes.encrypt(testVector1, out, 16);
neilt6 0:a30389f7422b 205 if (strncmp(out, expected1, 16) != 0)
neilt6 0:a30389f7422b 206 return false;
neilt6 0:a30389f7422b 207 aes.encrypt(testVector2, out, 16);
neilt6 0:a30389f7422b 208 if (strncmp(out, expected2, 16) != 0)
neilt6 0:a30389f7422b 209 return false;
neilt6 0:a30389f7422b 210 aes.encrypt(testVector3, out, 16);
neilt6 0:a30389f7422b 211 if (strncmp(out, expected3, 16) != 0)
neilt6 0:a30389f7422b 212 return false;
neilt6 0:a30389f7422b 213 aes.encrypt(testVector4, out, 16);
neilt6 0:a30389f7422b 214 if (strncmp(out, expected4, 16) != 0)
neilt6 0:a30389f7422b 215 return false;
neilt6 0:a30389f7422b 216
neilt6 0:a30389f7422b 217 //Success!
neilt6 0:a30389f7422b 218 return true;
neilt6 0:a30389f7422b 219 }
neilt6 0:a30389f7422b 220
neilt6 0:a30389f7422b 221 bool testDecrypt128Cbc()
neilt6 0:a30389f7422b 222 {
neilt6 0:a30389f7422b 223 const char key[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
neilt6 0:a30389f7422b 224 const char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
neilt6 0:a30389f7422b 225 const char in1[16] = { 0x76, 0x49, 0xAB, 0xAC, 0x81, 0x19, 0xB2, 0x46, 0xCE, 0xE9, 0x8E, 0x9B, 0x12, 0xE9, 0x19, 0x7D };
neilt6 0:a30389f7422b 226 const char in2[16] = { 0x50, 0x86, 0xCB, 0x9B, 0x50, 0x72, 0x19, 0xEE, 0x95, 0xDB, 0x11, 0x3A, 0x91, 0x76, 0x78, 0xB2 };
neilt6 0:a30389f7422b 227 const char in3[16] = { 0x73, 0xBE, 0xD6, 0xB8, 0xE3, 0xC1, 0x74, 0x3B, 0x71, 0x16, 0xE6, 0x9E, 0x22, 0x22, 0x95, 0x16 };
neilt6 0:a30389f7422b 228 const char in4[16] = { 0x3F, 0xF1, 0xCA, 0xA1, 0x68, 0x1F, 0xAC, 0x09, 0x12, 0x0E, 0xCA, 0x30, 0x75, 0x86, 0xE1, 0xA7 };
neilt6 0:a30389f7422b 229 char out[16];
neilt6 0:a30389f7422b 230
neilt6 0:a30389f7422b 231 //Decrypt the test vectors using AES 128-bit CBC mode
neilt6 0:a30389f7422b 232 AES aes(key, AES::KEY_128, AES::MODE_CBC, iv);
neilt6 0:a30389f7422b 233 aes.decrypt(in1, out, 16);
neilt6 0:a30389f7422b 234 if (strncmp(out, testVector1, 16) != 0)
neilt6 0:a30389f7422b 235 return false;
neilt6 0:a30389f7422b 236 aes.decrypt(in2, out, 16);
neilt6 0:a30389f7422b 237 if (strncmp(out, testVector2, 16) != 0)
neilt6 0:a30389f7422b 238 return false;
neilt6 0:a30389f7422b 239 aes.decrypt(in3, out, 16);
neilt6 0:a30389f7422b 240 if (strncmp(out, testVector3, 16) != 0)
neilt6 0:a30389f7422b 241 return false;
neilt6 0:a30389f7422b 242 aes.decrypt(in4, out, 16);
neilt6 0:a30389f7422b 243 if (strncmp(out, testVector4, 16) != 0)
neilt6 0:a30389f7422b 244 return false;
neilt6 0:a30389f7422b 245
neilt6 0:a30389f7422b 246 //Success!
neilt6 0:a30389f7422b 247 return true;
neilt6 0:a30389f7422b 248 }
neilt6 0:a30389f7422b 249
neilt6 0:a30389f7422b 250 bool testEncrypt192Cbc()
neilt6 0:a30389f7422b 251 {
neilt6 0:a30389f7422b 252 const char key[24] = { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52, 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5, 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B };
neilt6 0:a30389f7422b 253 const char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
neilt6 0:a30389f7422b 254 const char expected1[16] = { 0x4F, 0x02, 0x1D, 0xB2, 0x43, 0xBC, 0x63, 0x3D, 0x71, 0x78, 0x18, 0x3A, 0x9F, 0xA0, 0x71, 0xE8 };
neilt6 0:a30389f7422b 255 const char expected2[16] = { 0xB4, 0xD9, 0xAD, 0xA9, 0xAD, 0x7D, 0xED, 0xF4, 0xE5, 0xE7, 0x38, 0x76, 0x3F, 0x69, 0x14, 0x5A };
neilt6 0:a30389f7422b 256 const char expected3[16] = { 0x57, 0x1B, 0x24, 0x20, 0x12, 0xFB, 0x7A, 0xE0, 0x7F, 0xA9, 0xBA, 0xAC, 0x3D, 0xF1, 0x02, 0xE0 };
neilt6 0:a30389f7422b 257 const char expected4[16] = { 0x08, 0xB0, 0xE2, 0x79, 0x88, 0x59, 0x88, 0x81, 0xD9, 0x20, 0xA9, 0xE6, 0x4F, 0x56, 0x15, 0xCD };
neilt6 0:a30389f7422b 258 char out[16];
neilt6 0:a30389f7422b 259
neilt6 0:a30389f7422b 260 //Encrypt the test vectors using AES 192-bit CBC mode
neilt6 0:a30389f7422b 261 AES aes(key, AES::KEY_192, AES::MODE_CBC, iv);
neilt6 0:a30389f7422b 262 aes.encrypt(testVector1, out, 16);
neilt6 0:a30389f7422b 263 if (strncmp(out, expected1, 16) != 0)
neilt6 0:a30389f7422b 264 return false;
neilt6 0:a30389f7422b 265 aes.encrypt(testVector2, out, 16);
neilt6 0:a30389f7422b 266 if (strncmp(out, expected2, 16) != 0)
neilt6 0:a30389f7422b 267 return false;
neilt6 0:a30389f7422b 268 aes.encrypt(testVector3, out, 16);
neilt6 0:a30389f7422b 269 if (strncmp(out, expected3, 16) != 0)
neilt6 0:a30389f7422b 270 return false;
neilt6 0:a30389f7422b 271 aes.encrypt(testVector4, out, 16);
neilt6 0:a30389f7422b 272 if (strncmp(out, expected4, 16) != 0)
neilt6 0:a30389f7422b 273 return false;
neilt6 0:a30389f7422b 274
neilt6 0:a30389f7422b 275 //Success!
neilt6 0:a30389f7422b 276 return true;
neilt6 0:a30389f7422b 277 }
neilt6 0:a30389f7422b 278
neilt6 0:a30389f7422b 279 bool testDecrypt192Cbc()
neilt6 0:a30389f7422b 280 {
neilt6 0:a30389f7422b 281 const char key[24] = { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52, 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5, 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B };
neilt6 0:a30389f7422b 282 const char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
neilt6 0:a30389f7422b 283 const char in1[16] = { 0x4F, 0x02, 0x1D, 0xB2, 0x43, 0xBC, 0x63, 0x3D, 0x71, 0x78, 0x18, 0x3A, 0x9F, 0xA0, 0x71, 0xE8 };
neilt6 0:a30389f7422b 284 const char in2[16] = { 0xB4, 0xD9, 0xAD, 0xA9, 0xAD, 0x7D, 0xED, 0xF4, 0xE5, 0xE7, 0x38, 0x76, 0x3F, 0x69, 0x14, 0x5A };
neilt6 0:a30389f7422b 285 const char in3[16] = { 0x57, 0x1B, 0x24, 0x20, 0x12, 0xFB, 0x7A, 0xE0, 0x7F, 0xA9, 0xBA, 0xAC, 0x3D, 0xF1, 0x02, 0xE0 };
neilt6 0:a30389f7422b 286 const char in4[16] = { 0x08, 0xB0, 0xE2, 0x79, 0x88, 0x59, 0x88, 0x81, 0xD9, 0x20, 0xA9, 0xE6, 0x4F, 0x56, 0x15, 0xCD };
neilt6 0:a30389f7422b 287 char out[16];
neilt6 0:a30389f7422b 288
neilt6 0:a30389f7422b 289 //Decrypt the test vectors using AES 192-bit CBC mode
neilt6 0:a30389f7422b 290 AES aes(key, AES::KEY_192, AES::MODE_CBC, iv);
neilt6 0:a30389f7422b 291 aes.decrypt(in1, out, 16);
neilt6 0:a30389f7422b 292 if (strncmp(out, testVector1, 16) != 0)
neilt6 0:a30389f7422b 293 return false;
neilt6 0:a30389f7422b 294 aes.decrypt(in2, out, 16);
neilt6 0:a30389f7422b 295 if (strncmp(out, testVector2, 16) != 0)
neilt6 0:a30389f7422b 296 return false;
neilt6 0:a30389f7422b 297 aes.decrypt(in3, out, 16);
neilt6 0:a30389f7422b 298 if (strncmp(out, testVector3, 16) != 0)
neilt6 0:a30389f7422b 299 return false;
neilt6 0:a30389f7422b 300 aes.decrypt(in4, out, 16);
neilt6 0:a30389f7422b 301 if (strncmp(out, testVector4, 16) != 0)
neilt6 0:a30389f7422b 302 return false;
neilt6 0:a30389f7422b 303
neilt6 0:a30389f7422b 304 //Success!
neilt6 0:a30389f7422b 305 return true;
neilt6 0:a30389f7422b 306 }
neilt6 0:a30389f7422b 307
neilt6 0:a30389f7422b 308 bool testEncrypt256Cbc()
neilt6 0:a30389f7422b 309 {
neilt6 0:a30389f7422b 310 const char key[32] = { 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 };
neilt6 0:a30389f7422b 311 const char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
neilt6 0:a30389f7422b 312 const char expected1[16] = { 0xF5, 0x8C, 0x4C, 0x04, 0xD6, 0xE5, 0xF1, 0xBA, 0x77, 0x9E, 0xAB, 0xFB, 0x5F, 0x7B, 0xFB, 0xD6 };
neilt6 0:a30389f7422b 313 const char expected2[16] = { 0x9C, 0xFC, 0x4E, 0x96, 0x7E, 0xDB, 0x80, 0x8D, 0x67, 0x9F, 0x77, 0x7B, 0xC6, 0x70, 0x2C, 0x7D };
neilt6 0:a30389f7422b 314 const char expected3[16] = { 0x39, 0xF2, 0x33, 0x69, 0xA9, 0xD9, 0xBA, 0xCF, 0xA5, 0x30, 0xE2, 0x63, 0x04, 0x23, 0x14, 0x61 };
neilt6 0:a30389f7422b 315 const char expected4[16] = { 0xB2, 0xEB, 0x05, 0xE2, 0xC3, 0x9B, 0xE9, 0xFC, 0xDA, 0x6C, 0x19, 0x07, 0x8C, 0x6A, 0x9D, 0x1B };
neilt6 0:a30389f7422b 316 char out[16];
neilt6 0:a30389f7422b 317
neilt6 0:a30389f7422b 318 //Encrypt the test vectors using AES 256-bit CBC mode
neilt6 0:a30389f7422b 319 AES aes(key, AES::KEY_256, AES::MODE_CBC, iv);
neilt6 0:a30389f7422b 320 aes.encrypt(testVector1, out, 16);
neilt6 0:a30389f7422b 321 if (strncmp(out, expected1, 16) != 0)
neilt6 0:a30389f7422b 322 return false;
neilt6 0:a30389f7422b 323 aes.encrypt(testVector2, out, 16);
neilt6 0:a30389f7422b 324 if (strncmp(out, expected2, 16) != 0)
neilt6 0:a30389f7422b 325 return false;
neilt6 0:a30389f7422b 326 aes.encrypt(testVector3, out, 16);
neilt6 0:a30389f7422b 327 if (strncmp(out, expected3, 16) != 0)
neilt6 0:a30389f7422b 328 return false;
neilt6 0:a30389f7422b 329 aes.encrypt(testVector4, out, 16);
neilt6 0:a30389f7422b 330 if (strncmp(out, expected4, 16) != 0)
neilt6 0:a30389f7422b 331 return false;
neilt6 0:a30389f7422b 332
neilt6 0:a30389f7422b 333 //Success!
neilt6 0:a30389f7422b 334 return true;
neilt6 0:a30389f7422b 335 }
neilt6 0:a30389f7422b 336
neilt6 0:a30389f7422b 337 bool testDecrypt256Cbc()
neilt6 0:a30389f7422b 338 {
neilt6 0:a30389f7422b 339 const char key[32] = { 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 };
neilt6 0:a30389f7422b 340 const char iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
neilt6 0:a30389f7422b 341 const char in1[16] = { 0xF5, 0x8C, 0x4C, 0x04, 0xD6, 0xE5, 0xF1, 0xBA, 0x77, 0x9E, 0xAB, 0xFB, 0x5F, 0x7B, 0xFB, 0xD6 };
neilt6 0:a30389f7422b 342 const char in2[16] = { 0x9C, 0xFC, 0x4E, 0x96, 0x7E, 0xDB, 0x80, 0x8D, 0x67, 0x9F, 0x77, 0x7B, 0xC6, 0x70, 0x2C, 0x7D };
neilt6 0:a30389f7422b 343 const char in3[16] = { 0x39, 0xF2, 0x33, 0x69, 0xA9, 0xD9, 0xBA, 0xCF, 0xA5, 0x30, 0xE2, 0x63, 0x04, 0x23, 0x14, 0x61 };
neilt6 0:a30389f7422b 344 const char in4[16] = { 0xB2, 0xEB, 0x05, 0xE2, 0xC3, 0x9B, 0xE9, 0xFC, 0xDA, 0x6C, 0x19, 0x07, 0x8C, 0x6A, 0x9D, 0x1B };
neilt6 0:a30389f7422b 345 char out[16];
neilt6 0:a30389f7422b 346
neilt6 0:a30389f7422b 347 //Decrypt the test vectors using AES 256-bit CBC mode
neilt6 0:a30389f7422b 348 AES aes(key, AES::KEY_256, AES::MODE_CBC, iv);
neilt6 0:a30389f7422b 349 aes.decrypt(in1, out, 16);
neilt6 0:a30389f7422b 350 if (strncmp(out, testVector1, 16) != 0)
neilt6 0:a30389f7422b 351 return false;
neilt6 0:a30389f7422b 352 aes.decrypt(in2, out, 16);
neilt6 0:a30389f7422b 353 if (strncmp(out, testVector2, 16) != 0)
neilt6 0:a30389f7422b 354 return false;
neilt6 0:a30389f7422b 355 aes.decrypt(in3, out, 16);
neilt6 0:a30389f7422b 356 if (strncmp(out, testVector3, 16) != 0)
neilt6 0:a30389f7422b 357 return false;
neilt6 0:a30389f7422b 358 aes.decrypt(in4, out, 16);
neilt6 0:a30389f7422b 359 if (strncmp(out, testVector4, 16) != 0)
neilt6 0:a30389f7422b 360 return false;
neilt6 0:a30389f7422b 361
neilt6 0:a30389f7422b 362 //Success!
neilt6 0:a30389f7422b 363 return true;
neilt6 0:a30389f7422b 364 }
neilt6 0:a30389f7422b 365
neilt6 0:a30389f7422b 366 int main()
neilt6 0:a30389f7422b 367 {
neilt6 0:a30389f7422b 368 //Run all of the tests
neilt6 0:a30389f7422b 369 printf("Running AES tests...\n");
neilt6 0:a30389f7422b 370 printf("\tTesting AES 128-bit ECB mode encryption...");
neilt6 0:a30389f7422b 371 if (testEncrypt128Ecb())
neilt6 0:a30389f7422b 372 printf("passed!\n");
neilt6 0:a30389f7422b 373 else
neilt6 0:a30389f7422b 374 error("failed!\n");
neilt6 0:a30389f7422b 375 printf("\tTesting AES 128-bit ECB mode decryption...");
neilt6 0:a30389f7422b 376 if (testDecrypt128Ecb())
neilt6 0:a30389f7422b 377 printf("passed!\n");
neilt6 0:a30389f7422b 378 else
neilt6 0:a30389f7422b 379 error("failed!\n");
neilt6 0:a30389f7422b 380 printf("\tTesting AES 192-bit ECB mode encryption...");
neilt6 0:a30389f7422b 381 if (testEncrypt192Ecb())
neilt6 0:a30389f7422b 382 printf("passed!\n");
neilt6 0:a30389f7422b 383 else
neilt6 0:a30389f7422b 384 error("failed!\n");
neilt6 0:a30389f7422b 385 printf("\tTesting AES 192-bit ECB mode decryption...");
neilt6 0:a30389f7422b 386 if (testDecrypt192Ecb())
neilt6 0:a30389f7422b 387 printf("passed!\n");
neilt6 0:a30389f7422b 388 else
neilt6 0:a30389f7422b 389 error("failed!\n");
neilt6 0:a30389f7422b 390 printf("\tTesting AES 256-bit ECB mode encryption...");
neilt6 0:a30389f7422b 391 if (testEncrypt256Ecb())
neilt6 0:a30389f7422b 392 printf("passed!\n");
neilt6 0:a30389f7422b 393 else
neilt6 0:a30389f7422b 394 error("failed!\n");
neilt6 0:a30389f7422b 395 printf("\tTesting AES 256-bit ECB mode decryption...");
neilt6 0:a30389f7422b 396 if (testDecrypt256Ecb())
neilt6 0:a30389f7422b 397 printf("passed!\n");
neilt6 0:a30389f7422b 398 else
neilt6 0:a30389f7422b 399 error("failed!\n");
neilt6 0:a30389f7422b 400 printf("\tTesting AES 128-bit CBC mode encryption...");
neilt6 0:a30389f7422b 401 if (testEncrypt128Cbc())
neilt6 0:a30389f7422b 402 printf("passed!\n");
neilt6 0:a30389f7422b 403 else
neilt6 0:a30389f7422b 404 error("failed!\n");
neilt6 0:a30389f7422b 405 printf("\tTesting AES 128-bit CBC mode decryption...");
neilt6 0:a30389f7422b 406 if (testDecrypt128Cbc())
neilt6 0:a30389f7422b 407 printf("passed!\n");
neilt6 0:a30389f7422b 408 else
neilt6 0:a30389f7422b 409 error("failed!\n");
neilt6 0:a30389f7422b 410 printf("\tTesting AES 192-bit CBC mode encryption...");
neilt6 0:a30389f7422b 411 if (testEncrypt192Cbc())
neilt6 0:a30389f7422b 412 printf("passed!\n");
neilt6 0:a30389f7422b 413 else
neilt6 0:a30389f7422b 414 error("failed!\n");
neilt6 0:a30389f7422b 415 printf("\tTesting AES 192-bit CBC mode decryption...");
neilt6 0:a30389f7422b 416 if (testDecrypt192Cbc())
neilt6 0:a30389f7422b 417 printf("passed!\n");
neilt6 0:a30389f7422b 418 else
neilt6 0:a30389f7422b 419 error("failed!\n");
neilt6 0:a30389f7422b 420 printf("\tTesting AES 256-bit CBC mode encryption...");
neilt6 0:a30389f7422b 421 if (testEncrypt256Cbc())
neilt6 0:a30389f7422b 422 printf("passed!\n");
neilt6 0:a30389f7422b 423 else
neilt6 0:a30389f7422b 424 error("failed!\n");
neilt6 0:a30389f7422b 425 printf("\tTesting AES 256-bit CBC mode decryption...");
neilt6 0:a30389f7422b 426 if (testDecrypt256Cbc())
neilt6 0:a30389f7422b 427 printf("passed!\n");
neilt6 0:a30389f7422b 428 else
neilt6 0:a30389f7422b 429 error("failed!\n");
neilt6 0:a30389f7422b 430 printf("done!\n");
neilt6 0:a30389f7422b 431 }