2 Unix SMB/CIFS implementation.
4 Functions to create reasonable random numbers for crypto use.
6 Copyright (C) Jeremy Allison 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/util/fault.h"
24 #include "lib/util/genrand.h"
26 #include <gnutls/gnutls.h>
27 #include <gnutls/crypto.h>
30 * Details about the GnuTLS CSPRNG:
32 * https://nikmav.blogspot.com/2017/03/improving-by-simplifying-gnutls-prng.html
36 _NORETURN_
static void genrand_panic(int err
,
41 snprintf(buf
, sizeof(buf
),
42 "%s:%s: GnuTLS could not generate a random buffer: %s [%d]\n",
43 location
, func
, gnutls_strerror_name(err
), err
);
48 _PUBLIC_
void generate_random_buffer(uint8_t *out
, size_t len
)
50 /* Random number generator for temporary keys. */
51 int ret
= gnutls_rnd(GNUTLS_RND_RANDOM
, out
, len
);
53 genrand_panic(ret
, __location__
, __func__
);
57 _PUBLIC_
void generate_secret_buffer(uint8_t *out
, size_t len
)
60 * Random number generator for long term keys.
62 * The key generator, will re-seed after a fixed amount of bytes is
63 * generated (typically less than the nonce), and will also re-seed
64 * based on time, i.e., after few hours of operation without reaching
65 * the limit for a re-seed. For its re-seed it mixes data obtained
66 * from the OS random device with the previous key.
68 int ret
= gnutls_rnd(GNUTLS_RND_KEY
, out
, len
);
70 genrand_panic(ret
, __location__
, __func__
);
74 _PUBLIC_
void generate_nonce_buffer(uint8_t *out
, size_t len
)
77 * Random number generator for nonce and initialization vectors.
79 * The nonce generator will reseed after outputting a fixed amount of
80 * bytes (typically few megabytes), or after few hours of operation
81 * without reaching the limit has passed.
83 int ret
= gnutls_rnd(GNUTLS_RND_NONCE
, out
, len
);
85 genrand_panic(ret
, __location__
, __func__
);