xref: /linux-6.15/net/ceph/crypto.c (revision 2b1e1a7c)
1 
2 #include <linux/ceph/ceph_debug.h>
3 
4 #include <linux/err.h>
5 #include <linux/scatterlist.h>
6 #include <linux/slab.h>
7 #include <crypto/aes.h>
8 #include <crypto/skcipher.h>
9 #include <linux/key-type.h>
10 
11 #include <keys/ceph-type.h>
12 #include <keys/user-type.h>
13 #include <linux/ceph/decode.h>
14 #include "crypto.h"
15 
16 int 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));
20 	dst->key = kmemdup(src->key, src->len, GFP_NOFS);
21 	if (!dst->key)
22 		return -ENOMEM;
23 	return 0;
24 }
25 
26 int 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 
38 int 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 
51 bad:
52 	dout("failed to decode crypto key\n");
53 	return -EINVAL;
54 }
55 
56 int 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 
83 static struct crypto_skcipher *ceph_crypto_alloc_cipher(void)
84 {
85 	return crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
86 }
87 
88 static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
89 
90 /*
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  */
102 static 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 
154 static void teardown_sgtable(struct sg_table *sgt)
155 {
156 	if (sgt->orig_nents > 1)
157 		sg_free_table(sgt);
158 }
159 
160 static 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 
225 out_sgt:
226 	teardown_sgtable(&sgt);
227 out_tfm:
228 	crypto_free_skcipher(tfm);
229 	return ret;
230 }
231 
232 int 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 
247 static int ceph_key_preparse(struct key_preparsed_payload *prep)
248 {
249 	struct ceph_crypto_key *ckey;
250 	size_t datalen = prep->datalen;
251 	int ret;
252 	void *p;
253 
254 	ret = -EINVAL;
255 	if (datalen <= 0 || datalen > 32767 || !prep->data)
256 		goto err;
257 
258 	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 */
264 	p = (void *)prep->data;
265 	ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
266 	if (ret < 0)
267 		goto err_ckey;
268 
269 	prep->payload.data[0] = ckey;
270 	prep->quotalen = datalen;
271 	return 0;
272 
273 err_ckey:
274 	kfree(ckey);
275 err:
276 	return ret;
277 }
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 
286 static void ceph_key_destroy(struct key *key)
287 {
288 	struct ceph_crypto_key *ckey = key->payload.data[0];
289 
290 	ceph_crypto_key_destroy(ckey);
291 	kfree(ckey);
292 }
293 
294 struct key_type key_type_ceph = {
295 	.name		= "ceph",
296 	.preparse	= ceph_key_preparse,
297 	.free_preparse	= ceph_key_free_preparse,
298 	.instantiate	= generic_key_instantiate,
299 	.destroy	= ceph_key_destroy,
300 };
301 
302 int ceph_crypto_init(void) {
303 	return register_key_type(&key_type_ceph);
304 }
305 
306 void ceph_crypto_shutdown(void) {
307 	unregister_key_type(&key_type_ceph);
308 }
309