iPXE
drbg.h
Go to the documentation of this file.
1 #ifndef _IPXE_DRBG_H
2 #define _IPXE_DRBG_H
3 
4 /** @file
5  *
6  * DRBG mechanism
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 FILE_SECBOOT ( PERMITTED );
12 
13 #include <stdint.h>
14 #include <ipxe/sha256.h>
15 #include <ipxe/hmac_drbg.h>
16 
17 /** Choose HMAC_DRBG using SHA-256
18  *
19  * HMAC_DRBG using SHA-256 is an Approved algorithm in ANS X9.82.
20  */
21 #define HMAC_DRBG_ALGORITHM HMAC_DRBG_SHA256
22 
23 /** Maximum security strength */
24 #define DRBG_MAX_SECURITY_STRENGTH \
25  HMAC_DRBG_MAX_SECURITY_STRENGTH ( HMAC_DRBG_ALGORITHM )
26 
27 /** Security strength
28  *
29  * We choose to operate at a strength of 128 bits.
30  */
31 #define DRBG_SECURITY_STRENGTH 128
32 
33 /** Minimum entropy input length */
34 #define DRBG_MIN_ENTROPY_LEN_BYTES \
35  HMAC_DRBG_MIN_ENTROPY_LEN_BYTES ( DRBG_SECURITY_STRENGTH )
36 
37 /** Maximum entropy input length */
38 #define DRBG_MAX_ENTROPY_LEN_BYTES HMAC_DRBG_MAX_ENTROPY_LEN_BYTES
39 
40 /** Maximum personalisation string length */
41 #define DRBG_MAX_PERSONAL_LEN_BYTES HMAC_DRBG_MAX_PERSONAL_LEN_BYTES
42 
43 /** Maximum additional input length */
44 #define DRBG_MAX_ADDITIONAL_LEN_BYTES HMAC_DRBG_MAX_ADDITIONAL_LEN_BYTES
45 
46 /** Maximum length of generated pseudorandom data per request */
47 #define DRBG_MAX_GENERATED_LEN_BYTES HMAC_DRBG_MAX_GENERATED_LEN_BYTES
48 
49 /** A Deterministic Random Bit Generator */
50 struct drbg_state {
51  /** Algorithm internal state */
53  /** Reseed required flag */
55  /** State is valid */
56  int valid;
57 };
58 
59 /**
60  * Instantiate DRBG algorithm
61  *
62  * @v state Algorithm state
63  * @v entropy Entropy input
64  * @v entropy_len Length of entropy input
65  * @v personal Personalisation string
66  * @v personal_len Length of personalisation string
67  *
68  * This is the Instantiate_algorithm function defined in ANS X9.82
69  * Part 3-2007 Section 9.2 (NIST SP 800-90 Section 9.1).
70  */
71 static inline void drbg_instantiate_algorithm ( struct drbg_state *state,
72  const void *entropy,
73  size_t entropy_len,
74  const void *personal,
75  size_t personal_len ) {
77  &state->internal, entropy, entropy_len,
78  personal, personal_len );
79 }
80 
81 /**
82  * Reseed DRBG algorithm
83  *
84  * @v state Algorithm state
85  * @v entropy Entropy input
86  * @v entropy_len Length of entropy input
87  * @v additional Additional input
88  * @v additional_len Length of additional input
89  *
90  * This is the Reseed_algorithm function defined in ANS X9.82
91  * Part 3-2007 Section 9.3 (NIST SP 800-90 Section 9.2).
92  */
93 static inline void drbg_reseed_algorithm ( struct drbg_state *state,
94  const void *entropy,
95  size_t entropy_len,
96  const void *additional,
97  size_t additional_len ) {
99  &state->internal, entropy, entropy_len,
100  additional, additional_len );
101 }
102 
103 /**
104  * Generate pseudorandom bits using DRBG algorithm
105  *
106  * @v state Algorithm state
107  * @v additional Additional input
108  * @v additional_len Length of additional input
109  * @v data Output buffer
110  * @v len Length of output buffer
111  * @ret rc Return status code
112  *
113  * This is the Generate_algorithm function defined in ANS X9.82
114  * Part 3-2007 Section 9.4 (NIST SP 800-90 Section 9.3).
115  *
116  * Note that the only permitted error is "reseed required".
117  */
118 static inline int drbg_generate_algorithm ( struct drbg_state *state,
119  const void *additional,
120  size_t additional_len,
121  void *data, size_t len ) {
123  &state->internal, additional,
124  additional_len, data, len );
125 }
126 
127 extern int drbg_instantiate ( struct drbg_state *state, const void *personal,
128  size_t personal_len );
129 extern int drbg_reseed ( struct drbg_state *state, const void *additional,
130  size_t additional_len );
131 extern int drbg_generate ( struct drbg_state *state, const void *additional,
132  size_t additional_len, int prediction_resist,
133  void *data, size_t len );
134 extern void drbg_uninstantiate ( struct drbg_state *state );
135 
136 #endif /* _IPXE_DRBG_H */
int hmac_drbg_generate(struct digest_algorithm *hash, struct hmac_drbg_state *state, const void *additional, size_t additional_len, void *data, size_t len)
Generate pseudorandom bits using HMAC_DRBG.
Definition: hmac_drbg.c:307
int reseed_required
Reseed required flag.
Definition: drbg.h:54
uint8_t state
State.
Definition: eth_slow.h:48
void hmac_drbg_instantiate(struct digest_algorithm *hash, struct hmac_drbg_state *state, const void *entropy, size_t entropy_len, const void *personal, size_t personal_len)
Instantiate HMAC_DRBG.
Definition: hmac_drbg.c:207
static void drbg_instantiate_algorithm(struct drbg_state *state, const void *entropy, size_t entropy_len, const void *personal, size_t personal_len)
Instantiate DRBG algorithm.
Definition: drbg.h:71
struct hmac_drbg_state internal
Algorithm internal state.
Definition: drbg.h:52
int drbg_instantiate(struct drbg_state *state, const void *personal, size_t personal_len)
Instantiate DRBG.
Definition: drbg.c:79
void hmac_drbg_reseed(struct digest_algorithm *hash, struct hmac_drbg_state *state, const void *entropy, size_t entropy_len, const void *additional, size_t additional_len)
Reseed HMAC_DRBG.
Definition: hmac_drbg.c:256
A Deterministic Random Bit Generator.
Definition: drbg.h:50
void drbg_uninstantiate(struct drbg_state *state)
Uninstantiate DRBG.
Definition: drbg.c:424
#define HMAC_DRBG_HASH(hmac_drbg)
Underlying hash algorithm.
Definition: hmac_drbg.h:91
#define HMAC_DRBG_ALGORITHM
Choose HMAC_DRBG using SHA-256.
Definition: drbg.h:21
static void drbg_reseed_algorithm(struct drbg_state *state, const void *entropy, size_t entropy_len, const void *additional, size_t additional_len)
Reseed DRBG algorithm.
Definition: drbg.h:93
int valid
State is valid.
Definition: drbg.h:56
uint16_t additional
Additional sense code and qualifier.
Definition: scsi.h:28
static int drbg_generate_algorithm(struct drbg_state *state, const void *additional, size_t additional_len, void *data, size_t len)
Generate pseudorandom bits using DRBG algorithm.
Definition: drbg.h:118
ring len
Length.
Definition: dwmac.h:231
HMAC_DRBG internal state.
Definition: hmac_drbg.h:219
int drbg_generate(struct drbg_state *state, const void *additional, size_t additional_len, int prediction_resist, void *data, size_t len)
Generate pseudorandom bits using DRBG.
Definition: drbg.c:284
uint8_t data[48]
Additional event data.
Definition: ena.h:22
FILE_SECBOOT(PERMITTED)
int drbg_reseed(struct drbg_state *state, const void *additional, size_t additional_len)
Reseed DRBG.
Definition: drbg.c:191
SHA-256 algorithm.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
HMAC_DRBG algorithm.