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.
18  * The method is use the date string to hash the device passwd
19  * Take care that this implement depends on the hmac of the mbedtls
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_MAX (65)
29 #define CN_HMAC256_LEN   32
30 #define RIGHT_MOVE_BIT_4 (4)
31 #define NUMBER_9 (9)
32 #define DECIMAL_BASE (10)
33 #define STRING_LEN_TIMES (2)
34 #define LEN_TIMES (2)
35 #define OFFSET (1)
36 // make a byte to 2 ascii hex
byte2hexstr(unsigned char *bufin, int len, char *bufout)37 static int byte2hexstr(unsigned char *bufin, int len, char *bufout)
38 {
39     int i = 0;
40     unsigned char  tmp_l = 0x0;
41     unsigned char  tmp_h = 0;
42     if ((bufin == NULL)||(len <= 0)||(bufout == NULL)) {
43         return -1;
44     }
45     for (i = 0; i < len; i++) {
46         tmp_h = (bufin[i] >> RIGHT_MOVE_BIT_4) & 0X0F;
47         tmp_l = bufin[i] & 0x0F;
48         bufout[STRING_LEN_TIMES * i] = (tmp_h > NUMBER_9) ?
49             (tmp_h - DECIMAL_BASE + 'a'):(tmp_h +'0');
50         bufout[STRING_LEN_TIMES * i + OFFSET] = (tmp_l > NUMBER_9) ?
51             (tmp_l - DECIMAL_BASE + 'a'):(tmp_l +'0');
52     }
53     bufout[STRING_LEN_TIMES * len] = '\0';
54 
55     return 0;
56 }
57 
HmacGeneratePwd(const unsigned char *content, int contentLen, const unsigned char *key, int keyLen, unsigned char *buf)58 int HmacGeneratePwd(const unsigned char *content, int contentLen, const unsigned char *key,
59     int keyLen, unsigned char *buf)
60 {
61     int ret = -1;
62     mbedtls_md_context_t mbedtls_md_ctx;
63     const mbedtls_md_info_t *md_info;
64     unsigned char hash[CN_HMAC256_LEN];
65 
66     if ((key == NULL)||(content == NULL)||(buf == NULL)||
67         (keyLen == 0)||(contentLen == 0)||
68         (CN_HMAC256_LEN_MAX < (CN_HMAC256_LEN * LEN_TIMES + OFFSET))) {
69         return ret;
70     }
71 
72     md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
73     if ((md_info == NULL)||((size_t)md_info->size > CN_HMAC256_LEN)) {
74         return ret;
75     }
76 
77     mbedtls_md_init(&mbedtls_md_ctx);
78     ret = mbedtls_md_setup(&mbedtls_md_ctx, md_info, 1);
79     if (ret != 0) {
80         mbedtls_md_free(&mbedtls_md_ctx);
81         return ret;
82     }
83 
84     (void)mbedtls_md_hmac_starts(&mbedtls_md_ctx, key, keyLen);
85     (void)mbedtls_md_hmac_update(&mbedtls_md_ctx, content, contentLen);
86     (void)mbedtls_md_hmac_finish(&mbedtls_md_ctx, hash);
87 
88     // <transfer the hash code to the string mode
89     ret = byte2hexstr(hash, CN_HMAC256_LEN, (char *)buf);
90     if (ret != 0) {
91         return ret;
92     }
93 
94     return ret;
95 }