2 * Copyright (c) 2007, Cameron Rich
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
32 * Some performance testing of bigint.
40 /**************************************************************************
43 **************************************************************************/
45 int main(int argc, char *argv[])
47 #ifdef CONFIG_SSL_CERT_VERIFICATION
50 bigint *bi_data, *bi_res;
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 */
62 plaintext = /* 64 byte number */
63 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
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 */
72 gettimeofday(&tv_old, NULL);
73 for (i = 0; i < max_biggie; i++)
75 bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
82 gettimeofday(&tv_new, NULL);
83 bi_free(ctx, bi_data);
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);
89 bi_export(ctx, bi_res, compare, 64);
92 if (memcmp(plaintext, compare, 64) != 0)
98 plaintext = /* 128 byte number */
99 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
100 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
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 */
109 gettimeofday(&tv_old, NULL);
110 for (i = 0; i < max_biggie; i++)
112 bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
113 if (i < max_biggie-1)
115 bi_free(ctx, bi_res);
119 gettimeofday(&tv_new, NULL);
120 bi_free(ctx, bi_data);
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);
126 bi_export(ctx, bi_res, compare, 128);
129 if (memcmp(plaintext, compare, 128) != 0)
135 plaintext = /* 256 byte number */
136 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
137 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
138 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
139 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
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 */
148 gettimeofday(&tv_old, NULL);
149 for (i = 0; i < max_biggie; i++)
151 bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
152 if (i < max_biggie-1)
154 bi_free(ctx, bi_res);
157 gettimeofday(&tv_new, NULL);
158 bi_free(ctx, bi_data);
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);
164 bi_export(ctx, bi_res, compare, 256);
167 if (memcmp(plaintext, compare, 256) != 0)
173 plaintext = /* 512 byte number */
174 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
175 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
176 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
177 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
178 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
179 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
180 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
181 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
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);
194 bi_data = bi_res; /* reuse again */
196 gettimeofday(&tv_old, NULL);
197 for (i = 0; i < max_biggie; i++)
199 bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
200 if (i < max_biggie-1)
202 bi_free(ctx, bi_res);
206 gettimeofday(&tv_new, NULL);
207 bi_free(ctx, bi_data);
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);
213 bi_export(ctx, bi_res, compare, 512);
216 if (memcmp(plaintext, compare, 512) != 0)
220 printf("Bigint performance testing complete\n");