9 byte cipher [N_BLOCK] ;
10 byte check [N_BLOCK] ;
12 void print_value (const char * str, byte * a, int bits);
13 void prekey_test_var_plaintext (int bits);
14 void prekey_test_var_key (int bits);
15 void set_bits (int bits, byte * a, int count);
16 void check_same (byte * a, byte * b, int bits);
17 void monte_carlo (int bits);
19 int main(int argc, char** argv)
21 printf ("AES library test vectors") ;
25 for (int keysize = 128 ; keysize <= 256 ; keysize += 64)
27 prekey_test_var_plaintext (keysize) ;
28 prekey_test_var_key (keysize) ;
33 void prekey_test_var_plaintext (int bits)
35 printf ("\nECB Varying Plaintext %i bits\n",bits) ;
38 set_bits (bits, key, 0) ; // all zero key
39 succ = aes.set_key (key, bits) ;
41 printf("Failure set_key\n") ;
44 for (int bitcount = 1 ; bitcount <= 128 ; bitcount++)
46 printf ("COUNT = %i \n",bitcount-1);
47 print_value ("KEY = ", key, bits) ;
48 set_bits (128, plain, bitcount) ;
50 print_value ("PLAINTEXT = ", plain, 128) ;
52 succ = aes.encrypt (plain, cipher) ;
54 printf ("Failure encrypt\n") ;
56 print_value ("CIPHERTEXT = ", cipher, 128) ;
58 succ = aes.decrypt (cipher, check) ;
60 printf ("Failure decrypt\n") ;
62 //print_value ("CHECK = ", check, 128) ;
63 check_same (plain, check, 128) ;
69 void prekey_test_var_key (int bits)
71 printf ("\nECB Varying key %i bits\n",bits);
74 set_bits (128, plain, 0) ;
76 for (int bitcount = 1 ; bitcount <= bits ; bitcount++)
78 set_bits (bits, key, bitcount) ; // all zero key
79 succ = aes.set_key (key, bits) ;
81 printf ("Failure set_key\n") ;
82 printf ("COUNT = %i\n",bitcount-1);
83 print_value ("KEY = ", key, bits) ;
85 print_value ("PLAINTEXT = ", plain, 128) ;
87 succ = aes.encrypt (plain, cipher) ;
89 printf ("Failure encrypt\n") ;
91 print_value ("CIPHERTEXT = ", cipher, 128) ;
93 succ = aes.decrypt (cipher, check) ;
95 printf ("Failure decrypt\n") ;
97 check_same (plain, check, 128) ;
102 void set_bits (int bits, byte * a, int count)
105 byte bcount = count >> 3 ;
106 for (byte i = 0 ; i < bcount ; i++)
108 if ((count & 7) != 0)
109 a [bcount++] = 0xFF & (0xFF00 >> (count & 7)) ;
110 for (byte i = bcount ; i < bits ; i++)
114 void check_same (byte * a, byte * b, int bits)
117 for (byte i = 0 ; i < bits ; i++)
120 printf ("Failure plain != check\n") ;
125 char hex[] = "0123456789abcdef" ;
128 void print_value (const char * str, byte * a, int bits)
132 for (int i = 0 ; i < bits ; i++)
135 printf("%c%c",hex[b >> 4],hex [b & 15]);
136 //Serial.print (hex [b >> 4]) ;
137 //Serial.print (hex [b & 15]) ;
143 { 0xb9, 0x14, 0x5a, 0x76, 0x8b, 0x7d, 0xc4, 0x89,
144 0xa0, 0x96, 0xb5, 0x46, 0xf4, 0x3b, 0x23, 0x1f } ;
146 { 0x13, 0x9a, 0x35, 0x42, 0x2f, 0x1d, 0x61, 0xde,
147 0x3c, 0x91, 0x78, 0x7f, 0xe0, 0x50, 0x7a, 0xfd } ;
149 void monte_carlo (int bits)
151 printf ("\nMonte Carlo %i bits\n",bits);
153 for (int i = 0 ; i < 16 ; i++)
155 plain [i] = monteplain [i] ;
156 key [i] = montekey [i] ;
158 for (int i = 0 ; i < 100 ; i++)
160 printf ("COUNT = %i\n",i);
161 print_value ("KEY = ", key, bits) ;
162 print_value ("PLAINTEXT = ", plain, 128) ;
163 succ = aes.set_key (key, bits) ;
164 for (int j = 0 ; j < 1000 ; j++)
166 succ = aes.encrypt (plain, cipher) ;
167 aes.copy_n_bytes (plain, cipher, 16) ;
169 print_value ("CIPHERTEXT = ", cipher, 128) ;
173 for (byte k = 0 ; k < 16 ; k++)
174 key [k] ^= cipher [k] ;
176 else if (bits == 192)