crypto.c 7.8 KB

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