1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
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 #include "dm_softbus_adapter_crypto.h"
17
18 #include "dm_constants.h"
19 #include "dm_log.h"
20 #include "md.h"
21 #include "securec.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
25
26 namespace {
27 constexpr int SHA_HASH_LEN = 32;
28 constexpr int SHORT_DEVICE_ID_HASH_LENGTH = 8;
29 constexpr int HEXIFY_UNIT_LEN = 2;
30 constexpr int HEX_DIGIT_MAX_NUM = 16;
31 constexpr int DEC_MAX_NUM = 10;
32 #define HEXIFY_LEN(len) ((len) * HEXIFY_UNIT_LEN + 1)
33 } // namespace
34
DmGenerateStrHash(const unsigned char * str,uint32_t len,unsigned char * hash)35 int32_t DmGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash)
36 {
37 if (str == nullptr || hash == nullptr || len == 0) {
38 return ERR_DM_INPUT_PARA_INVALID;
39 }
40
41 mbedtls_md_context_t ctx;
42 const mbedtls_md_info_t *info = NULL;
43 mbedtls_md_init(&ctx);
44
45 info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
46 if (mbedtls_md_setup(&ctx, info, 0) != 0) {
47 mbedtls_md_free(&ctx);
48 return ERR_DM_FAILED;
49 }
50 if (mbedtls_md_starts(&ctx) != 0) {
51 mbedtls_md_free(&ctx);
52 return ERR_DM_FAILED;
53 }
54 if (mbedtls_md_update(&ctx, str, len) != 0) {
55 mbedtls_md_free(&ctx);
56 return ERR_DM_FAILED;
57 }
58 if (mbedtls_md_finish(&ctx, hash) != 0) {
59 mbedtls_md_free(&ctx);
60 return ERR_DM_FAILED;
61 }
62
63 mbedtls_md_free(&ctx);
64 return DM_OK;
65 }
66
ConvertBytesToHexString(char * outBuf,uint32_t outBufLen,const unsigned char * inBuf,uint32_t inLen)67 int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, const unsigned char *inBuf,
68 uint32_t inLen)
69 {
70 if ((outBuf == nullptr) || (inBuf == nullptr) || (outBufLen < HEXIFY_LEN(inLen))) {
71 return ERR_DM_INPUT_PARA_INVALID;
72 }
73
74 while (inLen > 0) {
75 unsigned char h = *inBuf / HEX_DIGIT_MAX_NUM;
76 unsigned char l = *inBuf % HEX_DIGIT_MAX_NUM;
77 if (h < DEC_MAX_NUM) {
78 *outBuf++ = '0' + h;
79 } else {
80 *outBuf++ = 'a' + h - DEC_MAX_NUM;
81 }
82 if (l < DEC_MAX_NUM) {
83 *outBuf++ = '0' + l;
84 } else {
85 *outBuf++ = 'a' + l - DEC_MAX_NUM;
86 }
87 ++inBuf;
88 inLen--;
89 }
90 return DM_OK;
91 }
92
GetUdidHash(const std::string & udid,unsigned char * udidHash)93 int32_t DmSoftbusAdapterCrypto::GetUdidHash(const std::string &udid, unsigned char *udidHash)
94 {
95 char hashResult[SHA_HASH_LEN] = {0};
96 int32_t ret = DmGenerateStrHash((const uint8_t *)udid.c_str(), strlen(udid.c_str()) + 1, (uint8_t *)hashResult);
97 if (ret != DM_OK) {
98 LOGE("GenerateStrHash failed");
99 return ret;
100 }
101 ret = ConvertBytesToHexString((char *)udidHash, SHORT_DEVICE_ID_HASH_LENGTH + 1, (const uint8_t *)hashResult,
102 SHORT_DEVICE_ID_HASH_LENGTH / HEXIFY_UNIT_LEN);
103 if (ret != DM_OK) {
104 LOGE("ConvertBytesToHexString failed");
105 return ret;
106 }
107 return DM_OK;
108 }
109 } // namespace DistributedHardware
110 } // namespace OHOS