blob: bdbce801974d047894f3569403b86366d9642de9 [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
Ken Sumrall160b4d62013-04-22 12:15:39 -070029#include <cutils/properties.h>
30
Kenny Rootc96a5f82013-06-14 12:08:28 -070031/* The current cryptfs version */
32#define CURRENT_MAJOR_VERSION 1
33#define CURRENT_MINOR_VERSION 2
34
Ken Sumrall8f869aa2010-12-03 03:47:09 -080035#define CRYPT_FOOTER_OFFSET 0x4000
Ken Sumrall160b4d62013-04-22 12:15:39 -070036#define CRYPT_FOOTER_TO_PERSIST_OFFSET 0x1000
37#define CRYPT_PERSIST_DATA_SIZE 0x1000
Ken Sumrall8f869aa2010-12-03 03:47:09 -080038
39#define MAX_CRYPTO_TYPE_NAME_LEN 64
40
Ken Sumrall160b4d62013-04-22 12:15:39 -070041#define MAX_KEY_LEN 48
Ken Sumralle8744072011-01-18 22:01:55 -080042#define SALT_LEN 16
Ken Sumralle8744072011-01-18 22:01:55 -080043
Ken Sumrall8f869aa2010-12-03 03:47:09 -080044/* definitions of flags in the structure below */
45#define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */
Ken Sumralld33d4172011-02-01 00:49:13 -080046#define CRYPT_ENCRYPTION_IN_PROGRESS 0x2 /* Set when starting encryption,
47 * clear when done before rebooting */
Ken Sumrall8f869aa2010-12-03 03:47:09 -080048
49#define CRYPT_MNT_MAGIC 0xD0B5B1C4
Ken Sumrall160b4d62013-04-22 12:15:39 -070050#define PERSIST_DATA_MAGIC 0xE950CD44
Ken Sumrall8f869aa2010-12-03 03:47:09 -080051
52#define __le32 unsigned int
53#define __le16 unsigned short int
54
55struct crypt_mnt_ftr {
56 __le32 magic; /* See above */
57 __le16 major_version;
58 __le16 minor_version;
59 __le32 ftr_size; /* in bytes, not including key following */
60 __le32 flags; /* See above */
61 __le32 keysize; /* in bytes */
62 __le32 spare1; /* ignored */
63 __le64 fs_size; /* Size of the encrypted fs, in 512 byte sectors */
64 __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and
65 mount, set to 0 on successful mount */
66 unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption
67 needed to decrypt this
68 partition, null terminated */
Ken Sumrall160b4d62013-04-22 12:15:39 -070069 __le32 spare2; /* ignored */
70 unsigned char master_key[MAX_KEY_LEN]; /* The encrypted key for decrypting the filesystem */
71 unsigned char salt[SALT_LEN]; /* The salt used for this encryption */
72 __le64 persist_data_offset[2]; /* Absolute offset to both copies of crypt_persist_data
73 * on device with that info, either the footer of the
74 * real_blkdevice or the metadata partition. */
75
76 __le32 persist_data_size; /* The number of bytes allocated to each copy of the
77 * persistent data table*/
78};
79
80/* Persistant data that should be available before decryption.
81 * Things like airplane mode, locale and timezone are kept
82 * here and can be retrieved by the CryptKeeper UI to properly
83 * configure the phone before asking for the password
84 * This is only valid if the major and minor version above
85 * is set to 1.1 or higher.
86 *
87 * This is a 4K structure. There are 2 copies, and the code alternates
88 * writing one and then clearing the previous one. The reading
89 * code reads the first valid copy it finds, based on the magic number.
90 * The absolute offset to the first of the two copies is kept in rev 1.1
91 * and higher crypt_mnt_ftr structures.
92 */
93struct crypt_persist_entry {
94 char key[PROPERTY_KEY_MAX];
95 char val[PROPERTY_VALUE_MAX];
96};
97
98/* Should be exactly 4K in size */
99struct crypt_persist_data {
100 __le32 persist_magic;
101 __le32 persist_valid_entries;
102 __le32 persist_spare[30];
103 struct crypt_persist_entry persist_entry[0];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800104};
105
Ken Sumrall29d8da82011-05-18 17:20:07 -0700106struct volume_info {
107 unsigned int size;
108 unsigned int flags;
109 struct crypt_mnt_ftr crypt_ftr;
110 char mnt_point[256];
111 char blk_dev[256];
112 char crypto_blkdev[256];
113 char label[256];
114};
115#define VOL_NONREMOVABLE 0x1
116#define VOL_ENCRYPTABLE 0x2
117
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800118#ifdef __cplusplus
119extern "C" {
120#endif
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800121 int cryptfs_crypto_complete(void);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800122 int cryptfs_check_passwd(char *pw);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700123 int cryptfs_verify_passwd(char *newpw);
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800124 int cryptfs_restart(void);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800125 int cryptfs_enable(char *flag, char *passwd);
Jason parks70a4b3f2011-01-28 10:10:47 -0600126 int cryptfs_changepw(char *newpw);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700127 int cryptfs_setup_volume(const char *label, int major, int minor,
128 char *crypto_dev_path, unsigned int max_pathlen,
129 int *new_major, int *new_minor);
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700130 int cryptfs_revert_volume(const char *label);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700131 int cryptfs_getfield(char *fieldname, char *value, int len);
132 int cryptfs_setfield(char *fieldname, char *value);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800133#ifdef __cplusplus
134}
135#endif
136