Jaegeuk Kim | 0adda90 | 2015-04-21 16:23:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/f2fs/crypto_key.c |
| 3 | * |
| 4 | * Copied from linux/fs/f2fs/crypto_key.c |
| 5 | * |
| 6 | * Copyright (C) 2015, Google, Inc. |
| 7 | * |
| 8 | * This contains encryption key functions for f2fs |
| 9 | * |
| 10 | * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. |
| 11 | */ |
| 12 | #include <keys/encrypted-type.h> |
| 13 | #include <keys/user-type.h> |
| 14 | #include <linux/random.h> |
| 15 | #include <linux/scatterlist.h> |
| 16 | #include <uapi/linux/keyctl.h> |
| 17 | #include <crypto/hash.h> |
| 18 | #include <linux/f2fs_fs.h> |
| 19 | |
| 20 | #include "f2fs.h" |
| 21 | #include "xattr.h" |
| 22 | |
| 23 | static void derive_crypt_complete(struct crypto_async_request *req, int rc) |
| 24 | { |
| 25 | struct f2fs_completion_result *ecr = req->data; |
| 26 | |
| 27 | if (rc == -EINPROGRESS) |
| 28 | return; |
| 29 | |
| 30 | ecr->res = rc; |
| 31 | complete(&ecr->completion); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * f2fs_derive_key_aes() - Derive a key using AES-128-ECB |
| 36 | * @deriving_key: Encryption key used for derivatio. |
| 37 | * @source_key: Source key to which to apply derivation. |
| 38 | * @derived_key: Derived key. |
| 39 | * |
| 40 | * Return: Zero on success; non-zero otherwise. |
| 41 | */ |
| 42 | static int f2fs_derive_key_aes(char deriving_key[F2FS_AES_128_ECB_KEY_SIZE], |
| 43 | char source_key[F2FS_AES_256_XTS_KEY_SIZE], |
| 44 | char derived_key[F2FS_AES_256_XTS_KEY_SIZE]) |
| 45 | { |
| 46 | int res = 0; |
| 47 | struct ablkcipher_request *req = NULL; |
| 48 | DECLARE_F2FS_COMPLETION_RESULT(ecr); |
| 49 | struct scatterlist src_sg, dst_sg; |
| 50 | struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0, |
| 51 | 0); |
| 52 | |
| 53 | if (IS_ERR(tfm)) { |
| 54 | res = PTR_ERR(tfm); |
| 55 | tfm = NULL; |
| 56 | goto out; |
| 57 | } |
| 58 | crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 59 | req = ablkcipher_request_alloc(tfm, GFP_NOFS); |
| 60 | if (!req) { |
| 61 | res = -ENOMEM; |
| 62 | goto out; |
| 63 | } |
| 64 | ablkcipher_request_set_callback(req, |
| 65 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 66 | derive_crypt_complete, &ecr); |
| 67 | res = crypto_ablkcipher_setkey(tfm, deriving_key, |
| 68 | F2FS_AES_128_ECB_KEY_SIZE); |
| 69 | if (res < 0) |
| 70 | goto out; |
| 71 | |
| 72 | sg_init_one(&src_sg, source_key, F2FS_AES_256_XTS_KEY_SIZE); |
| 73 | sg_init_one(&dst_sg, derived_key, F2FS_AES_256_XTS_KEY_SIZE); |
| 74 | ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, |
| 75 | F2FS_AES_256_XTS_KEY_SIZE, NULL); |
| 76 | res = crypto_ablkcipher_encrypt(req); |
| 77 | if (res == -EINPROGRESS || res == -EBUSY) { |
| 78 | BUG_ON(req->base.data != &ecr); |
| 79 | wait_for_completion(&ecr.completion); |
| 80 | res = ecr.res; |
| 81 | } |
| 82 | out: |
| 83 | if (req) |
| 84 | ablkcipher_request_free(req); |
| 85 | if (tfm) |
| 86 | crypto_free_ablkcipher(tfm); |
| 87 | return res; |
| 88 | } |
| 89 | |
| 90 | void f2fs_free_encryption_info(struct inode *inode) |
| 91 | { |
| 92 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 93 | struct f2fs_crypt_info *ci = fi->i_crypt_info; |
| 94 | |
| 95 | if (!ci) |
| 96 | return; |
| 97 | |
| 98 | if (ci->ci_keyring_key) |
| 99 | key_put(ci->ci_keyring_key); |
| 100 | crypto_free_ablkcipher(ci->ci_ctfm); |
| 101 | memzero_explicit(&ci->ci_raw, sizeof(ci->ci_raw)); |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame^] | 102 | kmem_cache_free(f2fs_crypt_info_cachep, ci); |
Jaegeuk Kim | 0adda90 | 2015-04-21 16:23:47 -0700 | [diff] [blame] | 103 | fi->i_crypt_info = NULL; |
| 104 | } |
| 105 | |
| 106 | int _f2fs_get_encryption_info(struct inode *inode) |
| 107 | { |
| 108 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 109 | struct f2fs_crypt_info *crypt_info; |
| 110 | char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE + |
| 111 | (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1]; |
| 112 | struct key *keyring_key = NULL; |
| 113 | struct f2fs_encryption_key *master_key; |
| 114 | struct f2fs_encryption_context ctx; |
| 115 | struct user_key_payload *ukp; |
| 116 | int res; |
| 117 | |
| 118 | if (!f2fs_read_workqueue) { |
| 119 | res = f2fs_init_crypto(); |
| 120 | if (res) |
| 121 | return res; |
| 122 | } |
| 123 | |
| 124 | if (fi->i_crypt_info) { |
| 125 | if (!fi->i_crypt_info->ci_keyring_key || |
| 126 | key_validate(fi->i_crypt_info->ci_keyring_key) == 0) |
| 127 | return 0; |
| 128 | f2fs_free_encryption_info(inode); |
| 129 | } |
| 130 | |
| 131 | res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION, |
| 132 | F2FS_XATTR_NAME_ENCRYPTION_CONTEXT, |
| 133 | &ctx, sizeof(ctx), NULL); |
| 134 | if (res < 0) |
| 135 | return res; |
| 136 | else if (res != sizeof(ctx)) |
| 137 | return -EINVAL; |
| 138 | res = 0; |
| 139 | |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame^] | 140 | crypt_info = kmem_cache_alloc(f2fs_crypt_info_cachep, GFP_NOFS); |
Jaegeuk Kim | 0adda90 | 2015-04-21 16:23:47 -0700 | [diff] [blame] | 141 | if (!crypt_info) |
| 142 | return -ENOMEM; |
| 143 | |
| 144 | crypt_info->ci_flags = ctx.flags; |
| 145 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 146 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
| 147 | crypt_info->ci_ctfm = NULL; |
| 148 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, |
| 149 | sizeof(crypt_info->ci_master_key)); |
| 150 | if (S_ISREG(inode->i_mode)) |
| 151 | crypt_info->ci_mode = ctx.contents_encryption_mode; |
| 152 | else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 153 | crypt_info->ci_mode = ctx.filenames_encryption_mode; |
| 154 | else { |
| 155 | printk(KERN_ERR "f2fs crypto: Unsupported inode type.\n"); |
| 156 | BUG(); |
| 157 | } |
| 158 | crypt_info->ci_size = f2fs_encryption_key_size(crypt_info->ci_mode); |
| 159 | BUG_ON(!crypt_info->ci_size); |
| 160 | |
| 161 | memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX, |
| 162 | F2FS_KEY_DESC_PREFIX_SIZE); |
| 163 | sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE, |
| 164 | "%*phN", F2FS_KEY_DESCRIPTOR_SIZE, |
| 165 | ctx.master_key_descriptor); |
| 166 | full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE + |
| 167 | (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0'; |
| 168 | keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL); |
| 169 | if (IS_ERR(keyring_key)) { |
| 170 | res = PTR_ERR(keyring_key); |
| 171 | keyring_key = NULL; |
| 172 | goto out; |
| 173 | } |
| 174 | BUG_ON(keyring_key->type != &key_type_logon); |
| 175 | ukp = ((struct user_key_payload *)keyring_key->payload.data); |
| 176 | if (ukp->datalen != sizeof(struct f2fs_encryption_key)) { |
| 177 | res = -EINVAL; |
| 178 | goto out; |
| 179 | } |
| 180 | master_key = (struct f2fs_encryption_key *)ukp->data; |
| 181 | BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE != |
| 182 | F2FS_KEY_DERIVATION_NONCE_SIZE); |
| 183 | BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE); |
| 184 | res = f2fs_derive_key_aes(ctx.nonce, master_key->raw, |
| 185 | crypt_info->ci_raw); |
| 186 | out: |
| 187 | if (res < 0) { |
| 188 | if (res == -ENOKEY) |
| 189 | res = 0; |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame^] | 190 | kmem_cache_free(f2fs_crypt_info_cachep, crypt_info); |
Jaegeuk Kim | 0adda90 | 2015-04-21 16:23:47 -0700 | [diff] [blame] | 191 | } else { |
| 192 | fi->i_crypt_info = crypt_info; |
| 193 | crypt_info->ci_keyring_key = keyring_key; |
| 194 | keyring_key = NULL; |
| 195 | } |
| 196 | if (keyring_key) |
| 197 | key_put(keyring_key); |
| 198 | return res; |
| 199 | } |
| 200 | |
| 201 | int f2fs_has_encryption_key(struct inode *inode) |
| 202 | { |
| 203 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 204 | |
| 205 | return (fi->i_crypt_info != NULL); |
| 206 | } |