iPXE
entropy.h File Reference

Entropy source. More...

#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <ipxe/hash_df.h>
#include <ipxe/sha256.h>
#include <ipxe/tables.h>
#include <config/entropy.h>

Go to the source code of this file.

Data Structures

struct  entropy_repetition_count_test
 Repetition count test state. More...
struct  entropy_adaptive_proportion_test
 Adaptive proportion test state. More...
struct  entropy_startup_test
 Startup test state. More...
struct  entropy_source
 An entropy source. More...

Macros

#define MIN_ENTROPY_SCALE   ( 1 << 16 )
 Fixed-point scale for min-entropy amounts.
#define MIN_ENTROPY(bits)
 Construct a min-entropy fixed-point value.
#define ENTROPY_SOURCES   __table ( struct entropy_source, "entropy_sources" )
 Entropy source table.
#define __entropy_source(order)
 Declare an entropy source.
#define ENTROPY_PREFERRED   01
 Preferred entropy source.
#define ENTROPY_NORMAL   02
 Normal entropy source.
#define ENTROPY_FALLBACK   03
 Fallback entropy source.
#define entropy_hash_df_algorithm   sha256_algorithm
 Use SHA-256 as the underlying hash algorithm for Hash_df.
#define ENTROPY_HASH_DF_OUTLEN_BYTES   SHA256_DIGEST_SIZE
 Underlying hash algorithm output length (in bytes)
#define ADAPTIVE_PROPORTION_WINDOW_SIZE   64
 Window size for the adaptive proportion test.
#define APC_N_H(n, h)
 Combine adaptive proportion test window size and min-entropy.
#define APC_TABLE_ROW(h, c16, c64, c256, c4096, c65536)
 Define a row of the adaptive proportion cutoff table.
#define APC_NA   0
 Value used to represent "N/A" in adaptive proportion cutoff table.

Typedefs

typedef uint8_t noise_sample_t
 A noise sample.
typedef uint8_t entropy_sample_t
 An entropy sample.
typedef unsigned int min_entropy_t
 An amount of min-entropy.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int get_entropy_input_tmp (min_entropy_t min_entropy, uint8_t *tmp, size_t tmp_len)
 Obtain entropy input temporary buffer.
static int get_noise (struct entropy_source *source, noise_sample_t *noise)
 Get noise sample.
static int get_entropy_input (unsigned int min_entropy_bits, void *data, size_t min_len, size_t max_len)
 Obtain entropy input.
static unsigned int entropy_repetition_count_cutoff (min_entropy_t min_entropy_per_sample)
 Calculate cutoff value for the repetition count test.
static unsigned int entropy_adaptive_proportion_cutoff_lookup (unsigned int n, unsigned int h)
 Look up value in adaptive proportion test cutoff table.
static unsigned int entropy_adaptive_proportion_cutoff (min_entropy_t min_entropy_per_sample)
 Calculate cutoff value for the adaptive proportion test.
static unsigned int entropy_startup_test_count (unsigned int repetition_count_cutoff, unsigned int adaptive_proportion_cutoff)
 Calculate number of samples required for startup tests.
static void entropy_init (struct entropy_source *source, min_entropy_t min_entropy_per_sample)
 Initialise entropy source.
int entropy_enable (struct entropy_source *source)
 Enable entropy gathering.
void entropy_disable (struct entropy_source *source)
 Disable entropy gathering.

Detailed Description

Entropy source.

Definition in file entropy.h.

Macro Definition Documentation

◆ MIN_ENTROPY_SCALE

#define MIN_ENTROPY_SCALE   ( 1 << 16 )

Fixed-point scale for min-entropy amounts.

Definition at line 35 of file entropy.h.

Referenced by entropy_adaptive_proportion_cutoff(), and get_entropy_input_tmp().

◆ MIN_ENTROPY

#define MIN_ENTROPY ( bits)
Value:
static volatile void * bits
Definition bitops.h:28
#define MIN_ENTROPY_SCALE
Fixed-point scale for min-entropy amounts.
Definition entropy.h:35
unsigned int min_entropy_t
An amount of min-entropy.
Definition entropy.h:32

