1 /***************************************************************************//**
2 * \file cy_crypto_core_prng_v2.c
3 * \version 2.120
4 *
5 * \brief
6 * This file provides the source code to the API for the PRNG
7 * 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 #include "cy_device.h"
29
30 #if defined(CY_IP_MXCRYPTO)
31
32 #include "cy_crypto_core_prng_v2.h"
33
34 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
35
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif
39
40 #if (CPUSS_CRYPTO_PR == 1) && defined(CY_CRYPTO_CFG_PRNG_C)
41
42 #include "cy_crypto_core_hw_v2.h"
43 #include "cy_syslib.h"
44
45 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 11.3', 2, \
46 'CRYPTO_Type will typecast to either CRYPTO_V1_Type or CRYPTO_V2_Type but not both on PDL initialization based on the target device at compile time.')
47
48 /*******************************************************************************
49 * Function Name: Cy_Crypto_Core_V2_Prng_Init
50 ****************************************************************************//**
51 *
52 * Initializes the PRND parameters.
53 * Invoking this function causes a restart of the pseudo-random sequence.
54 *
55 * \param base
56 * The pointer to the CRYPTO instance.
57 *
58 * \param lfsr32InitState
59 * A non-zero seed value for the first LFSR.
60
61 * \param lfsr31InitState
62 * A non-zero seed value for the second LFSR.
63
64 * \param lfsr29InitState
65 * A non-zero seed value for the third LFSR.
66 *
67 * \return
68 * \ref cy_en_crypto_status_t
69 *
70 *******************************************************************************/
Cy_Crypto_Core_V2_Prng_Init(CRYPTO_Type * base,uint32_t lfsr32InitState,uint32_t lfsr31InitState,uint32_t lfsr29InitState)71 cy_en_crypto_status_t Cy_Crypto_Core_V2_Prng_Init(CRYPTO_Type *base,
72 uint32_t lfsr32InitState,
73 uint32_t lfsr31InitState,
74 uint32_t lfsr29InitState)
75 {
76 REG_CRYPTO_PR_LFSR_CTL0(base) = (uint32_t)(_VAL2FLD(CRYPTO_PR_LFSR_CTL0_LFSR32, lfsr32InitState));
77
78 REG_CRYPTO_PR_LFSR_CTL1(base) = (uint32_t)(_VAL2FLD(CRYPTO_PR_LFSR_CTL1_LFSR31, lfsr31InitState));
79
80 REG_CRYPTO_PR_LFSR_CTL2(base) = (uint32_t)(_VAL2FLD(CRYPTO_PR_LFSR_CTL2_LFSR29, lfsr29InitState));
81
82 return (CY_CRYPTO_SUCCESS);
83 }
84
85 /*******************************************************************************
86 * Function Name: Cy_Crypto_Core_V2_Prng
87 ****************************************************************************//**
88 *
89 * Generates a Pseudo Random Number.
90 *
91 * \param base
92 * The pointer to the CRYPTO instance.
93 *
94 * \param max
95 * The maximum value of a random number.
96 *
97 * \param randomNum
98 * The pointer to a variable to store the generated pseudo random number.
99 *
100 * \return
101 * \ref cy_en_crypto_status_t
102 *
103 *******************************************************************************/
Cy_Crypto_Core_V2_Prng(CRYPTO_Type * base,uint32_t max,uint32_t * randomNum)104 cy_en_crypto_status_t Cy_Crypto_Core_V2_Prng(CRYPTO_Type *base,
105 uint32_t max,
106 uint32_t *randomNum)
107 {
108 REG_CRYPTO_PR_MAX_CTL(base) = max;
109
110 REG_CRYPTO_PR_CMD(base) = _VAL2FLD(CRYPTO_V2_PR_CMD_START, 1u);
111
112 /* Wait until the PRNG instruction is complete */
113 while(0uL != _FLD2VAL(CRYPTO_V2_STATUS_BUSY, REG_CRYPTO_STATUS(base)))
114 {
115 }
116
117 *randomNum = (uint32_t)_FLD2VAL(CRYPTO_V2_PR_RESULT_DATA32, REG_CRYPTO_PR_RESULT(base));
118
119 return (CY_CRYPTO_SUCCESS);
120 }
121
122 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 11.3')
123
124 #endif /* (CPUSS_CRYPTO_PR == 1) && defined(CY_CRYPTO_CFG_PRNG_C) */
125
126 #if defined(__cplusplus)
127 }
128 #endif
129
130 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
131
132 #endif /* defined(CY_IP_MXCRYPTO) */
133
134
135 /* [] END OF FILE */
136