• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #define LOG_TAG "OhRdbCryptoParam"
16 #include "oh_rdb_crypto_param.h"
17 #include <vector>
18 #include "oh_data_define.h"
19 #include "relational_store_error_code.h"
20 
21 using namespace OHOS::DistributedRdb;
22 using namespace OHOS::NativeRdb;
23 
IsValid() const24 bool OH_Rdb_CryptoParam::IsValid() const
25 {
26     return id == OH_CRYPTO_PARAM_ID;
27 }
28 
OH_Rdb_CreateCryptoParam(void)29 OH_Rdb_CryptoParam *OH_Rdb_CreateCryptoParam(void)
30 {
31     OH_Rdb_CryptoParam *value = new (std::nothrow) OH_Rdb_CryptoParam;
32     if (value == nullptr) {
33         return nullptr;
34     }
35     return value;
36 }
37 
OH_Rdb_DestroyCryptoParam(OH_Rdb_CryptoParam * cryptoParam)38 int OH_Rdb_DestroyCryptoParam(OH_Rdb_CryptoParam *cryptoParam)
39 {
40     if (cryptoParam == nullptr || !cryptoParam->IsValid()) {
41         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
42     }
43     delete cryptoParam;
44     return OH_Rdb_ErrCode::RDB_OK;
45 }
46 
OH_Crypto_SetEncryptionKey(OH_Rdb_CryptoParam * param,const uint8_t * key,int32_t length)47 int OH_Crypto_SetEncryptionKey(OH_Rdb_CryptoParam *param, const uint8_t *key, int32_t length)
48 {
49     if (param == nullptr || !param->IsValid()) {
50         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
51     }
52     if (key == nullptr || length == 0) {
53         param->cryptoParam.encryptKey_.assign(param->cryptoParam.encryptKey_.size(), 0);
54     } else {
55         param->cryptoParam.encryptKey_ = std::vector<uint8_t>{ key, key + length };
56     }
57     return OH_Rdb_ErrCode::RDB_OK;
58 }
59 
OH_Crypto_SetIteration(OH_Rdb_CryptoParam * param,int64_t iteration)60 int OH_Crypto_SetIteration(OH_Rdb_CryptoParam *param, int64_t iteration)
61 {
62     if (param == nullptr || !param->IsValid() || iteration < 0) {
63         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
64     }
65     param->cryptoParam.iterNum = static_cast<int32_t>(iteration);
66     return OH_Rdb_ErrCode::RDB_OK;
67 }
68 
OH_Crypto_SetEncryptionAlgo(OH_Rdb_CryptoParam * param,int32_t algo)69 int OH_Crypto_SetEncryptionAlgo(OH_Rdb_CryptoParam *param, int32_t algo)
70 {
71     if (param == nullptr || !param->IsValid() ||
72         (algo != static_cast<int32_t>(RDB_AES_256_GCM) &&
73         algo != static_cast<int32_t>(RDB_AES_256_CBC))) {
74         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
75     }
76     param->cryptoParam.encryptAlgo = algo;
77     return OH_Rdb_ErrCode::RDB_OK;
78 }
79 
OH_Crypto_SetHmacAlgo(OH_Rdb_CryptoParam * param,int32_t algo)80 int OH_Crypto_SetHmacAlgo(OH_Rdb_CryptoParam *param, int32_t algo)
81 {
82     if (param == nullptr || !param->IsValid() ||
83         algo < static_cast<int32_t>(RDB_HMAC_SHA1) || algo > static_cast<int32_t>(RDB_HMAC_SHA512)) {
84         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
85     }
86     param->cryptoParam.hmacAlgo = algo;
87     return OH_Rdb_ErrCode::RDB_OK;
88 }
89 
OH_Crypto_SetKdfAlgo(OH_Rdb_CryptoParam * param,int32_t algo)90 int OH_Crypto_SetKdfAlgo(OH_Rdb_CryptoParam *param, int32_t algo)
91 {
92     if (param == nullptr || !param->IsValid() ||
93         algo < static_cast<int32_t>(RDB_KDF_SHA1) || algo > static_cast<int32_t>(RDB_KDF_SHA512)) {
94         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
95     }
96     param->cryptoParam.kdfAlgo = algo;
97     return OH_Rdb_ErrCode::RDB_OK;
98 }
99 
OH_Crypto_SetCryptoPageSize(OH_Rdb_CryptoParam * param,int64_t size)100 int OH_Crypto_SetCryptoPageSize(OH_Rdb_CryptoParam *param, int64_t size)
101 {
102     if (param == nullptr || !param->IsValid() || size < 0) {
103         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
104     }
105     uint32_t value = static_cast<uint32_t>(size);
106     if (!((value != 0) && ((value & RdbStoreConfig::DB_INVALID_CRYPTO_PAGE_SIZE_MASK) == 0) &&
107         (value & (value - 1)) == 0)) {
108         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
109     }
110     param->cryptoParam.cryptoPageSize = value;
111     return OH_Rdb_ErrCode::RDB_OK;
112 }