blob: 38936e1fd644cdd089b36370eea30c2223538f51 [file] [log] [blame]
Sage Weil8b6e4f22010-02-02 16:07:07 -08001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weil8b6e4f22010-02-02 16:07:07 -08003
4#include <linux/err.h>
5#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Herbert Xue59dd982016-01-24 21:18:40 +08007#include <crypto/aes.h>
8#include <crypto/skcipher.h>
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -07009#include <linux/key-type.h>
Sage Weil8b6e4f22010-02-02 16:07:07 -080010
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -070011#include <keys/ceph-type.h>
David Howells7c3bec02014-07-18 18:56:35 +010012#include <keys/user-type.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013#include <linux/ceph/decode.h>
Sage Weil8b6e4f22010-02-02 16:07:07 -080014#include "crypto.h"
Sage Weil8b6e4f22010-02-02 16:07:07 -080015
Tommi Virtanen8323c3a2011-03-25 16:32:57 -070016int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
17 const struct ceph_crypto_key *src)
18{
19 memcpy(dst, src, sizeof(struct ceph_crypto_key));
Thomas Meyer18648252011-11-10 19:45:21 +010020 dst->key = kmemdup(src->key, src->len, GFP_NOFS);
Tommi Virtanen8323c3a2011-03-25 16:32:57 -070021 if (!dst->key)
22 return -ENOMEM;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -070023 return 0;
24}
25
Sage Weil8b6e4f22010-02-02 16:07:07 -080026int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
27{
28 if (*p + sizeof(u16) + sizeof(key->created) +
29 sizeof(u16) + key->len > end)
30 return -ERANGE;
31 ceph_encode_16(p, key->type);
32 ceph_encode_copy(p, &key->created, sizeof(key->created));
33 ceph_encode_16(p, key->len);
34 ceph_encode_copy(p, key->key, key->len);
35 return 0;
36}
37
38int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
39{
40 ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad);
41 key->type = ceph_decode_16(p);
42 ceph_decode_copy(p, &key->created, sizeof(key->created));
43 key->len = ceph_decode_16(p);
44 ceph_decode_need(p, end, key->len, bad);
45 key->key = kmalloc(key->len, GFP_NOFS);
46 if (!key->key)
47 return -ENOMEM;
48 ceph_decode_copy(p, key->key, key->len);
49 return 0;
50
51bad:
52 dout("failed to decode crypto key\n");
53 return -EINVAL;
54}
55
56int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
57{
58 int inlen = strlen(inkey);
59 int blen = inlen * 3 / 4;
60 void *buf, *p;
61 int ret;
62
63 dout("crypto_key_unarmor %s\n", inkey);
64 buf = kmalloc(blen, GFP_NOFS);
65 if (!buf)
66 return -ENOMEM;
67 blen = ceph_unarmor(buf, inkey, inkey+inlen);
68 if (blen < 0) {
69 kfree(buf);
70 return blen;
71 }
72
73 p = buf;
74 ret = ceph_crypto_key_decode(key, &p, p + blen);
75 kfree(buf);
76 if (ret)
77 return ret;
78 dout("crypto_key_unarmor key %p type %d len %d\n", key,
79 key->type, key->len);
80 return 0;
81}
82
Herbert Xue59dd982016-01-24 21:18:40 +080083static struct crypto_skcipher *ceph_crypto_alloc_cipher(void)
Sage Weil8b6e4f22010-02-02 16:07:07 -080084{
Herbert Xue59dd982016-01-24 21:18:40 +080085 return crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
Sage Weil8b6e4f22010-02-02 16:07:07 -080086}
87
Sage Weilcbbfe492010-08-02 15:48:23 -070088static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
Sage Weil8b6e4f22010-02-02 16:07:07 -080089
Ilya Dryomovaaef3172014-10-23 00:25:22 +040090/*
91 * Should be used for buffers allocated with ceph_kvmalloc().
92 * Currently these are encrypt out-buffer (ceph_buffer) and decrypt
93 * in-buffer (msg front).
94 *
95 * Dispose of @sgt with teardown_sgtable().
96 *
97 * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
98 * in cases where a single sg is sufficient. No attempt to reduce the
99 * number of sgs by squeezing physically contiguous pages together is
100 * made though, for simplicity.
101 */
102static int setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
103 const void *buf, unsigned int buf_len)
104{
105 struct scatterlist *sg;
106 const bool is_vmalloc = is_vmalloc_addr(buf);
107 unsigned int off = offset_in_page(buf);
108 unsigned int chunk_cnt = 1;
109 unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
110 int i;
111 int ret;
112
113 if (buf_len == 0) {
114 memset(sgt, 0, sizeof(*sgt));
115 return -EINVAL;
116 }
117
118 if (is_vmalloc) {
119 chunk_cnt = chunk_len >> PAGE_SHIFT;
120 chunk_len = PAGE_SIZE;
121 }
122
123 if (chunk_cnt > 1) {
124 ret = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
125 if (ret)
126 return ret;
127 } else {
128 WARN_ON(chunk_cnt != 1);
129 sg_init_table(prealloc_sg, 1);
130 sgt->sgl = prealloc_sg;
131 sgt->nents = sgt->orig_nents = 1;
132 }
133
134 for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
135 struct page *page;
136 unsigned int len = min(chunk_len - off, buf_len);
137
138 if (is_vmalloc)
139 page = vmalloc_to_page(buf);
140 else
141 page = virt_to_page(buf);
142
143 sg_set_page(sg, page, len, off);
144
145 off = 0;
146 buf += len;
147 buf_len -= len;
148 }
149 WARN_ON(buf_len != 0);
150
151 return 0;
152}
153
154static void teardown_sgtable(struct sg_table *sgt)
155{
156 if (sgt->orig_nents > 1)
157 sg_free_table(sgt);
158}
159
Ilya Dryomova45f7952016-12-02 16:35:07 +0100160static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
161 void *buf, int buf_len, int in_len, int *pout_len)
162{
163 struct crypto_skcipher *tfm = ceph_crypto_alloc_cipher();
164 SKCIPHER_REQUEST_ON_STACK(req, tfm);
165 struct sg_table sgt;
166 struct scatterlist prealloc_sg;
167 char iv[AES_BLOCK_SIZE];
168 int pad_byte = AES_BLOCK_SIZE - (in_len & (AES_BLOCK_SIZE - 1));
169 int crypt_len = encrypt ? in_len + pad_byte : in_len;
170 int ret;
171
172 if (IS_ERR(tfm))
173 return PTR_ERR(tfm);
174
175 WARN_ON(crypt_len > buf_len);
176 if (encrypt)
177 memset(buf + in_len, pad_byte, pad_byte);
178 ret = setup_sgtable(&sgt, &prealloc_sg, buf, crypt_len);
179 if (ret)
180 goto out_tfm;
181
182 crypto_skcipher_setkey((void *)tfm, key->key, key->len);
183 memcpy(iv, aes_iv, AES_BLOCK_SIZE);
184
185 skcipher_request_set_tfm(req, tfm);
186 skcipher_request_set_callback(req, 0, NULL, NULL);
187 skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv);
188
189 /*
190 print_hex_dump(KERN_ERR, "key: ", DUMP_PREFIX_NONE, 16, 1,
191 key->key, key->len, 1);
192 print_hex_dump(KERN_ERR, " in: ", DUMP_PREFIX_NONE, 16, 1,
193 buf, crypt_len, 1);
194 */
195 if (encrypt)
196 ret = crypto_skcipher_encrypt(req);
197 else
198 ret = crypto_skcipher_decrypt(req);
199 skcipher_request_zero(req);
200 if (ret) {
201 pr_err("%s %scrypt failed: %d\n", __func__,
202 encrypt ? "en" : "de", ret);
203 goto out_sgt;
204 }
205 /*
206 print_hex_dump(KERN_ERR, "out: ", DUMP_PREFIX_NONE, 16, 1,
207 buf, crypt_len, 1);
208 */
209
210 if (encrypt) {
211 *pout_len = crypt_len;
212 } else {
213 pad_byte = *(char *)(buf + in_len - 1);
214 if (pad_byte > 0 && pad_byte <= AES_BLOCK_SIZE &&
215 in_len >= pad_byte) {
216 *pout_len = in_len - pad_byte;
217 } else {
218 pr_err("%s got bad padding %d on in_len %d\n",
219 __func__, pad_byte, in_len);
220 ret = -EPERM;
221 goto out_sgt;
222 }
223 }
224
225out_sgt:
226 teardown_sgtable(&sgt);
227out_tfm:
228 crypto_free_skcipher(tfm);
229 return ret;
230}
231
232int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
233 void *buf, int buf_len, int in_len, int *pout_len)
234{
235 switch (key->type) {
236 case CEPH_CRYPTO_NONE:
237 *pout_len = in_len;
238 return 0;
239 case CEPH_CRYPTO_AES:
240 return ceph_aes_crypt(key, encrypt, buf, buf_len, in_len,
241 pout_len);
242 default:
243 return -ENOTSUPP;
244 }
245}
246
David Howellsefa64c02014-07-18 18:56:35 +0100247static int ceph_key_preparse(struct key_preparsed_payload *prep)
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700248{
249 struct ceph_crypto_key *ckey;
David Howellscf7f6012012-09-13 13:06:29 +0100250 size_t datalen = prep->datalen;
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700251 int ret;
252 void *p;
253
254 ret = -EINVAL;
David Howellscf7f6012012-09-13 13:06:29 +0100255 if (datalen <= 0 || datalen > 32767 || !prep->data)
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700256 goto err;
257
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700258 ret = -ENOMEM;
259 ckey = kmalloc(sizeof(*ckey), GFP_KERNEL);
260 if (!ckey)
261 goto err;
262
263 /* TODO ceph_crypto_key_decode should really take const input */
David Howellscf7f6012012-09-13 13:06:29 +0100264 p = (void *)prep->data;
265 ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700266 if (ret < 0)
267 goto err_ckey;
268
David Howells146aa8b2015-10-21 14:04:48 +0100269 prep->payload.data[0] = ckey;
David Howellsefa64c02014-07-18 18:56:35 +0100270 prep->quotalen = datalen;
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700271 return 0;
272
273err_ckey:
274 kfree(ckey);
275err:
276 return ret;
277}
278
David Howellsefa64c02014-07-18 18:56:35 +0100279static void ceph_key_free_preparse(struct key_preparsed_payload *prep)
280{
David Howells146aa8b2015-10-21 14:04:48 +0100281 struct ceph_crypto_key *ckey = prep->payload.data[0];
David Howellsefa64c02014-07-18 18:56:35 +0100282 ceph_crypto_key_destroy(ckey);
283 kfree(ckey);
284}
285
David Howellsefa64c02014-07-18 18:56:35 +0100286static void ceph_key_destroy(struct key *key)
287{
David Howells146aa8b2015-10-21 14:04:48 +0100288 struct ceph_crypto_key *ckey = key->payload.data[0];
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700289
290 ceph_crypto_key_destroy(ckey);
Sylvain Munautf0666b12012-08-02 09:12:59 -0700291 kfree(ckey);
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700292}
293
294struct key_type key_type_ceph = {
295 .name = "ceph",
David Howellsefa64c02014-07-18 18:56:35 +0100296 .preparse = ceph_key_preparse,
297 .free_preparse = ceph_key_free_preparse,
298 .instantiate = generic_key_instantiate,
Tommi Virtanen4b2a58a2011-03-28 14:59:38 -0700299 .destroy = ceph_key_destroy,
300};
301
302int ceph_crypto_init(void) {
303 return register_key_type(&key_type_ceph);
304}
305
306void ceph_crypto_shutdown(void) {
307 unregister_key_type(&key_type_ceph);
308}