xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_rand.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bn/bn_rand.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate  * are met:
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
710Sstevel@tonic-gate  *    distribution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate  *    software must display the following acknowledgment:
750Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate  *    endorse or promote products derived from this software without
800Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
810Sstevel@tonic-gate  *    openssl-core@openssl.org.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate  *    permission of the OpenSSL Project.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate  *    acknowledgment:
890Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate  * ====================================================================
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
1080Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include <time.h>
1140Sstevel@tonic-gate #include "cryptlib.h"
1150Sstevel@tonic-gate #include "bn_lcl.h"
1160Sstevel@tonic-gate #include <openssl/rand.h>
1170Sstevel@tonic-gate 
bnrand(int pseudorand,BIGNUM * rnd,int bits,int top,int bottom)1180Sstevel@tonic-gate static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
1190Sstevel@tonic-gate 	{
1200Sstevel@tonic-gate 	unsigned char *buf=NULL;
1210Sstevel@tonic-gate 	int ret=0,bit,bytes,mask;
1220Sstevel@tonic-gate 	time_t tim;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (bits == 0)
1250Sstevel@tonic-gate 		{
1260Sstevel@tonic-gate 		BN_zero(rnd);
1270Sstevel@tonic-gate 		return 1;
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	bytes=(bits+7)/8;
1310Sstevel@tonic-gate 	bit=(bits-1)%8;
1320Sstevel@tonic-gate 	mask=0xff<<(bit+1);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	buf=(unsigned char *)OPENSSL_malloc(bytes);
1350Sstevel@tonic-gate 	if (buf == NULL)
1360Sstevel@tonic-gate 		{
137*2139Sjp161948 		BNerr(BN_F_BNRAND,ERR_R_MALLOC_FAILURE);
1380Sstevel@tonic-gate 		goto err;
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	/* make a random number and set the top and bottom bits */
1420Sstevel@tonic-gate 	time(&tim);
143*2139Sjp161948 	RAND_add(&tim,sizeof(tim),0.0);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (pseudorand)
1460Sstevel@tonic-gate 		{
1470Sstevel@tonic-gate 		if (RAND_pseudo_bytes(buf, bytes) == -1)
1480Sstevel@tonic-gate 			goto err;
1490Sstevel@tonic-gate 		}
1500Sstevel@tonic-gate 	else
1510Sstevel@tonic-gate 		{
1520Sstevel@tonic-gate 		if (RAND_bytes(buf, bytes) <= 0)
1530Sstevel@tonic-gate 			goto err;
1540Sstevel@tonic-gate 		}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate #if 1
1570Sstevel@tonic-gate 	if (pseudorand == 2)
1580Sstevel@tonic-gate 		{
1590Sstevel@tonic-gate 		/* generate patterns that are more likely to trigger BN
1600Sstevel@tonic-gate 		   library bugs */
1610Sstevel@tonic-gate 		int i;
1620Sstevel@tonic-gate 		unsigned char c;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		for (i = 0; i < bytes; i++)
1650Sstevel@tonic-gate 			{
1660Sstevel@tonic-gate 			RAND_pseudo_bytes(&c, 1);
1670Sstevel@tonic-gate 			if (c >= 128 && i > 0)
1680Sstevel@tonic-gate 				buf[i] = buf[i-1];
1690Sstevel@tonic-gate 			else if (c < 42)
1700Sstevel@tonic-gate 				buf[i] = 0;
1710Sstevel@tonic-gate 			else if (c < 84)
1720Sstevel@tonic-gate 				buf[i] = 255;
1730Sstevel@tonic-gate 			}
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate #endif
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (top != -1)
1780Sstevel@tonic-gate 		{
1790Sstevel@tonic-gate 		if (top)
1800Sstevel@tonic-gate 			{
1810Sstevel@tonic-gate 			if (bit == 0)
1820Sstevel@tonic-gate 				{
1830Sstevel@tonic-gate 				buf[0]=1;
1840Sstevel@tonic-gate 				buf[1]|=0x80;
1850Sstevel@tonic-gate 				}
1860Sstevel@tonic-gate 			else
1870Sstevel@tonic-gate 				{
1880Sstevel@tonic-gate 				buf[0]|=(3<<(bit-1));
1890Sstevel@tonic-gate 				}
1900Sstevel@tonic-gate 			}
1910Sstevel@tonic-gate 		else
1920Sstevel@tonic-gate 			{
1930Sstevel@tonic-gate 			buf[0]|=(1<<bit);
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 	buf[0] &= ~mask;
1970Sstevel@tonic-gate 	if (bottom) /* set bottom bit if requested */
1980Sstevel@tonic-gate 		buf[bytes-1]|=1;
1990Sstevel@tonic-gate 	if (!BN_bin2bn(buf,bytes,rnd)) goto err;
2000Sstevel@tonic-gate 	ret=1;
2010Sstevel@tonic-gate err:
2020Sstevel@tonic-gate 	if (buf != NULL)
2030Sstevel@tonic-gate 		{
2040Sstevel@tonic-gate 		OPENSSL_cleanse(buf,bytes);
2050Sstevel@tonic-gate 		OPENSSL_free(buf);
2060Sstevel@tonic-gate 		}
207*2139Sjp161948 	bn_check_top(rnd);
2080Sstevel@tonic-gate 	return(ret);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
BN_rand(BIGNUM * rnd,int bits,int top,int bottom)2110Sstevel@tonic-gate int     BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
2120Sstevel@tonic-gate 	{
2130Sstevel@tonic-gate 	return bnrand(0, rnd, bits, top, bottom);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
BN_pseudo_rand(BIGNUM * rnd,int bits,int top,int bottom)2160Sstevel@tonic-gate int     BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
2170Sstevel@tonic-gate 	{
2180Sstevel@tonic-gate 	return bnrand(1, rnd, bits, top, bottom);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate #if 1
BN_bntest_rand(BIGNUM * rnd,int bits,int top,int bottom)2220Sstevel@tonic-gate int     BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
2230Sstevel@tonic-gate 	{
2240Sstevel@tonic-gate 	return bnrand(2, rnd, bits, top, bottom);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate #endif
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /* random number r:  0 <= r < range */
bn_rand_range(int pseudo,BIGNUM * r,BIGNUM * range)2300Sstevel@tonic-gate static int bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
2310Sstevel@tonic-gate 	{
2320Sstevel@tonic-gate 	int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand;
2330Sstevel@tonic-gate 	int n;
234*2139Sjp161948 	int count = 100;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	if (range->neg || BN_is_zero(range))
2370Sstevel@tonic-gate 		{
2380Sstevel@tonic-gate 		BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);
2390Sstevel@tonic-gate 		return 0;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	n = BN_num_bits(range); /* n > 0 */
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	/* BN_is_bit_set(range, n - 1) always holds */
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (n == 1)
247*2139Sjp161948 		BN_zero(r);
2480Sstevel@tonic-gate 	else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3))
2490Sstevel@tonic-gate 		{
2500Sstevel@tonic-gate 		/* range = 100..._2,
2510Sstevel@tonic-gate 		 * so  3*range (= 11..._2)  is exactly one bit longer than  range */
2520Sstevel@tonic-gate 		do
2530Sstevel@tonic-gate 			{
2540Sstevel@tonic-gate 			if (!bn_rand(r, n + 1, -1, 0)) return 0;
2550Sstevel@tonic-gate 			/* If  r < 3*range,  use  r := r MOD range
2560Sstevel@tonic-gate 			 * (which is either  r, r - range,  or  r - 2*range).
2570Sstevel@tonic-gate 			 * Otherwise, iterate once more.
2580Sstevel@tonic-gate 			 * Since  3*range = 11..._2, each iteration succeeds with
2590Sstevel@tonic-gate 			 * probability >= .75. */
2600Sstevel@tonic-gate 			if (BN_cmp(r ,range) >= 0)
2610Sstevel@tonic-gate 				{
2620Sstevel@tonic-gate 				if (!BN_sub(r, r, range)) return 0;
2630Sstevel@tonic-gate 				if (BN_cmp(r, range) >= 0)
2640Sstevel@tonic-gate 					if (!BN_sub(r, r, range)) return 0;
2650Sstevel@tonic-gate 				}
266*2139Sjp161948 
267*2139Sjp161948 			if (!--count)
268*2139Sjp161948 				{
269*2139Sjp161948 				BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
270*2139Sjp161948 				return 0;
271*2139Sjp161948 				}
272*2139Sjp161948 
2730Sstevel@tonic-gate 			}
2740Sstevel@tonic-gate 		while (BN_cmp(r, range) >= 0);
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 	else
2770Sstevel@tonic-gate 		{
2780Sstevel@tonic-gate 		do
2790Sstevel@tonic-gate 			{
2800Sstevel@tonic-gate 			/* range = 11..._2  or  range = 101..._2 */
2810Sstevel@tonic-gate 			if (!bn_rand(r, n, -1, 0)) return 0;
282*2139Sjp161948 
283*2139Sjp161948 			if (!--count)
284*2139Sjp161948 				{
285*2139Sjp161948 				BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
286*2139Sjp161948 				return 0;
287*2139Sjp161948 				}
2880Sstevel@tonic-gate 			}
2890Sstevel@tonic-gate 		while (BN_cmp(r, range) >= 0);
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 
292*2139Sjp161948 	bn_check_top(r);
2930Sstevel@tonic-gate 	return 1;
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 
BN_rand_range(BIGNUM * r,BIGNUM * range)2970Sstevel@tonic-gate int	BN_rand_range(BIGNUM *r, BIGNUM *range)
2980Sstevel@tonic-gate 	{
2990Sstevel@tonic-gate 	return bn_rand_range(0, r, range);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
BN_pseudo_rand_range(BIGNUM * r,BIGNUM * range)3020Sstevel@tonic-gate int	BN_pseudo_rand_range(BIGNUM *r, BIGNUM *range)
3030Sstevel@tonic-gate 	{
3040Sstevel@tonic-gate 	return bn_rand_range(1, r, range);
3050Sstevel@tonic-gate 	}
306