blob: 51fe7016d8df199fb9ddc9fe7a882bce0f4ece1d [file] [log] [blame]
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001/*
2 * Copyright (C) 2010 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/* This structure starts 16,384 bytes before the end of a hardware
Ken Sumrall160b4d62013-04-22 12:15:39 -070018 * partition that is encrypted, or in a separate partition. It's location
19 * is specified by a property set in init.<device>.rc.
20 * The structure allocates 48 bytes for a key, but the real key size is
21 * specified in the struct. Currently, the code is hardcoded to use 128
22 * bit keys.
23 * The fields after salt are only valid in rev 1.1 and later stuctures.
Ken Sumrall8f869aa2010-12-03 03:47:09 -080024 * Obviously, the filesystem does not include the last 16 kbytes
Ken Sumrall160b4d62013-04-22 12:15:39 -070025 * of the partition if the crypt_mnt_ftr lives at the end of the
26 * partition.
Ken Sumrall8f869aa2010-12-03 03:47:09 -080027 */
28
Paul Lawrence2f32cda2015-05-05 14:28:25 -070029#include <stdbool.h>
Ken Sumrall160b4d62013-04-22 12:15:39 -070030#include <cutils/properties.h>
31
Kenny Rootc96a5f82013-06-14 12:08:28 -070032/* The current cryptfs version */
33#define CURRENT_MAJOR_VERSION 1
Paul Lawrencef4faa572014-01-29 13:31:03 -080034#define CURRENT_MINOR_VERSION 3
Kenny Rootc96a5f82013-06-14 12:08:28 -070035
Ken Sumrall8f869aa2010-12-03 03:47:09 -080036#define CRYPT_FOOTER_OFFSET 0x4000
Ken Sumrall160b4d62013-04-22 12:15:39 -070037#define CRYPT_FOOTER_TO_PERSIST_OFFSET 0x1000
38#define CRYPT_PERSIST_DATA_SIZE 0x1000
Ken Sumrall8f869aa2010-12-03 03:47:09 -080039
40#define MAX_CRYPTO_TYPE_NAME_LEN 64
41
Ken Sumrall160b4d62013-04-22 12:15:39 -070042#define MAX_KEY_LEN 48
Ken Sumralle8744072011-01-18 22:01:55 -080043#define SALT_LEN 16
Paul Lawrenced0c7b172014-08-08 14:28:10 -070044#define SCRYPT_LEN 32
Ken Sumralle8744072011-01-18 22:01:55 -080045
Ken Sumrall8f869aa2010-12-03 03:47:09 -080046/* definitions of flags in the structure below */
47#define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */
Paul Lawrence6bfed202014-07-28 12:47:22 -070048#define CRYPT_ENCRYPTION_IN_PROGRESS 0x2 /* Encryption partially completed,
49 encrypted_upto valid*/
50#define CRYPT_INCONSISTENT_STATE 0x4 /* Set when starting encryption, clear when
51 exit cleanly, either through success or
52 correctly marked partial encryption */
Paul Lawrence74f29f12014-08-28 15:54:10 -070053#define CRYPT_DATA_CORRUPT 0x8 /* Set when encryption is fine, but the
54 underlying volume is corrupt */
Dinesh K Garg2d1170a2015-10-16 19:27:15 -070055#ifdef CONFIG_HW_DISK_ENCRYPTION
56/* This flag is used to transition from L->M upgrade. L release passed
57 * a byte for every nible of user password while M release is passing
58 * ascii value of user password.
59 * Random flag value is chosen so that it does not conflict with other use cases
60 */
61#define CRYPT_ASCII_PASSWORD_UPDATED 0x1000
62#endif
Ken Sumrall8f869aa2010-12-03 03:47:09 -080063
Paul Lawrencef4faa572014-01-29 13:31:03 -080064/* Allowed values for type in the structure below */
65#define CRYPT_TYPE_PASSWORD 0 /* master_key is encrypted with a password
66 * Must be zero to be compatible with pre-L
67 * devices where type is always password.*/
68#define CRYPT_TYPE_DEFAULT 1 /* master_key is encrypted with default
69 * password */
70#define CRYPT_TYPE_PATTERN 2 /* master_key is encrypted with a pattern */
71#define CRYPT_TYPE_PIN 3 /* master_key is encrypted with a pin */
72#define CRYPT_TYPE_MAX_TYPE 3 /* type cannot be larger than this value */
73
Ken Sumrall8f869aa2010-12-03 03:47:09 -080074#define CRYPT_MNT_MAGIC 0xD0B5B1C4
Ken Sumrall160b4d62013-04-22 12:15:39 -070075#define PERSIST_DATA_MAGIC 0xE950CD44
Ken Sumrall8f869aa2010-12-03 03:47:09 -080076
Kenny Rootc4c70f12013-06-14 12:11:38 -070077#define SCRYPT_PROP "ro.crypto.scrypt_params"
78#define SCRYPT_DEFAULTS { 15, 3, 1 }
79
80/* Key Derivation Function algorithms */
81#define KDF_PBKDF2 1
82#define KDF_SCRYPT 2
Paul Lawrencedb3730c2015-02-03 13:08:10 -080083/* Algorithms 3 & 4 deprecated before shipping outside of google, so removed */
Shawn Willdene17a9c42014-09-08 13:04:08 -060084#define KDF_SCRYPT_KEYMASTER 5
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070085
86/* Maximum allowed keymaster blob size. */
87#define KEYMASTER_BLOB_SIZE 2048
Kenny Rootc4c70f12013-06-14 12:11:38 -070088
Mark Salyzyn3e971272014-01-21 13:27:04 -080089/* __le32 and __le16 defined in system/extras/ext4_utils/ext4_utils.h */
Kenny Rootc4c70f12013-06-14 12:11:38 -070090#define __le8 unsigned char
Ken Sumrall8f869aa2010-12-03 03:47:09 -080091
Adam Langley41405bb2015-01-22 16:45:28 -080092#if !defined(SHA256_DIGEST_LENGTH)
93#define SHA256_DIGEST_LENGTH 32
94#endif
95
Ken Sumrall8f869aa2010-12-03 03:47:09 -080096struct crypt_mnt_ftr {
Paul Lawrencef4faa572014-01-29 13:31:03 -080097 __le32 magic; /* See above */
Ken Sumrall8f869aa2010-12-03 03:47:09 -080098 __le16 major_version;
99 __le16 minor_version;
Paul Lawrencef4faa572014-01-29 13:31:03 -0800100 __le32 ftr_size; /* in bytes, not including key following */
101 __le32 flags; /* See above */
102 __le32 keysize; /* in bytes */
103 __le32 crypt_type; /* how master_key is encrypted. Must be a
104 * CRYPT_TYPE_XXX value */
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800105 __le64 fs_size; /* Size of the encrypted fs, in 512 byte sectors */
106 __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and
Paul Lawrence87999172014-02-20 12:21:31 -0800107 mount, set to 0 on successful mount */
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800108 unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption
Paul Lawrence87999172014-02-20 12:21:31 -0800109 needed to decrypt this
110 partition, null terminated */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700111 __le32 spare2; /* ignored */
112 unsigned char master_key[MAX_KEY_LEN]; /* The encrypted key for decrypting the filesystem */
113 unsigned char salt[SALT_LEN]; /* The salt used for this encryption */
114 __le64 persist_data_offset[2]; /* Absolute offset to both copies of crypt_persist_data
115 * on device with that info, either the footer of the
116 * real_blkdevice or the metadata partition. */
117
118 __le32 persist_data_size; /* The number of bytes allocated to each copy of the
119 * persistent data table*/
Kenny Rootc4c70f12013-06-14 12:11:38 -0700120
121 __le8 kdf_type; /* The key derivation function used. */
122
123 /* scrypt parameters. See www.tarsnap.com/scrypt/scrypt.pdf */
124 __le8 N_factor; /* (1 << N) */
125 __le8 r_factor; /* (1 << r) */
126 __le8 p_factor; /* (1 << p) */
Paul Lawrence87999172014-02-20 12:21:31 -0800127 __le64 encrypted_upto; /* If we are in state CRYPT_ENCRYPTION_IN_PROGRESS and
128 we have to stop (e.g. power low) this is the last
129 encrypted 512 byte sector.*/
130 __le8 hash_first_block[SHA256_DIGEST_LENGTH]; /* When CRYPT_ENCRYPTION_IN_PROGRESS
131 set, hash of first block, used
132 to validate before continuing*/
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700133
Paul Lawrenced0c7b172014-08-08 14:28:10 -0700134 /* key_master key, used to sign the derived key which is then used to generate
135 * the intermediate key
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700136 * This key should be used for no other purposes! We use this key to sign unpadded
137 * data, which is acceptable but only if the key is not reused elsewhere. */
138 __le8 keymaster_blob[KEYMASTER_BLOB_SIZE];
139 __le32 keymaster_blob_size;
Paul Lawrenced0c7b172014-08-08 14:28:10 -0700140
141 /* Store scrypt of salted intermediate key. When decryption fails, we can
142 check if this matches, and if it does, we know that the problem is with the
143 drive, and there is no point in asking the user for more passwords.
144
145 Note that if any part of this structure is corrupt, this will not match and
146 we will continue to believe the user entered the wrong password. In that
147 case the only solution is for the user to enter a password enough times to
148 force a wipe.
149
150 Note also that there is no need to worry about migration. If this data is
151 wrong, we simply won't recognise a right password, and will continue to
152 prompt. On the first password change, this value will be populated and
153 then we will be OK.
154 */
155 unsigned char scrypted_intermediate_key[SCRYPT_LEN];
Ken Sumrall160b4d62013-04-22 12:15:39 -0700156};
157
158/* Persistant data that should be available before decryption.
159 * Things like airplane mode, locale and timezone are kept
160 * here and can be retrieved by the CryptKeeper UI to properly
161 * configure the phone before asking for the password
162 * This is only valid if the major and minor version above
163 * is set to 1.1 or higher.
164 *
165 * This is a 4K structure. There are 2 copies, and the code alternates
166 * writing one and then clearing the previous one. The reading
167 * code reads the first valid copy it finds, based on the magic number.
168 * The absolute offset to the first of the two copies is kept in rev 1.1
169 * and higher crypt_mnt_ftr structures.
170 */
171struct crypt_persist_entry {
172 char key[PROPERTY_KEY_MAX];
173 char val[PROPERTY_VALUE_MAX];
174};
175
176/* Should be exactly 4K in size */
177struct crypt_persist_data {
178 __le32 persist_magic;
179 __le32 persist_valid_entries;
180 __le32 persist_spare[30];
181 struct crypt_persist_entry persist_entry[0];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800182};
183
JP Abgrall502dc742013-11-01 13:06:20 -0700184#define DATA_MNT_POINT "/data"
185
Paul Lawrence74f29f12014-08-28 15:54:10 -0700186/* Return values for cryptfs_crypto_complete */
Amit Blay3a713202015-08-31 10:16:04 +0300187#define CRYPTO_COMPLETE_ENCRYPTED_MDTP_ACTIVATED 2
188#define CRYPTO_COMPLETE_NOT_ENCRYPTED 1
189#define CRYPTO_COMPLETE_ENCRYPTED 0
190#define CRYPTO_COMPLETE_BAD_METADATA -1
191#define CRYPTO_COMPLETE_PARTIAL -2
192#define CRYPTO_COMPLETE_INCONSISTENT -3
193#define CRYPTO_COMPLETE_CORRUPT -4
194#define CRYPTO_COMPLETE_ERROR_MDTP_ACTIVATED -5
195
Paul Lawrence74f29f12014-08-28 15:54:10 -0700196
JP Abgrall7fc1de82014-10-10 18:43:41 -0700197/* Return values for cryptfs_enable_inplace*() */
198#define ENABLE_INPLACE_OK 0
199#define ENABLE_INPLACE_ERR_OTHER -1
200#define ENABLE_INPLACE_ERR_DEV -2 /* crypto_blkdev issue */
201
Rubin Xu85c01f92014-10-13 12:49:54 +0100202/* Return values for cryptfs_getfield */
203#define CRYPTO_GETFIELD_OK 0
204#define CRYPTO_GETFIELD_ERROR_NO_FIELD -1
205#define CRYPTO_GETFIELD_ERROR_OTHER -2
206#define CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL -3
207
208/* Return values for cryptfs_setfield */
209#define CRYPTO_SETFIELD_OK 0
210#define CRYPTO_SETFIELD_ERROR_OTHER -1
211#define CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG -2
212#define CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG -3
213
214/* Return values for persist_del_key */
215#define PERSIST_DEL_KEY_OK 0
216#define PERSIST_DEL_KEY_ERROR_OTHER -1
217#define PERSIST_DEL_KEY_ERROR_NO_FIELD -2
218
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800219#ifdef __cplusplus
220extern "C" {
221#endif
Kenny Rootc4c70f12013-06-14 12:11:38 -0700222
Paul Lawrence2f32cda2015-05-05 14:28:25 -0700223 int wait_and_unmount(const char *mountpoint, bool kill);
224
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700225 typedef int (*kdf_func)(const char *passwd, const unsigned char *salt,
Paul Lawrence13486032014-02-03 13:28:11 -0800226 unsigned char *ikey, void *params);
Kenny Rootc4c70f12013-06-14 12:11:38 -0700227
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800228 int cryptfs_crypto_complete(void);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800229 int cryptfs_check_passwd(char *pw);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700230 int cryptfs_verify_passwd(char *newpw);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800231 int cryptfs_restart(void);
Paul Lawrence569649f2015-09-09 12:13:00 -0700232 int cryptfs_enable(char *flag, int type, char *passwd, int no_ui);
Dinesh K Garga7219c62015-08-06 16:31:16 -0700233 int cryptfs_changepw(int type, const char *currentpw, const char *newpw);
Paul Lawrence569649f2015-09-09 12:13:00 -0700234 int cryptfs_enable_default(char *flag, int no_ui);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700235 int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
236 const unsigned char* key, int keysize, char* out_crypto_blkdev);
237 int cryptfs_revert_ext_volume(const char* label);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000238 int cryptfs_enable_file();
Rubin Xu85c01f92014-10-13 12:49:54 +0100239 int cryptfs_getfield(const char *fieldname, char *value, int len);
240 int cryptfs_setfield(const char *fieldname, const char *value);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800241 int cryptfs_mount_default_encrypted(void);
242 int cryptfs_get_password_type(void);
Paul Lawrence05335c32015-03-05 09:46:23 -0800243 const char* cryptfs_get_password(void);
Paul Lawrence399317e2014-03-10 13:20:50 -0700244 void cryptfs_clear_password(void);
Paul Lawrence731a7a22015-04-28 22:14:15 +0000245
246 // Functions for file encryption to use to inherit our encryption logic
247 int cryptfs_create_default_ftr(struct crypt_mnt_ftr* ftr, int key_length);
248 int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password,
249 unsigned char* master_key);
250 int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
251 const unsigned char* master_key);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800252#ifdef __cplusplus
253}
254#endif