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 <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "crypt_errno.h"
21 #include "bsl_sal.h"
22 #include "bsl_err.h"
23 #include "crypt_algid.h"
24 #include "crypt_eal_md.h"
25
26 #define PBKDF2_PARAM_LEN (4)
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_GetErrorFileLine(&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 = 0;
42 CRYPT_EAL_MdCTX *ctx = NULL;
43 uint8_t digest[32] = {0};
44 unsigned int digestLen = 32;
45 uint8_t data[] = {0x1b, 0x50, 0x3f, 0xb9, 0xa7, 0x3b, 0x16, 0xad, 0xa3,
46 0xfc, 0xf1, 0x04, 0x26, 0x23, 0xae, 0x76, 0x10};
47 uint8_t expResult[] = {0xd5, 0xc3, 0x03, 0x15, 0xf7, 0x2e, 0xd0, 0x5f, 0xe5, 0x19, 0xa1, 0xbf, 0x75,
48 0xab, 0x5f, 0xd0, 0xff, 0xec, 0x5a, 0xc1, 0xac, 0xb0, 0xda, 0xf6, 0x6b, 0x6b, 0x76, 0x95, 0x98,
49 0x59, 0x45, 0x09};
50
51 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, StdMalloc);
52 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
53
54 // Initialize the error code module.
55 BSL_ERR_Init();
56
57 /**
58 * Before calling the algorithm APIs,
59 * call the BSL_SAL_CallBack_Ctrl function to register the malloc and free functions.
60 * Execute this step only once. If the memory allocation ability of Linux is available,
61 * the two functions can be registered using Linux by default.
62 */
63 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, StdMalloc);
64 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
65
66 ctx = CRYPT_EAL_MdNewCtx(CRYPT_MD_SHA256);
67 if (ctx == NULL) {
68 PrintLastError();
69 goto EXIT;
70 }
71
72 ret = CRYPT_EAL_MdInit(ctx);
73 if (ret != 0) {
74 PrintLastError();
75 goto EXIT;
76 }
77
78 ret = CRYPT_EAL_MdUpdate(ctx, data, sizeof(data));
79 if (ret != 0) {
80 PrintLastError();
81 goto EXIT;
82 }
83
84 ret = CRYPT_EAL_MdFinal(ctx, digest, &digestLen);
85 if (ret != 0) {
86 PrintLastError();
87 goto EXIT;
88 }
89 printf("hash result: ");
90 for (uint32_t i = 0; i < digestLen; i++) {
91 printf("%02x", digest[i]);
92 }
93 printf("\n");
94 // result compare
95 if (digestLen != sizeof(expResult) || memcmp(expResult, digest, digestLen) != 0) {
96 printf("hash result comparison failed\n");
97 goto EXIT;
98 }
99 printf("pass \n");
100
101 EXIT:
102 BSL_ERR_DeInit();
103 CRYPT_EAL_MdFreeCtx(ctx);
104 return ret;
105 }