1 /* 2 * Copyright (c) 2022 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 #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_SECURITY_MANAGER_H 16 #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_SECURITY_MANAGER_H 17 #include "types.h" 18 #include "types_export.h" 19 namespace OHOS::DistributedKv { 20 class SecurityManager { 21 public: 22 using DBPassword = DistributedDB::CipherPassword; 23 static SecurityManager &GetInstance(); 24 DBPassword GetDBPassword(const std::string &name, const std::string &path, bool needCreate = false); 25 bool SaveDBPassword(const std::string &name, const std::string &path, const DBPassword &key); 26 void DelDBPassword(const std::string &name, const std::string &path); 27 void Init(); 28 29 private: 30 static constexpr const char *ROOT_KEY_ALIAS = "distributeddb_client_root_key"; 31 static constexpr const char *HKS_BLOB_TYPE_NONCE = "Z5s0Bo571KoqwIi6"; 32 static constexpr const char *HKS_BLOB_TYPE_AAD = "distributeddata_client"; 33 static constexpr int KEY_SIZE = 32; 34 35 SecurityManager(); 36 ~SecurityManager(); 37 std::vector<uint8_t> Random(int32_t len); 38 std::vector<uint8_t> LoadKeyFromFile(const std::string &name, const std::string &path); 39 bool SaveKeyToFile(const std::string &name, const std::string &path, std::vector<uint8_t> &key); 40 int32_t GenerateRootKey(); 41 int32_t CheckRootKey(); 42 std::function<void()> Retry(); 43 std::vector<uint8_t> Encrypt(const std::vector<uint8_t> &key); 44 bool Decrypt(std::vector<uint8_t> &source, std::vector<uint8_t> &key); 45 46 std::vector<uint8_t> vecRootKeyAlias_{}; 47 std::vector<uint8_t> vecNonce_{}; 48 std::vector<uint8_t> vecAad_{}; 49 }; 50 } // namespace OHOS::DistributedKv 51 #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_SECURITY_MANAGER_H 52