Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [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 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 17 | #ifndef ANDROID_VOLD_CRYPTFS_H |
| 18 | #define ANDROID_VOLD_CRYPTFS_H |
| 19 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 20 | /* This structure starts 16,384 bytes before the end of a hardware |
| 21 | * partition that is encrypted, or in a separate partition. It's location |
| 22 | * is specified by a property set in init.<device>.rc. |
| 23 | * The structure allocates 48 bytes for a key, but the real key size is |
| 24 | * specified in the struct. Currently, the code is hardcoded to use 128 |
| 25 | * bit keys. |
| 26 | * The fields after salt are only valid in rev 1.1 and later stuctures. |
| 27 | * Obviously, the filesystem does not include the last 16 kbytes |
| 28 | * of the partition if the crypt_mnt_ftr lives at the end of the |
| 29 | * partition. |
| 30 | */ |
| 31 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 32 | #include <stdbool.h> |
| 33 | #include <stdint.h> |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 35 | |
| 36 | /* The current cryptfs version */ |
| 37 | #define CURRENT_MAJOR_VERSION 1 |
| 38 | #define CURRENT_MINOR_VERSION 3 |
| 39 | |
| 40 | #define CRYPT_FOOTER_OFFSET 0x4000 |
| 41 | #define CRYPT_FOOTER_TO_PERSIST_OFFSET 0x1000 |
| 42 | #define CRYPT_PERSIST_DATA_SIZE 0x1000 |
| 43 | |
| 44 | #define MAX_CRYPTO_TYPE_NAME_LEN 64 |
| 45 | |
| 46 | #define MAX_KEY_LEN 48 |
| 47 | #define SALT_LEN 16 |
| 48 | #define SCRYPT_LEN 32 |
| 49 | |
| 50 | /* definitions of flags in the structure below */ |
| 51 | #define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */ |
| 52 | #define CRYPT_ENCRYPTION_IN_PROGRESS 0x2 /* Encryption partially completed, |
| 53 | encrypted_upto valid*/ |
| 54 | #define CRYPT_INCONSISTENT_STATE 0x4 /* Set when starting encryption, clear when |
| 55 | exit cleanly, either through success or |
| 56 | correctly marked partial encryption */ |
| 57 | #define CRYPT_DATA_CORRUPT 0x8 /* Set when encryption is fine, but the |
| 58 | underlying volume is corrupt */ |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 59 | #define CRYPT_FORCE_ENCRYPTION 0x10 /* Set when it is time to encrypt this |
| 60 | volume on boot. Everything in this |
| 61 | structure is set up correctly as |
| 62 | though device is encrypted except |
| 63 | that the master key is encrypted with the |
| 64 | default password. */ |
| 65 | #define CRYPT_FORCE_COMPLETE 0x20 /* Set when the above encryption cycle is |
| 66 | complete. On next cryptkeeper entry, match |
| 67 | the password. If it matches fix the master |
| 68 | key and remove this flag. */ |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 69 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 70 | /* This flag is used to transition from L->M upgrade. L release passed |
| 71 | * a byte for every nible of user password while M release is passing |
| 72 | * ascii value of user password. |
| 73 | * Random flag value is chosen so that it does not conflict with other use cases |
| 74 | */ |
| 75 | #define CRYPT_ASCII_PASSWORD_UPDATED 0x1000 |
| 76 | #endif |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 77 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 78 | /* Allowed values for type in the structure below */ |
| 79 | #define CRYPT_TYPE_PASSWORD 0 /* master_key is encrypted with a password |
| 80 | * Must be zero to be compatible with pre-L |
| 81 | * devices where type is always password.*/ |
| 82 | #define CRYPT_TYPE_DEFAULT 1 /* master_key is encrypted with default |
| 83 | * password */ |
| 84 | #define CRYPT_TYPE_PATTERN 2 /* master_key is encrypted with a pattern */ |
| 85 | #define CRYPT_TYPE_PIN 3 /* master_key is encrypted with a pin */ |
| 86 | #define CRYPT_TYPE_MAX_TYPE 3 /* type cannot be larger than this value */ |
| 87 | |
| 88 | #define CRYPT_MNT_MAGIC 0xD0B5B1C4 |
| 89 | #define PERSIST_DATA_MAGIC 0xE950CD44 |
| 90 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 91 | /* Key Derivation Function algorithms */ |
| 92 | #define KDF_PBKDF2 1 |
| 93 | #define KDF_SCRYPT 2 |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 94 | /* Algorithms 3 & 4 deprecated before shipping outside of google, so removed */ |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 95 | #define KDF_SCRYPT_KEYMASTER_UNPADDED 3 |
| 96 | #define KDF_SCRYPT_KEYMASTER_BADLY_PADDED 4 |
| 97 | #define KDF_SCRYPT_KEYMASTER 5 |
| 98 | |
| 99 | /* Maximum allowed keymaster blob size. */ |
| 100 | #define KEYMASTER_BLOB_SIZE 2048 |
| 101 | |
| 102 | /* __le32 and __le16 defined in system/extras/ext4_utils/ext4_utils.h */ |
| 103 | #define __le8 unsigned char |
| 104 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 105 | #if !defined(SHA256_DIGEST_LENGTH) |
| 106 | #define SHA256_DIGEST_LENGTH 32 |
| 107 | #endif |
| 108 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 109 | struct crypt_mnt_ftr { |
| 110 | __le32 magic; /* See above */ |
| 111 | __le16 major_version; |
| 112 | __le16 minor_version; |
| 113 | __le32 ftr_size; /* in bytes, not including key following */ |
| 114 | __le32 flags; /* See above */ |
| 115 | __le32 keysize; /* in bytes */ |
| 116 | __le32 crypt_type; /* how master_key is encrypted. Must be a |
| 117 | * CRYPT_TYPE_XXX value */ |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 118 | __le64 fs_size; /* Size of the encrypted fs, in 512 byte sectors */ |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 119 | __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and |
| 120 | mount, set to 0 on successful mount */ |
| 121 | unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption |
| 122 | needed to decrypt this |
| 123 | partition, null terminated */ |
| 124 | __le32 spare2; /* ignored */ |
| 125 | unsigned char master_key[MAX_KEY_LEN]; /* The encrypted key for decrypting the filesystem */ |
| 126 | unsigned char salt[SALT_LEN]; /* The salt used for this encryption */ |
| 127 | __le64 persist_data_offset[2]; /* Absolute offset to both copies of crypt_persist_data |
| 128 | * on device with that info, either the footer of the |
| 129 | * real_blkdevice or the metadata partition. */ |
| 130 | |
| 131 | __le32 persist_data_size; /* The number of bytes allocated to each copy of the |
| 132 | * persistent data table*/ |
| 133 | |
| 134 | __le8 kdf_type; /* The key derivation function used. */ |
| 135 | |
| 136 | /* scrypt parameters. See www.tarsnap.com/scrypt/scrypt.pdf */ |
| 137 | __le8 N_factor; /* (1 << N) */ |
| 138 | __le8 r_factor; /* (1 << r) */ |
| 139 | __le8 p_factor; /* (1 << p) */ |
| 140 | __le64 encrypted_upto; /* If we are in state CRYPT_ENCRYPTION_IN_PROGRESS and |
| 141 | we have to stop (e.g. power low) this is the last |
| 142 | encrypted 512 byte sector.*/ |
| 143 | __le8 hash_first_block[SHA256_DIGEST_LENGTH]; /* When CRYPT_ENCRYPTION_IN_PROGRESS |
| 144 | set, hash of first block, used |
| 145 | to validate before continuing*/ |
| 146 | |
| 147 | /* key_master key, used to sign the derived key which is then used to generate |
| 148 | * the intermediate key |
| 149 | * This key should be used for no other purposes! We use this key to sign unpadded |
| 150 | * data, which is acceptable but only if the key is not reused elsewhere. */ |
| 151 | __le8 keymaster_blob[KEYMASTER_BLOB_SIZE]; |
| 152 | __le32 keymaster_blob_size; |
| 153 | |
| 154 | /* Store scrypt of salted intermediate key. When decryption fails, we can |
| 155 | check if this matches, and if it does, we know that the problem is with the |
| 156 | drive, and there is no point in asking the user for more passwords. |
| 157 | |
| 158 | Note that if any part of this structure is corrupt, this will not match and |
| 159 | we will continue to believe the user entered the wrong password. In that |
| 160 | case the only solution is for the user to enter a password enough times to |
| 161 | force a wipe. |
| 162 | |
| 163 | Note also that there is no need to worry about migration. If this data is |
| 164 | wrong, we simply won't recognise a right password, and will continue to |
| 165 | prompt. On the first password change, this value will be populated and |
| 166 | then we will be OK. |
| 167 | */ |
| 168 | unsigned char scrypted_intermediate_key[SCRYPT_LEN]; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 169 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 170 | /* sha of this structure with this element set to zero |
| 171 | Used when encrypting on reboot to validate structure before doing something |
| 172 | fatal |
| 173 | */ |
| 174 | unsigned char sha256[SHA256_DIGEST_LENGTH]; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 175 | }; |
| 176 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 177 | #define DATA_MNT_POINT "/data" |
| 178 | |
| 179 | /* Return values for cryptfs_crypto_complete */ |
| 180 | #define CRYPTO_COMPLETE_NOT_ENCRYPTED 1 |
| 181 | #define CRYPTO_COMPLETE_ENCRYPTED 0 |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 182 | #define CRYPTO_COMPLETE_BAD_METADATA (-1) |
| 183 | #define CRYPTO_COMPLETE_PARTIAL (-2) |
| 184 | #define CRYPTO_COMPLETE_INCONSISTENT (-3) |
| 185 | #define CRYPTO_COMPLETE_CORRUPT (-4) |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 186 | |
| 187 | /* Return values for cryptfs_enable_inplace*() */ |
| 188 | #define ENABLE_INPLACE_OK 0 |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 189 | #define ENABLE_INPLACE_ERR_OTHER (-1) |
| 190 | #define ENABLE_INPLACE_ERR_DEV (-2) /* crypto_blkdev issue */ |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 191 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 192 | typedef int (*kdf_func)(const char* passwd, const unsigned char* salt, unsigned char* ikey, |
| 193 | void* params); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 194 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 195 | int cryptfs_check_passwd(const char* pw); |
| 196 | int cryptfs_verify_passwd(const char* pw); |
| 197 | int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, const unsigned char* key, int keysize, char* out_crypto_blkdev); |
| 198 | int cryptfs_revert_ext_volume(const char* label); |
| 199 | int cryptfs_get_password_type(void); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 200 | |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 201 | uint32_t cryptfs_get_keysize(); |
| 202 | const char* cryptfs_get_crypto_name(); |
| 203 | |
| 204 | void set_partition_data(const char* block_device, const char* key_location, const char* fs); |
| 205 | int cryptfs_check_footer(); |
| 206 | int delete_crypto_blk_dev(const char *name); |
| 207 | |
| 208 | #endif /* ANDROID_VOLD_CRYPTFS_H */ |