ext4 crypto: add ext4 encryption facilities
[linux-2.6-block.git] / fs / ext4 / ext4_crypto.h
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;
23         char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
24 } __attribute__((__packed__));
25
26 #define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1
27 #define EXT4_KEY_DERIVATION_NONCE_SIZE 16
28
29 /**
30  * Encryption context for inode
31  *
32  * Protector format:
33  *  1 byte: Protector format (1 = this version)
34  *  1 byte: File contents encryption mode
35  *  1 byte: File names encryption mode
36  *  1 byte: Reserved
37  *  8 bytes: Master Key descriptor
38  *  16 bytes: Encryption Key derivation nonce
39  */
40 struct ext4_encryption_context {
41         char format;
42         char contents_encryption_mode;
43         char filenames_encryption_mode;
44         char reserved;
45         char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
46         char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE];
47 } __attribute__((__packed__));
48
49 /* Encryption parameters */
50 #define EXT4_XTS_TWEAK_SIZE 16
51 #define EXT4_AES_128_ECB_KEY_SIZE 16
52 #define EXT4_AES_256_GCM_KEY_SIZE 32
53 #define EXT4_AES_256_CBC_KEY_SIZE 32
54 #define EXT4_AES_256_CTS_KEY_SIZE 32
55 #define EXT4_AES_256_XTS_KEY_SIZE 64
56 #define EXT4_MAX_KEY_SIZE 64
57
58 struct ext4_encryption_key {
59         uint32_t mode;
60         char raw[EXT4_MAX_KEY_SIZE];
61         uint32_t size;
62 };
63
64 #define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL             0x00000001
65 #define EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL     0x00000002
66
67 struct ext4_crypto_ctx {
68         struct crypto_tfm *tfm;         /* Crypto API context */
69         struct page *bounce_page;       /* Ciphertext page on write path */
70         struct page *control_page;      /* Original page on write path */
71         struct bio *bio;                /* The bio for this context */
72         struct work_struct work;        /* Work queue for read complete path */
73         struct list_head free_list;     /* Free list */
74         int flags;                      /* Flags */
75         int mode;                       /* Encryption mode for tfm */
76 };
77
78 struct ext4_completion_result {
79         struct completion completion;
80         int res;
81 };
82
83 #define DECLARE_EXT4_COMPLETION_RESULT(ecr) \
84         struct ext4_completion_result ecr = { \
85                 COMPLETION_INITIALIZER((ecr).completion), 0 }
86
87 static inline int ext4_encryption_key_size(int mode)
88 {
89         switch (mode) {
90         case EXT4_ENCRYPTION_MODE_AES_256_XTS:
91                 return EXT4_AES_256_XTS_KEY_SIZE;
92         case EXT4_ENCRYPTION_MODE_AES_256_GCM:
93                 return EXT4_AES_256_GCM_KEY_SIZE;
94         case EXT4_ENCRYPTION_MODE_AES_256_CBC:
95                 return EXT4_AES_256_CBC_KEY_SIZE;
96         case EXT4_ENCRYPTION_MODE_AES_256_CTS:
97                 return EXT4_AES_256_CTS_KEY_SIZE;
98         default:
99                 BUG();
100         }
101         return 0;
102 }
103
104 #endif  /* _EXT4_CRYPTO_H */