xref: /openbsd-src/regress/lib/libcrypto/rand/randtest.c (revision 657b055c4f2d15cc79398afc5793a57a63e700c8)
1*657b055cStb /*	$OpenBSD: randtest.c,v 1.3 2018/07/17 17:06:49 tb Exp $	*/
23c6bd008Smiod /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33c6bd008Smiod  * All rights reserved.
43c6bd008Smiod  *
53c6bd008Smiod  * This package is an SSL implementation written
63c6bd008Smiod  * by Eric Young (eay@cryptsoft.com).
73c6bd008Smiod  * The implementation was written so as to conform with Netscapes SSL.
83c6bd008Smiod  *
93c6bd008Smiod  * This library is free for commercial and non-commercial use as long as
103c6bd008Smiod  * the following conditions are aheared to.  The following conditions
113c6bd008Smiod  * apply to all code found in this distribution, be it the RC4, RSA,
123c6bd008Smiod  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
133c6bd008Smiod  * included with this distribution is covered by the same copyright terms
143c6bd008Smiod  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
153c6bd008Smiod  *
163c6bd008Smiod  * Copyright remains Eric Young's, and as such any Copyright notices in
173c6bd008Smiod  * the code are not to be removed.
183c6bd008Smiod  * If this package is used in a product, Eric Young should be given attribution
193c6bd008Smiod  * as the author of the parts of the library used.
203c6bd008Smiod  * This can be in the form of a textual message at program startup or
213c6bd008Smiod  * in documentation (online or textual) provided with the package.
223c6bd008Smiod  *
233c6bd008Smiod  * Redistribution and use in source and binary forms, with or without
243c6bd008Smiod  * modification, are permitted provided that the following conditions
253c6bd008Smiod  * are met:
263c6bd008Smiod  * 1. Redistributions of source code must retain the copyright
273c6bd008Smiod  *    notice, this list of conditions and the following disclaimer.
283c6bd008Smiod  * 2. Redistributions in binary form must reproduce the above copyright
293c6bd008Smiod  *    notice, this list of conditions and the following disclaimer in the
303c6bd008Smiod  *    documentation and/or other materials provided with the distribution.
313c6bd008Smiod  * 3. All advertising materials mentioning features or use of this software
323c6bd008Smiod  *    must display the following acknowledgement:
333c6bd008Smiod  *    "This product includes cryptographic software written by
343c6bd008Smiod  *     Eric Young (eay@cryptsoft.com)"
353c6bd008Smiod  *    The word 'cryptographic' can be left out if the rouines from the library
363c6bd008Smiod  *    being used are not cryptographic related :-).
373c6bd008Smiod  * 4. If you include any Windows specific code (or a derivative thereof) from
383c6bd008Smiod  *    the apps directory (application code) you must include an acknowledgement:
393c6bd008Smiod  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
403c6bd008Smiod  *
413c6bd008Smiod  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
423c6bd008Smiod  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
433c6bd008Smiod  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
443c6bd008Smiod  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
453c6bd008Smiod  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
463c6bd008Smiod  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
473c6bd008Smiod  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
483c6bd008Smiod  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
493c6bd008Smiod  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
503c6bd008Smiod  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
513c6bd008Smiod  * SUCH DAMAGE.
523c6bd008Smiod  *
533c6bd008Smiod  * The licence and distribution terms for any publically available version or
543c6bd008Smiod  * derivative of this code cannot be changed.  i.e. this code cannot simply be
553c6bd008Smiod  * copied and put under another distribution licence
563c6bd008Smiod  * [including the GNU Public Licence.]
573c6bd008Smiod  */
583c6bd008Smiod 
593c6bd008Smiod #include <stdio.h>
603c6bd008Smiod #include <stdlib.h>
612212b4adSjsing 
622212b4adSjsing #undef LIBRESSL_INTERNAL	/* Needed to get RAND_pseudo_bytes(). */
633c6bd008Smiod #include <openssl/rand.h>
643c6bd008Smiod 
653c6bd008Smiod /* some FIPS 140-1 random number test */
663c6bd008Smiod /* some simple tests */
673c6bd008Smiod 
main(int argc,char ** argv)683c6bd008Smiod int main(int argc,char **argv)
693c6bd008Smiod 	{
703c6bd008Smiod 	unsigned char buf[2500];
713c6bd008Smiod 	int i,j,k,s,sign,nsign,err=0;
723c6bd008Smiod 	unsigned long n1;
733c6bd008Smiod 	unsigned long n2[16];
743c6bd008Smiod 	unsigned long runs[2][34];
753c6bd008Smiod 	/*double d; */
763c6bd008Smiod 	long d;
773c6bd008Smiod 
783c6bd008Smiod 	i = RAND_pseudo_bytes(buf,2500);
793c6bd008Smiod 	if (i < 0)
803c6bd008Smiod 		{
813c6bd008Smiod 		printf ("init failed, the rand method is not properly installed\n");
823c6bd008Smiod 		err++;
833c6bd008Smiod 		goto err;
843c6bd008Smiod 		}
853c6bd008Smiod 
863c6bd008Smiod 	n1=0;
873c6bd008Smiod 	for (i=0; i<16; i++) n2[i]=0;
883c6bd008Smiod 	for (i=0; i<34; i++) runs[0][i]=runs[1][i]=0;
893c6bd008Smiod 
903c6bd008Smiod 	/* test 1 and 2 */
913c6bd008Smiod 	sign=0;
923c6bd008Smiod 	nsign=0;
933c6bd008Smiod 	for (i=0; i<2500; i++)
943c6bd008Smiod 		{
953c6bd008Smiod 		j=buf[i];
963c6bd008Smiod 
973c6bd008Smiod 		n2[j&0x0f]++;
983c6bd008Smiod 		n2[(j>>4)&0x0f]++;
993c6bd008Smiod 
1003c6bd008Smiod 		for (k=0; k<8; k++)
1013c6bd008Smiod 			{
1023c6bd008Smiod 			s=(j&0x01);
1033c6bd008Smiod 			if (s == sign)
1043c6bd008Smiod 				nsign++;
1053c6bd008Smiod 			else
1063c6bd008Smiod 				{
1073c6bd008Smiod 				if (nsign > 34) nsign=34;
1083c6bd008Smiod 				if (nsign != 0)
1093c6bd008Smiod 					{
1103c6bd008Smiod 					runs[sign][nsign-1]++;
1113c6bd008Smiod 					if (nsign > 6)
1123c6bd008Smiod 						runs[sign][5]++;
1133c6bd008Smiod 					}
1143c6bd008Smiod 				sign=s;
1153c6bd008Smiod 				nsign=1;
1163c6bd008Smiod 				}
1173c6bd008Smiod 
1183c6bd008Smiod 			if (s) n1++;
1193c6bd008Smiod 			j>>=1;
1203c6bd008Smiod 			}
1213c6bd008Smiod 		}
1223c6bd008Smiod 		if (nsign > 34) nsign=34;
1233c6bd008Smiod 		if (nsign != 0) runs[sign][nsign-1]++;
1243c6bd008Smiod 
1253c6bd008Smiod 	/* test 1 */
1263c6bd008Smiod 	if (!((9654 < n1) && (n1 < 10346)))
1273c6bd008Smiod 		{
1283c6bd008Smiod 		printf("test 1 failed, X=%lu\n",n1);
1293c6bd008Smiod 		err++;
1303c6bd008Smiod 		}
1313c6bd008Smiod 	printf("test 1 done\n");
1323c6bd008Smiod 
1333c6bd008Smiod 	/* test 2 */
1343c6bd008Smiod 	d=0;
1353c6bd008Smiod 	for (i=0; i<16; i++)
1363c6bd008Smiod 		d+=n2[i]*n2[i];
1373c6bd008Smiod 	d=(d*8)/25-500000;
1383c6bd008Smiod 	if (!((103 < d) && (d < 5740)))
1393c6bd008Smiod 		{
1403c6bd008Smiod 		printf("test 2 failed, X=%ld.%02ld\n",d/100L,d%100L);
1413c6bd008Smiod 		err++;
1423c6bd008Smiod 		}
1433c6bd008Smiod 	printf("test 2 done\n");
1443c6bd008Smiod 
1453c6bd008Smiod 	/* test 3 */
1463c6bd008Smiod 	for (i=0; i<2; i++)
1473c6bd008Smiod 		{
1483c6bd008Smiod 		if (!((2267 < runs[i][0]) && (runs[i][0] < 2733)))
1493c6bd008Smiod 			{
1503c6bd008Smiod 			printf("test 3 failed, bit=%d run=%d num=%lu\n",
1513c6bd008Smiod 				i,1,runs[i][0]);
1523c6bd008Smiod 			err++;
1533c6bd008Smiod 			}
1543c6bd008Smiod 		if (!((1079 < runs[i][1]) && (runs[i][1] < 1421)))
1553c6bd008Smiod 			{
1563c6bd008Smiod 			printf("test 3 failed, bit=%d run=%d num=%lu\n",
1573c6bd008Smiod 				i,2,runs[i][1]);
1583c6bd008Smiod 			err++;
1593c6bd008Smiod 			}
1603c6bd008Smiod 		if (!(( 502 < runs[i][2]) && (runs[i][2] <  748)))
1613c6bd008Smiod 			{
1623c6bd008Smiod 			printf("test 3 failed, bit=%d run=%d num=%lu\n",
1633c6bd008Smiod 				i,3,runs[i][2]);
1643c6bd008Smiod 			err++;
1653c6bd008Smiod 			}
1663c6bd008Smiod 		if (!(( 223 < runs[i][3]) && (runs[i][3] <  402)))
1673c6bd008Smiod 			{
1683c6bd008Smiod 			printf("test 3 failed, bit=%d run=%d num=%lu\n",
1693c6bd008Smiod 				i,4,runs[i][3]);
1703c6bd008Smiod 			err++;
1713c6bd008Smiod 			}
1723c6bd008Smiod 		if (!((  90 < runs[i][4]) && (runs[i][4] <  223)))
1733c6bd008Smiod 			{
1743c6bd008Smiod 			printf("test 3 failed, bit=%d run=%d num=%lu\n",
1753c6bd008Smiod 				i,5,runs[i][4]);
1763c6bd008Smiod 			err++;
1773c6bd008Smiod 			}
1783c6bd008Smiod 		if (!((  90 < runs[i][5]) && (runs[i][5] <  223)))
1793c6bd008Smiod 			{
1803c6bd008Smiod 			printf("test 3 failed, bit=%d run=%d num=%lu\n",
1813c6bd008Smiod 				i,6,runs[i][5]);
1823c6bd008Smiod 			err++;
1833c6bd008Smiod 			}
1843c6bd008Smiod 		}
1853c6bd008Smiod 	printf("test 3 done\n");
1863c6bd008Smiod 
1873c6bd008Smiod 	/* test 4 */
1883c6bd008Smiod 	if (runs[0][33] != 0)
1893c6bd008Smiod 		{
1903c6bd008Smiod 		printf("test 4 failed, bit=%d run=%d num=%lu\n",
1913c6bd008Smiod 			0,34,runs[0][33]);
1923c6bd008Smiod 		err++;
1933c6bd008Smiod 		}
1943c6bd008Smiod 	if (runs[1][33] != 0)
1953c6bd008Smiod 		{
1963c6bd008Smiod 		printf("test 4 failed, bit=%d run=%d num=%lu\n",
1973c6bd008Smiod 			1,34,runs[1][33]);
1983c6bd008Smiod 		err++;
1993c6bd008Smiod 		}
2003c6bd008Smiod 	printf("test 4 done\n");
2013c6bd008Smiod  err:
2023c6bd008Smiod 	err=((err)?1:0);
2033c6bd008Smiod 	exit(err);
2043c6bd008Smiod 	}
205