crypto.c 7.9 KB

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