Annotation of embedaddon/axTLS/ssl/test/perf_bigint.c, revision 1.1.1.1

1.1       misho       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: 
                     39: #include "os_port.h"
                     40: #include "ssl.h"
                     41: 
                     42: /**************************************************************************
                     43:  * BIGINT tests 
                     44:  *
                     45:  **************************************************************************/
                     46: 
                     47: int main(int argc, char *argv[])
                     48: {
                     49: #ifdef CONFIG_SSL_CERT_VERIFICATION
                     50:     RSA_CTX *rsa_ctx = NULL;
                     51:     BI_CTX *ctx;
                     52:     bigint *bi_data, *bi_res;
                     53:     float diff;
                     54:     int res = 1;
                     55:     struct timeval tv_old, tv_new;
                     56:     const char *plaintext;
                     57:     uint8_t compare[MAX_KEY_BYTE_SIZE];
                     58:     int i, max_biggie = 10;    /* really crank performance */
                     59:     int len; 
                     60:     uint8_t *buf;
                     61: 
                     62:     /**
                     63:      * 512 bit key
                     64:      */
                     65:     plaintext = /* 64 byte number */
                     66:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
                     67: 
                     68:     len = get_file("../ssl/test/axTLS.key_512", &buf);
                     69:     asn1_get_private_key(buf, len, &rsa_ctx);
                     70:     ctx = rsa_ctx->bi_ctx;
                     71:     bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
                     72:     bi_res = RSA_public(rsa_ctx, bi_data);
                     73:     bi_data = bi_res;   /* reuse again */
                     74: 
                     75:     gettimeofday(&tv_old, NULL);
                     76:     for (i = 0; i < max_biggie; i++)
                     77:     {
                     78:         bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
                     79:         if (i < max_biggie-1)
                     80:         {
                     81:             bi_free(ctx, bi_res);
                     82:         }
                     83:     }
                     84: 
                     85:     gettimeofday(&tv_new, NULL);
                     86:     bi_free(ctx, bi_data);
                     87: 
                     88:     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
                     89:                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
                     90:     printf("512 bit decrypt time: %.2fms\n", diff/max_biggie);
                     91:     TTY_FLUSH();
                     92:     bi_export(ctx, bi_res, compare, 64);
                     93:     RSA_free(rsa_ctx);
                     94:     free(buf);
                     95:     if (memcmp(plaintext, compare, 64) != 0)
                     96:         goto end;
                     97: 
                     98:     /**
                     99:      * 1024 bit key
                    100:      */
                    101:     plaintext = /* 128 byte number */
                    102:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    103:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
                    104: 
                    105:     len = get_file("../ssl/test/axTLS.key_1024", &buf);
                    106:     rsa_ctx = NULL;
                    107:     asn1_get_private_key(buf, len, &rsa_ctx);
                    108:     ctx = rsa_ctx->bi_ctx;
                    109:     bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
                    110:     bi_res = RSA_public(rsa_ctx, bi_data);
                    111:     bi_data = bi_res;   /* reuse again */
                    112: 
                    113:     gettimeofday(&tv_old, NULL);
                    114:     for (i = 0; i < max_biggie; i++)
                    115:     {
                    116:         bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
                    117:         if (i < max_biggie-1)
                    118:         {
                    119:             bi_free(ctx, bi_res);
                    120:         }
                    121:     }
                    122: 
                    123:     gettimeofday(&tv_new, NULL);
                    124:     bi_free(ctx, bi_data);
                    125: 
                    126:     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
                    127:                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
                    128:     printf("1024 bit decrypt time: %.2fms\n", diff/max_biggie);
                    129:     TTY_FLUSH();
                    130:     bi_export(ctx, bi_res, compare, 128);
                    131:     RSA_free(rsa_ctx);
                    132:     free(buf);
                    133:     if (memcmp(plaintext, compare, 128) != 0)
                    134:         goto end;
                    135: 
                    136:     /**
                    137:      * 2048 bit key
                    138:      */
                    139:     plaintext = /* 256 byte number */
                    140:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    141:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    142:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    143:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
                    144: 
                    145:     len = get_file("../ssl/test/axTLS.key_2048", &buf);
                    146:     rsa_ctx = NULL;
                    147:     asn1_get_private_key(buf, len, &rsa_ctx);
                    148:     ctx = rsa_ctx->bi_ctx;
                    149:     bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
                    150:     bi_res = RSA_public(rsa_ctx, bi_data);
                    151:     bi_data = bi_res;   /* reuse again */
                    152: 
                    153:     gettimeofday(&tv_old, NULL);
                    154:     for (i = 0; i < max_biggie; i++)
                    155:     {
                    156:         bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
                    157:         if (i < max_biggie-1)
                    158:         {
                    159:             bi_free(ctx, bi_res);
                    160:         }
                    161:     }
                    162:     gettimeofday(&tv_new, NULL);
                    163:     bi_free(ctx, bi_data);
                    164: 
                    165:     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
                    166:                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
                    167:     printf("2048 bit decrypt time: %.2fms\n", diff/max_biggie);
                    168:     TTY_FLUSH();
                    169:     bi_export(ctx, bi_res, compare, 256);
                    170:     RSA_free(rsa_ctx);
                    171:     free(buf);
                    172:     if (memcmp(plaintext, compare, 256) != 0)
                    173:         goto end;
                    174: 
                    175:     /**
                    176:      * 4096 bit key
                    177:      */
                    178:     plaintext = /* 512 byte number */
                    179:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    180:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    181:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    182:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    183:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    184:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    185:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
                    186:         "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
                    187: 
                    188:     len = get_file("../ssl/test/axTLS.key_4096", &buf);
                    189:     rsa_ctx = NULL;
                    190:     asn1_get_private_key(buf, len, &rsa_ctx);
                    191:     ctx = rsa_ctx->bi_ctx;
                    192:     bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
                    193:     gettimeofday(&tv_old, NULL);
                    194:     bi_res = RSA_public(rsa_ctx, bi_data);
                    195:     gettimeofday(&tv_new, NULL);
                    196:     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
                    197:                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
                    198:     printf("4096 bit encrypt time: %.2fms\n", diff);
                    199:     TTY_FLUSH();
                    200:     bi_data = bi_res;   /* reuse again */
                    201: 
                    202:     gettimeofday(&tv_old, NULL);
                    203:     for (i = 0; i < max_biggie; i++)
                    204:     {
                    205:         bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
                    206:         if (i < max_biggie-1)
                    207:         {
                    208:             bi_free(ctx, bi_res);
                    209:         }
                    210:     }
                    211: 
                    212:     gettimeofday(&tv_new, NULL);
                    213:     bi_free(ctx, bi_data);
                    214: 
                    215:     diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
                    216:                 (tv_new.tv_usec-tv_old.tv_usec)/1000;
                    217:     printf("4096 bit decrypt time: %.2fms\n", diff/max_biggie);
                    218:     TTY_FLUSH();
                    219:     bi_export(ctx, bi_res, compare, 512);
                    220:     RSA_free(rsa_ctx);
                    221:     free(buf);
                    222:     if (memcmp(plaintext, compare, 512) != 0)
                    223:         goto end;
                    224: 
                    225:     /* done */
                    226:     printf("Bigint performance testing complete\n");
                    227:     res = 0;
                    228: 
                    229: end:
                    230:     return res;
                    231: #else
                    232:     return 0;
                    233: #endif
                    234: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>