1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/crypto_test/src/randtest.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,251 @@
1.4 +/* crypto/rand/randtest.c */
1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.6 + * All rights reserved.
1.7 + *
1.8 + * This package is an SSL implementation written
1.9 + * by Eric Young (eay@cryptsoft.com).
1.10 + * The implementation was written so as to conform with Netscapes SSL.
1.11 + *
1.12 + * This library is free for commercial and non-commercial use as long as
1.13 + * the following conditions are aheared to. The following conditions
1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
1.15 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.16 + * included with this distribution is covered by the same copyright terms
1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.18 + *
1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.20 + * the code are not to be removed.
1.21 + * If this package is used in a product, Eric Young should be given attribution
1.22 + * as the author of the parts of the library used.
1.23 + * This can be in the form of a textual message at program startup or
1.24 + * in documentation (online or textual) provided with the package.
1.25 + *
1.26 + * Redistribution and use in source and binary forms, with or without
1.27 + * modification, are permitted provided that the following conditions
1.28 + * are met:
1.29 + * 1. Redistributions of source code must retain the copyright
1.30 + * notice, this list of conditions and the following disclaimer.
1.31 + * 2. Redistributions in binary form must reproduce the above copyright
1.32 + * notice, this list of conditions and the following disclaimer in the
1.33 + * documentation and/or other materials provided with the distribution.
1.34 + * 3. All advertising materials mentioning features or use of this software
1.35 + * must display the following acknowledgement:
1.36 + * "This product includes cryptographic software written by
1.37 + * Eric Young (eay@cryptsoft.com)"
1.38 + * The word 'cryptographic' can be left out if the rouines from the library
1.39 + * being used are not cryptographic related :-).
1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.41 + * the apps directory (application code) you must include an acknowledgement:
1.42 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.43 + *
1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.47 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.54 + * SUCH DAMAGE.
1.55 + *
1.56 + * The licence and distribution terms for any publically available version or
1.57 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.58 + * copied and put under another distribution licence
1.59 + * [including the GNU Public Licence.]
1.60 + */
1.61 +/*
1.62 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.63 + */
1.64 +#include <stdio.h>
1.65 +#include <stdlib.h>
1.66 +#include <errno.h>
1.67 +#include <openssl/rand.h>
1.68 +#ifndef SYMBIAN
1.69 +#include "../e_os.h"
1.70 +#else
1.71 +#include "e_os.h"
1.72 +#endif
1.73 +#ifdef SYMBIAN
1.74 +#ifdef stdin
1.75 +#undef stdin
1.76 +#endif
1.77 +#ifdef stdout
1.78 +#undef stdout
1.79 +#endif
1.80 +#ifdef stderr
1.81 +#undef stderr
1.82 +#endif
1.83 +
1.84 +#define stdin fp_stdin
1.85 +#define stdout fp_stdout
1.86 +#define stderr fp_stderr
1.87 +
1.88 +extern FILE *fp_stdout;
1.89 +extern FILE *fp_stderr;
1.90 +#endif
1.91 +/* some FIPS 140-1 random number test */
1.92 +/* some simple tests */
1.93 +#ifndef SYMBIAN
1.94 +int main(int argc,char **argv)
1.95 +#else
1.96 +int rand_main(int argc,char **argv)
1.97 +#endif
1.98 + {
1.99 + unsigned char buf[2500];
1.100 + int i,j,k,s,sign,nsign,err=0;
1.101 + unsigned long n1;
1.102 + unsigned long n2[16];
1.103 + unsigned long runs[2][34];
1.104 + /*double d; */
1.105 + long d;
1.106 +
1.107 + i = RAND_pseudo_bytes(buf,2500);
1.108 + if(errno==ENOMEM)
1.109 + {
1.110 + return 1;
1.111 + }
1.112 +
1.113 + if (i < 0)
1.114 + {
1.115 + fprintf (stdout,"init failed, the rand method is not properly installed\n");
1.116 + err++;
1.117 + goto err;
1.118 + }
1.119 +
1.120 + n1=0;
1.121 + for (i=0; i<16; i++) n2[i]=0;
1.122 + for (i=0; i<34; i++) runs[0][i]=runs[1][i]=0;
1.123 +
1.124 + /* test 1 and 2 */
1.125 + sign=0;
1.126 + nsign=0;
1.127 + for (i=0; i<2500; i++)
1.128 + {
1.129 + j=buf[i];
1.130 +
1.131 + n2[j&0x0f]++;
1.132 + n2[(j>>4)&0x0f]++;
1.133 +
1.134 + for (k=0; k<8; k++)
1.135 + {
1.136 + s=(j&0x01);
1.137 + if (s == sign)
1.138 + nsign++;
1.139 + else
1.140 + {
1.141 + if (nsign > 34) nsign=34;
1.142 + if (nsign != 0)
1.143 + {
1.144 + runs[sign][nsign-1]++;
1.145 + if (nsign > 6)
1.146 + runs[sign][5]++;
1.147 + }
1.148 + sign=s;
1.149 + nsign=1;
1.150 + }
1.151 +
1.152 + if (s) n1++;
1.153 + j>>=1;
1.154 + }
1.155 + }
1.156 + if (nsign > 34) nsign=34;
1.157 + if (nsign != 0) runs[sign][nsign-1]++;
1.158 +
1.159 + /* test 1 */
1.160 + if (!((9654 < n1) && (n1 < 10346)))
1.161 + {
1.162 + fprintf(stdout,"test 1 failed, X=%lu\n",n1);
1.163 + err++;
1.164 + }
1.165 + fprintf(stdout,"rand test 1 done\n");
1.166 +
1.167 + /* test 2 */
1.168 +#ifdef undef
1.169 + d=0;
1.170 + for (i=0; i<16; i++)
1.171 + d+=n2[i]*n2[i];
1.172 + d=d*16.0/5000.0-5000.0;
1.173 + if (!((1.03 < d) && (d < 57.4)))
1.174 + {
1.175 + fprintf(stdout,"test 2 failed, X=%.2f\n",d);
1.176 + err++;
1.177 + }
1.178 +#endif
1.179 + d=0;
1.180 + for (i=0; i<16; i++)
1.181 + d+=n2[i]*n2[i];
1.182 + d=(d*8)/25-500000;
1.183 + if (!((103 < d) && (d < 5740)))
1.184 + {
1.185 + fprintf(stdout,"test 2 failed, X=%ld.%02ld\n",d/100L,d%100L);
1.186 + err++;
1.187 + }
1.188 + fprintf(stdout,"rand test 2 done\n");
1.189 +
1.190 + /* test 3 */
1.191 + for (i=0; i<2; i++)
1.192 + {
1.193 + if (!((2267 < runs[i][0]) && (runs[i][0] < 2733)))
1.194 + {
1.195 + fprintf(stdout,"test 3 failed, bit=%d run=%d num=%lu\n",
1.196 + i,1,runs[i][0]);
1.197 + err++;
1.198 + }
1.199 + if (!((1079 < runs[i][1]) && (runs[i][1] < 1421)))
1.200 + {
1.201 + fprintf(stdout,"test 3 failed, bit=%d run=%d num=%lu\n",
1.202 + i,2,runs[i][1]);
1.203 + err++;
1.204 + }
1.205 + if (!(( 502 < runs[i][2]) && (runs[i][2] < 748)))
1.206 + {
1.207 + fprintf(stdout,"test 3 failed, bit=%d run=%d num=%lu\n",
1.208 + i,3,runs[i][2]);
1.209 + err++;
1.210 + }
1.211 + if (!(( 223 < runs[i][3]) && (runs[i][3] < 402)))
1.212 + {
1.213 + fprintf(stdout,"test 3 failed, bit=%d run=%d num=%lu\n",
1.214 + i,4,runs[i][3]);
1.215 + err++;
1.216 + }
1.217 + if (!(( 90 < runs[i][4]) && (runs[i][4] < 223)))
1.218 + {
1.219 + fprintf(stdout,"test 3 failed, bit=%d run=%d num=%lu\n",
1.220 + i,5,runs[i][4]);
1.221 + err++;
1.222 + }
1.223 + if (!(( 90 < runs[i][5]) && (runs[i][5] < 223)))
1.224 + {
1.225 + fprintf(stdout,"test 3 failed, bit=%d run=%d num=%lu\n",
1.226 + i,6,runs[i][5]);
1.227 + err++;
1.228 + }
1.229 + }
1.230 + fprintf(stdout,"rand test 3 done\n");
1.231 +
1.232 + /* test 4 */
1.233 + if (runs[0][33] != 0)
1.234 + {
1.235 + fprintf(stdout,"test 4 failed, bit=%d run=%d num=%lu\n",
1.236 + 0,34,runs[0][33]);
1.237 + err++;
1.238 + }
1.239 + if (runs[1][33] != 0)
1.240 + {
1.241 + fprintf(stdout,"test 4 failed, bit=%d run=%d num=%lu\n",
1.242 + 1,34,runs[1][33]);
1.243 + err++;
1.244 + }
1.245 + fprintf(stdout,"rand test 4 done\n");
1.246 + err:
1.247 + err=((err)?1:0);
1.248 +#ifdef OPENSSL_SYS_NETWARE
1.249 + if (err) fprintf(stdout,"ERROR: %d\n", err);
1.250 +#endif
1.251 + fprintf(stdout,"Test case passed\n");
1.252 + //EXIT(err);
1.253 + return(err);
1.254 + }