1*091f637aStb /* $OpenBSD: bn_rand.c,v 1.30 2024/03/16 20:42:33 tb Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker * All rights reserved.
45b37fcf3Sryker *
55b37fcf3Sryker * This package is an SSL implementation written
65b37fcf3Sryker * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker *
95b37fcf3Sryker * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker * the following conditions are aheared to. The following conditions
115b37fcf3Sryker * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker * lhash, DES, etc., code; not just the SSL code. The SSL documentation
135b37fcf3Sryker * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker *
165b37fcf3Sryker * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker * the code are not to be removed.
185b37fcf3Sryker * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker * as the author of the parts of the library used.
205b37fcf3Sryker * This can be in the form of a textual message at program startup or
215b37fcf3Sryker * in documentation (online or textual) provided with the package.
225b37fcf3Sryker *
235b37fcf3Sryker * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker * modification, are permitted provided that the following conditions
255b37fcf3Sryker * are met:
265b37fcf3Sryker * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker * notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker * notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker * documentation and/or other materials provided with the distribution.
315b37fcf3Sryker * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker * must display the following acknowledgement:
335b37fcf3Sryker * "This product includes cryptographic software written by
345b37fcf3Sryker * Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker * The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker * being used are not cryptographic related :-).
375b37fcf3Sryker * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker * the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker *
415b37fcf3Sryker * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker * SUCH DAMAGE.
525b37fcf3Sryker *
535b37fcf3Sryker * The licence and distribution terms for any publically available version or
545b37fcf3Sryker * derivative of this code cannot be changed. i.e. this code cannot simply be
555b37fcf3Sryker * copied and put under another distribution licence
565b37fcf3Sryker * [including the GNU Public Licence.]
575b37fcf3Sryker */
58da347917Sbeck /* ====================================================================
59da347917Sbeck * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
60da347917Sbeck *
61da347917Sbeck * Redistribution and use in source and binary forms, with or without
62da347917Sbeck * modification, are permitted provided that the following conditions
63da347917Sbeck * are met:
64da347917Sbeck *
65da347917Sbeck * 1. Redistributions of source code must retain the above copyright
66da347917Sbeck * notice, this list of conditions and the following disclaimer.
67da347917Sbeck *
68da347917Sbeck * 2. Redistributions in binary form must reproduce the above copyright
69da347917Sbeck * notice, this list of conditions and the following disclaimer in
70da347917Sbeck * the documentation and/or other materials provided with the
71da347917Sbeck * distribution.
72da347917Sbeck *
73da347917Sbeck * 3. All advertising materials mentioning features or use of this
74da347917Sbeck * software must display the following acknowledgment:
75da347917Sbeck * "This product includes software developed by the OpenSSL Project
76da347917Sbeck * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77da347917Sbeck *
78da347917Sbeck * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79da347917Sbeck * endorse or promote products derived from this software without
80da347917Sbeck * prior written permission. For written permission, please contact
81da347917Sbeck * openssl-core@openssl.org.
82da347917Sbeck *
83da347917Sbeck * 5. Products derived from this software may not be called "OpenSSL"
84da347917Sbeck * nor may "OpenSSL" appear in their names without prior written
85da347917Sbeck * permission of the OpenSSL Project.
86da347917Sbeck *
87da347917Sbeck * 6. Redistributions of any form whatsoever must retain the following
88da347917Sbeck * acknowledgment:
89da347917Sbeck * "This product includes software developed by the OpenSSL Project
90da347917Sbeck * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91da347917Sbeck *
92da347917Sbeck * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93da347917Sbeck * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94da347917Sbeck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95da347917Sbeck * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96da347917Sbeck * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97da347917Sbeck * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98da347917Sbeck * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99da347917Sbeck * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100da347917Sbeck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101da347917Sbeck * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102da347917Sbeck * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103da347917Sbeck * OF THE POSSIBILITY OF SUCH DAMAGE.
104da347917Sbeck * ====================================================================
105da347917Sbeck *
106da347917Sbeck * This product includes cryptographic software written by Eric Young
107da347917Sbeck * (eay@cryptsoft.com). This product includes software written by Tim
108da347917Sbeck * Hudson (tjh@cryptsoft.com).
109da347917Sbeck *
110da347917Sbeck */
1115b37fcf3Sryker
112*091f637aStb #include <limits.h>
1135b37fcf3Sryker #include <stdio.h>
114ef624301Sjsing #include <stdlib.h>
1150f777b12Sjsing #include <string.h>
1165b37fcf3Sryker #include <time.h>
117b6ab114eSjsing
118b6ab114eSjsing #include <openssl/err.h>
1195b37fcf3Sryker
120c9675a23Stb #include "bn_local.h"
121b6ab114eSjsing
1222bd9bb84Sjsing static int
bnrand(int pseudorand,BIGNUM * rnd,int bits,int top,int bottom)1232bd9bb84Sjsing bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
1245b37fcf3Sryker {
1255b37fcf3Sryker unsigned char *buf = NULL;
1265b37fcf3Sryker int ret = 0, bit, bytes, mask;
1275b37fcf3Sryker
128aab8a614Sjsing if (rnd == NULL) {
1295067ae9fSbeck BNerror(ERR_R_PASSED_NULL_PARAMETER);
130aab8a614Sjsing return (0);
131aab8a614Sjsing }
132aab8a614Sjsing
133f779ca2bStb if (bits < 0 || (bits == 1 && top > 0)) {
134f779ca2bStb BNerror(BN_R_BITS_TOO_SMALL);
135f779ca2bStb return (0);
136f779ca2bStb }
137*091f637aStb if (bits > INT_MAX - 7) {
138*091f637aStb BNerror(BN_R_BIGNUM_TOO_LONG);
139*091f637aStb return (0);
140*091f637aStb }
141f779ca2bStb
1422bd9bb84Sjsing if (bits == 0) {
143c109e398Sbeck BN_zero(rnd);
144aab8a614Sjsing return (1);
145c109e398Sbeck }
146c109e398Sbeck
1475b37fcf3Sryker bytes = (bits + 7) / 8;
1485b37fcf3Sryker bit = (bits - 1) % 8;
149ce6fc090Sbeck mask = 0xff << (bit + 1);
1505b37fcf3Sryker
1514163340bSderaadt buf = malloc(bytes);
1522bd9bb84Sjsing if (buf == NULL) {
1535067ae9fSbeck BNerror(ERR_R_MALLOC_FAILURE);
1545b37fcf3Sryker goto err;
1555b37fcf3Sryker }
1565b37fcf3Sryker
1575b37fcf3Sryker /* make a random number and set the top and bottom bits */
158ef624301Sjsing arc4random_buf(buf, bytes);
159ba5406e9Sbeck
160ce6fc090Sbeck #if 1
1612bd9bb84Sjsing if (pseudorand == 2) {
162ce6fc090Sbeck /* generate patterns that are more likely to trigger BN
163ce6fc090Sbeck library bugs */
164ce6fc090Sbeck int i;
165ce6fc090Sbeck unsigned char c;
166ce6fc090Sbeck
1672bd9bb84Sjsing for (i = 0; i < bytes; i++) {
168ef624301Sjsing arc4random_buf(&c, 1);
169ce6fc090Sbeck if (c >= 128 && i > 0)
170ce6fc090Sbeck buf[i] = buf[i - 1];
171ce6fc090Sbeck else if (c < 42)
172ce6fc090Sbeck buf[i] = 0;
173ce6fc090Sbeck else if (c < 84)
174ce6fc090Sbeck buf[i] = 255;
175ce6fc090Sbeck }
176ce6fc090Sbeck }
177ce6fc090Sbeck #endif
178ce6fc090Sbeck
179f779ca2bStb if (top > 0) {
1802bd9bb84Sjsing if (bit == 0) {
1815b37fcf3Sryker buf[0] = 1;
1825b37fcf3Sryker buf[1] |= 0x80;
1832bd9bb84Sjsing } else {
1845b37fcf3Sryker buf[0] |= (3 << (bit - 1));
1855b37fcf3Sryker }
18603eaa739Stb }
18703eaa739Stb if (top == 0)
1885b37fcf3Sryker buf[0] |= (1 << bit);
189ce6fc090Sbeck buf[0] &= ~mask;
190ce6fc090Sbeck if (bottom) /* set bottom bit if requested */
1915b37fcf3Sryker buf[bytes - 1] |= 1;
192aab8a614Sjsing if (BN_bin2bn(buf, bytes, rnd) == NULL)
1932bd9bb84Sjsing goto err;
1945b37fcf3Sryker ret = 1;
1952bd9bb84Sjsing
1965b37fcf3Sryker err:
1977de8a684Sderaadt freezero(buf, bytes);
1985b37fcf3Sryker return (ret);
1995b37fcf3Sryker }
2005b37fcf3Sryker
2012bd9bb84Sjsing int
BN_rand(BIGNUM * rnd,int bits,int top,int bottom)2022bd9bb84Sjsing BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
203ba5406e9Sbeck {
204ba5406e9Sbeck return bnrand(0, rnd, bits, top, bottom);
205ba5406e9Sbeck }
206ca1d80d6Sbeck LCRYPTO_ALIAS(BN_rand);
207ba5406e9Sbeck
2082bd9bb84Sjsing int
BN_pseudo_rand(BIGNUM * rnd,int bits,int top,int bottom)2092bd9bb84Sjsing BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
210ba5406e9Sbeck {
211ba5406e9Sbeck return bnrand(1, rnd, bits, top, bottom);
212ba5406e9Sbeck }
213ca1d80d6Sbeck LCRYPTO_ALIAS(BN_pseudo_rand);
214ce6fc090Sbeck
215ce6fc090Sbeck #if 1
2162bd9bb84Sjsing int
BN_bntest_rand(BIGNUM * rnd,int bits,int top,int bottom)2172bd9bb84Sjsing BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
218ce6fc090Sbeck {
219ce6fc090Sbeck return bnrand(2, rnd, bits, top, bottom);
220ce6fc090Sbeck }
221ce6fc090Sbeck #endif
222ce6fc090Sbeck
223da347917Sbeck
224ce6fc090Sbeck /* random number r: 0 <= r < range */
2252bd9bb84Sjsing static int
bn_rand_range(int pseudo,BIGNUM * r,const BIGNUM * range)2262bd9bb84Sjsing bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range)
227ce6fc090Sbeck {
228da347917Sbeck int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand;
229ce6fc090Sbeck int n;
2304fcf65c5Sdjm int count = 100;
231ce6fc090Sbeck
2322bd9bb84Sjsing if (range->neg || BN_is_zero(range)) {
2335067ae9fSbeck BNerror(BN_R_INVALID_RANGE);
234ce6fc090Sbeck return 0;
235ce6fc090Sbeck }
236ce6fc090Sbeck
237ce6fc090Sbeck n = BN_num_bits(range); /* n > 0 */
238ce6fc090Sbeck
239da347917Sbeck /* BN_is_bit_set(range, n - 1) always holds */
240da347917Sbeck
241ce6fc090Sbeck if (n == 1)
2424fcf65c5Sdjm BN_zero(r);
2432bd9bb84Sjsing else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
244da347917Sbeck /* range = 100..._2,
245ce6fc090Sbeck * so 3*range (= 11..._2) is exactly one bit longer than range */
2462bd9bb84Sjsing do {
2472bd9bb84Sjsing if (!bn_rand(r, n + 1, -1, 0))
2482bd9bb84Sjsing return 0;
249ce6fc090Sbeck /* If r < 3*range, use r := r MOD range
250ce6fc090Sbeck * (which is either r, r - range, or r - 2*range).
251ce6fc090Sbeck * Otherwise, iterate once more.
252ce6fc090Sbeck * Since 3*range = 11..._2, each iteration succeeds with
253ce6fc090Sbeck * probability >= .75. */
2542bd9bb84Sjsing if (BN_cmp(r, range) >= 0) {
2552bd9bb84Sjsing if (!BN_sub(r, r, range))
2562bd9bb84Sjsing return 0;
257ce6fc090Sbeck if (BN_cmp(r, range) >= 0)
2582bd9bb84Sjsing if (!BN_sub(r, r, range))
2594fcf65c5Sdjm return 0;
2604fcf65c5Sdjm }
2614fcf65c5Sdjm
2622bd9bb84Sjsing if (!--count) {
2635067ae9fSbeck BNerror(BN_R_TOO_MANY_ITERATIONS);
2642bd9bb84Sjsing return 0;
265ce6fc090Sbeck }
2662bd9bb84Sjsing
2672bd9bb84Sjsing } while (BN_cmp(r, range) >= 0);
2682bd9bb84Sjsing } else {
2692bd9bb84Sjsing do {
270da347917Sbeck /* range = 11..._2 or range = 101..._2 */
2712bd9bb84Sjsing if (!bn_rand(r, n, -1, 0))
2722bd9bb84Sjsing return 0;
2734fcf65c5Sdjm
2742bd9bb84Sjsing if (!--count) {
2755067ae9fSbeck BNerror(BN_R_TOO_MANY_ITERATIONS);
2764fcf65c5Sdjm return 0;
2774fcf65c5Sdjm }
2782bd9bb84Sjsing } while (BN_cmp(r, range) >= 0);
279da347917Sbeck }
280ce6fc090Sbeck
281ce6fc090Sbeck return 1;
282ce6fc090Sbeck }
283da347917Sbeck
2842bd9bb84Sjsing int
BN_rand_range(BIGNUM * r,const BIGNUM * range)2852bd9bb84Sjsing BN_rand_range(BIGNUM *r, const BIGNUM *range)
286da347917Sbeck {
287da347917Sbeck return bn_rand_range(0, r, range);
288da347917Sbeck }
289ca1d80d6Sbeck LCRYPTO_ALIAS(BN_rand_range);
290da347917Sbeck
2912bd9bb84Sjsing int
bn_rand_in_range(BIGNUM * rnd,const BIGNUM * lower_inc,const BIGNUM * upper_exc)29212347e81Stb bn_rand_in_range(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc)
293b2da2623Stb {
29412347e81Stb BIGNUM *len;
295b2da2623Stb int ret = 0;
296b2da2623Stb
297b2da2623Stb if ((len = BN_new()) == NULL)
298b2da2623Stb goto err;
299b2da2623Stb if (!BN_sub(len, upper_exc, lower_inc))
300b2da2623Stb goto err;
30112347e81Stb if (!BN_rand_range(rnd, len))
302b2da2623Stb goto err;
303b2da2623Stb if (!BN_add(rnd, rnd, lower_inc))
304b2da2623Stb goto err;
305b2da2623Stb
306b2da2623Stb ret = 1;
30712347e81Stb
308b2da2623Stb err:
309b2da2623Stb BN_free(len);
31012347e81Stb
31112347e81Stb return ret;
31212347e81Stb }
31312347e81Stb
31412347e81Stb int
bn_rand_interval(BIGNUM * rnd,BN_ULONG lower_word,const BIGNUM * upper_exc)31512347e81Stb bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc)
31612347e81Stb {
31712347e81Stb BIGNUM *lower_inc = NULL;
31812347e81Stb int ret = 0;
31912347e81Stb
32012347e81Stb if ((lower_inc = BN_new()) == NULL)
32112347e81Stb goto err;
32212347e81Stb if (!BN_set_word(lower_inc, lower_word))
32312347e81Stb goto err;
32412347e81Stb if (!bn_rand_in_range(rnd, lower_inc, upper_exc))
32512347e81Stb goto err;
32612347e81Stb
32712347e81Stb ret = 1;
32812347e81Stb
32912347e81Stb err:
33012347e81Stb BN_free(lower_inc);
33112347e81Stb
332b2da2623Stb return ret;
333b2da2623Stb }
334b2da2623Stb
335b2da2623Stb int
BN_pseudo_rand_range(BIGNUM * r,const BIGNUM * range)3362bd9bb84Sjsing BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
337da347917Sbeck {
338da347917Sbeck return bn_rand_range(1, r, range);
339da347917Sbeck }
340ca1d80d6Sbeck LCRYPTO_ALIAS(BN_pseudo_rand_range);
341