1 /*
2 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
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 */
15
16 #include "mbedtls/entropy_poll.h"
17
18 #ifdef MBEDTLS_ENTROPY_HARDWARE_ALT
19
20 #include "lega_rnd.h"
21 #include "lega_hw_common.h"
22
mbedtls_hardware_poll(void * data,unsigned char * output,size_t len,size_t * olen)23 int mbedtls_hardware_poll(void *data,
24 unsigned char *output, size_t len, size_t *olen)
25 {
26 int ret = 0;
27 lega_rnd_init();
28 ret = lega_RND_AddAdditionalInput(rndContext_ptr, "0123", 4);
29 if (ret != LEGA_HW_OK) {
30 printf("\n lega_RND_AddAdditionalInput failed with 0x%x \n", ret);
31 goto rnd_err;
32 }
33 ret = lega_RND_Reseeding (rndContext_ptr, rndWorkBuff_ptr);
34 if (ret != LEGA_HW_OK) {
35 printf("\n CRYS_RND_Reseeding failed with 0x%x \n", ret);
36 goto rnd_err;
37 }
38 ret = lega_RND_GenerateVector(rndContext_ptr, len, output);
39 if (ret != LEGA_HW_OK) {
40 printf("\n CRYS_RND_GenerateVector for vector 1 failed with 0x%x \n", ret);
41 goto rnd_err;
42 }
43
44 *olen = len;
45 lega_rnd_deinit();
46 rnd_err:
47 return ret;
48 }
49 #if 0
50 #define RND_MAX_LEN 192
51 #define RND_TEST_LEN 192
52 uint32_t mbedtls_hw_rand()
53 {
54 uint32_t ret = 0;
55 uint8_t buffer[RND_MAX_LEN] = {0};
56 uint32_t olen = 0;
57 mbedtls_hardware_poll("0", &buffer[0], RND_TEST_LEN, &olen);
58 printf("data len = %d, data = \n", (int)olen);
59 for (ret = 0; ret < RND_TEST_LEN; ret++) {
60 printf("0x%x ", buffer[ret]);
61 }
62 printf("\n");
63 return ret;
64 }
65 #endif
66 #endif