• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVICES_SERVICE_CRYPTO_CRYPTO_MANAGER_H
16 #define OHOS_DISTRIBUTED_DATA_SERVICES_SERVICE_CRYPTO_CRYPTO_MANAGER_H
17 
18 #include <cstdint>
19 #include <mutex>
20 #include <vector>
21 #include "metadata/secret_key_meta_data.h"
22 #include "metadata/store_meta_data.h"
23 #include "visibility.h"
24 
25 namespace OHOS::DistributedData {
26 class API_EXPORT CryptoManager {
27 public:
28     static constexpr const char *DEFAULT_USER = "0";
29 
30     enum SecretKeyType {
31         LOCAL_SECRET_KEY,
32         CLONE_SECRET_KEY,
33     };
34 
35     enum Area : int32_t {
36         EL0,
37         EL1,
38         EL2,
39         EL3,
40         EL4,
41         EL5,
42     };
43 
44     enum ErrCode : int32_t {
45         SUCCESS,
46         NOT_EXIST,
47         ERROR,
48     };
49 
50     struct CryptoParams {
51         int32_t area = Area::EL1;
52         std::string userId = DEFAULT_USER;
53         std::vector<uint8_t> keyAlias;
54         std::vector<uint8_t> nonce;
55     };
56 
57     struct ParamConfig {
58         uint32_t purpose;
59         uint32_t storageLevel;
60         std::string userId;
61         std::vector<uint8_t> nonce;
62         std::vector<uint8_t> aadValue;
63     };
64 
65     static CryptoManager &GetInstance();
66 
67     int32_t GenerateRootKey();
68     int32_t CheckRootKey();
69 
70     std::vector<uint8_t> Encrypt(const std::vector<uint8_t> &password, CryptoParams &encryptParams);
71     std::vector<uint8_t> Decrypt(const std::vector<uint8_t> &source, CryptoParams &decryptParams);
72     void UpdateSecretMeta(const std::vector<uint8_t> &password, const StoreMetaData &metaData,
73         const std::string &metaKey, SecretKeyMetaData &secretKey);
74 
75     bool ImportKey(const std::vector<uint8_t> &key, const std::vector<uint8_t> &keyAlias);
76     bool DeleteKey(const std::vector<uint8_t> &keyAlias);
77 
78 private:
79     CryptoManager();
80     ~CryptoManager();
81 
82     uint32_t GetStorageLevel(int32_t area);
83     int32_t GenerateRootKey(uint32_t storageLevel, const std::string &userId);
84     int32_t CheckRootKey(uint32_t storageLevel, const std::string &userId);
85     int32_t PrepareRootKey(uint32_t storageLevel, const std::string &userId);
86 
87     std::mutex mutex_;
88     std::vector<uint8_t> vecRootKeyAlias_{};
89     std::vector<uint8_t> vecNonce_{};
90     std::vector<uint8_t> vecAad_{};
91 };
92 } // namespace OHOS::DistributedData
93 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_SERVICE_CRYPTO_CRYPTO_MANAGER_H