1 /*
2  * Copyright (c) 2024 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 #include "key_impl.h"
16 #include "log.h"
17 
18 namespace OHOS {
19     namespace CryptoFramework {
KeyImpl(HcfKey *hcfKey)20         KeyImpl::KeyImpl(HcfKey *hcfKey)
21         {
22             hcfKey_ = hcfKey;
23         }
24 
~KeyImpl()25         KeyImpl::~KeyImpl() {}
26 
GetHcfKey() const27         HcfKey *KeyImpl::GetHcfKey() const
28         {
29             return hcfKey_;
30         }
31 
GetFormat(int32_t* errCode)32         const char *KeyImpl::GetFormat(int32_t* errCode)
33         {
34             if (hcfKey_ == nullptr) {
35                 LOGE("fail to get key obj!");
36                 *errCode = HCF_ERR_MALLOC;
37                 return nullptr;
38             }
39             const char *format = hcfKey_->getFormat(hcfKey_);
40             *errCode = HCF_SUCCESS;
41             return format;
42         }
43 
GetAlgorithm(int32_t* errCode)44         const char *KeyImpl::GetAlgorithm(int32_t* errCode)
45         {
46             if (hcfKey_ == nullptr) {
47                 LOGE("fail to get key obj!");
48                 *errCode = HCF_ERR_MALLOC;
49                 return nullptr;
50             }
51             const char *algo = hcfKey_->getAlgorithm(hcfKey_);
52             *errCode = HCF_SUCCESS;
53             return algo;
54         }
55 
GetEncoded(HcfBlob *returnBlob)56         HcfResult KeyImpl::GetEncoded(HcfBlob *returnBlob)
57         {
58             if (hcfKey_ == nullptr) {
59                 LOGE("fail to get key obj!");
60                 return HCF_ERR_MALLOC;
61             }
62             return hcfKey_->getEncoded(hcfKey_, returnBlob);
63         }
64     }
65 }