crypto.c 7.8 KB

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