2 * linux/fs/ext4/ext4_crypto.h
4 * Copyright (C) 2015, Google, Inc.
6 * This contains encryption header content for ext4
8 * Written by Michael Halcrow, 2015.
11 #ifndef _EXT4_CRYPTO_H
12 #define _EXT4_CRYPTO_H
16 #define EXT4_KEY_DESCRIPTOR_SIZE 8
18 /* Policy provided via an ioctl on the topmost directory */
19 struct ext4_encryption_policy {
21 char contents_encryption_mode;
22 char filenames_encryption_mode;
24 char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
25 } __attribute__((__packed__));
27 #define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1
28 #define EXT4_KEY_DERIVATION_NONCE_SIZE 16
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
38 * Encryption context for inode
41 * 1 byte: Protector format (1 = this version)
42 * 1 byte: File contents encryption mode
43 * 1 byte: File names encryption mode
45 * 8 bytes: Master Key descriptor
46 * 16 bytes: Encryption Key derivation nonce
48 struct ext4_encryption_context {
50 char contents_encryption_mode;
51 char filenames_encryption_mode;
53 char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
54 char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE];
55 } __attribute__((__packed__));
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
66 #define EXT4_KEY_DESC_PREFIX "ext4:"
67 #define EXT4_KEY_DESC_PREFIX_SIZE 5
69 struct ext4_encryption_key {
71 char raw[EXT4_MAX_KEY_SIZE];
75 #define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
76 #define EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL 0x00000002
78 struct ext4_crypto_ctx {
79 struct crypto_tfm *tfm; /* Crypto API context */
80 struct page *bounce_page; /* Ciphertext page on write path */
81 struct page *control_page; /* Original page on write path */
82 struct bio *bio; /* The bio for this context */
83 struct work_struct work; /* Work queue for read complete path */
84 struct list_head free_list; /* Free list */
85 int flags; /* Flags */
86 int mode; /* Encryption mode for tfm */
89 struct ext4_completion_result {
90 struct completion completion;
94 #define DECLARE_EXT4_COMPLETION_RESULT(ecr) \
95 struct ext4_completion_result ecr = { \
96 COMPLETION_INITIALIZER((ecr).completion), 0 }
98 static inline int ext4_encryption_key_size(int mode)
101 case EXT4_ENCRYPTION_MODE_AES_256_XTS:
102 return EXT4_AES_256_XTS_KEY_SIZE;
103 case EXT4_ENCRYPTION_MODE_AES_256_GCM:
104 return EXT4_AES_256_GCM_KEY_SIZE;
105 case EXT4_ENCRYPTION_MODE_AES_256_CBC:
106 return EXT4_AES_256_CBC_KEY_SIZE;
107 case EXT4_ENCRYPTION_MODE_AES_256_CTS:
108 return EXT4_AES_256_CTS_KEY_SIZE;
115 #define EXT4_FNAME_NUM_SCATTER_ENTRIES 4
116 #define EXT4_CRYPTO_BLOCK_SIZE 16
117 #define EXT4_FNAME_CRYPTO_DIGEST_SIZE 32
124 struct ext4_fname_crypto_ctx {
126 char tmp_buf[EXT4_CRYPTO_BLOCK_SIZE];
127 struct crypto_ablkcipher *ctfm;
128 struct crypto_hash *htfm;
129 struct page *workpage;
130 struct ext4_encryption_key key;
132 unsigned has_valid_key : 1;
133 unsigned ctfm_key_is_ready : 1;
137 * For encrypted symlinks, the ciphertext length is stored at the beginning
138 * of the string in little-endian format.
140 struct ext4_encrypted_symlink_data {
142 char encrypted_path[1];
143 } __attribute__((__packed__));
146 * This function is used to calculate the disk space required to
147 * store a filename of length l in encrypted symlink format.
149 static inline u32 encrypted_symlink_data_len(u32 l)
151 if (l < EXT4_CRYPTO_BLOCK_SIZE)
152 l = EXT4_CRYPTO_BLOCK_SIZE;
153 return (l + sizeof(struct ext4_encrypted_symlink_data) - 1);
156 #endif /* _EXT4_CRYPTO_H */