1 /*
2  * Copyright (c) 2001-2022, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "llf_rnd_trng.h"
8 #include "cc_rng_plat.h"
9 #include "cc_rnd_common.h"
10 #include "cc_pal_log.h"
11 #include "mbedtls_common.h"
12 #include "cc_pal_mem.h"
13 
14 #include "mbedtls/build_info.h"
15 
16 #if defined(MBEDTLS_PLATFORM_C)
17 #include "mbedtls/platform.h"
18 #else
19 #include <stdio.h>
20 #define mbedtls_fprintf fprintf
21 #define mbedtls_calloc calloc
22 #define mbedtls_free   free
23 #endif
24 
25 #ifdef DEBUG
26 #define GOTO_END(ERR) \
27     do { \
28         Error = ERR; \
29         mbedtls_fprintf(stderr, "%s:%d - %s: %d (0x%08x)\n", __FILE__, __LINE__, __func__, (int)Error, (int)Error); \
30         goto End; \
31     } while (0)
32 
33 #define GOTO_CLEANUP(ERR) \
34     do { \
35         Error = ERR; \
36         mbedtls_fprintf(stderr, "%s:%d - %s: %d (0x%08x)\n", __FILE__, __LINE__, __func__, (int)Error, (int)Error); \
37         goto Cleanup; \
38     } while (0)
39 #else // DEBUG
40 #define GOTO_END(ERR) \
41     do { \
42         Error = ERR; \
43         goto End; \
44     } while (0)
45 
46 #define GOTO_CLEANUP(ERR) \
47     do { \
48         Error = ERR; \
49         goto Cleanup; \
50     } while (0)
51 #endif // DEBUG
52 
53 
mbedtls_hardware_poll(void * data,unsigned char * output,size_t len,size_t * olen)54 int mbedtls_hardware_poll( void *data,
55                            unsigned char *output, size_t len, size_t *olen )
56 {
57     CCRndWorkBuff_t  *rndWorkBuff_ptr;
58     CCRndState_t rndState;
59     CCRndParams_t trngParams;
60     int ret, Error = 0;
61     uint32_t  *entrSource_ptr;
62 
63     CC_UNUSED_PARAM(data);
64 
65     if ( NULL == output )
66     {
67         CC_PAL_LOG_ERR( "output cannot be NULL\n" );
68         GOTO_END( -1 );
69     }
70     if ( NULL == olen )
71     {
72         CC_PAL_LOG_ERR( "olen cannot be NULL\n" );
73         GOTO_END( -1 );
74 
75     }
76     if ( 0 == len )
77     {
78         CC_PAL_LOG_ERR( "len cannot be zero\n" );
79         GOTO_END( -1 );
80     }
81 
82     rndWorkBuff_ptr = ( CCRndWorkBuff_t * )mbedtls_calloc( 1, sizeof ( CCRndWorkBuff_t ) );
83     if ( NULL == rndWorkBuff_ptr )
84     {
85         CC_PAL_LOG_ERR( "Error: cannot allocate memory for rndWorkbuff\n" );
86         GOTO_END ( -1 );
87     }
88     CC_PalMemSetZero( &rndState, sizeof( CCRndState_t ) );
89     CC_PalMemSetZero( &trngParams, sizeof( CCRndParams_t ) );
90 
91     ret = RNG_PLAT_SetUserRngParameters(  &trngParams );
92     if ( ret != 0 )
93     {
94         CC_PAL_LOG_ERR( "Error: RNG_PLAT_SetUserRngParameters() failed.\n" );
95         GOTO_CLEANUP( -1 );
96     }
97 
98     ret = LLF_RND_GetTrngSource(
99                 &rndState ,    /*in/out*/
100                 &trngParams,       /*in/out*/
101                 0,                 /*in  -  isContinued - false*/
102                 (uint32_t*)&len,  /*in/out*/
103                 &entrSource_ptr,   /*out*/
104                 (uint32_t*)olen,               /*out*/
105                 (uint32_t*)rndWorkBuff_ptr,   /*in*/
106                 0                  /*in - isFipsSupport false*/ );
107     if ( ret != 0 )
108     {
109         CC_PAL_LOG_ERR( "Error: LLF_RND_GetTrngSource() failed.\n" );
110         GOTO_CLEANUP( -1 );
111     }
112 
113     if (*olen <= len ){
114         CC_PalMemCopy ( output, entrSource_ptr + CC_RND_TRNG_SRC_INNER_OFFSET_WORDS , *olen );
115     } else{
116         CC_PAL_LOG_ERR( "buffer length is smaller than LLF_RND_GetTrngSource output length\n" );
117         GOTO_CLEANUP( -1 );
118     }
119 
120 Cleanup:
121     mbedtls_zeroize_internal( rndWorkBuff_ptr, sizeof( CCRndWorkBuff_t ) );
122     mbedtls_free( rndWorkBuff_ptr );
123     mbedtls_zeroize_internal( &rndState, sizeof( CCRndState_t ) );
124 End:
125     return Error;
126 }
127