Added SILC Thread Queue API
[silc.git] / lib / silccrypt / tests / test_des.c
1 #include "silc.h"
2
3 /* Test vectors from */
4
5 /* First test vector, 16 bytes plaintext, 56, 168 bits key */
6 const unsigned char key1[] = "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06";
7 int key1_len = 8 * 8;
8 const unsigned char iv1[] = "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41";
9 const unsigned char p1[] = "Single block msg";
10 int p1_len = 16;
11 const unsigned char c1[] = "\xD1\xB4\x0D\xB2\x53\x55\x82\xC4\x6A\xF4\xFC\xC4\x86\x8C\xCE\xCA";
12
13 /* Second test vector, 32 bytes plaintext, 56, 168 bits key */
14 const unsigned char key2[] = "\xc2\x86\x69\x6d\x88\x7c\x9a\xa0\x61\x1b\xbb\x3e\x20\x25\xa4\x5a";
15 int key2_len = 8 * 8;
16 const unsigned char iv2[] = "\x56\x2e\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58";
17 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";
18 int p2_len = 32;
19 const unsigned char c2[] = "\xE8\xDE\x56\x51\x45\xD0\x68\xD8\x4D\x6F\x2F\x0C\xAF\xB5\xBE\xD7\xD1\x6C\xF6\x7B\x60\x24\xB7\x9D\x6E\xB7\x4D\xF7\x5B\xF1\x82\x0E";
20
21 /* CTR test vectors from RFC3686. */
22
23 /* 32 bytes plaintext, 56, 168 bits key */
24 const unsigned char key4[] = "\x7E\x24\x06\x78\x17\xFA\xE0\xD7\x43\xD6\xCE\x1F\x32\x53\x91\x63";
25 int key4_len = 8 * 8;
26 const unsigned char iv4[] = "\x00\x6C\xB6\xDB\xC0\x54\x3B\x59\xDA\x48\xD9\x0B\x00\x00\x00\x00";
27 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";
28 int p4_len = 32;
29 const unsigned char c4[] = "\xF0\xEC\xDF\xE4\x77\x2D\x9D\x94\x7E\xB0\x49\x06\x08\x8E\x48\x2A\x84\xF3\xF6\x01\x43\x8D\x9D\x4B\x78\x59\x5B\xAE\x3A\x8D\x59\x5D";
30
31 /* 36 bytes plaintext, 56, 168 bits key */
32 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";
33 int key5_len = 8 * 8;
34 const unsigned char iv5[] = "\x00\x1C\xC5\xB7\x51\xA5\x1D\x70\xA1\xC1\x11\x48\x00\x00\x00\x00";
35 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";
36 int p5_len = 36;
37 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";
38
39 /* CFB */
40
41 /* 36 bytes plaintext, 56, 168 bits key */
42 const unsigned char key6[] = "\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";
43 int key6_len = 32 * 8;
44 const unsigned char iv6[] = "\x00\x1C\xC5\xB7\x51\xA5\x1D\x70\xA1\xC1\x11\x48\x00\x00\x00\x00";
45 const unsigned char p6[] = "\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";
46 int p6_len = 36;
47 const unsigned char c6[] = "\x7A\xD8\x13\x76\xB8\x0B\xEC\xC7\x0C\xA8\x5F\x7C\xEB\xB9\xD9\xCD\x39\x7E\x89\x94\xAA\xD2\x9C\xB6\x92\xF0\xD0\xEC\x7F\x80\x5B\x74\xBD\x1B\xFE\xB0";
48
49 int main(int argc, char **argv)
50 {
51   SilcBool success = FALSE;
52   SilcCipher cipher, cipher2;
53   unsigned char dst[256], pdst[256];
54   int i;
55
56   if (argc > 1 && !strcmp(argv[1], "-d")) {
57     silc_log_debug(TRUE);
58     silc_log_debug_hexdump(TRUE);
59     silc_log_set_debug_string("*crypt*,*des*,*cipher*");
60   }
61
62   SILC_LOG_DEBUG(("Registering builtin hash functions"));
63   silc_cipher_register_default();
64
65   SILC_LOG_DEBUG(("Allocating des-CBC cipher"));
66   if (!silc_cipher_alloc("des-56-cbc", &cipher)) {
67     SILC_LOG_DEBUG(("Allocating cas5-CBC cipher failed"));
68     goto err;
69   }
70   if (!silc_cipher_alloc("des-56-cbc", &cipher2)) {
71     SILC_LOG_DEBUG(("Allocating des-CBC cipher failed"));
72     goto err;
73   }
74
75   /* First test vector */
76   SILC_LOG_DEBUG(("First test vector"));
77   memset(dst, 0, sizeof(dst));
78   memset(pdst, 0, sizeof(pdst));
79   silc_cipher_set_iv(cipher, iv1);
80   assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE));
81   assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE));
82   assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL));
83   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
84                  silc_cipher_get_block_len(cipher),
85                  silc_cipher_get_key_len(cipher),
86                  silc_cipher_get_name(cipher)));
87   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len);
88   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len);
89   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len);
90   if (memcmp(dst, c1, p1_len)) {
91     SILC_LOG_DEBUG(("Encrypt failed"));
92     goto err;
93   }
94   SILC_LOG_DEBUG(("Encrypt is successful"));
95   silc_cipher_set_iv(cipher2, iv1);
96   assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL));
97   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len);
98   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len);
99   if (memcmp(pdst, p1, p1_len)) {
100     SILC_LOG_DEBUG(("Decrypt failed"));
101     goto err;
102   }
103   SILC_LOG_DEBUG(("Decrypt is successful"));
104
105
106   /* Second test vector */
107   SILC_LOG_DEBUG(("Second test vector"));
108   memset(dst, 0, sizeof(dst));
109   memset(pdst, 0, sizeof(pdst));
110   silc_cipher_set_iv(cipher, iv2);
111   assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE));
112   assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE));
113   assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL));
114   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
115                  silc_cipher_get_block_len(cipher),
116                  silc_cipher_get_key_len(cipher),
117                  silc_cipher_get_name(cipher)));
118   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len);
119   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len);
120   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len);
121   if (memcmp(dst, c2, p2_len)) {
122     SILC_LOG_DEBUG(("Encrypt failed"));
123     goto err;
124   }
125   SILC_LOG_DEBUG(("Encrypt is successful"));
126   silc_cipher_set_iv(cipher2, iv2);
127   assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL));
128   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len);
129   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len);
130   if (memcmp(pdst, p2, p2_len)) {
131     SILC_LOG_DEBUG(("Decrypt failed"));
132     goto err;
133   }
134   SILC_LOG_DEBUG(("Decrypt is successful"));
135   silc_cipher_free(cipher);
136   silc_cipher_free(cipher2);
137
138   SILC_LOG_DEBUG(("Allocating des-56-ctr cipher"));
139   if (!silc_cipher_alloc("des-56-ctr", &cipher)) {
140     SILC_LOG_DEBUG(("Allocating des-56-ctr cipher failed"));
141     goto err;
142   }
143
144   /* Fourth test vector */
145   SILC_LOG_DEBUG(("Fourth test vector"));
146   memset(dst, 0, sizeof(dst));
147   memset(pdst, 0, sizeof(pdst));
148   silc_cipher_set_iv(cipher, iv4);
149   assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE));
150   assert(silc_cipher_encrypt(cipher, p4, dst, p4_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 *)p4, p4_len);
156   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len);
157   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len);
158   if (memcmp(dst, c4, p4_len)) {
159     SILC_LOG_DEBUG(("Encrypt failed"));
160     goto err;
161   }
162   SILC_LOG_DEBUG(("Encrypt is successful"));
163   silc_cipher_set_iv(cipher, iv4);
164   assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL));
165   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len);
166   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len);
167   if (memcmp(pdst, p4, p4_len)) {
168     SILC_LOG_DEBUG(("Decrypt failed"));
169     goto err;
170   }
171   SILC_LOG_DEBUG(("Decrypt is successful"));
172   silc_cipher_free(cipher);
173
174
175   SILC_LOG_DEBUG(("Allocating des-56-cfb cipher"));
176   if (!silc_cipher_alloc("des-56-cfb", &cipher)) {
177     SILC_LOG_DEBUG(("Allocating des-56-cfb cipher failed"));
178     goto err;
179   }
180   if (!silc_cipher_alloc("des-56-cfb", &cipher2)) {
181     SILC_LOG_DEBUG(("Allocating des-56-cfb cipher failed"));
182     goto err;
183   }
184
185   SILC_LOG_DEBUG(("CFB test vector"));
186   memset(dst, 0, sizeof(dst));
187   memset(pdst, 0, sizeof(pdst));
188   silc_cipher_set_iv(cipher, iv6);
189   assert(silc_cipher_set_key(cipher, key6, key6_len, TRUE));
190   assert(silc_cipher_set_key(cipher2, key6, key6_len, FALSE));
191   assert(silc_cipher_encrypt(cipher, p6, dst, p6_len, NULL));
192   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
193                  silc_cipher_get_block_len(cipher),
194                  silc_cipher_get_key_len(cipher),
195                  silc_cipher_get_name(cipher)));
196   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p6, p6_len);
197   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p6_len);
198   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c6, p6_len);
199   if (memcmp(dst, c6, p6_len)) {
200     SILC_LOG_DEBUG(("Encrypt failed"));
201     goto err;
202   }
203   SILC_LOG_DEBUG(("Encrypt is successful"));
204   silc_cipher_set_iv(cipher2, iv6);
205   assert(silc_cipher_decrypt(cipher2, dst, pdst, p6_len, NULL));
206   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p6_len);
207   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p6, p6_len);
208   if (memcmp(pdst, p6, p6_len)) {
209     SILC_LOG_DEBUG(("Decrypt failed"));
210     goto err;
211   }
212   SILC_LOG_DEBUG(("Decrypt is successful"));
213   silc_cipher_free(cipher2);
214
215
216   /* 3des */
217
218   SILC_LOG_DEBUG(("Allocating 3des-CBC cipher"));
219   if (!silc_cipher_alloc("3des-168-cbc", &cipher)) {
220     SILC_LOG_DEBUG(("Allocating cas5-CBC cipher failed"));
221     goto err;
222   }
223   if (!silc_cipher_alloc("3des-168-cbc", &cipher2)) {
224     SILC_LOG_DEBUG(("Allocating 3des-CBC cipher failed"));
225     goto err;
226   }
227
228   /* First test vector */
229   SILC_LOG_DEBUG(("First test vector"));
230   memset(dst, 0, sizeof(dst));
231   memset(pdst, 0, sizeof(pdst));
232   silc_cipher_set_iv(cipher, iv1);
233   assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE));
234   assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE));
235   assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL));
236   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
237                  silc_cipher_get_block_len(cipher),
238                  silc_cipher_get_key_len(cipher),
239                  silc_cipher_get_name(cipher)));
240   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len);
241   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len);
242 #if 0
243   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len);
244   if (memcmp(dst, c1, p1_len)) {
245     SILC_LOG_DEBUG(("Encrypt failed"));
246     goto err;
247   }
248 #endif
249   SILC_LOG_DEBUG(("Encrypt is successful"));
250   silc_cipher_set_iv(cipher2, iv1);
251   assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL));
252   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len);
253   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len);
254   if (memcmp(pdst, p1, p1_len)) {
255     SILC_LOG_DEBUG(("Decrypt failed"));
256     goto err;
257   }
258   SILC_LOG_DEBUG(("Decrypt is successful"));
259
260
261   /* Second test vector */
262   SILC_LOG_DEBUG(("Second test vector"));
263   memset(dst, 0, sizeof(dst));
264   memset(pdst, 0, sizeof(pdst));
265   silc_cipher_set_iv(cipher, iv2);
266   assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE));
267   assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE));
268   assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL));
269   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
270                  silc_cipher_get_block_len(cipher),
271                  silc_cipher_get_key_len(cipher),
272                  silc_cipher_get_name(cipher)));
273   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len);
274   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len);
275 #if 0
276   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len);
277   if (memcmp(dst, c2, p2_len)) {
278     SILC_LOG_DEBUG(("Encrypt failed"));
279     goto err;
280   }
281 #endif
282   SILC_LOG_DEBUG(("Encrypt is successful"));
283   silc_cipher_set_iv(cipher2, iv2);
284   assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL));
285   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len);
286   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len);
287   if (memcmp(pdst, p2, p2_len)) {
288     SILC_LOG_DEBUG(("Decrypt failed"));
289     goto err;
290   }
291   SILC_LOG_DEBUG(("Decrypt is successful"));
292   silc_cipher_free(cipher);
293   silc_cipher_free(cipher2);
294
295   SILC_LOG_DEBUG(("Allocating 3des-168-ctr cipher"));
296   if (!silc_cipher_alloc("3des-168-ctr", &cipher)) {
297     SILC_LOG_DEBUG(("Allocating 3des-168-ctr cipher failed"));
298     goto err;
299   }
300
301   /* Fourth test vector */
302   SILC_LOG_DEBUG(("Fourth test vector"));
303   memset(dst, 0, sizeof(dst));
304   memset(pdst, 0, sizeof(pdst));
305   silc_cipher_set_iv(cipher, iv4);
306   assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE));
307   assert(silc_cipher_encrypt(cipher, p4, dst, p4_len, NULL));
308   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
309                  silc_cipher_get_block_len(cipher),
310                  silc_cipher_get_key_len(cipher),
311                  silc_cipher_get_name(cipher)));
312   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p4, p4_len);
313   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len);
314 #if 0
315   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len);
316   if (memcmp(dst, c4, p4_len)) {
317     SILC_LOG_DEBUG(("Encrypt failed"));
318     goto err;
319   }
320 #endif
321   SILC_LOG_DEBUG(("Encrypt is successful"));
322   silc_cipher_set_iv(cipher, iv4);
323   assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL));
324   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len);
325   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len);
326   if (memcmp(pdst, p4, p4_len)) {
327     SILC_LOG_DEBUG(("Decrypt failed"));
328     goto err;
329   }
330   SILC_LOG_DEBUG(("Decrypt is successful"));
331   silc_cipher_free(cipher);
332
333
334   SILC_LOG_DEBUG(("Allocating 3des-168-cfb cipher"));
335   if (!silc_cipher_alloc("3des-168-cfb", &cipher)) {
336     SILC_LOG_DEBUG(("Allocating 3des-168-cfb cipher failed"));
337     goto err;
338   }
339   if (!silc_cipher_alloc("3des-168-cfb", &cipher2)) {
340     SILC_LOG_DEBUG(("Allocating 3des-168-cfb cipher failed"));
341     goto err;
342   }
343
344   SILC_LOG_DEBUG(("CFB test vector"));
345   memset(dst, 0, sizeof(dst));
346   memset(pdst, 0, sizeof(pdst));
347   silc_cipher_set_iv(cipher, iv6);
348   assert(silc_cipher_set_key(cipher, key6, key6_len, TRUE));
349   assert(silc_cipher_set_key(cipher2, key6, key6_len, FALSE));
350   assert(silc_cipher_encrypt(cipher, p6, dst, p6_len, NULL));
351   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
352                  silc_cipher_get_block_len(cipher),
353                  silc_cipher_get_key_len(cipher),
354                  silc_cipher_get_name(cipher)));
355   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p6, p6_len);
356   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p6_len);
357 #if 0
358   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c6, p6_len);
359   if (memcmp(dst, c6, p6_len)) {
360     SILC_LOG_DEBUG(("Encrypt failed"));
361     goto err;
362   }
363 #endif
364   SILC_LOG_DEBUG(("Encrypt is successful"));
365   silc_cipher_set_iv(cipher2, iv6);
366   assert(silc_cipher_decrypt(cipher2, dst, pdst, p6_len, NULL));
367   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p6_len);
368   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p6, p6_len);
369   if (memcmp(pdst, p6, p6_len)) {
370     SILC_LOG_DEBUG(("Decrypt failed"));
371     goto err;
372   }
373   SILC_LOG_DEBUG(("Decrypt is successful"));
374   silc_cipher_free(cipher2);
375
376   success = TRUE;
377
378  err:
379   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
380   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
381
382   silc_cipher_unregister_all();
383   return success;
384 }