xref: /openbsd-src/lib/libcrypto/man/BN_rand.3 (revision ceb4d7caee429c5d359c43e92565bc3fea95936a)
1*ceb4d7caStb.\"	$OpenBSD: BN_rand.3,v 1.18 2021/11/30 18:34:35 tb Exp $
2886433dfSschwarze.\" full merge up to: OpenSSL 05ea606a May 20 20:52:46 2016 -0400
3886433dfSschwarze.\" selective merge up to: OpenSSL df75c2bf Dec 9 01:02:36 2018 +0100
48974101aSjmc.\"
50e657c6eSschwarze.\" This file was written by Ulf Moeller <ulf@openssl.org>.
60e657c6eSschwarze.\" Copyright (c) 2000, 2001, 2002, 2013, 2015 The OpenSSL Project.
70e657c6eSschwarze.\" All rights reserved.
80e657c6eSschwarze.\"
90e657c6eSschwarze.\" Redistribution and use in source and binary forms, with or without
100e657c6eSschwarze.\" modification, are permitted provided that the following conditions
110e657c6eSschwarze.\" are met:
120e657c6eSschwarze.\"
130e657c6eSschwarze.\" 1. Redistributions of source code must retain the above copyright
140e657c6eSschwarze.\"    notice, this list of conditions and the following disclaimer.
150e657c6eSschwarze.\"
160e657c6eSschwarze.\" 2. Redistributions in binary form must reproduce the above copyright
170e657c6eSschwarze.\"    notice, this list of conditions and the following disclaimer in
180e657c6eSschwarze.\"    the documentation and/or other materials provided with the
190e657c6eSschwarze.\"    distribution.
200e657c6eSschwarze.\"
210e657c6eSschwarze.\" 3. All advertising materials mentioning features or use of this
220e657c6eSschwarze.\"    software must display the following acknowledgment:
230e657c6eSschwarze.\"    "This product includes software developed by the OpenSSL Project
240e657c6eSschwarze.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
250e657c6eSschwarze.\"
260e657c6eSschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
270e657c6eSschwarze.\"    endorse or promote products derived from this software without
280e657c6eSschwarze.\"    prior written permission. For written permission, please contact
290e657c6eSschwarze.\"    openssl-core@openssl.org.
300e657c6eSschwarze.\"
310e657c6eSschwarze.\" 5. Products derived from this software may not be called "OpenSSL"
320e657c6eSschwarze.\"    nor may "OpenSSL" appear in their names without prior written
330e657c6eSschwarze.\"    permission of the OpenSSL Project.
340e657c6eSschwarze.\"
350e657c6eSschwarze.\" 6. Redistributions of any form whatsoever must retain the following
360e657c6eSschwarze.\"    acknowledgment:
370e657c6eSschwarze.\"    "This product includes software developed by the OpenSSL Project
380e657c6eSschwarze.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
390e657c6eSschwarze.\"
400e657c6eSschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
410e657c6eSschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
420e657c6eSschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
430e657c6eSschwarze.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
440e657c6eSschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
450e657c6eSschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
460e657c6eSschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
470e657c6eSschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480e657c6eSschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
490e657c6eSschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
500e657c6eSschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
510e657c6eSschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE.
520e657c6eSschwarze.\"
53*ceb4d7caStb.Dd $Mdocdate: November 30 2021 $
54f1048625Sschwarze.Dt BN_RAND 3
55f1048625Sschwarze.Os
56f1048625Sschwarze.Sh NAME
57f1048625Sschwarze.Nm BN_rand ,
58f1048625Sschwarze.Nm BN_rand_range ,
59beed21b3Sschwarze.Nm BN_pseudo_rand ,
60f1048625Sschwarze.Nm BN_pseudo_rand_range
61f1048625Sschwarze.Nd generate pseudo-random number
62f1048625Sschwarze.Sh SYNOPSIS
63f1048625Sschwarze.In openssl/bn.h
64f1048625Sschwarze.Ft int
65f1048625Sschwarze.Fo BN_rand
66f1048625Sschwarze.Fa "BIGNUM *rnd"
67f1048625Sschwarze.Fa "int bits"
68f1048625Sschwarze.Fa "int top"
69f1048625Sschwarze.Fa "int bottom"
70f1048625Sschwarze.Fc
71f1048625Sschwarze.Ft int
72f1048625Sschwarze.Fo BN_rand_range
73f1048625Sschwarze.Fa "BIGNUM *rnd"
74*ceb4d7caStb.Fa "const BIGNUM *range"
75f1048625Sschwarze.Fc
76f1048625Sschwarze.Sh DESCRIPTION
77f1048625Sschwarze.Fn BN_rand
78f1048625Sschwarzegenerates a cryptographically strong pseudo-random number of
79f1048625Sschwarze.Fa bits
8031a34a56Ssobradoin length and stores it in
81f1048625Sschwarze.Fa rnd .
82f1048625SschwarzeIf
83f1048625Sschwarze.Fa top
8482953e70Stbis
8582953e70Stb.Dv BN_RAND_TOP_ANY ,
8682953e70Stbthe most significant bit of the random number can be zero.
87f1048625SschwarzeIf
88f1048625Sschwarze.Fa top
8982953e70Stbis
9082953e70Stb.Dv BN_RAND_TOP_ONE ,
9182953e70Stbthe most significant bit is set to 1, and if
92f1048625Sschwarze.Fa top
9382953e70Stbis
9482953e70Stb.Dv BN_RAND_TOP_TWO ,
9582953e70Stbthe two most significant bits of the number will be set to 1, so
96f1048625Sschwarzethat the product of two such random numbers will always have
97f1048625Sschwarze.Pf 2* Fa bits
98f1048625Sschwarzelength.
99f1048625SschwarzeIf
100f1048625Sschwarze.Fa bottom
10182953e70Stbis
10282953e70Stb.Dv BN_RAND_BOTTOM_ODD ,
10382953e70Stbthe number will be odd;
10482953e70Stbif it is
10582953e70Stb.Dv BN_RAND_BOTTOM_ANY ,
10682953e70Stbit can be odd or even.
1070e657c6eSschwarzeThe value of
1080e657c6eSschwarze.Fa bits
1090e657c6eSschwarzemust be zero or greater.
1100e657c6eSschwarzeIf
1110e657c6eSschwarze.Fa bits
1120e657c6eSschwarzeis +1 then
1130e657c6eSschwarze.Fa top
11482953e70Stbcannot be
11582953e70Stb.Dv BN_RAND_TOP_TWO .
116f1048625Sschwarze.Pp
117f1048625Sschwarze.Fn BN_rand_range
118f1048625Sschwarzegenerates a cryptographically strong pseudo-random number
119f1048625Sschwarze.Fa rnd
120f1048625Sschwarzein the range 0 <=
121f1048625Sschwarze.Fa rnd No < Fa range .
122beed21b3Sschwarze.Pp
123beed21b3Sschwarze.Fn BN_pseudo_rand
124886433dfSschwarzeis a deprecated alias for
125beed21b3Sschwarze.Fn BN_rand ,
126beed21b3Sschwarzeand
127beed21b3Sschwarze.Fn BN_pseudo_rand_range
128beed21b3Sschwarzefor
129886433dfSschwarze.Fn BN_rand_range .
130f1048625Sschwarze.Sh RETURN VALUES
131f1048625SschwarzeThe functions return 1 on success, 0 on error.
132f1048625SschwarzeThe error codes can be obtained by
133f1048625Sschwarze.Xr ERR_get_error 3 .
134f1048625Sschwarze.Sh SEE ALSO
135ab3cf6dbSschwarze.Xr BN_new 3
136f1048625Sschwarze.Sh HISTORY
137f1048625Sschwarze.Fn BN_rand
13810e00d17Sschwarzefirst appeared in SSLeay 0.5.1 and has been available since
1390c85fcf7Sschwarze.Ox 2.4 .
1400c85fcf7Sschwarze.Pp
141f1048625SschwarzeThe
142f1048625Sschwarze.Fa top
143f1048625Sschwarze== -1 case and the function
144f1048625Sschwarze.Fn BN_rand_range
145eaf43badSschwarzefirst appeared in OpenSSL 0.9.6a and have been available since
146eaf43badSschwarze.Ox 3.0 .
147