Skip to content

Commit 5030484

Browse files
committed
wolfcrypt/src/random.c and wolfssl/wolfcrypt/random.h:
* add WC_DRBG_{NOT_INIT,OK,FAILED,CONT_FAILED} in public header file, and * move setup for RNG_SECURITY_STRENGTH, ENTROPY_SCALE_FACTOR, SEED_BLOCK_SZ, SEED_SZ, MAX_SEED_SZ, and RNG_HEALTH_TEST_CHECK_SIZE from random.c to random.h, with public WC_DRBG_SEED_SZ and WC_DRBG_MAX_SEED_SZ.
1 parent b2ef89b commit 5030484

2 files changed

Lines changed: 92 additions & 78 deletions

File tree

wolfcrypt/src/random.c

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -225,79 +225,6 @@ This library contains implementation for the random number generator.
225225
#define OUTPUT_BLOCK_LEN (WC_SHA256_DIGEST_SIZE)
226226
#define MAX_REQUEST_LEN (0x10000)
227227

228-
229-
/* The security strength for the RNG is the target number of bits of
230-
* entropy you are looking for in a seed. */
231-
#ifndef RNG_SECURITY_STRENGTH
232-
/* SHA-256 requires a minimum of 256-bits of entropy. */
233-
#define RNG_SECURITY_STRENGTH (256)
234-
#endif
235-
236-
/* wolfentropy.h will define for HAVE_ENTROPY_MEMUSE */
237-
#ifdef HAVE_ENTROPY_MEMUSE
238-
#include <wolfssl/wolfcrypt/wolfentropy.h>
239-
#endif
240-
241-
#ifndef ENTROPY_SCALE_FACTOR
242-
/* The entropy scale factor should be the whole number inverse of the
243-
* minimum bits of entropy per bit of NDRNG output. */
244-
#if defined(HAVE_AMD_RDSEED)
245-
/* This will yield a SEED_SZ of 16kb. Since nonceSz will be 0,
246-
* we'll add an additional 8kb on top.
247-
*
248-
* See "AMD RNG ESV Public Use Document". Version 0.7 of October 24,
249-
* 2024 specifies 0.656 to 1.312 bits of entropy per 128 bit block of
250-
* RDSEED output, depending on CPU family.
251-
*/
252-
#define ENTROPY_SCALE_FACTOR (512)
253-
#elif defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND)
254-
/* The value of 2 applies to Intel's RDSEED which provides about
255-
* 0.5 bits minimum of entropy per bit. The value of 4 gives a
256-
* conservative margin for FIPS. */
257-
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
258-
(HAVE_FIPS_VERSION >= 2)
259-
#define ENTROPY_SCALE_FACTOR (2*4)
260-
#else
261-
/* Not FIPS, but Intel RDSEED, only double. */
262-
#define ENTROPY_SCALE_FACTOR (2)
263-
#endif
264-
#elif defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
265-
(HAVE_FIPS_VERSION >= 2)
266-
/* If doing a FIPS build without a specific scale factor, default
267-
* to 4. This will give 1024 bits of entropy. More is better, but
268-
* more is also slower. */
269-
#define ENTROPY_SCALE_FACTOR (4)
270-
#else
271-
/* Setting the default to 1. */
272-
#define ENTROPY_SCALE_FACTOR (1)
273-
#endif
274-
#endif /* !ENTROPY_SCALE_FACTOR */
275-
276-
#ifndef SEED_BLOCK_SZ
277-
/* The seed block size, is the size of the output of the underlying NDRNG.
278-
* This value is used for testing the output of the NDRNG. */
279-
#if defined(HAVE_AMD_RDSEED)
280-
/* AMD's RDSEED instruction works in 128-bit blocks read 64-bits
281-
* at a time. */
282-
#define SEED_BLOCK_SZ (sizeof(word64)*2)
283-
#elif defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND)
284-
/* RDSEED outputs in blocks of 64-bits. */
285-
#define SEED_BLOCK_SZ sizeof(word64)
286-
#else
287-
/* Setting the default to 4. */
288-
#define SEED_BLOCK_SZ 4
289-
#endif
290-
#endif
291-
292-
#define SEED_SZ (RNG_SECURITY_STRENGTH*ENTROPY_SCALE_FACTOR/8)
293-
294-
/* The maximum seed size will be the seed size plus a seed block for the
295-
* test, and an additional half of the seed size. This additional half
296-
* is in case the user does not supply a nonce. A nonce will be obtained
297-
* from the NDRNG. */
298-
#define MAX_SEED_SZ (SEED_SZ + SEED_SZ/2 + SEED_BLOCK_SZ)
299-
300-
301228
#ifdef WC_RNG_SEED_CB
302229

303230
#ifndef HAVE_FIPS
@@ -323,12 +250,13 @@ int wc_SetSeed_Cb(wc_RngSeed_Cb cb)
323250
#define DRBG_NO_SEED_CB 4
324251

325252
/* RNG health states */
326-
#define DRBG_NOT_INIT 0
327-
#define DRBG_OK 1
328-
#define DRBG_FAILED 2
329-
#define DRBG_CONT_FAILED 3
253+
#define DRBG_NOT_INIT WC_DRBG_NOT_INIT
254+
#define DRBG_OK WC_DRBG_OK
255+
#define DRBG_FAILED WC_DRBG_FAILED
256+
#define DRBG_CONT_FAILED WC_DRBG_CONT_FAILED
330257

