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 the FLAG0 field in the Application Command is set to a value
310 * other than kMultiBitBool4True or kMultiBitBool4False.
311 */
313 1U << CSRNG_RECOV_ALERT_STS_ACMD_FLAG0_FIELD_ALERT_BIT,
314 /**
315 * Indicates the genbits bus saw two identical values, indicating a possible
316 * attack.
317 */
319 1U << CSRNG_RECOV_ALERT_STS_CS_BUS_CMP_ALERT_BIT,
320 /**
321 * Indicates an unsupported CSRNG command was issued.
322 */
324 1U << CSRNG_RECOV_ALERT_STS_CMD_STAGE_INVALID_ACMD_ALERT_BIT,
325 /**
326 * Indicates a Generate command was issued with a `glen` field of zero.
327 */
329 1U << CSRNG_RECOV_ALERT_STS_CMD_STAGE_INVALID_GEN_CMD_ALERT_BIT,
330 /**
331 * Indicates a supported CSRNG command was issued out of sequence.
332 */
334 1U << CSRNG_RECOV_ALERT_STS_CMD_STAGE_INVALID_CMD_SEQ_ALERT_BIT,
335 /**
336 * Indicates that too many generate commands were issued in a row.
337 */
339 1U << CSRNG_RECOV_ALERT_STS_CMD_STAGE_RESEED_CNT_ALERT_BIT,
341
342/**
343 * Configures CSRNG.
344 *
345 * This function should need to be called once for the lifetime of `csrng`.
346 *
347 * @param csrng A CSRNG handle.
348 * @return The result of the operation.
349 */
352
353/**
354 * Initializes CSRNG instance with a new seed value.
355 *
356 * `seed_material` is used as specified in NIST SP 800-90Ar1 section
357 * 10.2.1.3.1. See `dif_csrng_entropy_src` for details on how this value
358 * is mixed with the CSRNG seed.
359 *
360 * `seed_material` can be NULL, in which case CSRNG will use a zero
361 * vector instead.
362 *
363 * @param csrng A CSRNG handle.
364 * @param entropy_src_enable Entropy source input enable.
365 * @param seed_material Seed initialization parameters.
366 * @return The result of the operation.
367 */
370 const dif_csrng_t *csrng, dif_csrng_entropy_src_toggle_t entropy_src_enable,
371 const dif_csrng_seed_material_t *seed_material);
372
373/**
374 * Reseeds CSRNG instance.
375 *
376 * When `seed_material.seed_material_len` is set to 0, only the entropy source
377 * seed is used to reseed the instance, otherwise it will be XOR'ed with the
378 * entropy source.
379 *
380 * @param csrng A CSRNG handle.
381 * @param seed_material Reseed parameters.
382 * @return The result of the operation.
383 */
386 const dif_csrng_seed_material_t *seed_material);
387
388/**
389 * Updates CSRNG state.
390 *
391 * This function is similar to `dif_csrng_reseed()`, except:
392 *
393 * - Only `seed_material.seed_material` is used in the update operation.
394 * - The update operation does not reset the internal CSRNG reseed
395 * counter.
396 *
397 * @param csrng A CSRNG handle.
398 * @param seed_material Update parameters.
399 * @return The result of the operation.
400 */
403 const dif_csrng_seed_material_t *seed_material);
404
405/**
406 * Requests cryptographic entropy bits from the CSRNG.
407 *
408 * The prediction resistance flag as specified in SP 800-90Ar1 section
409 * 10.2.1.1 is not directily supported by the hardware. It is the
410 * responsibility of the caller to reseed as needed before calling
411 * this function.
412 *
413 * The CSRNG accepts generation requests with 128-bit granularity, with
414 * a minimum 128-bit request size. This function will increase the size
415 * of the request to align it to the nearest 128-bit boundary.
416 *
417 * @param csrng A CSRNG handle.
418 * @param additional_data Additional data for the generate command. Set to NULL
419 * if unused.
420 * @param len Number of uint32_t words to generate.
421 * @return The result of the operation. KDifOutOfRange if the `len` parameter
422 * results in a 128bit block level size greater than 0x800.
423 */
426 const dif_csrng_t *csrng, const dif_csrng_seed_material_t *additional_data,
427 size_t len);
428
429/**
430 * Reads the output of the last CSRNG generate call.
431 *
432 * This function reads `len` words out of the CSRNG. This function should be
433 * called repeatedly until the number of words requested in
434 * `dif_csrng_generate_start()` is exhausted. This function will block until
435 * `len` words are read.
436 *
437 * `dif_csrng_get_output_status()` can be called before this function to ensure
438 * there is data available in the CSRNG output buffer.
439 *
440 * @param csrng A CSRNG handle.
441 * @param[out] buf A buffer to fill with words from the pipeline.
442 * @param len The number of words to read into `buf`.
443 * @return The result of the operation.
444 */
446dif_result_t dif_csrng_generate_read(const dif_csrng_t *csrng, uint32_t *buf,
447 size_t len);
448
449/**
450 * Uninstantiates CSRNG
451 *
452 * Resets the CSRNG instance. Values in the CSRNG are zeroed out. This
453 * command effectively resets the CSRNG, clearing any errors that it
454 * may have encountered due to processing or entropy source errors.
455 *
456 * @param csrng An CSRNG handle.
457 * @return The result of the operation.
458 */
461
462/**
463 * Gets the current command interface status.
464 *
465 * This function can be called before or after any of the following functions:
466 *
467 * - `dif_csrng_instantiate()`
468 * - `dif_csrng_reseed()`
469 * - `dif_csrng_update()`
470 * - `dif_csrng_generate_start()`
471 * - `dif_csrng_uninstantiate()`
472 *
473 * @param csrng An CSRNG handle
474 * @param[out] status Command interface status.
475 * @return The result of the operation.
476 */
480
481/**
482 * Forces the status registers to indicate `fifo` as being in an unhealthy
483 * state.
484 *
485 * @param csrng An CSRNG handle
486 * @param fifo The FIFO to mark as unhealthy.
487 * @return The result of the operation.
488 */
491 dif_csrng_fifo_t fifo);
492
493/**
494 * Forces the status registers to indicate a particular error cause.
495 *
496 * @param csrng An CSRNG handle
497 * @param error The error to force.
498 * @return The result of the operation.
499 */
502 dif_csrng_error_t error);
503
504/**
505 * Returns an opaque blob indicating the main state machine's current state.
506 *
507 * @param csrng An CSRNG handle
508 * @param state[out] The state machine state as an opaque blob.
509 * @return The result of the operation.
510 */
513 uint32_t *state);
514
515/**
516 * Returns a bitset indicating which hardware CSRNGs have encountered
517 * exceptions.
518 *
519 * @param csrng An CSRNG handle
520 * @param exceptions[out] The bitset of exception states.
521 * @return The result of the operation.
522 */
525 uint32_t *exceptions);
526
527/**
528 * Clears recorded hardware CSRNG exceptions.
529 *
530 * @param csrng An CSRNG handle
531 * @return The result of the operation.
532 */
535
536/**
537 * Gets the current cryptographic entropy output data status.
538 *
539 * This function can be used before calling `dif_csrng_generate_read()` to
540 * check if there is data available to read.
541 *
542 * @param csrng A CSRNG handle.
543 * @param[out] status CSRNG output status.
544 * @return The result of the operation.
545 */
549
550/**
551 * Gets the working state of a CSRNG instance.
552 *
553 * @param csrng A CSRNG handle
554 * @param instance_id CSRNG instance ID.
555 * @param[out] state The working state of a CSRNG instance.
556 * @return The result of the operation.
557 */
560 const dif_csrng_t *csrng, dif_csrng_internal_state_id_t instance_id,
562
563/**
564 * Gets the reseed counter of a CSRNG instance.
565 *
566 * @param csrng A CSRNG handle
567 * @param instance_id CSRNG instance ID.
568 * @param[out] reseed counter The current reseed counter value of a CSRNG
569 * instance.
570 * @return The result of the operation.
571 */
574 const dif_csrng_t *csrng, dif_csrng_internal_state_id_t instance_id,
575 uint32_t *reseed_counter);
576
577/**
578 * Locks out CSRNG functionality.
579 *
580 * This function is reentrant: calling it while functionality is locked will
581 * have no effect and return `kDifCsrngOk`.
582 *
583 * @param csrng A CSRNG handle.
584 * @return The result of the operation.
585 */
588
589/**
590 * Checks whether this CSRNG is locked.
591 *
592 * @param csrng A CSRNG handle.
593 * @param[out] is_locked Out-param for the locked state.
594 * @return The result of the operation.
595 */
597dif_result_t dif_csrng_is_locked(const dif_csrng_t *csrng, bool *is_locked);
598
599/**
600 * Disables the CSRNG module.
601 *
602 * @param csrng A CSRNG handle.
603 * @return The result of the operation.
604 */
607
608/**
609 * Gets the recoverable alerts currently recorded in the CSRNG block.
610 *
611 * This function returns the alerts in a bitset whose indices are given by
612 * `dif_csrng_recoverable_alert_t`.
613 *
614 * @param csrng A CSRNG handle.
615 * @param[out] alerts Bitset of alerts currently recorded.
616 * @return The result of the operation.
617 */
620 uint32_t *alerts);
621
622/**
623 * Clears all recoverable alerts currently recorded in the CSRNG block.
624 *
625 * @param csrng A CSRNG handle.
626 * @return The result of the operation.
627 */
630
631#ifdef __cplusplus
632} // extern "C"
633#endif // __cplusplus
634
635#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_CSRNG_H_