10Sstevel@tonic-gate /* unused */
20Sstevel@tonic-gate
30Sstevel@tonic-gate /* crypto/bn/expspeed.c */
40Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
50Sstevel@tonic-gate * All rights reserved.
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * This package is an SSL implementation written
80Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
90Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
120Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
130Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
140Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
150Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
160Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
170Sstevel@tonic-gate *
180Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
190Sstevel@tonic-gate * the code are not to be removed.
200Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
210Sstevel@tonic-gate * as the author of the parts of the library used.
220Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
230Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
260Sstevel@tonic-gate * modification, are permitted provided that the following conditions
270Sstevel@tonic-gate * are met:
280Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
300Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
310Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
320Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
330Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
340Sstevel@tonic-gate * must display the following acknowledgement:
350Sstevel@tonic-gate * "This product includes cryptographic software written by
360Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
370Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
380Sstevel@tonic-gate * being used are not cryptographic related :-).
390Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
400Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
410Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
420Sstevel@tonic-gate *
430Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
440Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
450Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
460Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
470Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
480Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
490Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
500Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
510Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
520Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
530Sstevel@tonic-gate * SUCH DAMAGE.
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
560Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
570Sstevel@tonic-gate * copied and put under another distribution licence
580Sstevel@tonic-gate * [including the GNU Public Licence.]
590Sstevel@tonic-gate */
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* most of this code has been pilfered from my libdes speed.c program */
620Sstevel@tonic-gate
630Sstevel@tonic-gate #define BASENUM 5000
640Sstevel@tonic-gate #define NUM_START 0
650Sstevel@tonic-gate
660Sstevel@tonic-gate
670Sstevel@tonic-gate /* determine timings for modexp, modmul, modsqr, gcd, Kronecker symbol,
680Sstevel@tonic-gate * modular inverse, or modular square roots */
690Sstevel@tonic-gate #define TEST_EXP
700Sstevel@tonic-gate #undef TEST_MUL
710Sstevel@tonic-gate #undef TEST_SQR
720Sstevel@tonic-gate #undef TEST_GCD
730Sstevel@tonic-gate #undef TEST_KRON
740Sstevel@tonic-gate #undef TEST_INV
750Sstevel@tonic-gate #undef TEST_SQRT
760Sstevel@tonic-gate #define P_MOD_64 9 /* least significant 6 bits for prime to be used for BN_sqrt timings */
770Sstevel@tonic-gate
780Sstevel@tonic-gate #if defined(TEST_EXP) + defined(TEST_MUL) + defined(TEST_SQR) + defined(TEST_GCD) + defined(TEST_KRON) + defined(TEST_INV) +defined(TEST_SQRT) != 1
790Sstevel@tonic-gate # error "choose one test"
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate
820Sstevel@tonic-gate #if defined(TEST_INV) || defined(TEST_SQRT)
830Sstevel@tonic-gate # define C_PRIME
840Sstevel@tonic-gate static void genprime_cb(int p, int n, void *arg);
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate
870Sstevel@tonic-gate
880Sstevel@tonic-gate
890Sstevel@tonic-gate #undef PROG
900Sstevel@tonic-gate #define PROG bnspeed_main
910Sstevel@tonic-gate
920Sstevel@tonic-gate #include <stdio.h>
930Sstevel@tonic-gate #include <stdlib.h>
940Sstevel@tonic-gate #include <signal.h>
950Sstevel@tonic-gate #include <string.h>
960Sstevel@tonic-gate #include <openssl/crypto.h>
970Sstevel@tonic-gate #include <openssl/err.h>
980Sstevel@tonic-gate #include <openssl/rand.h>
990Sstevel@tonic-gate
1000Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
1010Sstevel@tonic-gate #define TIMES
1020Sstevel@tonic-gate #endif
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate #ifndef _IRIX
1050Sstevel@tonic-gate #include <time.h>
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate #ifdef TIMES
1080Sstevel@tonic-gate #include <sys/types.h>
1090Sstevel@tonic-gate #include <sys/times.h>
1100Sstevel@tonic-gate #endif
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /* Depending on the VMS version, the tms structure is perhaps defined.
1130Sstevel@tonic-gate The __TMS macro will show if it was. If it wasn't defined, we should
1140Sstevel@tonic-gate undefine TIMES, since that tells the rest of the program how things
1150Sstevel@tonic-gate should be handled. -- Richard Levitte */
1160Sstevel@tonic-gate #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
1170Sstevel@tonic-gate #undef TIMES
1180Sstevel@tonic-gate #endif
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate #ifndef TIMES
1210Sstevel@tonic-gate #include <sys/timeb.h>
1220Sstevel@tonic-gate #endif
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate #if defined(sun) || defined(__ultrix)
1250Sstevel@tonic-gate #define _POSIX_SOURCE
1260Sstevel@tonic-gate #include <limits.h>
1270Sstevel@tonic-gate #include <sys/param.h>
1280Sstevel@tonic-gate #endif
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate #include <openssl/bn.h>
1310Sstevel@tonic-gate #include <openssl/x509.h>
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /* The following if from times(3) man page. It may need to be changed */
1340Sstevel@tonic-gate #ifndef HZ
1350Sstevel@tonic-gate # ifndef CLK_TCK
1360Sstevel@tonic-gate # ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
1370Sstevel@tonic-gate # define HZ 100.0
1380Sstevel@tonic-gate # else /* _BSD_CLK_TCK_ */
1390Sstevel@tonic-gate # define HZ ((double)_BSD_CLK_TCK_)
1400Sstevel@tonic-gate # endif
1410Sstevel@tonic-gate # else /* CLK_TCK */
1420Sstevel@tonic-gate # define HZ ((double)CLK_TCK)
1430Sstevel@tonic-gate # endif
1440Sstevel@tonic-gate #endif
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate #undef BUFSIZE
1470Sstevel@tonic-gate #define BUFSIZE ((long)1024*8)
1480Sstevel@tonic-gate int run=0;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate static double Time_F(int s);
1510Sstevel@tonic-gate #define START 0
1520Sstevel@tonic-gate #define STOP 1
1530Sstevel@tonic-gate
Time_F(int s)1540Sstevel@tonic-gate static double Time_F(int s)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate double ret;
1570Sstevel@tonic-gate #ifdef TIMES
1580Sstevel@tonic-gate static struct tms tstart,tend;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (s == START)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate times(&tstart);
1630Sstevel@tonic-gate return(0);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate else
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate times(&tend);
1680Sstevel@tonic-gate ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
1690Sstevel@tonic-gate return((ret < 1e-3)?1e-3:ret);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate #else /* !times() */
1720Sstevel@tonic-gate static struct timeb tstart,tend;
1730Sstevel@tonic-gate long i;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate if (s == START)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate ftime(&tstart);
1780Sstevel@tonic-gate return(0);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate else
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate ftime(&tend);
1830Sstevel@tonic-gate i=(long)tend.millitm-(long)tstart.millitm;
1840Sstevel@tonic-gate ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
1850Sstevel@tonic-gate return((ret < 0.001)?0.001:ret);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate #endif
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate #define NUM_SIZES 7
1910Sstevel@tonic-gate #if NUM_START > NUM_SIZES
1920Sstevel@tonic-gate # error "NUM_START > NUM_SIZES"
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate static int sizes[NUM_SIZES]={128,256,512,1024,2048,4096,8192};
1950Sstevel@tonic-gate static int mul_c[NUM_SIZES]={8*8*8*8*8*8,8*8*8*8*8,8*8*8*8,8*8*8,8*8,8,1};
1960Sstevel@tonic-gate /*static int sizes[NUM_SIZES]={59,179,299,419,539}; */
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate #define RAND_SEED(string) { const char str[] = string; RAND_seed(string, sizeof str); }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate void do_mul_exp(BIGNUM *r,BIGNUM *a,BIGNUM *b,BIGNUM *c,BN_CTX *ctx);
2010Sstevel@tonic-gate
main(int argc,char ** argv)2020Sstevel@tonic-gate int main(int argc, char **argv)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate BN_CTX *ctx;
2050Sstevel@tonic-gate BIGNUM *a,*b,*c,*r;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate #if 1
2080Sstevel@tonic-gate if (!CRYPTO_set_mem_debug_functions(0,0,0,0,0))
2090Sstevel@tonic-gate abort();
2100Sstevel@tonic-gate #endif
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate ctx=BN_CTX_new();
2130Sstevel@tonic-gate a=BN_new();
2140Sstevel@tonic-gate b=BN_new();
2150Sstevel@tonic-gate c=BN_new();
2160Sstevel@tonic-gate r=BN_new();
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate while (!RAND_status())
2190Sstevel@tonic-gate /* not enough bits */
2200Sstevel@tonic-gate RAND_SEED("I demand a manual recount!");
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate do_mul_exp(r,a,b,c,ctx);
2230Sstevel@tonic-gate return 0;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
do_mul_exp(BIGNUM * r,BIGNUM * a,BIGNUM * b,BIGNUM * c,BN_CTX * ctx)2260Sstevel@tonic-gate void do_mul_exp(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate int i,k;
2290Sstevel@tonic-gate double tm;
2300Sstevel@tonic-gate long num;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate num=BASENUM;
2330Sstevel@tonic-gate for (i=NUM_START; i<NUM_SIZES; i++)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate #ifdef C_PRIME
2360Sstevel@tonic-gate # ifdef TEST_SQRT
2370Sstevel@tonic-gate if (!BN_set_word(a, 64)) goto err;
2380Sstevel@tonic-gate if (!BN_set_word(b, P_MOD_64)) goto err;
2390Sstevel@tonic-gate # define ADD a
2400Sstevel@tonic-gate # define REM b
2410Sstevel@tonic-gate # else
2420Sstevel@tonic-gate # define ADD NULL
2430Sstevel@tonic-gate # define REM NULL
2440Sstevel@tonic-gate # endif
2450Sstevel@tonic-gate if (!BN_generate_prime(c,sizes[i],0,ADD,REM,genprime_cb,NULL)) goto err;
2460Sstevel@tonic-gate putc('\n', stderr);
2470Sstevel@tonic-gate fflush(stderr);
2480Sstevel@tonic-gate #endif
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate for (k=0; k<num; k++)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate if (k%50 == 0) /* Average over num/50 different choices of random numbers. */
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate if (!BN_pseudo_rand(a,sizes[i],1,0)) goto err;
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate if (!BN_pseudo_rand(b,sizes[i],1,0)) goto err;
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate #ifndef C_PRIME
2590Sstevel@tonic-gate if (!BN_pseudo_rand(c,sizes[i],1,1)) goto err;
2600Sstevel@tonic-gate #endif
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate #ifdef TEST_SQRT
2630Sstevel@tonic-gate if (!BN_mod_sqr(a,a,c,ctx)) goto err;
2640Sstevel@tonic-gate if (!BN_mod_sqr(b,b,c,ctx)) goto err;
2650Sstevel@tonic-gate #else
2660Sstevel@tonic-gate if (!BN_nnmod(a,a,c,ctx)) goto err;
2670Sstevel@tonic-gate if (!BN_nnmod(b,b,c,ctx)) goto err;
2680Sstevel@tonic-gate #endif
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate if (k == 0)
2710Sstevel@tonic-gate Time_F(START);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate #if defined(TEST_EXP)
2750Sstevel@tonic-gate if (!BN_mod_exp(r,a,b,c,ctx)) goto err;
2760Sstevel@tonic-gate #elif defined(TEST_MUL)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate int i = 0;
2790Sstevel@tonic-gate for (i = 0; i < 50; i++)
2800Sstevel@tonic-gate if (!BN_mod_mul(r,a,b,c,ctx)) goto err;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate #elif defined(TEST_SQR)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate int i = 0;
2850Sstevel@tonic-gate for (i = 0; i < 50; i++)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate if (!BN_mod_sqr(r,a,c,ctx)) goto err;
2880Sstevel@tonic-gate if (!BN_mod_sqr(r,b,c,ctx)) goto err;
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate #elif defined(TEST_GCD)
2920Sstevel@tonic-gate if (!BN_gcd(r,a,b,ctx)) goto err;
2930Sstevel@tonic-gate if (!BN_gcd(r,b,c,ctx)) goto err;
2940Sstevel@tonic-gate if (!BN_gcd(r,c,a,ctx)) goto err;
2950Sstevel@tonic-gate #elif defined(TEST_KRON)
2960Sstevel@tonic-gate if (-2 == BN_kronecker(a,b,ctx)) goto err;
2970Sstevel@tonic-gate if (-2 == BN_kronecker(b,c,ctx)) goto err;
2980Sstevel@tonic-gate if (-2 == BN_kronecker(c,a,ctx)) goto err;
2990Sstevel@tonic-gate #elif defined(TEST_INV)
3000Sstevel@tonic-gate if (!BN_mod_inverse(r,a,c,ctx)) goto err;
3010Sstevel@tonic-gate if (!BN_mod_inverse(r,b,c,ctx)) goto err;
3020Sstevel@tonic-gate #else /* TEST_SQRT */
3030Sstevel@tonic-gate if (!BN_mod_sqrt(r,a,c,ctx)) goto err;
3040Sstevel@tonic-gate if (!BN_mod_sqrt(r,b,c,ctx)) goto err;
3050Sstevel@tonic-gate #endif
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate tm=Time_F(STOP);
3080Sstevel@tonic-gate printf(
3090Sstevel@tonic-gate #if defined(TEST_EXP)
3100Sstevel@tonic-gate "modexp %4d ^ %4d %% %4d"
3110Sstevel@tonic-gate #elif defined(TEST_MUL)
3120Sstevel@tonic-gate "50*modmul %4d %4d %4d"
3130Sstevel@tonic-gate #elif defined(TEST_SQR)
3140Sstevel@tonic-gate "100*modsqr %4d %4d %4d"
3150Sstevel@tonic-gate #elif defined(TEST_GCD)
3160Sstevel@tonic-gate "3*gcd %4d %4d %4d"
3170Sstevel@tonic-gate #elif defined(TEST_KRON)
3180Sstevel@tonic-gate "3*kronecker %4d %4d %4d"
3190Sstevel@tonic-gate #elif defined(TEST_INV)
3200Sstevel@tonic-gate "2*inv %4d %4d mod %4d"
3210Sstevel@tonic-gate #else /* TEST_SQRT */
3220Sstevel@tonic-gate "2*sqrt [prime == %d (mod 64)] %4d %4d mod %4d"
3230Sstevel@tonic-gate #endif
324*2139Sjp161948 " -> %8.6fms %5.1f (%ld)\n",
3250Sstevel@tonic-gate #ifdef TEST_SQRT
3260Sstevel@tonic-gate P_MOD_64,
3270Sstevel@tonic-gate #endif
3280Sstevel@tonic-gate sizes[i],sizes[i],sizes[i],tm*1000.0/num,tm*mul_c[i]/num, num);
3290Sstevel@tonic-gate num/=7;
3300Sstevel@tonic-gate if (num <= 0) num=1;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate return;
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate err:
3350Sstevel@tonic-gate ERR_print_errors_fp(stderr);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate #ifdef C_PRIME
genprime_cb(int p,int n,void * arg)3400Sstevel@tonic-gate static void genprime_cb(int p, int n, void *arg)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate char c='*';
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate if (p == 0) c='.';
3450Sstevel@tonic-gate if (p == 1) c='+';
3460Sstevel@tonic-gate if (p == 2) c='*';
3470Sstevel@tonic-gate if (p == 3) c='\n';
3480Sstevel@tonic-gate putc(c, stderr);
3490Sstevel@tonic-gate fflush(stderr);
3500Sstevel@tonic-gate (void)n;
3510Sstevel@tonic-gate (void)arg;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate #endif
354