crypto.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/err.h>
  3. #include <linux/scatterlist.h>
  4. #include <linux/slab.h>
  5. #include <crypto/hash.h>
  6. #include <linux/key-type.h>
  7. #include <keys/ceph-type.h>
  8. #include <keys/user-type.h>
  9. #include <linux/ceph/decode.h>
  10. #include "crypto.h"
  11. int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
  12. const struct ceph_crypto_key *src)
  13. {
  14. memcpy(dst, src, sizeof(struct ceph_crypto_key));
  15. dst->key = kmemdup(src->key, src->len, GFP_NOFS);
  16. if (!dst->key)
  17. return -ENOMEM;
  18. return 0;
  19. }
  20. int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
  21. {
  22. if (*p + sizeof(u16) + sizeof(key->created) +
  23. sizeof(u16) + key->len > end)
  24. return -ERANGE;
  25. ceph_encode_16(p, key->type);
  26. ceph_encode_copy(p, &key->created, sizeof(key->created));
  27. ceph_encode_16(p, key->len);
  28. ceph_encode_copy(p, key->key, key->len);
  29. return 0;
  30. }
  31. int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
  32. {
  33. ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad);
  34. key->type = ceph_decode_16(p);
  35. ceph_decode_copy(p, &key->created, sizeof(key->created));
  36. key->len = ceph_decode_16(p);
  37. ceph_decode_need(p, end, key->len, bad);
  38. key->key = kmalloc(key->len, GFP_NOFS);
  39. if (!key->key)
  40. return -ENOMEM;
  41. ceph_decode_copy(p, key->key, key->len);
  42. return 0;
  43. bad:
  44. dout("failed to decode crypto key\n");
  45. return -EINVAL;
  46. }
  47. int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
  48. {
  49. int inlen = strlen(inkey);
  50. int blen = inlen * 3 / 4;
  51. void *buf, *p;
  52. int ret;
  53. dout("crypto_key_unarmor %s\n", inkey);
  54. buf = kmalloc(blen, GFP_NOFS);
  55. if (!buf)
  56. return -ENOMEM;
  57. blen = ceph_unarmor(buf, inkey, inkey+inlen);
  58. if (blen < 0) {
  59. kfree(buf);
  60. return blen;
  61. }
  62. p = buf;
  63. ret = ceph_crypto_key_decode(key, &p, p + blen);
  64. kfree(buf);
  65. if (ret)
  66. return ret;
  67. dout("crypto_key_unarmor key %p type %d len %d\n", key,
  68. key->type, key->len);
  69. return 0;
  70. }
  71. static struct crypto_blkcipher *ceph_crypto_alloc_cipher(void)
  72. {
  73. return crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  74. }
  75. static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
  76. /*
  77. * Should be used for buffers allocated with ceph_kvmalloc().
  78. * Currently these are encrypt out-buffer (ceph_buffer) and decrypt
  79. * in-buffer (msg front).
  80. *
  81. * Dispose of @sgt with teardown_sgtable().
  82. *
  83. * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
  84. * in cases where a single sg is sufficient. No attempt to reduce the
  85. * number of sgs by squeezing physically contiguous pages together is
  86. * made though, for simplicity.
  87. */
  88. static int setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
  89. const void *buf, unsigned int buf_len)
  90. {
  91. struct scatterlist *sg;
  92. const bool is_vmalloc = is_vmalloc_addr(buf);
  93. unsigned int off = offset_in_page(buf);
  94. unsigned int chunk_cnt = 1;
  95. unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
  96. int i;
  97. int ret;
  98. if (buf_len == 0) {
  99. memset(sgt, 0, sizeof(*sgt));
  100. return -EINVAL;
  101. }
  102. if (is_vmalloc) {
  103. chunk_cnt = chunk_len >> PAGE_SHIFT;
  104. chunk_len = PAGE_SIZE;
  105. }
  106. if (chunk_cnt > 1) {
  107. ret = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
  108. if (ret)
  109. return ret;
  110. } else {
  111. WARN_ON(chunk_cnt != 1);
  112. sg_init_table(prealloc_sg, 1);
  113. sgt->sgl = prealloc_sg;
  114. sgt->nents = sgt->orig_nents = 1;
  115. }
  116. for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
  117. struct page *page;
  118. unsigned int len = min(chunk_len - off, buf_len);
  119. if (is_vmalloc)
  120. page = vmalloc_to_page(buf);
  121. else
  122. page = virt_to_page(buf);
  123. sg_set_page(sg, page, len, off);
  124. off = 0;
  125. buf += len;
  126. buf_len -= len;
  127. }
  128. WARN_ON(buf_len != 0);
  129. return 0;
  130. }
  131. static void teardown_sgtable(struct sg_table *sgt)
  132. {
  133. if (sgt->orig_nents > 1)
  134. sg_free_table(sgt);
  135. }
  136. static int ceph_aes_encrypt(const void *key, int key_len,
  137. void *dst, size_t *dst_len,
  138. const void *src, size_t src_len)
  139. {
  140. struct scatterlist sg_in[2], prealloc_sg;
  141. struct sg_table sg_out;
  142. struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
  143. struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
  144. int ret;
  145. void *iv;
  146. int ivsize;
  147. size_t zero_padding = (0x10 - (src_len & 0x0f));
  148. char pad[16];
  149. if (IS_ERR(tfm))
  150. return PTR_ERR(tfm);
  151. memset(pad, zero_padding, zero_padding);
  152. *dst_len = src_len + zero_padding;
  153. sg_init_table(sg_in, 2);
  154. sg_set_buf(&sg_in[0], src, src_len);
  155. sg_set_buf(&sg_in[1], pad, zero_padding);
  156. ret = setup_sgtable(&sg_out, &prealloc_sg, dst, *dst_len);
  157. if (ret)
  158. goto out_tfm;
  159. crypto_blkcipher_setkey((void *)tfm, key, key_len);
  160. iv = crypto_blkcipher_crt(tfm)->iv;
  161. ivsize = crypto_blkcipher_ivsize(tfm);
  162. memcpy(iv, aes_iv, ivsize);
  163. /*
  164. print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
  165. key, key_len, 1);
  166. print_hex_dump(KERN_ERR, "enc src: ", DUMP_PREFIX_NONE, 16, 1,
  167. src, src_len, 1);
  168. print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
  169. pad, zero_padding, 1);
  170. */
  171. ret = crypto_blkcipher_encrypt(&desc, sg_out.sgl, sg_in,
  172. src_len + zero_padding);
  173. if (ret < 0) {
  174. pr_err("ceph_aes_crypt failed %d\n", ret);
  175. goto out_sg;
  176. }
  177. /*
  178. print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
  179. dst, *dst_len, 1);
  180. */
  181. out_sg:
  182. teardown_sgtable(&sg_out);
  183. out_tfm:
  184. crypto_free_blkcipher(tfm);
  185. return ret;
  186. }
  187. static int ceph_aes_encrypt2(const void *key, int key_len, void *dst,
  188. size_t *dst_len,
  189. const void *src1, size_t src1_len,
  190. const void *src2, size_t src2_len)
  191. {
  192. struct scatterlist sg_in[3], prealloc_sg;
  193. struct sg_table sg_out;
  194. struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
  195. struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
  196. int ret;
  197. void *iv;
  198. int ivsize;
  199. size_t zero_padding = (0x10 - ((src1_len + src2_len) & 0x0f));
  200. char pad[16];
  201. if (IS_ERR(tfm))
  202. return PTR_ERR(tfm);
  203. memset(pad, zero_padding, zero_padding);
  204. *dst_len = src1_len + src2_len + zero_padding;
  205. sg_init_table(sg_in, 3);
  206. sg_set_buf(&sg_in[0], src1, src1_len);
  207. sg_set_buf(&sg_in[1], src2, src2_len);
  208. sg_set_buf(&sg_in[2], pad, zero_padding);
  209. ret = setup_sgtable(&sg_out, &prealloc_sg, dst, *dst_len);
  210. if (ret)
  211. goto out_tfm;
  212. crypto_blkcipher_setkey((void *)tfm, key, key_len);
  213. iv = crypto_blkcipher_crt(tfm)->iv;
  214. ivsize = crypto_blkcipher_ivsize(tfm);
  215. memcpy(iv, aes_iv, ivsize);
  216. /*
  217. print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
  218. key, key_len, 1);
  219. print_hex_dump(KERN_ERR, "enc src1: ", DUMP_PREFIX_NONE, 16, 1,
  220. src1, src1_len, 1);
  221. print_hex_dump(KERN_ERR, "enc src2: ", DUMP_PREFIX_NONE, 16, 1,
  222. src2, src2_len, 1);
  223. print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
  224. pad, zero_padding, 1);
  225. */
  226. ret = crypto_blkcipher_encrypt(&desc, sg_out.sgl, sg_in,
  227. src1_len + src2_len + zero_padding);
  228. if (ret < 0) {
  229. pr_err("ceph_aes_crypt2 failed %d\n", ret);
  230. goto out_sg;
  231. }
  232. /*
  233. print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
  234. dst, *dst_len, 1);
  235. */
  236. out_sg:
  237. teardown_sgtable(&sg_out);
  238. out_tfm:
  239. crypto_free_blkcipher(tfm);
  240. return ret;
  241. }
  242. static int ceph_aes_decrypt(const void *key, int key_len,
  243. void *dst, size_t *dst_len,
  244. const void *src, size_t src_len)
  245. {
  246. struct sg_table sg_in;
  247. struct scatterlist sg_out[2], prealloc_sg;
  248. struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
  249. struct blkcipher_desc desc = { .tfm = tfm };
  250. char pad[16];
  251. void *iv;
  252. int ivsize;
  253. int ret;
  254. int last_byte;
  255. if (IS_ERR(tfm))
  256. return PTR_ERR(tfm);
  257. sg_init_table(sg_out, 2);
  258. sg_set_buf(&sg_out[0], dst, *dst_len);
  259. sg_set_buf(&sg_out[1], pad, sizeof(pad));
  260. ret = setup_sgtable(&sg_in, &prealloc_sg, src, src_len);
  261. if (ret)
  262. goto out_tfm;
  263. crypto_blkcipher_setkey((void *)tfm, key, key_len);
  264. iv = crypto_blkcipher_crt(tfm)->iv;
  265. ivsize = crypto_blkcipher_ivsize(tfm);
  266. memcpy(iv, aes_iv, ivsize);
  267. /*
  268. print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
  269. key, key_len, 1);
  270. print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
  271. src, src_len, 1);
  272. */
  273. ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in.sgl, src_len);
  274. if (ret < 0) {
  275. pr_err("ceph_aes_decrypt failed %d\n", ret);
  276. goto out_sg;
  277. }
  278. if (src_len <= *dst_len)
  279. last_byte = ((char *)dst)[src_len - 1];
  280. else
  281. last_byte = pad[src_len - *dst_len - 1];
  282. if (last_byte <= 16 && src_len >= last_byte) {
  283. *dst_len = src_len - last_byte;
  284. } else {
  285. pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
  286. last_byte, (int)src_len);
  287. return -EPERM; /* bad padding */
  288. }
  289. /*
  290. print_hex_dump(KERN_ERR, "dec out: ", DUMP_PREFIX_NONE, 16, 1,
  291. dst, *dst_len, 1);
  292. */
  293. out_sg:
  294. teardown_sgtable(&sg_in);
  295. out_tfm:
  296. crypto_free_blkcipher(tfm);
  297. return ret;
  298. }
  299. static int ceph_aes_decrypt2(const void *key, int key_len,
  300. void *dst1, size_t *dst1_len,
  301. void *dst2, size_t *dst2_len,
  302. const void *src, size_t src_len)
  303. {
  304. struct sg_table sg_in;
  305. struct scatterlist sg_out[3], prealloc_sg;
  306. struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
  307. struct blkcipher_desc desc = { .tfm = tfm };
  308. char pad[16];
  309. void *iv;
  310. int ivsize;
  311. int ret;
  312. int last_byte;
  313. if (IS_ERR(tfm))
  314. return PTR_ERR(tfm);
  315. sg_init_table(sg_out, 3);
  316. sg_set_buf(&sg_out[0], dst1, *dst1_len);
  317. sg_set_buf(&sg_out[1], dst2, *dst2_len);
  318. sg_set_buf(&sg_out[2], pad, sizeof(pad));
  319. ret = setup_sgtable(&sg_in, &prealloc_sg, src, src_len);
  320. if (ret)
  321. goto out_tfm;
  322. crypto_blkcipher_setkey((void *)tfm, key, key_len);
  323. iv = crypto_blkcipher_crt(tfm)->iv;
  324. ivsize = crypto_blkcipher_ivsize(tfm);
  325. memcpy(iv, aes_iv, ivsize);
  326. /*
  327. print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
  328. key, key_len, 1);
  329. print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
  330. src, src_len, 1);
  331. */
  332. ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in.sgl, src_len);
  333. if (ret < 0) {
  334. pr_err("ceph_aes_decrypt failed %d\n", ret);
  335. goto out_sg;
  336. }
  337. if (src_len <= *dst1_len)
  338. last_byte = ((char *)dst1)[src_len - 1];
  339. else if (src_len <= *dst1_len + *dst2_len)
  340. last_byte = ((char *)dst2)[src_len - *dst1_len - 1];
  341. else
  342. last_byte = pad[src_len - *dst1_len - *dst2_len - 1];
  343. if (last_byte <= 16 && src_len >= last_byte) {
  344. src_len -= last_byte;
  345. } else {
  346. pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
  347. last_byte, (int)src_len);
  348. return -EPERM; /* bad padding */
  349. }
  350. if (src_len < *dst1_len) {
  351. *dst1_len = src_len;
  352. *dst2_len = 0;
  353. } else {
  354. *dst2_len = src_len - *dst1_len;
  355. }
  356. /*
  357. print_hex_dump(KERN_ERR, "dec out1: ", DUMP_PREFIX_NONE, 16, 1,
  358. dst1, *dst1_len, 1);
  359. print_hex_dump(KERN_ERR, "dec out2: ", DUMP_PREFIX_NONE, 16, 1,
  360. dst2, *dst2_len, 1);
  361. */
  362. out_sg:
  363. teardown_sgtable(&sg_in);
  364. out_tfm:
  365. crypto_free_blkcipher(tfm);
  366. return ret;
  367. }
  368. int ceph_decrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
  369. const void *src, size_t src_len)
  370. {
  371. switch (secret->type) {
  372. case CEPH_CRYPTO_NONE:
  373. if (*dst_len < src_len)
  374. return -ERANGE;
  375. memcpy(dst, src, src_len);
  376. *dst_len = src_len;
  377. return 0;
  378. case CEPH_CRYPTO_AES:
  379. return ceph_aes_decrypt(secret->key, secret->len, dst,
  380. dst_len, src, src_len);
  381. default:
  382. return -EINVAL;
  383. }
  384. }
  385. int ceph_decrypt2(struct ceph_crypto_key *secret,
  386. void *dst1, size_t *dst1_len,
  387. void *dst2, size_t *dst2_len,
  388. const void *src, size_t src_len)
  389. {
  390. size_t t;
  391. switch (secret->type) {
  392. case CEPH_CRYPTO_NONE:
  393. if (*dst1_len + *dst2_len < src_len)
  394. return -ERANGE;
  395. t = min(*dst1_len, src_len);
  396. memcpy(dst1, src, t);
  397. *dst1_len = t;
  398. src += t;
  399. src_len -= t;
  400. if (src_len) {
  401. t = min(*dst2_len, src_len);
  402. memcpy(dst2, src, t);
  403. *dst2_len = t;
  404. }
  405. return 0;
  406. case CEPH_CRYPTO_AES:
  407. return ceph_aes_decrypt2(secret->key, secret->len,
  408. dst1, dst1_len, dst2, dst2_len,
  409. src, src_len);
  410. default:
  411. return -EINVAL;
  412. }
  413. }
  414. int ceph_encrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
  415. const void *src, size_t src_len)
  416. {
  417. switch (secret->type) {
  418. case CEPH_CRYPTO_NONE:
  419. if (*dst_len < src_len)
  420. return -ERANGE;
  421. memcpy(dst, src, src_len);
  422. *dst_len = src_len;
  423. return 0;
  424. case CEPH_CRYPTO_AES:
  425. return ceph_aes_encrypt(secret->key, secret->len, dst,
  426. dst_len, src, src_len);
  427. default:
  428. return -EINVAL;
  429. }
  430. }
  431. int ceph_encrypt2(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
  432. const void *src1, size_t src1_len,
  433. const void *src2, size_t src2_len)
  434. {
  435. switch (secret->type) {
  436. case CEPH_CRYPTO_NONE:
  437. if (*dst_len < src1_len + src2_len)
  438. return -ERANGE;
  439. memcpy(dst, src1, src1_len);
  440. memcpy(dst + src1_len, src2, src2_len);
  441. *dst_len = src1_len + src2_len;
  442. return 0;
  443. case CEPH_CRYPTO_AES:
  444. return ceph_aes_encrypt2(secret->key, secret->len, dst, dst_len,
  445. src1, src1_len, src2, src2_len);
  446. default:
  447. return -EINVAL;
  448. }
  449. }
  450. static int ceph_key_preparse(struct key_preparsed_payload *prep)
  451. {
  452. struct ceph_crypto_key *ckey;
  453. size_t datalen = prep->datalen;
  454. int ret;
  455. void *p;
  456. ret = -EINVAL;
  457. if (datalen <= 0 || datalen > 32767 || !prep->data)
  458. goto err;
  459. ret = -ENOMEM;
  460. ckey = kmalloc(sizeof(*ckey), GFP_KERNEL);
  461. if (!ckey)
  462. goto err;
  463. /* TODO ceph_crypto_key_decode should really take const input */
  464. p = (void *)prep->data;
  465. ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
  466. if (ret < 0)
  467. goto err_ckey;
  468. prep->payload.data[0] = ckey;
  469. prep->quotalen = datalen;
  470. return 0;
  471. err_ckey:
  472. kfree(ckey);
  473. err:
  474. return ret;
  475. }
  476. static void ceph_key_free_preparse(struct key_preparsed_payload *prep)
  477. {
  478. struct ceph_crypto_key *ckey = prep->payload.data[0];
  479. ceph_crypto_key_destroy(ckey);
  480. kfree(ckey);
  481. }
  482. static void ceph_key_destroy(struct key *key)
  483. {
  484. struct ceph_crypto_key *ckey = key->payload.data[0];
  485. ceph_crypto_key_destroy(ckey);
  486. kfree(ckey);
  487. }
  488. struct key_type key_type_ceph = {
  489. .name = "ceph",
  490. .preparse = ceph_key_preparse,
  491. .free_preparse = ceph_key_free_preparse,
  492. .instantiate = generic_key_instantiate,
  493. .destroy = ceph_key_destroy,
  494. };
  495. int ceph_crypto_init(void) {
  496. return register_key_type(&key_type_ceph);
  497. }
  498. void ceph_crypto_shutdown(void) {
  499. unregister_key_type(&key_type_ceph);
  500. }