2 #include <linux/ceph/ceph_debug.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>
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
);
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
)
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
);
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
);
47 ceph_decode_copy(p
, key
->key
, key
->len
);
51 dout("failed to decode crypto key\n");
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;
62 dout("crypto_key_unarmor %s\n", inkey
);
63 buf
= kmalloc(blen
, GFP_NOFS
);
66 blen
= ceph_unarmor(buf
, inkey
, inkey
+inlen
);
73 ret
= ceph_crypto_key_decode(key
, &p
, p
+ blen
);
77 dout("crypto_key_unarmor key %p type %d len %d\n", key
,
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
);
117 memset(sgt
, 0, sizeof(*sgt
));
122 chunk_cnt
= chunk_len
>> PAGE_SHIFT
;
123 chunk_len
= PAGE_SIZE
;
127 ret
= sg_alloc_table(sgt
, chunk_cnt
, GFP_NOFS
);
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
) {
139 unsigned int len
= min(chunk_len
- off
, buf_len
);
142 page
= vmalloc_to_page(buf
);
144 page
= virt_to_page(buf
);
146 sg_set_page(sg
, page
, len
, off
);
152 WARN_ON(buf_len
!= 0);
157 static void teardown_sgtable(struct sg_table
*sgt
)
159 if (sgt
->orig_nents
> 1)
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 };
174 size_t zero_padding
= (0x10 - (src_len
& 0x0f));
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
);
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,
199 print_hex_dump(KERN_ERR, "enc src: ", DUMP_PREFIX_NONE, 16, 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
);
207 pr_err("ceph_aes_crypt failed %d\n", ret
);
211 print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
216 teardown_sgtable(&sg_out
);
218 crypto_free_blkcipher(tfm
);
222 static int ceph_aes_encrypt2(const void *key
, int key_len
, void *dst
,
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 };
234 size_t zero_padding
= (0x10 - ((src1_len
+ src2_len
) & 0x0f));
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
);
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,
260 print_hex_dump(KERN_ERR, "enc src1: ", DUMP_PREFIX_NONE, 16, 1,
262 print_hex_dump(KERN_ERR, "enc src2: ", DUMP_PREFIX_NONE, 16, 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
);
270 pr_err("ceph_aes_crypt2 failed %d\n", ret
);
274 print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
279 teardown_sgtable(&sg_out
);
281 crypto_free_blkcipher(tfm
);
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
};
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
);
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,
317 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
320 ret
= crypto_blkcipher_decrypt(&desc
, sg_out
, sg_in
.sgl
, src_len
);
322 pr_err("ceph_aes_decrypt failed %d\n", ret
);
326 if (src_len
<= *dst_len
)
327 last_byte
= ((char *)dst
)[src_len
- 1];
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
;
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,
343 teardown_sgtable(&sg_in
);
345 crypto_free_blkcipher(tfm
);
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
};
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
);
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,
383 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
386 ret
= crypto_blkcipher_decrypt(&desc
, sg_out
, sg_in
.sgl
, src_len
);
388 pr_err("ceph_aes_decrypt failed %d\n", ret
);
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];
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
;
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
) {
410 *dst2_len
= src_len
- *dst1_len
;
413 print_hex_dump(KERN_ERR, "dec out1: ", DUMP_PREFIX_NONE, 16, 1,
415 print_hex_dump(KERN_ERR, "dec out2: ", DUMP_PREFIX_NONE, 16, 1,
420 teardown_sgtable(&sg_in
);
422 crypto_free_blkcipher(tfm
);
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
)
434 memcpy(dst
, src
, src_len
);
438 case CEPH_CRYPTO_AES
:
439 return ceph_aes_decrypt(secret
->key
, secret
->len
, dst
,
440 dst_len
, src
, src_len
);
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
)
454 switch (secret
->type
) {
455 case CEPH_CRYPTO_NONE
:
456 if (*dst1_len
+ *dst2_len
< src_len
)
458 t
= min(*dst1_len
, src_len
);
459 memcpy(dst1
, src
, t
);
464 t
= min(*dst2_len
, src_len
);
465 memcpy(dst2
, src
, t
);
470 case CEPH_CRYPTO_AES
:
471 return ceph_aes_decrypt2(secret
->key
, secret
->len
,
472 dst1
, dst1_len
, dst2
, dst2_len
,
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
)
487 memcpy(dst
, src
, src_len
);
491 case CEPH_CRYPTO_AES
:
492 return ceph_aes_encrypt(secret
->key
, secret
->len
, dst
,
493 dst_len
, src
, src_len
);
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
)
508 memcpy(dst
, src1
, src1_len
);
509 memcpy(dst
+ src1_len
, src2
, src2_len
);
510 *dst_len
= src1_len
+ src2_len
;
513 case CEPH_CRYPTO_AES
:
514 return ceph_aes_encrypt2(secret
->key
, secret
->len
, dst
, dst_len
,
515 src1
, src1_len
, src2
, src2_len
);
522 static int ceph_key_preparse(struct key_preparsed_payload
*prep
)
524 struct ceph_crypto_key
*ckey
;
525 size_t datalen
= prep
->datalen
;
530 if (datalen
<= 0 || datalen
> 32767 || !prep
->data
)
534 ckey
= kmalloc(sizeof(*ckey
), GFP_KERNEL
);
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
);
544 prep
->payload
[0] = ckey
;
545 prep
->quotalen
= datalen
;
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
);
561 static void ceph_key_destroy(struct key
*key
)
563 struct ceph_crypto_key
*ckey
= key
->payload
.data
;
565 ceph_crypto_key_destroy(ckey
);
569 struct key_type key_type_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
);