Construct a min-entropy fixed-point value.

Parameters
bitsmin-entropy in bits
Return values
min_entropymin-entropy as a fixed-point value

Definition at line 43 of file entropy.h.

43#define MIN_ENTROPY( bits ) \
44 ( ( min_entropy_t ) ( (bits) * MIN_ENTROPY_SCALE ) )

Referenced by efi_entropy_enable(), efirng_enable(), entropy_init(), entropy_repetition_count_cutoff(), get_entropy_input(), get_entropy_input_tmp(), rdrand_entropy_enable(), and rtc_entropy_enable().

◆ ENTROPY_SOURCES

#define ENTROPY_SOURCES   __table ( struct entropy_source, "entropy_sources" )

Entropy source table.

Definition at line 170 of file entropy.h.

Referenced by entropy_enable_working(), and entropy_sample_test_exec().

◆ __entropy_source

#define __entropy_source ( order)
Value:
#define ENTROPY_SOURCES
Entropy source table.
Definition entropy.h:170
#define __table_entry(table, idx)
Declare a linker table entry.
Definition tables.h:239

Declare an entropy source.

Definition at line 173 of file entropy.h.

Referenced by __entropy_source(), __entropy_source(), and __entropy_source().

◆ entropy_hash_df_algorithm

#define entropy_hash_df_algorithm   sha256_algorithm

Use SHA-256 as the underlying hash algorithm for Hash_df.

Hash_df using SHA-256 is an Approved algorithm in ANS X9.82.

Definition at line 193 of file entropy.h.

Referenced by get_entropy_input(), and get_entropy_input_tmp().

◆ ENTROPY_HASH_DF_OUTLEN_BYTES

#define ENTROPY_HASH_DF_OUTLEN_BYTES   SHA256_DIGEST_SIZE

Underlying hash algorithm output length (in bytes)

Definition at line 196 of file entropy.h.

Referenced by get_entropy_input().

◆ ADAPTIVE_PROPORTION_WINDOW_SIZE

#define ADAPTIVE_PROPORTION_WINDOW_SIZE   64

Window size for the adaptive proportion test.

ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.1.1 allows five possible window sizes: 16, 64, 256, 4096 and 65536.

We expect to generate relatively few (<256) entropy samples during a typical iPXE run; the use of a large window size would mean that the test would never complete a single cycle. We use a window size of 64, which is the smallest window size that permits values of H_min down to one bit per sample.

Definition at line 354 of file entropy.h.

Referenced by adaptive_proportion_test(), adaptive_proportion_test_init(), and entropy_adaptive_proportion_cutoff().

◆ APC_N_H

#define APC_N_H ( n,
h )
Value:
( ( (n) << 8 ) | (h) )
uint8_t h
Definition registers.h:4

Combine adaptive proportion test window size and min-entropy.

Parameters
nN (window size)
hH (min-entropy)
Return values
n_h(N,H) combined value

Definition at line 363 of file entropy.h.

Referenced by entropy_adaptive_proportion_cutoff_lookup().

◆ APC_TABLE_ROW

#define APC_TABLE_ROW ( h,
c16,
c64,
c256,
c4096,
c65536 )
Value:
case APC_N_H ( 16, h ) : return c16; \
case APC_N_H ( 64, h ) : return c64; \
case APC_N_H ( 256, h ) : return c256; \
case APC_N_H ( 4096, h ) : return c4096; \
case APC_N_H ( 65536, h ) : return c65536;
#define APC_N_H(n, h)
Combine adaptive proportion test window size and min-entropy.
Definition entropy.h:363
return
Definition natsemi.h:326

Define a row of the adaptive proportion cutoff table.

Parameters
hH (min-entropy)
c16Cutoff for N=16
c64Cutoff for N=64
c256Cutoff for N=256
c4096Cutoff for N=4096
c65536Cutoff for N=65536

