crypto.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/err.h>
  4. #include <linux/scatterlist.h>
  5. #include <linux/sched.h>
  6. #include <linux/slab.h>
  7. #include <crypto/aes.h>
  8. #include <crypto/skcipher.h>
  9. #include <linux/key-type.h>
  10. #include <linux/sched/mm.h>
  11. #include <keys/ceph-type.h>
  12. #include <keys/user-type.h>
  13. #include <linux/ceph/decode.h>
  14. #include "crypto.h"
  15. /*
  16. * Set ->key and ->tfm. The rest of the key should be filled in before
  17. * this function is called.
  18. */
  19. static int set_secret(struct ceph_crypto_key *key, void *buf)
  20. {
  21. unsigned int noio_flag;
  22. int ret;
  23. key->key = NULL;
  24. key->tfm = NULL;
  25. switch (key->type) {
  26. case CEPH_CRYPTO_NONE:
  27. return 0; /* nothing to do */
  28. case CEPH_CRYPTO_AES:
  29. break;
  30. default:
  31. return -ENOTSUPP;
  32. }
  33. if (!key->len)
  34. return -EINVAL;
  35. key->key = kmemdup(buf, key->len, GFP_NOIO);
  36. if (!key->key) {
  37. ret = -ENOMEM;
  38. goto fail;
  39. }
  40. /* crypto_alloc_skcipher() allocates with GFP_KERNEL */
  41. noio_flag = memalloc_noio_save();
  42. key->tfm = crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  43. memalloc_noio_restore(noio_flag);
  44. if (IS_ERR(key->tfm)) {
  45. ret = PTR_ERR(key->tfm);
  46. key->tfm = NULL;
  47. goto fail;
  48. }
  49. ret = crypto_skcipher_setkey(key->tfm, key->key, key->len);
  50. if (ret)
  51. goto fail;
  52. return 0;
  53. fail:
  54. ceph_crypto_key_destroy(key);
  55. return ret;
  56. }
  57. int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
  58. const struct ceph_crypto_key *src)
  59. {
  60. memcpy(dst, src, sizeof(struct ceph_crypto_key));
  61. return set_secret(dst, src->key);
  62. }
  63. int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
  64. {
  65. if (*p + sizeof(u16) + sizeof(key->created) +
  66. sizeof(u16) + key->len > end)
  67. return -ERANGE;
  68. ceph_encode_16(p, key->type);
  69. ceph_encode_copy(p, &key->created, sizeof(key->created));
  70. ceph_encode_16(p, key->len);
  71. ceph_encode_copy(p, key->key, key->len);
  72. return 0;
  73. }
  74. int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
  75. {
  76. int ret;
  77. ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad);
  78. key->type = ceph_decode_16(p);
  79. ceph_decode_copy(p, &key->created, sizeof(key->created));
  80. key->len = ceph_decode_16(p);
  81. ceph_decode_need(p, end, key->len, bad);
  82. ret = set_secret(key, *p);
  83. *p += key->len;
  84. return ret;
  85. bad:
  86. dout("failed to decode crypto key\n");
  87. return -EINVAL;
  88. }
  89. int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
  90. {
  91. int inlen = strlen(inkey);
  92. int blen = inlen * 3 / 4;
  93. void *buf, *p;
  94. int ret;
  95. dout("crypto_key_unarmor %s\n", inkey);
  96. buf = kmalloc(blen, GFP_NOFS);
  97. if (!buf)
  98. return -ENOMEM;
  99. blen = ceph_unarmor(buf, inkey, inkey+inlen);
  100. if (blen < 0) {
  101. kfree(buf);
  102. return blen;
  103. }
  104. p = buf;
  105. ret = ceph_crypto_key_decode(key, &p, p + blen);
  106. kfree(buf);
  107. if (ret)
  108. return ret;
  109. dout("crypto_key_unarmor key %p type %d len %d\n", key,
  110. key->type, key->len);
  111. return 0;
  112. }
  113. void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
  114. {
  115. if (key) {
  116. kfree(key->key);
  117. key->key = NULL;
  118. crypto_free_skcipher(key->tfm);
  119. key->tfm = NULL;
  120. }
  121. }
  122. static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
  123. /*
  124. * Should be used for buffers allocated with ceph_kvmalloc().
  125. * Currently these are encrypt out-buffer (ceph_buffer) and decrypt
  126. * in-buffer (msg front).
  127. *
  128. * Dispose of @sgt with teardown_sgtable().
  129. *
  130. * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
  131. * in cases where a single sg is sufficient. No attempt to reduce the
  132. * number of sgs by squeezing physically contiguous pages together is
  133. * made though, for simplicity.
  134. */
  135. static int setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
  136. const void *buf, unsigned int buf_len)
  137. {
  138. struct scatterlist *sg;
  139. const bool is_vmalloc = is_vmalloc_addr(buf);
  140. unsigned int off = offset_in_page(buf);
  141. unsigned int chunk_cnt = 1;
  142. unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
  143. int i;
  144. int ret;
  145. if (buf_len == 0) {
  146. memset(sgt, 0, sizeof(*sgt));
  147. return -EINVAL;
  148. }
  149. if (is_vmalloc) {
  150. chunk_cnt = chunk_len >> PAGE_SHIFT;
  151. chunk_len = PAGE_SIZE;
  152. }
  153. if (chunk_cnt > 1) {
  154. ret = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
  155. if (ret)
  156. return ret;
  157. } else {
  158. WARN_ON(chunk_cnt != 1);
  159. sg_init_table(prealloc_sg, 1);
  160. sgt->sgl = prealloc_sg;
  161. sgt->nents = sgt->orig_nents = 1;
  162. }
  163. for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
  164. struct page *page;
  165. unsigned int len = min(chunk_len - off, buf_len);
  166. if (is_vmalloc)
  167. page = vmalloc_to_page(buf);
  168. else
  169. page = virt_to_page(buf);
  170. sg_set_page(sg, page, len, off);
  171. off = 0;
  172. buf += len;
  173. buf_len -= len;
  174. }
  175. WARN_ON(buf_len != 0);
  176. return 0;
  177. }
  178. static void teardown_sgtable(struct sg_table *sgt)
  179. {
  180. if (sgt->orig_nents > 1)
  181. sg_free_table(sgt);
  182. }
  183. static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
  184. void *buf, int buf_len, int in_len, int *pout_len)
  185. {
  186. SKCIPHER_REQUEST_ON_STACK(req, key->tfm);
  187. struct sg_table sgt;
  188. struct scatterlist prealloc_sg;
  189. char iv[AES_BLOCK_SIZE] __aligned(8);
  190. int pad_byte = AES_BLOCK_SIZE - (in_len & (AES_BLOCK_SIZE - 1));
  191. int crypt_len = encrypt ? in_len + pad_byte : in_len;
  192. int ret;
  193. WARN_ON(crypt_len > buf_len);
  194. if (encrypt)
  195. memset(buf + in_len, pad_byte, pad_byte);
  196. ret = setup_sgtable(&sgt, &prealloc_sg, buf, crypt_len);
  197. if (ret)
  198. return ret;
  199. memcpy(iv, aes_iv, AES_BLOCK_SIZE);
  200. skcipher_request_set_tfm(req, key->tfm);
  201. skcipher_request_set_callback(req, 0, NULL, NULL);
  202. skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv);
  203. /*
  204. print_hex_dump(KERN_ERR, "key: ", DUMP_PREFIX_NONE, 16, 1,
  205. key->key, key->len, 1);
  206. print_hex_dump(KERN_ERR, " in: ", DUMP_PREFIX_NONE, 16, 1,
  207. buf, crypt_len, 1);
  208. */
  209. if (encrypt)
  210. ret = crypto_skcipher_encrypt(req);
  211. else
  212. ret = crypto_skcipher_decrypt(req);
  213. skcipher_request_zero(req);
  214. if (ret) {
  215. pr_err("%s %scrypt failed: %d\n", __func__,
  216. encrypt ? "en" : "de", ret);
  217. goto out_sgt;
  218. }
  219. /*
  220. print_hex_dump(KERN_ERR, "out: ", DUMP_PREFIX_NONE, 16, 1,
  221. buf, crypt_len, 1);
  222. */
  223. if (encrypt) {
  224. *pout_len = crypt_len;
  225. } else {
  226. pad_byte = *(char *)(buf + in_len - 1);
  227. if (pad_byte > 0 && pad_byte <= AES_BLOCK_SIZE &&
  228. in_len >= pad_byte) {
  229. *pout_len = in_len - pad_byte;
  230. } else {
  231. pr_err("%s got bad padding %d on in_len %d\n",
  232. __func__, pad_byte, in_len);
  233. ret = -EPERM;
  234. goto out_sgt;
  235. }
  236. }
  237. out_sgt:
  238. teardown_sgtable(&sgt);
  239. return ret;
  240. }
  241. int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
  242. void *buf, int buf_len, int in_len, int *pout_len)
  243. {
  244. switch (key->type) {
  245. case CEPH_CRYPTO_NONE:
  246. *pout_len = in_len;
  247. return 0;
  248. case CEPH_CRYPTO_AES:
  249. return ceph_aes_crypt(key, encrypt, buf, buf_len, in_len,
  250. pout_len);
  251. default:
  252. return -ENOTSUPP;
  253. }
  254. }
  255. static int ceph_key_preparse(struct key_preparsed_payload *prep)
  256. {
  257. struct ceph_crypto_key *ckey;
  258. size_t datalen = prep->datalen;
  259. int ret;
  260. void *p;
  261. ret = -EINVAL;
  262. if (datalen <= 0 || datalen > 32767 || !prep->data)
  263. goto err;
  264. ret = -ENOMEM;
  265. ckey = kmalloc(sizeof(*ckey), GFP_KERNEL);
  266. if (!ckey)
  267. goto err;
  268. /* TODO ceph_crypto_key_decode should really take const input */
  269. p = (void *)prep->data;
  270. ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
  271. if (ret < 0)
  272. goto err_ckey;
  273. prep->payload.data[0] = ckey;
  274. prep->quotalen = datalen;
  275. return 0;
  276. err_ckey:
  277. kfree(ckey);
  278. err:
  279. return ret;
  280. }
  281. static void ceph_key_free_preparse(struct key_preparsed_payload *prep)
  282. {
  283. struct ceph_crypto_key *ckey = prep->payload.data[0];
  284. ceph_crypto_key_destroy(ckey);
  285. kfree(ckey);
  286. }
  287. static void ceph_key_destroy(struct key *key)
  288. {
  289. struct ceph_crypto_key *ckey = key->payload.data[0];
  290. ceph_crypto_key_destroy(ckey);
  291. kfree(ckey);
  292. }
  293. struct key_type key_type_ceph = {
  294. .name = "ceph",
  295. .preparse = ceph_key_preparse,
  296. .free_preparse = ceph_key_free_preparse,
  297. .instantiate = generic_key_instantiate,
  298. .destroy = ceph_key_destroy,
  299. };
  300. int __init ceph_crypto_init(void)
  301. {
  302. return register_key_type(&key_type_ceph);
  303. }
  304. void ceph_crypto_shutdown(void)
  305. {
  306. unregister_key_type(&key_type_ceph);
  307. }