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 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 29 | #include <cutils/properties.h> |
| 30 | |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 31 | /* The current cryptfs version */ |
| 32 | #define CURRENT_MAJOR_VERSION 1 |
| 33 | #define CURRENT_MINOR_VERSION 2 |
| 34 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 35 | #define CRYPT_FOOTER_OFFSET 0x4000 |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 36 | #define CRYPT_FOOTER_TO_PERSIST_OFFSET 0x1000 |
| 37 | #define CRYPT_PERSIST_DATA_SIZE 0x1000 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 38 | |
| 39 | #define MAX_CRYPTO_TYPE_NAME_LEN 64 |
| 40 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 41 | #define MAX_KEY_LEN 48 |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 42 | #define SALT_LEN 16 |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 43 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 44 | /* definitions of flags in the structure below */ |
| 45 | #define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */ |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 46 | #define CRYPT_ENCRYPTION_IN_PROGRESS 0x2 /* Set when starting encryption, |
| 47 | * clear when done before rebooting */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 48 | |
| 49 | #define CRYPT_MNT_MAGIC 0xD0B5B1C4 |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 50 | #define PERSIST_DATA_MAGIC 0xE950CD44 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 51 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 52 | #define SCRYPT_PROP "ro.crypto.scrypt_params" |
| 53 | #define SCRYPT_DEFAULTS { 15, 3, 1 } |
| 54 | |
| 55 | /* Key Derivation Function algorithms */ |
| 56 | #define KDF_PBKDF2 1 |
| 57 | #define KDF_SCRYPT 2 |
| 58 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 59 | #define __le32 unsigned int |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 60 | #define __le16 unsigned short int |
| 61 | #define __le8 unsigned char |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 62 | |
| 63 | struct crypt_mnt_ftr { |
| 64 | __le32 magic; /* See above */ |
| 65 | __le16 major_version; |
| 66 | __le16 minor_version; |
| 67 | __le32 ftr_size; /* in bytes, not including key following */ |
| 68 | __le32 flags; /* See above */ |
| 69 | __le32 keysize; /* in bytes */ |
| 70 | __le32 spare1; /* ignored */ |
| 71 | __le64 fs_size; /* Size of the encrypted fs, in 512 byte sectors */ |
| 72 | __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and |
| 73 | mount, set to 0 on successful mount */ |
| 74 | unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption |
| 75 | needed to decrypt this |
| 76 | partition, null terminated */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 77 | __le32 spare2; /* ignored */ |
| 78 | unsigned char master_key[MAX_KEY_LEN]; /* The encrypted key for decrypting the filesystem */ |
| 79 | unsigned char salt[SALT_LEN]; /* The salt used for this encryption */ |
| 80 | __le64 persist_data_offset[2]; /* Absolute offset to both copies of crypt_persist_data |
| 81 | * on device with that info, either the footer of the |
| 82 | * real_blkdevice or the metadata partition. */ |
| 83 | |
| 84 | __le32 persist_data_size; /* The number of bytes allocated to each copy of the |
| 85 | * persistent data table*/ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 86 | |
| 87 | __le8 kdf_type; /* The key derivation function used. */ |
| 88 | |
| 89 | /* scrypt parameters. See www.tarsnap.com/scrypt/scrypt.pdf */ |
| 90 | __le8 N_factor; /* (1 << N) */ |
| 91 | __le8 r_factor; /* (1 << r) */ |
| 92 | __le8 p_factor; /* (1 << p) */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* Persistant data that should be available before decryption. |
| 96 | * Things like airplane mode, locale and timezone are kept |
| 97 | * here and can be retrieved by the CryptKeeper UI to properly |
| 98 | * configure the phone before asking for the password |
| 99 | * This is only valid if the major and minor version above |
| 100 | * is set to 1.1 or higher. |
| 101 | * |
| 102 | * This is a 4K structure. There are 2 copies, and the code alternates |
| 103 | * writing one and then clearing the previous one. The reading |
| 104 | * code reads the first valid copy it finds, based on the magic number. |
| 105 | * The absolute offset to the first of the two copies is kept in rev 1.1 |
| 106 | * and higher crypt_mnt_ftr structures. |
| 107 | */ |
| 108 | struct crypt_persist_entry { |
| 109 | char key[PROPERTY_KEY_MAX]; |
| 110 | char val[PROPERTY_VALUE_MAX]; |
| 111 | }; |
| 112 | |
| 113 | /* Should be exactly 4K in size */ |
| 114 | struct crypt_persist_data { |
| 115 | __le32 persist_magic; |
| 116 | __le32 persist_valid_entries; |
| 117 | __le32 persist_spare[30]; |
| 118 | struct crypt_persist_entry persist_entry[0]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 121 | struct volume_info { |
| 122 | unsigned int size; |
| 123 | unsigned int flags; |
| 124 | struct crypt_mnt_ftr crypt_ftr; |
| 125 | char mnt_point[256]; |
| 126 | char blk_dev[256]; |
| 127 | char crypto_blkdev[256]; |
| 128 | char label[256]; |
| 129 | }; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 130 | #define VOL_NONREMOVABLE 0x1 |
| 131 | #define VOL_ENCRYPTABLE 0x2 |
| 132 | #define VOL_PRIMARY 0x4 |
| 133 | #define VOL_PROVIDES_ASEC 0x8 |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 134 | |
JP Abgrall | 502dc74 | 2013-11-01 13:06:20 -0700 | [diff] [blame^] | 135 | #define DATA_MNT_POINT "/data" |
| 136 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 137 | #ifdef __cplusplus |
| 138 | extern "C" { |
| 139 | #endif |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 140 | |
| 141 | typedef void (*kdf_func)(char *passwd, unsigned char *salt, unsigned char *ikey, void *params); |
| 142 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 143 | int cryptfs_crypto_complete(void); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 144 | int cryptfs_check_passwd(char *pw); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 145 | int cryptfs_verify_passwd(char *newpw); |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 146 | int cryptfs_restart(void); |
JP Abgrall | 502dc74 | 2013-11-01 13:06:20 -0700 | [diff] [blame^] | 147 | int cryptfs_enable(char *flag, char *passwd, int allow_reboot); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 148 | int cryptfs_changepw(char *newpw); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 149 | int cryptfs_setup_volume(const char *label, int major, int minor, |
| 150 | char *crypto_dev_path, unsigned int max_pathlen, |
| 151 | int *new_major, int *new_minor); |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 152 | int cryptfs_revert_volume(const char *label); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 153 | int cryptfs_getfield(char *fieldname, char *value, int len); |
| 154 | int cryptfs_setfield(char *fieldname, char *value); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 155 | #ifdef __cplusplus |
| 156 | } |
| 157 | #endif |
| 158 | |