Definition at line 375 of file entropy.h.

375#define APC_TABLE_ROW( h, c16, c64, c256, c4096, c65536) \
376 case APC_N_H ( 16, h ) : return c16; \
377 case APC_N_H ( 64, h ) : return c64; \
378 case APC_N_H ( 256, h ) : return c256; \
379 case APC_N_H ( 4096, h ) : return c4096; \
380 case APC_N_H ( 65536, h ) : return c65536;

Referenced by entropy_adaptive_proportion_cutoff_lookup().

◆ APC_NA

#define APC_NA   0

Value used to represent "N/A" in adaptive proportion cutoff table.

Definition at line 383 of file entropy.h.

Referenced by entropy_adaptive_proportion_cutoff(), and entropy_adaptive_proportion_cutoff_lookup().

Typedef Documentation

◆ noise_sample_t

A noise sample.

Definition at line 22 of file entropy.h.

◆ entropy_sample_t

An entropy sample.

Definition at line 25 of file entropy.h.

◆ min_entropy_t

typedef unsigned int min_entropy_t

An amount of min-entropy.

Expressed as a fixed-point quantity in order to avoid floating point calculations.

Definition at line 32 of file entropy.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ get_entropy_input_tmp()

int get_entropy_input_tmp ( min_entropy_t min_entropy,
uint8_t * tmp,
size_t tmp_len )
extern

Obtain entropy input temporary buffer.

Parameters
min_entropyMin-entropy required
tmpTemporary buffer
tmp_lenLength of temporary buffer
Return values
rcReturn status code

This is (part of) the implementation of the Get_entropy_input function (using an entropy source as the source of entropy input and condensing each entropy source output after each GetEntropy call) as defined in ANS X9.82 Part 4 (April 2011 Draft) Section 13.3.4.2.

Definition at line 426 of file entropy.c.

427 {
428 struct entropy_source *source;
429 struct {
431 entropy_sample_t sample;
432 } __attribute__ (( packed )) data;;
433 uint8_t df_buf[tmp_len];
434 min_entropy_t entropy_total;
435 unsigned int num_samples;
436 unsigned int i;
437 int rc;
438
439 /* Enable entropy gathering */
440 if ( ( rc = entropy_enable_working ( &source ) ) != 0 )
441 goto err_enable_working;
442
443 /* Sanity checks */
444 assert ( source->startup_test.count > 0 );
445 assert ( source->startup_test.tested >= source->startup_test.count );
446
447 /* 3. entropy_total = 0 */
448 entropy_total = MIN_ENTROPY ( 0 );
449
450 /* 4. tmp = a fixed n-bit value, such as 0^n */
451 memset ( tmp, 0, tmp_len );
452
453 /* 5. While ( entropy_total < min_entropy ) */
454 for ( num_samples = 0 ; entropy_total < min_entropy ; num_samples++ ) {
455 /* 5.1. ( status, entropy_bitstring, assessed_entropy )
456 * = GetEntropy()
457 * 5.2. If status indicates an error, return ( status, Null )
458 */
459 if ( ( rc = get_entropy ( source, &data.sample ) ) != 0 )
460 goto err_get_entropy;
461
462 /* 5.3. nonce = MakeNextNonce() */
463 data.nonce = make_next_nonce();
464
465 /* 5.4. tmp = tmp XOR
466 * df ( ( nonce || entropy_bitstring ), n )
467 */
469 df_buf, sizeof ( df_buf ) );
470 for ( i = 0 ; i < tmp_len ; i++ )
471 tmp[i] ^= df_buf[i];
472
473 /* 5.5. entropy_total = entropy_total + assessed_entropy */
474 entropy_total += source->min_entropy_per_sample;
475 }
476
477 /* Disable entropy gathering */
478 entropy_disable ( source );
479
480 DBGC ( source, "ENTROPY %s gathered %d bits in %d samples\n",
481 source->name, ( min_entropy / MIN_ENTROPY_SCALE ), num_samples );
482 return 0;
483
484 err_get_entropy:
485 entropy_disable ( source );
486 assert ( source->rc == rc );
487 err_enable_working:
488 return rc;
489}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned int uint32_t
Definition stdint.h:12
unsigned char uint8_t
Definition stdint.h:10
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
uint8_t data[48]
Additional event data.
Definition ena.h:11
void entropy_disable(struct entropy_source *source)
Disable entropy gathering.
Definition entropy.c:386
static int entropy_enable_working(struct entropy_source **source)
Enable first working entropy source.
Definition entropy.c:366
static int get_entropy(struct entropy_source *source, entropy_sample_t *entropy)
Get entropy sample.
Definition entropy.c:226
static uint32_t make_next_nonce(void)
Create next nonce value.
Definition entropy.c:403
#define DBGC(...)
Definition compiler.h:505
void hash_df(struct digest_algorithm *hash, const void *input, size_t input_len, void *output, size_t output_len)
Distribute entropy throughout a buffer.
Definition hash_df.c:85
#define __attribute__(x)
Definition compiler.h:10
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition entropy.h:43
uint8_t entropy_sample_t
An entropy sample.
Definition entropy.h:25
#define entropy_hash_df_algorithm
Use SHA-256 as the underlying hash algorithm for Hash_df.
Definition entropy.h:193
void * memset(void *dest, int character, size_t len) __nonnull
unsigned long tmp
Definition linux_pci.h:65
An entropy source.
Definition entropy.h:117
min_entropy_t min_entropy_per_sample
min-entropy per sample
Definition entropy.h:132
int rc
Failure status (if any)
Definition entropy.h:144
struct entropy_startup_test startup_test
Startup test state.
Definition entropy.h:138
const char * name
Name.
Definition entropy.h:119
unsigned int count
Number of startup tests required for one full cycle.
Definition entropy.h:113
unsigned int tested
Number of startup tests performed.
Definition entropy.h:107
u8 nonce[32]
Nonce value.
Definition wpa.h:25

