blob: 5a3b4b20c18bcb143b15286dfbab3d750ded60a2 [file] [log] [blame]
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001/*
2 * Copyright (C) 2015 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/* TWRP NOTE: Kanged from system/extras/ext4_utils/ext4_crypt.cpp
18 * because policy_to_hex, e4crypt_policy_set, and e4crypt_policy_get
19 * are not exposed to be used. There was also a bug in e4crypt_policy_get
20 * that may or may not be fixed in the user's local repo:
21 * https://android.googlesource.com/platform/system/extras/+/30b93dd5715abcabd621235733733c0503f9c552
22 */
23
24#include "ext4_crypt.h"
Ethan Yonkerfefe5912017-09-30 22:22:13 -050025#include "ext4crypt_tar.h"
Ethan Yonker79f88bd2016-12-09 14:52:12 -060026
27#include <dirent.h>
28#include <errno.h>
29#include <string.h>
30#include <unistd.h>
31
32#include <fcntl.h>
33#include <asm/ioctl.h>
34#include <sys/syscall.h>
35#include <sys/stat.h>
36#include <sys/types.h>
37
38#include <android-base/file.h>
39#include <android-base/logging.h>
40#include <cutils/properties.h>
41
42#define XATTR_NAME_ENCRYPTION_POLICY "encryption.policy"
43#define EXT4_KEYREF_DELIMITER ((char)'.')
44
Ethan Yonker79f88bd2016-12-09 14:52:12 -060045#define EXT4_ENCRYPTION_MODE_AES_256_XTS 1
46#define EXT4_ENCRYPTION_MODE_AES_256_CTS 4
Ethan Yonkerfefe5912017-09-30 22:22:13 -050047#define EXT4_ENCRYPTION_MODE_AES_256_HEH 126
Ethan Yonker79f88bd2016-12-09 14:52:12 -060048#define EXT4_ENCRYPTION_MODE_PRIVATE 127
49
50static int encryption_mode = EXT4_ENCRYPTION_MODE_PRIVATE;
51
Ethan Yonker79f88bd2016-12-09 14:52:12 -060052#define HEX_LOOKUP "0123456789abcdef"
53
54extern "C" void policy_to_hex(const char* policy, char* hex) {
55 for (size_t i = 0, j = 0; i < EXT4_KEY_DESCRIPTOR_SIZE; i++) {
56 hex[j++] = HEX_LOOKUP[(policy[i] & 0xF0) >> 4];
57 hex[j++] = HEX_LOOKUP[policy[i] & 0x0F];
58 }
59 hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX - 1] = '\0';
60}
61
62extern "C" bool e4crypt_policy_set(const char *directory, const char *policy,
63 size_t policy_length, int contents_encryption_mode) {
64 if (contents_encryption_mode == 0)
65 contents_encryption_mode = encryption_mode;
66 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
67 printf("policy wrong length\n");
68 LOG(ERROR) << "Policy wrong length: " << policy_length;
69 return false;
70 }
71 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
72 if (fd == -1) {
73 printf("failed to open %s\n", directory);
74 PLOG(ERROR) << "Failed to open directory " << directory;
75 return false;
76 }
77
78 ext4_encryption_policy eep;
79 eep.version = 0;
80 eep.contents_encryption_mode = contents_encryption_mode;
81 eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
82 eep.flags = 0;
83 memcpy(eep.master_key_descriptor, policy, EXT4_KEY_DESCRIPTOR_SIZE);
84 if (ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &eep)) {
85 printf("failed to set policy for '%s' '%s'\n", directory, policy);
86 PLOG(ERROR) << "Failed to set encryption policy for " << directory;
87 close(fd);
88 return false;
89 }
90 close(fd);
91
92 char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
93 policy_to_hex(policy, policy_hex);
94 LOG(INFO) << "Policy for " << directory << " set to " << policy_hex;
95 return true;
96}
97
98extern "C" bool e4crypt_policy_get(const char *directory, char *policy,
99 size_t policy_length, int contents_encryption_mode) {
100 if (contents_encryption_mode == 0)
101 contents_encryption_mode = encryption_mode;
102 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
103 LOG(ERROR) << "Policy wrong length: " << policy_length;
104 return false;
105 }
106
107 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
108 if (fd == -1) {
109 PLOG(ERROR) << "Failed to open directory " << directory;
110 return false;
111 }
112
113 ext4_encryption_policy eep;
114 memset(&eep, 0, sizeof(ext4_encryption_policy));
115 if (ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &eep) != 0) {
116 PLOG(ERROR) << "Failed to get encryption policy for " << directory;
117 close(fd);
118 return false;
119 }
120 close(fd);
121
122 if ((eep.version != 0)
123 || (eep.contents_encryption_mode != contents_encryption_mode)
124 || (eep.filenames_encryption_mode != EXT4_ENCRYPTION_MODE_AES_256_CTS)
125 || (eep.flags != 0)) {
126 LOG(ERROR) << "Failed to find matching encryption policy for " << directory;
127 return false;
128 }
129 memcpy(policy, eep.master_key_descriptor, EXT4_KEY_DESCRIPTOR_SIZE);
130
131 return true;
132}
133
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500134extern "C" void e4crypt_policy_fill_default_struct(ext4_encryption_policy *eep) {
135 eep->version = 0;
136 eep->contents_encryption_mode = encryption_mode;
137 eep->filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
138 eep->flags = 0;
139 memset((void*)&eep->master_key_descriptor[0], 0, EXT4_KEY_DESCRIPTOR_SIZE);
140}
141
142extern "C" bool e4crypt_policy_set_struct(const char *directory, const ext4_encryption_policy *eep) {
143 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
144 if (fd == -1) {
145 printf("failed to open %s\n", directory);
146 PLOG(ERROR) << "Failed to open directory " << directory;
147 return false;
148 }
149 if (ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, eep)) {
150 printf("failed to set policy for '%s'\n", directory);
151 PLOG(ERROR) << "Failed to set encryption policy for " << directory;
152 close(fd);
153 return false;
154 }
155 close(fd);
156 return true;
157}
158
159extern "C" bool e4crypt_policy_get_struct(const char *directory, ext4_encryption_policy *eep) {
160 int fd = open(directory, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
161 if (fd == -1) {
162 printf("Failed to open '%s'\n", directory);
163 PLOG(ERROR) << "Failed to open directory " << directory;
164 return false;
165 }
166 memset(eep, 0, sizeof(ext4_encryption_policy));
167 if (ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, eep) != 0) {
168 PLOG(ERROR) << "Failed to get encryption policy for " << directory;
169 close(fd);
170 return false;
171 }
172 close(fd);
173 return true;
174}
175
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600176extern "C" bool e4crypt_set_mode() {
177 const char* mode_file = "/data/unencrypted/mode";
178 struct stat st;
179 if (stat(mode_file, &st) != 0 || st.st_size <= 0) {
180 printf("Invalid encryption mode file %s\n", mode_file);
181 return false;
182 }
183 size_t mode_size = st.st_size;
184 char contents_encryption_mode[mode_size + 1];
185 memset((void*)contents_encryption_mode, 0, mode_size + 1);
186 int fd = open(mode_file, O_RDONLY);
187 if (fd < 0) {
188 printf("error opening '%s': %s\n", mode_file, strerror(errno));
189 return false;
190 }
191 if (read(fd, contents_encryption_mode, mode_size) != mode_size) {
192 printf("read error on '%s': %s\n", mode_file, strerror(errno));
193 close(fd);
194 return false;
195 }
196 close(fd);
197 if (!strcmp(contents_encryption_mode, "software")) {
198 encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
199 } else if (!strcmp(contents_encryption_mode, "ice")) {
200 encryption_mode = EXT4_ENCRYPTION_MODE_PRIVATE;
201 } else {
202 printf("Invalid encryption mode '%s'\n", contents_encryption_mode);
203 return false;
204 }
205 printf("set encryption mode to %i\n", encryption_mode);
206 return true;
207}