1 /* 2 * Copyright (c) 2021 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 STORAGE_DAEMON_CRYPTO_BASEKEY_H 16 #define STORAGE_DAEMON_CRYPTO_BASEKEY_H 17 18 #include <string> 19 20 #include "key_utils.h" 21 #include "key_ctrl.h" 22 23 namespace OHOS { 24 namespace StorageDaemon { 25 class BaseKey { 26 public: 27 BaseKey() = delete; 28 BaseKey(std::string dir, uint8_t keyLen = CRYPTO_AES_256_XTS_KEY_SIZE); 29 ~BaseKey() = default; 30 31 bool InitKey(); 32 bool StoreKey(const UserAuth &auth); 33 bool UpdateKey(const std::string &keypath = ""); 34 bool RestoreKey(const UserAuth &auth); 35 virtual bool ActiveKey(const std::string &mnt = MNT_DATA) = 0; 36 virtual bool InactiveKey(const std::string &mnt = MNT_DATA) = 0; 37 bool ClearKey(const std::string &mnt = MNT_DATA); 38 39 KeyInfo keyInfo_; GetDir()40 std::string GetDir() const 41 { 42 return dir_; 43 } 44 45 protected: 46 static bool SaveKeyBlob(const KeyBlob &blob, const std::string &path); 47 std::string dir_ {}; 48 49 private: 50 bool DoStoreKey(const UserAuth &auth); 51 bool DoRestoreKey(const UserAuth &auth, const std::string &keypath); 52 static bool GenerateAndSaveKeyBlob(KeyBlob &blob, const std::string &path, const uint32_t size); 53 static bool GenerateKeyBlob(KeyBlob &blob, const uint32_t size); 54 static bool LoadKeyBlob(KeyBlob &blob, const std::string &path, const uint32_t size); 55 bool EncryptKey(const UserAuth &auth); 56 bool DecryptKey(const UserAuth &auth); 57 bool RemoveAlias(const std::string& keypath); 58 int GetCandidateVersion() const; 59 std::string GetCandidateDir() const; 60 std::string GetNextCandidateDir() const; 61 62 KeyContext keyContext_ {}; 63 uint8_t keyLen_ {}; 64 }; 65 } // namespace StorageDaemon 66 } // namespace OHOS 67 68 #endif // STORAGE_DAEMON_CRYPTO_BASEKEY_H 69