Software APIs
dif_csrng.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_DIF_DIF_CSRNG_H_
6 #define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_CSRNG_H_
7 
8 /**
9  * @file
10  * @brief <a href="/hw/ip/csrng/doc/">CSRNG</a> Device Interface Functions
11  */
12 
13 #include <stdint.h>
14 
18 
19 #include "csrng_regs.h" // Generated
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif // __cplusplus
25 
26 /**
27  * This API implements an interface for the CSRNG hardware.
28  *
29  * The API follows the naming conventions used in NIST SP 800-90Ar1:
30  *
31  * - Instantiate.
32  * - Update
33  * - Reseed
34  * - Generate
35  * - Uninstantiate
36  *
37  * The seed used for instantiation can either be provided directly in
38  * hardware by an entropy source module; or directly by software. See
39  * `dif_csrng_instantiate()` and `dif_csrng_entropy_src` for
40  * more details.
41  *
42  * The following sequence of operations are required to initialize
43  * and generate cryptographic entropy from the CSRNG module:
44  *
45  * - `dif_csrng_init()`
46  * - `dif_csrng_configure()`
47  * - `dif_csrng_instantiate()`
48  * - `dif_csrng_generate_start()`
49  * - `dif_csrng_uninstantiate()`
50  *
51  * The following functions can be used for reseed and update operations:
52  *
53  * - `dif_csrng_reseed()`
54  * - `dif_csrng_update()`
55  *
56  * The following utility functions are available to poll the state of
57  * the hardware:
58  *
59  * - `dif_csrng_get_cmd_interface_status()`
60  * - `dif_csrng_get_output_status()`
61  *
62  * Please see the following documentation for more information:
63  * https://docs.opentitan.org/hw/ip/csrng/doc/
64  *
65  * Remaining work:
66  *
67  * - Add error status interface.
68  * - Add internal state control and debug interface.
69  */
70 
71 /**
72  * Enumeration of CSRNG command interface states.
73  */
75  /**
76  * The command interface is ready to accept commands.
77  */
79  /**
80  * The command interface is processing a command.
81  */
83  /**
84  * The command interface completed with an error.
85  */
88 
89 /**
90  * Enumeration of CSRNG FIFOs, which indicates which part of the hardware
91  * produced an error.
92  */
93 typedef enum dif_csrng_fifo {
94  kDifCsrngFifoCmd,
95  kDifCsrngFifoGenBits,
96  kDifCsrngFifoCmdReq,
97  kDifCsrngFifoRcStage,
98  kDifCsrngFifoKeyVrc,
99  kDifCsrngFifoUpdateReq,
100  kDifCsrngFifoBencRec,
101  kDifCsrngFifoBencAck,
102  kDifCsrngFifoPData,
103  kDifCsrngFifoFinal,
104  kDifCsrngFifoGBencAck,
105  kDifCsrngFifoGrcStage,
106  kDifCsrngFifoGGenReq,
107  kDifCsrngFifoGadStage,
108  kDifCsrngFifoBlockEnc,
110 
111 /**
112  * Enumeration of CSRNG FIFO errors.
113  */
114 typedef enum dif_csrng_error {
115  /**
116  * Indicates an error in the command stage state machine.
117  */
119  /**
120  * Indicates an error in the main state machine.
121  */
123  /**
124  * Indicates an error in the DRBG's generator state machine.
125  */
127  /**
128  * Indicates an error in the DRBG's block encoding state machine.
129  */
131  /**
132  * Indicates an error in the DRBG's block output state machine.
133  */
135  /**
136  * Indicates an error in the AES state machine.
137  */
139  /**
140  * Indicates an error in the generate command's counter.
141  */
143  /**
144  * Indicates a write to a full FIFO occured.
145  */
147  /**
148  * Indicates a read from an empty FIFO occured.
149  */
151  /**
152  * Indicates a FIFO was somehow both full and empty.
153  */
156 
157 /**
158  * Enumeration of CSRNG command status errors.
159  */
161  /**
162  * Indicates that the command completed successfully.
163  */
165  /**
166  * Indicates that an invalid apllication command has been issued.
167  */
169  /**
170  * Indicates that the state wasn't zeroized properly after an uninstantiate
171  * command due to invalid state parameters in the cmd drbg.
172  */
174  /**
175  * Indicates that CSRNG entropy was generated for a command that is not a
176  * generate command.
177  */
180 
181 /**
182  * The status of the CSRNG block at a particular moment in time.
183  */
184 typedef struct dif_csrng_cmd_status {
185  /**
186  * The kind of status the CSRNG is in.
187  */
189  /**
190  * The status value CSRNG returns.
191  */
194 
195 /**
196  * CSRNG consume seed from entropy source enable.
197  */
199  /**
200  * Seed material used as the only seed material.
201  *
202  * This configuration option will toggle the hardware state of the
203  * CSRNG to non-FIPS compliant until it is re-instantiated.
204  *
205  * Note: Software may opt to XOR the seed material with a seed to implement
206  * a software assisted FIPS mode of operation.
207  */
209  /**
210  * Entropy source XOR'ed with the provided seed material.
211  */
214 
215 enum {
216  /**
217  * Maximum seed material number of uint32_t words supported in CSRNG
218  * instantiate and seed commands.
219  */
221 };
222 
223 /**
224  * CSRNG common transaction parameters.
225  */
226 typedef struct dif_csrng_seed_material {
227  /**
228  * Number of uint32_t words in `seed_material`. Up to 12 words can be
229  * set to initialize the CSRNG. CSRNG will extend the `seed_material`
230  * to zeros if the provided value is less that 12.
231  */
233  /**
234  * Seed material used in CSRNG
235  */
238 
239 /**
240  * Generated output state.
241  */
242 typedef struct dif_csrng_output_status {
243  /**
244  * Set to true when there is cryptographic entropy data available to
245  * read using `dif_csrng_generate_read()`.
246  */
248  /**
249  * Set to true when the cryptographic entropy data available to read
250  * is FIPS/CC compliant at the hardware level.
251  */
252  bool fips_mode;
254 
255 /**
256  * CSRNG internal state selector ID.
257  */
259  /**
260  * CSRNG instance assigned to Entropy Distribution Network (EDN) 0.
261  */
263  /**
264  * CSRNG instance assigned to Entropy Distribution Network (EDN) 1.
265  */
267  /**
268  * CSRNG instance assigned to software interface.
269  */
272 
273 /**
274  * CSRNG internal state.
275  */
276 typedef struct dif_csrng_internal_state {
277  /**
278  * Indicates the number of requests for pseudorandom bits since instantiation
279  * or reseeding.
280  */
281  uint32_t reseed_counter;
282  /**
283  * Internal V working state with a 128bit block size.
284  */
285  uint32_t v[4];
286  /**
287  * Internal key used to configure the internal CSRNG cipher.
288  */
289  uint32_t key[8];
290  /**
291  * Set to true when the CSRNG instance has been instantiated.
292  */
294  /**
295  * Set to true when FIPS compliant entropy was provided directly by the
296  * entropy source to instantiate or reseed the CSRNG instance.
297  */
300 
301 /**
302  * Recoverable alerts emitted by the CSRNG.
303  */
305  /**
306  * Indicates a bad value was written to the ENABLE field of the control
307  * register.
308  */
310  1U << CSRNG_RECOV_ALERT_STS_ENABLE_FIELD_ALERT_BIT,
311  /**
312  * Indicates a bad value was written to the SW_APP_ENABLE field of the control
313  * register.
314  */
316  1U << CSRNG_RECOV_ALERT_STS_SW_APP_ENABLE_FIELD_ALERT_BIT,
317  /**
318  * Indicates a bad value was written to the READ_INT_STATE field of the
319  * control register.
320  */
322  1U << CSRNG_RECOV_ALERT_STS_READ_INT_STATE_FIELD_ALERT_BIT,
323  /**
324  * Indicates the FLAG0 field in the Application Command is set to a value
325  * other than kMultiBitBool4True or kMultiBitBool4False.
326  */
328  1U << CSRNG_RECOV_ALERT_STS_ACMD_FLAG0_FIELD_ALERT_BIT,
329  /**
330  * Indicates the genbits bus saw two identical values, indicating a possible
331  * attack.
332  */
334  1U << CSRNG_RECOV_ALERT_STS_CS_BUS_CMP_ALERT_BIT,
335  /**
336  * Indicates an unsupported CSRNG command is being processed, causing the main
337  * FSM to hang unless the module enable field is set to the disabled state.
338  */
340  1U << CSRNG_RECOV_ALERT_STS_CS_MAIN_SM_ALERT_BIT,
342 
343 /**
344  * Configures CSRNG.
345  *
346  * This function should need to be called once for the lifetime of `csrng`.
347  *
348  * @param csrng A CSRNG handle.
349  * @return The result of the operation.
350  */
353 
354 /**
355  * Initializes CSRNG instance with a new seed value.
356  *
357  * `seed_material` is used as specified in NIST SP 800-90Ar1 section
358  * 10.2.1.3.1. See `dif_csrng_entropy_src` for details on how this value
359  * is mixed with the CSRNG seed.
360  *
361  * `seed_material` can be NULL, in which case CSRNG will use a zero
362  * vector instead.
363  *
364  * @param csrng A CSRNG handle.
365  * @param entropy_src_enable Entropy source input enable.
366  * @param seed_material Seed initialization parameters.
367  * @return The result of the operation.
368  */
371  const dif_csrng_t *csrng, dif_csrng_entropy_src_toggle_t entropy_src_enable,
372  const dif_csrng_seed_material_t *seed_material);
373 
374 /**
375  * Reseeds CSRNG instance.
376  *
377  * When `seed_material.seed_material_len` is set to 0, only the entropy source
378  * seed is used to reseed the instance, otherwise it will be XOR'ed with the
379  * entropy source.
380  *
381  * @param csrng A CSRNG handle.
382  * @param seed_material Reseed parameters.
383  * @return The result of the operation.
384  */
387  const dif_csrng_seed_material_t *seed_material);
388 
389 /**
390  * Updates CSRNG state.
391  *
392  * This function is similar to `dif_csrng_reseed()`, except:
393  *
394  * - Only `seed_material.seed_material` is used in the update operation.
395  * - The update operation does not reset the internal CSRNG reseed
396  * counter.
397  *
398  * @param csrng A CSRNG handle.
399  * @param seed_material Update parameters.
400  * @return The result of the operation.
401  */
404  const dif_csrng_seed_material_t *seed_material);
405 
406 /**
407  * Requests cryptographic entropy bits from the CSRNG.
408  *
409  * The prediction resistance flag as specified in SP 800-90Ar1 section
410  * 10.2.1.1 is not directily supported by the hardware. It is the
411  * responsibility of the caller to reseed as needed before calling
412  * this function.
413  *
414  * The CSRNG accepts generation requests with 128-bit granularity, with
415  * a minimum 128-bit request size. This function will increase the size
416  * of the request to align it to the nearest 128-bit boundary.
417  *
418  * @param csrng A CSRNG handle.
419  * @param len Number of uint32_t words to generate.
420  * @return The result of the operation. KDifOutOfRange if the `len` parameter
421  * results in a 128bit block level size greater than 0x800.
422  */
424 dif_result_t dif_csrng_generate_start(const dif_csrng_t *csrng, size_t len);
425 
426 /**
427  * Reads the output of the last CSRNG generate call.
428  *
429  * This function reads `len` words out of the CSRNG. This function should be
430  * called repeatedly until the number of words requested in
431  * `dif_csrng_generate_start()` is exhausted. This function will block until
432  * `len` words are read.
433  *
434  * `dif_csrng_get_output_status()` can be called before this function to ensure
435  * there is data available in the CSRNG output buffer.
436  *
437  * @param csrng A CSRNG handle.
438  * @param[out] buf A buffer to fill with words from the pipeline.
439  * @param len The number of words to read into `buf`.
440  * @return The result of the operation.
441  */
443 dif_result_t dif_csrng_generate_read(const dif_csrng_t *csrng, uint32_t *buf,
444  size_t len);
445 
446 /**
447  * Uninstantiates CSRNG
448  *
449  * Resets the CSRNG instance. Values in the CSRNG are zeroed out. This
450  * command effectively resets the CSRNG, clearing any errors that it
451  * may have encountered due to processing or entropy source errors.
452  *
453  * @param csrng An CSRNG handle.
454  * @return The result of the operation.
455  */
458 
459 /**
460  * Gets the current command interface status.
461  *
462  * This function can be called before or after any of the following functions:
463  *
464  * - `dif_csrng_instantiate()`
465  * - `dif_csrng_reseed()`
466  * - `dif_csrng_update()`
467  * - `dif_csrng_generate_start()`
468  * - `dif_csrng_uninstantiate()`
469  *
470  * @param csrng An CSRNG handle
471  * @param[out] status Command interface status.
472  * @return The result of the operation.
473  */
477 
478 /**
479  * Forces the status registers to indicate `fifo` as being in an unhealthy
480  * state.
481  *
482  * @param csrng An CSRNG handle
483  * @param fifo The FIFO to mark as unhealthy.
484  * @return The result of the operation.
485  */
488  dif_csrng_fifo_t fifo);
489 
490 /**
491  * Forces the status registers to indicate a particular error cause.
492  *
493  * @param csrng An CSRNG handle
494  * @param error The error to force.
495  * @return The result of the operation.
496  */
499  dif_csrng_error_t error);
500 
501 /**
502  * Returns an opaque blob indicating the main state machine's current state.
503  *
504  * @param csrng An CSRNG handle
505  * @param state[out] The state machine state as an opaque blob.
506  * @return The result of the operation.
507  */
510  uint32_t *state);
511 
512 /**
513  * Returns a bitset indicating which hardware CSRNGs have encountered
514  * exceptions.
515  *
516  * @param csrng An CSRNG handle
517  * @param exceptions[out] The bitset of exception states.
518  * @return The result of the operation.
519  */
522  uint32_t *exceptions);
523 
524 /**
525  * Clears recorded hardware CSRNG exceptions.
526  *
527  * @param csrng An CSRNG handle
528  * @return The result of the operation.
529  */
532 
533 /**
534  * Gets the current cryptographic entropy output data status.
535  *
536  * This function can be used before calling `dif_csrng_generate_read()` to
537  * check if there is data available to read.
538  *
539  * @param csrng A CSRNG handle.
540  * @param[out] status CSRNG output status.
541  * @return The result of the operation.
542  */
546 
547 /**
548  * Gets the working state of a CSRNG instance.
549  *
550  * @param csrng A CSRNG handle
551  * @param instance_id CSRNG instance ID.
552  * @param[out] state The working state of a CSRNG instance.
553  * @return The result of the operation.
554  */
557  const dif_csrng_t *csrng, dif_csrng_internal_state_id_t instance_id,
559 
560 /**
561  * Locks out CSRNG functionality.
562  *
563  * This function is reentrant: calling it while functionality is locked will
564  * have no effect and return `kDifCsrngOk`.
565  *
566  * @param csrng A CSRNG handle.
567  * @return The result of the operation.
568  */
571 
572 /**
573  * Checks whether this CSRNG is locked.
574  *
575  * @param csrng A CSRNG handle.
576  * @param[out] is_locked Out-param for the locked state.
577  * @return The result of the operation.
578  */
580 dif_result_t dif_csrng_is_locked(const dif_csrng_t *csrng, bool *is_locked);
581 
582 /**
583  * Disables the CSRNG module.
584  *
585  * @param csrng A CSRNG handle.
586  * @return The result of the operation.
587  */
590 
591 /**
592  * Gets the recoverable alerts currently recorded in the CSRNG block.
593  *
594  * This function returns the alerts in a bitset whose indices are given by
595  * `dif_csrng_recoverable_alert_t`.
596  *
597  * @param csrng A CSRNG handle.
598  * @param[out] alerts Bitset of alerts currently recorded.
599  * @return The result of the operation.
600  */
603  uint32_t *alerts);
604 
605 /**
606  * Clears all recoverable alerts currently recorded in the CSRNG block.
607  *
608  * @param csrng A CSRNG handle.
609  * @return The result of the operation.
610  */
613 
614 #ifdef __cplusplus
615 } // extern "C"
616 #endif // __cplusplus
617 
618 #endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_CSRNG_H_