References __attribute__, assert, entropy_startup_test::count, data, DBGC, entropy_disable(), entropy_enable_working(), entropy_hash_df_algorithm, get_entropy(), hash_df(), make_next_nonce(), memset(), MIN_ENTROPY, entropy_source::min_entropy_per_sample, MIN_ENTROPY_SCALE, entropy_source::name, nonce, entropy_source::rc, rc, entropy_source::startup_test, entropy_startup_test::tested, and tmp.

Referenced by get_entropy_input().

◆ get_noise()

int get_noise ( struct entropy_source * source,
noise_sample_t * noise )
inlinestatic

Get noise sample.

Parameters
sourceEntropy source
Return values
noiseNoise sample
rcReturn status code

This is the GetNoise function defined in ANS X9.82 Part 2 (October 2011 Draft) Section 6.5.2.

Definition at line 209 of file entropy.h.

209 {
210
211 return source->get_noise ( noise );
212}
int(* get_noise)(noise_sample_t *noise)
Get noise sample.
Definition entropy.h:166

References entropy_source::get_noise.

Referenced by entropy_sample(), and get_entropy().

◆ get_entropy_input()

int get_entropy_input ( unsigned int min_entropy_bits,
void * data,
size_t min_len,
size_t max_len )
inlinestatic

Obtain entropy input.

Parameters
min_entropy_bitsMinimum amount of entropy, in bits
dataData buffer
min_lenMinimum length of entropy input, in bytes
max_lenMaximum length of entropy input, in bytes
Return values
lenLength of entropy input, in bytes, or negative error

This is the implementation of the Get_entropy_input function (using an entropy source as the source of entropy input and condensing each entropy source output after each GetEntropy call) as defined in ANS X9.82 Part 4 (April 2011 Draft) Section 13.3.4.2.

This function is inlined since the entropy amount and length inputs are always compile-time constants.

Definition at line 232 of file entropy.h.

