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 "cipher_impl.h"
16 #include "log.h"
17 
18 namespace OHOS {
19     namespace CryptoFramework {
CipherImpl(HcfCipher * cipher)20         CipherImpl::CipherImpl(HcfCipher *cipher)
21         {
22             cipher_ = cipher;
23         }
24 
~CipherImpl()25         CipherImpl::~CipherImpl()
26         {
27             HcfObjDestroy(this->cipher_);
28         }
29 
CipherInit(HcfCryptoMode opMode,HcfKey * key,HcfParamsSpec * params)30         HcfResult CipherImpl::CipherInit(HcfCryptoMode opMode, HcfKey *key, HcfParamsSpec *params)
31         {
32             if (cipher_ == nullptr) {
33                 LOGE("fail to get cipher obj!");
34                 return HCF_ERR_MALLOC;
35             }
36             HcfResult res = cipher_->init(cipher_, opMode, key, params);
37             return res;
38         }
39 
CipherUpdate(HcfBlob * input,HcfBlob * output)40         HcfResult CipherImpl::CipherUpdate(HcfBlob *input, HcfBlob *output)
41         {
42             if (cipher_ == nullptr) {
43                 LOGE("fail to get cipher obj!");
44                 return HCF_ERR_MALLOC;
45             }
46             HcfResult res = cipher_->update(cipher_, input, output);
47             return res;
48         }
49 
CipherDoFinal(HcfBlob * input,HcfBlob * output)50         HcfResult CipherImpl::CipherDoFinal(HcfBlob *input, HcfBlob *output)
51         {
52             if (cipher_ == nullptr) {
53                 LOGE("fail to get cipher obj!");
54                 return HCF_ERR_MALLOC;
55             }
56             HcfResult res = cipher_->doFinal(cipher_, input, output);
57             return res;
58         }
59 
SetCipherSpec(CipherSpecItem item,HcfBlob pSource)60         HcfResult CipherImpl::SetCipherSpec(CipherSpecItem item, HcfBlob pSource)
61         {
62             if (cipher_ == nullptr) {
63                 LOGE("fail to get cipher obj!");
64                 return HCF_INVALID_PARAMS;
65             }
66             return cipher_->setCipherSpecUint8Array(cipher_, item, pSource);
67         }
68 
GetCipherSpecString(CipherSpecItem item,char * returnString)69         HcfResult CipherImpl::GetCipherSpecString(CipherSpecItem item, char *returnString)
70         {
71             if (cipher_ == nullptr) {
72                 LOGE("fail to get cipher obj!");
73                 return HCF_INVALID_PARAMS;
74             }
75             return cipher_->getCipherSpecString(cipher_, item, &returnString);
76         }
77 
GetCipherSpecUint8Array(CipherSpecItem item,HcfBlob * returnUint8Array)78         HcfResult CipherImpl::GetCipherSpecUint8Array(CipherSpecItem item, HcfBlob *returnUint8Array)
79         {
80             if (cipher_ == nullptr) {
81                 LOGE("fail to get cipher obj!");
82                 return HCF_INVALID_PARAMS;
83             }
84             return cipher_->getCipherSpecUint8Array(cipher_, item, returnUint8Array);
85         }
86 
GetAlgorithm(int32_t * errCode)87         const char *CipherImpl::GetAlgorithm(int32_t* errCode)
88         {
89             if (cipher_ == nullptr) {
90                 LOGE("fail to get cipher obj!");
91                 *errCode = HCF_ERR_MALLOC;
92                 return nullptr;
93             }
94             const char *algo = cipher_->getAlgorithm(cipher_);
95             *errCode = HCF_SUCCESS;
96             return algo;
97         }
98     }
99 }