Mistake on this page?
Report an issue in GitHub or email us
crys_rnd.h
Go to the documentation of this file.
1 /**************************************************************************************
2 * Copyright (c) 2016-2017, ARM Limited or its affiliates. All rights reserved *
3 * *
4 * This file and the related binary are licensed under the following license: *
5 * *
6 * ARM Object Code and Header Files License, v1.0 Redistribution. *
7 * *
8 * Redistribution and use of object code, header files, and documentation, without *
9 * modification, are permitted provided that the following conditions are met: *
10 * *
11 * 1) Redistributions must reproduce the above copyright notice and the *
12 * following disclaimer in the documentation and/or other materials *
13 * provided with the distribution. *
14 * *
15 * 2) Unless to the extent explicitly permitted by law, no reverse *
16 * engineering, decompilation, or disassembly of is permitted. *
17 * *
18 * 3) Redistribution and use is permitted solely for the purpose of *
19 * developing or executing applications that are targeted for use *
20 * on an ARM-based product. *
21 * *
22 * DISCLAIMER. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
23 * CONTRIBUTORS "AS IS." ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT *
24 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, *
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED *
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
33 **************************************************************************************/
34 
35 
36 
37 
38 #ifndef CRYS_RND_H
39 #define CRYS_RND_H
40 
41 #include "crys_error.h"
42 #include "ssi_aes.h"
43 
44 #ifdef __cplusplus
45 extern "C"
46 {
47 #endif
48 
49 /*!
50 @file
51 @brief This file contains the CRYS APIs used for random number generation.
52 The random-number generation module implements referenced standard [SP800-90].
53 @defgroup crys_rnd CryptoCell Random Generator APIs
54 @{
55 @ingroup cryptocell_api
56 */
57 
58 /************************ Defines ******************************/
59 
60 /*! Maximal reseed counter - indicates maximal number of
61 requests allowed between reseeds; according to NIST 800-90
62 it is (2^48 - 1), our restriction is : (0xFFFFFFFF - 0xF).*/
63 #define CRYS_RND_MAX_RESEED_COUNTER (0xFFFFFFFF - 0xF)
64 
65 /* Max size for one RNG generation (in bits) =
66  max_num_of_bits_per_request = 2^19 (FIPS 800-90 Tab.3) */
67 /*! Maximal size of generated vector in bits. */
68 #define CRYS_RND_MAX_GEN_VECTOR_SIZE_BITS 0x7FFFF
69 /*! Maximal size of generated vector in bytes. */
70 #define CRYS_RND_MAX_GEN_VECTOR_SIZE_BYTES 0xFFFF
71 
72 /*! AES output block size in words. */
73 #define CRYS_RND_AES_BLOCK_SIZE_IN_WORDS SASI_AES_BLOCK_SIZE_IN_WORDS
74 
75 
76 /* RND seed and additional input sizes */
77 /*! Maximal size of random seed in words. */
78 #define CRYS_RND_SEED_MAX_SIZE_WORDS 12
79 
80 #ifndef CRYS_RND_ADDITINAL_INPUT_MAX_SIZE_WORDS
81 /*! Maximal size of additional input data in words. */
82 #define CRYS_RND_ADDITINAL_INPUT_MAX_SIZE_WORDS CRYS_RND_SEED_MAX_SIZE_WORDS
83 #endif
84 
85 /* allowed sizes of AES Key, in words */
86 /*! AES key size (128 bits) in words. */
87 #define CRYS_RND_AES_KEY_128_SIZE_WORDS 4
88 /*! AES key size (192 bits) in words. */
89 #define CRYS_RND_AES_KEY_192_SIZE_WORDS 6
90 /*! AES key size (256 bits) in words. */
91 #define CRYS_RND_AES_KEY_256_SIZE_WORDS 8
92 
93 /* Definitions of temp buffer for RND_DMA version of CRYS_RND */
94 /*******************************************************************/
95 /* Definitions of temp buffer for DMA version of CRYS_RND */
96 
97 /*! Temporary buffer size in words. */
98 #define CRYS_RND_WORK_BUFFER_SIZE_WORDS 1528
99 
100 /*! A definition for RAM buffer to be internally used in instantiation (or reseeding) operation. */
101 typedef struct
102 {
103  /*! Internal buffer*/
104  uint32_t crysRndWorkBuff[CRYS_RND_WORK_BUFFER_SIZE_WORDS];
106 
107 /*! A definition for entropy estimation data type. */
108 #define CRYS_RND_EntropyEstimatData_t CRYS_RND_WorkBuff_t
109 /*! A definition for entropy estimation buffer. */
110 #define crysRndEntrIntBuff crysRndWorkBuff
111 
112 
113 /* RND source buffer inner (entrpopy) offset */
114 /*! An internal offset definition. */
115 #define CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS 2
116 /*! An internal offset definition. */
117 #define CRYS_RND_TRNG_SRC_INNER_OFFSET_BYTES (CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS*sizeof(uint32_t))
118 
119 
120 
121 
122 /* Size of the expected output buffer used by FIPS KAT */
123 /*! FIPS Known answer test output size. */
124 #define CRYS_PRNG_FIPS_KAT_OUT_DATA_SIZE 64
125 
126 /************************ Enumerators ****************************/
127 
128 /*! Definition of random operation modes. */
129 typedef enum
130 {
131  /*! SW entropy estimation mode. */
133  /*! Full entropy mode. */
135  /*! Reserved. */
136  CRYS_RND_ModeLast = 0x7FFFFFFF,
138 
139 
140 
141 /************************ Structs *****************************/
142 
143 
144 /* The internal state of DRBG mechanism based on AES CTR and CBC-MAC
145  algorithms. It is set as global data defined by the following
146  structure */
147 /*! RND state structure. Includes internal data that needs to be saved between boots by the user.*/
148 typedef struct
149 {
150  /* Seed buffer, consists from concatenated Key||V: max size 12 words */
151  /*! Random Seed buffer */
153  /* Previous value for continuous test */
154  /*! Previous random data (used for continuous test). */
155  uint32_t PreviousRandValue[SASI_AES_BLOCK_SIZE_IN_WORDS];
156 
157  /* AdditionalInput buffer max size = seed max size words + 4w for padding*/
158  /*! Previous additional input buffer. */
159  uint32_t PreviousAdditionalInput[CRYS_RND_ADDITINAL_INPUT_MAX_SIZE_WORDS+5];
160  /*! Additional input buffer. */
161  uint32_t AdditionalInput[CRYS_RND_ADDITINAL_INPUT_MAX_SIZE_WORDS+4];
162  /*! Additional input size in words. */
163  uint32_t AddInputSizeWords; /* size of additional data set by user, words */
164 
165  /*! Entropy source size in words */
167 
168  /*! Reseed counter (32 bits active) - indicates number of requests for entropy
169  since instantiation or reseeding */
170  uint32_t ReseedCounter;
171 
172  /*! Key size: 4 or 8 words according to security strength 128 bits or 256 bits*/
173  uint32_t KeySizeWords;
174 
175  /* State flag (see definition of StateFlag above), containing bit-fields, defining:
176  - b'0: instantiation steps: 0 - not done, 1 - done;
177  - 2b'9,8: working or testing mode: 0 - working, 1 - KAT DRBG test, 2 -
178  KAT TRNG test;
179  b'16: flag defining is Previous random valid or not:
180  0 - not valid, 1 - valid */
181  /*! State flag used internally in the code.*/
182  uint32_t StateFlag;
183 
184  /* Trng processing flag - indicates which ROSC lengths are:
185  - allowed (bits 0-3);
186  - total started (bits 8-11);
187  - processed (bits 16-19);
188  - started, but not processed (bits24-27) */
189  /*! TRNG process state used internally in the code */
190  uint32_t TrngProcesState;
191 
192  /* validation tag */
193  /*! Validation tag used internally in the code */
194  uint32_t ValidTag;
195 
196  /*! Rnd source entropy size in bits */
197  uint32_t EntropySizeBits;
198 
200 
201 
202 /*! The RND Generate vector function pointer type definition.
203  The prototype intendent for External and CRYS internal RND functions
204  pointers definitions.
205  Full description can be found in ::CRYS_RND_GenerateVector function API. */
206 typedef uint32_t (*SaSiRndGenerateVectWorkFunc_t)( \
207  void *rndState_ptr, /*context*/ \
208  uint16_t outSizeBytes, /*in*/ \
209  uint8_t *out_ptr /*out*/);
210 
211 
212 
213 /*! Data structure required for internal FIPS verification for PRNG KAT. */
214 typedef struct
215 {
216  /*! Internal working buffer. */
218  /*! Output buffer. */
219  uint8_t rndOutputBuff[CRYS_PRNG_FIPS_KAT_OUT_DATA_SIZE];
221 
222 
223 /*****************************************************************************/
224 /********************** Public Functions *************************/
225 /*****************************************************************************/
226 
227 /*!
228 @brief This function initializes the RND context.
229 It must be called at least once prior to using this context with any API that requires it as a parameter (e.g., other RND APIs, asymmetric
230 cryptography key generation and signatures).
231 It is called as part of ARM TrustZone CryptoCell library initialization, which initializes and returns the primary RND context.
232 This primary context can be used as a single global context for all RND needs.
233 Alternatively, other contexts may be initialized and used with a more limited scope (for specific applications or specific threads).
234 The call to this function must be followed by a call to ::CRYS_RND_SetGenerateVectorFunc API to set the generate vector function.
235 It implements referenced standard [SP800-90] - 10.2.1.3.2 - CTR-DRBG Instantiate algorithm using AES (FIPS-PUB 197) and Derivation Function (DF).
236 \note Additional data can be mixed with the random seed (personalization data or nonce). If required, this data should be provided by calling ::CRYS_RND_AddAdditionalInput prior to using this API.
237 
238 @return CRYS_OK on success.
239 @return A non-zero value from crys_rnd_error.h on failure.
240 */
242  void *rndState_ptr, /*!< [in/out] Pointer to the RND state buffer allocated by the user, which is used to
243  maintain the RND state. This context state must be saved and provided as a
244  parameter to any API that uses the RND module.
245  \note the context must be cleared before sent to the function. */
246  CRYS_RND_WorkBuff_t *rndWorkBuff_ptr /*!< [in/out] Scratchpad for the RND module's work. */
247 );
248 
249 
250 /*!
251 @brief Clears existing RNG instantiation state.
252 
253 @return CRYS_OK on success.
254 @return A non-zero value from crys_rnd_error.h on failure.
255 */
257  void *rndState_ptr /*!< [in/out] Pointer to the RND context state buffer. */
258 );
259 
260 
261 /*!
262 @brief This function is used for reseeding the RNG with additional entropy and additional user-provided input.
263 (additional data should be provided by calling ::CRYS_RND_AddAdditionalInput prior to using this API).
264 It implements referenced standard [SP800-90] - 10.2.1.4.2 - CTR-DRBG Reseeding algorithm, using AES (FIPS-PUB 197) and Derivation Function (DF).
265 
266 @return CRYS_OK on success.
267 @return A non-zero value from crys_rnd_error.h on failure.
268 */
270  void *rndState_ptr, /*!< [in/out] Pointer to the RND context buffer. */
271  CRYS_RND_WorkBuff_t *rndWorkBuff_ptr /*!< [in/out] Scratchpad for the RND module's work. */
272 );
273 
274 
275 /****************************************************************************************/
276 /*!
277 @brief Generates a random vector according to the algorithm defined in referenced standard [SP800-90] - 10.2.1.5.2 - CTR-DRBG.
278 The generation algorithm uses AES (FIPS-PUB 197) and Derivation Function (DF).
279 
280 \note
281 <ul id="noteb"><li> The RND module must be instantiated prior to invocation of this API.</li>
282 <li> In the following cases, Reseeding operation must be performed prior to vector generation:</li>
283  <ul><li> Prediction resistance is required.</li>
284  <li> The function returns CRYS_RND_RESEED_COUNTER_OVERFLOW_ERROR, stating that the Reseed Counter has passed its upper-limit (2^32-2).</li></ul></ul>
285 
286 @return CRYS_OK on success.
287 @return A non-zero value from crys_rnd_error.h on failure.
288 */
290  void *rndState_ptr, /*!< [in/out] Pointer to the RND state structure, which is part of the RND context structure.
291  Use rndContext->rndState field of the context for this parameter. */
292  uint16_t outSizeBytes, /*!< [in] The size in bytes of the random vector required. The maximal size is 2^16 -1 bytes. */
293  uint8_t *out_ptr /*!< [out] The pointer to output buffer. */
294 );
295 
296 
297 
298 /**********************************************************************************************************/
299 /*!
300 @brief Generates a random vector with specific limitations by testing candidates (described and used in FIPS 186-4: B.1.2, B.4.2 etc.).
301 
302 This function draws a random vector, compare it to the range limits, and if within range - return it in rndVect_ptr.
303 If outside the range, the function continues retrying until a conforming vector is found, or the maximal retries limit is exceeded.
304 If maxVect_ptr is provided, rndSizeInBits specifies its size, and the output vector must conform to the range [1 < rndVect < maxVect].
305 If maxVect_ptr is NULL, rndSizeInBits specifies the exact required vector size, and the output vector must be the exact same
306 bit size (with its most significant bit = 1).
307 \note
308 The RND module must be instantiated prior to invocation of this API.
309 
310 @return CRYS_OK on success.
311 @return A non-zero value from crys_rnd_error.h on failure.
312 */
314  void *rndState_ptr, /*!< [in/out] Pointer to the RND state structure. */
315  SaSiRndGenerateVectWorkFunc_t rndGenerateVectFunc, /*!< [in] Pointer to the random vector generation function. */
316  uint32_t rndSizeInBits, /*!< [in] The size in bits of the random vector required. The allowed size in range 2 <= rndSizeInBits < 2^19-1, bits. */
317  uint8_t *maxVect_ptr, /*!< [in] Pointer to the vector defining the upper limit for the random vector output, Given as little-endian byte array.
318  If not NULL, its actual size is treated as [(rndSizeInBits+7)/8] bytes and its value must be in range (3, 2^19) */
319  uint8_t *rndVect_ptr /*!< [in/out] Pointer to the output buffer for the random vector. Must be at least [(rndSizeInBits+7)/8] bytes.
320  Treated as little-endian byte array. */
321 );
322 
323 
324 /*************************************************************************************/
325 /*!
326 @brief Used for adding additional input/personalization data provided by the user,
327 to be later used by the ::CRYS_RND_Instantiation/::CRYS_RND_Reseeding/::CRYS_RND_GenerateVector functions.
328 
329 @return CRYS_OK on success.
330 @return A non-zero value from crys_rnd_error.h on failure.
331 */
333  void *rndState_ptr, /*!< [in/out] Pointer to the RND context state buffer. */
334  uint8_t *additonalInput_ptr, /*!< [in] The Additional Input buffer. */
335  uint16_t additonalInputSize /*!< [in] The size of the Additional Input buffer. It must
336  be <= CRYS_RND_ADDITINAL_INPUT_MAX_SIZE_WORDS and a multiple of 4. */
337 );
338 
339 /*!
340 @brief The CRYS_RND_EnterKatMode function sets KAT mode bit into StateFlag of global CRYS_RND_WorkingState structure.
341 
342 The user must call this function before calling functions performing KAT tests.
343 
344 \note Total size of entropy and nonce must be not great than 126 words (maximal size of entropy and nonce).
345 
346 @return CRYS_OK on success.
347 @return A non-zero value from crys_rnd_error.h on failure.
348 */
350  void *rndState_ptr, /*!< [in/out] Pointer to the RND context state buffer. */
351  uint8_t *entrData_ptr, /*!< [in] Entropy data. */
352  uint32_t entrSize, /*!< [in] Entropy size in bytes. */
353  uint8_t *nonce_ptr, /*!< [in] Nonce. */
354  uint32_t nonceSize, /*!< [in] Entropy size in bytes. */
355  CRYS_RND_WorkBuff_t *workBuff_ptr /*!< [out] RND working buffer, must be the same buffer, which should be passed into
356  Instantiation/Reseeding functions. */
357 );
358 
359 /**********************************************************************************************************/
360 /*!
361 @brief The CRYS_RND_DisableKatMode function disables KAT mode bit into StateFlag of global CRYS_RND_State_t structure.
362 
363 The user must call this function after KAT tests before actual using RND module (Instantiation etc.).
364 
365 @return CRYS_OK on success.
366 @return A non-zero value from crys_rnd_error.h on failure.
367 */
368 CIMPORT_C void CRYS_RND_DisableKatMode(
369  void *rndState_ptr /*!< [in/out] Pointer to the RND state buffer. */
370 );
371 
372 
373 #ifdef __cplusplus
374 }
375 #endif
376 /**
377 @}
378  */
379 #endif /* #ifndef CRYS_RND_H */
380 
uint32_t ValidTag
Definition: crys_rnd.h:194
#define CRYS_RND_SEED_MAX_SIZE_WORDS
Definition: crys_rnd.h:78
uint32_t TrngProcesState
Definition: crys_rnd.h:190
CRYSError_t CRYS_RND_EnterKatMode(void *rndState_ptr, uint8_t *entrData_ptr, uint32_t entrSize, uint8_t *nonce_ptr, uint32_t nonceSize, CRYS_RND_WorkBuff_t *workBuff_ptr)
The CRYS_RND_EnterKatMode function sets KAT mode bit into StateFlag of global CRYS_RND_WorkingState s...
#define CRYS_RND_ADDITINAL_INPUT_MAX_SIZE_WORDS
Definition: crys_rnd.h:82
uint32_t EntropySizeBits
Definition: crys_rnd.h:197
CRYSError_t CRYS_RND_GenerateVector(void *rndState_ptr, uint16_t outSizeBytes, uint8_t *out_ptr)
Generates a random vector according to the algorithm defined in referenced standard [SP800-90] - 10...
CRYSError_t CRYS_RND_Reseeding(void *rndState_ptr, CRYS_RND_WorkBuff_t *rndWorkBuff_ptr)
This function is used for reseeding the RNG with additional entropy and additional user-provided inpu...
uint32_t AddInputSizeWords
Definition: crys_rnd.h:163
#define CRYS_RND_WORK_BUFFER_SIZE_WORDS
Definition: crys_rnd.h:98
uint32_t EntropySourceSizeWords
Definition: crys_rnd.h:166
uint32_t ReseedCounter
Definition: crys_rnd.h:170
#define CRYS_PRNG_FIPS_KAT_OUT_DATA_SIZE
Definition: crys_rnd.h:124
#define SASI_AES_BLOCK_SIZE_IN_WORDS
Definition: ssi_aes_defs.h:62
CRYS_RND_WorkBuff_t rndWorkBuff
Definition: crys_rnd.h:217
CRYSError_t CRYS_RND_AddAdditionalInput(void *rndState_ptr, uint8_t *additonalInput_ptr, uint16_t additonalInputSize)
Used for adding additional input/personalization data provided by the user, to be later used by the C...
CRYSError_t CRYS_RND_UnInstantiation(void *rndState_ptr)
Clears existing RNG instantiation state.
This file contains all of the enums and definitions that are used for the CryptoCell AES APIs...
CRYS_RND_mode_t
Definition: crys_rnd.h:129
uint32_t CRYSError_t
Definition: crys_error.h:253
CRYSError_t CRYS_RND_GenerateVectorInRange(void *rndState_ptr, SaSiRndGenerateVectWorkFunc_t rndGenerateVectFunc, uint32_t rndSizeInBits, uint8_t *maxVect_ptr, uint8_t *rndVect_ptr)
Generates a random vector with specific limitations by testing candidates (described and used in FIPS...
void CRYS_RND_DisableKatMode(void *rndState_ptr)
The CRYS_RND_DisableKatMode function disables KAT mode bit into StateFlag of global CRYS_RND_State_t ...
uint32_t StateFlag
Definition: crys_rnd.h:182
uint32_t(* SaSiRndGenerateVectWorkFunc_t)(void *rndState_ptr, uint16_t outSizeBytes, uint8_t *out_ptr)
Definition: crys_rnd.h:206
uint32_t KeySizeWords
Definition: crys_rnd.h:173
CRYSError_t CRYS_RND_Instantiation(void *rndState_ptr, CRYS_RND_WorkBuff_t *rndWorkBuff_ptr)
This function initializes the RND context. It must be called at least once prior to using this contex...
This module defines the error return code types and the numbering spaces of the error codes for each ...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.