Major restructuring of the internals of SILC Cipher API
[crypto.git] / lib / silccrypt / tests / test_twofish.c
1
2 #include "silccrypto.h"
3
4 /* CBC */
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[] = "\x6C\x98\x5B\xF3\x71\x63\x4D\x57\x01\x95\x8D\x2D\x45\x01\xAA\x27";
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[] = "\x58\x31\x6E\xAB\x22\xF1\x13\x00\x03\xA6\x21\x7B\xAF\x9F\xF5\x4D\x60\x0E\xC3\x3F\xF8\x0B\xF9\x4D\x16\x47\x38\x64\x04\xFD\xFE\xD3";
21
22 /* CTR */
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[] = "\x88\x56\x65\x38\x6F\xEB\x2C\x97\xCD\x35\xB7\xB5\x7B\x3C\xC7\x3E";
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[] = "\x00\xAD\x58\xD3\x76\x12\x7D\xD4\x09\xDC\x38\x4D\x3E\x19\x3F\xC8\x58\x8E\x8A\xF6\xAD\x97\x39\x18\x16\xA6\x11\x06\xC4\x86\x5E\xB2";
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[] = "\x71\x2F\x9C\xE1\x4F\xD5\x9E\xBF\x6A\x1E\x7D\x76\x0C\xBA\x70\xE9\x5E\xCE\x27\xAD\x5B\xE1\x38\xDB\x99\xEF\x46\x78\x4D\xCF\x99\x24\x63\x0E\x84\x58";
47
48 /* CFB */
49
50 /* 36 bytes plaintext, 256 bits key */
51 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";
52 int key6_len = 32 * 8;
53 const unsigned char iv6[] = "\x00\x1C\xC5\xB7\x51\xA5\x1D\x70\xA1\xC1\x11\x48\x00\x00\x00\x00";
54 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";
55 int p6_len = 36;
56 const unsigned char c6[] = "\x2C\x0E\x4D\xEF\xE4\x71\xEB\x2A\x4B\x03\x21\x96\xD1\xCD\x73\xD7\x3A\xA9\xEB\x08\x87\xB2\xAB\x66\x28\x3A\xC2\x99\xB7\x13\x8C\x92\xEA\xD6\xFD\x41";
57
58 int main(int argc, char **argv)
59 {
60   SilcBool success = FALSE;
61   SilcCipher cipher, cipher2;
62   unsigned char dst[256], pdst[256];
63   int i;
64
65   if (argc > 1 && !strcmp(argv[1], "-d")) {
66     silc_log_debug(TRUE);
67     silc_log_debug_hexdump(TRUE);
68     silc_log_set_debug_string("*crypt*,*twofish*,*cipher*");
69   }
70
71   SILC_LOG_DEBUG(("Registering builtin hash functions"));
72   silc_cipher_register_default();
73
74   SILC_LOG_DEBUG(("Allocating twofish-CBC cipher"));
75   if (!silc_cipher_alloc("twofish-128-cbc", &cipher)) {
76     SILC_LOG_DEBUG(("Allocating twofish-CBC cipher failed"));
77     goto err;
78   }
79   if (!silc_cipher_alloc("twofish-128-cbc", &cipher2)) {
80     SILC_LOG_DEBUG(("Allocating twofish-CBC cipher failed"));
81     goto err;
82   }
83
84   /* First test vector */
85   SILC_LOG_DEBUG(("First test vector"));
86   memset(dst, 0, sizeof(dst));
87   memset(pdst, 0, sizeof(pdst));
88   silc_cipher_set_iv(cipher, iv1);
89   assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE));
90   assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE));
91   assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL));
92   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
93                  silc_cipher_get_block_len(cipher),
94                  silc_cipher_get_key_len(cipher),
95                  silc_cipher_get_name(cipher)));
96   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len);
97   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len);
98   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len);
99   if (memcmp(dst, c1, p1_len)) {
100     SILC_LOG_DEBUG(("Encrypt failed"));
101     goto err;
102   }
103   SILC_LOG_DEBUG(("Encrypt is successful"));
104   silc_cipher_set_iv(cipher2, iv1);
105   assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL));
106   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len);
107   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len);
108   if (memcmp(pdst, p1, p1_len)) {
109     SILC_LOG_DEBUG(("Decrypt failed"));
110     goto err;
111   }
112   SILC_LOG_DEBUG(("Decrypt is successful"));
113
114
115   /* Second test vector */
116   SILC_LOG_DEBUG(("Second test vector"));
117   memset(dst, 0, sizeof(dst));
118   memset(pdst, 0, sizeof(pdst));
119   silc_cipher_set_iv(cipher, iv2);
120   assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE));
121   assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE));
122   assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL));
123   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
124                  silc_cipher_get_block_len(cipher),
125                  silc_cipher_get_key_len(cipher),
126                  silc_cipher_get_name(cipher)));
127   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len);
128   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len);
129   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len);
130   if (memcmp(dst, c2, p2_len)) {
131     SILC_LOG_DEBUG(("Encrypt failed"));
132     goto err;
133   }
134   SILC_LOG_DEBUG(("Encrypt is successful"));
135   silc_cipher_set_iv(cipher2, iv2);
136   assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL));
137   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len);
138   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len);
139   if (memcmp(pdst, p2, p2_len)) {
140     SILC_LOG_DEBUG(("Decrypt failed"));
141     goto err;
142   }
143   SILC_LOG_DEBUG(("Decrypt is successful"));
144   silc_cipher_free(cipher);
145   silc_cipher_free(cipher2);
146
147
148   SILC_LOG_DEBUG(("Allocating twofish-128-ctr cipher"));
149   if (!silc_cipher_alloc("twofish-128-ctr", &cipher)) {
150     SILC_LOG_DEBUG(("Allocating twofish-128-ctr cipher failed"));
151     goto err;
152   }
153
154   /* Third test vector */
155   SILC_LOG_DEBUG(("CTR test vector"));
156   memset(dst, 0, sizeof(dst));
157   memset(pdst, 0, sizeof(pdst));
158   silc_cipher_set_iv(cipher, iv3);
159   assert(silc_cipher_set_key(cipher, key3, key3_len, TRUE));
160   assert(silc_cipher_encrypt(cipher, p3, dst, p3_len, NULL));
161   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
162                  silc_cipher_get_block_len(cipher),
163                  silc_cipher_get_key_len(cipher),
164                  silc_cipher_get_name(cipher)));
165   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p3, p3_len);
166   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p3_len);
167   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c3, p3_len);
168   if (memcmp(dst, c3, p3_len)) {
169     SILC_LOG_DEBUG(("Encrypt failed"));
170     goto err;
171   }
172   SILC_LOG_DEBUG(("Encrypt is successful"));
173   silc_cipher_set_iv(cipher, iv3);
174   assert(silc_cipher_decrypt(cipher, dst, pdst, p3_len, NULL));
175   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p3_len);
176   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p3, p3_len);
177   if (memcmp(pdst, p3, p3_len)) {
178     SILC_LOG_DEBUG(("Decrypt failed"));
179     goto err;
180   }
181   SILC_LOG_DEBUG(("Decrypt is successful"));
182
183
184   /* Fourth test vector */
185   SILC_LOG_DEBUG(("CTR test vector"));
186   memset(dst, 0, sizeof(dst));
187   memset(pdst, 0, sizeof(pdst));
188   silc_cipher_set_iv(cipher, iv4);
189   assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE));
190   assert(silc_cipher_encrypt(cipher, p4, dst, p4_len, NULL));
191   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
192                  silc_cipher_get_block_len(cipher),
193                  silc_cipher_get_key_len(cipher),
194                  silc_cipher_get_name(cipher)));
195   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p4, p4_len);
196   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len);
197   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len);
198   if (memcmp(dst, c4, p4_len)) {
199     SILC_LOG_DEBUG(("Encrypt failed"));
200     goto err;
201   }
202   SILC_LOG_DEBUG(("Encrypt is successful"));
203   silc_cipher_set_iv(cipher, iv4);
204   assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL));
205   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len);
206   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len);
207   if (memcmp(pdst, p4, p4_len)) {
208     SILC_LOG_DEBUG(("Decrypt failed"));
209     goto err;
210   }
211   SILC_LOG_DEBUG(("Decrypt is successful"));
212   silc_cipher_free(cipher);
213
214   SILC_LOG_DEBUG(("Allocating twofish-256-ctr cipher"));
215   if (!silc_cipher_alloc("twofish-256-ctr", &cipher)) {
216     SILC_LOG_DEBUG(("Allocating twofish-256-ctr cipher failed"));
217     goto err;
218   }
219   if (!silc_cipher_alloc("twofish-256-ctr", &cipher2)) {
220     SILC_LOG_DEBUG(("Allocating twofish-256-ctr cipher failed"));
221     goto err;
222   }
223
224   /* Fifth test vector */
225   SILC_LOG_DEBUG(("CTR test vector"));
226   memset(dst, 0, sizeof(dst));
227   memset(pdst, 0, sizeof(pdst));
228   silc_cipher_set_iv(cipher, iv5);
229   assert(silc_cipher_set_key(cipher, key5, key5_len, TRUE));
230   assert(silc_cipher_set_key(cipher2, key5, key5_len, FALSE));
231   assert(silc_cipher_encrypt(cipher, p5, dst, p5_len, NULL));
232   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
233                  silc_cipher_get_block_len(cipher),
234                  silc_cipher_get_key_len(cipher),
235                  silc_cipher_get_name(cipher)));
236   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p5, p5_len);
237   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p5_len);
238   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c5, p5_len);
239   if (memcmp(dst, c5, p5_len)) {
240     SILC_LOG_DEBUG(("Encrypt failed"));
241     goto err;
242   }
243   SILC_LOG_DEBUG(("Encrypt is successful"));
244   silc_cipher_set_iv(cipher2, iv5);
245   assert(silc_cipher_decrypt(cipher2, dst, pdst, p5_len, NULL));
246   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p5_len);
247   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p5, p5_len);
248   if (memcmp(pdst, p5, p5_len)) {
249     SILC_LOG_DEBUG(("Decrypt failed"));
250     goto err;
251   }
252   SILC_LOG_DEBUG(("Decrypt is successful"));
253   silc_cipher_free(cipher2);
254
255
256   SILC_LOG_DEBUG(("Allocating twofish-256-cfb cipher"));
257   if (!silc_cipher_alloc("twofish-256-cfb", &cipher)) {
258     SILC_LOG_DEBUG(("Allocating twofish-256-cfb cipher failed"));
259     goto err;
260   }
261   if (!silc_cipher_alloc("twofish-256-cfb", &cipher2)) {
262     SILC_LOG_DEBUG(("Allocating twofish-256-cfb cipher failed"));
263     goto err;
264   }
265
266   /* Fifth test vector */
267   SILC_LOG_DEBUG(("CFB test vector"));
268   memset(dst, 0, sizeof(dst));
269   memset(pdst, 0, sizeof(pdst));
270   silc_cipher_set_iv(cipher, iv6);
271   assert(silc_cipher_set_key(cipher, key6, key6_len, TRUE));
272   assert(silc_cipher_set_key(cipher2, key6, key6_len, FALSE));
273   assert(silc_cipher_encrypt(cipher, p6, dst, p6_len, NULL));
274   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
275                  silc_cipher_get_block_len(cipher),
276                  silc_cipher_get_key_len(cipher),
277                  silc_cipher_get_name(cipher)));
278   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p6, p6_len);
279   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p6_len);
280   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c6, p6_len);
281   if (memcmp(dst, c6, p6_len)) {
282     SILC_LOG_DEBUG(("Encrypt failed"));
283     goto err;
284   }
285   SILC_LOG_DEBUG(("Encrypt is successful"));
286   silc_cipher_set_iv(cipher2, iv6);
287   assert(silc_cipher_decrypt(cipher2, dst, pdst, p6_len, NULL));
288   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p6_len);
289   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p5, p6_len);
290   if (memcmp(pdst, p6, p6_len)) {
291     SILC_LOG_DEBUG(("Decrypt failed"));
292     goto err;
293   }
294   SILC_LOG_DEBUG(("Decrypt is successful"));
295   silc_cipher_free(cipher2);
296
297   success = TRUE;
298
299  err:
300   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
301   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
302
303   silc_cipher_free(cipher);
304   silc_cipher_unregister_all();
305   return success;
306 }