• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "iot_hmac.h"
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 HMAC256_LEN  65
30 
31 // make a byte to 2 ascii hex
Byte2hexstr(unsigned char * bufin,int len,char * bufout)32 static int Byte2hexstr(unsigned char *bufin, int len, char *bufout)
33 {
34     if ((bufin == NULL) || (len <= 0) || (bufout == NULL)) {
35         return -1;
36     }
37     for (int i = 0; i < len; i++) {
38         unsigned char tmpH = (bufin[i] >> 4) & 0X0F; /* 高字节前4位保存到tmpH */
39         unsigned char tmpL = bufin[i] & 0x0F;
40         bufout[2 * i] = (tmpH > 9) ? (tmpH - 10 + 'a') : (tmpH + '0'); /* 如果高字节大于9与高字节减10,将高字节转成字符形式,同时字符占2字节 */
41         bufout[2 * i + 1] = (tmpL > 9) ? (tmpL - 10 + 'a') : (tmpL + '0'); /* 如果低字节大于9与高字节减10,将低字节转成字符形式,同时字符占2字节 */
42     }
43     bufout[2 * len] = '\0'; /* 字符占2字节 */
44     return 0;
45 }
46 
HmacGeneratePwd(unsigned char * content,int contentLen,unsigned char * key,int keyLen,unsigned char * buf)47 int HmacGeneratePwd(unsigned char *content, int contentLen, unsigned char *key, int keyLen,
48                     unsigned char *buf)
49 {
50     int ret = -1;
51     mbedtls_md_context_t mbedtls_md_ctx;
52     const mbedtls_md_info_t *mdInfo;
53     unsigned char hash[CN_HMAC256_LEN];
54     if (key == NULL || content == NULL || buf == NULL || keyLen == 0 || contentLen == 0 ||
55         ((CN_HMAC256_LEN * 2 + 1) > HMAC256_LEN)) { /* 2倍的CN_HMAC256_LEN+1判断buflen是否合理 */
56         return ret;
57     }
58 
59     mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
60     if (mdInfo == NULL || (size_t)mdInfo->size > CN_HMAC256_LEN) {
61         return ret;
62     }
63 
64     mbedtls_md_init(&mbedtls_md_ctx);
65     ret = mbedtls_md_setup(&mbedtls_md_ctx, mdInfo, 1);
66     if (ret != 0) {
67         mbedtls_md_free(&mbedtls_md_ctx);
68         return ret;
69     }
70 
71     (void)mbedtls_md_hmac_starts(&mbedtls_md_ctx, key, keyLen);
72     (void)mbedtls_md_hmac_update(&mbedtls_md_ctx, content, contentLen);
73     (void)mbedtls_md_hmac_finish(&mbedtls_md_ctx, hash);
74 
75     // transfer the hash code to the string mode
76     Byte2hexstr(hash, CN_HMAC256_LEN, (char *)buf);
77     return ret;
78 }