Skip to content

Commit 6694e51

Browse files
committed
Provide rand_bytes_ex and rand_priv_bytes_ex
We provider internal versions of RAND_bytes() and RAND_priv_bytes() which have the addition of taking an OPENSSL_CTX as a parameter. Reviewed-by: Matthias St. Pierre <[email protected]> (Merged from openssl#9193)
1 parent f690ef1 commit 6694e51

File tree

6 files changed

+79
-8
lines changed

6 files changed

+79
-8
lines changed

crypto/err/openssl.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ RAND_F_DRBG_GET_ENTROPY:105:drbg_get_entropy
11401140
RAND_F_DRBG_SETUP:117:drbg_setup
11411141
RAND_F_GET_ENTROPY:106:get_entropy
11421142
RAND_F_RAND_BYTES:100:RAND_bytes
1143+
RAND_F_RAND_BYTES_EX:126:rand_bytes_ex
11431144
RAND_F_RAND_DRBG_ENABLE_LOCKING:119:rand_drbg_enable_locking
11441145
RAND_F_RAND_DRBG_GENERATE:107:RAND_DRBG_generate
11451146
RAND_F_RAND_DRBG_GET_ENTROPY:120:rand_drbg_get_entropy

crypto/include/internal/rand_int.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,10 @@ void rand_pool_cleanup(void);
137137
*/
138138
void rand_pool_keep_random_devices_open(int keep);
139139

140+
/* Equivalent of RAND_priv_bytes() but additionally taking an OPENSSL_CTX */
141+
int rand_priv_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num);
142+
143+
/* Equivalent of RAND_bytes() but additionally taking an OPENSSL_CTX */
144+
int rand_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num);
145+
140146
#endif

crypto/rand/rand_err.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static const ERR_STRING_DATA RAND_str_functs[] = {
2020
{ERR_PACK(ERR_LIB_RAND, RAND_F_DRBG_SETUP, 0), "drbg_setup"},
2121
{ERR_PACK(ERR_LIB_RAND, RAND_F_GET_ENTROPY, 0), "get_entropy"},
2222
{ERR_PACK(ERR_LIB_RAND, RAND_F_RAND_BYTES, 0), "RAND_bytes"},
23+
{ERR_PACK(ERR_LIB_RAND, RAND_F_RAND_BYTES_EX, 0), "rand_bytes_ex"},
2324
{ERR_PACK(ERR_LIB_RAND, RAND_F_RAND_DRBG_ENABLE_LOCKING, 0),
2425
"rand_drbg_enable_locking"},
2526
{ERR_PACK(ERR_LIB_RAND, RAND_F_RAND_DRBG_GENERATE, 0),

crypto/rand/rand_lib.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -749,31 +749,52 @@ void RAND_add(const void *buf, int num, double randomness)
749749
* the default method, then just call RAND_bytes(). Otherwise make
750750
* sure we're instantiated and use the private DRBG.
751751
*/
752-
int RAND_priv_bytes(unsigned char *buf, int num)
752+
int rand_priv_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num)
753753
{
754754
RAND_DRBG *drbg;
755755
int ret;
756756
const RAND_METHOD *meth = RAND_get_rand_method();
757757

758758
if (meth != RAND_OpenSSL())
759-
return RAND_bytes(buf, num);
759+
return meth->bytes(buf, num);
760760

761-
drbg = RAND_DRBG_get0_private();
761+
drbg = OPENSSL_CTX_get0_private_drbg(ctx);
762762
if (drbg == NULL)
763763
return 0;
764764

765765
ret = RAND_DRBG_bytes(drbg, buf, num);
766766
return ret;
767767
}
768768

769-
int RAND_bytes(unsigned char *buf, int num)
769+
int RAND_priv_bytes(unsigned char *buf, int num)
770770
{
771+
return rand_priv_bytes_ex(NULL, buf, num);
772+
}
773+
774+
int rand_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num)
775+
{
776+
RAND_DRBG *drbg;
777+
int ret;
771778
const RAND_METHOD *meth = RAND_get_rand_method();
772779

773-
if (meth->bytes != NULL)
774-
return meth->bytes(buf, num);
775-
RANDerr(RAND_F_RAND_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
776-
return -1;
780+
if (meth != RAND_OpenSSL()) {
781+
if (meth->bytes != NULL)
782+
return meth->bytes(buf, num);
783+
RANDerr(RAND_F_RAND_BYTES_EX, RAND_R_FUNC_NOT_IMPLEMENTED);
784+
return -1;
785+
}
786+
787+
drbg = OPENSSL_CTX_get0_public_drbg(ctx);
788+
if (drbg == NULL)
789+
return 0;
790+
791+
ret = RAND_DRBG_bytes(drbg, buf, num);
792+
return ret;
793+
}
794+
795+
int RAND_bytes(unsigned char *buf, int num)
796+
{
797+
return rand_bytes_ex(NULL, buf, num);
777798
}
778799

779800
#if !OPENSSL_API_1_1_0 && !defined(FIPS_MODE)

doc/internal/man3/rand_bytes_ex.pod

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=pod
2+
3+
=head1 NAME
4+
5+
rand_bytes_ex, rand_priv_bytes_ex
6+
- internal random number routines
7+
8+
=head1 SYNOPSIS
9+
10+
#include "internal/rand_int.h"
11+
12+
int rand_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num);
13+
int rand_priv_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num);
14+
15+
=head1 DESCRIPTION
16+
17+
rand_bytes_ex() and rand_priv_bytes_ex() are the equivalent of RAND_bytes() and
18+
RAND_priv_bytes() in the public API except that they both take an additional
19+
B<ctx> parameter.
20+
The DRBG used for the operation is the public or private DRBG associated with
21+
the specified B<ctx>. The parameter can be NULL, in which case
22+
the default library ctx is used.
23+
If the default RAND_METHOD has been changed then for compatibility reasons the
24+
RAND_METHOD will be used in preference and the DRBG of the library context
25+
ignored.
26+
27+
=head1 RETURN VALUES
28+
29+
rand_bytes_ex() and rand_bytes_priv_ex() return 0 or less on error or 1 on
30+
success.
31+
32+
=head1 COPYRIGHT
33+
34+
Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
35+
36+
Licensed under the Apache License 2.0 (the "License"). You may not use
37+
this file except in compliance with the License. You can obtain a copy
38+
in the file LICENSE in the source distribution or at
39+
L<https://www.openssl.org/source/license.html>.
40+
41+
=cut

include/openssl/randerr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int ERR_load_RAND_strings(void);
2929
# define RAND_F_DRBG_SETUP 117
3030
# define RAND_F_GET_ENTROPY 106
3131
# define RAND_F_RAND_BYTES 100
32+
# define RAND_F_RAND_BYTES_EX 126
3233
# define RAND_F_RAND_DRBG_ENABLE_LOCKING 119
3334
# define RAND_F_RAND_DRBG_GENERATE 107
3435
# define RAND_F_RAND_DRBG_GET_ENTROPY 120

0 commit comments

Comments
 (0)