1 /***************************************************************************//**
2 * \file cyhal_trng_impl.h
3 *
4 * \brief
5 * Provides an implementation of the ModusToolbox™ TRNG HAL API.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2018-2022 Cypress Semiconductor Corporation (an Infineon company) or
10 * an affiliate of Cypress Semiconductor Corporation
11 *
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *******************************************************************************/
26
27 #pragma once
28
29 #include "cyhal_trng.h"
30
31 #if (CYHAL_DRIVER_AVAILABLE_TRNG)
32
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif /* __cplusplus */
36
37 /**
38 * \addtogroup group_hal_impl_trng TRNG (True Random Number Generator)
39 * \ingroup group_hal_impl
40 * \{
41 * Initialization polynomial values for the True Random Number Generator.
42 */
43
44 /** Galois ring oscillator value */
45 #define CYHAL_GARO31_INITSTATE (0x04c11db7UL)
46 /** Fibonacci ring oscillator value */
47 #define CYHAL_FIRO31_INITSTATE (0x04c11db7UL)
48
49 /** \} group_hal_impl_trng */
50
51 #define MAX_TRNG_BIT_SIZE (32UL)
52
53 // This helper function mirrors the definition of cyhal_trng_generate
_cyhal_trng_generate_internal(const cyhal_trng_t * obj)54 static inline uint32_t _cyhal_trng_generate_internal(const cyhal_trng_t *obj)
55 {
56 CY_ASSERT(NULL != obj);
57 uint32_t value;
58
59 #if defined(CY_IP_MXCRYPTO)
60 cy_en_crypto_status_t status = Cy_Crypto_Core_Trng(
61 obj->base, CYHAL_GARO31_INITSTATE, CYHAL_FIRO31_INITSTATE, MAX_TRNG_BIT_SIZE, &value);
62 #elif defined(CY_IP_M0S8CRYPTO) || defined(CY_IP_M0S8CRYPTOLITE)
63 cy_en_crypto_status_t status = Cy_Crypto_Trng(
64 obj->base, MAX_TRNG_BIT_SIZE, &value);
65 #endif
66
67 (void)status;
68 CY_ASSERT(CY_CRYPTO_SUCCESS == status);
69 return value;
70 }
71
72 #define cyhal_trng_generate(obj) _cyhal_trng_generate_internal(obj)
73
74
75 #if defined(__cplusplus)
76 }
77 #endif /* __cplusplus */
78
79 #endif /* CYHAL_DRIVER_AVAILABLE_TRNG */
80