• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: mbedtls harden adapt internal source file.
15  *
16  * Create: 2023-05-10
17 */
18 
19 #include "common.h"
20 
21 #if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
22 #include "mbedtls/error.h"
23 #include "mbedtls/entropy.h"
24 #include "entropy_poll.h"
25 #include "cipher_adapt.h"
26 #include "dfx.h"
27 
mbedtls_hardware_poll(void * data,unsigned char * output,size_t len,size_t * olen)28 int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
29 {
30     int ret;
31 
32     mbedtls_harden_log_func_enter();
33 
34     if( IS_TRNG_FUNC_REGISTERED() != TD_TRUE )
35 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
36         return mbedtls_platform_entropy_poll( data, output, len, olen );
37 #else
38         return ( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
39 #endif
40 
41     if( len == 0 || olen == NULL || output == NULL )
42         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
43 
44     (void) data;
45 
46     ret = CIPHER_TRNG_GET_MULTI_RANDOM( len, output );
47     if( ret != MBEDTLS_HARDEN_SUCCESS )
48     {
49         mbedtls_harden_log_err("CIPHER_TRNG_GET_MULTI_RANDOM failed, ret = 0x%x!\n", ret );
50         return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
51     }
52     *olen = len;
53 
54     mbedtls_harden_log_func_exit();
55 
56     return( 0 );
57 }
58 
59 #endif /* MBEDTLS_ENTROPY_C && MBEDTLS_ENTROPY_HARDWARE_ALT */