Commit | Line | Data |
---|---|---|
cde4de12 JK |
1 | /* |
2 | * linux/fs/f2fs/f2fs_crypto.h | |
3 | * | |
4 | * Copied from linux/fs/ext4/ext4_crypto.h | |
5 | * | |
6 | * Copyright (C) 2015, Google, Inc. | |
7 | * | |
8 | * This contains encryption header content for f2fs | |
9 | * | |
10 | * Written by Michael Halcrow, 2015. | |
11 | * Modified by Jaegeuk Kim, 2015. | |
12 | */ | |
13 | #ifndef _F2FS_CRYPTO_H | |
14 | #define _F2FS_CRYPTO_H | |
15 | ||
16 | #include <linux/fs.h> | |
17 | ||
18 | #define F2FS_KEY_DESCRIPTOR_SIZE 8 | |
19 | ||
20 | /* Policy provided via an ioctl on the topmost directory */ | |
21 | struct f2fs_encryption_policy { | |
22 | char version; | |
23 | char contents_encryption_mode; | |
24 | char filenames_encryption_mode; | |
25 | char flags; | |
26 | char master_key_descriptor[F2FS_KEY_DESCRIPTOR_SIZE]; | |
27 | } __attribute__((__packed__)); | |
28 | ||
29 | #define F2FS_ENCRYPTION_CONTEXT_FORMAT_V1 1 | |
30 | #define F2FS_KEY_DERIVATION_NONCE_SIZE 16 | |
31 | ||
32 | #define F2FS_POLICY_FLAGS_PAD_4 0x00 | |
33 | #define F2FS_POLICY_FLAGS_PAD_8 0x01 | |
34 | #define F2FS_POLICY_FLAGS_PAD_16 0x02 | |
35 | #define F2FS_POLICY_FLAGS_PAD_32 0x03 | |
36 | #define F2FS_POLICY_FLAGS_PAD_MASK 0x03 | |
37 | #define F2FS_POLICY_FLAGS_VALID 0x03 | |
38 | ||
39 | /** | |
40 | * Encryption context for inode | |
41 | * | |
42 | * Protector format: | |
43 | * 1 byte: Protector format (1 = this version) | |
44 | * 1 byte: File contents encryption mode | |
45 | * 1 byte: File names encryption mode | |
46 | * 1 byte: Flags | |
47 | * 8 bytes: Master Key descriptor | |
48 | * 16 bytes: Encryption Key derivation nonce | |
49 | */ | |
50 | struct f2fs_encryption_context { | |
51 | char format; | |
52 | char contents_encryption_mode; | |
53 | char filenames_encryption_mode; | |
54 | char flags; | |
55 | char master_key_descriptor[F2FS_KEY_DESCRIPTOR_SIZE]; | |
56 | char nonce[F2FS_KEY_DERIVATION_NONCE_SIZE]; | |
57 | } __attribute__((__packed__)); | |
58 | ||
59 | /* Encryption parameters */ | |
60 | #define F2FS_XTS_TWEAK_SIZE 16 | |
61 | #define F2FS_AES_128_ECB_KEY_SIZE 16 | |
62 | #define F2FS_AES_256_GCM_KEY_SIZE 32 | |
63 | #define F2FS_AES_256_CBC_KEY_SIZE 32 | |
64 | #define F2FS_AES_256_CTS_KEY_SIZE 32 | |
65 | #define F2FS_AES_256_XTS_KEY_SIZE 64 | |
66 | #define F2FS_MAX_KEY_SIZE 64 | |
67 | ||
68 | struct f2fs_encryption_key { | |
69 | __u32 mode; | |
70 | char raw[F2FS_MAX_KEY_SIZE]; | |
71 | __u32 size; | |
72 | } __attribute__((__packed__)); | |
73 | ||
74 | struct f2fs_crypt_info { | |
75 | unsigned char ci_mode; | |
76 | unsigned char ci_size; | |
77 | char ci_data_mode; | |
78 | char ci_filename_mode; | |
79 | char ci_flags; | |
80 | struct crypto_ablkcipher *ci_ctfm; | |
81 | struct key *ci_keyring_key; | |
82 | char ci_raw[F2FS_MAX_KEY_SIZE]; | |
83 | char ci_master_key[F2FS_KEY_DESCRIPTOR_SIZE]; | |
84 | }; | |
85 | ||
86 | #define F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001 | |
87 | #define F2FS_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL 0x00000002 | |
88 | ||
89 | struct f2fs_crypto_ctx { | |
90 | struct crypto_tfm *tfm; /* Crypto API context */ | |
91 | struct page *bounce_page; /* Ciphertext page on write path */ | |
92 | struct page *control_page; /* Original page on write path */ | |
93 | struct bio *bio; /* The bio for this context */ | |
94 | struct work_struct work; /* Work queue for read complete path */ | |
95 | struct list_head free_list; /* Free list */ | |
96 | int flags; /* Flags */ | |
97 | int mode; /* Encryption mode for tfm */ | |
98 | }; | |
99 | ||
100 | struct f2fs_completion_result { | |
101 | struct completion completion; | |
102 | int res; | |
103 | }; | |
104 | ||
105 | #define DECLARE_F2FS_COMPLETION_RESULT(ecr) \ | |
106 | struct f2fs_completion_result ecr = { \ | |
107 | COMPLETION_INITIALIZER((ecr).completion), 0 } | |
108 | ||
109 | static inline int f2fs_encryption_key_size(int mode) | |
110 | { | |
111 | switch (mode) { | |
112 | case F2FS_ENCRYPTION_MODE_AES_256_XTS: | |
113 | return F2FS_AES_256_XTS_KEY_SIZE; | |
114 | case F2FS_ENCRYPTION_MODE_AES_256_GCM: | |
115 | return F2FS_AES_256_GCM_KEY_SIZE; | |
116 | case F2FS_ENCRYPTION_MODE_AES_256_CBC: | |
117 | return F2FS_AES_256_CBC_KEY_SIZE; | |
118 | case F2FS_ENCRYPTION_MODE_AES_256_CTS: | |
119 | return F2FS_AES_256_CTS_KEY_SIZE; | |
120 | default: | |
121 | BUG(); | |
122 | } | |
123 | return 0; | |
124 | } | |
125 | ||
126 | #define F2FS_FNAME_NUM_SCATTER_ENTRIES 4 | |
127 | #define F2FS_CRYPTO_BLOCK_SIZE 16 | |
128 | #define F2FS_FNAME_CRYPTO_DIGEST_SIZE 32 | |
129 | ||
130 | /** | |
131 | * For encrypted symlinks, the ciphertext length is stored at the beginning | |
132 | * of the string in little-endian format. | |
133 | */ | |
134 | struct f2fs_encrypted_symlink_data { | |
135 | __le16 len; | |
136 | char encrypted_path[1]; | |
137 | } __attribute__((__packed__)); | |
138 | ||
139 | /** | |
140 | * This function is used to calculate the disk space required to | |
141 | * store a filename of length l in encrypted symlink format. | |
142 | */ | |
143 | static inline u32 encrypted_symlink_data_len(u32 l) | |
144 | { | |
145 | return (l + sizeof(struct f2fs_encrypted_symlink_data) - 1); | |
146 | } | |
147 | #endif /* _F2FS_CRYPTO_H */ |