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="/book/hw/ip/csrng/">CSRNG</a> Device Interface Functions
11 */
12
13#include <stdint.h>
14
18
19#include "hw/top/csrng_regs.h" // Generated
20#include "sw/device/lib/dif/autogen/dif_csrng_autogen.h"
21
22#ifdef __cplusplus
23extern "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://opentitan.org/book/hw/ip/csrng/
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 */
93typedef enum dif_csrng_fifo {
94 kDifCsrngFifoCmd,
95 kDifCsrngFifoGenBits,
97
98/**
99 * Enumeration of CSRNG FIFO errors.
100 */
101typedef enum dif_csrng_error {
102 /**
103 * Indicates an error in the command stage state machine.
104 */
106 /**
107 * Indicates an error in the main state machine.
108 */
110 /**
111 * Indicates an error in the CTR_DRBG data path state machine.
112 */
114 /**
115 * Indicates an error in the AES state machine.
116 */
118 /**
119 * Indicates an error in one of the counters.
120 */
122 /**
123 * Indicates a write to a full FIFO occurred.
124 */
126 /**
127 * Indicates a read from an empty FIFO occurred.
128 */
130 /**
131 * Indicates a FIFO was somehow both full and empty.
132 */
135
136/**
137 * Enumeration of CSRNG command status errors.
138 */
140 /**
141 * Indicates that the command completed successfully.
142 */
144 /**
145 * Indicates that an invalid apllication command has been issued.
146 */
148 /**
149 * Indicates that the state wasn't zeroized properly after an uninstantiate
150 * command due to invalid state parameters in the cmd drbg.
151 */
153 /**
154 * Indicates that CSRNG entropy was generated for a command that is not a
155 * generate command.
156 */
158 /**
159 * Indicates that last command was not issued in sequence.
160 * E.g. an instantiate on an instantiated state or any command other than an
161 * instantiate on an uninstantiated state.
162 */
165
166/**
167 * The status of the CSRNG block at a particular moment in time.
168 */
169typedef struct dif_csrng_cmd_status {
170 /**
171 * The kind of status the CSRNG is in.
172 */
174 /**
175 * The status value CSRNG returns.
176 */
179
180/**
181 * CSRNG consume seed from entropy source enable.
182 */
184 /**
185 * Seed material used as the only seed material.
186 *
187 * This configuration option will toggle the hardware state of the
188 * CSRNG to non-FIPS compliant until it is re-instantiated.
189 *
190 * Note: Software may opt to XOR the seed material with a seed to implement
191 * a software assisted FIPS mode of operation.
192 */
194 /**
195 * Entropy source XOR'ed with the provided seed material.
196 */
199
200enum {
201 /**
202 * Maximum seed material number of uint32_t words supported in CSRNG
203 * instantiate and seed commands.
204 */
206};
207
208/**
209 * CSRNG common transaction parameters.
210 */
212 /**
213 * Number of uint32_t words in `seed_material`. Up to 12 words can be
214 * set to initialize the CSRNG. CSRNG will extend the `seed_material`
215 * to zeros if the provided value is less that 12.
216 */
218 /**
219 * Seed material used in CSRNG
220 */
223
224/**
225 * Generated output state.
226 */
228 /**
229 * Set to true when there is cryptographic entropy data available to
230 * read using `dif_csrng_generate_read()`.
231 */
233 /**
234 * Set to true when the cryptographic entropy data available to read
235 * is FIPS/CC compliant at the hardware level.
236 */
239
240/**
241 * CSRNG internal state selector ID.
242 */
244 /**
245 * CSRNG instance assigned to Entropy Distribution Network (EDN) 0.
246 */
248 /**
249 * CSRNG instance assigned to Entropy Distribution Network (EDN) 1.
250 */
252 /**
253 * CSRNG instance assigned to software interface.
254 */
257
258/**
259 * CSRNG internal state.
260 */
262 /**
263 * Indicates the number of requests for pseudorandom bits since instantiation
264 * or reseeding.
265 */
267 /**
268 * Internal V working state with a 128bit block size.
269 */
270 uint32_t v[4];
271 /**
272 * Internal key used to configure the internal CSRNG cipher.
273 */
274 uint32_t key[8];
275 /**
276 * Set to true when the CSRNG instance has been instantiated.
277 */
279 /**
280 * Set to true when FIPS compliant entropy was provided directly by the
281 * entropy source to instantiate or reseed the CSRNG instance.
282 */
285
286/**
287 * Recoverable alerts emitted by the CSRNG.
288 */
290 /**
291 * Indicates a bad value was written to the ENABLE field of the control
292 * register.
293 */
295 1U << CSRNG_RECOV_ALERT_STS_ENABLE_FIELD_ALERT_BIT,
296 /**
297 * Indicates a bad value was written to the SW_APP_ENABLE field of the control
298 * register.
299 */
301 1U << CSRNG_RECOV_ALERT_STS_SW_APP_ENABLE_FIELD_ALERT_BIT,
302 /**
303 * Indicates a bad value was written to the READ_INT_STATE field of the
304 * control register.
305 */
307 1U << CSRNG_RECOV_ALERT_STS_READ_INT_STATE_FIELD_ALERT_BIT,
308 /**
309 * Indicates a bad value was written to the WRITE_INT_STATE field of the
310 * control register.
311 */
313 1U << CSRNG_RECOV_ALERT_STS_WRITE_INT_STATE_FIELD_ALERT_BIT,
314 /**
315 * Indicates a bad value was written to the FIPS_FORCE_ENABLE field of the
316 * control register.
317 */
319 1U << CSRNG_RECOV_ALERT_STS_FIPS_FORCE_ENABLE_FIELD_ALERT_BIT,
320 /**
321 * Indicates the FLAG0 field in the Application Command is set to a value
322 * other than kMultiBitBool4True or kMultiBitBool4False.
323 */
325 1U << CSRNG_RECOV_ALERT_STS_ACMD_FLAG0_FIELD_ALERT_BIT,
326 /**
327 * Indicates a Generate command abort was requested for an instance without a
328 * Generate command in progress.
329 */
331 1U << CSRNG_RECOV_ALERT_STS_GEN_ABORT_INVALID_ALERT_BIT,
332 /**
333 * Indicates a bad value was written to the GEN_ABORT register.
334 */
336 1U << CSRNG_RECOV_ALERT_STS_GEN_ABORT_FIELD_ALERT_BIT,
337 /**
338 * Indicates a bad value was written to the INT_STATE_HALT register.
339 */
341 1U << CSRNG_RECOV_ALERT_STS_INT_STATE_HALT_FIELD_ALERT_BIT,
342 /**
343 * Indicates the selected internal state was updated by CSRNG while it was
344 * being accessed via INT_STATE_VAL.
345 */
347 1U << CSRNG_RECOV_ALERT_STS_INT_STATE_OVERWRITE_ALERT_BIT,
348 /**
349 * Indicates the genbits bus saw two identical values, indicating a possible
350 * attack.
351 */
353 1U << CSRNG_RECOV_ALERT_STS_CS_BUS_CMP_ALERT_BIT,
354 /**
355 * Indicates an unsupported CSRNG command was issued.
356 */
358 1U << CSRNG_RECOV_ALERT_STS_CMD_STAGE_INVALID_ACMD_ALERT_BIT,
359 /**
360 * Indicates a Generate command was issued with a `glen` field of zero.
361 */
363 1U << CSRNG_RECOV_ALERT_STS_CMD_STAGE_INVALID_GEN_CMD_ALERT_BIT,
364 /**
365 * Indicates a supported CSRNG command was issued out of sequence.
366 */
368 1U << CSRNG_RECOV_ALERT_STS_CMD_STAGE_INVALID_CMD_SEQ_ALERT_BIT,
369 /**
370 * Indicates that too many generate commands were issued in a row.
371 */
373 1U << CSRNG_RECOV_ALERT_STS_CMD_STAGE_RESEED_CNT_ALERT_BIT,
375
376/**
377 * Configures CSRNG.
378 *
379 * This function should need to be called once for the lifetime of `csrng`.
380 *
381 * @param csrng A CSRNG handle.
382 * @return The result of the operation.
383 */
386
387/**
388 * Initializes CSRNG instance with a new seed value.
389 *
390 * `seed_material` is used as specified in NIST SP 800-90Ar1 section
391 * 10.2.1.3.1. See `dif_csrng_entropy_src` for details on how this value
392 * is mixed with the CSRNG seed.
393 *
394 * `seed_material` can be NULL, in which case CSRNG will use a zero
395 * vector instead.
396 *
397 * @param csrng A CSRNG handle.
398 * @param entropy_src_enable Entropy source input enable.
399 * @param seed_material Seed initialization parameters.
400 * @return The result of the operation.
401 */
404 const dif_csrng_t *csrng, dif_csrng_entropy_src_toggle_t entropy_src_enable,
405 const dif_csrng_seed_material_t *seed_material);
406
407/**
408 * Reseeds CSRNG instance.
409 *
410 * When `seed_material.seed_material_len` is set to 0, only the entropy source
411 * seed is used to reseed the instance, otherwise it will be XOR'ed with the
412 * entropy source.
413 *
414 * @param csrng A CSRNG handle.
415 * @param seed_material Reseed parameters.
416 * @return The result of the operation.
417 */
420 const dif_csrng_seed_material_t *seed_material);
421
422/**
423 * Updates CSRNG state.
424 *
425 * This function is similar to `dif_csrng_reseed()`, except:
426 *
427 * - Only `seed_material.seed_material` is used in the update operation.
428 * - The update operation does not reset the internal CSRNG reseed
429 * counter.
430 *
431 * @param csrng A CSRNG handle.
432 * @param seed_material Update parameters.
433 * @return The result of the operation.
434 */
437 const dif_csrng_seed_material_t *seed_material);
438
439/**
440 * Requests cryptographic entropy bits from the CSRNG.
441 *
442 * The prediction resistance flag as specified in SP 800-90Ar1 section
443 * 10.2.1.1 is not directily supported by the hardware. It is the
444 * responsibility of the caller to reseed as needed before calling
445 * this function.
446 *
447 * The CSRNG accepts generation requests with 128-bit granularity, with
448 * a minimum 128-bit request size. This function will increase the size
449 * of the request to align it to the nearest 128-bit boundary.
450 *
451 * @param csrng A CSRNG handle.
452 * @param additional_data Additional data for the generate command. Set to NULL
453 * if unused.
454 * @param len Number of uint32_t words to generate.
455 * @return The result of the operation. KDifOutOfRange if the `len` parameter
456 * results in a 128bit block level size greater than 0x800.
457 */
460 const dif_csrng_t *csrng, const dif_csrng_seed_material_t *additional_data,
461 size_t len);
462
463/**
464 * Reads the output of the last CSRNG generate call.
465 *
466 * This function reads `len` words out of the CSRNG. This function should be
467 * called repeatedly until the number of words requested in
468 * `dif_csrng_generate_start()` is exhausted. This function will block until
469 * `len` words are read.
470 *
471 * `dif_csrng_get_output_status()` can be called before this function to ensure
472 * there is data available in the CSRNG output buffer.
473 *
474 * @param csrng A CSRNG handle.
475 * @param[out] buf A buffer to fill with words from the pipeline.
476 * @param len The number of words to read into `buf`.
477 * @return The result of the operation.
478 */
480dif_result_t dif_csrng_generate_read(const dif_csrng_t *csrng, uint32_t *buf,
481 size_t len);
482
483/**
484 * Uninstantiates CSRNG
485 *
486 * Resets the CSRNG instance. Values in the CSRNG are zeroed out. This
487 * command effectively resets the CSRNG, clearing any errors that it
488 * may have encountered due to processing or entropy source errors.
489 *
490 * @param csrng An CSRNG handle.
491 * @return The result of the operation.
492 */
495
496/**
497 * Gets the current command interface status.
498 *
499 * This function can be called before or after any of the following functions:
500 *
501 * - `dif_csrng_instantiate()`
502 * - `dif_csrng_reseed()`
503 * - `dif_csrng_update()`
504 * - `dif_csrng_generate_start()`
505 * - `dif_csrng_uninstantiate()`
506 *
507 * @param csrng An CSRNG handle
508 * @param[out] status Command interface status.
509 * @return The result of the operation.
510 */
514
515/**
516 * Forces the status registers to indicate `fifo` as being in an unhealthy
517 * state.
518 *
519 * @param csrng An CSRNG handle
520 * @param fifo The FIFO to mark as unhealthy.
521 * @return The result of the operation.
522 */
525 dif_csrng_fifo_t fifo);
526
527/**
528 * Forces the status registers to indicate a particular error cause.
529 *
530 * @param csrng An CSRNG handle
531 * @param error The error to force.
532 * @return The result of the operation.
533 */
536 dif_csrng_error_t error);
537
538/**
539 * Returns an opaque blob indicating the main state machine's current state.
540 *
541 * @param csrng An CSRNG handle
542 * @param state[out] The state machine state as an opaque blob.
543 * @return The result of the operation.
544 */
547 uint32_t *state);
548
549/**
550 * Returns a bitset indicating which hardware CSRNGs have encountered
551 * exceptions.
552 *
553 * @param csrng An CSRNG handle
554 * @param exceptions[out] The bitset of exception states.
555 * @return The result of the operation.
556 */
559 uint32_t *exceptions);
560
561/**
562 * Clears recorded hardware CSRNG exceptions.
563 *
564 * @param csrng An CSRNG handle
565 * @return The result of the operation.
566 */
569
570/**
571 * Gets the current cryptographic entropy output data status.
572 *
573 * This function can be used before calling `dif_csrng_generate_read()` to
574 * check if there is data available to read.
575 *
576 * @param csrng A CSRNG handle.
577 * @param[out] status CSRNG output status.
578 * @return The result of the operation.
579 */
583
584/**
585 * Gets the working state of a CSRNG instance.
586 *
587 * @param csrng A CSRNG handle
588 * @param instance_id CSRNG instance ID.
589 * @param[out] state The working state of a CSRNG instance.
590 * @return The result of the operation.
591 */
594 const dif_csrng_t *csrng, dif_csrng_internal_state_id_t instance_id,
596
597/**
598 * Gets the reseed counter of a CSRNG instance.
599 *
600 * @param csrng A CSRNG handle
601 * @param instance_id CSRNG instance ID.
602 * @param[out] reseed counter The current reseed counter value of a CSRNG
603 * instance.
604 * @return The result of the operation.
605 */
608 const dif_csrng_t *csrng, dif_csrng_internal_state_id_t instance_id,
609 uint32_t *reseed_counter);
610
611/**
612 * Locks out CSRNG functionality.
613 *
614 * This function is reentrant: calling it while functionality is locked will
615 * have no effect and return `kDifCsrngOk`.
616 *
617 * @param csrng A CSRNG handle.
618 * @return The result of the operation.
619 */
622
623/**
624 * Checks whether this CSRNG is locked.
625 *
626 * @param csrng A CSRNG handle.
627 * @param[out] is_locked Out-param for the locked state.
628 * @return The result of the operation.
629 */
631dif_result_t dif_csrng_is_locked(const dif_csrng_t *csrng, bool *is_locked);
632
633/**
634 * Disables the CSRNG module.
635 *
636 * @param csrng A CSRNG handle.
637 * @return The result of the operation.
638 */
641
642/**
643 * Gets the recoverable alerts currently recorded in the CSRNG block.
644 *
645 * This function returns the alerts in a bitset whose indices are given by
646 * `dif_csrng_recoverable_alert_t`.
647 *
648 * @param csrng A CSRNG handle.
649 * @param[out] alerts Bitset of alerts currently recorded.
650 * @return The result of the operation.
651 */
654 uint32_t *alerts);
655
656/**
657 * Clears all recoverable alerts currently recorded in the CSRNG block.
658 *
659 * @param csrng A CSRNG handle.
660 * @return The result of the operation.
661 */
664
665#ifdef __cplusplus
666} // extern "C"
667#endif // __cplusplus
668
669#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_CSRNG_H_