3a37d36c4901eddbc1ca5adc2cf1c8add85e56b2
[silc.git] / lib / silccrypt / tests / test_aes.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "silcincludes.h"
4
5 #include "aes.h"
6
7 int main()
8 {
9         int i;
10         unsigned char key[256];
11         unsigned char plain[256];
12         unsigned char plain2[256];
13         unsigned char cipher[256];
14         unsigned char iv[256];
15         void *context;
16         int len;
17
18         memset(&key, 0, sizeof(key));
19         memset(&plain, 0, sizeof(plain));
20         memset(&plain2, 0, sizeof(plain2));
21         memset(&cipher, 0, sizeof(cipher));
22         memset(&iv, 0, sizeof(iv));
23
24         context = malloc(silc_aes_context_len());
25
26         fprintf(stderr, "\nKey:\n");
27 #if 0
28         len = 32;
29
30         for (i = 0; i < len; i += 2) {
31                 fprintf(stderr, "%02x%02x ", key[i], key[i+1]);
32         }
33
34         fprintf(stderr, "\nSetting key\n");
35         silc_aes_set_key(context, key, len * 8);
36
37         fprintf(stderr, "\nPlaintext:\n");
38         for (i = 0; i < len; i += 2) {
39                 plain[i] = i;
40                 plain[i+1] = i+1;
41                 fprintf(stderr, "%02x%02x ", plain[i], plain[i+1]);
42         }
43
44 #else
45         len = 16;
46
47         key[0] = 0x2b;
48         key[1] = 0x7e;
49         key[2] = 0x15;
50         key[3] = 0x16;
51         key[4] = 0x28;
52         key[5] = 0xae;
53         key[6] = 0xd2;
54         key[7] = 0xa6;
55         key[8] = 0xab;
56         key[9] = 0xf7;
57         key[10] = 0x15;
58         key[11] = 0x88;
59         key[12] = 0x09;
60         key[13] = 0xcf;
61         key[14] = 0x4f;
62         key[15] = 0x3c;
63         for (i = 0; i < len ; i += 2) {
64                 fprintf(stderr, "%02x%02x ", key[i], key[i+1]);
65         }
66
67         fprintf(stderr, "\nSetting key\n");
68         silc_aes_set_key(context, key, len * 8);
69
70         plain[0] = 0x32;
71         plain[1] = 0x43;
72         plain[2] = 0xf6;
73         plain[3] = 0xa8;
74         plain[4] = 0x88;
75         plain[5] = 0x5a;
76         plain[6] = 0x30;
77         plain[7] = 0x8d;
78         plain[8] = 0x31;
79         plain[9] = 0x31;
80         plain[10] = 0x98;
81         plain[11] = 0xa2;
82         plain[12] = 0xe0;
83         plain[13] = 0x37;
84         plain[14] = 0x07;
85         plain[15] = 0x34;
86
87         fprintf(stderr, "\nPlaintext:\n");
88         for (i = 0; i < len; i += 2) {
89                 fprintf(stderr, "%02x%02x ", plain[i], plain[i+1]);
90         }
91
92 #endif
93
94         fprintf(stderr, "\n\nEncrypting\n");
95         silc_aes_encrypt_cbc(context, plain, cipher, len, iv);
96
97         fprintf(stderr, "Ciphertext:\n");
98         for (i = 0; i < len; i += 2) {
99                 fprintf(stderr, "%02x", cipher[i]);
100                 fprintf(stderr, "%02x ", cipher[i+1]);
101         }
102
103         memset(&iv, 0, sizeof(iv));
104
105         fprintf(stderr, "\n\nDecrypting\n");
106         silc_aes_decrypt_cbc(context, cipher, plain2, len, iv);
107
108         fprintf(stderr, "Decryptedtext:\n");
109         for (i = 0; i < len; i += 2) {
110                 fprintf(stderr, "%02x", plain2[i]);
111                 fprintf(stderr, "%02x ", plain2[i+1]);
112         }
113         fprintf(stderr, "\nDone\n");
114
115         return 0;
116 }