crypto.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_sync_skcipher() allocates with GFP_KERNEL */
  41. noio_flag = memalloc_noio_save();
  42. key->tfm = crypto_alloc_sync_skcipher("cbc(aes)", 0, 0);
  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_sync_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. memzero_explicit(*p, key->len);
  84. *p += key->len;
  85. return ret;
  86. bad:
  87. dout("failed to decode crypto key\n");
  88. return -EINVAL;
  89. }
  90. int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
  91. {
  92. int inlen = strlen(inkey);
  93. int blen = inlen * 3 / 4;
  94. void *buf, *p;
  95. int ret;
  96. dout("crypto_key_unarmor %s\n", inkey);
  97. buf = kmalloc(blen, GFP_NOFS);
  98. if (!buf)
  99. return -ENOMEM;
  100. blen = ceph_unarmor(buf, inkey, inkey+inlen);
  101. if (blen < 0) {
  102. kfree(buf);
  103. return blen;
  104. }
  105. p = buf;
  106. ret = ceph_crypto_key_decode(key, &p, p + blen);
  107. kfree(buf);
  108. if (ret)
  109. return ret;
  110. dout("crypto_key_unarmor key %p type %d len %d\n", key,
  111. key->type, key->len);
  112. return 0;
  113. }
  114. void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
  115. {
  116. if (key) {
  117. kfree_sensitive(key->key);
  118. key->key = NULL;
  119. if (key->tfm) {
  120. crypto_free_sync_skcipher(key->tfm);
  121. key->tfm = NULL;
  122. }
  123. }
  124. }
  125. static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
  126. /*
  127. * Should be used for buffers allocated with kvmalloc().
  128. * Currently these are encrypt out-buffer (ceph_buffer) and decrypt
  129. * in-buffer (msg front).
  130. *
  131. * Dispose of @sgt with teardown_sgtable().
  132. *
  133. * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
  134. * in cases where a single sg is sufficient. No attempt to reduce the
  135. * number of sgs by squeezing physically contiguous pages together is
  136. * made though, for simplicity.
  137. */
  138. static int setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
  139. const void *buf, unsigned int buf_len)
  140. {
  141. struct scatterlist *sg;
  142. const bool is_vmalloc = is_vmalloc_addr(buf);
  143. unsigned int off = offset_in_page(buf);
  144. unsigned int chunk_cnt = 1;
  145. unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
  146. int i;
  147. int ret;
  148. if (buf_len == 0) {
  149. memset(sgt, 0, sizeof(*sgt));
  150. return -EINVAL;
  151. }
  152. if (is_vmalloc) {
  153. chunk_cnt = chunk_len >> PAGE_SHIFT;
  154. chunk_len = PAGE_SIZE;
  155. }
  156. if (chunk_cnt > 1) {
  157. ret = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
  158. if (ret)
  159. return ret;
  160. } else {
  161. WARN_ON(chunk_cnt != 1);
  162. sg_init_table(prealloc_sg, 1);
  163. sgt->sgl = prealloc_sg;
  164. sgt->nents = sgt->orig_nents = 1;
  165. }
  166. for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
  167. struct page *page;
  168. unsigned int len = min(chunk_len - off, buf_len);
  169. if (is_vmalloc)
  170. page = vmalloc_to_page(buf);
  171. else
  172. page = virt_to_page(buf);
  173. sg_set_page(sg, page, len, off);
  174. off = 0;
  175. buf += len;
  176. buf_len -= len;
  177. }
  178. WARN_ON(buf_len != 0);
  179. return 0;
  180. }
  181. static void teardown_sgtable(struct sg_table *sgt)
  182. {
  183. if (sgt->orig_nents > 1)
  184. sg_free_table(sgt);
  185. }
  186. static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
  187. void *buf, int buf_len, int in_len, int *pout_len)
  188. {
  189. SYNC_SKCIPHER_REQUEST_ON_STACK(req, key->tfm);
  190. struct sg_table sgt;
  191. struct scatterlist prealloc_sg;
  192. char iv[AES_BLOCK_SIZE] __aligned(8);
  193. int pad_byte = AES_BLOCK_SIZE - (in_len & (AES_BLOCK_SIZE - 1));
  194. int crypt_len = encrypt ? in_len + pad_byte : in_len;
  195. int ret;
  196. WARN_ON(crypt_len > buf_len);
  197. if (encrypt)
  198. memset(buf + in_len, pad_byte, pad_byte);
  199. ret = setup_sgtable(&sgt, &prealloc_sg, buf, crypt_len);
  200. if (ret)
  201. return ret;
  202. memcpy(iv, aes_iv, AES_BLOCK_SIZE);
  203. skcipher_request_set_sync_tfm(req, key->tfm);
  204. skcipher_request_set_callback(req, 0, NULL, NULL);
  205. skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv);
  206. /*
  207. print_hex_dump(KERN_ERR, "key: ", DUMP_PREFIX_NONE, 16, 1,
  208. key->key, key->len, 1);
  209. print_hex_dump(KERN_ERR, " in: ", DUMP_PREFIX_NONE, 16, 1,
  210. buf, crypt_len, 1);
  211. */
  212. if (encrypt)
  213. ret = crypto_skcipher_encrypt(req);
  214. else
  215. ret = crypto_skcipher_decrypt(req);
  216. skcipher_request_zero(req);
  217. if (ret) {
  218. pr_err("%s %scrypt failed: %d\n", __func__,
  219. encrypt ? "en" : "de", ret);
  220. goto out_sgt;
  221. }
  222. /*
  223. print_hex_dump(KERN_ERR, "out: ", DUMP_PREFIX_NONE, 16, 1,
  224. buf, crypt_len, 1);
  225. */
  226. if (encrypt) {
  227. *pout_len = crypt_len;
  228. } else {
  229. pad_byte = *(char *)(buf + in_len - 1);
  230. if (pad_byte > 0 && pad_byte <= AES_BLOCK_SIZE &&
  231. in_len >= pad_byte) {
  232. *pout_len = in_len - pad_byte;
  233. } else {
  234. pr_err("%s got bad padding %d on in_len %d\n",
  235. __func__, pad_byte, in_len);
  236. ret = -EPERM;
  237. goto out_sgt;
  238. }
  239. }
  240. out_sgt:
  241. teardown_sgtable(&sgt);
  242. return ret;
  243. }
  244. int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
  245. void *buf, int buf_len, int in_len, int *pout_len)
  246. {
  247. switch (key->type) {
  248. case CEPH_CRYPTO_NONE:
  249. *pout_len = in_len;
  250. return 0;
  251. case CEPH_CRYPTO_AES:
  252. return ceph_aes_crypt(key, encrypt, buf, buf_len, in_len,
  253. pout_len);
  254. default:
  255. return -ENOTSUPP;
  256. }
  257. }
  258. static int ceph_key_preparse(struct key_preparsed_payload *prep)
  259. {
  260. struct ceph_crypto_key *ckey;
  261. size_t datalen = prep->datalen;
  262. int ret;
  263. void *p;
  264. ret = -EINVAL;
  265. if (datalen <= 0 || datalen > 32767 || !prep->data)
  266. goto err;
  267. ret = -ENOMEM;
  268. ckey = kmalloc(sizeof(*ckey), GFP_KERNEL);
  269. if (!ckey)
  270. goto err;
  271. /* TODO ceph_crypto_key_decode should really take const input */
  272. p = (void *)prep->data;
  273. ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
  274. if (ret < 0)
  275. goto err_ckey;
  276. prep->payload.data[0] = ckey;
  277. prep->quotalen = datalen;
  278. return 0;
  279. err_ckey:
  280. kfree(ckey);
  281. err:
  282. return ret;
  283. }
  284. static void ceph_key_free_preparse(struct key_preparsed_payload *prep)
  285. {
  286. struct ceph_crypto_key *ckey = prep->payload.data[0];
  287. ceph_crypto_key_destroy(ckey);
  288. kfree(ckey);
  289. }
  290. static void ceph_key_destroy(struct key *key)
  291. {
  292. struct ceph_crypto_key *ckey = key->payload.data[0];
  293. ceph_crypto_key_destroy(ckey);
  294. kfree(ckey);
  295. }
  296. struct key_type key_type_ceph = {
  297. .name = "ceph",
  298. .preparse = ceph_key_preparse,
  299. .free_preparse = ceph_key_free_preparse,
  300. .instantiate = generic_key_instantiate,
  301. .destroy = ceph_key_destroy,
  302. };
  303. int __init ceph_crypto_init(void)
  304. {
  305. return register_key_type(&key_type_ceph);
  306. }
  307. void ceph_crypto_shutdown(void)
  308. {
  309. unregister_key_type(&key_type_ceph);
  310. }