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 
16 #ifndef STORAGE_DAEMON_CRYPTO_FBEX_X
17 #define STORAGE_DAEMON_CRYPTO_FBEX_X
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 namespace OHOS {
24 namespace StorageDaemon {
25 constexpr uint32_t USERID_GLOBAL_EL1 = 0;
26 constexpr uint32_t TYPE_EL1 = 0;
27 constexpr uint32_t TYPE_EL2 = 1;
28 constexpr uint32_t TYPE_EL3 = 3;
29 constexpr uint32_t TYPE_EL4 = 2;
30 constexpr uint32_t TYPE_EL5 = 6;
31 constexpr uint32_t TYPE_GLOBAL_EL1 = 4;
32 
33 constexpr uint32_t FBEX_IV_SIZE = 64;
34 constexpr uint32_t FBEX_KEYID_SIZE = 64;
35 constexpr uint32_t FBEX_E_BUFFER_SIZE = 64;
36 const uint32_t FBEX_UNSUPPORT_CODE = 0xfbe30203;
37 const uint32_t UNLOCK_STATUS = 0x2;
38 const int STORAGE_UNSUPPORT_CODE = 0;
39 const int SINGLE_ID_INDEX = 0;
40 const int DOUBLE_ID_INDEX = 1;
41 const int USER_ID_SIZE = 2;
42 
43 struct UserIdToFbeStr {
44     uint32_t userIds[USER_ID_SIZE];
45     int size = USER_ID_SIZE;
46 };
47 
48 class FBEX {
49 public:
50     static bool IsFBEXSupported();
51     static int InstallKeyToKernel(uint32_t userId, uint32_t type, uint8_t *iv, uint32_t size, uint8_t flag);
52     static int InstallDoubleDeKeyToKernel(UserIdToFbeStr &userIdToFbe, uint8_t *iv, uint32_t size, uint8_t flag);
53     static int UninstallOrLockUserKeyToKernel(uint32_t userId, uint32_t type, uint8_t *iv, uint32_t size, bool destroy);
54     static int LockScreenToKernel(uint32_t userId);
55     static int UnlockScreenToKernel(uint32_t userId, uint32_t type, uint8_t *iv, uint32_t size);
56     static int ReadESecretToKernel(UserIdToFbeStr &userIdToFbe, uint32_t status,
57                                    std::unique_ptr<uint8_t[]> &eBuffer, uint32_t length, bool &isFbeSupport);
58     static int WriteESecretToKernel(UserIdToFbeStr &userIdToFbe, uint32_t status, uint8_t *eBuffer, uint32_t length);
59     static bool IsMspReady();
60     static int GetStatus();
61     static int InstallEL5KeyToKernel(uint32_t userIdSingle, uint32_t userIdDouble, uint8_t flag,
62                                      bool &isSupport, bool &isNeedEncryptClassE);
63     static int DeleteClassEPinCode(uint32_t userIdSingle, uint32_t userIdDouble);
64     static int ChangePinCodeClassE(uint32_t userIdSingle, uint32_t userIdDouble, bool &isFbeSupport);
65     static int GenerateAppkey(UserIdToFbeStr &userIdToFbe, uint32_t hashId, std::unique_ptr<uint8_t[]> &keyId,
66                               uint32_t size);
67     static int LockUece(uint32_t userIdSingle, uint32_t userIdDouble, bool &isFbeSupport);
68 };
69 } // namespace StorageDaemon
70 } // namespace OHOS
71 
72 #endif // STORAGE_DAEMON_CRYPTO_FBEX_X
73