Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1 | /* |
| 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 Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 18 | * 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 Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 24 | * Obviously, the filesystem does not include the last 16 kbytes |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 25 | * of the partition if the crypt_mnt_ftr lives at the end of the |
| 26 | * partition. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 27 | */ |
| 28 | |
Paul Lawrence | 2f32cda | 2015-05-05 14:28:25 -0700 | [diff] [blame^] | 29 | #include <stdbool.h> |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
| 31 | |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 32 | /* The current cryptfs version */ |
| 33 | #define CURRENT_MAJOR_VERSION 1 |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 34 | #define CURRENT_MINOR_VERSION 3 |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 35 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 36 | #define CRYPT_FOOTER_OFFSET 0x4000 |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 37 | #define CRYPT_FOOTER_TO_PERSIST_OFFSET 0x1000 |
| 38 | #define CRYPT_PERSIST_DATA_SIZE 0x1000 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 39 | |
| 40 | #define MAX_CRYPTO_TYPE_NAME_LEN 64 |
| 41 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 42 | #define MAX_KEY_LEN 48 |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 43 | #define SALT_LEN 16 |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 44 | #define SCRYPT_LEN 32 |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 45 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 46 | /* definitions of flags in the structure below */ |
| 47 | #define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */ |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 48 | #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 Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 53 | #define CRYPT_DATA_CORRUPT 0x8 /* Set when encryption is fine, but the |
| 54 | underlying volume is corrupt */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 55 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 56 | /* Allowed values for type in the structure below */ |
| 57 | #define CRYPT_TYPE_PASSWORD 0 /* master_key is encrypted with a password |
| 58 | * Must be zero to be compatible with pre-L |
| 59 | * devices where type is always password.*/ |
| 60 | #define CRYPT_TYPE_DEFAULT 1 /* master_key is encrypted with default |
| 61 | * password */ |
| 62 | #define CRYPT_TYPE_PATTERN 2 /* master_key is encrypted with a pattern */ |
| 63 | #define CRYPT_TYPE_PIN 3 /* master_key is encrypted with a pin */ |
| 64 | #define CRYPT_TYPE_MAX_TYPE 3 /* type cannot be larger than this value */ |
| 65 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 66 | #define CRYPT_MNT_MAGIC 0xD0B5B1C4 |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 67 | #define PERSIST_DATA_MAGIC 0xE950CD44 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 68 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 69 | #define SCRYPT_PROP "ro.crypto.scrypt_params" |
| 70 | #define SCRYPT_DEFAULTS { 15, 3, 1 } |
| 71 | |
| 72 | /* Key Derivation Function algorithms */ |
| 73 | #define KDF_PBKDF2 1 |
| 74 | #define KDF_SCRYPT 2 |
Paul Lawrence | db3730c | 2015-02-03 13:08:10 -0800 | [diff] [blame] | 75 | /* Algorithms 3 & 4 deprecated before shipping outside of google, so removed */ |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 76 | #define KDF_SCRYPT_KEYMASTER 5 |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 77 | |
| 78 | /* Maximum allowed keymaster blob size. */ |
| 79 | #define KEYMASTER_BLOB_SIZE 2048 |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 80 | |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 81 | /* __le32 and __le16 defined in system/extras/ext4_utils/ext4_utils.h */ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 82 | #define __le8 unsigned char |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 83 | |
Adam Langley | 41405bb | 2015-01-22 16:45:28 -0800 | [diff] [blame] | 84 | #if !defined(SHA256_DIGEST_LENGTH) |
| 85 | #define SHA256_DIGEST_LENGTH 32 |
| 86 | #endif |
| 87 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 88 | struct crypt_mnt_ftr { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 89 | __le32 magic; /* See above */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 90 | __le16 major_version; |
| 91 | __le16 minor_version; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 92 | __le32 ftr_size; /* in bytes, not including key following */ |
| 93 | __le32 flags; /* See above */ |
| 94 | __le32 keysize; /* in bytes */ |
| 95 | __le32 crypt_type; /* how master_key is encrypted. Must be a |
| 96 | * CRYPT_TYPE_XXX value */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 97 | __le64 fs_size; /* Size of the encrypted fs, in 512 byte sectors */ |
| 98 | __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 99 | mount, set to 0 on successful mount */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 100 | unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 101 | needed to decrypt this |
| 102 | partition, null terminated */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 103 | __le32 spare2; /* ignored */ |
| 104 | unsigned char master_key[MAX_KEY_LEN]; /* The encrypted key for decrypting the filesystem */ |
| 105 | unsigned char salt[SALT_LEN]; /* The salt used for this encryption */ |
| 106 | __le64 persist_data_offset[2]; /* Absolute offset to both copies of crypt_persist_data |
| 107 | * on device with that info, either the footer of the |
| 108 | * real_blkdevice or the metadata partition. */ |
| 109 | |
| 110 | __le32 persist_data_size; /* The number of bytes allocated to each copy of the |
| 111 | * persistent data table*/ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 112 | |
| 113 | __le8 kdf_type; /* The key derivation function used. */ |
| 114 | |
| 115 | /* scrypt parameters. See www.tarsnap.com/scrypt/scrypt.pdf */ |
| 116 | __le8 N_factor; /* (1 << N) */ |
| 117 | __le8 r_factor; /* (1 << r) */ |
| 118 | __le8 p_factor; /* (1 << p) */ |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 119 | __le64 encrypted_upto; /* If we are in state CRYPT_ENCRYPTION_IN_PROGRESS and |
| 120 | we have to stop (e.g. power low) this is the last |
| 121 | encrypted 512 byte sector.*/ |
| 122 | __le8 hash_first_block[SHA256_DIGEST_LENGTH]; /* When CRYPT_ENCRYPTION_IN_PROGRESS |
| 123 | set, hash of first block, used |
| 124 | to validate before continuing*/ |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 125 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 126 | /* key_master key, used to sign the derived key which is then used to generate |
| 127 | * the intermediate key |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 128 | * This key should be used for no other purposes! We use this key to sign unpadded |
| 129 | * data, which is acceptable but only if the key is not reused elsewhere. */ |
| 130 | __le8 keymaster_blob[KEYMASTER_BLOB_SIZE]; |
| 131 | __le32 keymaster_blob_size; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 132 | |
| 133 | /* Store scrypt of salted intermediate key. When decryption fails, we can |
| 134 | check if this matches, and if it does, we know that the problem is with the |
| 135 | drive, and there is no point in asking the user for more passwords. |
| 136 | |
| 137 | Note that if any part of this structure is corrupt, this will not match and |
| 138 | we will continue to believe the user entered the wrong password. In that |
| 139 | case the only solution is for the user to enter a password enough times to |
| 140 | force a wipe. |
| 141 | |
| 142 | Note also that there is no need to worry about migration. If this data is |
| 143 | wrong, we simply won't recognise a right password, and will continue to |
| 144 | prompt. On the first password change, this value will be populated and |
| 145 | then we will be OK. |
| 146 | */ |
| 147 | unsigned char scrypted_intermediate_key[SCRYPT_LEN]; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | /* Persistant data that should be available before decryption. |
| 151 | * Things like airplane mode, locale and timezone are kept |
| 152 | * here and can be retrieved by the CryptKeeper UI to properly |
| 153 | * configure the phone before asking for the password |
| 154 | * This is only valid if the major and minor version above |
| 155 | * is set to 1.1 or higher. |
| 156 | * |
| 157 | * This is a 4K structure. There are 2 copies, and the code alternates |
| 158 | * writing one and then clearing the previous one. The reading |
| 159 | * code reads the first valid copy it finds, based on the magic number. |
| 160 | * The absolute offset to the first of the two copies is kept in rev 1.1 |
| 161 | * and higher crypt_mnt_ftr structures. |
| 162 | */ |
| 163 | struct crypt_persist_entry { |
| 164 | char key[PROPERTY_KEY_MAX]; |
| 165 | char val[PROPERTY_VALUE_MAX]; |
| 166 | }; |
| 167 | |
| 168 | /* Should be exactly 4K in size */ |
| 169 | struct crypt_persist_data { |
| 170 | __le32 persist_magic; |
| 171 | __le32 persist_valid_entries; |
| 172 | __le32 persist_spare[30]; |
| 173 | struct crypt_persist_entry persist_entry[0]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
JP Abgrall | 502dc74 | 2013-11-01 13:06:20 -0700 | [diff] [blame] | 176 | #define DATA_MNT_POINT "/data" |
| 177 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 178 | /* Return values for cryptfs_crypto_complete */ |
| 179 | #define CRYPTO_COMPLETE_NOT_ENCRYPTED 1 |
| 180 | #define CRYPTO_COMPLETE_ENCRYPTED 0 |
| 181 | #define CRYPTO_COMPLETE_BAD_METADATA -1 |
| 182 | #define CRYPTO_COMPLETE_PARTIAL -2 |
| 183 | #define CRYPTO_COMPLETE_INCONSISTENT -3 |
| 184 | #define CRYPTO_COMPLETE_CORRUPT -4 |
| 185 | |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 186 | /* Return values for cryptfs_enable_inplace*() */ |
| 187 | #define ENABLE_INPLACE_OK 0 |
| 188 | #define ENABLE_INPLACE_ERR_OTHER -1 |
| 189 | #define ENABLE_INPLACE_ERR_DEV -2 /* crypto_blkdev issue */ |
| 190 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 191 | /* Return values for cryptfs_getfield */ |
| 192 | #define CRYPTO_GETFIELD_OK 0 |
| 193 | #define CRYPTO_GETFIELD_ERROR_NO_FIELD -1 |
| 194 | #define CRYPTO_GETFIELD_ERROR_OTHER -2 |
| 195 | #define CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL -3 |
| 196 | |
| 197 | /* Return values for cryptfs_setfield */ |
| 198 | #define CRYPTO_SETFIELD_OK 0 |
| 199 | #define CRYPTO_SETFIELD_ERROR_OTHER -1 |
| 200 | #define CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG -2 |
| 201 | #define CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG -3 |
| 202 | |
| 203 | /* Return values for persist_del_key */ |
| 204 | #define PERSIST_DEL_KEY_OK 0 |
| 205 | #define PERSIST_DEL_KEY_ERROR_OTHER -1 |
| 206 | #define PERSIST_DEL_KEY_ERROR_NO_FIELD -2 |
| 207 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 208 | #ifdef __cplusplus |
| 209 | extern "C" { |
| 210 | #endif |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 211 | |
Paul Lawrence | 2f32cda | 2015-05-05 14:28:25 -0700 | [diff] [blame^] | 212 | int wait_and_unmount(const char *mountpoint, bool kill); |
| 213 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 214 | typedef int (*kdf_func)(const char *passwd, const unsigned char *salt, |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 215 | unsigned char *ikey, void *params); |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 216 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 217 | int cryptfs_crypto_complete(void); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 218 | int cryptfs_check_passwd(char *pw); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 219 | int cryptfs_verify_passwd(char *newpw); |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 220 | int cryptfs_restart(void); |
Paul Lawrence | 45f1053 | 2014-04-04 18:11:56 +0000 | [diff] [blame] | 221 | int cryptfs_enable(char *flag, int type, char *passwd, int allow_reboot); |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 222 | int cryptfs_changepw(int type, const char *newpw); |
| 223 | int cryptfs_enable_default(char *flag, int allow_reboot); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 224 | int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, |
| 225 | const unsigned char* key, int keysize, char* out_crypto_blkdev); |
| 226 | int cryptfs_revert_ext_volume(const char* label); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 227 | int cryptfs_enable_file(); |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 228 | int cryptfs_getfield(const char *fieldname, char *value, int len); |
| 229 | int cryptfs_setfield(const char *fieldname, const char *value); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 230 | int cryptfs_mount_default_encrypted(void); |
| 231 | int cryptfs_get_password_type(void); |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 232 | const char* cryptfs_get_password(void); |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 233 | void cryptfs_clear_password(void); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 234 | |
| 235 | // Functions for file encryption to use to inherit our encryption logic |
| 236 | int cryptfs_create_default_ftr(struct crypt_mnt_ftr* ftr, int key_length); |
| 237 | int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password, |
| 238 | unsigned char* master_key); |
| 239 | int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password, |
| 240 | const unsigned char* master_key); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 241 | #ifdef __cplusplus |
| 242 | } |
| 243 | #endif |