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
,
82 static struct crypto_blkcipher
*ceph_crypto_alloc_cipher(void)
84 return crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC
);
87 static const u8
*aes_iv
= (u8
*)CEPH_AES_IV
;
90 * Should be used for buffers allocated with ceph_kvmalloc().
91 * Currently these are encrypt out-buffer (ceph_buffer) and decrypt
92 * in-buffer (msg front).
94 * Dispose of @sgt with teardown_sgtable().
96 * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
97 * in cases where a single sg is sufficient. No attempt to reduce the
98 * number of sgs by squeezing physically contiguous pages together is
99 * made though, for simplicity.
101 static int setup_sgtable(struct sg_table
*sgt
, struct scatterlist
*prealloc_sg
,
102 const void *buf
, unsigned int buf_len
)
104 struct scatterlist
*sg
;
105 const bool is_vmalloc
= is_vmalloc_addr(buf
);
106 unsigned int off
= offset_in_page(buf
);
107 unsigned int chunk_cnt
= 1;
108 unsigned int chunk_len
= PAGE_ALIGN(off
+ buf_len
);
113 memset(sgt
, 0, sizeof(*sgt
));
118 chunk_cnt
= chunk_len
>> PAGE_SHIFT
;
119 chunk_len
= PAGE_SIZE
;
123 ret
= sg_alloc_table(sgt
, chunk_cnt
, GFP_NOFS
);
127 WARN_ON(chunk_cnt
!= 1);
128 sg_init_table(prealloc_sg
, 1);
129 sgt
->sgl
= prealloc_sg
;
130 sgt
->nents
= sgt
->orig_nents
= 1;
133 for_each_sg(sgt
->sgl
, sg
, sgt
->orig_nents
, i
) {
135 unsigned int len
= min(chunk_len
- off
, buf_len
);
138 page
= vmalloc_to_page(buf
);
140 page
= virt_to_page(buf
);
142 sg_set_page(sg
, page
, len
, off
);
148 WARN_ON(buf_len
!= 0);
153 static void teardown_sgtable(struct sg_table
*sgt
)
155 if (sgt
->orig_nents
> 1)
159 static int ceph_aes_encrypt(const void *key
, int key_len
,
160 void *dst
, size_t *dst_len
,
161 const void *src
, size_t src_len
)
163 struct scatterlist sg_in
[2], prealloc_sg
;
164 struct sg_table sg_out
;
165 struct crypto_blkcipher
*tfm
= ceph_crypto_alloc_cipher();
166 struct blkcipher_desc desc
= { .tfm
= tfm
, .flags
= 0 };
170 size_t zero_padding
= (0x10 - (src_len
& 0x0f));
176 memset(pad
, zero_padding
, zero_padding
);
178 *dst_len
= src_len
+ zero_padding
;
180 sg_init_table(sg_in
, 2);
181 sg_set_buf(&sg_in
[0], src
, src_len
);
182 sg_set_buf(&sg_in
[1], pad
, zero_padding
);
183 ret
= setup_sgtable(&sg_out
, &prealloc_sg
, dst
, *dst_len
);
187 crypto_blkcipher_setkey((void *)tfm
, key
, key_len
);
188 iv
= crypto_blkcipher_crt(tfm
)->iv
;
189 ivsize
= crypto_blkcipher_ivsize(tfm
);
190 memcpy(iv
, aes_iv
, ivsize
);
193 print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
195 print_hex_dump(KERN_ERR, "enc src: ", DUMP_PREFIX_NONE, 16, 1,
197 print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
198 pad, zero_padding, 1);
200 ret
= crypto_blkcipher_encrypt(&desc
, sg_out
.sgl
, sg_in
,
201 src_len
+ zero_padding
);
203 pr_err("ceph_aes_crypt failed %d\n", ret
);
207 print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
212 teardown_sgtable(&sg_out
);
214 crypto_free_blkcipher(tfm
);
218 static int ceph_aes_encrypt2(const void *key
, int key_len
, void *dst
,
220 const void *src1
, size_t src1_len
,
221 const void *src2
, size_t src2_len
)
223 struct scatterlist sg_in
[3], prealloc_sg
;
224 struct sg_table sg_out
;
225 struct crypto_blkcipher
*tfm
= ceph_crypto_alloc_cipher();
226 struct blkcipher_desc desc
= { .tfm
= tfm
, .flags
= 0 };
230 size_t zero_padding
= (0x10 - ((src1_len
+ src2_len
) & 0x0f));
236 memset(pad
, zero_padding
, zero_padding
);
238 *dst_len
= src1_len
+ src2_len
+ zero_padding
;
240 sg_init_table(sg_in
, 3);
241 sg_set_buf(&sg_in
[0], src1
, src1_len
);
242 sg_set_buf(&sg_in
[1], src2
, src2_len
);
243 sg_set_buf(&sg_in
[2], pad
, zero_padding
);
244 ret
= setup_sgtable(&sg_out
, &prealloc_sg
, dst
, *dst_len
);
248 crypto_blkcipher_setkey((void *)tfm
, key
, key_len
);
249 iv
= crypto_blkcipher_crt(tfm
)->iv
;
250 ivsize
= crypto_blkcipher_ivsize(tfm
);
251 memcpy(iv
, aes_iv
, ivsize
);
254 print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
256 print_hex_dump(KERN_ERR, "enc src1: ", DUMP_PREFIX_NONE, 16, 1,
258 print_hex_dump(KERN_ERR, "enc src2: ", DUMP_PREFIX_NONE, 16, 1,
260 print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
261 pad, zero_padding, 1);
263 ret
= crypto_blkcipher_encrypt(&desc
, sg_out
.sgl
, sg_in
,
264 src1_len
+ src2_len
+ zero_padding
);
266 pr_err("ceph_aes_crypt2 failed %d\n", ret
);
270 print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
275 teardown_sgtable(&sg_out
);
277 crypto_free_blkcipher(tfm
);
281 static int ceph_aes_decrypt(const void *key
, int key_len
,
282 void *dst
, size_t *dst_len
,
283 const void *src
, size_t src_len
)
285 struct sg_table sg_in
;
286 struct scatterlist sg_out
[2], prealloc_sg
;
287 struct crypto_blkcipher
*tfm
= ceph_crypto_alloc_cipher();
288 struct blkcipher_desc desc
= { .tfm
= tfm
};
298 sg_init_table(sg_out
, 2);
299 sg_set_buf(&sg_out
[0], dst
, *dst_len
);
300 sg_set_buf(&sg_out
[1], pad
, sizeof(pad
));
301 ret
= setup_sgtable(&sg_in
, &prealloc_sg
, src
, src_len
);
305 crypto_blkcipher_setkey((void *)tfm
, key
, key_len
);
306 iv
= crypto_blkcipher_crt(tfm
)->iv
;
307 ivsize
= crypto_blkcipher_ivsize(tfm
);
308 memcpy(iv
, aes_iv
, ivsize
);
311 print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
313 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
316 ret
= crypto_blkcipher_decrypt(&desc
, sg_out
, sg_in
.sgl
, src_len
);
318 pr_err("ceph_aes_decrypt failed %d\n", ret
);
322 if (src_len
<= *dst_len
)
323 last_byte
= ((char *)dst
)[src_len
- 1];
325 last_byte
= pad
[src_len
- *dst_len
- 1];
326 if (last_byte
<= 16 && src_len
>= last_byte
) {
327 *dst_len
= src_len
- last_byte
;
329 pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
330 last_byte
, (int)src_len
);
331 return -EPERM
; /* bad padding */
334 print_hex_dump(KERN_ERR, "dec out: ", DUMP_PREFIX_NONE, 16, 1,
339 teardown_sgtable(&sg_in
);
341 crypto_free_blkcipher(tfm
);
345 static int ceph_aes_decrypt2(const void *key
, int key_len
,
346 void *dst1
, size_t *dst1_len
,
347 void *dst2
, size_t *dst2_len
,
348 const void *src
, size_t src_len
)
350 struct sg_table sg_in
;
351 struct scatterlist sg_out
[3], prealloc_sg
;
352 struct crypto_blkcipher
*tfm
= ceph_crypto_alloc_cipher();
353 struct blkcipher_desc desc
= { .tfm
= tfm
};
363 sg_init_table(sg_out
, 3);
364 sg_set_buf(&sg_out
[0], dst1
, *dst1_len
);
365 sg_set_buf(&sg_out
[1], dst2
, *dst2_len
);
366 sg_set_buf(&sg_out
[2], pad
, sizeof(pad
));
367 ret
= setup_sgtable(&sg_in
, &prealloc_sg
, src
, src_len
);
371 crypto_blkcipher_setkey((void *)tfm
, key
, key_len
);
372 iv
= crypto_blkcipher_crt(tfm
)->iv
;
373 ivsize
= crypto_blkcipher_ivsize(tfm
);
374 memcpy(iv
, aes_iv
, ivsize
);
377 print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
379 print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
382 ret
= crypto_blkcipher_decrypt(&desc
, sg_out
, sg_in
.sgl
, src_len
);
384 pr_err("ceph_aes_decrypt failed %d\n", ret
);
388 if (src_len
<= *dst1_len
)
389 last_byte
= ((char *)dst1
)[src_len
- 1];
390 else if (src_len
<= *dst1_len
+ *dst2_len
)
391 last_byte
= ((char *)dst2
)[src_len
- *dst1_len
- 1];
393 last_byte
= pad
[src_len
- *dst1_len
- *dst2_len
- 1];
394 if (last_byte
<= 16 && src_len
>= last_byte
) {
395 src_len
-= last_byte
;
397 pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
398 last_byte
, (int)src_len
);
399 return -EPERM
; /* bad padding */
402 if (src_len
< *dst1_len
) {
406 *dst2_len
= src_len
- *dst1_len
;
409 print_hex_dump(KERN_ERR, "dec out1: ", DUMP_PREFIX_NONE, 16, 1,
411 print_hex_dump(KERN_ERR, "dec out2: ", DUMP_PREFIX_NONE, 16, 1,
416 teardown_sgtable(&sg_in
);
418 crypto_free_blkcipher(tfm
);
423 int ceph_decrypt(struct ceph_crypto_key
*secret
, void *dst
, size_t *dst_len
,
424 const void *src
, size_t src_len
)
426 switch (secret
->type
) {
427 case CEPH_CRYPTO_NONE
:
428 if (*dst_len
< src_len
)
430 memcpy(dst
, src
, src_len
);
434 case CEPH_CRYPTO_AES
:
435 return ceph_aes_decrypt(secret
->key
, secret
->len
, dst
,
436 dst_len
, src
, src_len
);
443 int ceph_decrypt2(struct ceph_crypto_key
*secret
,
444 void *dst1
, size_t *dst1_len
,
445 void *dst2
, size_t *dst2_len
,
446 const void *src
, size_t src_len
)
450 switch (secret
->type
) {
451 case CEPH_CRYPTO_NONE
:
452 if (*dst1_len
+ *dst2_len
< src_len
)
454 t
= min(*dst1_len
, src_len
);
455 memcpy(dst1
, src
, t
);
460 t
= min(*dst2_len
, src_len
);
461 memcpy(dst2
, src
, t
);
466 case CEPH_CRYPTO_AES
:
467 return ceph_aes_decrypt2(secret
->key
, secret
->len
,
468 dst1
, dst1_len
, dst2
, dst2_len
,
476 int ceph_encrypt(struct ceph_crypto_key
*secret
, void *dst
, size_t *dst_len
,
477 const void *src
, size_t src_len
)
479 switch (secret
->type
) {
480 case CEPH_CRYPTO_NONE
:
481 if (*dst_len
< src_len
)
483 memcpy(dst
, src
, src_len
);
487 case CEPH_CRYPTO_AES
:
488 return ceph_aes_encrypt(secret
->key
, secret
->len
, dst
,
489 dst_len
, src
, src_len
);
496 int ceph_encrypt2(struct ceph_crypto_key
*secret
, void *dst
, size_t *dst_len
,
497 const void *src1
, size_t src1_len
,
498 const void *src2
, size_t src2_len
)
500 switch (secret
->type
) {
501 case CEPH_CRYPTO_NONE
:
502 if (*dst_len
< src1_len
+ src2_len
)
504 memcpy(dst
, src1
, src1_len
);
505 memcpy(dst
+ src1_len
, src2
, src2_len
);
506 *dst_len
= src1_len
+ src2_len
;
509 case CEPH_CRYPTO_AES
:
510 return ceph_aes_encrypt2(secret
->key
, secret
->len
, dst
, dst_len
,
511 src1
, src1_len
, src2
, src2_len
);
518 static int ceph_key_preparse(struct key_preparsed_payload
*prep
)
520 struct ceph_crypto_key
*ckey
;
521 size_t datalen
= prep
->datalen
;
526 if (datalen
<= 0 || datalen
> 32767 || !prep
->data
)
530 ckey
= kmalloc(sizeof(*ckey
), GFP_KERNEL
);
534 /* TODO ceph_crypto_key_decode should really take const input */
535 p
= (void *)prep
->data
;
536 ret
= ceph_crypto_key_decode(ckey
, &p
, (char*)prep
->data
+datalen
);
540 prep
->payload
.data
[0] = ckey
;
541 prep
->quotalen
= datalen
;
550 static void ceph_key_free_preparse(struct key_preparsed_payload
*prep
)
552 struct ceph_crypto_key
*ckey
= prep
->payload
.data
[0];
553 ceph_crypto_key_destroy(ckey
);
557 static void ceph_key_destroy(struct key
*key
)
559 struct ceph_crypto_key
*ckey
= key
->payload
.data
[0];
561 ceph_crypto_key_destroy(ckey
);
565 struct key_type key_type_ceph
= {
567 .preparse
= ceph_key_preparse
,
568 .free_preparse
= ceph_key_free_preparse
,
569 .instantiate
= generic_key_instantiate
,
570 .destroy
= ceph_key_destroy
,
573 int ceph_crypto_init(void) {
574 return register_key_type(&key_type_ceph
);
577 void ceph_crypto_shutdown(void) {
578 unregister_key_type(&key_type_ceph
);