1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* unused */ 2656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* crypto/bn/expspeed.c */ 4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * All rights reserved. 6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This package is an SSL implementation written 8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * by Eric Young (eay@cryptsoft.com). 9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The implementation was written so as to conform with Netscapes SSL. 10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This library is free for commercial and non-commercial use as long as 12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the following conditions are aheared to. The following conditions 13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * apply to all code found in this distribution, be it the RC4, RSA, 14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * lhash, DES, etc., code; not just the SSL code. The SSL documentation 15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * included with this distribution is covered by the same copyright terms 16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * except that the holder is Tim Hudson (tjh@cryptsoft.com). 17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright remains Eric Young's, and as such any Copyright notices in 19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the code are not to be removed. 20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * If this package is used in a product, Eric Young should be given attribution 21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * as the author of the parts of the library used. 22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This can be in the form of a textual message at program startup or 23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * in documentation (online or textual) provided with the package. 24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without 26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions 27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met: 28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the copyright 29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * notice, this list of conditions and the following disclaimer. 30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright 31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * notice, this list of conditions and the following disclaimer in the 32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * documentation and/or other materials provided with the distribution. 33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this software 34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * must display the following acknowledgement: 35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * "This product includes cryptographic software written by 36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Eric Young (eay@cryptsoft.com)" 37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The word 'cryptographic' can be left out if the rouines from the library 38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * being used are not cryptographic related :-). 39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. If you include any Windows specific code (or a derivative thereof) from 40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the apps directory (application code) you must include an acknowledgement: 41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SUCH DAMAGE. 54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The licence and distribution terms for any publically available version or 56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * derivative of this code cannot be changed. i.e. this code cannot simply be 57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * copied and put under another distribution licence 58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * [including the GNU Public Licence.] 59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */ 60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* most of this code has been pilfered from my libdes speed.c program */ 62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define BASENUM 5000 64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define NUM_START 0 65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* determine timings for modexp, modmul, modsqr, gcd, Kronecker symbol, 68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modular inverse, or modular square roots */ 69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define TEST_EXP 70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef TEST_MUL 71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef TEST_SQR 72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef TEST_GCD 73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef TEST_KRON 74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef TEST_INV 75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef TEST_SQRT 76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define P_MOD_64 9 /* least significant 6 bits for prime to be used for BN_sqrt timings */ 77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(TEST_EXP) + defined(TEST_MUL) + defined(TEST_SQR) + defined(TEST_GCD) + defined(TEST_KRON) + defined(TEST_INV) +defined(TEST_SQRT) != 1 79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# error "choose one test" 80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(TEST_INV) || defined(TEST_SQRT) 83656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define C_PRIME 84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic void genprime_cb(int p, int n, void *arg); 85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 88656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef PROG 90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define PROG bnspeed_main 91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stdio.h> 93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stdlib.h> 94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <signal.h> 95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <string.h> 96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/crypto.h> 97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/err.h> 98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/rand.h> 99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX) 101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define TIMES 102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef _IRIX 105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <time.h> 106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifdef TIMES 108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <sys/types.h> 109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <sys/times.h> 110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Depending on the VMS version, the tms structure is perhaps defined. 113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project The __TMS macro will show if it was. If it wasn't defined, we should 114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project undefine TIMES, since that tells the rest of the program how things 115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project should be handled. -- Richard Levitte */ 116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS) 117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef TIMES 118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef TIMES 121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <sys/timeb.h> 122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(sun) || defined(__ultrix) 125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define _POSIX_SOURCE 126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <limits.h> 127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <sys/param.h> 128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/bn.h> 131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/x509.h> 132656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 133656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* The following if from times(3) man page. It may need to be changed */ 134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef HZ 135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# ifndef CLK_TCK 136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# ifndef _BSD_CLK_TCK_ /* FreeBSD hack */ 137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define HZ 100.0 138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# else /* _BSD_CLK_TCK_ */ 139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define HZ ((double)_BSD_CLK_TCK_) 140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# endif 141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# else /* CLK_TCK */ 142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define HZ ((double)CLK_TCK) 143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# endif 144656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 145656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 146656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#undef BUFSIZE 147656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define BUFSIZE ((long)1024*8) 148656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint run=0; 149656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 150656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic double Time_F(int s); 151656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define START 0 152656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define STOP 1 153656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 154656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic double Time_F(int s) 155656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 156656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project double ret; 157656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifdef TIMES 158656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project static struct tms tstart,tend; 159656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 160656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (s == START) 161656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 162656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project times(&tstart); 163656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(0); 164656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 165656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 166656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 167656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project times(&tend); 168656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ; 169656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return((ret < 1e-3)?1e-3:ret); 170656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 171656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#else /* !times() */ 172656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project static struct timeb tstart,tend; 173656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project long i; 174656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 175656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (s == START) 176656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 177656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ftime(&tstart); 178656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(0); 179656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 180656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 181656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 182656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ftime(&tend); 183656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project i=(long)tend.millitm-(long)tstart.millitm; 184656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ret=((double)(tend.time-tstart.time))+((double)i)/1000.0; 185656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return((ret < 0.001)?0.001:ret); 186656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 187656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 188656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 189656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 190656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define NUM_SIZES 7 191656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if NUM_START > NUM_SIZES 192656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# error "NUM_START > NUM_SIZES" 193656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 194656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic int sizes[NUM_SIZES]={128,256,512,1024,2048,4096,8192}; 195656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic 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}; 196656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/*static int sizes[NUM_SIZES]={59,179,299,419,539}; */ 197656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 198656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#define RAND_SEED(string) { const char str[] = string; RAND_seed(string, sizeof str); } 199656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 200656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid do_mul_exp(BIGNUM *r,BIGNUM *a,BIGNUM *b,BIGNUM *c,BN_CTX *ctx); 201656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 202656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint main(int argc, char **argv) 203656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 204656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BN_CTX *ctx; 205656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project BIGNUM *a,*b,*c,*r; 206656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 207656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if 1 208656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!CRYPTO_set_mem_debug_functions(0,0,0,0,0)) 209656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project abort(); 210656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 211656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 212656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ctx=BN_CTX_new(); 213656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project a=BN_new(); 214656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project b=BN_new(); 215656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project c=BN_new(); 216656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project r=BN_new(); 217656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 218656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project while (!RAND_status()) 219656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project /* not enough bits */ 220656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project RAND_SEED("I demand a manual recount!"); 221656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 222656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project do_mul_exp(r,a,b,c,ctx); 223656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return 0; 224656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 225656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 226656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid do_mul_exp(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx) 227656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 228656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int i,k; 229656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project double tm; 230656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project long num; 231656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 232656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project num=BASENUM; 233656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project for (i=NUM_START; i<NUM_SIZES; i++) 234656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 235656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifdef C_PRIME 236656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# ifdef TEST_SQRT 237656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_set_word(a, 64)) goto err; 238656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_set_word(b, P_MOD_64)) goto err; 239656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define ADD a 240656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define REM b 241656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# else 242656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define ADD NULL 243656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# define REM NULL 244656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project# endif 245656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_generate_prime(c,sizes[i],0,ADD,REM,genprime_cb,NULL)) goto err; 246656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project putc('\n', stderr); 247656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project fflush(stderr); 248656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 249656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 250656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project for (k=0; k<num; k++) 251656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 252656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (k%50 == 0) /* Average over num/50 different choices of random numbers. */ 253656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 254656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_pseudo_rand(a,sizes[i],1,0)) goto err; 255656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 256656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_pseudo_rand(b,sizes[i],1,0)) goto err; 257656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 258656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifndef C_PRIME 259656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_pseudo_rand(c,sizes[i],1,1)) goto err; 260656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 261656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 262656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifdef TEST_SQRT 263656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_sqr(a,a,c,ctx)) goto err; 264656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_sqr(b,b,c,ctx)) goto err; 265656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#else 266656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_nnmod(a,a,c,ctx)) goto err; 267656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_nnmod(b,b,c,ctx)) goto err; 268656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 269656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 270656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (k == 0) 271656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project Time_F(START); 272656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 273656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 274656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(TEST_EXP) 275656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_exp(r,a,b,c,ctx)) goto err; 276656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_MUL) 277656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 278656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int i = 0; 279656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project for (i = 0; i < 50; i++) 280656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_mul(r,a,b,c,ctx)) goto err; 281656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 282656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_SQR) 283656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 284656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int i = 0; 285656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project for (i = 0; i < 50; i++) 286656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 287656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_sqr(r,a,c,ctx)) goto err; 288656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_sqr(r,b,c,ctx)) goto err; 289656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 290656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 291656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_GCD) 292656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_gcd(r,a,b,ctx)) goto err; 293656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_gcd(r,b,c,ctx)) goto err; 294656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_gcd(r,c,a,ctx)) goto err; 295656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_KRON) 296656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (-2 == BN_kronecker(a,b,ctx)) goto err; 297656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (-2 == BN_kronecker(b,c,ctx)) goto err; 298656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (-2 == BN_kronecker(c,a,ctx)) goto err; 299656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_INV) 300656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_inverse(r,a,c,ctx)) goto err; 301656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_inverse(r,b,c,ctx)) goto err; 302656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#else /* TEST_SQRT */ 303656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_sqrt(r,a,c,ctx)) goto err; 304656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!BN_mod_sqrt(r,b,c,ctx)) goto err; 305656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 306656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 307656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project tm=Time_F(STOP); 308656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project printf( 309656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#if defined(TEST_EXP) 310656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "modexp %4d ^ %4d %% %4d" 311656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_MUL) 312656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "50*modmul %4d %4d %4d" 313656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_SQR) 314656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "100*modsqr %4d %4d %4d" 315656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_GCD) 316656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "3*gcd %4d %4d %4d" 317656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_KRON) 318656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "3*kronecker %4d %4d %4d" 319656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#elif defined(TEST_INV) 320656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "2*inv %4d %4d mod %4d" 321656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#else /* TEST_SQRT */ 322656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project "2*sqrt [prime == %d (mod 64)] %4d %4d mod %4d" 323656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 324656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project " -> %8.6fms %5.1f (%ld)\n", 325656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifdef TEST_SQRT 326656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project P_MOD_64, 327656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 328656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project sizes[i],sizes[i],sizes[i],tm*1000.0/num,tm*mul_c[i]/num, num); 329656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project num/=7; 330656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (num <= 0) num=1; 331656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 332656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return; 333656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 334656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project err: 335656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ERR_print_errors_fp(stderr); 336656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 337656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 338656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 339656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#ifdef C_PRIME 340656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic void genprime_cb(int p, int n, void *arg) 341656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 342656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project char c='*'; 343656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 344656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (p == 0) c='.'; 345656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (p == 1) c='+'; 346656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (p == 2) c='*'; 347656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (p == 3) c='\n'; 348656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project putc(c, stderr); 349656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project fflush(stderr); 350656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project (void)n; 351656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project (void)arg; 352656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 353656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#endif 354