• 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 KEY_CONTROL_H
16 #define KEY_CONTROL_H
17 
18 #include <linux/keyctl.h>
19 #include <linux/version.h>
20 #if ((defined LINUX_VERSION_CODE ) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0))
21 #include <linux/fscrypt.h>
22 #define SUPPORT_FSCRYPT_V2
23 #else
24 #include "fscrypt_uapi.h"
25 #endif
26 
27 #include <stdbool.h>
28 #include <stdint.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #define POLICY_BUF_SIZE (100)
35 
36 static const uint32_t FSCRYPT_CE_CLASS = 1;
37 static const uint32_t FSCRYPT_SDP_ECE_CLASS = 2;
38 static const uint32_t FSCRYPT_SDP_SECE_CLASS = 3;
39 static const uint32_t FSCRYPT_DPS_CLASS = 4;
40 
41 #define EXT4_AES_256_XTS_KEY_SIZE 64
42 #define EXT4_ENCRYPTION_MODE_ECDH 3
43 #define EXT4_ENCRYPTION_MODE_AES_256_XTS 1
44 #define SECE_PUB_KEY_LEN 64
45 #define SECE_PRI_KEY_LEN 32
46 #define EXT4_AES_256_XTS_KEY_SIZE_TO_KEYRING 32
47 #define EXT4_AES_256_XTS_APP_KEY_SIZE_TO_KEYRING 8
48 
49 enum {
50     FSCRYPT_INVALID = 0,
51     FSCRYPT_V1 = 1,
52     FSCRYPT_V2 = 2,
53     FSCRYPT_INVALID_REALPATH = 3,
54     FSCRYPT_INVALID_OPEN = 4,
55     FSCRYPT_INVALID_NOT_SUPPORT = 5,
56     FSCRYPT_INVALID_UNEXPECTED = 6,
57 };
58 
59 #define EXT4_MAX_KEY_SIZE 64
60 #pragma pack(push, 1)
61 struct EncryptionKeySdp {
62     uint32_t version;
63     uint32_t sdpClass; //ECE || SECE
64     uint32_t mode; //xts or ecdh
65     char raw[EXT4_MAX_KEY_SIZE];
66     uint32_t size;
67     char pubkey[EXT4_MAX_KEY_SIZE];
68     uint32_t pubkeySize;
69 };
70 #pragma pack(pop)
71 
72 #define FSCRYPT_MAX_KEY_SIZE 64
73 #pragma pack(push, 1)
74 struct EncryptAsdpKey {
75     uint32_t version;
76     uint8_t raw[FSCRYPT_MAX_KEY_SIZE];
77     uint32_t size;
78 };
79 #pragma pack(pop)
80 
81 union FscryptPolicy {
82     struct fscrypt_policy_v1 v1;
83 #ifdef SUPPORT_FSCRYPT_V2
84     struct fscrypt_policy_v2 v2;
85 #endif
86 };
87 
88 typedef unsigned char uint8_t;
89 typedef int key_serial_t;
90 
91 static const char *FSCRYPT_POLICY_KEY = "fscrypt.policy.config";
92 static const char *PATH_FSCRYPT_VER = "/fscrypt_version";
93 
94 key_serial_t KeyCtrlGetKeyringId(key_serial_t id, int create);
95 key_serial_t KeyCtrlAddKey(const char *type, const char *description,
96     const key_serial_t ringId);
97 key_serial_t KeyCtrlAddKeyEx(const char *type, const char *description,
98     struct fscrypt_key *fsKey, const key_serial_t ringId);
99 key_serial_t KeyCtrlAddKeySdp(const char *type, const char *description,
100                               struct EncryptionKeySdp *fsKey, const key_serial_t ringId);
101 key_serial_t KeyCtrlAddAppAsdpKey(const char *type,
102                                   const char *description,
103                                   struct EncryptAsdpKey *fsKey,
104                                   const key_serial_t ringId);
105 long KeyCtrlSearch(key_serial_t ringId, const char *type, const char *description,
106     key_serial_t destRingId);
107 long KeyCtrlUnlink(key_serial_t key, key_serial_t keyring);
108 
109 #ifdef SUPPORT_FSCRYPT_V2
110 bool KeyCtrlInstallKey(const char *mnt, struct fscrypt_add_key_arg *arg);
111 bool KeyCtrlRemoveKey(const char *mnt, struct fscrypt_remove_key_arg *arg);
112 bool KeyCtrlGetKeyStatus(const char *mnt, struct fscrypt_get_key_status_arg *arg);
113 bool KeyCtrlGetPolicyEx(const char *path, struct fscrypt_get_policy_ex_arg *policy);
114 #endif
115 
116 bool KeyCtrlSetPolicy(const char *path, union FscryptPolicy *policy);
117 bool KeyCtrlGetPolicy(const char *path, struct fscrypt_policy *policy);
118 
119 uint8_t KeyCtrlGetFscryptVersion(const char *mnt);
120 uint8_t KeyCtrlLoadVersion(const char *keyPath);
121 
122 bool KeyCtrlHasFscryptSyspara(void);
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif