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 fscrypt_is_native();
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 
39     // Ensure that "version" is not valid on creation and so must be explicitly set
EncryptionOptionsEncryptionOptions40     EncryptionOptions() : version(0) {}
41 };
42 
43 struct EncryptionPolicy {
44     EncryptionOptions options;
45     std::string key_raw_ref;
46 };
47 
48 void BytesToHex(const std::string& bytes, std::string* hex);
49 
50 unsigned int GetFirstApiLevel();
51 
52 bool OptionsToString(const EncryptionOptions& options, std::string* options_string);
53 
54 bool OptionsToStringForApiLevel(unsigned int first_api_level, const EncryptionOptions& options,
55                                 std::string* options_string);
56 
57 bool ParseOptions(const std::string& options_string, EncryptionOptions* options);
58 
59 bool ParseOptionsForApiLevel(unsigned int first_api_level, const std::string& options_string,
60                              EncryptionOptions* options);
61 
62 bool EnsurePolicy(const EncryptionPolicy& policy, const std::string& directory);
63 
64 inline bool operator==(const EncryptionOptions& lhs, const EncryptionOptions& rhs) {
65     return (lhs.version == rhs.version) && (lhs.contents_mode == rhs.contents_mode) &&
66              (lhs.filenames_mode == rhs.filenames_mode) && (lhs.flags == rhs.flags) &&
67              (lhs.use_hw_wrapped_key == rhs.use_hw_wrapped_key);
68 }
69 
70 inline bool operator!=(const EncryptionOptions& lhs, const EncryptionOptions& rhs) {
71     return !(lhs == rhs);
72 }
73 
74 inline bool operator==(const EncryptionPolicy& lhs, const EncryptionPolicy& rhs) {
75     return lhs.key_raw_ref == rhs.key_raw_ref && lhs.options == rhs.options;
76 }
77 
78 inline bool operator!=(const EncryptionPolicy& lhs, const EncryptionPolicy& rhs) {
79     return !(lhs == rhs);
80 }
81 
82 }  // namespace fscrypt
83 }  // namespace android
84 
85 #endif // _FSCRYPT_H_
86