233 {
234 size_t tmp_len = ( ( ( min_entropy_bits * 2 ) + 7 ) / 8 );
235 uint8_t tmp_buf[ tmp_len ];
236 uint8_t *tmp = ( ( tmp_len > max_len ) ? tmp_buf : data );
237 unsigned int n;
238 int rc;
239
240 /* Sanity check */
241 build_assert ( min_entropy_bits <= ( 8 * max_len ) );
242
243 /* Round up minimum entropy to an integral number of bytes */
244 min_entropy_bits = ( ( min_entropy_bits + 7 ) & ~7 );
245
246 /* (Unnumbered). The output length of the hash function shall
247 * meet or exceed the security strength indicated by the
248 * min_entropy parameter.
249 */
251 min_entropy_bits );
252
253 /* 1. If ( min_length > max_length ), then return ( FAILURE, Null ) */
254 build_assert ( min_len <= max_len );
255
256 /* 2. n = 2 * min_entropy */
257 n = ( 2 * min_entropy_bits );
258
259 /* 3. entropy_total = 0
260 * 4. tmp = a fixed n-bit value, such as 0^n
261 * 5. While ( entropy_total < min_entropy )
262 * 5.1. ( status, entropy_bitstring, assessed_entropy )
263 * = GetEntropy()
264 * 5.2. If status indicates an error, return ( status, Null )
265 * 5.3. nonce = MakeNextNonce()
266 * 5.4. tmp = tmp XOR df ( ( nonce || entropy_bitstring ), n )
267 * 5.5. entropy_total = entropy_total + assessed_entropy
268 *
269 * (The implementation of these steps is inside the function
270 * get_entropy_input_tmp().)
271 */
272 build_assert ( __builtin_constant_p ( tmp_len ) );
273 build_assert ( n == ( 8 * tmp_len ) );
274 if ( ( rc = get_entropy_input_tmp ( MIN_ENTROPY ( min_entropy_bits ),
275 tmp, tmp_len ) ) != 0 ) {
276 return rc;
277 }
278
279 /* 6. If ( n < min_length ), then tmp = tmp || 0^(min_length-n)
280 * 7. If ( n > max_length ), then tmp = df ( tmp, max_length )
281 * 8. Return ( SUCCESS, tmp )
282 */
283 if ( tmp_len < min_len ) {
284 /* (Data is already in-place.) */
285 build_assert ( data == tmp );
286 memset ( ( data + tmp_len ), 0, ( min_len - tmp_len ) );
287 return min_len;
288 } else if ( tmp_len > max_len ) {
289 build_assert ( tmp == tmp_buf );
291 data, max_len );
292 return max_len;
293 } else {
294 /* (Data is already in-place.) */
295 build_assert ( data == tmp );
296 return tmp_len;
297 }
298}
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition assert.h:77
int get_entropy_input_tmp(min_entropy_t min_entropy, uint8_t *tmp, size_t tmp_len)
Obtain entropy input temporary buffer.
Definition entropy.c:426
#define ENTROPY_HASH_DF_OUTLEN_BYTES
Underlying hash algorithm output length (in bytes)
Definition entropy.h:196

References build_assert, data, entropy_hash_df_algorithm, ENTROPY_HASH_DF_OUTLEN_BYTES, get_entropy_input_tmp(), hash_df(), memset(), MIN_ENTROPY, rc, and tmp.

Referenced by drbg_instantiate(), and drbg_reseed().

◆ entropy_repetition_count_cutoff()

unsigned int entropy_repetition_count_cutoff ( min_entropy_t min_entropy_per_sample)
inlinestatic

Calculate cutoff value for the repetition count test.

Parameters
min_entropy_per_sampleMin-entropy per sample
Return values
cutoffCutoff value

This is the cutoff value for the Repetition Count Test defined in ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.2.

Definition at line 310 of file entropy.h.

