Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * key management facility for FS encryption support. |
| 3 | * |
| 4 | * Copyright (C) 2015, Google, Inc. |
| 5 | * |
| 6 | * This contains encryption key functions. |
| 7 | * |
| 8 | * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. |
| 9 | */ |
| 10 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 11 | #include <keys/user-type.h> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 12 | #include <linux/scatterlist.h> |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 13 | #include <linux/ratelimit.h> |
| 14 | #include <crypto/aes.h> |
| 15 | #include <crypto/sha.h> |
Theodore Ts'o | 3325bea | 2016-11-26 20:32:46 -0500 | [diff] [blame] | 16 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 17 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 18 | static struct crypto_shash *essiv_hash_tfm; |
| 19 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 20 | /** |
| 21 | * derive_key_aes() - Derive a key using AES-128-ECB |
| 22 | * @deriving_key: Encryption key used for derivation. |
| 23 | * @source_key: Source key to which to apply derivation. |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 24 | * @derived_raw_key: Derived raw key. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 25 | * |
| 26 | * Return: Zero on success; non-zero otherwise. |
| 27 | */ |
| 28 | static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE], |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 29 | const struct fscrypt_key *source_key, |
| 30 | u8 derived_raw_key[FS_MAX_KEY_SIZE]) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 31 | { |
| 32 | int res = 0; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 33 | struct skcipher_request *req = NULL; |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame^] | 34 | DECLARE_CRYPTO_WAIT(wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 35 | struct scatterlist src_sg, dst_sg; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 36 | struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 37 | |
| 38 | if (IS_ERR(tfm)) { |
| 39 | res = PTR_ERR(tfm); |
| 40 | tfm = NULL; |
| 41 | goto out; |
| 42 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 43 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 44 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 45 | if (!req) { |
| 46 | res = -ENOMEM; |
| 47 | goto out; |
| 48 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 49 | skcipher_request_set_callback(req, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 50 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame^] | 51 | crypto_req_done, &wait); |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 52 | res = crypto_skcipher_setkey(tfm, deriving_key, |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 53 | FS_AES_128_ECB_KEY_SIZE); |
| 54 | if (res < 0) |
| 55 | goto out; |
| 56 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 57 | sg_init_one(&src_sg, source_key->raw, source_key->size); |
| 58 | sg_init_one(&dst_sg, derived_raw_key, source_key->size); |
| 59 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size, |
| 60 | NULL); |
Gilad Ben-Yossef | d0082e1 | 2017-10-18 08:00:44 +0100 | [diff] [blame^] | 61 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 62 | out: |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 63 | skcipher_request_free(req); |
| 64 | crypto_free_skcipher(tfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 65 | return res; |
| 66 | } |
| 67 | |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 68 | static int validate_user_key(struct fscrypt_info *crypt_info, |
| 69 | struct fscrypt_context *ctx, u8 *raw_key, |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 70 | const char *prefix, int min_keysize) |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 71 | { |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 72 | char *description; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 73 | struct key *keyring_key; |
| 74 | struct fscrypt_key *master_key; |
| 75 | const struct user_key_payload *ukp; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 76 | int res; |
| 77 | |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 78 | description = kasprintf(GFP_NOFS, "%s%*phN", prefix, |
| 79 | FS_KEY_DESCRIPTOR_SIZE, |
| 80 | ctx->master_key_descriptor); |
| 81 | if (!description) |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 82 | return -ENOMEM; |
| 83 | |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 84 | keyring_key = request_key(&key_type_logon, description, NULL); |
| 85 | kfree(description); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 86 | if (IS_ERR(keyring_key)) |
| 87 | return PTR_ERR(keyring_key); |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 88 | down_read(&keyring_key->sem); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 89 | |
| 90 | if (keyring_key->type != &key_type_logon) { |
| 91 | printk_once(KERN_WARNING |
| 92 | "%s: key type must be logon\n", __func__); |
| 93 | res = -ENOKEY; |
| 94 | goto out; |
| 95 | } |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 96 | ukp = user_key_payload_locked(keyring_key); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 97 | if (ukp->datalen != sizeof(struct fscrypt_key)) { |
| 98 | res = -EINVAL; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 99 | goto out; |
| 100 | } |
| 101 | master_key = (struct fscrypt_key *)ukp->data; |
| 102 | BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE); |
| 103 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 104 | if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE |
| 105 | || master_key->size % AES_BLOCK_SIZE != 0) { |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 106 | printk_once(KERN_WARNING |
| 107 | "%s: key size incorrect: %d\n", |
| 108 | __func__, master_key->size); |
| 109 | res = -ENOKEY; |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 110 | goto out; |
| 111 | } |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 112 | res = derive_key_aes(ctx->nonce, master_key, raw_key); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 113 | out: |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 114 | up_read(&keyring_key->sem); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 115 | key_put(keyring_key); |
| 116 | return res; |
| 117 | } |
| 118 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 119 | static const struct { |
| 120 | const char *cipher_str; |
| 121 | int keysize; |
| 122 | } available_modes[] = { |
| 123 | [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)", |
| 124 | FS_AES_256_XTS_KEY_SIZE }, |
| 125 | [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))", |
| 126 | FS_AES_256_CTS_KEY_SIZE }, |
| 127 | [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)", |
| 128 | FS_AES_128_CBC_KEY_SIZE }, |
| 129 | [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))", |
| 130 | FS_AES_128_CTS_KEY_SIZE }, |
| 131 | }; |
| 132 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 133 | static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode, |
| 134 | const char **cipher_str_ret, int *keysize_ret) |
| 135 | { |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 136 | u32 mode; |
| 137 | |
| 138 | if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) { |
| 139 | pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n", |
| 140 | inode->i_ino, |
| 141 | ci->ci_data_mode, ci->ci_filename_mode); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 145 | if (S_ISREG(inode->i_mode)) { |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 146 | mode = ci->ci_data_mode; |
| 147 | } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) { |
| 148 | mode = ci->ci_filename_mode; |
| 149 | } else { |
| 150 | WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", |
| 151 | inode->i_ino, (inode->i_mode & S_IFMT)); |
| 152 | return -EINVAL; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 155 | *cipher_str_ret = available_modes[mode].cipher_str; |
| 156 | *keysize_ret = available_modes[mode].keysize; |
| 157 | return 0; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 160 | static void put_crypt_info(struct fscrypt_info *ci) |
| 161 | { |
| 162 | if (!ci) |
| 163 | return; |
| 164 | |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 165 | crypto_free_skcipher(ci->ci_ctfm); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 166 | crypto_free_cipher(ci->ci_essiv_tfm); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 167 | kmem_cache_free(fscrypt_info_cachep, ci); |
| 168 | } |
| 169 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 170 | static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) |
| 171 | { |
| 172 | struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm); |
| 173 | |
| 174 | /* init hash transform on demand */ |
| 175 | if (unlikely(!tfm)) { |
| 176 | struct crypto_shash *prev_tfm; |
| 177 | |
| 178 | tfm = crypto_alloc_shash("sha256", 0, 0); |
| 179 | if (IS_ERR(tfm)) { |
| 180 | pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n", |
| 181 | PTR_ERR(tfm)); |
| 182 | return PTR_ERR(tfm); |
| 183 | } |
| 184 | prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm); |
| 185 | if (prev_tfm) { |
| 186 | crypto_free_shash(tfm); |
| 187 | tfm = prev_tfm; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | { |
| 192 | SHASH_DESC_ON_STACK(desc, tfm); |
| 193 | desc->tfm = tfm; |
| 194 | desc->flags = 0; |
| 195 | |
| 196 | return crypto_shash_digest(desc, key, keysize, salt); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key, |
| 201 | int keysize) |
| 202 | { |
| 203 | int err; |
| 204 | struct crypto_cipher *essiv_tfm; |
| 205 | u8 salt[SHA256_DIGEST_SIZE]; |
| 206 | |
| 207 | essiv_tfm = crypto_alloc_cipher("aes", 0, 0); |
| 208 | if (IS_ERR(essiv_tfm)) |
| 209 | return PTR_ERR(essiv_tfm); |
| 210 | |
| 211 | ci->ci_essiv_tfm = essiv_tfm; |
| 212 | |
| 213 | err = derive_essiv_salt(raw_key, keysize, salt); |
| 214 | if (err) |
| 215 | goto out; |
| 216 | |
| 217 | /* |
| 218 | * Using SHA256 to derive the salt/key will result in AES-256 being |
| 219 | * used for IV generation. File contents encryption will still use the |
| 220 | * configured keysize (AES-128) nevertheless. |
| 221 | */ |
| 222 | err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt)); |
| 223 | if (err) |
| 224 | goto out; |
| 225 | |
| 226 | out: |
| 227 | memzero_explicit(salt, sizeof(salt)); |
| 228 | return err; |
| 229 | } |
| 230 | |
| 231 | void __exit fscrypt_essiv_cleanup(void) |
| 232 | { |
| 233 | crypto_free_shash(essiv_hash_tfm); |
| 234 | } |
| 235 | |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 236 | int fscrypt_get_encryption_info(struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 237 | { |
| 238 | struct fscrypt_info *crypt_info; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 239 | struct fscrypt_context ctx; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 240 | struct crypto_skcipher *ctfm; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 241 | const char *cipher_str; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 242 | int keysize; |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 243 | u8 *raw_key = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 244 | int res; |
| 245 | |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 246 | if (inode->i_crypt_info) |
| 247 | return 0; |
| 248 | |
David Gstir | f32d7ac | 2016-12-06 23:53:57 +0100 | [diff] [blame] | 249 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 250 | if (res) |
| 251 | return res; |
| 252 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 253 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 254 | if (res < 0) { |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 255 | if (!fscrypt_dummy_context_enabled(inode) || |
| 256 | inode->i_sb->s_cop->is_encrypted(inode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 257 | return res; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 258 | /* Fake up a context for an unencrypted directory */ |
| 259 | memset(&ctx, 0, sizeof(ctx)); |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 260 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 261 | ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 262 | ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 263 | memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 264 | } else if (res != sizeof(ctx)) { |
| 265 | return -EINVAL; |
| 266 | } |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 267 | |
| 268 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) |
| 272 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 273 | |
| 274 | crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS); |
| 275 | if (!crypt_info) |
| 276 | return -ENOMEM; |
| 277 | |
| 278 | crypt_info->ci_flags = ctx.flags; |
| 279 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 280 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
| 281 | crypt_info->ci_ctfm = NULL; |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 282 | crypt_info->ci_essiv_tfm = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 283 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, |
| 284 | sizeof(crypt_info->ci_master_key)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 285 | |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 286 | res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize); |
| 287 | if (res) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 288 | goto out; |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 289 | |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 290 | /* |
| 291 | * This cannot be a stack buffer because it is passed to the scatterlist |
| 292 | * crypto API as part of key derivation. |
| 293 | */ |
| 294 | res = -ENOMEM; |
| 295 | raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS); |
| 296 | if (!raw_key) |
| 297 | goto out; |
| 298 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 299 | res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX, |
| 300 | keysize); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 301 | if (res && inode->i_sb->s_cop->key_prefix) { |
Eric Biggers | a5d431e | 2017-01-05 13:51:18 -0800 | [diff] [blame] | 302 | int res2 = validate_user_key(crypt_info, &ctx, raw_key, |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 303 | inode->i_sb->s_cop->key_prefix, |
| 304 | keysize); |
Jaegeuk Kim | b5a7aef | 2016-05-04 22:05:01 -0700 | [diff] [blame] | 305 | if (res2) { |
| 306 | if (res2 == -ENOKEY) |
| 307 | res = -ENOKEY; |
| 308 | goto out; |
| 309 | } |
| 310 | } else if (res) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 311 | goto out; |
| 312 | } |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 313 | ctfm = crypto_alloc_skcipher(cipher_str, 0, 0); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 314 | if (!ctfm || IS_ERR(ctfm)) { |
| 315 | res = ctfm ? PTR_ERR(ctfm) : -ENOMEM; |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 316 | pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n", |
| 317 | __func__, res, inode->i_ino); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 318 | goto out; |
| 319 | } |
| 320 | crypt_info->ci_ctfm = ctfm; |
Linus Torvalds | d407574 | 2016-03-21 11:03:02 -0700 | [diff] [blame] | 321 | crypto_skcipher_clear_flags(ctfm, ~0); |
| 322 | crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY); |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 323 | /* |
| 324 | * if the provided key is longer than keysize, we use the first |
| 325 | * keysize bytes of the derived key only |
| 326 | */ |
Eric Biggers | 8f39850 | 2016-09-15 13:32:11 -0400 | [diff] [blame] | 327 | res = crypto_skcipher_setkey(ctfm, raw_key, keysize); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 328 | if (res) |
| 329 | goto out; |
| 330 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 331 | if (S_ISREG(inode->i_mode) && |
| 332 | crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) { |
| 333 | res = init_essiv_generator(crypt_info, raw_key, keysize); |
| 334 | if (res) { |
| 335 | pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n", |
| 336 | __func__, res, inode->i_ino); |
| 337 | goto out; |
| 338 | } |
| 339 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 340 | if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL) |
| 341 | crypt_info = NULL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 342 | out: |
| 343 | if (res == -ENOKEY) |
| 344 | res = 0; |
| 345 | put_crypt_info(crypt_info); |
Eric Biggers | a6e0891 | 2016-11-13 20:41:09 -0500 | [diff] [blame] | 346 | kzfree(raw_key); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 347 | return res; |
| 348 | } |
Eric Biggers | 1b53cf9 | 2017-02-21 15:07:11 -0800 | [diff] [blame] | 349 | EXPORT_SYMBOL(fscrypt_get_encryption_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 350 | |
| 351 | void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci) |
| 352 | { |
| 353 | struct fscrypt_info *prev; |
| 354 | |
| 355 | if (ci == NULL) |
| 356 | ci = ACCESS_ONCE(inode->i_crypt_info); |
| 357 | if (ci == NULL) |
| 358 | return; |
| 359 | |
| 360 | prev = cmpxchg(&inode->i_crypt_info, ci, NULL); |
| 361 | if (prev != ci) |
| 362 | return; |
| 363 | |
| 364 | put_crypt_info(ci); |
| 365 | } |
| 366 | EXPORT_SYMBOL(fscrypt_put_encryption_info); |