cryptfs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #ifndef ANDROID_VOLD_CRYPTFS_H
  17. #define ANDROID_VOLD_CRYPTFS_H
  18. /* This structure starts 16,384 bytes before the end of a hardware
  19. * partition that is encrypted, or in a separate partition. It's location
  20. * is specified by a property set in init.<device>.rc.
  21. * The structure allocates 48 bytes for a key, but the real key size is
  22. * specified in the struct. Currently, the code is hardcoded to use 128
  23. * bit keys.
  24. * The fields after salt are only valid in rev 1.1 and later stuctures.
  25. * Obviously, the filesystem does not include the last 16 kbytes
  26. * of the partition if the crypt_mnt_ftr lives at the end of the
  27. * partition.
  28. */
  29. #include <linux/types.h>
  30. #include <stdbool.h>
  31. #include <stdint.h>
  32. #include <cutils/properties.h>
  33. /* The current cryptfs version */
  34. #define CURRENT_MAJOR_VERSION 1
  35. #define CURRENT_MINOR_VERSION 3
  36. #define CRYPT_FOOTER_OFFSET 0x4000
  37. #define CRYPT_FOOTER_TO_PERSIST_OFFSET 0x1000
  38. #define CRYPT_PERSIST_DATA_SIZE 0x1000
  39. #define MAX_CRYPTO_TYPE_NAME_LEN 64
  40. #define MAX_KEY_LEN 48
  41. #define SALT_LEN 16
  42. #define SCRYPT_LEN 32
  43. /* definitions of flags in the structure below */
  44. #define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */
  45. #define CRYPT_ENCRYPTION_IN_PROGRESS \
  46. 0x2 /* Encryption partially completed, \
  47. encrypted_upto valid*/
  48. #define CRYPT_INCONSISTENT_STATE \
  49. 0x4 /* Set when starting encryption, clear when \
  50. exit cleanly, either through success or \
  51. correctly marked partial encryption */
  52. #define CRYPT_DATA_CORRUPT \
  53. 0x8 /* Set when encryption is fine, but the \
  54. underlying volume is corrupt */
  55. #define CRYPT_FORCE_ENCRYPTION \
  56. 0x10 /* Set when it is time to encrypt this \
  57. volume on boot. Everything in this \
  58. structure is set up correctly as \
  59. though device is encrypted except \
  60. that the master key is encrypted with the \
  61. default password. */
  62. #define CRYPT_FORCE_COMPLETE \
  63. 0x20 /* Set when the above encryption cycle is \
  64. complete. On next cryptkeeper entry, match \
  65. the password. If it matches fix the master \
  66. key and remove this flag. */
  67. /* Allowed values for type in the structure below */
  68. #define CRYPT_TYPE_PASSWORD \
  69. 0 /* master_key is encrypted with a password \
  70. * Must be zero to be compatible with pre-L \
  71. * devices where type is always password.*/
  72. #define CRYPT_TYPE_DEFAULT \
  73. 1 /* master_key is encrypted with default \
  74. * password */
  75. #define CRYPT_TYPE_PATTERN 2 /* master_key is encrypted with a pattern */
  76. #define CRYPT_TYPE_PIN 3 /* master_key is encrypted with a pin */
  77. #define CRYPT_TYPE_MAX_TYPE 3 /* type cannot be larger than this value */
  78. #define CRYPT_MNT_MAGIC 0xD0B5B1C4
  79. #define PERSIST_DATA_MAGIC 0xE950CD44
  80. /* Key Derivation Function algorithms */
  81. #define KDF_PBKDF2 1
  82. #define KDF_SCRYPT 2
  83. /* Algorithms 3 & 4 deprecated before shipping outside of google, so removed */
  84. #define KDF_SCRYPT_KEYMASTER 5
  85. /* Maximum allowed keymaster blob size. */
  86. #define KEYMASTER_BLOB_SIZE 2048
  87. /* __le32 and __le16 defined in system/extras/ext4_utils/ext4_utils.h */
  88. #define __le8 unsigned char
  89. #if !defined(SHA256_DIGEST_LENGTH)
  90. #define SHA256_DIGEST_LENGTH 32
  91. #endif
  92. struct crypt_mnt_ftr {
  93. __le32 magic; /* See above */
  94. __le16 major_version;
  95. __le16 minor_version;
  96. __le32 ftr_size; /* in bytes, not including key following */
  97. __le32 flags; /* See above */
  98. __le32 keysize; /* in bytes */
  99. __le32 crypt_type; /* how master_key is encrypted. Must be a
  100. * CRYPT_TYPE_XXX value */
  101. __le64 fs_size; /* Size of the encrypted fs, in 512 byte sectors */
  102. __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and
  103. mount, set to 0 on successful mount */
  104. unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption
  105. needed to decrypt this
  106. partition, null terminated */
  107. __le32 spare2; /* ignored */
  108. unsigned char master_key[MAX_KEY_LEN]; /* The encrypted key for decrypting the filesystem */
  109. unsigned char salt[SALT_LEN]; /* The salt used for this encryption */
  110. __le64 persist_data_offset[2]; /* Absolute offset to both copies of crypt_persist_data
  111. * on device with that info, either the footer of the
  112. * real_blkdevice or the metadata partition. */
  113. __le32 persist_data_size; /* The number of bytes allocated to each copy of the
  114. * persistent data table*/
  115. __le8 kdf_type; /* The key derivation function used. */
  116. /* scrypt parameters. See www.tarsnap.com/scrypt/scrypt.pdf */
  117. __le8 N_factor; /* (1 << N) */
  118. __le8 r_factor; /* (1 << r) */
  119. __le8 p_factor; /* (1 << p) */
  120. __le64 encrypted_upto; /* If we are in state CRYPT_ENCRYPTION_IN_PROGRESS and
  121. we have to stop (e.g. power low) this is the last
  122. encrypted 512 byte sector.*/
  123. __le8 hash_first_block[SHA256_DIGEST_LENGTH]; /* When CRYPT_ENCRYPTION_IN_PROGRESS
  124. set, hash of first block, used
  125. to validate before continuing*/
  126. /* key_master key, used to sign the derived key which is then used to generate
  127. * the intermediate key
  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;
  132. /* Store scrypt of salted intermediate key. When decryption fails, we can
  133. check if this matches, and if it does, we know that the problem is with the
  134. drive, and there is no point in asking the user for more passwords.
  135. Note that if any part of this structure is corrupt, this will not match and
  136. we will continue to believe the user entered the wrong password. In that
  137. case the only solution is for the user to enter a password enough times to
  138. force a wipe.
  139. Note also that there is no need to worry about migration. If this data is
  140. wrong, we simply won't recognise a right password, and will continue to
  141. prompt. On the first password change, this value will be populated and
  142. then we will be OK.
  143. */
  144. unsigned char scrypted_intermediate_key[SCRYPT_LEN];
  145. /* sha of this structure with this element set to zero
  146. Used when encrypting on reboot to validate structure before doing something
  147. fatal
  148. */
  149. unsigned char sha256[SHA256_DIGEST_LENGTH];
  150. };
  151. /* Persistant data that should be available before decryption.
  152. * Things like airplane mode, locale and timezone are kept
  153. * here and can be retrieved by the CryptKeeper UI to properly
  154. * configure the phone before asking for the password
  155. * This is only valid if the major and minor version above
  156. * is set to 1.1 or higher.
  157. *
  158. * This is a 4K structure. There are 2 copies, and the code alternates
  159. * writing one and then clearing the previous one. The reading
  160. * code reads the first valid copy it finds, based on the magic number.
  161. * The absolute offset to the first of the two copies is kept in rev 1.1
  162. * and higher crypt_mnt_ftr structures.
  163. */
  164. struct crypt_persist_entry {
  165. char key[PROPERTY_KEY_MAX];
  166. char val[PROPERTY_VALUE_MAX];
  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];
  174. };
  175. #define DATA_MNT_POINT "/data"
  176. /* Return values for cryptfs_crypto_complete */
  177. #define CRYPTO_COMPLETE_NOT_ENCRYPTED 1
  178. #define CRYPTO_COMPLETE_ENCRYPTED 0
  179. #define CRYPTO_COMPLETE_BAD_METADATA (-1)
  180. #define CRYPTO_COMPLETE_PARTIAL (-2)
  181. #define CRYPTO_COMPLETE_INCONSISTENT (-3)
  182. #define CRYPTO_COMPLETE_CORRUPT (-4)
  183. /* Return values for cryptfs_enable_inplace*() */
  184. #define ENABLE_INPLACE_OK 0
  185. #define ENABLE_INPLACE_ERR_OTHER (-1)
  186. #define ENABLE_INPLACE_ERR_DEV (-2) /* crypto_blkdev issue */
  187. /* Return values for cryptfs_getfield */
  188. #define CRYPTO_GETFIELD_OK 0
  189. #define CRYPTO_GETFIELD_ERROR_NO_FIELD (-1)
  190. #define CRYPTO_GETFIELD_ERROR_OTHER (-2)
  191. #define CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL (-3)
  192. /* Return values for cryptfs_setfield */
  193. #define CRYPTO_SETFIELD_OK 0
  194. #define CRYPTO_SETFIELD_ERROR_OTHER (-1)
  195. #define CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG (-2)
  196. #define CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG (-3)
  197. /* Return values for persist_del_key */
  198. #define PERSIST_DEL_KEY_OK 0
  199. #define PERSIST_DEL_KEY_ERROR_OTHER (-1)
  200. #define PERSIST_DEL_KEY_ERROR_NO_FIELD (-2)
  201. int match_multi_entry(const char* key, const char* field, unsigned index);
  202. int wait_and_unmount(const char* mountpoint, bool kill);
  203. typedef int (*kdf_func)(const char* passwd, const unsigned char* salt, unsigned char* ikey,
  204. void* params);
  205. int cryptfs_crypto_complete(void);
  206. int cryptfs_check_passwd(const char* pw);
  207. int cryptfs_verify_passwd(const char* pw);
  208. int cryptfs_restart(void);
  209. int cryptfs_enable(int type, const char* passwd, int no_ui);
  210. int cryptfs_changepw(int type, const char* newpw);
  211. int cryptfs_enable_default(int no_ui);
  212. int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, const unsigned char* key,
  213. char* out_crypto_blkdev);
  214. int cryptfs_revert_ext_volume(const char* label);
  215. int cryptfs_getfield(const char* fieldname, char* value, int len);
  216. int cryptfs_setfield(const char* fieldname, const char* value);
  217. int cryptfs_mount_default_encrypted(void);
  218. int cryptfs_get_password_type(void);
  219. const char* cryptfs_get_password(void);
  220. void cryptfs_clear_password(void);
  221. int cryptfs_isConvertibleToFBE(void);
  222. uint32_t cryptfs_get_keysize();
  223. const char* cryptfs_get_crypto_name();
  224. #endif /* ANDROID_VOLD_CRYPTFS_H */