155714Skris/* crypto/rand/randtest.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280304Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280304Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <stdlib.h> 6155714Skris#include <openssl/rand.h> 6255714Skris 63109998Smarkm#include "../e_os.h" 64109998Smarkm 6555714Skris/* some FIPS 140-1 random number test */ 6655714Skris/* some simple tests */ 6755714Skris 68280304Sjkimint main(int argc, char **argv) 69280304Sjkim{ 70280304Sjkim unsigned char buf[2500]; 71280304Sjkim int i, j, k, s, sign, nsign, err = 0; 72280304Sjkim unsigned long n1; 73280304Sjkim unsigned long n2[16]; 74280304Sjkim unsigned long runs[2][34]; 75280304Sjkim /* 76280304Sjkim * double d; 77280304Sjkim */ 78280304Sjkim long d; 7955714Skris 80280304Sjkim i = RAND_pseudo_bytes(buf, 2500); 81280304Sjkim if (i < 0) { 82280304Sjkim printf("init failed, the rand method is not properly installed\n"); 83280304Sjkim err++; 84280304Sjkim goto err; 85280304Sjkim } 8655714Skris 87280304Sjkim n1 = 0; 88280304Sjkim for (i = 0; i < 16; i++) 89280304Sjkim n2[i] = 0; 90280304Sjkim for (i = 0; i < 34; i++) 91280304Sjkim runs[0][i] = runs[1][i] = 0; 9255714Skris 93280304Sjkim /* test 1 and 2 */ 94280304Sjkim sign = 0; 95280304Sjkim nsign = 0; 96280304Sjkim for (i = 0; i < 2500; i++) { 97280304Sjkim j = buf[i]; 9855714Skris 99280304Sjkim n2[j & 0x0f]++; 100280304Sjkim n2[(j >> 4) & 0x0f]++; 10155714Skris 102280304Sjkim for (k = 0; k < 8; k++) { 103280304Sjkim s = (j & 0x01); 104280304Sjkim if (s == sign) 105280304Sjkim nsign++; 106280304Sjkim else { 107280304Sjkim if (nsign > 34) 108280304Sjkim nsign = 34; 109280304Sjkim if (nsign != 0) { 110280304Sjkim runs[sign][nsign - 1]++; 111280304Sjkim if (nsign > 6) 112280304Sjkim runs[sign][5]++; 113280304Sjkim } 114280304Sjkim sign = s; 115280304Sjkim nsign = 1; 116280304Sjkim } 11755714Skris 118280304Sjkim if (s) 119280304Sjkim n1++; 120280304Sjkim j >>= 1; 121280304Sjkim } 122280304Sjkim } 123280304Sjkim if (nsign > 34) 124280304Sjkim nsign = 34; 125280304Sjkim if (nsign != 0) 126280304Sjkim runs[sign][nsign - 1]++; 12755714Skris 128280304Sjkim /* test 1 */ 129280304Sjkim if (!((9654 < n1) && (n1 < 10346))) { 130280304Sjkim printf("test 1 failed, X=%lu\n", n1); 131280304Sjkim err++; 132280304Sjkim } 133280304Sjkim printf("test 1 done\n"); 13455714Skris 135280304Sjkim /* test 2 */ 13655714Skris#ifdef undef 137280304Sjkim d = 0; 138280304Sjkim for (i = 0; i < 16; i++) 139280304Sjkim d += n2[i] * n2[i]; 140280304Sjkim d = d * 16.0 / 5000.0 - 5000.0; 141280304Sjkim if (!((1.03 < d) && (d < 57.4))) { 142280304Sjkim printf("test 2 failed, X=%.2f\n", d); 143280304Sjkim err++; 144280304Sjkim } 14555714Skris#endif 146280304Sjkim d = 0; 147280304Sjkim for (i = 0; i < 16; i++) 148280304Sjkim d += n2[i] * n2[i]; 149280304Sjkim d = (d * 8) / 25 - 500000; 150280304Sjkim if (!((103 < d) && (d < 5740))) { 151280304Sjkim printf("test 2 failed, X=%ld.%02ld\n", d / 100L, d % 100L); 152280304Sjkim err++; 153280304Sjkim } 154280304Sjkim printf("test 2 done\n"); 15555714Skris 156280304Sjkim /* test 3 */ 157280304Sjkim for (i = 0; i < 2; i++) { 158280304Sjkim if (!((2267 < runs[i][0]) && (runs[i][0] < 2733))) { 159280304Sjkim printf("test 3 failed, bit=%d run=%d num=%lu\n", 160280304Sjkim i, 1, runs[i][0]); 161280304Sjkim err++; 162280304Sjkim } 163280304Sjkim if (!((1079 < runs[i][1]) && (runs[i][1] < 1421))) { 164280304Sjkim printf("test 3 failed, bit=%d run=%d num=%lu\n", 165280304Sjkim i, 2, runs[i][1]); 166280304Sjkim err++; 167280304Sjkim } 168280304Sjkim if (!((502 < runs[i][2]) && (runs[i][2] < 748))) { 169280304Sjkim printf("test 3 failed, bit=%d run=%d num=%lu\n", 170280304Sjkim i, 3, runs[i][2]); 171280304Sjkim err++; 172280304Sjkim } 173280304Sjkim if (!((223 < runs[i][3]) && (runs[i][3] < 402))) { 174280304Sjkim printf("test 3 failed, bit=%d run=%d num=%lu\n", 175280304Sjkim i, 4, runs[i][3]); 176280304Sjkim err++; 177280304Sjkim } 178280304Sjkim if (!((90 < runs[i][4]) && (runs[i][4] < 223))) { 179280304Sjkim printf("test 3 failed, bit=%d run=%d num=%lu\n", 180280304Sjkim i, 5, runs[i][4]); 181280304Sjkim err++; 182280304Sjkim } 183280304Sjkim if (!((90 < runs[i][5]) && (runs[i][5] < 223))) { 184280304Sjkim printf("test 3 failed, bit=%d run=%d num=%lu\n", 185280304Sjkim i, 6, runs[i][5]); 186280304Sjkim err++; 187280304Sjkim } 188280304Sjkim } 189280304Sjkim printf("test 3 done\n"); 190280304Sjkim 191280304Sjkim /* test 4 */ 192280304Sjkim if (runs[0][33] != 0) { 193280304Sjkim printf("test 4 failed, bit=%d run=%d num=%lu\n", 0, 34, runs[0][33]); 194280304Sjkim err++; 195280304Sjkim } 196280304Sjkim if (runs[1][33] != 0) { 197280304Sjkim printf("test 4 failed, bit=%d run=%d num=%lu\n", 1, 34, runs[1][33]); 198280304Sjkim err++; 199280304Sjkim } 200280304Sjkim printf("test 4 done\n"); 201109998Smarkm err: 202280304Sjkim err = ((err) ? 1 : 0); 203160814Ssimon#ifdef OPENSSL_SYS_NETWARE 204280304Sjkim if (err) 205280304Sjkim printf("ERROR: %d\n", err); 206160814Ssimon#endif 207280304Sjkim EXIT(err); 208280304Sjkim return (err); 209280304Sjkim} 210