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 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 BUF_OUT_LEN_1 (1) 300a7ce71fSopenharmony_ci#define BUF_OUT_LEN_2 (2) 310a7ce71fSopenharmony_ci 320a7ce71fSopenharmony_ci#define IOT_BIT_4 (4) 330a7ce71fSopenharmony_ci#define LOW_BIT_4 ((unsigned char)0x0f) 340a7ce71fSopenharmony_ci#define IOT_HEX_TO_STRING_1 (9) 350a7ce71fSopenharmony_ci#define IOT_HEX_TO_STRING_2 (10) 360a7ce71fSopenharmony_ci 370a7ce71fSopenharmony_ci// make a byte to 2 ascii hex 380a7ce71fSopenharmony_cistatic int Byte2HexStr(unsigned char *bufin, int len, char *bufout) 390a7ce71fSopenharmony_ci{ 400a7ce71fSopenharmony_ci int i; 410a7ce71fSopenharmony_ci unsigned char tmp_l; 420a7ce71fSopenharmony_ci unsigned char tmp_h; 430a7ce71fSopenharmony_ci if ((bufin == NULL) || (len <= 0) || (bufout == NULL)) { 440a7ce71fSopenharmony_ci return -1; 450a7ce71fSopenharmony_ci } 460a7ce71fSopenharmony_ci for (i = 0; i < len; i++) { 470a7ce71fSopenharmony_ci tmp_h = (bufin[i] >> IOT_BIT_4) & LOW_BIT_4; 480a7ce71fSopenharmony_ci tmp_l = bufin[i] & LOW_BIT_4; 490a7ce71fSopenharmony_ci bufout[BUF_OUT_LEN_2 * i] = (tmp_h > IOT_HEX_TO_STRING_1) ? \ 500a7ce71fSopenharmony_ci (tmp_h - IOT_HEX_TO_STRING_2 + 'a') : (tmp_h +'0'); 510a7ce71fSopenharmony_ci bufout[BUF_OUT_LEN_2 * i + BUF_OUT_LEN_1] = (tmp_l > IOT_HEX_TO_STRING_1) ? \ 520a7ce71fSopenharmony_ci (tmp_l - IOT_HEX_TO_STRING_2 + 'a') : (tmp_l +'0'); 530a7ce71fSopenharmony_ci } 540a7ce71fSopenharmony_ci bufout[BUF_OUT_LEN_2 * len] = '\0'; 550a7ce71fSopenharmony_ci 560a7ce71fSopenharmony_ci return 0; 570a7ce71fSopenharmony_ci} 580a7ce71fSopenharmony_ci 590a7ce71fSopenharmony_ci#define HMAC_PWD_LEN 65 600a7ce71fSopenharmony_ci 610a7ce71fSopenharmony_ciint HmacGeneratePwd(unsigned char *content, int contentLen, unsigned char *key, int keyLen, 620a7ce71fSopenharmony_ci unsigned char *buf) 630a7ce71fSopenharmony_ci{ 640a7ce71fSopenharmony_ci int ret = -1; 650a7ce71fSopenharmony_ci mbedtls_md_context_t mbedtls_md_ctx; 660a7ce71fSopenharmony_ci const mbedtls_md_info_t *mdInfo; 670a7ce71fSopenharmony_ci unsigned char hash[CN_HMAC256_LEN]; 680a7ce71fSopenharmony_ci if (key == NULL || content == NULL || buf == NULL || keyLen == 0 || contentLen == 0 || 690a7ce71fSopenharmony_ci ((CN_HMAC256_LEN * 2 + 1) > HMAC_PWD_LEN)) { /* 2倍的CN_HMAC256_LEN+1判断buflen是否合理 */ 700a7ce71fSopenharmony_ci return ret; 710a7ce71fSopenharmony_ci } 720a7ce71fSopenharmony_ci 730a7ce71fSopenharmony_ci mdInfo = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); 740a7ce71fSopenharmony_ci if (mdInfo == NULL || (size_t)mdInfo->size > CN_HMAC256_LEN) { 750a7ce71fSopenharmony_ci return ret; 760a7ce71fSopenharmony_ci } 770a7ce71fSopenharmony_ci 780a7ce71fSopenharmony_ci mbedtls_md_init(&mbedtls_md_ctx); 790a7ce71fSopenharmony_ci ret = mbedtls_md_setup(&mbedtls_md_ctx, mdInfo, 1); 800a7ce71fSopenharmony_ci if (ret != 0) { 810a7ce71fSopenharmony_ci mbedtls_md_free(&mbedtls_md_ctx); 820a7ce71fSopenharmony_ci return ret; 830a7ce71fSopenharmony_ci } 840a7ce71fSopenharmony_ci 850a7ce71fSopenharmony_ci (void)mbedtls_md_hmac_starts(&mbedtls_md_ctx, key, keyLen); 860a7ce71fSopenharmony_ci (void)mbedtls_md_hmac_update(&mbedtls_md_ctx, content, contentLen); 870a7ce71fSopenharmony_ci (void)mbedtls_md_hmac_finish(&mbedtls_md_ctx, hash); 880a7ce71fSopenharmony_ci 890a7ce71fSopenharmony_ci // transfer the hash code to the string mode 900a7ce71fSopenharmony_ci Byte2HexStr(hash, CN_HMAC256_LEN, (char *)buf); 910a7ce71fSopenharmony_ci return ret; 920a7ce71fSopenharmony_ci} 930a7ce71fSopenharmony_ci 94