155714Skris/* crypto/bn/bn_rand.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. 8296465Sdelphij * 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). 15296465Sdelphij * 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. 22296465Sdelphij * 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 :-). 37296465Sdelphij * 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)" 40296465Sdelphij * 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. 52296465Sdelphij * 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 */ 5889837Skris/* ==================================================================== 5989837Skris * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 6089837Skris * 6189837Skris * Redistribution and use in source and binary forms, with or without 6289837Skris * modification, are permitted provided that the following conditions 6389837Skris * are met: 6489837Skris * 6589837Skris * 1. Redistributions of source code must retain the above copyright 66296465Sdelphij * notice, this list of conditions and the following disclaimer. 6789837Skris * 6889837Skris * 2. Redistributions in binary form must reproduce the above copyright 6989837Skris * notice, this list of conditions and the following disclaimer in 7089837Skris * the documentation and/or other materials provided with the 7189837Skris * distribution. 7289837Skris * 7389837Skris * 3. All advertising materials mentioning features or use of this 7489837Skris * software must display the following acknowledgment: 7589837Skris * "This product includes software developed by the OpenSSL Project 7689837Skris * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 7789837Skris * 7889837Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 7989837Skris * endorse or promote products derived from this software without 8089837Skris * prior written permission. For written permission, please contact 8189837Skris * openssl-core@openssl.org. 8289837Skris * 8389837Skris * 5. Products derived from this software may not be called "OpenSSL" 8489837Skris * nor may "OpenSSL" appear in their names without prior written 8589837Skris * permission of the OpenSSL Project. 8689837Skris * 8789837Skris * 6. Redistributions of any form whatsoever must retain the following 8889837Skris * acknowledgment: 8989837Skris * "This product includes software developed by the OpenSSL Project 9089837Skris * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 9189837Skris * 9289837Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 9389837Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 9489837Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 9589837Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 9689837Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 9789837Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 9889837Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 9989837Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 10089837Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 10189837Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10289837Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 10389837Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 10489837Skris * ==================================================================== 10589837Skris * 10689837Skris * This product includes cryptographic software written by Eric Young 10789837Skris * (eay@cryptsoft.com). This product includes software written by Tim 10889837Skris * Hudson (tjh@cryptsoft.com). 10989837Skris * 11089837Skris */ 11155714Skris 11255714Skris#include <stdio.h> 11355714Skris#include <time.h> 11455714Skris#include "cryptlib.h" 11555714Skris#include "bn_lcl.h" 11655714Skris#include <openssl/rand.h> 11755714Skris 11859191Skrisstatic int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) 119296465Sdelphij{ 120296465Sdelphij unsigned char *buf = NULL; 121296465Sdelphij int ret = 0, bit, bytes, mask; 122296465Sdelphij time_t tim; 12355714Skris 124296465Sdelphij if (bits < 0 || (bits == 1 && top > 0)) { 125296465Sdelphij BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL); 126296465Sdelphij return 0; 127296465Sdelphij } 12868651Skris 129296465Sdelphij if (bits == 0) { 130296465Sdelphij BN_zero(rnd); 131296465Sdelphij return 1; 132296465Sdelphij } 13355714Skris 134296465Sdelphij bytes = (bits + 7) / 8; 135296465Sdelphij bit = (bits - 1) % 8; 136296465Sdelphij mask = 0xff << (bit + 1); 13755714Skris 138296465Sdelphij buf = (unsigned char *)OPENSSL_malloc(bytes); 139296465Sdelphij if (buf == NULL) { 140296465Sdelphij BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE); 141296465Sdelphij goto err; 142296465Sdelphij } 14355714Skris 144296465Sdelphij /* make a random number and set the top and bottom bits */ 145296465Sdelphij time(&tim); 146296465Sdelphij RAND_add(&tim, sizeof(tim), 0.0); 14759191Skris 148296465Sdelphij if (pseudorand) { 149296465Sdelphij if (RAND_pseudo_bytes(buf, bytes) == -1) 150296465Sdelphij goto err; 151296465Sdelphij } else { 152296465Sdelphij if (RAND_bytes(buf, bytes) <= 0) 153296465Sdelphij goto err; 154296465Sdelphij } 155296465Sdelphij 15676866Skris#if 1 157296465Sdelphij if (pseudorand == 2) { 158296465Sdelphij /* 159296465Sdelphij * generate patterns that are more likely to trigger BN library bugs 160296465Sdelphij */ 161296465Sdelphij int i; 162296465Sdelphij unsigned char c; 16376866Skris 164296465Sdelphij for (i = 0; i < bytes; i++) { 165296465Sdelphij RAND_pseudo_bytes(&c, 1); 166296465Sdelphij if (c >= 128 && i > 0) 167296465Sdelphij buf[i] = buf[i - 1]; 168296465Sdelphij else if (c < 42) 169296465Sdelphij buf[i] = 0; 170296465Sdelphij else if (c < 84) 171296465Sdelphij buf[i] = 255; 172296465Sdelphij } 173296465Sdelphij } 17476866Skris#endif 17576866Skris 176296465Sdelphij if (top >= 0) { 177296465Sdelphij if (top) { 178296465Sdelphij if (bit == 0) { 179296465Sdelphij buf[0] = 1; 180296465Sdelphij buf[1] |= 0x80; 181296465Sdelphij } else { 182296465Sdelphij buf[0] |= (3 << (bit - 1)); 183296465Sdelphij } 184296465Sdelphij } else { 185296465Sdelphij buf[0] |= (1 << bit); 186296465Sdelphij } 187296465Sdelphij } 188296465Sdelphij buf[0] &= ~mask; 189296465Sdelphij if (bottom) /* set bottom bit if requested */ 190296465Sdelphij buf[bytes - 1] |= 1; 191296465Sdelphij if (!BN_bin2bn(buf, bytes, rnd)) 192296465Sdelphij goto err; 193296465Sdelphij ret = 1; 194296465Sdelphij err: 195296465Sdelphij if (buf != NULL) { 196296465Sdelphij OPENSSL_cleanse(buf, bytes); 197296465Sdelphij OPENSSL_free(buf); 198296465Sdelphij } 199296465Sdelphij bn_check_top(rnd); 200296465Sdelphij return (ret); 201296465Sdelphij} 20255714Skris 203296465Sdelphijint BN_rand(BIGNUM *rnd, int bits, int top, int bottom) 204296465Sdelphij{ 205296465Sdelphij return bnrand(0, rnd, bits, top, bottom); 206296465Sdelphij} 20759191Skris 208296465Sdelphijint BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) 209296465Sdelphij{ 210296465Sdelphij return bnrand(1, rnd, bits, top, bottom); 211296465Sdelphij} 21272613Skris 21376866Skris#if 1 214296465Sdelphijint BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom) 215296465Sdelphij{ 216296465Sdelphij return bnrand(2, rnd, bits, top, bottom); 217296465Sdelphij} 21876866Skris#endif 21976866Skris 22072613Skris/* random number r: 0 <= r < range */ 221194206Ssimonstatic int bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range) 222296465Sdelphij{ 223296465Sdelphij int (*bn_rand) (BIGNUM *, int, int, int) = 224296465Sdelphij pseudo ? BN_pseudo_rand : BN_rand; 225296465Sdelphij int n; 226296465Sdelphij int count = 100; 22772613Skris 228296465Sdelphij if (range->neg || BN_is_zero(range)) { 229296465Sdelphij BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE); 230296465Sdelphij return 0; 231296465Sdelphij } 23272613Skris 233296465Sdelphij n = BN_num_bits(range); /* n > 0 */ 23476866Skris 235296465Sdelphij /* BN_is_bit_set(range, n - 1) always holds */ 23689837Skris 237296465Sdelphij if (n == 1) 238296465Sdelphij BN_zero(r); 239296465Sdelphij else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) { 240296465Sdelphij /* 241296465Sdelphij * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer 242296465Sdelphij * than range 243296465Sdelphij */ 244296465Sdelphij do { 245296465Sdelphij if (!bn_rand(r, n + 1, -1, 0)) 246296465Sdelphij return 0; 247296465Sdelphij /* 248296465Sdelphij * If r < 3*range, use r := r MOD range (which is either r, r - 249296465Sdelphij * range, or r - 2*range). Otherwise, iterate once more. Since 250296465Sdelphij * 3*range = 11..._2, each iteration succeeds with probability >= 251296465Sdelphij * .75. 252296465Sdelphij */ 253296465Sdelphij if (BN_cmp(r, range) >= 0) { 254296465Sdelphij if (!BN_sub(r, r, range)) 255296465Sdelphij return 0; 256296465Sdelphij if (BN_cmp(r, range) >= 0) 257296465Sdelphij if (!BN_sub(r, r, range)) 258296465Sdelphij return 0; 259296465Sdelphij } 260160814Ssimon 261296465Sdelphij if (!--count) { 262296465Sdelphij BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS); 263296465Sdelphij return 0; 264296465Sdelphij } 265160814Ssimon 266296465Sdelphij } 267296465Sdelphij while (BN_cmp(r, range) >= 0); 268296465Sdelphij } else { 269296465Sdelphij do { 270296465Sdelphij /* range = 11..._2 or range = 101..._2 */ 271296465Sdelphij if (!bn_rand(r, n, -1, 0)) 272296465Sdelphij return 0; 27372613Skris 274296465Sdelphij if (!--count) { 275296465Sdelphij BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS); 276296465Sdelphij return 0; 277296465Sdelphij } 278296465Sdelphij } 279296465Sdelphij while (BN_cmp(r, range) >= 0); 280296465Sdelphij } 28189837Skris 282296465Sdelphij bn_check_top(r); 283296465Sdelphij return 1; 284296465Sdelphij} 28589837Skris 286296465Sdelphijint BN_rand_range(BIGNUM *r, const BIGNUM *range) 287296465Sdelphij{ 288296465Sdelphij return bn_rand_range(0, r, range); 289296465Sdelphij} 29089837Skris 291296465Sdelphijint BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range) 292296465Sdelphij{ 293296465Sdelphij return bn_rand_range(1, r, range); 294296465Sdelphij} 295