1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18
19 #include "common.h"
20
21 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
22
23 #include "multithread.h"
24 #include "trng.h"
25 #include <string.h>
26
mbedtls_hardware_poll(void * data,unsigned char * output,size_t len,size_t * olen)27 int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen)
28 {
29 (void)data;
30
31 if (output != NULL && len != 0 && olen != NULL) {
32 mbedtls_entropy_lock();
33 *olen = 0;
34 extern unsigned int g_rnd_m_w;
35 while (len / sizeof(g_rnd_m_w) != 0) {
36 trng_init();
37 *((unsigned int *)output) = g_rnd_m_w;
38 output += sizeof(g_rnd_m_w);
39 len -= sizeof(g_rnd_m_w);
40 *olen += sizeof(g_rnd_m_w);
41 }
42 if (len != 0) {
43 trng_init();
44 memcpy(output, &g_rnd_m_w, len);
45 *olen += len;
46 }
47 mbedtls_entropy_unlock();
48 }
49 return 0;
50 }
51
52 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
53