Documentation/ABI: Update sysfs-driver-toshiba_acpi entry
[linux/fpc-iii.git] / net / ceph / crypto.c
blob790fe89d90c0ac49301bfcc81ba1b6633b9559cd
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/err.h>
5 #include <linux/scatterlist.h>
6 #include <linux/slab.h>
7 #include <crypto/hash.h>
8 #include <linux/key-type.h>
10 #include <keys/ceph-type.h>
11 #include <keys/user-type.h>
12 #include <linux/ceph/decode.h>
13 #include "crypto.h"
15 int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
16 const struct ceph_crypto_key *src)
18 memcpy(dst, src, sizeof(struct ceph_crypto_key));
19 dst->key = kmemdup(src->key, src->len, GFP_NOFS);
20 if (!dst->key)
21 return -ENOMEM;
22 return 0;
25 int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
27 if (*p + sizeof(u16) + sizeof(key->created) +
28 sizeof(u16) + key->len > end)
29 return -ERANGE;
30 ceph_encode_16(p, key->type);
31 ceph_encode_copy(p, &key->created, sizeof(key->created));
32 ceph_encode_16(p, key->len);
33 ceph_encode_copy(p, key->key, key->len);
34 return 0;
37 int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
39 ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad);
40 key->type = ceph_decode_16(p);
41 ceph_decode_copy(p, &key->created, sizeof(key->created));
42 key->len = ceph_decode_16(p);
43 ceph_decode_need(p, end, key->len, bad);
44 key->key = kmalloc(key->len, GFP_NOFS);
45 if (!key->key)
46 return -ENOMEM;
47 ceph_decode_copy(p, key->key, key->len);
48 return 0;
50 bad:
51 dout("failed to decode crypto key\n");
52 return -EINVAL;
55 int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
57 int inlen = strlen(inkey);
58 int blen = inlen * 3 / 4;
59 void *buf, *p;
60 int ret;
62 dout("crypto_key_unarmor %s\n", inkey);
63 buf = kmalloc(blen, GFP_NOFS);
64 if (!buf)
65 return -ENOMEM;
66 blen = ceph_unarmor(buf, inkey, inkey+inlen);
67 if (blen < 0) {
68 kfree(buf);
69 return blen;
72 p = buf;
73 ret = ceph_crypto_key_decode(key, &p, p + blen);
74 kfree(buf);
75 if (ret)
76 return ret;
77 dout("crypto_key_unarmor key %p type %d len %d\n", key,
78 key->type, key->len);
79 return 0;
84 #define AES_KEY_SIZE 16
86 static struct crypto_blkcipher *ceph_crypto_alloc_cipher(void)
88 return crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
91 static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
94 * Should be used for buffers allocated with ceph_kvmalloc().
95 * Currently these are encrypt out-buffer (ceph_buffer) and decrypt
96 * in-buffer (msg front).
98 * Dispose of @sgt with teardown_sgtable().
100 * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
101 * in cases where a single sg is sufficient. No attempt to reduce the
102 * number of sgs by squeezing physically contiguous pages together is
103 * made though, for simplicity.
105 static int setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
106 const void *buf, unsigned int buf_len)
108 struct scatterlist *sg;
109 const bool is_vmalloc = is_vmalloc_addr(buf);
110 unsigned int off = offset_in_page(buf);
111 unsigned int chunk_cnt = 1;
112 unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
113 int i;
114 int ret;
116 if (buf_len == 0) {
117 memset(sgt, 0, sizeof(*sgt));
118 return -EINVAL;
121 if (is_vmalloc) {
122 chunk_cnt = chunk_len >> PAGE_SHIFT;
123 chunk_len = PAGE_SIZE;
126 if (chunk_cnt > 1) {
127 ret = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
128 if (ret)
129 return ret;
130 } else {
131 WARN_ON(chunk_cnt != 1);
132 sg_init_table(prealloc_sg, 1);
133 sgt->sgl = prealloc_sg;
134 sgt->nents = sgt->orig_nents = 1;
137 for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
138 struct page *page;
139 unsigned int len = min(chunk_len - off, buf_len);
141 if (is_vmalloc)
142 page = vmalloc_to_page(buf);
143 else
144 page = virt_to_page(buf);
146 sg_set_page(sg, page, len, off);
148 off = 0;
149 buf += len;
150 buf_len -= len;
152 WARN_ON(buf_len != 0);
154 return 0;
157 static void teardown_sgtable(struct sg_table *sgt)
159 if (sgt->orig_nents > 1)
160 sg_free_table(sgt);
163 static int ceph_aes_encrypt(const void *key, int key_len,
164 void *dst, size_t *dst_len,
165 const void *src, size_t src_len)
167 struct scatterlist sg_in[2], prealloc_sg;
168 struct sg_table sg_out;
169 struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
170 struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
171 int ret;
172 void *iv;
173 int ivsize;
174 size_t zero_padding = (0x10 - (src_len & 0x0f));
175 char pad[16];
177 if (IS_ERR(tfm))
178 return PTR_ERR(tfm);
180 memset(pad, zero_padding, zero_padding);
182 *dst_len = src_len + zero_padding;
184 sg_init_table(sg_in, 2);
185 sg_set_buf(&sg_in[0], src, src_len);
186 sg_set_buf(&sg_in[1], pad, zero_padding);
187 ret = setup_sgtable(&sg_out, &prealloc_sg, dst, *dst_len);
188 if (ret)
189 goto out_tfm;
191 crypto_blkcipher_setkey((void *)tfm, key, key_len);
192 iv = crypto_blkcipher_crt(tfm)->iv;
193 ivsize = crypto_blkcipher_ivsize(tfm);
194 memcpy(iv, aes_iv, ivsize);
197 print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
198 key, key_len, 1);
199 print_hex_dump(KERN_ERR, "enc src: ", DUMP_PREFIX_NONE, 16, 1,
200 src, src_len, 1);
201 print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
202 pad, zero_padding, 1);
204 ret = crypto_blkcipher_encrypt(&desc, sg_out.sgl, sg_in,
205 src_len + zero_padding);
206 if (ret < 0) {
207 pr_err("ceph_aes_crypt failed %d\n", ret);
208 goto out_sg;
211 print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
212 dst, *dst_len, 1);
215 out_sg:
216 teardown_sgtable(&sg_out);
217 out_tfm:
218 crypto_free_blkcipher(tfm);
219 return ret;
222 static int ceph_aes_encrypt2(const void *key, int key_len, void *dst,
223 size_t *dst_len,
224 const void *src1, size_t src1_len,
225 const void *src2, size_t src2_len)
227 struct scatterlist sg_in[3], prealloc_sg;
228 struct sg_table sg_out;
229 struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
230 struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
231 int ret;
232 void *iv;
233 int ivsize;
234 size_t zero_padding = (0x10 - ((src1_len + src2_len) & 0x0f));
235 char pad[16];
237 if (IS_ERR(tfm))
238 return PTR_ERR(tfm);
240 memset(pad, zero_padding, zero_padding);
242 *dst_len = src1_len + src2_len + zero_padding;
244 sg_init_table(sg_in, 3);
245 sg_set_buf(&sg_in[0], src1, src1_len);
246 sg_set_buf(&sg_in[1], src2, src2_len);
247 sg_set_buf(&sg_in[2], pad, zero_padding);
248 ret = setup_sgtable(&sg_out, &prealloc_sg, dst, *dst_len);
249 if (ret)
250 goto out_tfm;
252 crypto_blkcipher_setkey((void *)tfm, key, key_len);
253 iv = crypto_blkcipher_crt(tfm)->iv;
254 ivsize = crypto_blkcipher_ivsize(tfm);
255 memcpy(iv, aes_iv, ivsize);
258 print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
259 key, key_len, 1);
260 print_hex_dump(KERN_ERR, "enc src1: ", DUMP_PREFIX_NONE, 16, 1,
261 src1, src1_len, 1);
262 print_hex_dump(KERN_ERR, "enc src2: ", DUMP_PREFIX_NONE, 16, 1,
263 src2, src2_len, 1);
264 print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
265 pad, zero_padding, 1);
267 ret = crypto_blkcipher_encrypt(&desc, sg_out.sgl, sg_in,
268 src1_len + src2_len + zero_padding);
269 if (ret < 0) {
270 pr_err("ceph_aes_crypt2 failed %d\n", ret);
271 goto out_sg;
274 print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
275 dst, *dst_len, 1);
278 out_sg:
279 teardown_sgtable(&sg_out);
280 out_tfm:
281 crypto_free_blkcipher(tfm);
282 return ret;
285 static int ceph_aes_decrypt(const void *key, int key_len,
286 void *dst, size_t *dst_len,
287 const void *src, size_t src_len)
289 struct sg_table sg_in;
290 struct scatterlist sg_out[2], prealloc_sg;
291 struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
292 struct blkcipher_desc desc = { .tfm = tfm };
293 char pad[16];
294 void *iv;
295 int ivsize;
296 int ret;
297 int last_byte;
299 if (IS_ERR(tfm))
300 return PTR_ERR(tfm);
302 sg_init_table(sg_out, 2);
303 sg_set_buf(&sg_out[0], dst, *dst_len);
304 sg_set_buf(&sg_out[1], pad, sizeof(pad));
305 ret = setup_sgtable(&sg_in, &prealloc_sg, src, src_len);
306 if (ret)
307 goto out_tfm;
309 crypto_blkcipher_setkey((void *)tfm, key, key_len);
310 iv = crypto_blkcipher_crt(tfm)->iv;
311 ivsize = crypto_blkcipher_ivsize(tfm);
312 memcpy(iv, aes_iv, ivsize);
315 print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
316 key, key_len, 1);
317 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
318 src, src_len, 1);
320 ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in.sgl, src_len);
321 if (ret < 0) {
322 pr_err("ceph_aes_decrypt failed %d\n", ret);
323 goto out_sg;
326 if (src_len <= *dst_len)
327 last_byte = ((char *)dst)[src_len - 1];
328 else
329 last_byte = pad[src_len - *dst_len - 1];
330 if (last_byte <= 16 && src_len >= last_byte) {
331 *dst_len = src_len - last_byte;
332 } else {
333 pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
334 last_byte, (int)src_len);
335 return -EPERM; /* bad padding */
338 print_hex_dump(KERN_ERR, "dec out: ", DUMP_PREFIX_NONE, 16, 1,
339 dst, *dst_len, 1);
342 out_sg:
343 teardown_sgtable(&sg_in);
344 out_tfm:
345 crypto_free_blkcipher(tfm);
346 return ret;
349 static int ceph_aes_decrypt2(const void *key, int key_len,
350 void *dst1, size_t *dst1_len,
351 void *dst2, size_t *dst2_len,
352 const void *src, size_t src_len)
354 struct sg_table sg_in;
355 struct scatterlist sg_out[3], prealloc_sg;
356 struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
357 struct blkcipher_desc desc = { .tfm = tfm };
358 char pad[16];
359 void *iv;
360 int ivsize;
361 int ret;
362 int last_byte;
364 if (IS_ERR(tfm))
365 return PTR_ERR(tfm);
367 sg_init_table(sg_out, 3);
368 sg_set_buf(&sg_out[0], dst1, *dst1_len);
369 sg_set_buf(&sg_out[1], dst2, *dst2_len);
370 sg_set_buf(&sg_out[2], pad, sizeof(pad));
371 ret = setup_sgtable(&sg_in, &prealloc_sg, src, src_len);
372 if (ret)
373 goto out_tfm;
375 crypto_blkcipher_setkey((void *)tfm, key, key_len);
376 iv = crypto_blkcipher_crt(tfm)->iv;
377 ivsize = crypto_blkcipher_ivsize(tfm);
378 memcpy(iv, aes_iv, ivsize);
381 print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
382 key, key_len, 1);
383 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
384 src, src_len, 1);
386 ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in.sgl, src_len);
387 if (ret < 0) {
388 pr_err("ceph_aes_decrypt failed %d\n", ret);
389 goto out_sg;
392 if (src_len <= *dst1_len)
393 last_byte = ((char *)dst1)[src_len - 1];
394 else if (src_len <= *dst1_len + *dst2_len)
395 last_byte = ((char *)dst2)[src_len - *dst1_len - 1];
396 else
397 last_byte = pad[src_len - *dst1_len - *dst2_len - 1];
398 if (last_byte <= 16 && src_len >= last_byte) {
399 src_len -= last_byte;
400 } else {
401 pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
402 last_byte, (int)src_len);
403 return -EPERM; /* bad padding */
406 if (src_len < *dst1_len) {
407 *dst1_len = src_len;
408 *dst2_len = 0;
409 } else {
410 *dst2_len = src_len - *dst1_len;
413 print_hex_dump(KERN_ERR, "dec out1: ", DUMP_PREFIX_NONE, 16, 1,
414 dst1, *dst1_len, 1);
415 print_hex_dump(KERN_ERR, "dec out2: ", DUMP_PREFIX_NONE, 16, 1,
416 dst2, *dst2_len, 1);
419 out_sg:
420 teardown_sgtable(&sg_in);
421 out_tfm:
422 crypto_free_blkcipher(tfm);
423 return ret;
427 int ceph_decrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
428 const void *src, size_t src_len)
430 switch (secret->type) {
431 case CEPH_CRYPTO_NONE:
432 if (*dst_len < src_len)
433 return -ERANGE;
434 memcpy(dst, src, src_len);
435 *dst_len = src_len;
436 return 0;
438 case CEPH_CRYPTO_AES:
439 return ceph_aes_decrypt(secret->key, secret->len, dst,
440 dst_len, src, src_len);
442 default:
443 return -EINVAL;
447 int ceph_decrypt2(struct ceph_crypto_key *secret,
448 void *dst1, size_t *dst1_len,
449 void *dst2, size_t *dst2_len,
450 const void *src, size_t src_len)
452 size_t t;
454 switch (secret->type) {
455 case CEPH_CRYPTO_NONE:
456 if (*dst1_len + *dst2_len < src_len)
457 return -ERANGE;
458 t = min(*dst1_len, src_len);
459 memcpy(dst1, src, t);
460 *dst1_len = t;
461 src += t;
462 src_len -= t;
463 if (src_len) {
464 t = min(*dst2_len, src_len);
465 memcpy(dst2, src, t);
466 *dst2_len = t;
468 return 0;
470 case CEPH_CRYPTO_AES:
471 return ceph_aes_decrypt2(secret->key, secret->len,
472 dst1, dst1_len, dst2, dst2_len,
473 src, src_len);
475 default:
476 return -EINVAL;
480 int ceph_encrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
481 const void *src, size_t src_len)
483 switch (secret->type) {
484 case CEPH_CRYPTO_NONE:
485 if (*dst_len < src_len)
486 return -ERANGE;
487 memcpy(dst, src, src_len);
488 *dst_len = src_len;
489 return 0;
491 case CEPH_CRYPTO_AES:
492 return ceph_aes_encrypt(secret->key, secret->len, dst,
493 dst_len, src, src_len);
495 default:
496 return -EINVAL;
500 int ceph_encrypt2(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
501 const void *src1, size_t src1_len,
502 const void *src2, size_t src2_len)
504 switch (secret->type) {
505 case CEPH_CRYPTO_NONE:
506 if (*dst_len < src1_len + src2_len)
507 return -ERANGE;
508 memcpy(dst, src1, src1_len);
509 memcpy(dst + src1_len, src2, src2_len);
510 *dst_len = src1_len + src2_len;
511 return 0;
513 case CEPH_CRYPTO_AES:
514 return ceph_aes_encrypt2(secret->key, secret->len, dst, dst_len,
515 src1, src1_len, src2, src2_len);
517 default:
518 return -EINVAL;
522 static int ceph_key_preparse(struct key_preparsed_payload *prep)
524 struct ceph_crypto_key *ckey;
525 size_t datalen = prep->datalen;
526 int ret;
527 void *p;
529 ret = -EINVAL;
530 if (datalen <= 0 || datalen > 32767 || !prep->data)
531 goto err;
533 ret = -ENOMEM;
534 ckey = kmalloc(sizeof(*ckey), GFP_KERNEL);
535 if (!ckey)
536 goto err;
538 /* TODO ceph_crypto_key_decode should really take const input */
539 p = (void *)prep->data;
540 ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
541 if (ret < 0)
542 goto err_ckey;
544 prep->payload[0] = ckey;
545 prep->quotalen = datalen;
546 return 0;
548 err_ckey:
549 kfree(ckey);
550 err:
551 return ret;
554 static void ceph_key_free_preparse(struct key_preparsed_payload *prep)
556 struct ceph_crypto_key *ckey = prep->payload[0];
557 ceph_crypto_key_destroy(ckey);
558 kfree(ckey);
561 static void ceph_key_destroy(struct key *key)
563 struct ceph_crypto_key *ckey = key->payload.data;
565 ceph_crypto_key_destroy(ckey);
566 kfree(ckey);
569 struct key_type key_type_ceph = {
570 .name = "ceph",
571 .preparse = ceph_key_preparse,
572 .free_preparse = ceph_key_free_preparse,
573 .instantiate = generic_key_instantiate,
574 .destroy = ceph_key_destroy,
577 int ceph_crypto_init(void) {
578 return register_key_type(&key_type_ceph);
581 void ceph_crypto_shutdown(void) {
582 unregister_key_type(&key_type_ceph);