Rework LuCI build system
[project/luci.git] / libs / luci-lib-nixio / axTLS / ssl / test / datatest.c.old
1 #include "crypto.h"
2
3 #include <string.h>
4 #include <stdlib.h>
5 //#define DEBUG_TEST 
6
7 typedef enum {
8     encrypt, decrypt
9 } CryptoMode;
10
11 void hex_dump(const char* header, const unsigned char* data, const unsigned int data_length)
12 {
13     unsigned int byte_count;
14     printf("%s (%d bytes):\n", header, data_length);
15     for(byte_count = 0; byte_count < data_length; ++byte_count)
16     {
17         printf("%02X", data[byte_count]);
18     }
19     printf("\n");
20 }
21
22 void do_rsa(const CryptoMode crypto_mode,
23             const unsigned char* data, const unsigned int data_length,
24             const unsigned char* modulus, const unsigned int modulus_length,
25             const unsigned char* exponent, const unsigned int exponent_length,
26             unsigned char* result, const unsigned int result_length)
27 {
28     RSA_CTX* rsa_context = NULL;
29     BI_CTX *bi_ctx;
30     bigint *plaintext_bi;
31     bigint *enc_data_bi, *dec_data_bi;
32
33 #ifdef DEBUG_TEST
34     printf("do_rsa:\n");
35     hex_dump("data", data, data_length);
36     hex_dump("modulus", modulus, modulus_length);
37     hex_dump("exponent", exponent, exponent_length);
38 #endif
39
40     RSA_priv_key_new(&rsa_context, modulus, modulus_length, exponent, exponent_length, exponent, exponent_length);
41     memset(result, 0, result_length);
42     bi_ctx = rsa_context->bi_ctx;
43
44     switch(crypto_mode)
45     {
46     case encrypt:
47 #ifdef DEBUG_TEST
48         printf("encrypt\n");
49 #endif
50         plaintext_bi = bi_import(bi_ctx, data, data_length);
51         enc_data_bi = RSA_public(rsa_context, plaintext_bi);
52         bi_export(bi_ctx, enc_data_bi, result, result_length);
53         break;
54
55     case decrypt:
56
57 #ifdef DEBUG_TEST
58         printf("decrypt\n");
59 #endif
60         plaintext_bi = bi_import(bi_ctx, data, data_length);
61         dec_data_bi = RSA_private(rsa_context, plaintext_bi);
62         bi_export(bi_ctx, dec_data_bi, result, result_length);
63         break;
64     }
65 #ifdef DEBUG_TEST
66     hex_dump("result", result, result_length);
67 #endif
68
69     RSA_free(rsa_context);
70 }
71
72 void test_matching(char* test_description, 
73                    const unsigned char* expected, const unsigned int expected_length,
74                    const unsigned char* result, const unsigned int result_length)
75 {
76     int test_result = memcmp(expected, result, expected_length);
77     printf("Testing %s ... ", test_description);
78     if(test_result == 0)
79     {
80         printf("ok.\n");
81     }
82     else
83     {
84         printf("failed!\n");
85         hex_dump("should be", expected, expected_length);
86         hex_dump("but is", result, result_length);
87     }
88 }
89
90 void encrypt_decrypt_should_yield_original(char* test_description,
91                                            const unsigned char* data, const unsigned int data_length,
92                                            const unsigned char* modulus, const unsigned int modulus_length,
93                                            const unsigned char* private_exponent, const unsigned int private_exponent_length,
94                                            const unsigned char* public_exponent, const unsigned int public_exponent_length,
95                                            const unsigned char* cryptogram, const unsigned int cryptogram_length)
96 {
97     const unsigned int calculated_cryptogram_length = modulus_length;
98     unsigned char* calculated_cryptogram = malloc(calculated_cryptogram_length);
99     const unsigned int decrypted_data_length = modulus_length;
100     unsigned char* decrypted_data = malloc(decrypted_data_length);
101
102     printf("\nRunning \"%s\" ...\n", test_description);
103
104 #ifdef DEBUG_TEST
105     printf("encrypt_decrypt_should_yield_original:\n");
106     hex_dump("data", data, data_length);
107     hex_dump("modulus", modulus, modulus_length);
108     hex_dump("private_exponent", private_exponent, private_exponent_length);
109     hex_dump("public_exponent", public_exponent, public_exponent_length);
110     hex_dump("cryptogram", cryptogram, cryptogram_length);
111 #endif
112
113     do_rsa(encrypt, data, data_length,
114            modulus, modulus_length,
115            private_exponent, private_exponent_length,
116            calculated_cryptogram, calculated_cryptogram_length);
117
118 #ifdef DEBUG_TEST
119     hex_dump("calculated_cryptogram", calculated_cryptogram, calculated_cryptogram_length);
120 #endif
121
122     if(cryptogram != NULL)
123     {
124         test_matching("cryptogram", cryptogram, cryptogram_length, 
125                                     calculated_cryptogram, calculated_cryptogram_length);
126     }
127
128     do_rsa(decrypt, calculated_cryptogram, calculated_cryptogram_length,
129            modulus, modulus_length,
130            public_exponent, public_exponent_length,
131            decrypted_data, decrypted_data_length);
132
133     test_matching("decrypted plaintext", data, data_length,
134                                          decrypted_data, decrypted_data_length);
135
136     free(calculated_cryptogram);
137     free(decrypted_data);
138 }
139
140 /* configure without CRT!
141
142    prepare data with: 
143    > echo "<string>" |
144      ruby -ne '$_.gsub!(/ /, "").scan(/../).each_with_index \
145       { |b, i| print "\"\n\"" if i % 16 == 0; print "\\x" + b;}' */
146 int main(int argc, char *argv[])
147 {
148 #if 0
149     unsigned char stuff[] = { 
150         0x22, 0x33, 0x44, 0x81, 
151         0xF1, 0xFF, 0xAA, 0xBB, 
152         0xCC, 0xDD, 0xEE , 0x01,
153         0x45, 0x44, 0xfa, 0x8d,
154         0xfa, 0x20, 0x99, 0xFF,
155         0xab, 0xda, 0xac, 0x40 };
156     unsigned char resA[sizeof(stuff)*2], resB[sizeof(stuff)*2];
157
158     BI_CTX *bi_ctx = bi_initialize();
159     bigint *bi_data1, *bi_data2, *res1, *res2;
160     bi_data1 = bi_import(bi_ctx, stuff, sizeof(stuff));
161     bi_data2 = bi_import(bi_ctx, stuff, sizeof(stuff));
162
163     res1 = bi_multiply(bi_ctx, bi_copy(bi_data1), bi_copy(bi_data2));
164     res2 = bi_multiply(bi_ctx, bi_data1, bi_data2);
165     bi_print("MULTIPLY", res1);
166     bi_print("SQUARE", res2);
167     bi_export(bi_ctx, res1, resA, sizeof(resA));
168     bi_export(bi_ctx, res2, resB, sizeof(resB));
169     if (memcmp(resA, resB, sizeof(resA)))
170         printf("OUCH - difference!\n");
171     bi_terminate(bi_ctx);
172
173     exit(0);
174 #endif
175     encrypt_decrypt_should_yield_original("Works only with Montgomery",
176         (const unsigned char*) /* data */
177         "\xBC\xD3\x12\x6C\x93\x13\x14\x4C\x00\x5D\xFD\xBF\xDE\xE4\xD3\x60"
178         "\x29\xB8\xAE\x47\xBE\x0B\xB6\x0A\x39\x88\xB7\x93\x19\x14\xE8\x88"
179         "\x4A\xDE\x00\x46\x89\x5A\x11\x1A\xC4\x8F\xE8\xF7\x27\xAC\x59\x80"
180         "\x03\xC1\x93\x14\x01\x00\x93\x15\x07\x00\x00\x00\x01\x01\x05\x20"
181         "\x93\x16\x0F\x42\x34\x33\x3A\x58\x30\x30\x30\x31\x30\x31\x30\x30"
182         "\x30\x31\x92\x6B\x10\x6C\x69\x62\x65\x6C\x6D\x65\x74\x72\x65\x65"
183         "\x2E\x73\x6F\x2E\x30\x93\x18\x02\xA5\x92\x92\x6C\x03\x96\xE3\x0C"
184         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xB7\xBE", 128,
185         (const unsigned char*) /* modulus */
186         "\xc4\x5a\xcb\x35\x95\xad\x32\x4a\xcf\x9c\x82\x45\x13\xb7\x42\x35"
187         "\x22\x32\x6d\x2e\x6d\x26\x2e\x6d\x00\x9b\xae\x2d\x9e\x78\x1e\xdd"
188         "\x40\x23\x17\xa8\xbb\xa1\x07\x86\xb4\x3c\xbc\xe8\xd5\xfc\xd9\xeb"
189         "\x3c\xad\x63\x11\xf3\x1d\x64\x81\x96\xf2\xf5\xfe\xca\x5a\xf7\x8a"
190         "\x15\xcb\x90\x81\x68\xae\x59\xb4\xe1\xa4\x41\x99\xcd\xf3\x98\xbd"
191         "\x3c\x48\x37\xdb\xa1\xc3\x1c\x6f\x43\xd1\x89\x23\xe5\x3d\xa3\xa5"
192         "\x92\x7b\x19\x14\x1e\x7a\xf3\x88\x8a\x36\x21\x3e\x16\x40\x3c\xd7"
193         "\xd3\xdb\x13\xaf\xc9\x68\x45\x84\xb3\x39\x8f\x02\xed\x28\x02\x5f", 128,
194         (const unsigned char*) /* private exponent */
195         "\x5d\x19\xb7\xb4\x66\x8d\xc2\x84\xda\x3f\x99\x3c\xeb\x86\x3e\xec"
196         "\x36\x94\xb6\x54\x07\x08\xcd\x86\x7d\x7d\x53\x6e\xe9\xee\x86\xa3"
197         "\xdd\x5f\x46\x3e\x89\x08\x67\x2b\x25\x96\x8e\xf3\xcf\x52\x9e\x78"
198         "\xfd\x42\x30\xf1\x37\xd6\xbd\xea\xfc\x09\xa3\x3d\xf5\xf0\x7f\xe1"
199         "\xb1\xe0\x69\x13\x44\xf9\x8b\x95\x58\x2a\x81\xb3\xa8\x15\xce\x7e"
200         "\xd3\xea\x97\x0a\xa2\x14\xd4\xae\xc7\x75\xbb\x9f\x68\xa5\x53\x0e"
201         "\x85\x29\x88\x48\x6c\xc9\xcc\xde\x72\x40\x3a\x4c\x82\xde\x3c\xfb"
202         "\x08\xf8\x2c\x26\xb5\xd4\xea\xc4\xca\x98\x6e\x43\x3e\x67\x54\xc1", 128,
203         (const unsigned char*) /* public exponent */
204         "\x01\x00\x01", 3,
205         (const unsigned char*) /* precalculated encrypted data */
206         "\x93\xE8\x1F\xF9\x70\xFA\xAA\xED\x54\xFD\x48\x37\xC9\x71\x9A\x11"
207         "\x69\x80\xB4\x22\x0C\xAD\x5A\x95\x65\xCA\x7C\xF7\x70\x56\x92\xCB"
208         "\x45\x6D\x58\x84\x21\x80\x23\x76\x21\x4A\x61\x99\xC1\x11\x9C\x0F"
209         "\x40\xED\x80\x9C\x8F\x3A\x4F\x01\xB5\x72\xC3\x24\xAE\xF3\x6B\x98"
210         "\xA8\x60\xAC\xAF\x95\x98\x9A\xAA\xA4\x28\xF2\x02\x05\xFC\xF3\xDD"
211         "\xB0\x5A\x4E\xDE\x3C\x41\x4B\x1C\x5B\x1F\xF6\x3D\xAF\x93\x43\xCB"
212         "\xD8\xC7\x24\x97\x8F\x49\xE5\x5B\x10\x51\x3B\x1E\xA6\x39\xEA\x4E"
213         "\xA5\xE0\x71\x8C\xCA\x34\x8C\x2F\x6C\x5C\x78\x34\x86\x7C\x54\x6A", 128);
214
215     encrypt_decrypt_should_yield_original("Works only with Barrett",
216         (const unsigned char*) /* data */
217         "\x36\x42\x32\xe4\x1e\x78\x02\x8e\xfb\x64\x5f\x0c\xfc\x5a\xd7\x5c"
218         "\xe4\xb5\x91\x5c\x4b\x00\x87\x28\x87\x9b\xa0\x4b\x09\xc2\x6b\x64"
219         "\xac\x4b\xcf\xa5\xee\x8a\xb7\xc9\xc9\x90\x02\xc1\xa3\x47\x5c\x6b"
220         "\x71\x5d\x5d\x49\x27\xe1\x15\xc6\xcf\x37\x9e\xa7\x0f\xa1\xad\x96"
221         "\x83\xef\x4b\x53\x68\xcd\x77\xfc\x14\x5f\xf5\xb7\x78\xb0\x10\xeb"
222         "\x0d\x61\x94\x01\xf6\xaa\x1b\x19\x23\x39\xa7\xcc\x6c\x42\x4a\x87"
223         "\x79\x27\x04\xc6\xec\x8e\x50\xba\xb9\x26\x89\xd4\x00\x01\x25\xe5"
224         "\xf3\x9e\x98\x0c\x8d\x2e\x43\x1e\xe9\x29\x90\xd2\x75\x61\x85\xe7", 128,
225         (const unsigned char*) /* modulus */
226         "\x37\x0c\x32\xe4\x1e\x78\x02\x8e\xfb\x64\x5f\x0c\xfc\x5a\xd7\x5c"
227         "\xe4\xb5\x91\x5c\x4b\x00\x87\x28\x87\x9b\xa0\x4b\x09\xc2\x6b\x64"
228         "\xac\x4b\xcf\xa5\xee\x8a\xb7\xc9\xc9\x90\x02\xc1\xa3\x47\x5c\x6b"
229         "\x71\x5d\x5d\x49\x27\xe1\x15\xc6\xcf\x37\x9e\xa7\x0f\xa1\xad\x96"
230         "\x83\xef\x4b\x53\x68\xcd\x77\xfc\x14\x5f\xf5\xb7\x78\xb0\x10\xeb"
231         "\x0d\x61\x94\x01\xf6\xaa\x1b\x19\x23\x39\xa7\xcc\x6c\x42\x4a\x87"
232         "\x79\x27\x04\xc6\xec\x8e\x50\xba\xb9\x26\x89\xd4\x00\x01\x25\xe5"
233         "\xf3\x9e\x98\x0c\x8d\x2e\x43\x1e\xe9\x29\x90\xd2\x75\x61\x85\xe7", 128,
234         (const unsigned char*) /* private exponent */
235         "\x16\x3a\x76\xd2\x66\xfb\x4f\x0d\x2d\xb6\x7a\x2b\x64\x3b\xca\x7b"
236         "\x58\x5f\x79\x33\x2b\x96\x2a\xfd\xd2\xc4\xa5\x15\xa7\xfb\x3a\x22"
237         "\x8c\xf0\x90\x09\x11\x2a\x32\xcc\xe8\xf7\x9e\x25\x53\x29\x9d\xc8"
238         "\x45\x1e\xce\x6c\x9c\x0d\xe8\x1d\x3f\xcf\xd5\xe0\xe0\x0f\x09\x69"
239         "\x2d\xe7\xd5\xe6\xe5\x10\xd9\x4e\x20\xdb\xbd\xa1\x04\x6b\xe6\x1d"
240         "\x4c\x79\x28\x47\x30\x11\xde\x14\xb4\x6e\x35\x98\x38\x50\x44\x82"
241         "\xbd\xc4\xfb\x03\xb3\xf6\x5e\x5a\x29\xfa\x29\xaa\xde\xe4\xfd\x15"
242         "\xbe\xed\x4f\x93\x9d\x0d\x29\xe8\xd7\xa3\xf4\x18\xc8\x98\xb1\x01", 128, 
243         (const unsigned char*) /* public exponent */
244         "\x01\x00\x01", 3,
245         NULL, 0);
246
247     encrypt_decrypt_should_yield_original("Works always",
248         (const unsigned char*) /* data */
249         "\xB9\x42\x32\xe4\x1e\x78\x02\x8e\xfb\x64\x5f\x0c\xfc\x5a\xd7\x5c"
250         "\xe4\xb5\x91\x5c\x4b\x00\x87\x28\x87\x9b\xa0\x4b\x09\xc2\x6b\x64"
251         "\xac\x4b\xcf\xa5\xee\x8a\xb7\xc9\xc9\x90\x02\xc1\xa3\x47\x5c\x6b"
252         "\x71\x5d\x5d\x49\x27\xe1\x15\xc6\xcf\x37\x9e\xa7\x0f\xa1\xad\x96"
253         "\x83\xef\x4b\x53\x68\xcd\x77\xfc\x14\x5f\xf5\xb7\x78\xb0\x10\xeb"
254         "\x0d\x61\x94\x01\xf6\xaa\x1b\x19\x23\x39\xa7\xcc\x6c\x42\x4a\x87"
255         "\x79\x27\x04\xc6\xec\x8e\x50\xba\xb9\x26\x89\xd4\x00\x01\x25\xe5"
256         "\xf3\x9e\x98\x0c\x8d\x2e\x43\x1e\xe9\x29\x90\xd2\x75\x61\x85\xe7", 128,
257         (const unsigned char*) /* modulus */
258         "\xB9\x77\xEC\x83\x95\xAF\xB1\xF8\x21\x21\xFF\x05\x5E\x0C\x91\x0C"
259         "\x2E\xD5\xD2\x94\x1C\x38\x5E\xED\x5A\xCF\x84\xD0\x12\x8B\xAA\x4B"
260         "\x3A\x63\x65\x78\x13\xED\x24\x4E\x83\xF2\xF5\x02\x66\x5D\xFC\xC1"
261         "\x80\x5B\x78\x78\xB4\x0B\x45\xE5\x22\xC6\xCD\xEB\xCC\x74\x0B\x0B"
262         "\xD8\x8B\x91\x99\x48\x8E\x74\xA9\xD0\x1A\x39\x94\xC2\xD4\x2E\x9A"
263         "\x8C\x0C\x35\x0D\x97\x8F\xC4\x62\x20\xE9\x78\x40\x97\x05\x98\xE6"
264         "\x22\x48\x3D\x3D\xCA\x6A\x3F\xEF\xB0\x23\x14\x30\xDA\x35\x46\x65"
265         "\x55\xEF\xEB\xA1\xA9\xCF\x83\xE7\xEF\xF2\x83\x6D\x38\xEA\x88\xED", 128,
266         (const unsigned char*) /* private exponent */
267         "\x52\x2A\x68\xE3\x9A\xAA\xED\xA3\x49\xBA\x6F\xEA\x86\xD1\xF6\x68"
268         "\x79\x4F\x4D\x2D\x44\x9B\x4C\xA2\xC6\xBA\x6C\xD2\x69\x84\xEA\x7A"
269         "\xCD\x71\x3F\x80\xC5\x03\x28\x34\x88\x8C\x58\x33\x29\xFA\xB5\x81"
270         "\x5C\x46\x29\xC6\xFF\xAC\x86\xD8\x8E\x61\x98\xD4\xC0\x0D\x20\xDE"
271         "\xEB\x61\x1C\x0C\x3C\x19\xA3\x75\x10\x7D\xDA\xA9\x55\xA7\x64\x5F"
272         "\xE0\xB6\x35\x62\x00\xD9\xD2\xF7\xA4\xDF\x85\xFF\xDF\x86\x75\x29"
273         "\x66\x16\x03\x8C\xC0\xB0\x3F\xAB\xBA\x41\xB3\x3C\x76\x58\xB6\xE2"
274         "\x1F\x36\x47\x5F\x1F\x0E\x4C\xB5\x29\x90\xDC\xA1\xF8\xFA\x58\x19", 128,
275         (const unsigned char*) /* public exponent */
276         "\x01\x00\x01", 3,
277         NULL, 0);
278
279     return 0;
280 }