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