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 <stdlib.h>
18 #include <stdio.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_kdf.h"
25 #include "bsl_params.h"
26 #include "crypt_params_key.h"
27
28 #define PBKDF2_PARAM_LEN (4)
29
StdMalloc(uint32_t len)30 void *StdMalloc(uint32_t len) {
31 return malloc((size_t)len);
32 }
33
PrintLastError(void)34 void PrintLastError(void) {
35 const char *file = NULL;
36 uint32_t line = 0;
37 BSL_ERR_GetLastErrorFileLine(&file, &line);
38 printf("failed at file %s at line %d\n", file, line);
39 }
40
main(void)41 int main(void)
42 {
43 int32_t ret;
44 uint8_t key[] = {0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64};
45 uint8_t salt[] = {0x4e, 0x61, 0x43, 0x6c};
46 uint32_t iterations = 80000;
47 uint8_t result[] = {
48 0x4d, 0xdc, 0xd8, 0xf6, 0x0b, 0x98, 0xbe, 0x21,
49 0x83, 0x0c, 0xee, 0x5e, 0xf2, 0x27, 0x01, 0xf9,
50 0x64, 0x1a, 0x44, 0x18, 0xd0, 0x4c, 0x04, 0x14,
51 0xae, 0xff, 0x08, 0x87, 0x6b, 0x34, 0xab, 0x56,
52 0xa1, 0xd4, 0x25, 0xa1, 0x22, 0x58, 0x33, 0x54,
53 0x9a, 0xdb, 0x84, 0x1b, 0x51, 0xc9, 0xb3, 0x17,
54 0x6a, 0x27, 0x2b, 0xde, 0xbb, 0xa1, 0xd0, 0x78,
55 0x47, 0x8f, 0x62, 0xb3, 0x97, 0xf3, 0x3c, 0x8d};
56
57 uint8_t out[sizeof(result)] = {0};
58 uint32_t outLen = sizeof(result);
59
60 // Initialize the error code module.
61 BSL_ERR_Init();
62
63 /**
64 * Before calling the algorithm APIs,
65 * call the BSL_SAL_CallBack_Ctrl function to register the malloc and free functions.
66 * Execute this step only once. If the memory allocation ability of Linux is available,
67 * the two functions can be registered using Linux by default.
68 */
69 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, StdMalloc);
70 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
71
72 CRYPT_EAL_KdfCTX *ctx = CRYPT_EAL_KdfNewCtx(CRYPT_KDF_PBKDF2);
73 if (ctx == NULL) {
74 PrintLastError();
75 goto EXIT;
76 }
77 CRYPT_MAC_AlgId id = CRYPT_MAC_HMAC_SHA256;
78 BSL_Param params[5] = {{0}, {0}, {0}, {0}, BSL_PARAM_END};
79 (void)BSL_PARAM_InitValue(¶ms[0], CRYPT_PARAM_KDF_MAC_ID, BSL_PARAM_TYPE_UINT32, &id, sizeof(id));
80 (void)BSL_PARAM_InitValue(¶ms[1], CRYPT_PARAM_KDF_PASSWORD, BSL_PARAM_TYPE_OCTETS, key, sizeof(key));
81 (void)BSL_PARAM_InitValue(¶ms[2], CRYPT_PARAM_KDF_SALT, BSL_PARAM_TYPE_OCTETS, salt, sizeof(salt));
82 (void)BSL_PARAM_InitValue(¶ms[3], CRYPT_PARAM_KDF_ITER, BSL_PARAM_TYPE_UINT32, &iterations, sizeof(iterations));
83 ret = CRYPT_EAL_KdfSetParam(ctx, params);
84 if (ret != CRYPT_SUCCESS) {
85 printf("error code is %x\n", ret);
86 PrintLastError();
87 goto EXIT;
88 }
89
90 ret = CRYPT_EAL_KdfDerive(ctx, out, outLen);
91 if (ret != CRYPT_SUCCESS) {
92 printf("error code is %x\n", ret);
93 PrintLastError();
94 goto EXIT;
95 }
96
97 if (memcmp(out, result, sizeof(result)) != 0) {
98 printf("failed to compare test results\n");
99 ret = -1;
100 goto EXIT;
101 }
102 printf("pass \n");
103
104 EXIT:
105 BSL_ERR_DeInit();
106 CRYPT_EAL_KdfFreeCtx(ctx);
107 return ret;
108 }