1 /***************************************************************************//**
2 * \file cy_crypto_core_trng.h
3 * \version 2.120
4 *
5 * \brief
6 * This file provides provides constant and parameters
7 * for the API of the TRNG in the Crypto block driver.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright (c) (2020-2022), Cypress Semiconductor Corporation (an Infineon company) or
12 * an affiliate of Cypress Semiconductor Corporation.
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *******************************************************************************/
27
28 #if !defined (CY_CRYPTO_CORE_TRNG_H)
29 #define CY_CRYPTO_CORE_TRNG_H
30
31 #include "cy_device.h"
32
33 #if defined (CY_IP_MXCRYPTO)
34
35 #include "cy_crypto_common.h"
36
37 #if defined(__cplusplus)
38 extern "C" {
39 #endif
40
41 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 14.3', 2, \
42 'Since value of CY_CRYPTO_V1 is decided by PDL device agnostic / hardware specific model, controlling expression will not have an invariant value.')
43
44 #if (CPUSS_CRYPTO_TR == 1) && defined(CY_CRYPTO_CFG_TRNG_C)
45
46 #include "cy_crypto_core_hw.h"
47
48 #include "cy_crypto_core_trng_config.h"
49
50 typedef cy_en_crypto_status_t (*cy_crypto_trng_func_t)(CRYPTO_Type *base,
51 uint32_t GAROPol,
52 uint32_t FIROPol,
53 uint32_t max,
54 uint32_t *randomNum);
55
56 /**
57 * \addtogroup group_crypto_lld_rng_functions
58 * \{
59 */
60
61 /*******************************************************************************
62 * Function Name: Cy_Crypto_Core_Trng_Init
63 ****************************************************************************//**
64 *
65 * Initialize the TRNG hardware submodule
66 *
67 * \pre If the TRNG submodule is initialized previously, the
68 * \ref Cy_Crypto_Core_Trng_DeInit() must be called before calling this function.
69 *
70 * \param base
71 * The pointer to the CRYPTO instance.
72 *
73 * \param config
74 * The pointer to the configuration structure.
75 *
76 *******************************************************************************/
77 void Cy_Crypto_Core_Trng_Init(CRYPTO_Type *base, cy_stc_crypto_trng_config_t *config);
78
79
80
81 /*******************************************************************************
82 * Function Name: Cy_Crypto_Core_Trng_DeInit
83 ****************************************************************************//**
84 *
85 * Clears all TRNG registers by setting it to hardware default values.
86 *
87 * \param base
88 * The pointer to the CRYPTO instance.
89 *
90 *******************************************************************************/
91 void Cy_Crypto_Core_Trng_DeInit(CRYPTO_Type *base);
92
93
94
95 /*******************************************************************************
96 * Function Name: Cy_Crypto_Core_Trng_Start
97 ****************************************************************************//**
98 *
99 * Starts a random number generation.
100 *
101 * \param base
102 * The pointer to the CRYPTO instance.
103 *
104 * \param dataSize
105 * The maximum length of a random number, in the range [0, 32] bits.
106 *
107 * \return
108 * The error / status code. See \ref cy_en_crypto_status_t.
109 *
110 *******************************************************************************/
111 cy_en_crypto_status_t Cy_Crypto_Core_Trng_Start(CRYPTO_Type *base, uint32_t dataSize);
112
113
114 /*******************************************************************************
115 * Function Name: Cy_Crypto_Core_Trng_ReadData
116 ****************************************************************************//**
117 *
118 * Reads in blocking mode a generated random number.
119 *
120 * \note
121 * Call this API only after Cy_Crypto_Core_Trng_Start() is successful.
122 *
123 * \param base
124 * The pointer to the CRYPTO instance.
125 *
126 * \param randomData
127 * The pointer to a generated true random number. Must be 4-byte aligned.
128 *
129 * \return
130 * The error / status code. See \ref cy_en_crypto_status_t.
131 *
132 *******************************************************************************/
133 cy_en_crypto_status_t Cy_Crypto_Core_Trng_ReadData(CRYPTO_Type *base, uint32_t *randomData);
134
135
136 /*******************************************************************************
137 * Function Name: Cy_Crypto_Core_Trng
138 ****************************************************************************//**
139 *
140 * Generates a True Random Number.
141 *
142 * \param base
143 * The pointer to the CRYPTO instance.
144 *
145 * \param GAROPol
146 * The polynomial for the programmable Galois ring oscillator.
147 *
148 * \param FIROPol
149 * The polynomial for the programmable Fibonacci ring oscillator.
150 *
151 * \param max
152 * The maximum length of a random number, in the range of [0, 32] bits.
153 *
154 * \param randomNum
155 * The pointer to a generated true random number. Must be 4-byte aligned.
156 *
157 * \return
158 * \ref cy_en_crypto_status_t
159 *
160 *******************************************************************************/
161 cy_en_crypto_status_t Cy_Crypto_Core_Trng(CRYPTO_Type *base,
162 uint32_t GAROPol,
163 uint32_t FIROPol,
164 uint32_t max,
165 uint32_t *randomNum);
166
167
168 /*******************************************************************************
169 * Function Name: Cy_Crypto_Core_Trng_Ext
170 ****************************************************************************//**
171 *
172 * Generates a True Random Number.
173 *
174 * \param base
175 * The pointer to the CRYPTO instance.
176 *
177 * \param max
178 * The maximum length of a random number, in the range of [0, 32] bits.
179 *
180 * \param randomNum
181 * The pointer to a generated true random number. Must be 4-byte aligned.
182 *
183 * \return
184 * \ref cy_en_crypto_status_t
185 *
186 *******************************************************************************/
Cy_Crypto_Core_Trng_Ext(CRYPTO_Type * base,uint32_t max,uint32_t * randomNum)187 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Trng_Ext(CRYPTO_Type *base,
188 uint32_t max,
189 uint32_t *randomNum)
190 {
191 return Cy_Crypto_Core_Trng(base, CY_CRYPTO_DEF_TR_GARO, CY_CRYPTO_DEF_TR_FIRO, max, randomNum);
192 }
193
194 /** \} group_crypto_lld_rng_functions */
195
196
197
198 /** \cond INTERNAL */
199
200 /* Macros for conditions used by CY_ASSERT calls */
201 #define CY_CRYPTO_IS_SMPL_CLK_DIV_VALID(clkDiv) ((clkDiv) <= 255U)
202 #define CY_CRYPTO_IS_RED_CLK_DIV_VALID(clkDiv) ((clkDiv) <= 255U)
203 #define CY_CRYPTO_IS_INIT_DELAY_VALID(delay) ((delay) <= 255U)
204 #define CY_CRYPTO_IS_VONN_CORR_VALID(val) ((uint32_t)(val) <= 1U)
205 #define CY_CRYPTO_IS_AP_DETECT_VALID(val) ((uint32_t)(val) <= 1U)
206 #define CY_CRYPTO_IS_RC_DETECT_VALID(val) ((uint32_t)(val) <= 1U)
207
208 #define CY_CRYPTO_IS_RO11_EN_VALID(val) ((uint32_t)(val) <= 1U)
209 #define CY_CRYPTO_IS_RO15_EN_VALID(val) ((uint32_t)(val) <= 1U)
210 #define CY_CRYPTO_IS_GARO15_EN_VALID(val) ((uint32_t)(val) <= 1U)
211 #define CY_CRYPTO_IS_GARO31_EN_VALID(val) ((uint32_t)(val) <= 1U)
212 #define CY_CRYPTO_IS_FIRO15_EN_VALID(val) ((uint32_t)(val) <= 1U)
213 #define CY_CRYPTO_IS_FIRO31_EN_VALID(val) ((uint32_t)(val) <= 1U)
214
215 #define CY_CRYPTO_IS_GARO_POLY_VALID(val) ((val) <= 0x7FFFFFFFUL)
216 #define CY_CRYPTO_IS_FIRO_POLY_VALID(val) ((val) <= 0x7FFFFFFFUL)
217
218 #define CY_CRYPTO_IS_BS_SELECT_VALID(val) (((val) == CY_CRYPTO_TRMON_BS_DAS) || \
219 ((val) == CY_CRYPTO_TRMON_BS_RED) || \
220 ((val) == CY_CRYPTO_TRMON_BS_TR))
221
222 #define CY_CRYPTO_IS_CCOUNT8_VALID(val) ((val) <= 255U)
223 #define CY_CRYPTO_IS_CCOUNT16_VALID(val) ((val) <= 65535U)
224 #define CY_CRYPTO_IS_WINSIZE_VALID(val) ((val) <= 65535U)
225
226 #define CY_CRYPTO_TRNG_AP_DETECT_ERROR_BIT (0U)
227 #define CY_CRYPTO_TRNG_RC_DETECT_ERROR_BIT (1U)
228 #define CY_CRYPTO_TRNG_AP_DETECT_ERROR (1U << 0U)
229 #define CY_CRYPTO_TRNG_RC_DETECT_ERROR (1U << 1U)
230
231 #define CY_CRYPTO_TRNG_RO_ALL_MASK (uint32_t)(CRYPTO_TR_CTL1_RO11_EN_Msk | \
232 CRYPTO_TR_CTL1_RO15_EN_Msk | \
233 CRYPTO_TR_CTL1_GARO15_EN_Msk | CRYPTO_TR_CTL1_GARO31_EN_Msk | \
234 CRYPTO_TR_CTL1_FIRO15_EN_Msk | CRYPTO_TR_CTL1_FIRO31_EN_Msk)
235
236 extern const cy_stc_crypto_trng_config_t cy_trngDefaultConfig;
237
238 /** \endcond */
239
240 __STATIC_INLINE bool Cy_Crypto_Core_Trng_IsInitialized(CRYPTO_Type *base);
241
242 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_GetRoStatus(CRYPTO_Type *base, cy_en_crypto_trng_ro_sel_t roSelector);
243 __STATIC_INLINE void Cy_Crypto_Core_Trng_SetRoStatus(CRYPTO_Type *base,
244 cy_en_crypto_trng_ro_sel_t roSelector, uint8_t roStatus);
245 __STATIC_INLINE bool Cy_Crypto_Core_Trng_IsRoEnabled(CRYPTO_Type *base, cy_en_crypto_trng_ro_sel_t roSelector);
246 __STATIC_INLINE bool Cy_Crypto_Core_Trng_AnyRoEnabled(CRYPTO_Type *base);
247
248 __STATIC_INLINE uint32_t Cy_Crypto_Core_Trng_GetData(CRYPTO_Type *base);
249 __STATIC_INLINE void Cy_Crypto_Core_Trng_SetData(CRYPTO_Type *base, uint32_t randomData);
250
251 __STATIC_INLINE void Cy_Crypto_Core_Trng_SetGaroPoly(CRYPTO_Type *base, uint32_t poly);
252 __STATIC_INLINE void Cy_Crypto_Core_Trng_SetFiroPoly(CRYPTO_Type *base, uint32_t poly);
253 __STATIC_INLINE uint32_t Cy_Crypto_Core_Trng_GetGaroPoly(CRYPTO_Type *base);
254 __STATIC_INLINE uint32_t Cy_Crypto_Core_Trng_GetFiroPoly(CRYPTO_Type *base);
255
256 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetHealthStatus(CRYPTO_Type *base);
257
258 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetRcRepCount(CRYPTO_Type *base);
259 __STATIC_INLINE uint16_t Cy_Crypto_Core_Trng_MonGetApOccCount(CRYPTO_Type *base);
260 __STATIC_INLINE uint16_t Cy_Crypto_Core_Trng_MonGetApWindowIndex(CRYPTO_Type *base);
261
262 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetRcCurrentBit(CRYPTO_Type *base);
263 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetApCurrentBit(CRYPTO_Type *base);
264
265 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Trng_MonSetBSSelector(CRYPTO_Type *base, cy_en_crypto_trng_bs_sel_t bitStreamSelector);
266 __STATIC_INLINE cy_en_crypto_trng_bs_sel_t Cy_Crypto_Core_Trng_MonGetBSSelector(CRYPTO_Type *base);
267
268 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonEnableApTest(CRYPTO_Type *base);
269 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonDisableApTest(CRYPTO_Type *base);
270 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonEnableRcTest(CRYPTO_Type *base);
271 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonDisableRcTest(CRYPTO_Type *base);
272
273 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonSetRcCC8(CRYPTO_Type *base, uint8_t ccCount);
274 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetRcCC8(CRYPTO_Type *base);
275
276 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonSetApCC16(CRYPTO_Type *base, uint16_t ccCount);
277 __STATIC_INLINE uint16_t Cy_Crypto_Core_Trng_MonGetApCC16(CRYPTO_Type *base);
278
279 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonSetApWinSize(CRYPTO_Type *base, uint16_t windowSize);
280 __STATIC_INLINE uint16_t Cy_Crypto_Core_Trng_MonGetApWinSize(CRYPTO_Type *base);
281
282 __STATIC_INLINE void Cy_Crypto_Core_Trng_WaitForReady(CRYPTO_Type *base);
283 __STATIC_INLINE void Cy_Crypto_Core_Trng_WaitForComplete(CRYPTO_Type *base);
284
285 /*
286 * API Implementations
287 */
288
289 /*******************************************************************************
290 * Function Name: Cy_Crypto_Core_Trng_IsReady
291 ****************************************************************************//**
292 *
293 * Checks for TRNG hardware is ready.
294 *
295 * \param base
296 * The pointer to the CRYPTO instance.
297 *
298 * \return
299 * true when TRNG hardware is ready, otherwise false.
300 *
301 *******************************************************************************/
Cy_Crypto_Core_Trng_IsReady(CRYPTO_Type * base)302 __STATIC_INLINE bool Cy_Crypto_Core_Trng_IsReady(CRYPTO_Type *base)
303 {
304 bool status = false;
305
306 /* Check for TRNG instruction is complete */
307 if (CY_CRYPTO_V1)
308 {
309 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
310 status = (0uL == (REG_CRYPTO_STATUS(base) & CRYPTO_STATUS_TR_BUSY_Msk));
311 #endif
312 }
313 else
314 {
315 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
316 status = (0uL == _FLD2VAL(CRYPTO_V2_STATUS_BUSY, REG_CRYPTO_STATUS(base)));
317 #endif
318 }
319
320 return status;
321 }
322
323 /*******************************************************************************
324 * Function Name: Cy_Crypto_Core_Trng_IsRandomComplete
325 ****************************************************************************//**
326 *
327 * Checks for TRNG random number generation is complete.
328 *
329 * \param base
330 * The pointer to the CRYPTO instance.
331 *
332 * \return
333 * true when TRNG random number generation is complete, otherwise false.
334 *
335 *******************************************************************************/
Cy_Crypto_Core_Trng_IsRandomComplete(CRYPTO_Type * base)336 __STATIC_INLINE bool Cy_Crypto_Core_Trng_IsRandomComplete(CRYPTO_Type *base)
337 {
338 uint32_t status;
339
340 /* Check for TRNG instruction is complete */
341 status = Cy_Crypto_Core_GetInterruptStatus(base) & (CRYPTO_INTR_TR_DATA_AVAILABLE_Msk | CRYPTO_INTR_TR_AP_DETECT_ERROR_Msk | CRYPTO_INTR_TR_RC_DETECT_ERROR_Msk);
342
343 return (status != 0UL);
344 }
345
346 /*******************************************************************************
347 * Function Name: Cy_Crypto_Core_Trng_IsInitialized
348 ****************************************************************************//**
349 *
350 * Checks for TRNG hardware is initialized.
351 *
352 * \param base
353 * The pointer to the CRYPTO instance.
354 *
355 * \return
356 * true when TRNG hardware is initialized, otherwise false.
357 *
358 *******************************************************************************/
Cy_Crypto_Core_Trng_IsInitialized(CRYPTO_Type * base)359 __STATIC_INLINE bool Cy_Crypto_Core_Trng_IsInitialized(CRYPTO_Type *base)
360 {
361 return ((Cy_Crypto_Core_GetInterruptStatus(base) & CRYPTO_INTR_TR_INITIALIZED_Msk) != 0U);
362 }
363
364 /*******************************************************************************
365 * Function Name: Cy_Crypto_Core_Trng_WaitForComplete
366 ****************************************************************************//**
367 *
368 * Waits for TRNG random generation is complete.
369 *
370 * \param base
371 * The pointer to the CRYPTO instance.
372 *
373 * \note
374 * After this call the Health Monitor status should be checked to ensure that
375 * random data is healthy.
376 *
377 *******************************************************************************/
Cy_Crypto_Core_Trng_WaitForComplete(CRYPTO_Type * base)378 __STATIC_INLINE void Cy_Crypto_Core_Trng_WaitForComplete(CRYPTO_Type *base)
379 {
380 uint32_t status;
381 /* Wait until the TRNG random generation is complete */
382 do
383 {
384 status = Cy_Crypto_Core_GetInterruptStatus(base) & (CRYPTO_INTR_TR_DATA_AVAILABLE_Msk |
385 CRYPTO_INTR_TR_AP_DETECT_ERROR_Msk |
386 CRYPTO_INTR_TR_RC_DETECT_ERROR_Msk);
387 }
388 while (status == 0U);
389 }
390
391 /*******************************************************************************
392 * Function Name: Cy_Crypto_Core_Trng_WaitForReady
393 ****************************************************************************//**
394 *
395 * Waits for TRNG operation is ready.
396 *
397 * \param base
398 * The pointer to the CRYPTO instance.
399 *
400 *******************************************************************************/
Cy_Crypto_Core_Trng_WaitForReady(CRYPTO_Type * base)401 __STATIC_INLINE void Cy_Crypto_Core_Trng_WaitForReady(CRYPTO_Type *base)
402 {
403 /* Wait until the TRNG instruction is complete */
404 if (CY_CRYPTO_V1)
405 {
406 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
407 while (0uL != (REG_CRYPTO_STATUS(base) & CRYPTO_STATUS_TR_BUSY_Msk))
408 {
409 }
410 #endif
411 }
412 else
413 {
414 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
415 while (0uL != REG_CRYPTO_STATUS(base))
416 {
417 }
418 #endif
419 }
420 }
421
422 /*******************************************************************************
423 * Function Name: Cy_Crypto_Core_Trng_GetRoStatus
424 ****************************************************************************//**
425 *
426 * Reads the ring oscillator enable status.
427 *
428 * \param base
429 * The pointer to the CRYPTO instance.
430 *
431 * \param roSelector
432 * The ring oscillator selector (see \ref cy_en_crypto_trng_ro_sel_t).
433 *
434 * \return
435 * The ring oscillator enabled status (0 or 1).
436 *
437 *******************************************************************************/
Cy_Crypto_Core_Trng_GetRoStatus(CRYPTO_Type * base,cy_en_crypto_trng_ro_sel_t roSelector)438 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_GetRoStatus(CRYPTO_Type *base, cy_en_crypto_trng_ro_sel_t roSelector)
439 {
440 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to wider 32bit.');
441 return (REG_CRYPTO_TR_CTL1(base) & (uint32_t)(1U << ((uint32_t)roSelector))) != 0U ? 1U : 0U;
442 }
443
444 /*******************************************************************************
445 * Function Name: Cy_Crypto_Core_Trng_SetRoStatus
446 ****************************************************************************//**
447 *
448 * Sets the ring oscillator enable status.
449 *
450 * \param base
451 * The pointer to the CRYPTO instance.
452 *
453 * \param roSelector
454 * The ring oscillator selector (see \ref cy_en_crypto_trng_ro_sel_t).
455 *
456 * \param roStatus
457 * The new ring oscillator status value (0 or 1).
458 *
459 *******************************************************************************/
Cy_Crypto_Core_Trng_SetRoStatus(CRYPTO_Type * base,cy_en_crypto_trng_ro_sel_t roSelector,uint8_t roStatus)460 __STATIC_INLINE void Cy_Crypto_Core_Trng_SetRoStatus(CRYPTO_Type *base,
461 cy_en_crypto_trng_ro_sel_t roSelector, uint8_t roStatus)
462 {
463 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.6','enum cy_en_crypto_trng_ro_sel_t to uint32_t conversion intentional.');
464 uint32_t roMask = 1U << (uint32_t)roSelector;
465 uint32_t roData = (REG_CRYPTO_TR_CTL1(base) & ~roMask) | (((uint32_t)roStatus != 0U) ? roMask : 0U);
466 REG_CRYPTO_TR_CTL1(base) = roData;
467 }
468
469 /*******************************************************************************
470 * Function Name: Cy_Crypto_Core_Trng_IsRoEnabled
471 ****************************************************************************//**
472 *
473 * Checks if the specified ring oscillator is enabled.
474 *
475 * \param base
476 * The pointer to the CRYPTO instance.
477 *
478 * \param roSelector
479 * The ring oscillator selector (see \ref cy_en_crypto_trng_ro_sel_t).
480 *
481 * \return
482 * true - if the ring oscillator is enabled otherwise false.
483 *
484 *******************************************************************************/
Cy_Crypto_Core_Trng_IsRoEnabled(CRYPTO_Type * base,cy_en_crypto_trng_ro_sel_t roSelector)485 __STATIC_INLINE bool Cy_Crypto_Core_Trng_IsRoEnabled(CRYPTO_Type *base, cy_en_crypto_trng_ro_sel_t roSelector)
486 {
487 return (Cy_Crypto_Core_Trng_GetRoStatus(base, roSelector) != 0U);
488 }
489
490 /*******************************************************************************
491 * Function Name: Cy_Crypto_Core_Trng_AnyRoEnabled
492 ****************************************************************************//**
493 *
494 * Checks if any ring oscillator is enabled.
495 *
496 * \param base
497 * The pointer to the CRYPTO instance.
498 *
499 * \param roSelector
500 * The ring oscillator selector (see \ref cy_en_crypto_trng_ro_sel_t).
501 *
502 * \return
503 * true - if at least one ring oscillator is enabled otherwise false.
504 *
505 *******************************************************************************/
Cy_Crypto_Core_Trng_AnyRoEnabled(CRYPTO_Type * base)506 __STATIC_INLINE bool Cy_Crypto_Core_Trng_AnyRoEnabled(CRYPTO_Type *base)
507 {
508 return ((REG_CRYPTO_TR_CTL1(base) & CY_CRYPTO_TRNG_RO_ALL_MASK) != 0U);
509 }
510
511
512 /*******************************************************************************
513 * Function Name: Cy_Crypto_Core_Trng_GetData
514 ****************************************************************************//**
515 *
516 * Reads the generated random number in non-blocking mode.
517 *
518 * \param base
519 * The pointer to the CRYPTO instance.
520 *
521 * \return
522 * The generated random number.
523 *
524 *******************************************************************************/
Cy_Crypto_Core_Trng_GetData(CRYPTO_Type * base)525 __STATIC_INLINE uint32_t Cy_Crypto_Core_Trng_GetData(CRYPTO_Type *base)
526 {
527 return REG_CRYPTO_TR_RESULT(base);
528 }
529
530 /*******************************************************************************
531 * Function Name: Cy_Crypto_Core_Trng_SetData
532 ****************************************************************************//**
533 *
534 * Sets the random number value.
535 *
536 * \param base
537 * The pointer to the CRYPTO instance.
538 *
539 * \param randomData
540 * The new random number value.
541 *
542 *******************************************************************************/
Cy_Crypto_Core_Trng_SetData(CRYPTO_Type * base,uint32_t randomData)543 __STATIC_INLINE void Cy_Crypto_Core_Trng_SetData(CRYPTO_Type *base, uint32_t randomData)
544 {
545 REG_CRYPTO_TR_RESULT(base) = randomData;
546 }
547
548 /*******************************************************************************
549 * Function Name: Cy_Crypto_Core_Trng_SetGaroPoly
550 ****************************************************************************//**
551 *
552 * Sets the polynomial value for the Galois ring oscillator.
553 *
554 * \param base
555 * The pointer to the CRYPTO instance.
556 *
557 * \param poly
558 * The polynomial for the programmable Galois ring oscillator.
559 *
560 *******************************************************************************/
Cy_Crypto_Core_Trng_SetGaroPoly(CRYPTO_Type * base,uint32_t poly)561 __STATIC_INLINE void Cy_Crypto_Core_Trng_SetGaroPoly(CRYPTO_Type *base, uint32_t poly)
562 {
563 REG_CRYPTO_TR_GARO_CTL(base) = poly & CRYPTO_TR_GARO_CTL_POLYNOMIAL31_Msk;
564 }
565
566 /*******************************************************************************
567 * Function Name: Cy_Crypto_Core_Trng_SetFiroPoly
568 ****************************************************************************//**
569 *
570 * Sets the polynomial value for the Fibonacci ring oscillator.
571 *
572 * \param base
573 * The pointer to the CRYPTO instance.
574 *
575 * \param poly
576 * The polynomial for the programmable Fibonacci ring oscillator.
577 *
578 *******************************************************************************/
Cy_Crypto_Core_Trng_SetFiroPoly(CRYPTO_Type * base,uint32_t poly)579 __STATIC_INLINE void Cy_Crypto_Core_Trng_SetFiroPoly(CRYPTO_Type *base, uint32_t poly)
580 {
581 REG_CRYPTO_TR_FIRO_CTL(base) = poly & CRYPTO_TR_FIRO_CTL_POLYNOMIAL31_Msk;
582 }
583
584 /*******************************************************************************
585 * Function Name: Cy_Crypto_Core_Trng_GetGaroPoly
586 ****************************************************************************//**
587 *
588 * Reads the polynomial value of the Galois ring oscillator.
589 *
590 * \param base
591 * The pointer to the CRYPTO instance.
592 *
593 * \return
594 * The polynomial of the programmable Galois ring oscillator.
595 *
596 *******************************************************************************/
Cy_Crypto_Core_Trng_GetGaroPoly(CRYPTO_Type * base)597 __STATIC_INLINE uint32_t Cy_Crypto_Core_Trng_GetGaroPoly(CRYPTO_Type *base)
598 {
599 return REG_CRYPTO_TR_GARO_CTL(base);
600 }
601
602 /*******************************************************************************
603 * Function Name: Cy_Crypto_Core_Trng_GetFiroPoly
604 ****************************************************************************//**
605 *
606 * Reads the polynomial value of the Fibonacci ring oscillator.
607 *
608 * \param base
609 * The pointer to the CRYPTO instance.
610 *
611 * \return
612 * The polynomial of the programmable Fibonacci ring oscillator.
613 *
614 *******************************************************************************/
Cy_Crypto_Core_Trng_GetFiroPoly(CRYPTO_Type * base)615 __STATIC_INLINE uint32_t Cy_Crypto_Core_Trng_GetFiroPoly(CRYPTO_Type *base)
616 {
617 return REG_CRYPTO_TR_FIRO_CTL(base);
618 }
619
620 /*******************************************************************************
621 * Function Name: Cy_Crypto_Core_Trng_MonGetHealthStatus
622 ****************************************************************************//**
623 *
624 * Returns the health status of the random number health monitor in two bits.
625 *
626 * \param base
627 * The pointer to the CRYPTO instance.
628 *
629 * \return
630 * The health status:
631 * - ZERO - if data is healthy
632 * - bit 0 - Adaptive Proportion (AP) error is detected
633 * - bit 1 - Repetition Count (RC) error is detected
634 *
635 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetHealthStatus(CRYPTO_Type * base)636 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetHealthStatus(CRYPTO_Type *base)
637 {
638 return (uint8_t)((REG_CRYPTO_INTR(base) \
639 & (CRYPTO_INTR_TR_AP_DETECT_ERROR_Msk | CRYPTO_INTR_TR_RC_DETECT_ERROR_Msk)) \
640 >> CRYPTO_INTR_TR_AP_DETECT_ERROR_Pos);
641 }
642
643 /*******************************************************************************
644 * Function Name: Cy_Crypto_Core_Trng_MonClearHealthStatus
645 ****************************************************************************//**
646 *
647 * Clears the health status of the random number health monitor
648 * in the interrupts register
649 *
650 * \param base
651 * The pointer to the CRYPTO instance.
652 *
653 *******************************************************************************/
Cy_Crypto_Core_Trng_MonClearHealthStatus(CRYPTO_Type * base)654 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonClearHealthStatus(CRYPTO_Type *base)
655 {
656 Cy_Crypto_Core_ClearInterrupt(base, CRYPTO_INTR_TR_AP_DETECT_ERROR_Msk | CRYPTO_INTR_TR_RC_DETECT_ERROR_Msk);
657 }
658
659 /*******************************************************************************
660 * Function Name: Cy_Crypto_Core_Trng_MonGetRcRepCount
661 ****************************************************************************//**
662 *
663 * Reads the current number of repetitions from the repetition counter
664 * test monitor.
665 *
666 * \param base
667 * The pointer to the CRYPTO instance.
668 *
669 * \return
670 * The number of repetitions of the current active bit counter.
671 *
672 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetRcRepCount(CRYPTO_Type * base)673 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetRcRepCount(CRYPTO_Type *base)
674 {
675 return ((uint8_t)_FLD2VAL(CRYPTO_TR_MON_RC_STATUS1_REP_COUNT, REG_CRYPTO_TR_MON_RC_STATUS1(base)));
676 }
677
678 /*******************************************************************************
679 * Function Name: Cy_Crypto_Core_Trng_MonGetApOccCount
680 ****************************************************************************//**
681 *
682 * Reads the current Cutoff count number from the adaptive proportion
683 * test monitor.
684 *
685 * \param base
686 * The pointer to the CRYPTO instance.
687 *
688 * \return
689 * The Cutoff count number of the adaptive proportion test.
690 *
691 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetApOccCount(CRYPTO_Type * base)692 __STATIC_INLINE uint16_t Cy_Crypto_Core_Trng_MonGetApOccCount(CRYPTO_Type *base)
693 {
694 return ((uint16_t)_FLD2VAL(CRYPTO_TR_MON_AP_STATUS1_OCC_COUNT, REG_CRYPTO_TR_MON_AP_STATUS1(base)));
695 }
696
697 /*******************************************************************************
698 * Function Name: Cy_Crypto_Core_Trng_MonGetApWindowIndex
699 ****************************************************************************//**
700 *
701 * Reads the current window index from the adaptive proportion test monitor.
702 *
703 * \param base
704 * The pointer to the CRYPTO instance.
705 *
706 * \return
707 * The window index of the adaptive proportion test.
708 *
709 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetApWindowIndex(CRYPTO_Type * base)710 __STATIC_INLINE uint16_t Cy_Crypto_Core_Trng_MonGetApWindowIndex(CRYPTO_Type *base)
711 {
712 return ((uint16_t)_FLD2VAL(CRYPTO_TR_MON_AP_STATUS1_WINDOW_INDEX, REG_CRYPTO_TR_MON_AP_STATUS1(base)));
713 }
714
715 /*******************************************************************************
716 * Function Name: Cy_Crypto_Core_Trng_MonGetRcCurrentBit
717 ****************************************************************************//**
718 *
719 * Reads the active bit value from the adaptive proportion test monitor.
720 *
721 * \param base
722 * The pointer to the CRYPTO instance.
723 *
724 * \return
725 * The active bit value.
726 *
727 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetRcCurrentBit(CRYPTO_Type * base)728 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetRcCurrentBit(CRYPTO_Type *base)
729 {
730 return ((uint8_t)_FLD2VAL(CRYPTO_TR_MON_RC_STATUS0_BIT, REG_CRYPTO_TR_MON_RC_STATUS0(base)));
731 }
732
733 /*******************************************************************************
734 * Function Name: Cy_Crypto_Core_Trng_MonGetApCurrentBit
735 ****************************************************************************//**
736 *
737 * Reads the active bit value from the repetition counter test monitor.
738 *
739 * \param base
740 * The pointer to the CRYPTO instance.
741 *
742 * \return
743 * The active bit value.
744 *
745 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetApCurrentBit(CRYPTO_Type * base)746 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetApCurrentBit(CRYPTO_Type *base)
747 {
748 return ((uint8_t)_FLD2VAL(CRYPTO_TR_MON_AP_STATUS0_BIT, REG_CRYPTO_TR_MON_AP_STATUS0(base)));
749 }
750
751
752 /*******************************************************************************
753 * Function Name: Cy_Crypto_Core_Trng_MonSetBSSelector
754 ****************************************************************************//**
755 *
756 * Sets the BitStream selector value of the TRNG monitor.
757 *
758 * \param base
759 * The pointer to the CRYPTO instance.
760 *
761 * \param bitStreamSelector
762 * The BitStream selector value (see \ref cy_en_crypto_trng_bs_sel_t).
763 *
764 *******************************************************************************/
Cy_Crypto_Core_Trng_MonSetBSSelector(CRYPTO_Type * base,cy_en_crypto_trng_bs_sel_t bitStreamSelector)765 __STATIC_INLINE cy_en_crypto_status_t Cy_Crypto_Core_Trng_MonSetBSSelector(CRYPTO_Type *base,
766 cy_en_crypto_trng_bs_sel_t bitStreamSelector)
767 {
768 cy_en_crypto_status_t status = CY_CRYPTO_SUCCESS;
769
770 CY_ASSERT_L3(CY_CRYPTO_IS_BS_SELECT_VALID(bitStreamSelector));
771
772 CY_REG32_CLR_SET(REG_CRYPTO_TR_MON_CTL(base), CRYPTO_TR_MON_CTL_BITSTREAM_SEL, bitStreamSelector);
773
774 return status;
775 }
776
777 /*******************************************************************************
778 * Function Name: Cy_Crypto_Core_Trng_MonGetBSSelector
779 ****************************************************************************//**
780 *
781 * Reads the BitStream selector value of the TRNG monitor.
782 *
783 * \param base
784 * The pointer to the CRYPTO instance.
785 *
786 * \return
787 * The BitStream selector value (see \ref cy_en_crypto_trng_bs_sel_t).
788 *
789 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetBSSelector(CRYPTO_Type * base)790 __STATIC_INLINE cy_en_crypto_trng_bs_sel_t Cy_Crypto_Core_Trng_MonGetBSSelector(CRYPTO_Type *base)
791 {
792 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to cy_en_crypto_trng_bs_sel_t enum.');
793 return ((cy_en_crypto_trng_bs_sel_t)_FLD2VAL(CRYPTO_TR_MON_CTL_BITSTREAM_SEL, REG_CRYPTO_TR_MON_CTL(base)));
794 }
795
796 /*******************************************************************************
797 * Function Name: Cy_Crypto_Core_Trng_MonEnableApTest
798 ****************************************************************************//**
799 *
800 * Enables the Adaptive proportion (AP) test.
801 *
802 * \param base
803 * The pointer to the CRYPTO instance.
804 *
805 *******************************************************************************/
Cy_Crypto_Core_Trng_MonEnableApTest(CRYPTO_Type * base)806 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonEnableApTest(CRYPTO_Type *base)
807 {
808 CY_REG32_CLR_SET(REG_CRYPTO_TR_MON_CMD(base), CRYPTO_TR_MON_CMD_START_AP, 1U);
809 }
810
811 /*******************************************************************************
812 * Function Name: Cy_Crypto_Core_Trng_MonDisableApTest
813 ****************************************************************************//**
814 *
815 * Disables the Adaptive proportion (AP) test.
816 *
817 * \param base
818 * The pointer to the CRYPTO instance.
819 *
820 *******************************************************************************/
Cy_Crypto_Core_Trng_MonDisableApTest(CRYPTO_Type * base)821 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonDisableApTest(CRYPTO_Type *base)
822 {
823 CY_REG32_CLR_SET(REG_CRYPTO_TR_MON_CMD(base), CRYPTO_TR_MON_CMD_START_AP, 0U);
824 }
825
826 /*******************************************************************************
827 * Function Name: Cy_Crypto_Core_Trng_MonEnableRcTest
828 ****************************************************************************//**
829 *
830 * Enables the Repetition count (RC) test.
831 *
832 * \param base
833 * The pointer to the CRYPTO instance.
834 *
835 *******************************************************************************/
Cy_Crypto_Core_Trng_MonEnableRcTest(CRYPTO_Type * base)836 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonEnableRcTest(CRYPTO_Type *base)
837 {
838 CY_REG32_CLR_SET(REG_CRYPTO_TR_MON_CMD(base), CRYPTO_TR_MON_CMD_START_RC, 1U);
839 }
840
841 /*******************************************************************************
842 * Function Name: Cy_Crypto_Core_Trng_MonDisableRcTest
843 ****************************************************************************//**
844 *
845 * Disables the Repetition count (RC) test.
846 *
847 * \param base
848 * The pointer to the CRYPTO instance.
849 *
850 *******************************************************************************/
Cy_Crypto_Core_Trng_MonDisableRcTest(CRYPTO_Type * base)851 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonDisableRcTest(CRYPTO_Type *base)
852 {
853 CY_REG32_CLR_SET(REG_CRYPTO_TR_MON_CMD(base), CRYPTO_TR_MON_CMD_START_RC, 0U);
854 }
855
856 /*******************************************************************************
857 * Function Name: Cy_Crypto_Core_Trng_MonSetRcCC8
858 ****************************************************************************//**
859 *
860 * Sets the Cutoff counter value of the repetition counter test monitor.
861 *
862 * \param base
863 * The pointer to the CRYPTO instance.
864 *
865 * \param ccCount
866 * The new Cutoff counter value.
867 *
868 *******************************************************************************/
Cy_Crypto_Core_Trng_MonSetRcCC8(CRYPTO_Type * base,uint8_t ccCount)869 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonSetRcCC8(CRYPTO_Type *base, uint8_t ccCount)
870 {
871 REG_CRYPTO_TR_MON_RC_CTL(base) = (uint32_t)_VAL2FLD(CRYPTO_TR_MON_RC_CTL_CUTOFF_COUNT8, ccCount);
872 }
873
874 /*******************************************************************************
875 * Function Name: Cy_Crypto_Core_Trng_MonGetRcCC8
876 ****************************************************************************//**
877 *
878 * Reads the Cutoff counter value of the repetition counter test monitor.
879 *
880 * \param base
881 * The pointer to the CRYPTO instance.
882 *
883 * \return
884 * The Cutoff counter value.
885 *
886 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetRcCC8(CRYPTO_Type * base)887 __STATIC_INLINE uint8_t Cy_Crypto_Core_Trng_MonGetRcCC8(CRYPTO_Type *base)
888 {
889 return ((uint8_t)_FLD2VAL(CRYPTO_TR_MON_RC_CTL_CUTOFF_COUNT8, REG_CRYPTO_TR_MON_RC_CTL(base)));
890 }
891
892 /*******************************************************************************
893 * Function Name: Cy_Crypto_Core_Trng_MonSetApCC16
894 ****************************************************************************//**
895 *
896 * Sets the Cutoff counter value of the adaptive proportion test monitor.
897 *
898 * \param base
899 * The pointer to the CRYPTO instance.
900 *
901 * \param ccCount
902 * The new Cutoff counter value.
903 *
904 *******************************************************************************/
Cy_Crypto_Core_Trng_MonSetApCC16(CRYPTO_Type * base,uint16_t ccCount)905 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonSetApCC16(CRYPTO_Type *base, uint16_t ccCount)
906 {
907 CY_REG32_CLR_SET(REG_CRYPTO_TR_MON_AP_CTL(base), CRYPTO_TR_MON_AP_CTL_CUTOFF_COUNT16, ccCount);
908 }
909
910 /*******************************************************************************
911 * Function Name: Cy_Crypto_Core_Trng_MonGetApCC16
912 ****************************************************************************//**
913 *
914 * Reads the Cutoff counter value of the adaptive proportion test monitor.
915 *
916 * \param base
917 * The pointer to the CRYPTO instance.
918 *
919 * \return
920 * The Cutoff counter value.
921 *
922 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetApCC16(CRYPTO_Type * base)923 __STATIC_INLINE uint16_t Cy_Crypto_Core_Trng_MonGetApCC16(CRYPTO_Type *base)
924 {
925 return ((uint16_t)_FLD2VAL(CRYPTO_TR_MON_AP_CTL_CUTOFF_COUNT16, REG_CRYPTO_TR_MON_AP_CTL(base)));
926 }
927
928 /*******************************************************************************
929 * Function Name: Cy_Crypto_Core_Trng_MonSetApWinSize
930 ****************************************************************************//**
931 *
932 * Sets the window size value of the adaptive proportion test monitor.
933 *
934 * \param base
935 * The pointer to the CRYPTO instance.
936 *
937 * \param windowSize
938 * The new windows size value.
939 *
940 *******************************************************************************/
Cy_Crypto_Core_Trng_MonSetApWinSize(CRYPTO_Type * base,uint16_t windowSize)941 __STATIC_INLINE void Cy_Crypto_Core_Trng_MonSetApWinSize(CRYPTO_Type *base, uint16_t windowSize)
942 {
943 CY_REG32_CLR_SET(REG_CRYPTO_TR_MON_AP_CTL(base), CRYPTO_TR_MON_AP_CTL_WINDOW_SIZE, windowSize);
944 }
945
946 /*******************************************************************************
947 * Function Name: Cy_Crypto_Core_Trng_MonGetApWinSize
948 ****************************************************************************//**
949 *
950 * Reads the window size value of the adaptive proportion test monitor.
951 *
952 * \param base
953 * The pointer to the CRYPTO instance.
954 *
955 * \return
956 * The windows size value.
957 *
958 *******************************************************************************/
Cy_Crypto_Core_Trng_MonGetApWinSize(CRYPTO_Type * base)959 __STATIC_INLINE uint16_t Cy_Crypto_Core_Trng_MonGetApWinSize(CRYPTO_Type *base)
960 {
961 return ((uint16_t)_FLD2VAL(CRYPTO_TR_MON_AP_CTL_WINDOW_SIZE, REG_CRYPTO_TR_MON_AP_CTL(base)));
962 }
963
964
965
966
967
968 #endif /* #if (CPUSS_CRYPTO_TR == 1) && defined(CY_CRYPTO_CFG_TRNG_C) */
969
970 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 14.3')
971
972 #if defined(__cplusplus)
973 }
974 #endif
975
976 #endif /* CY_IP_MXCRYPTO */
977
978 #endif /* #if !defined (CY_CRYPTO_CORE_TRNG_H) */
979
980
981 /* [] END OF FILE */
982