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_CTR_H__ 9 #define __CC3XX_DRBG_CTR_H__ 10 11 #include "cc3xx_error.h" 12 #include "cc3xx_aes.h" 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 /** 19 * @brief CTR mode uses AES only as underlying block cipher 20 * 21 */ 22 #define CC3XX_DRBG_CTR_BLOCKLEN (AES_BLOCK_SIZE) 23 24 /** 25 * @brief CTR mode uses AES-128 only as underlying block cipher 26 * 27 */ 28 #define CC3XX_DRBG_CTR_KEYLEN (16) 29 30 /** 31 * @brief AES-128 key length in words, as used by the CTR_DRBG module 32 * 33 */ 34 #define CC3XX_DRBG_CTR_KEYLEN_WORDS ((CC3XX_DRBG_CTR_KEYLEN)/(4)) 35 36 /** 37 * @brief CTR mode uses AES only as underlying block cipher, and this 38 * define provides the value of the AES block length in words 39 * 40 */ 41 #define CC3XX_DRBG_CTR_BLOCKLEN_WORDS ((CC3XX_DRBG_CTR_BLOCKLEN)/(4)) 42 43 /** 44 * @brief CTR_DRBG defines SEEDLEN as BLOCKLEN + KEYLEN 45 * 46 */ 47 #define CC3XX_DRBG_CTR_SEEDLEN (CC3XX_DRBG_CTR_BLOCKLEN + CC3XX_DRBG_CTR_KEYLEN) 48 49 /** 50 * @brief CTR_DRBG defines SEEDLEN as BLOCKLEN + KEYLEN (This is in words) 51 * 52 */ 53 #define CC3XX_DRBG_CTR_SEEDLEN_WORDS (CC3XX_DRBG_CTR_BLOCKLEN_WORDS + CC3XX_DRBG_CTR_KEYLEN_WORDS) 54 55 /** 56 * @brief Contains the state of the CTR_DRBG 57 * 58 */ 59 struct cc3xx_drbg_ctr_state_t { 60 uint32_t key_k[CC3XX_DRBG_CTR_KEYLEN_WORDS]; 61 uint32_t block_v[CC3XX_DRBG_CTR_BLOCKLEN_WORDS]; 62 uint32_t reseed_counter; 63 }; 64 65 /** 66 * @brief Instantiate the CTR_DRBG 67 * 68 * @param[out] state A pointer to a state structure 69 * @param[in] entropy Buffer containing the entropy for the instantiation 70 * @param[in] entropy_len Size in bytes of the entropy buffer \param entropy 71 * @param[in] nonce Buffer containing the nonce 72 * @param[in] nonce_len Size in bytes of the nonce buffer \param nonce 73 * @param[in] personalization Buffer containing the personalization string 74 * @param[in] personalization_len Size in bytes of the personalization buffer \param personalization 75 * 76 * @return cc3xx_err_t 77 */ 78 cc3xx_err_t cc3xx_lowlevel_drbg_ctr_init( 79 struct cc3xx_drbg_ctr_state_t *state, 80 const uint8_t *entropy, size_t entropy_len, 81 const uint8_t *nonce, size_t nonce_len, 82 const uint8_t *personalization, size_t personalization_len); 83 84 /** 85 * @brief Generates random bits from the CTR_DRBG instance 86 * 87 * @param[in,out] state Pointer to an instantiated CTR_DRBG generator 88 * @param[in] len_bits Size in bits to be generated. Must be byte aligned for simplicity 89 * @param[out] returned_bits Pointer where the random bits will be written to 90 * @param[in] additional_input Pointer to the additional input to be used 91 * @param[in] additional_input_len Size in bytes of the additional input to be used 92 * 93 * @return cc3xx_err_t 94 */ 95 cc3xx_err_t cc3xx_lowlevel_drbg_ctr_generate( 96 struct cc3xx_drbg_ctr_state_t *state, 97 size_t len_bits, uint8_t *returned_bits, 98 const uint8_t *additional_input, size_t additional_input_len); 99 100 /** 101 * @brief Reseeds the CTR_DRBG 102 * 103 * @param[in,out] state A pointer to a state structure 104 * @param[in] entropy Entropy to be used for reseeding 105 * @param[in] entropy_len Size in bytes of the entropy pointed by \param entropy 106 * @param[in] additional_input Optional pointer containing additional input for reseeding 107 * @param[in] additional_input_len Size in bytes of the buffer pointed by \param additional_input 108 * 109 * @return cc3xx_err_t 110 */ 111 cc3xx_err_t cc3xx_lowlevel_drbg_ctr_reseed( 112 struct cc3xx_drbg_ctr_state_t *state, 113 const uint8_t *entropy, size_t entropy_len, 114 const uint8_t *additional_input, size_t additional_input_len); 115 116 /** 117 * @brief Un-initializes the state structure associated to the CTR_DRBG 118 * 119 * @param[out] state Pointer to the state structure 120 * 121 * @return cc3xx_err_t 122 */ 123 cc3xx_err_t cc3xx_lowlevel_drbg_ctr_uninit( 124 struct cc3xx_drbg_ctr_state_t *state); 125 126 #ifdef __cplusplus 127 } 128 #endif 129 130 #endif /* __CC3XX_DRBG_CTR_H__ */ 131