xref: /aosp_15_r20/system/extras/libfscrypt/include/fscrypt/fscrypt.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #ifndef _FSCRYPT_H_
18*288bf522SAndroid Build Coastguard Worker #define _FSCRYPT_H_
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <string>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker bool IsFbeEnabled();
23*288bf522SAndroid Build Coastguard Worker 
24*288bf522SAndroid Build Coastguard Worker static const char* fscrypt_unencrypted_folder = "/unencrypted";
25*288bf522SAndroid Build Coastguard Worker static const char* fscrypt_key_ref = "/unencrypted/ref";
26*288bf522SAndroid Build Coastguard Worker static const char* fscrypt_key_per_boot_ref = "/unencrypted/per_boot_ref";
27*288bf522SAndroid Build Coastguard Worker static const char* fscrypt_key_mode = "/unencrypted/mode";
28*288bf522SAndroid Build Coastguard Worker 
29*288bf522SAndroid Build Coastguard Worker namespace android {
30*288bf522SAndroid Build Coastguard Worker namespace fscrypt {
31*288bf522SAndroid Build Coastguard Worker 
32*288bf522SAndroid Build Coastguard Worker struct EncryptionOptions {
33*288bf522SAndroid Build Coastguard Worker     int version;
34*288bf522SAndroid Build Coastguard Worker     int contents_mode;
35*288bf522SAndroid Build Coastguard Worker     int filenames_mode;
36*288bf522SAndroid Build Coastguard Worker     int flags;
37*288bf522SAndroid Build Coastguard Worker     bool use_hw_wrapped_key;
38*288bf522SAndroid Build Coastguard Worker     bool dusize_4k;
39*288bf522SAndroid Build Coastguard Worker 
40*288bf522SAndroid Build Coastguard Worker     // Ensure that "version" is not valid on creation and so must be explicitly set
EncryptionOptionsEncryptionOptions41*288bf522SAndroid Build Coastguard Worker     EncryptionOptions() : version(0) {}
42*288bf522SAndroid Build Coastguard Worker };
43*288bf522SAndroid Build Coastguard Worker 
44*288bf522SAndroid Build Coastguard Worker struct EncryptionPolicy {
45*288bf522SAndroid Build Coastguard Worker     EncryptionOptions options;
46*288bf522SAndroid Build Coastguard Worker     std::string key_raw_ref;
47*288bf522SAndroid Build Coastguard Worker };
48*288bf522SAndroid Build Coastguard Worker 
49*288bf522SAndroid Build Coastguard Worker void BytesToHex(const std::string& bytes, std::string* hex);
50*288bf522SAndroid Build Coastguard Worker 
51*288bf522SAndroid Build Coastguard Worker unsigned int GetFirstApiLevel();
52*288bf522SAndroid Build Coastguard Worker 
53*288bf522SAndroid Build Coastguard Worker bool OptionsToString(const EncryptionOptions& options, std::string* options_string);
54*288bf522SAndroid Build Coastguard Worker 
55*288bf522SAndroid Build Coastguard Worker bool OptionsToStringForApiLevel(unsigned int first_api_level, const EncryptionOptions& options,
56*288bf522SAndroid Build Coastguard Worker                                 std::string* options_string);
57*288bf522SAndroid Build Coastguard Worker 
58*288bf522SAndroid Build Coastguard Worker bool ParseOptions(const std::string& options_string, EncryptionOptions* options);
59*288bf522SAndroid Build Coastguard Worker 
60*288bf522SAndroid Build Coastguard Worker bool ParseOptionsForApiLevel(unsigned int first_api_level, const std::string& options_string,
61*288bf522SAndroid Build Coastguard Worker                              EncryptionOptions* options);
62*288bf522SAndroid Build Coastguard Worker 
63*288bf522SAndroid Build Coastguard Worker bool EnsurePolicy(const EncryptionPolicy& policy, const std::string& directory);
64*288bf522SAndroid Build Coastguard Worker 
65*288bf522SAndroid Build Coastguard Worker inline bool operator==(const EncryptionOptions& lhs, const EncryptionOptions& rhs) {
66*288bf522SAndroid Build Coastguard Worker     return (lhs.version == rhs.version) && (lhs.contents_mode == rhs.contents_mode) &&
67*288bf522SAndroid Build Coastguard Worker            (lhs.filenames_mode == rhs.filenames_mode) && (lhs.flags == rhs.flags) &&
68*288bf522SAndroid Build Coastguard Worker            (lhs.use_hw_wrapped_key == rhs.use_hw_wrapped_key) && (lhs.dusize_4k == rhs.dusize_4k);
69*288bf522SAndroid Build Coastguard Worker }
70*288bf522SAndroid Build Coastguard Worker 
71*288bf522SAndroid Build Coastguard Worker inline bool operator!=(const EncryptionOptions& lhs, const EncryptionOptions& rhs) {
72*288bf522SAndroid Build Coastguard Worker     return !(lhs == rhs);
73*288bf522SAndroid Build Coastguard Worker }
74*288bf522SAndroid Build Coastguard Worker 
75*288bf522SAndroid Build Coastguard Worker inline bool operator==(const EncryptionPolicy& lhs, const EncryptionPolicy& rhs) {
76*288bf522SAndroid Build Coastguard Worker     return lhs.key_raw_ref == rhs.key_raw_ref && lhs.options == rhs.options;
77*288bf522SAndroid Build Coastguard Worker }
78*288bf522SAndroid Build Coastguard Worker 
79*288bf522SAndroid Build Coastguard Worker inline bool operator!=(const EncryptionPolicy& lhs, const EncryptionPolicy& rhs) {
80*288bf522SAndroid Build Coastguard Worker     return !(lhs == rhs);
81*288bf522SAndroid Build Coastguard Worker }
82*288bf522SAndroid Build Coastguard Worker 
83*288bf522SAndroid Build Coastguard Worker }  // namespace fscrypt
84*288bf522SAndroid Build Coastguard Worker }  // namespace android
85*288bf522SAndroid Build Coastguard Worker 
86*288bf522SAndroid Build Coastguard Worker #endif  // _FSCRYPT_H_
87