Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silccrypt / tests / test_aes.c
1
2 #include "silc.h"
3
4 /* Test vectors from RFC3602. */
5
6 /* First test vector, 16 bytes plaintext, 128 bits key */
7 const unsigned char key1[] = "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06";
8 int key1_len = 16 * 8;
9 const unsigned char iv1[] = "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41";
10 const unsigned char p1[] = "Single block msg";
11 int p1_len = 16;
12 const unsigned char c1[] = "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a";
13
14 /* Second test vector, 32 bytes plaintext, 128 bits key */
15 const unsigned char key2[] = "\xc2\x86\x69\x6d\x88\x7c\x9a\xa0\x61\x1b\xbb\x3e\x20\x25\xa4\x5a";
16 int key2_len = 16 * 8;
17 const unsigned char iv2[] = "\x56\x2e\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58";
18 const unsigned char p2[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
19 int p2_len = 32;
20 const unsigned char c2[] = "\xd2\x96\xcd\x94\xc2\xcc\xcf\x8a\x3a\x86\x30\x28\xb5\xe1\xdc\x0a\x75\x86\x60\x2d\x25\x3c\xff\xf9\x1b\x82\x66\xbe\xa6\xd6\x1a\xb1";
21
22 /* CTR test vectors from RFC3686. */
23
24 /* 16 bytes plaintext, 128 bits key */
25 const unsigned char key3[] = "\xAE\x68\x52\xF8\x12\x10\x67\xCC\x4B\xF7\xA5\x76\x55\x77\xF3\x9E";
26 int key3_len = 16 * 8;
27 const unsigned char iv3[] = "\x00\x00\x00\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
28 const unsigned char p3[] = "Single block msg";
29 int p3_len = 16;
30 const unsigned char c3[] = "\xE4\x09\x5D\x4F\xB7\xA7\xB3\x79\x2D\x61\x75\xA3\x26\x13\x11\xB8";
31
32 /* 32 bytes plaintext, 128 bits key */
33 const unsigned char key4[] = "\x7E\x24\x06\x78\x17\xFA\xE0\xD7\x43\xD6\xCE\x1F\x32\x53\x91\x63";
34 int key4_len = 16 * 8;
35 const unsigned char iv4[] = "\x00\x6C\xB6\xDB\xC0\x54\x3B\x59\xDA\x48\xD9\x0B\x00\x00\x00\x00";
36 const unsigned char p4[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
37 int p4_len = 32;
38 const unsigned char c4[] = "\x51\x04\xA1\x06\x16\x8A\x72\xD9\x79\x0D\x41\xEE\x8E\xDA\xD3\x88\xEB\x2E\x1E\xFC\x46\xDA\x57\xC8\xFC\xE6\x30\xDF\x91\x41\xBE\x28";
39
40 /* 36 bytes plaintext, 256 bits key */
41 const unsigned char key5[] = "\xFF\x7A\x61\x7C\xE6\x91\x48\xE4\xF1\x72\x6E\x2F\x43\x58\x1D\xE2\xAA\x62\xD9\xF8\x05\x53\x2E\xDF\xF1\xEE\xD6\x87\xFB\x54\x15\x3D";
42 int key5_len = 32 * 8;
43 const unsigned char iv5[] = "\x00\x1C\xC5\xB7\x51\xA5\x1D\x70\xA1\xC1\x11\x48\x00\x00\x00\x00";
44 const unsigned char p5[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23";
45 int p5_len = 36;
46 const unsigned char c5[] = "\xEB\x6C\x52\x82\x1D\x0B\xBB\xF7\xCE\x75\x94\x46\x2A\xCA\x4F\xAA\xB4\x07\xDF\x86\x65\x69\xFD\x07\xF4\x8C\xC0\xB5\x83\xD6\x07\x1F\x1E\xC0\xE6\xB8";
47
48 int main(int argc, char **argv)
49 {
50   SilcBool success = FALSE;
51   SilcCipher cipher, cipher2;
52   unsigned char dst[256], pdst[256];
53   int i;
54
55   if (argc > 1 && !strcmp(argv[1], "-d")) {
56     silc_log_debug(TRUE);
57     silc_log_debug_hexdump(TRUE);
58     silc_log_set_debug_string("*crypt*,*aes*,*cipher*");
59   }
60
61   SILC_LOG_DEBUG(("Registering builtin hash functions"));
62   silc_cipher_register_default();
63
64   SILC_LOG_DEBUG(("Allocating AES-CBC cipher"));
65   if (!silc_cipher_alloc("aes-128-cbc", &cipher)) {
66     SILC_LOG_DEBUG(("Allocating AES-CBC cipher failed"));
67     goto err;
68   }
69   if (!silc_cipher_alloc("aes-128-cbc", &cipher2)) {
70     SILC_LOG_DEBUG(("Allocating AES-CBC cipher failed"));
71     goto err;
72   }
73
74   /* First test vector */
75   SILC_LOG_DEBUG(("First test vector"));
76   memset(dst, 0, sizeof(dst));
77   memset(pdst, 0, sizeof(pdst));
78   silc_cipher_set_iv(cipher, iv1);
79   assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE));
80   assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE));
81   assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL));
82   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
83                  silc_cipher_get_block_len(cipher),
84                  silc_cipher_get_key_len(cipher),
85                  silc_cipher_get_name(cipher)));
86   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len);
87   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len);
88   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len);
89   if (memcmp(dst, c1, p1_len)) {
90     SILC_LOG_DEBUG(("Encrypt failed"));
91     goto err;
92   }
93   SILC_LOG_DEBUG(("Encrypt is successful"));
94   silc_cipher_set_iv(cipher2, iv1);
95   assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL));
96   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len);
97   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len);
98   if (memcmp(pdst, p1, p1_len)) {
99     SILC_LOG_DEBUG(("Decrypt failed"));
100     goto err;
101   }
102   SILC_LOG_DEBUG(("Decrypt is successful"));
103
104
105   /* Second test vector */
106   SILC_LOG_DEBUG(("Second test vector"));
107   memset(dst, 0, sizeof(dst));
108   memset(pdst, 0, sizeof(pdst));
109   silc_cipher_set_iv(cipher, iv2);
110   assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE));
111   assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE));
112   assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL));
113   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
114                  silc_cipher_get_block_len(cipher),
115                  silc_cipher_get_key_len(cipher),
116                  silc_cipher_get_name(cipher)));
117   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len);
118   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len);
119   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len);
120   if (memcmp(dst, c2, p2_len)) {
121     SILC_LOG_DEBUG(("Encrypt failed"));
122     goto err;
123   }
124   SILC_LOG_DEBUG(("Encrypt is successful"));
125   silc_cipher_set_iv(cipher2, iv2);
126   assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL));
127   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len);
128   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len);
129   if (memcmp(pdst, p2, p2_len)) {
130     SILC_LOG_DEBUG(("Decrypt failed"));
131     goto err;
132   }
133   SILC_LOG_DEBUG(("Decrypt is successful"));
134   silc_cipher_free(cipher);
135   silc_cipher_free(cipher2);
136
137
138   SILC_LOG_DEBUG(("Allocating aes-128-ctr cipher"));
139   if (!silc_cipher_alloc("aes-128-ctr", &cipher)) {
140     SILC_LOG_DEBUG(("Allocating aes-128-ctr cipher failed"));
141     goto err;
142   }
143
144   /* Third test vector */
145   SILC_LOG_DEBUG(("Third test vector"));
146   memset(dst, 0, sizeof(dst));
147   memset(pdst, 0, sizeof(pdst));
148   silc_cipher_set_iv(cipher, iv3);
149   assert(silc_cipher_set_key(cipher, key3, key3_len, TRUE));
150   assert(silc_cipher_encrypt(cipher, p3, dst, p3_len, NULL));
151   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
152                  silc_cipher_get_block_len(cipher),
153                  silc_cipher_get_key_len(cipher),
154                  silc_cipher_get_name(cipher)));
155   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p3, p3_len);
156   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p3_len);
157   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c3, p3_len);
158   if (memcmp(dst, c3, p3_len)) {
159     SILC_LOG_DEBUG(("Encrypt failed"));
160     goto err;
161   }
162   SILC_LOG_DEBUG(("Encrypt is successful"));
163   silc_cipher_set_iv(cipher, iv3);
164   assert(silc_cipher_decrypt(cipher, dst, pdst, p3_len, NULL));
165   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p3_len);
166   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p3, p3_len);
167   if (memcmp(pdst, p3, p3_len)) {
168     SILC_LOG_DEBUG(("Decrypt failed"));
169     goto err;
170   }
171   SILC_LOG_DEBUG(("Decrypt is successful"));
172
173
174   /* Fourth test vector */
175   SILC_LOG_DEBUG(("Fourth test vector"));
176   memset(dst, 0, sizeof(dst));
177   memset(pdst, 0, sizeof(pdst));
178   silc_cipher_set_iv(cipher, iv4);
179   assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE));
180   assert(silc_cipher_encrypt(cipher, p4, dst, p4_len, NULL));
181   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
182                  silc_cipher_get_block_len(cipher),
183                  silc_cipher_get_key_len(cipher),
184                  silc_cipher_get_name(cipher)));
185   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p4, p4_len);
186   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len);
187   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len);
188   if (memcmp(dst, c4, p4_len)) {
189     SILC_LOG_DEBUG(("Encrypt failed"));
190     goto err;
191   }
192   SILC_LOG_DEBUG(("Encrypt is successful"));
193   silc_cipher_set_iv(cipher, iv4);
194   assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL));
195   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len);
196   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len);
197   if (memcmp(pdst, p4, p4_len)) {
198     SILC_LOG_DEBUG(("Decrypt failed"));
199     goto err;
200   }
201   SILC_LOG_DEBUG(("Decrypt is successful"));
202   silc_cipher_free(cipher);
203
204   SILC_LOG_DEBUG(("Allocating aes-256-ctr cipher"));
205   if (!silc_cipher_alloc("aes-256-ctr", &cipher)) {
206     SILC_LOG_DEBUG(("Allocating aes-256-ctr cipher failed"));
207     goto err;
208   }
209   if (!silc_cipher_alloc("aes-256-ctr", &cipher2)) {
210     SILC_LOG_DEBUG(("Allocating aes-256-ctr cipher failed"));
211     goto err;
212   }
213
214   /* Fifth test vector */
215   SILC_LOG_DEBUG(("Fifth test vector"));
216   memset(dst, 0, sizeof(dst));
217   memset(pdst, 0, sizeof(pdst));
218   silc_cipher_set_iv(cipher, iv5);
219   assert(silc_cipher_set_key(cipher, key5, key5_len, TRUE));
220   assert(silc_cipher_set_key(cipher2, key5, key5_len, FALSE));
221   assert(silc_cipher_encrypt(cipher, p5, dst, p5_len, NULL));
222   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
223                  silc_cipher_get_block_len(cipher),
224                  silc_cipher_get_key_len(cipher),
225                  silc_cipher_get_name(cipher)));
226   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p5, p5_len);
227   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p5_len);
228   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c5, p5_len);
229   if (memcmp(dst, c5, p5_len)) {
230     SILC_LOG_DEBUG(("Encrypt failed"));
231     goto err;
232   }
233   SILC_LOG_DEBUG(("Encrypt is successful"));
234   silc_cipher_set_iv(cipher2, iv5);
235   assert(silc_cipher_decrypt(cipher2, dst, pdst, p5_len, NULL));
236   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p5_len);
237   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p5, p5_len);
238   if (memcmp(pdst, p5, p5_len)) {
239     SILC_LOG_DEBUG(("Decrypt failed"));
240     goto err;
241   }
242   SILC_LOG_DEBUG(("Decrypt is successful"));
243   silc_cipher_free(cipher2);
244
245   success = TRUE;
246
247  err:
248   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
249   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
250
251   silc_cipher_free(cipher);
252   silc_cipher_unregister_all();
253   return success;
254 }