310 {
311 double max_repetitions;
312 unsigned int cutoff;
313
314 /* The cutoff formula for the repetition test is:
315 *
316 * C = ( 1 + ( -log2(W) / H_min ) )
317 *
318 * where W is set at 2^(-30) (in ANS X9.82 Part 2 (October
319 * 2011 Draft) Section 8.5.2.1.3.1).
320 */
321 max_repetitions = ( 1 + ( MIN_ENTROPY ( 30 ) /
322 min_entropy_per_sample ) );
323
324 /* Round up to a whole number of repetitions. We don't have
325 * the ceil() function available, so do the rounding by hand.
326 */
327 cutoff = max_repetitions;
328 if ( cutoff < max_repetitions )
329 cutoff++;
330 build_assert ( cutoff >= max_repetitions );
331
332 /* Floating-point operations are not allowed in iPXE since we
333 * never set up a suitable environment. Abort the build
334 * unless the calculated number of repetitions is a
335 * compile-time constant.
336 */
337 build_assert ( __builtin_constant_p ( cutoff ) );
338
339 return cutoff;
340}

References build_assert, and MIN_ENTROPY.

Referenced by entropy_init().

◆ entropy_adaptive_proportion_cutoff_lookup()

unsigned int entropy_adaptive_proportion_cutoff_lookup ( unsigned int n,
unsigned int h )
inlinestatic

Look up value in adaptive proportion test cutoff table.

Parameters
nN (window size)
hH (min-entropy)
Return values
cutoffCutoff

This is the table of cutoff values defined in ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.1.2.

Definition at line 396 of file entropy.h.

396 {
397 switch ( APC_N_H ( n, h ) ) {
398 APC_TABLE_ROW ( 1, APC_NA, 51, 168, 2240, 33537 );
399 APC_TABLE_ROW ( 2, APC_NA, 35, 100, 1193, 17053 );
400 APC_TABLE_ROW ( 3, 10, 24, 61, 643, 8705 );
401 APC_TABLE_ROW ( 4, 8, 16, 38, 354, 4473 );
402 APC_TABLE_ROW ( 5, 6, 12, 25, 200, 2321 );
403 APC_TABLE_ROW ( 6, 5, 9, 17, 117, 1220 );
404 APC_TABLE_ROW ( 7, 4, 7, 15, 71, 653 );
405 APC_TABLE_ROW ( 8, 4, 5, 9, 45, 358 );
406 APC_TABLE_ROW ( 9, 3, 4, 7, 30, 202 );
407 APC_TABLE_ROW ( 10, 3, 4, 5, 21, 118 );
408 APC_TABLE_ROW ( 11, 2, 3, 4, 15, 71 );
409 APC_TABLE_ROW ( 12, 2, 3, 4, 11, 45 );
410 APC_TABLE_ROW ( 13, 2, 2, 3, 9, 30 );
411 APC_TABLE_ROW ( 14, 2, 2, 3, 7, 21 );
412 APC_TABLE_ROW ( 15, 1, 2, 2, 6, 15 );
413 APC_TABLE_ROW ( 16, 1, 2, 2, 5, 11 );
414 APC_TABLE_ROW ( 17, 1, 1, 2, 4, 9 );
415 APC_TABLE_ROW ( 18, 1, 1, 2, 4, 7 );
416 APC_TABLE_ROW ( 19, 1, 1, 1, 3, 6 );
417 APC_TABLE_ROW ( 20, 1, 1, 1, 3, 5 );
418 default:
419 return APC_NA;
420 }
421}
#define APC_TABLE_ROW(h, c16, c64, c256, c4096, c65536)
Define a row of the adaptive proportion cutoff table.
Definition entropy.h:375
#define APC_NA
Value used to represent "N/A" in adaptive proportion cutoff table.
Definition entropy.h:383

References APC_N_H, APC_NA, APC_TABLE_ROW, and h.

Referenced by entropy_adaptive_proportion_cutoff().

◆ entropy_adaptive_proportion_cutoff()

unsigned int entropy_adaptive_proportion_cutoff ( min_entropy_t min_entropy_per_sample)
inlinestatic

Calculate cutoff value for the adaptive proportion test.

