• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include "crypt_types.h"
21 #include "bsl_sal.h"
22 #include "bsl_err.h"
23 #include "crypt_algid.h"
24 #include "crypt_eal_init.h"
25 #include "crypt_errno.h"
26 #include "crypt_eal_rand.h"
27 
StdMalloc(uint32_t len)28 void *StdMalloc(uint32_t len) {
29     return malloc((size_t)len);
30 }
31 
PrintLastError(void)32 void PrintLastError(void) {
33     const char *file = NULL;
34     uint32_t line = 0;
35     BSL_ERR_GetLastErrorFileLine(&file, &line);
36     printf("failed at file %s at line %d\n", file, line);
37 }
38 
main(void)39 int main(void)
40 {
41     int ret;
42     uint8_t output[100] = {0};
43     uint32_t len = 100;
44     ret = CRYPT_EAL_Init(CRYPT_EAL_INIT_CPU | CRYPT_EAL_INIT_PROVIDER);
45     if (ret != CRYPT_SUCCESS) {
46         printf("CRYPT_EAL_Init: error code is %x\n", ret);
47         return ret;
48     }
49     /**
50      * Before calling the algorithm APIs,
51      * call the BSL_SAL_CallBack_Ctrl function to register the malloc and free functions.
52      * Execute this step only once. If the memory allocation ability of Linux is available,
53      * the two functions can be registered using Linux by default.
54     */
55     BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, StdMalloc);
56     BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
57 
58     BSL_ERR_Init(); // Initialize the error module.
59 
60     // Initialize the global random number by using the default entropy source from **/dev/random** of Linux.
61     ret = CRYPT_EAL_ProviderRandInitCtx(NULL, CRYPT_RAND_SHA256, "provider=default", NULL, 0, NULL);
62     if (ret != CRYPT_SUCCESS) {
63         printf("RandInit: error code is %x\n", ret);
64         PrintLastError();
65         goto EXIT;
66     }
67 
68     // Obtain the random number sequence of the **len** value.
69     ret = CRYPT_EAL_RandbytesEx(NULL, output, len);
70     if (ret != CRYPT_SUCCESS) {
71         printf("CRYPT_EAL_Randbytes: error code is %x\n", ret);
72         PrintLastError();
73         goto EXIT;
74     }
75 
76     printf("random value is: ");  // Output the random number.
77     for (uint32_t i = 0; i < len; i++) {
78         printf("%02x", output[i]);
79     }
80     printf("\n");
81 
82     // Reseeding
83     ret = CRYPT_EAL_RandSeedEx(NULL);
84     if (ret != CRYPT_SUCCESS) {
85         printf("CRYPT_EAL_RandSeed: error code is %x\n", ret);
86         PrintLastError();
87         goto EXIT;
88     }
89 
90     // Obtain the random number sequence of the **len** value.
91     ret = CRYPT_EAL_RandbytesEx(NULL, output, len);
92     if (ret != CRYPT_SUCCESS) {
93         printf("CRYPT_EAL_Randbytes: error code is %x\n", ret);
94         PrintLastError();
95         goto EXIT;
96     }
97 
98     printf("random value is: "); // Output the random number.
99     for (uint32_t i = 0; i < len; i++) {
100         printf("%02x", output[i]);
101     }
102     printf("\n");
103 
104 EXIT:
105     // Release the context memory.
106     CRYPT_EAL_RandDeinit();
107     BSL_ERR_DeInit();
108     return 0;
109 }