1 /***************************************************************************//**
2 * \file cy_crypto_core_prng_v1.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_v1.h"
33 
34 #if defined(CY_CRYPTO_CFG_HW_V1_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_v1.h"
43 #include "cy_syslib.h"
44 
45 /*******************************************************************************
46 * Function Name: Cy_Crypto_Core_V1_Prng_Init
47 ****************************************************************************//**
48 *
49 * Initializes the PRND parameters.
50 * Invoking this function causes a restart of the pseudo-random sequence.
51 *
52 * \param base
53 * The pointer to the CRYPTO instance.
54 *
55 * \param lfsr32InitState
56 * A non-zero seed value for the first LFSR.
57 
58 * \param lfsr31InitState
59 * A non-zero seed value for the second LFSR.
60 
61 * \param lfsr29InitState
62 * A non-zero seed value for the third LFSR.
63 *
64 * \return
65 * \ref cy_en_crypto_status_t
66 *
67 *******************************************************************************/
Cy_Crypto_Core_V1_Prng_Init(CRYPTO_Type * base,uint32_t lfsr32InitState,uint32_t lfsr31InitState,uint32_t lfsr29InitState)68 cy_en_crypto_status_t Cy_Crypto_Core_V1_Prng_Init(CRYPTO_Type *base,
69                                                   uint32_t lfsr32InitState,
70                                                   uint32_t lfsr31InitState,
71                                                   uint32_t lfsr29InitState)
72 {
73     REG_CRYPTO_PR_LFSR_CTL0(base) = (uint32_t)(_VAL2FLD(CRYPTO_PR_LFSR_CTL0_LFSR32, lfsr32InitState));
74 
75     REG_CRYPTO_PR_LFSR_CTL1(base) = (uint32_t)(_VAL2FLD(CRYPTO_PR_LFSR_CTL1_LFSR31, lfsr31InitState));
76 
77     REG_CRYPTO_PR_LFSR_CTL2(base) = (uint32_t)(_VAL2FLD(CRYPTO_PR_LFSR_CTL2_LFSR29, lfsr29InitState));
78 
79     return (CY_CRYPTO_SUCCESS);
80 }
81 
82 /*******************************************************************************
83 * Function Name: Cy_Crypto_Core_V1_Prng
84 ****************************************************************************//**
85 *
86 * Generates a Pseudo Random Number.
87 *
88 * \param base
89 * The pointer to the CRYPTO instance.
90 *
91 * \param max
92 * The maximum value of a random number.
93 *
94 * \param randomNum
95 * The pointer to a variable to store the generated pseudo random number.
96 *
97 * \return
98 * \ref cy_en_crypto_status_t
99 *
100 *******************************************************************************/
Cy_Crypto_Core_V1_Prng(CRYPTO_Type * base,uint32_t max,uint32_t * randomNum)101 cy_en_crypto_status_t Cy_Crypto_Core_V1_Prng(CRYPTO_Type *base,
102                                              uint32_t max,
103                                              uint32_t *randomNum)
104 {
105     Cy_Crypto_SetReg1Instr(base, max);
106 
107     Cy_Crypto_Run1ParamInstr(base,
108                              CY_CRYPTO_V1_PRNG_OPC,
109                              CY_CRYPTO_RSRC0_SHIFT);
110 
111     /* Wait until the PRNG instruction is complete */
112     while(0uL != _FLD2VAL(CRYPTO_STATUS_PR_BUSY, REG_CRYPTO_STATUS(base)))
113     {
114     }
115 
116     *randomNum = (uint32_t)_FLD2VAL(CRYPTO_PR_RESULT_DATA32, REG_CRYPTO_PR_RESULT(base));
117 
118     return (CY_CRYPTO_SUCCESS);
119 }
120 
121 #endif /* (CPUSS_CRYPTO_PR == 1) && defined(CY_CRYPTO_CFG_PRNG_C) */
122 
123 #if defined(__cplusplus)
124 }
125 #endif
126 
127 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
128 
129 #endif /* defined(CY_IP_MXCRYPTO) */
130 
131 
132 /* [] END OF FILE */
133