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";
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";
9 const unsigned char c1[] = "\x84\x00\x0a\x34\x1b\xf9\x1f\xb3";
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";
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";
19 /* CTR test vectors from RFC3686. */
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";
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";
29 int main(int argc, char **argv)
31 SilcBool success = FALSE;
32 SilcCipher cipher, cipher2;
33 unsigned char dst[256], pdst[256];
36 if (argc > 1 && !strcmp(argv[1], "-d")) {
38 silc_log_debug_hexdump(TRUE);
39 silc_log_set_debug_string("*crypt*,*cast*,*cipher*");
42 SILC_LOG_DEBUG(("Registering builtin hash functions"));
43 silc_cipher_register_default();
45 SILC_LOG_DEBUG(("Allocating cast5-CBC cipher"));
46 if (!silc_cipher_alloc("cast5-128-cbc", &cipher)) {
47 SILC_LOG_DEBUG(("Allocating cas5-CBC cipher failed"));
50 if (!silc_cipher_alloc("cast5-128-cbc", &cipher2)) {
51 SILC_LOG_DEBUG(("Allocating cast5-CBC cipher failed"));
55 /* First test vector */
56 SILC_LOG_DEBUG(("First test vector"));
57 memset(dst, 0, sizeof(dst));
58 memset(pdst, 0, sizeof(pdst));
59 silc_cipher_set_iv(cipher, iv1);
60 assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE));
61 assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE));
62 assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL));
63 SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
64 silc_cipher_get_block_len(cipher),
65 silc_cipher_get_key_len(cipher),
66 silc_cipher_get_name(cipher)));
67 SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len);
68 SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len);
69 SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len);
70 if (memcmp(dst, c1, p1_len)) {
71 SILC_LOG_DEBUG(("Encrypt failed"));
74 SILC_LOG_DEBUG(("Encrypt is successful"));
75 silc_cipher_set_iv(cipher2, iv1);
76 assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL));
77 SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len);
78 SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len);
79 if (memcmp(pdst, p1, p1_len)) {
80 SILC_LOG_DEBUG(("Decrypt failed"));
83 SILC_LOG_DEBUG(("Decrypt is successful"));
86 /* Second test vector */
87 SILC_LOG_DEBUG(("Second test vector"));
88 memset(dst, 0, sizeof(dst));
89 memset(pdst, 0, sizeof(pdst));
90 silc_cipher_set_iv(cipher, iv2);
91 assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE));
92 assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE));
93 assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL));
94 SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
95 silc_cipher_get_block_len(cipher),
96 silc_cipher_get_key_len(cipher),
97 silc_cipher_get_name(cipher)));
98 SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len);
99 SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len);
100 SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len);
101 if (memcmp(dst, c2, p2_len)) {
102 SILC_LOG_DEBUG(("Encrypt failed"));
105 SILC_LOG_DEBUG(("Encrypt is successful"));
106 silc_cipher_set_iv(cipher2, iv2);
107 assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL));
108 SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len);
109 SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len);
110 if (memcmp(pdst, p2, p2_len)) {
111 SILC_LOG_DEBUG(("Decrypt failed"));
114 SILC_LOG_DEBUG(("Decrypt is successful"));
115 silc_cipher_free(cipher);
116 silc_cipher_free(cipher2);
118 SILC_LOG_DEBUG(("Allocating cast5-128-ctr cipher"));
119 if (!silc_cipher_alloc("cast5-128-ctr", &cipher)) {
120 SILC_LOG_DEBUG(("Allocating cast5-128-ctr cipher failed"));
124 /* Fourth test vector */
125 SILC_LOG_DEBUG(("Fourth test vector"));
126 memset(dst, 0, sizeof(dst));
127 memset(pdst, 0, sizeof(pdst));
128 silc_cipher_set_iv(cipher, iv4);
129 assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE));
130 assert(silc_cipher_encrypt(cipher, p4, dst, p4_len, NULL));
131 SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
132 silc_cipher_get_block_len(cipher),
133 silc_cipher_get_key_len(cipher),
134 silc_cipher_get_name(cipher)));
135 SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p4, p4_len);
136 SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len);
137 SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len);
138 if (memcmp(dst, c4, p4_len)) {
139 SILC_LOG_DEBUG(("Encrypt failed"));
142 SILC_LOG_DEBUG(("Encrypt is successful"));
143 silc_cipher_set_iv(cipher, iv4);
144 assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL));
145 SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len);
146 SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len);
147 if (memcmp(pdst, p4, p4_len)) {
148 SILC_LOG_DEBUG(("Decrypt failed"));
151 SILC_LOG_DEBUG(("Decrypt is successful"));
152 silc_cipher_free(cipher);
157 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
158 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
160 silc_cipher_unregister_all();