1 /*
2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 /**
17 * This file make use the hmac to make mqtt pwd.The method is use the date string to hash the device passwd .
18 * Take care that this implement depends on the hmac of the mbedtls
19 */
20
21 #include <stdint.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "md.h"
26 #include "md_internal.h"
27
28 #define CN_HMAC256_LEN 32
29 #define BUF_OUT_LEN_1 (1)
30 #define BUF_OUT_LEN_2 (2)
31
32 #define IOT_BIT_4 (4)
33 #define LOW_BIT_4 ((unsigned char)0x0f)
34 #define IOT_HEX_TO_STRING_1 (9)
35 #define IOT_HEX_TO_STRING_2 (10)
36
37 // make a byte to 2 ascii hex
Byte2HexStr(unsigned char *bufin, int len, char *bufout)38 static int Byte2HexStr(unsigned char *bufin, int len, char *bufout)
39 {
40 int i;
41 unsigned char tmp_l;
42 unsigned char tmp_h;
43 if ((bufin == NULL) || (len <= 0) || (bufout == NULL)) {
44 return -1;
45 }
46 for (i = 0; i < len; i++) {
47 tmp_h = (bufin[i] >> IOT_BIT_4) & LOW_BIT_4;
48 tmp_l = bufin[i] & LOW_BIT_4;
49 bufout[BUF_OUT_LEN_2 * i] = (tmp_h > IOT_HEX_TO_STRING_1) ? \
50 (tmp_h - IOT_HEX_TO_STRING_2 + 'a') : (tmp_h +'0');
51 bufout[BUF_OUT_LEN_2 * i + BUF_OUT_LEN_1] = (tmp_l > IOT_HEX_TO_STRING_1) ? \
52 (tmp_l - IOT_HEX_TO_STRING_2 + 'a') : (tmp_l +'0');
53 }
54 bufout[BUF_OUT_LEN_2 * len] = '\0';
55
56 return 0;
57 }
58
59 #define HMAC_PWD_LEN 65
60
HmacGeneratePwd(unsigned char *content, int contentLen, unsigned char *key, int keyLen, unsigned char *buf)61 int HmacGeneratePwd(unsigned char *content, int contentLen, unsigned char *key, int keyLen,
62 unsigned char *buf)
63 {
64 int ret = -1;
65 mbedtls_md_context_t mbedtls_md_ctx;
66 const mbedtls_md_info_t *mdInfo;
67 unsigned char hash[CN_HMAC256_LEN];
68 if (key == NULL || content == NULL || buf == NULL || keyLen == 0 || contentLen == 0 ||
69 ((CN_HMAC256_LEN * 2 + 1) > HMAC_PWD_LEN)) { /* 2倍的CN_HMAC256_LEN+1判断buflen是否合理 */
70 return ret;
71 }
72
73 mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
74 if (mdInfo == NULL || (size_t)mdInfo->size > CN_HMAC256_LEN) {
75 return ret;
76 }
77
78 mbedtls_md_init(&mbedtls_md_ctx);
79 ret = mbedtls_md_setup(&mbedtls_md_ctx, mdInfo, 1);
80 if (ret != 0) {
81 mbedtls_md_free(&mbedtls_md_ctx);
82 return ret;
83 }
84
85 (void)mbedtls_md_hmac_starts(&mbedtls_md_ctx, key, keyLen);
86 (void)mbedtls_md_hmac_update(&mbedtls_md_ctx, content, contentLen);
87 (void)mbedtls_md_hmac_finish(&mbedtls_md_ctx, hash);
88
89 // transfer the hash code to the string mode
90 Byte2HexStr(hash, CN_HMAC256_LEN, (char *)buf);
91 return ret;
92 }
93
94