crypto.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 <linux/ceph/decode.h>
  9. #include "crypto.h"
  10. int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
  11. const struct ceph_crypto_key *src)
  12. {
  13. memcpy(dst, src, sizeof(struct ceph_crypto_key));
  14. dst->key = kmalloc(src->len, GFP_NOFS);
  15. if (!dst->key)
  16. return -ENOMEM;
  17. memcpy(dst->key, src->key, src->len);
  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. #define AES_KEY_SIZE 16
  72. static struct crypto_blkcipher *ceph_crypto_alloc_cipher(void)
  73. {
  74. return crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  75. }
  76. static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
  77. static int ceph_aes_encrypt(const void *key, int key_len,
  78. void *dst, size_t *dst_len,
  79. const void *src, size_t src_len)
  80. {
  81. struct scatterlist sg_in[2], sg_out[1];
  82. struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
  83. struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
  84. int ret;
  85. void *iv;
  86. int ivsize;
  87. size_t zero_padding = (0x10 - (src_len & 0x0f));
  88. char pad[16];
  89. if (IS_ERR(tfm))
  90. return PTR_ERR(tfm);
  91. memset(pad, zero_padding, zero_padding);
  92. *dst_len = src_len + zero_padding;
  93. crypto_blkcipher_setkey((void *)tfm, key, key_len);
  94. sg_init_table(sg_in, 2);
  95. sg_set_buf(&sg_in[0], src, src_len);
  96. sg_set_buf(&sg_in[1], pad, zero_padding);
  97. sg_init_table(sg_out, 1);
  98. sg_set_buf(sg_out, dst, *dst_len);
  99. iv = crypto_blkcipher_crt(tfm)->iv;
  100. ivsize = crypto_blkcipher_ivsize(tfm);
  101. memcpy(iv, aes_iv, ivsize);
  102. /*
  103. print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
  104. key, key_len, 1);
  105. print_hex_dump(KERN_ERR, "enc src: ", DUMP_PREFIX_NONE, 16, 1,
  106. src, src_len, 1);
  107. print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
  108. pad, zero_padding, 1);
  109. */
  110. ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
  111. src_len + zero_padding);
  112. crypto_free_blkcipher(tfm);
  113. if (ret < 0)
  114. pr_err("ceph_aes_crypt failed %d\n", ret);
  115. /*
  116. print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
  117. dst, *dst_len, 1);
  118. */
  119. return 0;
  120. }
  121. static int ceph_aes_encrypt2(const void *key, int key_len, void *dst,
  122. size_t *dst_len,
  123. const void *src1, size_t src1_len,
  124. const void *src2, size_t src2_len)
  125. {
  126. struct scatterlist sg_in[3], sg_out[1];
  127. struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
  128. struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 };
  129. int ret;
  130. void *iv;
  131. int ivsize;
  132. size_t zero_padding = (0x10 - ((src1_len + src2_len) & 0x0f));
  133. char pad[16];
  134. if (IS_ERR(tfm))
  135. return PTR_ERR(tfm);
  136. memset(pad, zero_padding, zero_padding);
  137. *dst_len = src1_len + src2_len + zero_padding;
  138. crypto_blkcipher_setkey((void *)tfm, key, key_len);
  139. sg_init_table(sg_in, 3);
  140. sg_set_buf(&sg_in[0], src1, src1_len);
  141. sg_set_buf(&sg_in[1], src2, src2_len);
  142. sg_set_buf(&sg_in[2], pad, zero_padding);
  143. sg_init_table(sg_out, 1);
  144. sg_set_buf(sg_out, dst, *dst_len);
  145. iv = crypto_blkcipher_crt(tfm)->iv;
  146. ivsize = crypto_blkcipher_ivsize(tfm);
  147. memcpy(iv, aes_iv, ivsize);
  148. /*
  149. print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1,
  150. key, key_len, 1);
  151. print_hex_dump(KERN_ERR, "enc src1: ", DUMP_PREFIX_NONE, 16, 1,
  152. src1, src1_len, 1);
  153. print_hex_dump(KERN_ERR, "enc src2: ", DUMP_PREFIX_NONE, 16, 1,
  154. src2, src2_len, 1);
  155. print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1,
  156. pad, zero_padding, 1);
  157. */
  158. ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
  159. src1_len + src2_len + zero_padding);
  160. crypto_free_blkcipher(tfm);
  161. if (ret < 0)
  162. pr_err("ceph_aes_crypt2 failed %d\n", ret);
  163. /*
  164. print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1,
  165. dst, *dst_len, 1);
  166. */
  167. return 0;
  168. }
  169. static int ceph_aes_decrypt(const void *key, int key_len,
  170. void *dst, size_t *dst_len,
  171. const void *src, size_t src_len)
  172. {
  173. struct scatterlist sg_in[1], sg_out[2];
  174. struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
  175. struct blkcipher_desc desc = { .tfm = tfm };
  176. char pad[16];
  177. void *iv;
  178. int ivsize;
  179. int ret;
  180. int last_byte;
  181. if (IS_ERR(tfm))
  182. return PTR_ERR(tfm);
  183. crypto_blkcipher_setkey((void *)tfm, key, key_len);
  184. sg_init_table(sg_in, 1);
  185. sg_init_table(sg_out, 2);
  186. sg_set_buf(sg_in, src, src_len);
  187. sg_set_buf(&sg_out[0], dst, *dst_len);
  188. sg_set_buf(&sg_out[1], pad, sizeof(pad));
  189. iv = crypto_blkcipher_crt(tfm)->iv;
  190. ivsize = crypto_blkcipher_ivsize(tfm);
  191. memcpy(iv, aes_iv, ivsize);
  192. /*
  193. print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
  194. key, key_len, 1);
  195. print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
  196. src, src_len, 1);
  197. */
  198. ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, src_len);
  199. crypto_free_blkcipher(tfm);
  200. if (ret < 0) {
  201. pr_err("ceph_aes_decrypt failed %d\n", ret);
  202. return ret;
  203. }
  204. if (src_len <= *dst_len)
  205. last_byte = ((char *)dst)[src_len - 1];
  206. else
  207. last_byte = pad[src_len - *dst_len - 1];
  208. if (last_byte <= 16 && src_len >= last_byte) {
  209. *dst_len = src_len - last_byte;
  210. } else {
  211. pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
  212. last_byte, (int)src_len);
  213. return -EPERM; /* bad padding */
  214. }
  215. /*
  216. print_hex_dump(KERN_ERR, "dec out: ", DUMP_PREFIX_NONE, 16, 1,
  217. dst, *dst_len, 1);
  218. */
  219. return 0;
  220. }
  221. static int ceph_aes_decrypt2(const void *key, int key_len,
  222. void *dst1, size_t *dst1_len,
  223. void *dst2, size_t *dst2_len,
  224. const void *src, size_t src_len)
  225. {
  226. struct scatterlist sg_in[1], sg_out[3];
  227. struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher();
  228. struct blkcipher_desc desc = { .tfm = tfm };
  229. char pad[16];
  230. void *iv;
  231. int ivsize;
  232. int ret;
  233. int last_byte;
  234. if (IS_ERR(tfm))
  235. return PTR_ERR(tfm);
  236. sg_init_table(sg_in, 1);
  237. sg_set_buf(sg_in, src, src_len);
  238. sg_init_table(sg_out, 3);
  239. sg_set_buf(&sg_out[0], dst1, *dst1_len);
  240. sg_set_buf(&sg_out[1], dst2, *dst2_len);
  241. sg_set_buf(&sg_out[2], pad, sizeof(pad));
  242. crypto_blkcipher_setkey((void *)tfm, key, key_len);
  243. iv = crypto_blkcipher_crt(tfm)->iv;
  244. ivsize = crypto_blkcipher_ivsize(tfm);
  245. memcpy(iv, aes_iv, ivsize);
  246. /*
  247. print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1,
  248. key, key_len, 1);
  249. print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1,
  250. src, src_len, 1);
  251. */
  252. ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, src_len);
  253. crypto_free_blkcipher(tfm);
  254. if (ret < 0) {
  255. pr_err("ceph_aes_decrypt failed %d\n", ret);
  256. return ret;
  257. }
  258. if (src_len <= *dst1_len)
  259. last_byte = ((char *)dst1)[src_len - 1];
  260. else if (src_len <= *dst1_len + *dst2_len)
  261. last_byte = ((char *)dst2)[src_len - *dst1_len - 1];
  262. else
  263. last_byte = pad[src_len - *dst1_len - *dst2_len - 1];
  264. if (last_byte <= 16 && src_len >= last_byte) {
  265. src_len -= last_byte;
  266. } else {
  267. pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n",
  268. last_byte, (int)src_len);
  269. return -EPERM; /* bad padding */
  270. }
  271. if (src_len < *dst1_len) {
  272. *dst1_len = src_len;
  273. *dst2_len = 0;
  274. } else {
  275. *dst2_len = src_len - *dst1_len;
  276. }
  277. /*
  278. print_hex_dump(KERN_ERR, "dec out1: ", DUMP_PREFIX_NONE, 16, 1,
  279. dst1, *dst1_len, 1);
  280. print_hex_dump(KERN_ERR, "dec out2: ", DUMP_PREFIX_NONE, 16, 1,
  281. dst2, *dst2_len, 1);
  282. */
  283. return 0;
  284. }
  285. int ceph_decrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
  286. const void *src, size_t src_len)
  287. {
  288. switch (secret->type) {
  289. case CEPH_CRYPTO_NONE:
  290. if (*dst_len < src_len)
  291. return -ERANGE;
  292. memcpy(dst, src, src_len);
  293. *dst_len = src_len;
  294. return 0;
  295. case CEPH_CRYPTO_AES:
  296. return ceph_aes_decrypt(secret->key, secret->len, dst,
  297. dst_len, src, src_len);
  298. default:
  299. return -EINVAL;
  300. }
  301. }
  302. int ceph_decrypt2(struct ceph_crypto_key *secret,
  303. void *dst1, size_t *dst1_len,
  304. void *dst2, size_t *dst2_len,
  305. const void *src, size_t src_len)
  306. {
  307. size_t t;
  308. switch (secret->type) {
  309. case CEPH_CRYPTO_NONE:
  310. if (*dst1_len + *dst2_len < src_len)
  311. return -ERANGE;
  312. t = min(*dst1_len, src_len);
  313. memcpy(dst1, src, t);
  314. *dst1_len = t;
  315. src += t;
  316. src_len -= t;
  317. if (src_len) {
  318. t = min(*dst2_len, src_len);
  319. memcpy(dst2, src, t);
  320. *dst2_len = t;
  321. }
  322. return 0;
  323. case CEPH_CRYPTO_AES:
  324. return ceph_aes_decrypt2(secret->key, secret->len,
  325. dst1, dst1_len, dst2, dst2_len,
  326. src, src_len);
  327. default:
  328. return -EINVAL;
  329. }
  330. }
  331. int ceph_encrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
  332. const void *src, size_t src_len)
  333. {
  334. switch (secret->type) {
  335. case CEPH_CRYPTO_NONE:
  336. if (*dst_len < src_len)
  337. return -ERANGE;
  338. memcpy(dst, src, src_len);
  339. *dst_len = src_len;
  340. return 0;
  341. case CEPH_CRYPTO_AES:
  342. return ceph_aes_encrypt(secret->key, secret->len, dst,
  343. dst_len, src, src_len);
  344. default:
  345. return -EINVAL;
  346. }
  347. }
  348. int ceph_encrypt2(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
  349. const void *src1, size_t src1_len,
  350. const void *src2, size_t src2_len)
  351. {
  352. switch (secret->type) {
  353. case CEPH_CRYPTO_NONE:
  354. if (*dst_len < src1_len + src2_len)
  355. return -ERANGE;
  356. memcpy(dst, src1, src1_len);
  357. memcpy(dst + src1_len, src2, src2_len);
  358. *dst_len = src1_len + src2_len;
  359. return 0;
  360. case CEPH_CRYPTO_AES:
  361. return ceph_aes_encrypt2(secret->key, secret->len, dst, dst_len,
  362. src1, src1_len, src2, src2_len);
  363. default:
  364. return -EINVAL;
  365. }
  366. }
  367. int ceph_key_instantiate(struct key *key, const void *data, size_t datalen)
  368. {
  369. struct ceph_crypto_key *ckey;
  370. int ret;
  371. void *p;
  372. ret = -EINVAL;
  373. if (datalen <= 0 || datalen > 32767 || !data)
  374. goto err;
  375. ret = key_payload_reserve(key, datalen);
  376. if (ret < 0)
  377. goto err;
  378. ret = -ENOMEM;
  379. ckey = kmalloc(sizeof(*ckey), GFP_KERNEL);
  380. if (!ckey)
  381. goto err;
  382. /* TODO ceph_crypto_key_decode should really take const input */
  383. p = (void*)data;
  384. ret = ceph_crypto_key_decode(ckey, &p, (char*)data+datalen);
  385. if (ret < 0)
  386. goto err_ckey;
  387. key->payload.data = ckey;
  388. return 0;
  389. err_ckey:
  390. kfree(ckey);
  391. err:
  392. return ret;
  393. }
  394. int ceph_key_match(const struct key *key, const void *description)
  395. {
  396. return strcmp(key->description, description) == 0;
  397. }
  398. void ceph_key_destroy(struct key *key) {
  399. struct ceph_crypto_key *ckey = key->payload.data;
  400. ceph_crypto_key_destroy(ckey);
  401. }
  402. struct key_type key_type_ceph = {
  403. .name = "ceph",
  404. .instantiate = ceph_key_instantiate,
  405. .match = ceph_key_match,
  406. .destroy = ceph_key_destroy,
  407. };
  408. int ceph_crypto_init(void) {
  409. return register_key_type(&key_type_ceph);
  410. }
  411. void ceph_crypto_shutdown(void) {
  412. unregister_key_type(&key_type_ceph);
  413. }