Added SILC Server library.
[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
23 int main(int argc, char **argv)
24 {
25   SilcBool success = FALSE;
26   SilcCipher cipher;
27   unsigned char dst[256], pdst[256];
28   int i;
29
30   if (argc > 1 && !strcmp(argv[1], "-d")) {
31     silc_log_debug(TRUE);
32     silc_log_debug_hexdump(TRUE);
33     silc_log_set_debug_string("*crypt*,*aes*,*cipher*");
34   }
35
36   SILC_LOG_DEBUG(("Registering builtin hash functions"));
37   silc_cipher_register_default();
38
39   if (!silc_cipher_is_supported("aes-128-cbc")) {
40     SILC_LOG_DEBUG(("aes-128-cbc is not supported"));
41     goto err;
42   }
43
44   SILC_LOG_DEBUG(("Allocating AES-CBC cipher"));
45   if (!silc_cipher_alloc("aes-128-cbc", &cipher)) {
46     SILC_LOG_DEBUG(("Allocating AES-CBC cipher failed"));
47     goto err;
48   }
49
50   /* First test vector */
51   SILC_LOG_DEBUG(("First test vector"));
52   memset(dst, 0, sizeof(dst));
53   memset(pdst, 0, sizeof(pdst));
54   silc_cipher_set_iv(cipher, iv1);
55   assert(silc_cipher_set_key(cipher, key1, key1_len));
56   assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL));
57   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
58                  silc_cipher_get_block_len(cipher),
59                  silc_cipher_get_key_len(cipher),
60                  silc_cipher_get_name(cipher)));
61   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len);
62   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len);
63   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len);
64   if (memcmp(dst, c1, p1_len)) {
65     SILC_LOG_DEBUG(("Encrypt failed"));
66     goto err;
67   }
68   SILC_LOG_DEBUG(("Encrypt is successful"));
69   silc_cipher_set_iv(cipher, iv1);
70   assert(silc_cipher_decrypt(cipher, dst, pdst, p1_len, NULL));
71   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len);
72   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len);
73   if (memcmp(pdst, p1, p1_len)) {
74     SILC_LOG_DEBUG(("Decrypt failed"));
75     goto err;
76   }
77   SILC_LOG_DEBUG(("Decrypt is successful"));
78
79
80   /* Second test vector */
81   SILC_LOG_DEBUG(("Second test vector"));
82   memset(dst, 0, sizeof(dst));
83   memset(pdst, 0, sizeof(pdst));
84   silc_cipher_set_iv(cipher, iv2);
85   assert(silc_cipher_set_key(cipher, key2, key2_len));
86   assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL));
87   SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
88                  silc_cipher_get_block_len(cipher),
89                  silc_cipher_get_key_len(cipher),
90                  silc_cipher_get_name(cipher)));
91   SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len);
92   SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len);
93   SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len);
94   if (memcmp(dst, c2, p2_len)) {
95     SILC_LOG_DEBUG(("Encrypt failed"));
96     goto err;
97   }
98   SILC_LOG_DEBUG(("Encrypt is successful"));
99   silc_cipher_set_iv(cipher, iv2);
100   assert(silc_cipher_decrypt(cipher, dst, pdst, p2_len, NULL));
101   SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len);
102   SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len);
103   if (memcmp(pdst, p2, p2_len)) {
104     SILC_LOG_DEBUG(("Decrypt failed"));
105     goto err;
106   }
107   SILC_LOG_DEBUG(("Decrypt is successful"));
108
109   success = TRUE;
110
111  err:
112   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
113   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
114
115   silc_cipher_free(cipher);
116   silc_cipher_unregister_all();
117   return success;
118 }