1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 
9 /************* Include Files ****************/
10 #include "cc_pal_types.h"
11 #include "cc_pal_trng.h"
12 #include "cc_pal_log.h"
13 
14 #if (CC_CONFIG_TRNG_MODE==0)
15 #define CC_CONFIG_SAMPLE_CNT_ROSC_1     5000
16 #define CC_CONFIG_SAMPLE_CNT_ROSC_2     1000
17 #define CC_CONFIG_SAMPLE_CNT_ROSC_3     500
18 #define CC_CONFIG_SAMPLE_CNT_ROSC_4     0
19 #elif (CC_CONFIG_TRNG_MODE==1)
20 /* amount of bytes for the required entropy bits = ROUND_UP(ROUND_UP(((required entropy bits)/(entropy per bit)), 1024), (EHR width in bytes)) / 8
21    (multiple of the window size 1024 bits and multiple of the EHR width 192 bits) */
22 #define CC_CONFIG_TRNG90B_AMOUNT_OF_BYTES                      144  /* ROUND_UP(ROUND_UP((384/0.5), 1024), 192) / 8 = 144 */
23 
24 /*** NIST SP 800-90B (2nd Draft) 4.4.1 ***/
25 /* C = ROUND_UP(1+(-log(W)/H)), W = 2^(-40), H=(entropy per bit) */
26 #define CC_CONFIG_TRNG90B_REPETITION_COUNTER_CUTOFF            81  /* ROUND_UP(1+(40/0.5)) = 81 */
27 
28 /*** NIST SP 800-90B (2nd Draft) 4.4.2 ***/
29 /* C =CRITBINOM(W, power(2,(-H)),1-a), W = 1024, a = 2^(-40), H=(entropy per bit) */
30 #define CC_CONFIG_TRNG90B_ADAPTIVE_PROPORTION_CUTOFF           823      /* =CRITBINOM(1024, power(2,(-0.5)),1-2^(-40)) */
31 
32 /* sample count for each ring oscillator */
33 /* for unallowed rosc, sample count = 0 */
34 #define CC_CONFIG_SAMPLE_CNT_ROSC_1     1000
35 #define CC_CONFIG_SAMPLE_CNT_ROSC_2     1000
36 #define CC_CONFIG_SAMPLE_CNT_ROSC_3     500
37 #define CC_CONFIG_SAMPLE_CNT_ROSC_4     0
38 #else
39 #error "CC_CONFIG_TRNG_MODE not defined or not supported"
40 #endif
41 
42 
43 /**
44  * @brief This function return the TRNG user parameters.
45  *
46  *
47  * @return Zero on success.
48  * @return A non-zero value on failure.
49  */
CC_PalTrngParamGet(CC_PalTrngParams_t * pTrngParams,size_t * pParamsSize)50 CCError_t CC_PalTrngParamGet(CC_PalTrngParams_t *pTrngParams, /*!< [out] A pointer to the TRNG user parameters. */
51                              size_t *pParamsSize)     /*!< [in/out] A poiinter to size of the TRNG user parameters structure used.
52                                                            As input: the function needs to verify its size is the same as CC_PalTrngParams_t.
53                                                            As output: return the size of CC_PalTrngParams_t for Library size verification */
54 {
55     CCError_t  error = CC_OK;
56 
57     if ((pTrngParams == NULL) ||
58              (pParamsSize == NULL) ||
59              (*pParamsSize != sizeof(CC_PalTrngParams_t))){
60             return CC_FAIL;
61     }
62 
63     *pParamsSize = sizeof(CC_PalTrngParams_t);
64 
65     pTrngParams->SubSamplingRatio1 = CC_CONFIG_SAMPLE_CNT_ROSC_1;
66     pTrngParams->SubSamplingRatio2 = CC_CONFIG_SAMPLE_CNT_ROSC_2;
67     pTrngParams->SubSamplingRatio3 = CC_CONFIG_SAMPLE_CNT_ROSC_3;
68     pTrngParams->SubSamplingRatio4 = CC_CONFIG_SAMPLE_CNT_ROSC_4;
69 
70 #if (CC_CONFIG_TRNG_MODE==1)
71     pTrngParams->trngModeParams.numOfBytes = CC_CONFIG_TRNG90B_AMOUNT_OF_BYTES;
72     pTrngParams->trngModeParams.repetitionCounterCutoff = CC_CONFIG_TRNG90B_REPETITION_COUNTER_CUTOFF;
73     pTrngParams->trngModeParams.adaptiveProportionCutOff = CC_CONFIG_TRNG90B_ADAPTIVE_PROPORTION_CUTOFF;
74 
75 #endif
76     return error;
77 }
78