Added SILC Thread Queue API
[silc.git] / lib / silccrypt / tests / test_cast5.c
1 #include "silc.h"
2
3 /* First test vector, 8 bytes plaintext, 128 bits key */
4 const unsigned char key1[] = "\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9a";
5 int key1_len = 16 * 8;
6 const unsigned char iv1[] = "\xef\xcd\xab\x89\x67\x45\x23\x01";
7 const unsigned char p1[] = "\x01\x23\x45\x67\x89\xab\xcd\xef";
8 int p1_len = 8;
9 const unsigned char c1[] = "\x84\x00\x0a\x34\x1b\xf9\x1f\xb3";
10
11 /* Second test vector, 32 bytes plaintext, 128 bits key */
12 const unsigned char key2[] = "\xc2\x86\x69\x6d\x88\x7c\x9a\xa0\x61\x1b\xbb\x3e\x20\x25\xa4\x5a";
13 int key2_len = 16 * 8;
14 const unsigned char iv2[] = "\x56\x2e\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58";
15 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";
16 int p2_len = 32;
17 const unsigned char c2[] = "\x23\xA5\xBD\xC5\x2A\x59\xD4\x06\x61\xBF\xF3\x47\x76\x4B\xC8\x41\x1C\x65\xE3\xE2\x1A\x1F\x42\x4B\xBD\x22\xDF\xEC\xD9\xC9\x52\x43";
18
19 /* CTR */
20
21 /* 32 bytes plaintext, 128 bits key */
22 const unsigned char key4[] = "\x7E\x24\x06\x78\x17\xFA\xE0\xD7\x43\xD6\xCE\x1F\x32\x53\x91\x63";
23 int key4_len = 16 * 8;
24 const unsigned char iv4[] = "\x00\x6C\xB6\xDB\xC0\x54\x3B\x59\xDA\x48\xD9\x0B\x00\x00\x00\x00";
25 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";
26 int p4_len = 32;
27 const unsigned char c4[] = "\xAA\x16\xEC\xEA\x77\x73\x12\xC1\x14\x0F\x10\x65\x06\x72\xF8\xE5\x89\xCF\x26\x2C\x84\xC6\x3A\x93\xEE\xC0\xB2\xBA\x92\xAD\x19\xCA";
28
29 /* CFB */
30
31 /* 36 bytes plaintext, 128 bits key */
32 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";
33 int key6_len = 16 * 8;
34 const unsigned char iv6[] = "\x00\x1C\xC5\xB7\x51\xA5\x1D\x70\xA1\xC1\x11\x48\x00\x00\x00\x00";
35 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";
36 int p6_len = 36;
37 const unsigned char c6[] = "\xE2\x30\xB2\x57\x12\x31\x04\x54\x37\xEC\xF1\x43\xC1\x3F\x20\x53\xA4\xDD\x99\x29\x3C\xCA\x4A\x7C\xCC\xEA\x12\xBF\xA8\x05\x37\x89\xA9\xB3\x3B\x15";
38
39 int main(int argc, char **argv)
40 {
41   SilcBool success = FALSE;
42   SilcCipher cipher, cipher2;
43   unsigned char dst[256], pdst[256];
44   int i;
45
46   if (argc > 1 && !strcmp(argv[1], "-d")) {
47     silc_log_debug(TRUE);
48     silc_log_debug_hexdump(TRUE);
49     silc_log_set_debug_string("*crypt*,*cast*,*cipher*");
50   }
51
52   SILC_LOG_DEBUG(("Registering builtin hash functions"));
53   silc_cipher_register_default();
54
55   SILC_LOG_DEBUG(("Allocating cast5-CBC cipher"));
56   if (!silc_cipher_alloc("cast5-128-cbc", &cipher)) {
57     SILC_LOG_DEBUG(("Allocating cas5-CBC cipher failed"));
58     goto err;
59   }
60   if (!silc_cipher_alloc("cast5-128-cbc", &cipher2)) {
61     SILC_LOG_DEBUG(("Allocating cast5-CBC cipher failed"));
62     goto err;
63   }
64
65   /* First test vector */
66   SILC_LOG_DEBUG(("First test vector"));
67   memset(dst, 0, sizeof(dst));
68   memset(pdst, 0, sizeof(pdst));
69   silc_cipher_set_iv(cipher, iv1);
70   assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE));
71   assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE));
72   assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL));
73   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
74                  silc_cipher_get_block_len(cipher),
75                  silc_cipher_get_key_len(cipher),
76                  silc_cipher_get_name(cipher)));
77   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len);
78   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len);
79   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len);
80   if (memcmp(dst, c1, p1_len)) {
81     SILC_LOG_DEBUG(("Encrypt failed"));
82     goto err;
83   }
84   SILC_LOG_DEBUG(("Encrypt is successful"));
85   silc_cipher_set_iv(cipher2, iv1);
86   assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL));
87   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len);
88   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len);
89   if (memcmp(pdst, p1, p1_len)) {
90     SILC_LOG_DEBUG(("Decrypt failed"));
91     goto err;
92   }
93   SILC_LOG_DEBUG(("Decrypt is successful"));
94
95
96   /* Second test vector */
97   SILC_LOG_DEBUG(("Second test vector"));
98   memset(dst, 0, sizeof(dst));
99   memset(pdst, 0, sizeof(pdst));
100   silc_cipher_set_iv(cipher, iv2);
101   assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE));
102   assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE));
103   assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL));
104   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
105                  silc_cipher_get_block_len(cipher),
106                  silc_cipher_get_key_len(cipher),
107                  silc_cipher_get_name(cipher)));
108   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len);
109   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len);
110   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len);
111   if (memcmp(dst, c2, p2_len)) {
112     SILC_LOG_DEBUG(("Encrypt failed"));
113     goto err;
114   }
115   SILC_LOG_DEBUG(("Encrypt is successful"));
116   silc_cipher_set_iv(cipher2, iv2);
117   assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL));
118   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len);
119   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len);
120   if (memcmp(pdst, p2, p2_len)) {
121     SILC_LOG_DEBUG(("Decrypt failed"));
122     goto err;
123   }
124   SILC_LOG_DEBUG(("Decrypt is successful"));
125   silc_cipher_free(cipher);
126   silc_cipher_free(cipher2);
127
128   SILC_LOG_DEBUG(("Allocating cast5-128-ctr cipher"));
129   if (!silc_cipher_alloc("cast5-128-ctr", &cipher)) {
130     SILC_LOG_DEBUG(("Allocating cast5-128-ctr cipher failed"));
131     goto err;
132   }
133
134   /* Fourth test vector */
135   SILC_LOG_DEBUG(("Fourth test vector"));
136   memset(dst, 0, sizeof(dst));
137   memset(pdst, 0, sizeof(pdst));
138   silc_cipher_set_iv(cipher, iv4);
139   assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE));
140   assert(silc_cipher_encrypt(cipher, p4, dst, p4_len, NULL));
141   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
142                  silc_cipher_get_block_len(cipher),
143                  silc_cipher_get_key_len(cipher),
144                  silc_cipher_get_name(cipher)));
145   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p4, p4_len);
146   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len);
147   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len);
148   if (memcmp(dst, c4, p4_len)) {
149     SILC_LOG_DEBUG(("Encrypt failed"));
150     goto err;
151   }
152   SILC_LOG_DEBUG(("Encrypt is successful"));
153   silc_cipher_set_iv(cipher, iv4);
154   assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL));
155   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len);
156   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len);
157   if (memcmp(pdst, p4, p4_len)) {
158     SILC_LOG_DEBUG(("Decrypt failed"));
159     goto err;
160   }
161   SILC_LOG_DEBUG(("Decrypt is successful"));
162   silc_cipher_free(cipher);
163
164
165   SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher"));
166   if (!silc_cipher_alloc("cast5-128-cfb", &cipher)) {
167     SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher failed"));
168     goto err;
169   }
170   if (!silc_cipher_alloc("cast5-128-cfb", &cipher2)) {
171     SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher failed"));
172     goto err;
173   }
174
175   SILC_LOG_DEBUG(("CFB test vector"));
176   memset(dst, 0, sizeof(dst));
177   memset(pdst, 0, sizeof(pdst));
178   silc_cipher_set_iv(cipher, iv6);
179   assert(silc_cipher_set_key(cipher, key6, key6_len, TRUE));
180   assert(silc_cipher_set_key(cipher2, key6, key6_len, FALSE));
181   assert(silc_cipher_encrypt(cipher, p6, dst, p6_len, NULL));
182   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
183                  silc_cipher_get_block_len(cipher),
184                  silc_cipher_get_key_len(cipher),
185                  silc_cipher_get_name(cipher)));
186   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p6, p6_len);
187   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p6_len);
188   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c6, p6_len);
189   if (memcmp(dst, c6, p6_len)) {
190     SILC_LOG_DEBUG(("Encrypt failed"));
191     goto err;
192   }
193   SILC_LOG_DEBUG(("Encrypt is successful"));
194   silc_cipher_set_iv(cipher2, iv6);
195   assert(silc_cipher_decrypt(cipher2, dst, pdst, p6_len, NULL));
196   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p6_len);
197   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p6, p6_len);
198   if (memcmp(pdst, p6, p6_len)) {
199     SILC_LOG_DEBUG(("Decrypt failed"));
200     goto err;
201   }
202   SILC_LOG_DEBUG(("Decrypt is successful"));
203   silc_cipher_free(cipher2);
204
205   success = TRUE;
206
207  err:
208   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
209   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
210
211   silc_cipher_unregister_all();
212   return success;
213 }