xref: /openbmc/linux/net/ceph/crypto.c (revision a421ef30)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
43d14c5d2SYehuda Sadeh 
53d14c5d2SYehuda Sadeh #include <linux/err.h>
63d14c5d2SYehuda Sadeh #include <linux/scatterlist.h>
77fea24c6SIlya Dryomov #include <linux/sched.h>
83d14c5d2SYehuda Sadeh #include <linux/slab.h>
9e59dd982SHerbert Xu #include <crypto/aes.h>
10e59dd982SHerbert Xu #include <crypto/skcipher.h>
114b2a58abSTommi Virtanen #include <linux/key-type.h>
125b3cc15aSIngo Molnar #include <linux/sched/mm.h>
133d14c5d2SYehuda Sadeh 
144b2a58abSTommi Virtanen #include <keys/ceph-type.h>
157c3bec0aSDavid Howells #include <keys/user-type.h>
163d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
173d14c5d2SYehuda Sadeh #include "crypto.h"
183d14c5d2SYehuda Sadeh 
197af3ea18SIlya Dryomov /*
207af3ea18SIlya Dryomov  * Set ->key and ->tfm.  The rest of the key should be filled in before
217af3ea18SIlya Dryomov  * this function is called.
227af3ea18SIlya Dryomov  */
set_secret(struct ceph_crypto_key * key,void * buf)237af3ea18SIlya Dryomov static int set_secret(struct ceph_crypto_key *key, void *buf)
247af3ea18SIlya Dryomov {
257af3ea18SIlya Dryomov 	unsigned int noio_flag;
267af3ea18SIlya Dryomov 	int ret;
277af3ea18SIlya Dryomov 
287af3ea18SIlya Dryomov 	key->key = NULL;
297af3ea18SIlya Dryomov 	key->tfm = NULL;
307af3ea18SIlya Dryomov 
317af3ea18SIlya Dryomov 	switch (key->type) {
327af3ea18SIlya Dryomov 	case CEPH_CRYPTO_NONE:
337af3ea18SIlya Dryomov 		return 0; /* nothing to do */
347af3ea18SIlya Dryomov 	case CEPH_CRYPTO_AES:
357af3ea18SIlya Dryomov 		break;
367af3ea18SIlya Dryomov 	default:
377af3ea18SIlya Dryomov 		return -ENOTSUPP;
387af3ea18SIlya Dryomov 	}
397af3ea18SIlya Dryomov 
40b1127085SEric Biggers 	if (!key->len)
41b1127085SEric Biggers 		return -EINVAL;
42b1127085SEric Biggers 
437af3ea18SIlya Dryomov 	key->key = kmemdup(buf, key->len, GFP_NOIO);
447af3ea18SIlya Dryomov 	if (!key->key) {
457af3ea18SIlya Dryomov 		ret = -ENOMEM;
467af3ea18SIlya Dryomov 		goto fail;
477af3ea18SIlya Dryomov 	}
487af3ea18SIlya Dryomov 
4969d6302bSKees Cook 	/* crypto_alloc_sync_skcipher() allocates with GFP_KERNEL */
507af3ea18SIlya Dryomov 	noio_flag = memalloc_noio_save();
5169d6302bSKees Cook 	key->tfm = crypto_alloc_sync_skcipher("cbc(aes)", 0, 0);
527af3ea18SIlya Dryomov 	memalloc_noio_restore(noio_flag);
537af3ea18SIlya Dryomov 	if (IS_ERR(key->tfm)) {
547af3ea18SIlya Dryomov 		ret = PTR_ERR(key->tfm);
557af3ea18SIlya Dryomov 		key->tfm = NULL;
567af3ea18SIlya Dryomov 		goto fail;
577af3ea18SIlya Dryomov 	}
587af3ea18SIlya Dryomov 
5969d6302bSKees Cook 	ret = crypto_sync_skcipher_setkey(key->tfm, key->key, key->len);
607af3ea18SIlya Dryomov 	if (ret)
617af3ea18SIlya Dryomov 		goto fail;
627af3ea18SIlya Dryomov 
637af3ea18SIlya Dryomov 	return 0;
647af3ea18SIlya Dryomov 
657af3ea18SIlya Dryomov fail:
667af3ea18SIlya Dryomov 	ceph_crypto_key_destroy(key);
677af3ea18SIlya Dryomov 	return ret;
687af3ea18SIlya Dryomov }
697af3ea18SIlya Dryomov 
ceph_crypto_key_clone(struct ceph_crypto_key * dst,const struct ceph_crypto_key * src)708323c3aaSTommi Virtanen int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
718323c3aaSTommi Virtanen 			  const struct ceph_crypto_key *src)
728323c3aaSTommi Virtanen {
738323c3aaSTommi Virtanen 	memcpy(dst, src, sizeof(struct ceph_crypto_key));
747af3ea18SIlya Dryomov 	return set_secret(dst, src->key);
758323c3aaSTommi Virtanen }
768323c3aaSTommi Virtanen 
ceph_crypto_key_encode(struct ceph_crypto_key * key,void ** p,void * end)773d14c5d2SYehuda Sadeh int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
783d14c5d2SYehuda Sadeh {
793d14c5d2SYehuda Sadeh 	if (*p + sizeof(u16) + sizeof(key->created) +
803d14c5d2SYehuda Sadeh 	    sizeof(u16) + key->len > end)
813d14c5d2SYehuda Sadeh 		return -ERANGE;
823d14c5d2SYehuda Sadeh 	ceph_encode_16(p, key->type);
833d14c5d2SYehuda Sadeh 	ceph_encode_copy(p, &key->created, sizeof(key->created));
843d14c5d2SYehuda Sadeh 	ceph_encode_16(p, key->len);
853d14c5d2SYehuda Sadeh 	ceph_encode_copy(p, key->key, key->len);
863d14c5d2SYehuda Sadeh 	return 0;
873d14c5d2SYehuda Sadeh }
883d14c5d2SYehuda Sadeh 
ceph_crypto_key_decode(struct ceph_crypto_key * key,void ** p,void * end)893d14c5d2SYehuda Sadeh int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
903d14c5d2SYehuda Sadeh {
917af3ea18SIlya Dryomov 	int ret;
927af3ea18SIlya Dryomov 
933d14c5d2SYehuda Sadeh 	ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad);
943d14c5d2SYehuda Sadeh 	key->type = ceph_decode_16(p);
953d14c5d2SYehuda Sadeh 	ceph_decode_copy(p, &key->created, sizeof(key->created));
963d14c5d2SYehuda Sadeh 	key->len = ceph_decode_16(p);
973d14c5d2SYehuda Sadeh 	ceph_decode_need(p, end, key->len, bad);
987af3ea18SIlya Dryomov 	ret = set_secret(key, *p);
9910f42b3eSIlya Dryomov 	memzero_explicit(*p, key->len);
1007af3ea18SIlya Dryomov 	*p += key->len;
1017af3ea18SIlya Dryomov 	return ret;
1023d14c5d2SYehuda Sadeh 
1033d14c5d2SYehuda Sadeh bad:
1043d14c5d2SYehuda Sadeh 	dout("failed to decode crypto key\n");
1053d14c5d2SYehuda Sadeh 	return -EINVAL;
1063d14c5d2SYehuda Sadeh }
1073d14c5d2SYehuda Sadeh 
ceph_crypto_key_unarmor(struct ceph_crypto_key * key,const char * inkey)1083d14c5d2SYehuda Sadeh int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
1093d14c5d2SYehuda Sadeh {
1103d14c5d2SYehuda Sadeh 	int inlen = strlen(inkey);
1113d14c5d2SYehuda Sadeh 	int blen = inlen * 3 / 4;
1123d14c5d2SYehuda Sadeh 	void *buf, *p;
1133d14c5d2SYehuda Sadeh 	int ret;
1143d14c5d2SYehuda Sadeh 
1153d14c5d2SYehuda Sadeh 	dout("crypto_key_unarmor %s\n", inkey);
1163d14c5d2SYehuda Sadeh 	buf = kmalloc(blen, GFP_NOFS);
1173d14c5d2SYehuda Sadeh 	if (!buf)
1183d14c5d2SYehuda Sadeh 		return -ENOMEM;
1193d14c5d2SYehuda Sadeh 	blen = ceph_unarmor(buf, inkey, inkey+inlen);
1203d14c5d2SYehuda Sadeh 	if (blen < 0) {
1213d14c5d2SYehuda Sadeh 		kfree(buf);
1223d14c5d2SYehuda Sadeh 		return blen;
1233d14c5d2SYehuda Sadeh 	}
1243d14c5d2SYehuda Sadeh 
1253d14c5d2SYehuda Sadeh 	p = buf;
1263d14c5d2SYehuda Sadeh 	ret = ceph_crypto_key_decode(key, &p, p + blen);
1273d14c5d2SYehuda Sadeh 	kfree(buf);
1283d14c5d2SYehuda Sadeh 	if (ret)
1293d14c5d2SYehuda Sadeh 		return ret;
1303d14c5d2SYehuda Sadeh 	dout("crypto_key_unarmor key %p type %d len %d\n", key,
1313d14c5d2SYehuda Sadeh 	     key->type, key->len);
1323d14c5d2SYehuda Sadeh 	return 0;
1333d14c5d2SYehuda Sadeh }
1343d14c5d2SYehuda Sadeh 
ceph_crypto_key_destroy(struct ceph_crypto_key * key)1356db2304aSIlya Dryomov void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
1366db2304aSIlya Dryomov {
1376db2304aSIlya Dryomov 	if (key) {
13810f42b3eSIlya Dryomov 		kfree_sensitive(key->key);
1396db2304aSIlya Dryomov 		key->key = NULL;
140e8c99200SJia-Ju Bai 		if (key->tfm) {
14169d6302bSKees Cook 			crypto_free_sync_skcipher(key->tfm);
1427af3ea18SIlya Dryomov 			key->tfm = NULL;
1436db2304aSIlya Dryomov 		}
1446db2304aSIlya Dryomov 	}
145e8c99200SJia-Ju Bai }
1466db2304aSIlya Dryomov 
1473d14c5d2SYehuda Sadeh static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
1483d14c5d2SYehuda Sadeh 
149aaef3170SIlya Dryomov /*
150*a421ef30SMichal Hocko  * Should be used for buffers allocated with kvmalloc().
151aaef3170SIlya Dryomov  * Currently these are encrypt out-buffer (ceph_buffer) and decrypt
152aaef3170SIlya Dryomov  * in-buffer (msg front).
153aaef3170SIlya Dryomov  *
154aaef3170SIlya Dryomov  * Dispose of @sgt with teardown_sgtable().
155aaef3170SIlya Dryomov  *
156aaef3170SIlya Dryomov  * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
157aaef3170SIlya Dryomov  * in cases where a single sg is sufficient.  No attempt to reduce the
158aaef3170SIlya Dryomov  * number of sgs by squeezing physically contiguous pages together is
159aaef3170SIlya Dryomov  * made though, for simplicity.
160aaef3170SIlya Dryomov  */
setup_sgtable(struct sg_table * sgt,struct scatterlist * prealloc_sg,const void * buf,unsigned int buf_len)161aaef3170SIlya Dryomov static int setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
162aaef3170SIlya Dryomov 			 const void *buf, unsigned int buf_len)
163aaef3170SIlya Dryomov {
164aaef3170SIlya Dryomov 	struct scatterlist *sg;
165aaef3170SIlya Dryomov 	const bool is_vmalloc = is_vmalloc_addr(buf);
166aaef3170SIlya Dryomov 	unsigned int off = offset_in_page(buf);
167aaef3170SIlya Dryomov 	unsigned int chunk_cnt = 1;
168aaef3170SIlya Dryomov 	unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
169aaef3170SIlya Dryomov 	int i;
170aaef3170SIlya Dryomov 	int ret;
171aaef3170SIlya Dryomov 
172aaef3170SIlya Dryomov 	if (buf_len == 0) {
173aaef3170SIlya Dryomov 		memset(sgt, 0, sizeof(*sgt));
174aaef3170SIlya Dryomov 		return -EINVAL;
175aaef3170SIlya Dryomov 	}
176aaef3170SIlya Dryomov 
177aaef3170SIlya Dryomov 	if (is_vmalloc) {
178aaef3170SIlya Dryomov 		chunk_cnt = chunk_len >> PAGE_SHIFT;
179aaef3170SIlya Dryomov 		chunk_len = PAGE_SIZE;
180aaef3170SIlya Dryomov 	}
181aaef3170SIlya Dryomov 
182aaef3170SIlya Dryomov 	if (chunk_cnt > 1) {
183aaef3170SIlya Dryomov 		ret = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
184aaef3170SIlya Dryomov 		if (ret)
185aaef3170SIlya Dryomov 			return ret;
186aaef3170SIlya Dryomov 	} else {
187aaef3170SIlya Dryomov 		WARN_ON(chunk_cnt != 1);
188aaef3170SIlya Dryomov 		sg_init_table(prealloc_sg, 1);
189aaef3170SIlya Dryomov 		sgt->sgl = prealloc_sg;
190aaef3170SIlya Dryomov 		sgt->nents = sgt->orig_nents = 1;
191aaef3170SIlya Dryomov 	}
192aaef3170SIlya Dryomov 
193aaef3170SIlya Dryomov 	for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
194aaef3170SIlya Dryomov 		struct page *page;
195aaef3170SIlya Dryomov 		unsigned int len = min(chunk_len - off, buf_len);
196aaef3170SIlya Dryomov 
197aaef3170SIlya Dryomov 		if (is_vmalloc)
198aaef3170SIlya Dryomov 			page = vmalloc_to_page(buf);
199aaef3170SIlya Dryomov 		else
200aaef3170SIlya Dryomov 			page = virt_to_page(buf);
201aaef3170SIlya Dryomov 
202aaef3170SIlya Dryomov 		sg_set_page(sg, page, len, off);
203aaef3170SIlya Dryomov 
204aaef3170SIlya Dryomov 		off = 0;
205aaef3170SIlya Dryomov 		buf += len;
206aaef3170SIlya Dryomov 		buf_len -= len;
207aaef3170SIlya Dryomov 	}
208aaef3170SIlya Dryomov 	WARN_ON(buf_len != 0);
209aaef3170SIlya Dryomov 
210aaef3170SIlya Dryomov 	return 0;
211aaef3170SIlya Dryomov }
212aaef3170SIlya Dryomov 
teardown_sgtable(struct sg_table * sgt)213aaef3170SIlya Dryomov static void teardown_sgtable(struct sg_table *sgt)
214aaef3170SIlya Dryomov {
215aaef3170SIlya Dryomov 	if (sgt->orig_nents > 1)
216aaef3170SIlya Dryomov 		sg_free_table(sgt);
217aaef3170SIlya Dryomov }
218aaef3170SIlya Dryomov 
ceph_aes_crypt(const struct ceph_crypto_key * key,bool encrypt,void * buf,int buf_len,int in_len,int * pout_len)219a45f795cSIlya Dryomov static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
220a45f795cSIlya Dryomov 			  void *buf, int buf_len, int in_len, int *pout_len)
221a45f795cSIlya Dryomov {
22269d6302bSKees Cook 	SYNC_SKCIPHER_REQUEST_ON_STACK(req, key->tfm);
223a45f795cSIlya Dryomov 	struct sg_table sgt;
224a45f795cSIlya Dryomov 	struct scatterlist prealloc_sg;
225124f930bSIlya Dryomov 	char iv[AES_BLOCK_SIZE] __aligned(8);
226a45f795cSIlya Dryomov 	int pad_byte = AES_BLOCK_SIZE - (in_len & (AES_BLOCK_SIZE - 1));
227a45f795cSIlya Dryomov 	int crypt_len = encrypt ? in_len + pad_byte : in_len;
228a45f795cSIlya Dryomov 	int ret;
229a45f795cSIlya Dryomov 
230a45f795cSIlya Dryomov 	WARN_ON(crypt_len > buf_len);
231a45f795cSIlya Dryomov 	if (encrypt)
232a45f795cSIlya Dryomov 		memset(buf + in_len, pad_byte, pad_byte);
233a45f795cSIlya Dryomov 	ret = setup_sgtable(&sgt, &prealloc_sg, buf, crypt_len);
234a45f795cSIlya Dryomov 	if (ret)
2357af3ea18SIlya Dryomov 		return ret;
236a45f795cSIlya Dryomov 
237a45f795cSIlya Dryomov 	memcpy(iv, aes_iv, AES_BLOCK_SIZE);
23869d6302bSKees Cook 	skcipher_request_set_sync_tfm(req, key->tfm);
239a45f795cSIlya Dryomov 	skcipher_request_set_callback(req, 0, NULL, NULL);
240a45f795cSIlya Dryomov 	skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv);
241a45f795cSIlya Dryomov 
242a45f795cSIlya Dryomov 	/*
243a45f795cSIlya Dryomov 	print_hex_dump(KERN_ERR, "key: ", DUMP_PREFIX_NONE, 16, 1,
244a45f795cSIlya Dryomov 		       key->key, key->len, 1);
245a45f795cSIlya Dryomov 	print_hex_dump(KERN_ERR, " in: ", DUMP_PREFIX_NONE, 16, 1,
246a45f795cSIlya Dryomov 		       buf, crypt_len, 1);
247a45f795cSIlya Dryomov 	*/
248a45f795cSIlya Dryomov 	if (encrypt)
249a45f795cSIlya Dryomov 		ret = crypto_skcipher_encrypt(req);
250a45f795cSIlya Dryomov 	else
251a45f795cSIlya Dryomov 		ret = crypto_skcipher_decrypt(req);
252a45f795cSIlya Dryomov 	skcipher_request_zero(req);
253a45f795cSIlya Dryomov 	if (ret) {
254a45f795cSIlya Dryomov 		pr_err("%s %scrypt failed: %d\n", __func__,
255a45f795cSIlya Dryomov 		       encrypt ? "en" : "de", ret);
256a45f795cSIlya Dryomov 		goto out_sgt;
257a45f795cSIlya Dryomov 	}
258a45f795cSIlya Dryomov 	/*
259a45f795cSIlya Dryomov 	print_hex_dump(KERN_ERR, "out: ", DUMP_PREFIX_NONE, 16, 1,
260a45f795cSIlya Dryomov 		       buf, crypt_len, 1);
261a45f795cSIlya Dryomov 	*/
262a45f795cSIlya Dryomov 
263a45f795cSIlya Dryomov 	if (encrypt) {
264a45f795cSIlya Dryomov 		*pout_len = crypt_len;
265a45f795cSIlya Dryomov 	} else {
266a45f795cSIlya Dryomov 		pad_byte = *(char *)(buf + in_len - 1);
267a45f795cSIlya Dryomov 		if (pad_byte > 0 && pad_byte <= AES_BLOCK_SIZE &&
268a45f795cSIlya Dryomov 		    in_len >= pad_byte) {
269a45f795cSIlya Dryomov 			*pout_len = in_len - pad_byte;
270a45f795cSIlya Dryomov 		} else {
271a45f795cSIlya Dryomov 			pr_err("%s got bad padding %d on in_len %d\n",
272a45f795cSIlya Dryomov 			       __func__, pad_byte, in_len);
273a45f795cSIlya Dryomov 			ret = -EPERM;
274a45f795cSIlya Dryomov 			goto out_sgt;
275a45f795cSIlya Dryomov 		}
276a45f795cSIlya Dryomov 	}
277a45f795cSIlya Dryomov 
278a45f795cSIlya Dryomov out_sgt:
279a45f795cSIlya Dryomov 	teardown_sgtable(&sgt);
280a45f795cSIlya Dryomov 	return ret;
281a45f795cSIlya Dryomov }
282a45f795cSIlya Dryomov 
ceph_crypt(const struct ceph_crypto_key * key,bool encrypt,void * buf,int buf_len,int in_len,int * pout_len)283a45f795cSIlya Dryomov int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
284a45f795cSIlya Dryomov 	       void *buf, int buf_len, int in_len, int *pout_len)
285a45f795cSIlya Dryomov {
286a45f795cSIlya Dryomov 	switch (key->type) {
287a45f795cSIlya Dryomov 	case CEPH_CRYPTO_NONE:
288a45f795cSIlya Dryomov 		*pout_len = in_len;
289a45f795cSIlya Dryomov 		return 0;
290a45f795cSIlya Dryomov 	case CEPH_CRYPTO_AES:
291a45f795cSIlya Dryomov 		return ceph_aes_crypt(key, encrypt, buf, buf_len, in_len,
292a45f795cSIlya Dryomov 				      pout_len);
293a45f795cSIlya Dryomov 	default:
294a45f795cSIlya Dryomov 		return -ENOTSUPP;
295a45f795cSIlya Dryomov 	}
296a45f795cSIlya Dryomov }
297a45f795cSIlya Dryomov 
ceph_key_preparse(struct key_preparsed_payload * prep)298efa64c09SDavid Howells static int ceph_key_preparse(struct key_preparsed_payload *prep)
2994b2a58abSTommi Virtanen {
3004b2a58abSTommi Virtanen 	struct ceph_crypto_key *ckey;
301cf7f601cSDavid Howells 	size_t datalen = prep->datalen;
3024b2a58abSTommi Virtanen 	int ret;
3034b2a58abSTommi Virtanen 	void *p;
3044b2a58abSTommi Virtanen 
3054b2a58abSTommi Virtanen 	ret = -EINVAL;
306cf7f601cSDavid Howells 	if (datalen <= 0 || datalen > 32767 || !prep->data)
3074b2a58abSTommi Virtanen 		goto err;
3084b2a58abSTommi Virtanen 
3094b2a58abSTommi Virtanen 	ret = -ENOMEM;
3104b2a58abSTommi Virtanen 	ckey = kmalloc(sizeof(*ckey), GFP_KERNEL);
3114b2a58abSTommi Virtanen 	if (!ckey)
3124b2a58abSTommi Virtanen 		goto err;
3134b2a58abSTommi Virtanen 
3144b2a58abSTommi Virtanen 	/* TODO ceph_crypto_key_decode should really take const input */
315cf7f601cSDavid Howells 	p = (void *)prep->data;
316cf7f601cSDavid Howells 	ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
3174b2a58abSTommi Virtanen 	if (ret < 0)
3184b2a58abSTommi Virtanen 		goto err_ckey;
3194b2a58abSTommi Virtanen 
320146aa8b1SDavid Howells 	prep->payload.data[0] = ckey;
321efa64c09SDavid Howells 	prep->quotalen = datalen;
3224b2a58abSTommi Virtanen 	return 0;
3234b2a58abSTommi Virtanen 
3244b2a58abSTommi Virtanen err_ckey:
3254b2a58abSTommi Virtanen 	kfree(ckey);
3264b2a58abSTommi Virtanen err:
3274b2a58abSTommi Virtanen 	return ret;
3284b2a58abSTommi Virtanen }
3294b2a58abSTommi Virtanen 
ceph_key_free_preparse(struct key_preparsed_payload * prep)330efa64c09SDavid Howells static void ceph_key_free_preparse(struct key_preparsed_payload *prep)
331efa64c09SDavid Howells {
332146aa8b1SDavid Howells 	struct ceph_crypto_key *ckey = prep->payload.data[0];
333efa64c09SDavid Howells 	ceph_crypto_key_destroy(ckey);
334efa64c09SDavid Howells 	kfree(ckey);
335efa64c09SDavid Howells }
336efa64c09SDavid Howells 
ceph_key_destroy(struct key * key)337efa64c09SDavid Howells static void ceph_key_destroy(struct key *key)
338efa64c09SDavid Howells {
339146aa8b1SDavid Howells 	struct ceph_crypto_key *ckey = key->payload.data[0];
3404b2a58abSTommi Virtanen 
3414b2a58abSTommi Virtanen 	ceph_crypto_key_destroy(ckey);
342f0666b1aSSylvain Munaut 	kfree(ckey);
3434b2a58abSTommi Virtanen }
3444b2a58abSTommi Virtanen 
3454b2a58abSTommi Virtanen struct key_type key_type_ceph = {
3464b2a58abSTommi Virtanen 	.name		= "ceph",
347efa64c09SDavid Howells 	.preparse	= ceph_key_preparse,
348efa64c09SDavid Howells 	.free_preparse	= ceph_key_free_preparse,
349efa64c09SDavid Howells 	.instantiate	= generic_key_instantiate,
3504b2a58abSTommi Virtanen 	.destroy	= ceph_key_destroy,
3514b2a58abSTommi Virtanen };
3524b2a58abSTommi Virtanen 
ceph_crypto_init(void)35357a35dfbSChengguang Xu int __init ceph_crypto_init(void)
35457a35dfbSChengguang Xu {
3554b2a58abSTommi Virtanen 	return register_key_type(&key_type_ceph);
3564b2a58abSTommi Virtanen }
3574b2a58abSTommi Virtanen 
ceph_crypto_shutdown(void)35857a35dfbSChengguang Xu void ceph_crypto_shutdown(void)
35957a35dfbSChengguang Xu {
3604b2a58abSTommi Virtanen 	unregister_key_type(&key_type_ceph);
3614b2a58abSTommi Virtanen }
362