• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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 "ani_mac.h"
17 #include "detailed_hmac_params.h"
18 
19 using namespace taihe;
20 using namespace ohos::security::cryptoFramework::cryptoFramework;
21 using namespace ANI::CryptoFramework;
22 
23 namespace ANI::CryptoFramework {
MacImpl()24 MacImpl::MacImpl() : macObj(nullptr) {}
25 
MacImpl(HcfMac * obj)26 MacImpl::MacImpl(HcfMac *obj) : macObj(obj) {}
27 
~MacImpl()28 MacImpl::~MacImpl()
29 {
30     HcfObjDestroy(macObj);
31     macObj = nullptr;
32 }
33 
InitSync(weak::SymKey key)34 void MacImpl::InitSync(weak::SymKey key)
35 {
36     if (macObj == nullptr) {
37         ANI_LOGE_THROW(HCF_INVALID_PARAMS, "mac obj is nullptr!");
38         return;
39     }
40     HcfSymKey *symKey = reinterpret_cast<HcfSymKey *>(key->GetSymKeyObj());
41     HcfResult res = macObj->init(macObj, symKey);
42     if (res != HCF_SUCCESS) {
43         ANI_LOGE_THROW(res, "mac init failed!");
44         return;
45     }
46 }
47 
UpdateSync(DataBlob const & input)48 void MacImpl::UpdateSync(DataBlob const& input)
49 {
50     if (macObj == nullptr) {
51         ANI_LOGE_THROW(HCF_INVALID_PARAMS, "mac obj is nullptr!");
52         return;
53     }
54     HcfBlob inBlob = { .data = input.data.data(), .len = input.data.size() };
55     HcfResult res = macObj->update(macObj, &inBlob);
56     if (res != HCF_SUCCESS) {
57         ANI_LOGE_THROW(res, "mac update failed!");
58         return;
59     }
60 }
61 
DoFinalSync()62 DataBlob MacImpl::DoFinalSync()
63 {
64     if (macObj == nullptr) {
65         ANI_LOGE_THROW(HCF_INVALID_PARAMS, "mac obj is nullptr!");
66         return { taihe::array<uint8_t>(nullptr, 0) };
67     }
68     HcfBlob outBlob = { .data = nullptr, .len = 0 };
69     HcfResult res = macObj->doFinal(macObj, &outBlob);
70     if (res != HCF_SUCCESS) {
71         ANI_LOGE_THROW(res, "mac doFinal failed!");
72         return { taihe::array<uint8_t>(nullptr, 0) };
73     }
74     taihe::array<uint8_t> data(move_data_t{}, outBlob.data, outBlob.len);
75     HcfBlobDataClearAndFree(&outBlob);
76     return { data };
77 }
78 
GetMacLength()79 int32_t MacImpl::GetMacLength()
80 {
81     if (macObj == nullptr) {
82         ANI_LOGE_THROW(HCF_INVALID_PARAMS, "mac obj is nullptr!");
83         return 0;
84     }
85     uint32_t length = macObj->getMacLength(macObj);
86     return static_cast<int32_t>(length);
87 }
88 
GetAlgName()89 string MacImpl::GetAlgName()
90 {
91     if (macObj == nullptr) {
92         return "";
93     }
94     const char *algName = macObj->getAlgoName(macObj);
95     return (algName == nullptr) ? "" : string(algName);
96 }
97 
CreateMac(string_view algName)98 Mac CreateMac(string_view algName)
99 {
100     HcfMac *macObj = nullptr;
101     HcfHmacParamsSpec params = { { "HMAC" }, algName.c_str() };
102     HcfResult res = HcfMacCreate(reinterpret_cast<HcfMacParamsSpec *>(&params), &macObj);
103     if (res != HCF_SUCCESS) {
104         ANI_LOGE_THROW(res, "create C mac obj failed.");
105         return make_holder<MacImpl, Mac>(nullptr);
106     }
107     return make_holder<MacImpl, Mac>(macObj);
108 }
109 } // namespace ANI::CryptoFramework
110 
111 TH_EXPORT_CPP_API_CreateMac(CreateMac);
112