Add axTLS sourcecode
[project/luci.git] / libs / nixio / axTLS / ssl / test / perf_bigint.c
1 /*
2  * Copyright (c) 2007, Cameron Rich
3  * 
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, 
10  *   this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice, 
12  *   this list of conditions and the following disclaimer in the documentation 
13  *   and/or other materials provided with the distribution.
14  * * Neither the name of the axTLS project nor the names of its contributors 
15  *   may be used to endorse or promote products derived from this software 
16  *   without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * Some performance testing of bigint.
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "ssl.h"
39
40 /**************************************************************************
41  * BIGINT tests 
42  *
43  **************************************************************************/
44
45 int main(int argc, char *argv[])
46 {
47 #ifdef CONFIG_SSL_CERT_VERIFICATION
48     RSA_CTX *rsa_ctx;
49     BI_CTX *ctx;
50     bigint *bi_data, *bi_res;
51     int diff, res = 1;
52     struct timeval tv_old, tv_new;
53     const char *plaintext;
54     uint8_t compare[MAX_KEY_BYTE_SIZE];
55     int i, max_biggie = 10;    /* really crank performance */
56     int len; 
57     uint8_t *buf;
58
59     /**
60      * 512 bit key
61      */
62     plaintext = /* 64 byte number */
63         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
64
65     len = get_file("../ssl/test/axTLS.key_512", &buf);
66     asn1_get_private_key(buf, len, &rsa_ctx);
67     ctx = rsa_ctx->bi_ctx;
68     bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
69     bi_res = RSA_public(rsa_ctx, bi_data);
70     bi_data = bi_res;   /* reuse again */
71
72     gettimeofday(&tv_old, NULL);
73     for (i = 0; i < max_biggie; i++)
74     {
75         bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
76         if (i < max_biggie-1)
77         {
78             bi_free(ctx, bi_res);
79         }
80     }
81
82     gettimeofday(&tv_new, NULL);
83     bi_free(ctx, bi_data);
84
85     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
86                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
87     printf("512 bit decrypt time: %dms\n", diff/max_biggie);
88     TTY_FLUSH();
89     bi_export(ctx, bi_res, compare, 64);
90     RSA_free(rsa_ctx);
91     free(buf);
92     if (memcmp(plaintext, compare, 64) != 0)
93         goto end;
94
95     /**
96      * 1024 bit key
97      */
98     plaintext = /* 128 byte number */
99         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
100         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
101
102     len = get_file("../ssl/test/axTLS.key_1024", &buf);
103     asn1_get_private_key(buf, len, &rsa_ctx);
104     ctx = rsa_ctx->bi_ctx;
105     bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
106     bi_res = RSA_public(rsa_ctx, bi_data);
107     bi_data = bi_res;   /* reuse again */
108
109     gettimeofday(&tv_old, NULL);
110     for (i = 0; i < max_biggie; i++)
111     {
112         bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
113         if (i < max_biggie-1)
114         {
115             bi_free(ctx, bi_res);
116         }
117     }
118
119     gettimeofday(&tv_new, NULL);
120     bi_free(ctx, bi_data);
121
122     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
123                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
124     printf("1024 bit decrypt time: %dms\n", diff/max_biggie);
125     TTY_FLUSH();
126     bi_export(ctx, bi_res, compare, 128);
127     RSA_free(rsa_ctx);
128     free(buf);
129     if (memcmp(plaintext, compare, 128) != 0)
130         goto end;
131
132     /**
133      * 2048 bit key
134      */
135     plaintext = /* 256 byte number */
136         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
137         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
138         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
139         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
140
141     len = get_file("../ssl/test/axTLS.key_2048", &buf);
142     asn1_get_private_key(buf, len, &rsa_ctx);
143     ctx = rsa_ctx->bi_ctx;
144     bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
145     bi_res = RSA_public(rsa_ctx, bi_data);
146     bi_data = bi_res;   /* reuse again */
147
148     gettimeofday(&tv_old, NULL);
149     for (i = 0; i < max_biggie; i++)
150     {
151         bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
152         if (i < max_biggie-1)
153         {
154             bi_free(ctx, bi_res);
155         }
156     }
157     gettimeofday(&tv_new, NULL);
158     bi_free(ctx, bi_data);
159
160     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
161                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
162     printf("2048 bit decrypt time: %dms\n", diff/max_biggie);
163     TTY_FLUSH();
164     bi_export(ctx, bi_res, compare, 256);
165     RSA_free(rsa_ctx);
166     free(buf);
167     if (memcmp(plaintext, compare, 256) != 0)
168         goto end;
169
170     /**
171      * 4096 bit key
172      */
173     plaintext = /* 512 byte number */
174         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
175         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
176         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
177         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
178         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
179         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
180         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
181         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
182
183     len = get_file("../ssl/test/axTLS.key_4096", &buf);
184     asn1_get_private_key(buf, len, &rsa_ctx);
185     ctx = rsa_ctx->bi_ctx;
186     bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
187     gettimeofday(&tv_old, NULL);
188     bi_res = RSA_public(rsa_ctx, bi_data);
189     gettimeofday(&tv_new, NULL);
190     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
191                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
192     printf("4096 bit encrypt time: %dms\n", diff);
193     TTY_FLUSH();
194     bi_data = bi_res;   /* reuse again */
195
196     gettimeofday(&tv_old, NULL);
197     for (i = 0; i < max_biggie; i++)
198     {
199         bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
200         if (i < max_biggie-1)
201         {
202             bi_free(ctx, bi_res);
203         }
204     }
205
206     gettimeofday(&tv_new, NULL);
207     bi_free(ctx, bi_data);
208
209     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
210                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
211     printf("4096 bit decrypt time: %dms\n", diff/max_biggie);
212     TTY_FLUSH();
213     bi_export(ctx, bi_res, compare, 512);
214     RSA_free(rsa_ctx);
215     free(buf);
216     if (memcmp(plaintext, compare, 512) != 0)
217         goto end;
218
219     /* done */
220     printf("Bigint performance testing complete\n");
221     res = 0;
222
223 end:
224     return res;
225 #else
226     return 0;
227 #endif
228 }