crypto.c 7.8 KB

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