1 /*
2  * Copyright (c) 2023, The TrustedFirmware-M Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef __CC3XX_DRBG_HASH_H__
9 #define __CC3XX_DRBG_HASH_H__
10 
11 #include <stdint.h>
12 #include "cc3xx_error.h"
13 
14 #include "cc3xx_hash.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /**
21  * @brief The value of seedlen is fixed by the spec to 440 bits. This
22  *        defines is used to convert it to bytes
23  */
24 #define CC3XX_DRBG_HASH_SEEDLEN (440/8)
25 
26 /**
27  * @brief Contains the state of the HASH_DRBG
28  *
29  */
30 struct cc3xx_drbg_hash_state_t {
31     uint8_t value_v[CC3XX_DRBG_HASH_SEEDLEN + 1]; /* +1 for word alignment */
32     uint8_t constant_c[CC3XX_DRBG_HASH_SEEDLEN + 1];
33     uint32_t reseed_counter;
34 };
35 
36 /**
37  * @brief Instantiate the HASH_DRBG generator
38  *
39  * @param[out] state              Pointer to a \ref cc3xx_drbg_hash_state_t context
40  * @param[in] entropy             Pointer to the collected entropy
41  * @param[in] entropy_len         Size in bytes of the entropy
42  * @param[in] nonce               Pointer to the nonce to be used
43  * @param[in] nonce_len           Size in bytes of the nonce
44  * @param[in] personalization     Pointer to the personalisation string to be used
45  * @param[in] personalization_len Size in bytes of the personalisation string
46  * @return cc3xx_err_t
47  */
48 cc3xx_err_t cc3xx_lowlevel_drbg_hash_init(
49     struct cc3xx_drbg_hash_state_t *state,
50     const uint8_t *entropy, size_t entropy_len,
51     const uint8_t *nonce, size_t nonce_len,
52     const uint8_t *personalization, size_t personalization_len);
53 
54 /**
55  * @brief Generates random bits from the HASH_DRBG instance
56  *
57  * @param[in,out] state            Pointer to an instantiated HASH_DRBG generator
58  * @param[in] len_bits             Size in bits to be generated. Must be byte aligned for simplicity
59  * @param[out] returned_bits       Pointer holding the returned random bit string
60  * @param[in] additional_input     Pointer to the additional input to be used
61  * @param[in] additional_input_len Size in bytes of the additional input to be used
62  * @return cc3xx_err_t
63  */
64 cc3xx_err_t cc3xx_lowlevel_drbg_hash_generate(
65     struct cc3xx_drbg_hash_state_t *state,
66     size_t len_bits, uint8_t *returned_bits,
67     const uint8_t *additional_input, size_t additional_input_len);
68 
69 /**
70  * @brief Reseeds the HASH_DRBG instance
71  *
72  * @param[in,out] state            Pointer to an instantiated HASH_DRBG generator to reseed
73  * @param[in] entropy              Pointer to the additional entropy to use for reseeding
74  * @param[in] entropy_len          Size in bytes of the additional entropy
75  * @param[in] additional_input     Pointer to the additional input to use for reseeding
76  * @param[in] additional_input_len Size in bytes of the additional input buffer
77  * @return cc3xx_err_t
78  */
79 cc3xx_err_t cc3xx_lowlevel_drbg_hash_reseed(
80     struct cc3xx_drbg_hash_state_t *state,
81     const uint8_t *entropy, size_t entropy_len,
82     const uint8_t *additional_input, size_t additional_input_len);
83 
84 /**
85  * @brief Un-initializes the state structure associated to the HASH_DRBG
86  *
87  * @param[out] state Pointer to the state structure
88  * @return cc3xx_err_t
89  */
90 cc3xx_err_t cc3xx_lowlevel_drbg_hash_uninit(struct cc3xx_drbg_hash_state_t *state);
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #endif /* __CC3XX_DRBG_HASH_H__ */
97