10a7ce71fSopenharmony_ci/*
20a7ce71fSopenharmony_ci * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
30a7ce71fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40a7ce71fSopenharmony_ci * you may not use this file except in compliance with the License.
50a7ce71fSopenharmony_ci * You may obtain a copy of the License at
60a7ce71fSopenharmony_ci *
70a7ce71fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80a7ce71fSopenharmony_ci *
90a7ce71fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100a7ce71fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110a7ce71fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120a7ce71fSopenharmony_ci * See the License for the specific language governing permissions and
130a7ce71fSopenharmony_ci * limitations under the License.
140a7ce71fSopenharmony_ci */
150a7ce71fSopenharmony_ci
160a7ce71fSopenharmony_ci/**
170a7ce71fSopenharmony_ci * This file make use the hmac to make mqtt pwd.The method is use the date string to hash the device passwd .
180a7ce71fSopenharmony_ci * Take care that this implement depends on the hmac of the mbedtls
190a7ce71fSopenharmony_ci*/
200a7ce71fSopenharmony_ci#include "iot_hmac.h"
210a7ce71fSopenharmony_ci#include <stdint.h>
220a7ce71fSopenharmony_ci#include <stddef.h>
230a7ce71fSopenharmony_ci#include <string.h>
240a7ce71fSopenharmony_ci#include <stdio.h>
250a7ce71fSopenharmony_ci#include "md.h"
260a7ce71fSopenharmony_ci#include "md_internal.h"
270a7ce71fSopenharmony_ci
280a7ce71fSopenharmony_ci#define CN_HMAC256_LEN   32
290a7ce71fSopenharmony_ci#define HMAC256_LEN  65
300a7ce71fSopenharmony_ci
310a7ce71fSopenharmony_ci// make a byte to 2 ascii hex
320a7ce71fSopenharmony_cistatic int Byte2hexstr(unsigned char *bufin, int len, char *bufout)
330a7ce71fSopenharmony_ci{
340a7ce71fSopenharmony_ci    if ((bufin == NULL) || (len <= 0) || (bufout == NULL)) {
350a7ce71fSopenharmony_ci        return -1;
360a7ce71fSopenharmony_ci    }
370a7ce71fSopenharmony_ci    for (int i = 0; i < len; i++) {
380a7ce71fSopenharmony_ci        unsigned char tmpH = (bufin[i] >> 4) & 0X0F; /* 高字节前4位保存到tmpH */
390a7ce71fSopenharmony_ci        unsigned char tmpL = bufin[i] & 0x0F;
400a7ce71fSopenharmony_ci        bufout[2 * i] = (tmpH > 9) ? (tmpH - 10 + 'a') : (tmpH + '0'); /* 如果高字节大于9与高字节减10,将高字节转成字符形式,同时字符占2字节 */
410a7ce71fSopenharmony_ci        bufout[2 * i + 1] = (tmpL > 9) ? (tmpL - 10 + 'a') : (tmpL + '0'); /* 如果低字节大于9与高字节减10,将低字节转成字符形式,同时字符占2字节 */
420a7ce71fSopenharmony_ci    }
430a7ce71fSopenharmony_ci    bufout[2 * len] = '\0'; /* 字符占2字节 */
440a7ce71fSopenharmony_ci    return 0;
450a7ce71fSopenharmony_ci}
460a7ce71fSopenharmony_ci
470a7ce71fSopenharmony_ciint HmacGeneratePwd(unsigned char *content, int contentLen, unsigned char *key, int keyLen,
480a7ce71fSopenharmony_ci                    unsigned char *buf)
490a7ce71fSopenharmony_ci{
500a7ce71fSopenharmony_ci    int ret = -1;
510a7ce71fSopenharmony_ci    mbedtls_md_context_t mbedtls_md_ctx;
520a7ce71fSopenharmony_ci    const mbedtls_md_info_t *mdInfo;
530a7ce71fSopenharmony_ci    unsigned char hash[CN_HMAC256_LEN];
540a7ce71fSopenharmony_ci    if (key == NULL || content == NULL || buf == NULL || keyLen == 0 || contentLen == 0 ||
550a7ce71fSopenharmony_ci        ((CN_HMAC256_LEN * 2 + 1) > HMAC256_LEN)) { /* 2倍的CN_HMAC256_LEN+1判断buflen是否合理 */
560a7ce71fSopenharmony_ci        return ret;
570a7ce71fSopenharmony_ci    }
580a7ce71fSopenharmony_ci
590a7ce71fSopenharmony_ci    mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
600a7ce71fSopenharmony_ci    if (mdInfo == NULL || (size_t)mdInfo->size > CN_HMAC256_LEN) {
610a7ce71fSopenharmony_ci        return ret;
620a7ce71fSopenharmony_ci    }
630a7ce71fSopenharmony_ci
640a7ce71fSopenharmony_ci    mbedtls_md_init(&mbedtls_md_ctx);
650a7ce71fSopenharmony_ci    ret = mbedtls_md_setup(&mbedtls_md_ctx, mdInfo, 1);
660a7ce71fSopenharmony_ci    if (ret != 0) {
670a7ce71fSopenharmony_ci        mbedtls_md_free(&mbedtls_md_ctx);
680a7ce71fSopenharmony_ci        return ret;
690a7ce71fSopenharmony_ci    }
700a7ce71fSopenharmony_ci
710a7ce71fSopenharmony_ci    (void)mbedtls_md_hmac_starts(&mbedtls_md_ctx, key, keyLen);
720a7ce71fSopenharmony_ci    (void)mbedtls_md_hmac_update(&mbedtls_md_ctx, content, contentLen);
730a7ce71fSopenharmony_ci    (void)mbedtls_md_hmac_finish(&mbedtls_md_ctx, hash);
740a7ce71fSopenharmony_ci
750a7ce71fSopenharmony_ci    // transfer the hash code to the string mode
760a7ce71fSopenharmony_ci    Byte2hexstr(hash, CN_HMAC256_LEN, (char *)buf);
770a7ce71fSopenharmony_ci    return ret;
780a7ce71fSopenharmony_ci}