331-
#define RNG_HEALTH_TEST_CHECK_SIZE (WC_SHA256_DIGEST_SIZE * 4)
258+
#define SEED_SZ WC_DRBG_SEED_SZ
259+
#define MAX_SEED_SZ WC_DRBG_MAX_SEED_SZ
332260

333261
/* Verify max gen block len */
334262
#if RNG_MAX_BLOCK_LEN > MAX_REQUEST_LEN

wolfssl/wolfcrypt/random.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,92 @@ struct OS_Seed {
163163
};
164164

165165
#ifdef HAVE_HASHDRBG
166+
167+
/* The security strength for the RNG is the target number of bits of
168+
* entropy you are looking for in a seed. */
169+
/* RNG_SECURITY_STRENGTH is unprefixed for backward compat. */
170+
#ifndef RNG_SECURITY_STRENGTH
171+
/* SHA-256 requires a minimum of 256-bits of entropy. */
172+
#define RNG_SECURITY_STRENGTH (256)
173+
#endif
174+
175+
/* wolfentropy.h will define for HAVE_ENTROPY_MEMUSE */
176+
#ifdef HAVE_ENTROPY_MEMUSE
177+
#include <wolfssl/wolfcrypt/wolfentropy.h>
178+
#endif
179+
180+
/* ENTROPY_SCALE_FACTOR is unprefixed for backward compat. */
181+
#ifndef ENTROPY_SCALE_FACTOR
182+
/* The entropy scale factor should be the whole number inverse of the
183+
* minimum bits of entropy per bit of NDRNG output. */
184+
#if defined(HAVE_AMD_RDSEED)
185+
/* This will yield a SEED_SZ of 16kb. Since nonceSz will be 0,
186+
* we'll add an additional 8kb on top.
187+
*
188+
* See "AMD RNG ESV Public Use Document". Version 0.7 of October 24,
189+
* 2024 specifies 0.656 to 1.312 bits of entropy per 128 bit block of
190+
* RDSEED output, depending on CPU family.
191+
*/
192+
#define ENTROPY_SCALE_FACTOR (512)
193+
#elif defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND)
194+
/* The value of 2 applies to Intel's RDSEED which provides about
195+
* 0.5 bits minimum of entropy per bit. The value of 4 gives a
196+
* conservative margin for FIPS. */
197+
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
198+
(HAVE_FIPS_VERSION >= 2)
199+
#define ENTROPY_SCALE_FACTOR (2*4)
200+
#else
201+
/* Not FIPS, but Intel RDSEED, only double. */
202+
#define ENTROPY_SCALE_FACTOR (2)
203+
#endif
204+
#elif defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
205+
(HAVE_FIPS_VERSION >= 2)
206+
/* If doing a FIPS build without a specific scale factor, default
207+
* to 4. This will give 1024 bits of entropy. More is better, but
208+
* more is also slower. */
209+
#define ENTROPY_SCALE_FACTOR (4)
210+
#else
211+
/* Setting the default to 1. */
212+
#define ENTROPY_SCALE_FACTOR (1)
213+
#endif
214+
#endif /* !ENTROPY_SCALE_FACTOR */
215+
216+
/* SEED_BLOCK_SZ is unprefixed for backward compat. */
217+
#ifndef SEED_BLOCK_SZ
218+
/* The seed block size, is the size of the output of the underlying NDRNG.
219+
* This value is used for testing the output of the NDRNG. */
220+
#if defined(HAVE_AMD_RDSEED)
221+
/* AMD's RDSEED instruction works in 128-bit blocks read 64-bits
222+
* at a time. */
223+
#define SEED_BLOCK_SZ (sizeof(word64)*2)
224+
#elif defined(HAVE_INTEL_RDSEED) || defined(HAVE_INTEL_RDRAND)
225+
/* RDSEED outputs in blocks of 64-bits. */
226+
#define SEED_BLOCK_SZ sizeof(word64)
227+
#else
228+
/* Setting the default to 4. */
229+
#define SEED_BLOCK_SZ 4
230+
#endif
231+
#endif
232+
233+
#define WC_DRBG_SEED_BLOCK_SZ SEED_BLOCK_SZ
234+
235+
#define WC_DRBG_SEED_SZ (RNG_SECURITY_STRENGTH*ENTROPY_SCALE_FACTOR/8)
236+
237+
/* The maximum seed size will be the seed size plus a seed block for the
238+
* test, and an additional half of the seed size. This additional half
239+
* is in case the user does not supply a nonce. A nonce will be obtained
240+
* from the NDRNG. */
241+
#define WC_DRBG_MAX_SEED_SZ (WC_DRBG_SEED_SZ + WC_DRBG_SEED_SZ/2 + \
242+
SEED_BLOCK_SZ)
243+
244+
#define RNG_HEALTH_TEST_CHECK_SIZE (WC_SHA256_DIGEST_SIZE * 4)
245+
246+
/* RNG health states */
247+
#define WC_DRBG_NOT_INIT 0
248+
#define WC_DRBG_OK 1
249+
#define WC_DRBG_FAILED 2
250+
#define WC_DRBG_CONT_FAILED 3
251+
166252
struct DRBG_internal {
167253
#ifdef WORD64_AVAILABLE
168254
word64 reseedCtr;

0 commit comments

Comments
 (0)