2 * This contains functions for filename crypto management
4 * Copyright (C) 2015, Google, Inc.
5 * Copyright (C) 2015, Motorola Mobility
7 * Written by Uday Savagaonkar, 2014.
8 * Modified by Jaegeuk Kim, 2015.
10 * This has not yet undergone a rigorous security audit.
13 #include <linux/scatterlist.h>
14 #include <linux/ratelimit.h>
15 #include <linux/fscrypto.h>
17 static u32 size_round_up(size_t size, size_t blksize)
19 return ((size + blksize - 1) / blksize) * blksize;
23 * fname_crypt_complete() - completion callback for filename crypto
24 * @req: The asynchronous cipher request context
25 * @res: The result of the cipher operation
27 static void fname_crypt_complete(struct crypto_async_request *req, int res)
29 struct fscrypt_completion_result *ecr = req->data;
31 if (res == -EINPROGRESS)
34 complete(&ecr->completion);
38 * fname_encrypt() - encrypt a filename
40 * The caller must have allocated sufficient memory for the @oname string.
42 * Return: 0 on success, -errno on failure
44 static int fname_encrypt(struct inode *inode,
45 const struct qstr *iname, struct fscrypt_str *oname)
48 struct skcipher_request *req = NULL;
49 DECLARE_FS_COMPLETION_RESULT(ecr);
50 struct fscrypt_info *ci = inode->i_crypt_info;
51 struct crypto_skcipher *tfm = ci->ci_ctfm;
53 char iv[FS_CRYPTO_BLOCK_SIZE];
54 struct scatterlist src_sg, dst_sg;
55 int padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
56 char *workbuf, buf[32], *alloc_buf = NULL;
59 lim = inode->i_sb->s_cop->max_namelen(inode);
60 if (iname->len <= 0 || iname->len > lim)
63 ciphertext_len = (iname->len < FS_CRYPTO_BLOCK_SIZE) ?
64 FS_CRYPTO_BLOCK_SIZE : iname->len;
65 ciphertext_len = size_round_up(ciphertext_len, padding);
66 ciphertext_len = (ciphertext_len > lim) ? lim : ciphertext_len;
68 if (ciphertext_len <= sizeof(buf)) {
71 alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
77 /* Allocate request */
78 req = skcipher_request_alloc(tfm, GFP_NOFS);
80 printk_ratelimited(KERN_ERR
81 "%s: crypto_request_alloc() failed\n", __func__);
85 skcipher_request_set_callback(req,
86 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
87 fname_crypt_complete, &ecr);
90 memcpy(workbuf, iname->name, iname->len);
91 if (iname->len < ciphertext_len)
92 memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
95 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
97 /* Create encryption request */
98 sg_init_one(&src_sg, workbuf, ciphertext_len);
99 sg_init_one(&dst_sg, oname->name, ciphertext_len);
100 skcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
101 res = crypto_skcipher_encrypt(req);
102 if (res == -EINPROGRESS || res == -EBUSY) {
103 wait_for_completion(&ecr.completion);
107 skcipher_request_free(req);
109 printk_ratelimited(KERN_ERR
110 "%s: Error (error code %d)\n", __func__, res);
114 oname->len = ciphertext_len;
119 * fname_decrypt() - decrypt a filename
121 * The caller must have allocated sufficient memory for the @oname string.
123 * Return: 0 on success, -errno on failure
125 static int fname_decrypt(struct inode *inode,
126 const struct fscrypt_str *iname,
127 struct fscrypt_str *oname)
129 struct skcipher_request *req = NULL;
130 DECLARE_FS_COMPLETION_RESULT(ecr);
131 struct scatterlist src_sg, dst_sg;
132 struct fscrypt_info *ci = inode->i_crypt_info;
133 struct crypto_skcipher *tfm = ci->ci_ctfm;
135 char iv[FS_CRYPTO_BLOCK_SIZE];
138 lim = inode->i_sb->s_cop->max_namelen(inode);
139 if (iname->len <= 0 || iname->len > lim)
142 /* Allocate request */
143 req = skcipher_request_alloc(tfm, GFP_NOFS);
145 printk_ratelimited(KERN_ERR
146 "%s: crypto_request_alloc() failed\n", __func__);
149 skcipher_request_set_callback(req,
150 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
151 fname_crypt_complete, &ecr);
154 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
156 /* Create decryption request */
157 sg_init_one(&src_sg, iname->name, iname->len);
158 sg_init_one(&dst_sg, oname->name, oname->len);
159 skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
160 res = crypto_skcipher_decrypt(req);
161 if (res == -EINPROGRESS || res == -EBUSY) {
162 wait_for_completion(&ecr.completion);
165 skcipher_request_free(req);
167 printk_ratelimited(KERN_ERR
168 "%s: Error (error code %d)\n", __func__, res);
172 oname->len = strnlen(oname->name, iname->len);
176 static const char *lookup_table =
177 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
182 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
183 * The encoded string is roughly 4/3 times the size of the input string.
185 static int digest_encode(const char *src, int len, char *dst)
187 int i = 0, bits = 0, ac = 0;
191 ac += (((unsigned char) src[i]) << bits);
194 *cp++ = lookup_table[ac & 0x3f];
201 *cp++ = lookup_table[ac & 0x3f];
205 static int digest_decode(const char *src, int len, char *dst)
207 int i = 0, bits = 0, ac = 0;
212 p = strchr(lookup_table, src[i]);
213 if (p == NULL || src[i] == 0)
215 ac += (p - lookup_table) << bits;
229 u32 fscrypt_fname_encrypted_size(struct inode *inode, u32 ilen)
232 struct fscrypt_info *ci = inode->i_crypt_info;
235 padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
236 if (ilen < FS_CRYPTO_BLOCK_SIZE)
237 ilen = FS_CRYPTO_BLOCK_SIZE;
238 return size_round_up(ilen, padding);
240 EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
243 * fscrypt_fname_crypto_alloc_obuff() -
245 * Allocates an output buffer that is sufficient for the crypto operation
246 * specified by the context and the direction.
248 int fscrypt_fname_alloc_buffer(struct inode *inode,
249 u32 ilen, struct fscrypt_str *crypto_str)
251 unsigned int olen = fscrypt_fname_encrypted_size(inode, ilen);
253 crypto_str->len = olen;
254 if (olen < FS_FNAME_CRYPTO_DIGEST_SIZE * 2)
255 olen = FS_FNAME_CRYPTO_DIGEST_SIZE * 2;
257 * Allocated buffer can hold one more character to null-terminate the
260 crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
261 if (!(crypto_str->name))
265 EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
268 * fscrypt_fname_crypto_free_buffer() -
270 * Frees the buffer allocated for crypto operation.
272 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
276 kfree(crypto_str->name);
277 crypto_str->name = NULL;
279 EXPORT_SYMBOL(fscrypt_fname_free_buffer);
282 * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
285 * The caller must have allocated sufficient memory for the @oname string.
287 * Return: 0 on success, -errno on failure
289 int fscrypt_fname_disk_to_usr(struct inode *inode,
290 u32 hash, u32 minor_hash,
291 const struct fscrypt_str *iname,
292 struct fscrypt_str *oname)
294 const struct qstr qname = FSTR_TO_QSTR(iname);
297 if (fscrypt_is_dot_dotdot(&qname)) {
298 oname->name[0] = '.';
299 oname->name[iname->len - 1] = '.';
300 oname->len = iname->len;
304 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
307 if (inode->i_crypt_info)
308 return fname_decrypt(inode, iname, oname);
310 if (iname->len <= FS_FNAME_CRYPTO_DIGEST_SIZE) {
311 oname->len = digest_encode(iname->name, iname->len,
316 memcpy(buf, &hash, 4);
317 memcpy(buf + 4, &minor_hash, 4);
321 memcpy(buf + 8, iname->name + iname->len - 16, 16);
322 oname->name[0] = '_';
323 oname->len = 1 + digest_encode(buf, 24, oname->name + 1);
326 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
329 * fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
332 * The caller must have allocated sufficient memory for the @oname string.
334 * Return: 0 on success, -errno on failure
336 int fscrypt_fname_usr_to_disk(struct inode *inode,
337 const struct qstr *iname,
338 struct fscrypt_str *oname)
340 if (fscrypt_is_dot_dotdot(iname)) {
341 oname->name[0] = '.';
342 oname->name[iname->len - 1] = '.';
343 oname->len = iname->len;
346 if (inode->i_crypt_info)
347 return fname_encrypt(inode, iname, oname);
349 * Without a proper key, a user is not allowed to modify the filenames
350 * in a directory. Consequently, a user space name cannot be mapped to
355 EXPORT_SYMBOL(fscrypt_fname_usr_to_disk);
357 int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
358 int lookup, struct fscrypt_name *fname)
360 int ret = 0, bigname = 0;
362 memset(fname, 0, sizeof(struct fscrypt_name));
363 fname->usr_fname = iname;
365 if (!dir->i_sb->s_cop->is_encrypted(dir) ||
366 fscrypt_is_dot_dotdot(iname)) {
367 fname->disk_name.name = (unsigned char *)iname->name;
368 fname->disk_name.len = iname->len;
371 ret = get_crypt_info(dir);
372 if (ret && ret != -EOPNOTSUPP)
375 if (dir->i_crypt_info) {
376 ret = fscrypt_fname_alloc_buffer(dir, iname->len,
380 ret = fname_encrypt(dir, iname, &fname->crypto_buf);
383 fname->disk_name.name = fname->crypto_buf.name;
384 fname->disk_name.len = fname->crypto_buf.len;
391 * We don't have the key and we are doing a lookup; decode the
394 if (iname->name[0] == '_')
396 if ((bigname && (iname->len != 33)) || (!bigname && (iname->len > 43)))
399 fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
400 if (fname->crypto_buf.name == NULL)
403 ret = digest_decode(iname->name + bigname, iname->len - bigname,
404 fname->crypto_buf.name);
409 fname->crypto_buf.len = ret;
411 memcpy(&fname->hash, fname->crypto_buf.name, 4);
412 memcpy(&fname->minor_hash, fname->crypto_buf.name + 4, 4);
414 fname->disk_name.name = fname->crypto_buf.name;
415 fname->disk_name.len = fname->crypto_buf.len;
420 fscrypt_fname_free_buffer(&fname->crypto_buf);
423 EXPORT_SYMBOL(fscrypt_setup_filename);
425 void fscrypt_free_filename(struct fscrypt_name *fname)
427 kfree(fname->crypto_buf.name);
428 fname->crypto_buf.name = NULL;
429 fname->usr_fname = NULL;
430 fname->disk_name.name = NULL;
432 EXPORT_SYMBOL(fscrypt_free_filename);