Software APIs
dif_entropy_src.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_ENTROPY_SRC_H_
6#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ENTROPY_SRC_H_
7
8/**
9 * @file
10 * @brief <a href="/book/hw/ip/entropy_src/">Entropy Source</a> Device Interface
11 * Functions
12 */
13
14#include <stdint.h>
15
19
20#include "sw/device/lib/dif/autogen/dif_entropy_src_autogen.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif // __cplusplus
25
26/**
27 * A single-bit RNG mode, where only one bit is sampled.
28 */
30 /**
31 * Single-bit-mode, sampling the zeroth bit.
32 */
34 /**
35 * Single-bit-mode, sampling the first bit.
36 */
38 /**
39 * Single-bit-mode, sampling the second bit.
40 */
42 /**
43 * Single-bit-mode, sampling the third bit.
44 */
46 /**
47 * Indicates that single-bit-mode is disabled.
48 */
51
52/**
53 * Main FSM state.
54 */
56 kDifEntropySrcMainFsmStateIdle = 0x0f5,
57 kDifEntropySrcMainFsmStateBootHTRunning = 0x1d2,
58 kDifEntropySrcMainFsmStateBootPostHTChk = 0x16e,
59 kDifEntropySrcMainFsmStateBootPhaseDone = 0x08e,
60 kDifEntropySrcMainFsmStateStartupHTStart = 0x02c,
61 kDifEntropySrcMainFsmStateStartupPhase1 = 0x101,
62 kDifEntropySrcMainFsmStateStartupPass1 = 0x1a5,
63 kDifEntropySrcMainFsmStateStartupFail1 = 0x017,
64 kDifEntropySrcMainFsmStateContHTStart = 0x040,
65 kDifEntropySrcMainFsmStateContHTRunning = 0x1a2,
66 kDifEntropySrcMainFsmStateFWInsertStart = 0x0c3,
67 kDifEntropySrcMainFsmStateFWInsertMsg = 0x059,
68 kDifEntropySrcMainFsmStateSha3MsgDone = 0x10f,
69 kDifEntropySrcMainFsmStateSha3Process = 0x0f8,
70 kDifEntropySrcMainFsmStateSha3Valid = 0x0bf,
71 kDifEntropySrcMainFsmStateSha3Done = 0x198,
72 kDifEntropySrcMainFsmStateAlertState = 0x1cd,
73 kDifEntropySrcMainFsmStateAlertHang = 0x1fb,
74 kDifEntropySrcMainFsmStateError = 0x73
76
77/**
78 * Firmware override parameters for an entropy source.
79 */
81 /**
82 * Enables firmware to insert entropy bits back into the pre-conditioner FIFO
83 * via `dif_entropy_src_fw_ov_data_write()` calls. This feature is useful when
84 * the firmware is required to implement additional health checks, and to
85 * perform known answer tests of the conditioner.
86 *
87 * To take effect, this requires the firmware override feature to be enabled.
88 */
90 /**
91 * This field sets the depth of the observe FIFO hardware buffer used when
92 * the firmware override feature is enabled.
93 */
96
97/**
98 * Runtime configuration for an entropy source.
99 *
100 * This struct describes runtime information for one-time configuration of the
101 * hardware.
102 */
104 /**
105 * If set, FIPS compliant entropy will be generated by this module after being
106 * processed by an SP 800-90B compliant conditioning function.
107 *
108 * Software may opt for implementing FIPS mode of operation without hardware
109 * support by setting this field to false. In such case, software is
110 * responsible for implementing the conditioning function.
111 */
113 /**
114 * If set, the produced output entropy is marked as FIPS compliant
115 * through the FIPS bit being set to high.
116 */
118 /**
119 * If set, the noise source is instructed to produce high quality entropy.
120 */
122 /**
123 * If set, entropy will be routed to a firmware-visible register instead of
124 * being distributed to other hardware IPs.
125 */
127 /**
128 * If set, raw entropy will be sent to CSRNG, bypassing the conditioner block
129 * and disabling the FIPS flag. Note that the FIPS flag is different from
130 * running the block in FIPS mode. FIPS mode refers to running the entropy_src
131 * in continuous mode. Also note that if `fips_enable` is set to `True`, then
132 * at most one of either `route_to_firmware` or `bypass_conditioner` may be
133 * set, but will result in disabling the FIPS mode of operation from a
134 * hardware perspective.
135 */
137 /**
138 * Specifies which single-bit-mode to use, if any at all. FIPS mode of
139 * operation is disabled in single-bit-mode of operation is selected.
140 */
142 /**
143 * Controls the scope (either by-line or by-sum) of the health tests.
144 *
145 * If true, the Adaptive Proportion and Markov Tests will accumulate all RNG
146 * input lines into a single score, and thresholds will be applied to the sum
147 * of all the entropy input lines.
148 *
149 * If false, the RNG input lines are all scored individually. A statistical
150 * deviation in any one input line, be it due to coincidence or failure, will
151 * force rejection of the sample, and count toward the total alert count.
152 */
154 /**
155 * The size of the window used for health tests.
156 *
157 * Units: bits
158 */
160 /**
161 * The number of health test failures that must occur before an alert is
162 * triggered. When set to 0, alerts are disabled.
163 */
166
167/**
168 * A statistical test on the bits emitted by an entropy source.
169 */
171 /**
172 * An SP 800-90B repetition count test.
173 *
174 * This test screens for stuck bits, or a total failure of the noise source.
175 * This test fails if any sequence of bits repeats too many times in a row
176 * for too many samples.
177 */
179 /**
180 * An SP 800-90B symbol repetition count test.
181 *
182 * This is similar to the above, test but is performed on a symbol, instead of
183 * bit, basis.
184 */
186 /**
187 * An SP 800-90B adaptive proportion test.
188 *
189 * This test screens for statistical bias in the number of ones or zeros
190 * output by the noise source.
191 */
193 /**
194 * A bucket test.
195 *
196 * This test looks for correlations between individual noise channels.
197 */
199 /**
200 * A "Markov" test.
201 *
202 * This test looks for unexpected first-order temporal correlations
203 * between individual noise channels.
204 */
206 /**
207 * A firmware-driven "mailbox" test.
208 *
209 * This test allows firmware to inspect 2kbit blocks of entropy, and signal
210 * potential concerns to the hardware.
211 */
213 /** \internal */
214 kDifEntropySrcTestNumVariants = 6,
216
217/**
218 * Criteria used by various entropy source health tests to decide whether the
219 * test has failed.
220 */
222 /**
223 * The entropy source health test type to configure.
224 */
226 /**
227 * The high threshold for the health test (contains both FIPS and bypass
228 * thresholds).
229 */
231 /**
232 * The low threshold for the health test (contains both FIPS and bypass
233 * thresholds).
234 *
235 * If the corresponding health test has no low threshold, set to 0, otherwise
236 * `dif_entropy_src_health_test_configure()` will return `kDifBadArg`.
237 */
240
241/**
242 * Values of the health test watermark number register to record the high or low
243 * watermarks of the corresponding health tests.
244 */
246 kDifEntropySrcWatermarkNumRepcntHi = 0,
247 kDifEntropySrcWatermarkNumRepcntsHi = 1,
248 kDifEntropySrcWatermarkNumAdaptpHi = 2,
249 kDifEntropySrcWatermarkNumAdaptpLo = 3,
250 kDifEntropySrcWatermarkNumBucketHi = 4,
251 kDifEntropySrcWatermarkNumMarkovHi = 5,
252 kDifEntropySrcWatermarkNumMarkovLo = 6,
253 kDifEntropySrcWatermarkNumExthtHi = 7,
254 kDifEntropySrcWatermarkNumExthtLo = 8,
256
257/**
258 * Revision information for an entropy source.
259 *
260 * The fields of this struct have an implementation-specific interpretation.
261 */
263 uint8_t abi_revision;
264 uint8_t hw_revision;
265 uint8_t chip_type;
267
268/**
269 * Statistics on entropy source health tests.
270 */
272 /**
273 * Identifier defining test and the watermark (high or low) the recorded
274 * number belongs to.
275 */
277 /**
278 * The recorded watermark. For low or high watermarks, this indicates the
279 * lowest or highest values emitted by the corresponding health test,
280 * respectively.
281 */
282 uint16_t watermark;
283 /**
284 * The number of times a particular test has failed above the high threshold
285 * (contains both failures when using the FIPS or the bypass thresholds).
286 */
287 uint32_t high_fails[kDifEntropySrcTestNumVariants];
288 /**
289 * The number of times a particular test has failed below the low threshold.
290 *
291 * Note, some health tests do not have a low threshold. For these tests, this
292 * value will always be 0.
293 */
294 uint32_t low_fails[kDifEntropySrcTestNumVariants];
296
297/**
298 * Statistics on entropy source health tests failures that triggered alerts.
299 */
301 /**
302 * The total number of test failures, across all test types, that contributed
303 * to the alerts fired.
304 */
305 uint16_t total_fails;
306 /**
307 * The number of test failures, due to the specific test execeeding the high
308 * threshold, that cause alerts to be fired.
309 */
310 uint8_t high_fails[kDifEntropySrcTestNumVariants];
311 /**
312 * The number of test failures, due to the specific test execeeding the low
313 * threshold, that cause alerts to be fired.
314 *
315 * Note, some health tests do not have a low threshold. For these tests, this
316 * value will always be 0.
317 */
318 uint8_t low_fails[kDifEntropySrcTestNumVariants];
320
321/**
322 * SHA3 state machine states.
323 *
324 * See `hw/ip/kmac/rtl/sha3_pkg.sv` for more details.
325 */
327 kDifEntropySrcSha3StateIdle = 0,
328 kDifEntropySrcSha3StateAbsorb = 1,
329 kDifEntropySrcSha3StateSqueeze = 2,
330 kDifEntropySrcSha3StateManualRun = 3,
331 kDifEntropySrcSha3StateFlush = 4,
332 kDifEntropySrcSha3StateError = 5,
334
335/**
336 * Debug status information.
337 */
339 /**
340 * Depth of the entropy source FIFO.
341 *
342 * Valid range: [0, 7]
343 */
345 /**
346 * The current state of the SHA3 preconditioner state machine.
347 *
348 * See `dif_entropy_src_sha3_state_t` for more details.
349 */
351 /**
352 * Whether the SHA3 preconditioner has completed processing the current block.
353 */
355 /**
356 * Whether the SHA3 preconditioner is in the squeezing state.
357 */
359 /**
360 * Whether the SHA3 preconditioner is in the absorbed state.
361 */
363 /**
364 * Whether the SHA3 preconditioner has is in an error state.
365 */
367 /**
368 * Whether the main FSM is in the idle state.
369 */
371 /**
372 * Whether the main FSM is in the boot done state.
373 */
376
377/**
378 * Recoverable alerts.
379 */
381 /**
382 * Triggered when the FIPS_ENABLE field in the CONF register is set to an
383 * unsupported value.
384 */
386 /**
387 * Triggered when the ENTROPY_DATA_REG_ENABLE field in the CONF register is
388 * set to an unsupported value.
389 */
391 /**
392 * Triggered when the MODULE_ENABLE field in the MODULE_ENABLE register is set
393 * to an unsupported value.
394 */
396 /**
397 * Triggered when the THRESHOLD_SCOPE field in the CONF register is set to an
398 * unsupported value.
399 */
401 /**
402 * Triggered when the RNG_BIT_ENABLE field in the CONF register is set to an
403 * unsupported value.
404 */
406 /**
407 * Triggered when the FW_OV_SHA3_START field in the FW_OV_SHA3_START register
408 * is set to an unsupported value.
409 */
411 /**
412 * Triggered when the FW_OV_MODE field in the FW_OV_CONTROL register is set to
413 * an unsupported value.
414 */
416 /**
417 * Triggered when the FW_OV_ENTROPY_INSERT field in the FW_OV_CONTROL register
418 * is set to an unsupported value.
419 */
421 /**
422 * Triggered when the ES_ROUTE field in the ENTROPY_CONTROL register is set to
423 * an unsupported value.
424 */
426 /**
427 * Triggered when the ES_TYPE field in the ENTROPY_CONTROL register is set to
428 * an unsupported value.
429 */
431 /**
432 * Triggered when the main state machine detects a threshold failure state.
433 */
435 /**
436 * Triggered when the internal entropy bus value is equal to the prior valid
437 * value on the bus, indicating a possible attack.
438 */
440 /**
441 * Triggered when the ALERT_THRESHOLD register is not configured properly,
442 * i.e., the upper field must be the exact inverse of the lower field.
443 */
445 /**
446 * Triggered when the packer FIFO has been written but was full at the time
447 * and we are in FW_OV_MODE and FW_OV_ENTROPY_INSERT modes.
448 */
450 /**
451 * Triggered when FW_OV_SHA3_START has been set to kMultiBitBool4False,
452 * without waiting for the bypass packer FIFO to clear.
453 */
455 /**
456 * Triggered when the FIPS_FLAG field in the CONF register is set to an
457 * unsupported value.
458 */
460 /**
461 * Triggered when the RNG_FIPS field in the CONF register is set to an
462 * unsupported value.
463 */
465 /**
466 * All alert reasons.
467 *
468 * This is useful when clearing all recoverable alerts at once.
469 */
472
473/**
474 * Error codes (non-recoverable).
475 */
477 /**
478 * Triggered when a write error has been detected for the esrng FIFO.
479 */
481 /**
482 * Triggered when a read error has been detected for the esrng FIFO.
483 */
485 /**
486 * Triggered when a state error has been detected for the esrng FIFO.
487 */
489 /**
490 * Triggered when a write error has been detected for the observe FIFO.
491 */
493 /**
494 * Triggered when a read error has been detected for the observe FIFO.
495 */
497 /**
498 * Triggered when a state error has been detected for the observe FIFO.
499 */
501 /**
502 * Triggered when a write error has been detected for the esfinal FIFO.
503 */
505 /**
506 * Triggered when a read error has been detected for the esfinal FIFO.
507 */
509 /**
510 * Triggered when a state error has been detected for the esfinal FIFO.
511 */
513 /**
514 * Triggered when an error has been detected for the acknowledgment stage
515 * state machine.
516 */
518 /**
519 * Triggered when an error has been detected for the main state machine.
520 */
522 /**
523 * Triggered when an error has been detected for a hardened counter.
524 */
527
528/**
529 * Stops the current mode of operation and disables the entropy_src module
530 *
531 * @param entropy_src An entropy source handle.
532 * @return The result of the operation.
533 */
536
537/**
538 * Configures entropy source with runtime information.
539 *
540 * This function should only need to be called once for the lifetime of the
541 * `entropy` handle.
542 *
543 * @param entropy_src An entropy source handle.
544 * @param config Runtime configuration parameters.
545 * @param enabled The enablement state of the entropy source.
546 * @return The result of the operation.
547 */
551 dif_toggle_t enabled);
552
553/**
554 * Configures entropy source firmware override feature with runtime information.
555 *
556 * This function should only need to be called once for the lifetime of the
557 * `entropy` handle.
558 *
559 * @param entropy_src An entropy source handle.
560 * @param config Runtime configuration parameters for firmware override.
561 * @param enabled The enablement state of the firmware override option.
562 * @return The result of the operation.
563 */
566 const dif_entropy_src_t *entropy_src,
568
569/**
570 * Configures whether to start the entropy source's SHA3 process and be ready to
571 * accept entropy data.
572 *
573 * This is used in firmware override mode and should be enabled before writing
574 * to the override FIFO. Disable this after writing has finished to ensure the
575 * SHA3 block finishes processing and pushes the results to the `esfinal` FIFO.
576 *
577 * @param entropy_src An entropy source handle.
578 * @param enabled Whether to start the SHA3 process.
579 */
582 const dif_entropy_src_t *entropy_src, dif_toggle_t enabled);
583
584/**
585 * Configures an entropy source health test feature with runtime information.
586 *
587 * This function should only need to be called once for each health test that
588 * requires configuration for the lifetime of the `entropy` handle.
589 *
590 * @param entropy_src An entropy source handle.
591 * @param config Runtime configuration parameters for the health test.
592 * @return The result of the operation.
593 */
596 const dif_entropy_src_t *entropy_src,
598
599/**
600 * Configures the health test watermark number register to record the high or
601 * low watermark of a specific health test.
602 *
603 * This function is primarily required for the initial validation during chip
604 * bring-up.
605 *
606 * @param entropy_src An entropy source handle.
607 * @param config Specific health test and high or low watermark to be recorded.
608 * @return The result of the operation.
609 */
612 const dif_entropy_src_t *entropy_src,
614
615/**
616 * Enables/Disables the entropy source.
617 *
618 * @param entropy_src An entropy source handle.
619 * @param enabled The enablement state to configure the entropy source in.
620 * @return The result of the operation.
621 */
624 dif_toggle_t enabled);
625
626/**
627 * Locks out entropy source functionality.
628 *
629 * This function is reentrant: calling it while functionality is locked will
630 * have no effect and return `kDifEntropySrcOk`.
631 *
632 * @param entropy_src An entropy source handle.
633 * @return The result of the operation.
634 */
637
638/**
639 * Checks whether this entropy source is locked.
640 *
641 * @param entropy_src An entropy source handle.
642 * @param[out] is_locked Out-param for the locked state.
643 * @return The result of the operation.
644 */
647 bool *is_locked);
648
649/**
650 * Queries the entropy_src source IP for its revision information.
651 *
652 * @param entropy_src An entropy source handle.
653 * @param[out] revision Out-param for revision data.
654 * @return The result of the operation.
655 */
659
660/**
661 * Queries the entropy source for health test statistics.
662 *
663 * @param entropy_src An entropy source handle.
664 * @param[out] stats Out-param for stats data.
665 * @return The result of the operation.
666 */
669 const dif_entropy_src_t *entropy_src,
671
672/**
673 * Queries the entropy source for health test failure statistics.
674 *
675 * @param entropy_src An entropy source handle.
676 * @param[out] counts Out-param for test failure data that triggers alerts.
677 * @return The result of the operation.
678 */
681 const dif_entropy_src_t *entropy_src,
683
684/**
685 * Checks to see if entropy is available for software consumption.
686 *
687 * @param entropy_src An entropy source handle.
688 * @return The result of the operation.
689 */
692 const dif_entropy_src_t *entropy_src);
693
694/**
695 * Reads a word of entropy from the entropy source.
696 *
697 * @param entropy_src An entropy source handle.
698 * @param[out] word Out-param for the entropy word.
699 * @return The result of the operation.
700 */
703 const dif_entropy_src_t *entropy_src, uint32_t *word);
704
705/**
706 * Performs a blocking read from the entropy pipeline through the observe FIFO,
707 * which contains post health-test, unconditioned entropy.
708 *
709 * The entropy source must be configured with firmware override mode enabled,
710 * and the `len` parameter must be less than or equal to the FIFO threshold set
711 * in the firmware override parameters (that is, the threshold that triggers an
712 * interrupt). Additionally, `buf` may be `NULL`; in this case, reads will be
713 * discarded.
714 *
715 * @param entropy_src An entropy source handle.
716 * @param[out] buf A buffer to fill with words from the pipeline.
717 * @param len The number of words to read into `buf`.
718 * @return The result of the operation.
719 */
722 const dif_entropy_src_t *entropy_src, uint32_t *buf, size_t len);
723
724/**
725 * Performs a nonblocking read from the entropy pipeline through the observe
726 * FIFO, which contains post health-test, unconditioned entropy.
727 *
728 * The entropy source must be configured with firmware override mode enabled.
729 * This function will read at most `*len` words from the observe FIFO and store
730 * them in `buf` if it is not `NULL`. If `buf` is `NULL` then the reads will be
731 * discarded instead. This function never blocks and returns as soon as the FIFO
732 * is empty. It updates `*len` to store the number of actually read words.
733 *
734 * @param entropy_src An entropy source handle.
735 * @param[out] buf A buffer to fill with words from the pipeline.
736 * @param[inout] len A pointer to the maximum number of words to reads. This
737 * value is updated to contain the number of words acually read.
738 * @return The result of the operation.
739 */
742 const dif_entropy_src_t *entropy_src, uint32_t *buf, size_t *len);
743
744/**
745 * Performs a write to the entropy pipeline through the firmware override FIFO.
746 *
747 * Entropy source must be configured with firmware override and insert mode
748 * enabled, otherwise the function will return `kDifError`.
749 *
750 * @param entropy_src An entropy source handle.
751 * @param buf A buffer to push words from into the pipeline.
752 * @param len The number of words to write from `buf`.
753 * @param[out] written The number of words successfully written.
754 * @return The result of the operation.
755 */
758 const dif_entropy_src_t *entropy_src, const uint32_t *buf, size_t len,
759 size_t *written);
760
761/**
762 * Starts conditioner operation.
763 *
764 * Initializes the conditioner. Use the `dif_entropy_src_fw_ov_data_write()`
765 * function to send data to the conditioner, and
766 * `dif_entropy_src_conditioner_stop()` once ready to stop the conditioner
767 * operation.
768 *
769 * This function is only available when firmware override mode is enabled. See
770 * `dif_entropy_src_fw_override_configure()` for more details.
771 *
772 * @param entropy_src An entropy source handle.
773 * @return The result of the operation.
774 */
777 const dif_entropy_src_t *entropy_src);
778
779/**
780 * Stops conditioner operation.
781 *
782 * The conditioner stops processing input data and deposits the result digest
783 * in the entropy source output buffer. This operation is only available in
784 * firmware override mode.
785 *
786 * @param entropy_src An entropy source handle.
787 * @return The result of the operation.
788 */
791 const dif_entropy_src_t *entropy_src);
792
793/**
794 * Checks whether the firmware override write FIFO is full.
795 *
796 * @param entropy_src An entropy source handle.
797 * @param[out] is_full Whether the FIFO is full.
798 * @return The result of the operation.
799 */
802 bool *is_full);
803
804/**
805 * Checks whether the firmware override read FIFO has overflowed.
806 *
807 * @param entropy_src An entropy source handle.
808 * @param[out] has_overflowed Whether the FIFO has overflowed, and data has been
809 * lost.
810 * @return The result of the operation.
811 */
814 const dif_entropy_src_t *entropy_src, bool *has_overflowed);
815
816/**
817 * Read the firmware override FIFO depth.
818 *
819 * @param entropy_src An entropy source handle.
820 * @param[out] fifo_depth The FIFO depth.
821 * @return The result of the operation.
822 */
825 const dif_entropy_src_t *entropy_src, uint32_t *fifo_depth);
826
827/**
828 * Reads the debug status register.
829 *
830 * @param entropy_src An entropy source handle.
831 * @param[out] debug_state The current debug state of the IP.
832 * @return The result of the operation.
833 */
836 const dif_entropy_src_t *entropy_src,
837 dif_entropy_src_debug_state_t *debug_state);
838
839/**
840 * Reads the recoverable alert status register.
841 *
842 * @param entropy_src An entropy source handle.
843 * @param[out] alerts The alerts that were triggered (one or more
844 * `dif_entropy_src_alert_t`'s ORed together).
845 * @return The result of the operation.
846 */
849 const dif_entropy_src_t *entropy_src, uint32_t *alerts);
850
851/**
852 * Clears the alerts that are recoverable.
853 *
854 * @param entropy_src An entropy source handle.
855 * @param alerts The alerts to be cleared (one or more
856 * `dif_entropy_src_alert_t`'s ORed together).
857 * @return The result of the operation.
858 */
861 const dif_entropy_src_t *entropy_src, uint32_t alerts);
862
863/**
864 * Reads the (nonrecoverable) error code status register.
865 *
866 * @param entropy_src An entropy source handle.
867 * @param[out] errors The errors that were triggered (one or more
868 * `dif_entropy_src_error_t`'s ORed together).
869 * @return The result of the operation.
870 */
873 uint32_t *errors);
874
875/**
876 * Forces the hardware to generate a error for testing purposes.
877 *
878 * @param entropy_src An entropy source handle.
879 * @param error The error to force (for testing purposes).
880 * @return The result of the operation.
881 */
885
886/**
887 * Reads the current main FSM state.
888 *
889 * @param entropy_src An entropy source handle.
890 * @param[out] state The current FSM state.
891 * @return The result of the operation.
892 */
895 const dif_entropy_src_t *entropy_src, dif_entropy_src_main_fsm_t *state);
896
897#ifdef __cplusplus
898} // extern "C"
899#endif // __cplusplus
900
901#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ENTROPY_SRC_H_