Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext4/ext4_crypto.h |
| 3 | * |
| 4 | * Copyright (C) 2015, Google, Inc. |
| 5 | * |
| 6 | * This contains encryption header content for ext4 |
| 7 | * |
| 8 | * Written by Michael Halcrow, 2015. |
| 9 | */ |
| 10 | |
| 11 | #ifndef _EXT4_CRYPTO_H |
| 12 | #define _EXT4_CRYPTO_H |
| 13 | |
| 14 | #include <linux/fs.h> |
| 15 | |
| 16 | #define EXT4_KEY_DESCRIPTOR_SIZE 8 |
| 17 | |
| 18 | /* Policy provided via an ioctl on the topmost directory */ |
| 19 | struct ext4_encryption_policy { |
| 20 | char version; |
| 21 | char contents_encryption_mode; |
| 22 | char filenames_encryption_mode; |
Theodore Ts'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 23 | char flags; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 24 | char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE]; |
| 25 | } __attribute__((__packed__)); |
| 26 | |
| 27 | #define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1 |
| 28 | #define EXT4_KEY_DERIVATION_NONCE_SIZE 16 |
| 29 | |
Theodore Ts'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 30 | #define EXT4_POLICY_FLAGS_PAD_4 0x00 |
| 31 | #define EXT4_POLICY_FLAGS_PAD_8 0x01 |
| 32 | #define EXT4_POLICY_FLAGS_PAD_16 0x02 |
| 33 | #define EXT4_POLICY_FLAGS_PAD_32 0x03 |
| 34 | #define EXT4_POLICY_FLAGS_PAD_MASK 0x03 |
| 35 | #define EXT4_POLICY_FLAGS_VALID 0x03 |
| 36 | |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 37 | /** |
| 38 | * Encryption context for inode |
| 39 | * |
| 40 | * Protector format: |
| 41 | * 1 byte: Protector format (1 = this version) |
| 42 | * 1 byte: File contents encryption mode |
| 43 | * 1 byte: File names encryption mode |
| 44 | * 1 byte: Reserved |
| 45 | * 8 bytes: Master Key descriptor |
| 46 | * 16 bytes: Encryption Key derivation nonce |
| 47 | */ |
| 48 | struct ext4_encryption_context { |
| 49 | char format; |
| 50 | char contents_encryption_mode; |
| 51 | char filenames_encryption_mode; |
Theodore Ts'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 52 | char flags; |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 53 | char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE]; |
| 54 | char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE]; |
| 55 | } __attribute__((__packed__)); |
| 56 | |
Michael Halcrow | b30ab0e | 2015-04-12 00:43:56 -0400 | [diff] [blame] | 57 | /* Encryption parameters */ |
| 58 | #define EXT4_XTS_TWEAK_SIZE 16 |
| 59 | #define EXT4_AES_128_ECB_KEY_SIZE 16 |
| 60 | #define EXT4_AES_256_GCM_KEY_SIZE 32 |
| 61 | #define EXT4_AES_256_CBC_KEY_SIZE 32 |
| 62 | #define EXT4_AES_256_CTS_KEY_SIZE 32 |
| 63 | #define EXT4_AES_256_XTS_KEY_SIZE 64 |
| 64 | #define EXT4_MAX_KEY_SIZE 64 |
| 65 | |
Michael Halcrow | 88bd6cc | 2015-04-12 00:55:06 -0400 | [diff] [blame] | 66 | #define EXT4_KEY_DESC_PREFIX "ext4:" |
| 67 | #define EXT4_KEY_DESC_PREFIX_SIZE 5 |
| 68 | |
Theodore Ts'o | e2881b1b | 2015-05-18 13:16:47 -0400 | [diff] [blame^] | 69 | /* This is passed in from userspace into the kernel keyring */ |
Michael Halcrow | b30ab0e | 2015-04-12 00:43:56 -0400 | [diff] [blame] | 70 | struct ext4_encryption_key { |
Theodore Ts'o | e2881b1b | 2015-05-18 13:16:47 -0400 | [diff] [blame^] | 71 | __u32 mode; |
| 72 | char raw[EXT4_MAX_KEY_SIZE]; |
| 73 | __u32 size; |
| 74 | } __attribute__((__packed__)); |
| 75 | |
| 76 | struct ext4_crypt_info { |
| 77 | unsigned char ci_mode; |
| 78 | unsigned char ci_size; |
| 79 | char ci_raw[EXT4_MAX_KEY_SIZE]; |
Michael Halcrow | b30ab0e | 2015-04-12 00:43:56 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | #define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001 |
| 83 | #define EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL 0x00000002 |
| 84 | |
| 85 | struct ext4_crypto_ctx { |
| 86 | struct crypto_tfm *tfm; /* Crypto API context */ |
| 87 | struct page *bounce_page; /* Ciphertext page on write path */ |
| 88 | struct page *control_page; /* Original page on write path */ |
| 89 | struct bio *bio; /* The bio for this context */ |
| 90 | struct work_struct work; /* Work queue for read complete path */ |
| 91 | struct list_head free_list; /* Free list */ |
| 92 | int flags; /* Flags */ |
| 93 | int mode; /* Encryption mode for tfm */ |
| 94 | }; |
| 95 | |
| 96 | struct ext4_completion_result { |
| 97 | struct completion completion; |
| 98 | int res; |
| 99 | }; |
| 100 | |
| 101 | #define DECLARE_EXT4_COMPLETION_RESULT(ecr) \ |
| 102 | struct ext4_completion_result ecr = { \ |
| 103 | COMPLETION_INITIALIZER((ecr).completion), 0 } |
| 104 | |
| 105 | static inline int ext4_encryption_key_size(int mode) |
| 106 | { |
| 107 | switch (mode) { |
| 108 | case EXT4_ENCRYPTION_MODE_AES_256_XTS: |
| 109 | return EXT4_AES_256_XTS_KEY_SIZE; |
| 110 | case EXT4_ENCRYPTION_MODE_AES_256_GCM: |
| 111 | return EXT4_AES_256_GCM_KEY_SIZE; |
| 112 | case EXT4_ENCRYPTION_MODE_AES_256_CBC: |
| 113 | return EXT4_AES_256_CBC_KEY_SIZE; |
| 114 | case EXT4_ENCRYPTION_MODE_AES_256_CTS: |
| 115 | return EXT4_AES_256_CTS_KEY_SIZE; |
| 116 | default: |
| 117 | BUG(); |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
Michael Halcrow | d5d0e8c | 2015-04-12 00:56:17 -0400 | [diff] [blame] | 122 | #define EXT4_FNAME_NUM_SCATTER_ENTRIES 4 |
| 123 | #define EXT4_CRYPTO_BLOCK_SIZE 16 |
| 124 | #define EXT4_FNAME_CRYPTO_DIGEST_SIZE 32 |
| 125 | |
| 126 | struct ext4_str { |
| 127 | unsigned char *name; |
| 128 | u32 len; |
| 129 | }; |
| 130 | |
| 131 | struct ext4_fname_crypto_ctx { |
| 132 | u32 lim; |
Michael Halcrow | d5d0e8c | 2015-04-12 00:56:17 -0400 | [diff] [blame] | 133 | struct crypto_ablkcipher *ctfm; |
| 134 | struct crypto_hash *htfm; |
Theodore Ts'o | e2881b1b | 2015-05-18 13:16:47 -0400 | [diff] [blame^] | 135 | struct ext4_crypt_info ci; |
Theodore Ts'o | a44cd7a | 2015-05-01 16:56:50 -0400 | [diff] [blame] | 136 | unsigned flags : 8; |
Michael Halcrow | d5d0e8c | 2015-04-12 00:56:17 -0400 | [diff] [blame] | 137 | unsigned has_valid_key : 1; |
| 138 | unsigned ctfm_key_is_ready : 1; |
| 139 | }; |
| 140 | |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 141 | /** |
| 142 | * For encrypted symlinks, the ciphertext length is stored at the beginning |
| 143 | * of the string in little-endian format. |
| 144 | */ |
| 145 | struct ext4_encrypted_symlink_data { |
| 146 | __le16 len; |
| 147 | char encrypted_path[1]; |
| 148 | } __attribute__((__packed__)); |
| 149 | |
| 150 | /** |
| 151 | * This function is used to calculate the disk space required to |
| 152 | * store a filename of length l in encrypted symlink format. |
| 153 | */ |
| 154 | static inline u32 encrypted_symlink_data_len(u32 l) |
| 155 | { |
| 156 | if (l < EXT4_CRYPTO_BLOCK_SIZE) |
| 157 | l = EXT4_CRYPTO_BLOCK_SIZE; |
| 158 | return (l + sizeof(struct ext4_encrypted_symlink_data) - 1); |
| 159 | } |
| 160 | |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 161 | #endif /* _EXT4_CRYPTO_H */ |