Parameters
min_entropy_per_sampleMin-entropy per sample
Return values
cutoffCutoff value

This is the cutoff value for the Adaptive Proportion Test defined in ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.1.2.

Definition at line 433 of file entropy.h.

433 {
434 unsigned int h;
435 unsigned int n;
436 unsigned int cutoff;
437
438 /* Look up cutoff value in cutoff table */
440 h = ( min_entropy_per_sample / MIN_ENTROPY_SCALE );
442
443 /* Fail unless cutoff value is a compile-time constant */
444 build_assert ( __builtin_constant_p ( cutoff ) );
445
446 /* Fail if cutoff value is N/A */
447 build_assert ( cutoff != APC_NA );
448
449 return cutoff;
450}
static unsigned int entropy_adaptive_proportion_cutoff_lookup(unsigned int n, unsigned int h)
Look up value in adaptive proportion test cutoff table.
Definition entropy.h:396
#define ADAPTIVE_PROPORTION_WINDOW_SIZE
Window size for the adaptive proportion test.
Definition entropy.h:354

References ADAPTIVE_PROPORTION_WINDOW_SIZE, APC_NA, build_assert, entropy_adaptive_proportion_cutoff_lookup(), h, and MIN_ENTROPY_SCALE.

Referenced by entropy_init().

◆ entropy_startup_test_count()

unsigned int entropy_startup_test_count ( unsigned int repetition_count_cutoff,
unsigned int adaptive_proportion_cutoff )
inlinestatic

Calculate number of samples required for startup tests.

Parameters
repetition_count_cutoffRepetition count test cutoff value
adaptive_proportion_cutoffAdaptive proportion test cutoff value
Return values
num_samplesNumber of samples required

ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.5 requires that at least one full cycle of the continuous tests must be performed at start-up.

Definition at line 464 of file entropy.h.

465 {
466 unsigned int num_samples;
467
468 /* At least max(N,C) samples shall be generated by the noise
469 * source for start-up testing.
470 */
471 num_samples = repetition_count_cutoff;
472 if ( num_samples < adaptive_proportion_cutoff )
473 num_samples = adaptive_proportion_cutoff;
474 build_assert ( __builtin_constant_p ( num_samples ) );
475
476 return num_samples;
477}

References build_assert.

Referenced by entropy_init().

◆ entropy_init()

void entropy_init ( struct entropy_source * source,
min_entropy_t min_entropy_per_sample )
inlinestatic

Initialise entropy source.

Parameters
sourceEntropy source
min_entropy_per_sampleMin-entropy per sample

The cutoff value calculations for the repetition count test and the adaptive proportion test are provided as static inline functions since the results will always be compile-time constants.

Definition at line 490 of file entropy.h.

491 {
492 unsigned int repetition_count_cutoff;
493 unsigned int adaptive_proportion_cutoff;
494 unsigned int startup_test_count;
495
496 /* Sanity check */
497 build_assert ( min_entropy_per_sample > MIN_ENTROPY ( 0 ) );
498 build_assert ( min_entropy_per_sample <=
499 MIN_ENTROPY ( 8 * sizeof ( noise_sample_t ) ) );
500
501 /* Calculate test cutoff values */
502 repetition_count_cutoff =
503 entropy_repetition_count_cutoff ( min_entropy_per_sample );
504 adaptive_proportion_cutoff =
505 entropy_adaptive_proportion_cutoff ( min_entropy_per_sample );
506 startup_test_count =
507 entropy_startup_test_count ( repetition_count_cutoff,
508 adaptive_proportion_cutoff );
509
510 /* Record min-entropy per sample and test cutoff values */
511 source->min_entropy_per_sample = min_entropy_per_sample;
512 source->repetition_count_test.cutoff = repetition_count_cutoff;
513 source->adaptive_proportion_test.cutoff = adaptive_proportion_cutoff;
514 source->startup_test.count = startup_test_count;
515}
static unsigned int entropy_adaptive_proportion_cutoff(min_entropy_t min_entropy_per_sample)
Calculate cutoff value for the adaptive proportion test.
Definition entropy.h:433
uint8_t noise_sample_t
A noise sample.
Definition entropy.h:22
static unsigned int entropy_startup_test_count(unsigned int repetition_count_cutoff, unsigned int adaptive_proportion_cutoff)
Calculate number of samples required for startup tests.
Definition entropy.h:464
static unsigned int entropy_repetition_count_cutoff(min_entropy_t min_entropy_per_sample)
Calculate cutoff value for the repetition count test.
Definition entropy.h:310
unsigned int cutoff
C = the cutoff value above which the repetition test should fail.
Definition entropy.h:95
unsigned int cutoff
C = the cutoff value above which the repetition test should fail.
Definition entropy.h:66
struct entropy_repetition_count_test repetition_count_test
Repetition count test state.
Definition entropy.h:134
struct entropy_adaptive_proportion_test adaptive_proportion_test
Adaptive proportion test state.
Definition entropy.h:136

References entropy_source::adaptive_proportion_test, build_assert, entropy_startup_test::count, entropy_adaptive_proportion_test::cutoff, entropy_repetition_count_test::cutoff, entropy_adaptive_proportion_cutoff(), entropy_repetition_count_cutoff(), entropy_startup_test_count(), MIN_ENTROPY, entropy_source::min_entropy_per_sample, entropy_source::repetition_count_test, and entropy_source::startup_test.

Referenced by efi_entropy_enable(), efirng_enable(), rdrand_entropy_enable(), and rtc_entropy_enable().

◆ entropy_enable()

int entropy_enable ( struct entropy_source * source)
extern

Enable entropy gathering.

Parameters
sourceEntropy source
Return values
rcReturn status code

Definition at line 303 of file entropy.c.

303 {
304 int rc;
305
306 /* Refuse to enable a previously failed source */
307 if ( ( rc = source->rc ) != 0 )
308 return rc;
309
310 /* Enable entropy source */
311 if ( ( rc = source->enable() ) != 0 ) {
312 DBGC ( source, "ENTROPY %s could not enable: %s\n",
313 source->name, strerror ( rc ) );
314 source->rc = rc;
315 return rc;
316 }
317
318 /* Sanity check */
319 assert ( source->min_entropy_per_sample > 0 );
320
321 /* Initialise test state if this source has not previously been used */
322 if ( source->startup_test.tested == 0 ) {
325 startup_test_init ( source );
326 }
327
328 DBGC ( source, "ENTROPY %s enabled\n", source->name );
329 return 0;
330}
static void startup_test_init(struct entropy_source *source)
Initialise startup test.
Definition entropy.c:266
static void repetition_count_test_init(struct entropy_source *source)
Initialise repetition count test.
Definition entropy.c:59
static void adaptive_proportion_test_init(struct entropy_source *source)
Initialise adaptive proportion test.
Definition entropy.c:129
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
int(* enable)(void)
Enable entropy gathering.
Definition entropy.h:151

References adaptive_proportion_test_init(), assert, DBGC, entropy_source::enable, entropy_source::min_entropy_per_sample, entropy_source::name, entropy_source::rc, rc, repetition_count_test_init(), entropy_source::startup_test, startup_test_init(), strerror(), and entropy_startup_test::tested.

Referenced by entropy_enable_and_test(), entropy_sample(), and REQUIRING_SYMBOL().

◆ entropy_disable()

void entropy_disable ( struct entropy_source * source)
extern

Disable entropy gathering.

Parameters
sourceEntropy source

Definition at line 386 of file entropy.c.

386 {
387
388 /* Disable entropy gathering, if applicable */
389 if ( source->disable )
390 source->disable();
391
392 DBGC ( source, "ENTROPY %s disabled\n", source->name );
393}
void(* disable)(void)
Disable entropy gathering.
Definition entropy.h:156

References DBGC, entropy_source::disable, and entropy_source::name.

Referenced by entropy_enable_and_test(), entropy_sample(), and get_entropy_input_tmp().