1d1775a17SDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later
2d1775a17SDavid Howells /*
3d1775a17SDavid Howells * AEAD wrapper for Kerberos 5 RFC3961 simplified profile.
4d1775a17SDavid Howells *
5d1775a17SDavid Howells * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
6d1775a17SDavid Howells * Written by David Howells (dhowells@redhat.com)
7d1775a17SDavid Howells *
8d1775a17SDavid Howells * Derived from authenc:
9d1775a17SDavid Howells * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
10d1775a17SDavid Howells */
11d1775a17SDavid Howells
12d1775a17SDavid Howells #include <crypto/internal/aead.h>
13d1775a17SDavid Howells #include <crypto/internal/hash.h>
14d1775a17SDavid Howells #include <crypto/internal/skcipher.h>
15d1775a17SDavid Howells #include <crypto/authenc.h>
16d1775a17SDavid Howells #include <crypto/scatterwalk.h>
17d1775a17SDavid Howells #include <linux/err.h>
18d1775a17SDavid Howells #include <linux/init.h>
19d1775a17SDavid Howells #include <linux/kernel.h>
20d1775a17SDavid Howells #include <linux/module.h>
21d1775a17SDavid Howells #include <linux/rtnetlink.h>
22d1775a17SDavid Howells #include <linux/slab.h>
23d1775a17SDavid Howells #include <linux/spinlock.h>
24d1775a17SDavid Howells
25d1775a17SDavid Howells struct krb5enc_instance_ctx {
26d1775a17SDavid Howells struct crypto_ahash_spawn auth;
27d1775a17SDavid Howells struct crypto_skcipher_spawn enc;
28d1775a17SDavid Howells unsigned int reqoff;
29d1775a17SDavid Howells };
30d1775a17SDavid Howells
31d1775a17SDavid Howells struct krb5enc_ctx {
32d1775a17SDavid Howells struct crypto_ahash *auth;
33d1775a17SDavid Howells struct crypto_skcipher *enc;
34d1775a17SDavid Howells };
35d1775a17SDavid Howells
36d1775a17SDavid Howells struct krb5enc_request_ctx {
37d1775a17SDavid Howells struct scatterlist src[2];
38d1775a17SDavid Howells struct scatterlist dst[2];
39d1775a17SDavid Howells char tail[];
40d1775a17SDavid Howells };
41d1775a17SDavid Howells
42d1775a17SDavid Howells /**
43d1775a17SDavid Howells * crypto_krb5enc_extractkeys - Extract Ke and Ki keys from the key blob.
44d1775a17SDavid Howells * @keys: Where to put the key sizes and pointers
45d1775a17SDavid Howells * @key: Encoded key material
46d1775a17SDavid Howells * @keylen: Amount of key material
47d1775a17SDavid Howells *
48d1775a17SDavid Howells * Decode the key blob we're given. It starts with an rtattr that indicates
49d1775a17SDavid Howells * the format and the length. Format CRYPTO_AUTHENC_KEYA_PARAM is:
50d1775a17SDavid Howells *
51d1775a17SDavid Howells * rtattr || __be32 enckeylen || authkey || enckey
52d1775a17SDavid Howells *
53d1775a17SDavid Howells * Note that the rtattr is in cpu-endian form, unlike enckeylen. This must be
54d1775a17SDavid Howells * handled correctly in static testmgr data.
55d1775a17SDavid Howells */
crypto_krb5enc_extractkeys(struct crypto_authenc_keys * keys,const u8 * key,unsigned int keylen)56d1775a17SDavid Howells int crypto_krb5enc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
57d1775a17SDavid Howells unsigned int keylen)
58d1775a17SDavid Howells {
59d1775a17SDavid Howells struct rtattr *rta = (struct rtattr *)key;
60d1775a17SDavid Howells struct crypto_authenc_key_param *param;
61d1775a17SDavid Howells
62d1775a17SDavid Howells if (!RTA_OK(rta, keylen))
63d1775a17SDavid Howells return -EINVAL;
64d1775a17SDavid Howells if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
65d1775a17SDavid Howells return -EINVAL;
66d1775a17SDavid Howells
67d1775a17SDavid Howells /*
68d1775a17SDavid Howells * RTA_OK() didn't align the rtattr's payload when validating that it
69d1775a17SDavid Howells * fits in the buffer. Yet, the keys should start on the next 4-byte
70d1775a17SDavid Howells * aligned boundary. To avoid confusion, require that the rtattr
71d1775a17SDavid Howells * payload be exactly the param struct, which has a 4-byte aligned size.
72d1775a17SDavid Howells */
73d1775a17SDavid Howells if (RTA_PAYLOAD(rta) != sizeof(*param))
74d1775a17SDavid Howells return -EINVAL;
75d1775a17SDavid Howells BUILD_BUG_ON(sizeof(*param) % RTA_ALIGNTO);
76d1775a17SDavid Howells
77d1775a17SDavid Howells param = RTA_DATA(rta);
78d1775a17SDavid Howells keys->enckeylen = be32_to_cpu(param->enckeylen);
79d1775a17SDavid Howells
80d1775a17SDavid Howells key += rta->rta_len;
81d1775a17SDavid Howells keylen -= rta->rta_len;
82d1775a17SDavid Howells
83d1775a17SDavid Howells if (keylen < keys->enckeylen)
84d1775a17SDavid Howells return -EINVAL;
85d1775a17SDavid Howells
86d1775a17SDavid Howells keys->authkeylen = keylen - keys->enckeylen;
87d1775a17SDavid Howells keys->authkey = key;
88d1775a17SDavid Howells keys->enckey = key + keys->authkeylen;
89d1775a17SDavid Howells return 0;
90d1775a17SDavid Howells }
91d1775a17SDavid Howells EXPORT_SYMBOL(crypto_krb5enc_extractkeys);
92d1775a17SDavid Howells
krb5enc_setkey(struct crypto_aead * krb5enc,const u8 * key,unsigned int keylen)93d1775a17SDavid Howells static int krb5enc_setkey(struct crypto_aead *krb5enc, const u8 *key,
94d1775a17SDavid Howells unsigned int keylen)
95d1775a17SDavid Howells {
96d1775a17SDavid Howells struct crypto_authenc_keys keys;
97d1775a17SDavid Howells struct krb5enc_ctx *ctx = crypto_aead_ctx(krb5enc);
98d1775a17SDavid Howells struct crypto_skcipher *enc = ctx->enc;
99d1775a17SDavid Howells struct crypto_ahash *auth = ctx->auth;
100d1775a17SDavid Howells unsigned int flags = crypto_aead_get_flags(krb5enc);
101d1775a17SDavid Howells int err = -EINVAL;
102d1775a17SDavid Howells
103d1775a17SDavid Howells if (crypto_krb5enc_extractkeys(&keys, key, keylen) != 0)
104d1775a17SDavid Howells goto out;
105d1775a17SDavid Howells
106d1775a17SDavid Howells crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
107d1775a17SDavid Howells crypto_ahash_set_flags(auth, flags & CRYPTO_TFM_REQ_MASK);
108d1775a17SDavid Howells err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
109d1775a17SDavid Howells if (err)
110d1775a17SDavid Howells goto out;
111d1775a17SDavid Howells
112d1775a17SDavid Howells crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
113d1775a17SDavid Howells crypto_skcipher_set_flags(enc, flags & CRYPTO_TFM_REQ_MASK);
114d1775a17SDavid Howells err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
115d1775a17SDavid Howells out:
116d1775a17SDavid Howells memzero_explicit(&keys, sizeof(keys));
117d1775a17SDavid Howells return err;
118d1775a17SDavid Howells }
119d1775a17SDavid Howells
krb5enc_encrypt_done(void * data,int err)120d1775a17SDavid Howells static void krb5enc_encrypt_done(void *data, int err)
121d1775a17SDavid Howells {
122d1775a17SDavid Howells struct aead_request *req = data;
123d1775a17SDavid Howells
124*3bfbf5f0SDudu Lu aead_request_complete(req, err);
125d1775a17SDavid Howells }
126d1775a17SDavid Howells
127d1775a17SDavid Howells /*
128d1775a17SDavid Howells * Start the encryption of the plaintext. We skip over the associated data as
129d1775a17SDavid Howells * that only gets included in the hash.
130d1775a17SDavid Howells */
krb5enc_dispatch_encrypt(struct aead_request * req,unsigned int flags)131d1775a17SDavid Howells static int krb5enc_dispatch_encrypt(struct aead_request *req,
132d1775a17SDavid Howells unsigned int flags)
133d1775a17SDavid Howells {
134d1775a17SDavid Howells struct crypto_aead *krb5enc = crypto_aead_reqtfm(req);
135d1775a17SDavid Howells struct aead_instance *inst = aead_alg_instance(krb5enc);
136d1775a17SDavid Howells struct krb5enc_ctx *ctx = crypto_aead_ctx(krb5enc);
137d1775a17SDavid Howells struct krb5enc_instance_ctx *ictx = aead_instance_ctx(inst);
138d1775a17SDavid Howells struct krb5enc_request_ctx *areq_ctx = aead_request_ctx(req);
139d1775a17SDavid Howells struct crypto_skcipher *enc = ctx->enc;
140d1775a17SDavid Howells struct skcipher_request *skreq = (void *)(areq_ctx->tail +
141d1775a17SDavid Howells ictx->reqoff);
142d1775a17SDavid Howells struct scatterlist *src, *dst;
143d1775a17SDavid Howells
144d1775a17SDavid Howells src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
145d1775a17SDavid Howells if (req->src == req->dst)
146d1775a17SDavid Howells dst = src;
147d1775a17SDavid Howells else
148d1775a17SDavid Howells dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
149d1775a17SDavid Howells
150d1775a17SDavid Howells skcipher_request_set_tfm(skreq, enc);
1512ef3bac1SWesley Atwell skcipher_request_set_callback(skreq, flags,
152d1775a17SDavid Howells krb5enc_encrypt_done, req);
153d1775a17SDavid Howells skcipher_request_set_crypt(skreq, src, dst, req->cryptlen, req->iv);
154d1775a17SDavid Howells
155d1775a17SDavid Howells return crypto_skcipher_encrypt(skreq);
156d1775a17SDavid Howells }
157d1775a17SDavid Howells
158d1775a17SDavid Howells /*
159d1775a17SDavid Howells * Insert the hash into the checksum field in the destination buffer directly
160d1775a17SDavid Howells * after the encrypted region.
161d1775a17SDavid Howells */
krb5enc_insert_checksum(struct aead_request * req,u8 * hash)162d1775a17SDavid Howells static void krb5enc_insert_checksum(struct aead_request *req, u8 *hash)
163d1775a17SDavid Howells {
164d1775a17SDavid Howells struct crypto_aead *krb5enc = crypto_aead_reqtfm(req);
165d1775a17SDavid Howells
166d1775a17SDavid Howells scatterwalk_map_and_copy(hash, req->dst,
167d1775a17SDavid Howells req->assoclen + req->cryptlen,
168d1775a17SDavid Howells crypto_aead_authsize(krb5enc), 1);
169d1775a17SDavid Howells }
170d1775a17SDavid Howells
171d1775a17SDavid Howells /*
172d1775a17SDavid Howells * Upon completion of an asynchronous digest, transfer the hash to the checksum
173d1775a17SDavid Howells * field.
174d1775a17SDavid Howells */
krb5enc_encrypt_ahash_done(void * data,int err)175d1775a17SDavid Howells static void krb5enc_encrypt_ahash_done(void *data, int err)
176d1775a17SDavid Howells {
177d1775a17SDavid Howells struct aead_request *req = data;
178d1775a17SDavid Howells struct crypto_aead *krb5enc = crypto_aead_reqtfm(req);
179d1775a17SDavid Howells struct aead_instance *inst = aead_alg_instance(krb5enc);
180d1775a17SDavid Howells struct krb5enc_instance_ctx *ictx = aead_instance_ctx(inst);
181d1775a17SDavid Howells struct krb5enc_request_ctx *areq_ctx = aead_request_ctx(req);
182d1775a17SDavid Howells struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
183d1775a17SDavid Howells
184d1775a17SDavid Howells if (err)
185*3bfbf5f0SDudu Lu goto out;
186d1775a17SDavid Howells
187d1775a17SDavid Howells krb5enc_insert_checksum(req, ahreq->result);
188d1775a17SDavid Howells
189*3bfbf5f0SDudu Lu err = krb5enc_dispatch_encrypt(req, 0);
190*3bfbf5f0SDudu Lu if (err == -EINPROGRESS)
191*3bfbf5f0SDudu Lu return;
192*3bfbf5f0SDudu Lu
193*3bfbf5f0SDudu Lu out:
194d1775a17SDavid Howells aead_request_complete(req, err);
195d1775a17SDavid Howells }
196d1775a17SDavid Howells
197d1775a17SDavid Howells /*
198d1775a17SDavid Howells * Start the digest of the plaintext for encryption. In theory, this could be
199d1775a17SDavid Howells * run in parallel with the encryption, provided the src and dst buffers don't
200d1775a17SDavid Howells * overlap.
201d1775a17SDavid Howells */
krb5enc_dispatch_encrypt_hash(struct aead_request * req)202d1775a17SDavid Howells static int krb5enc_dispatch_encrypt_hash(struct aead_request *req)
203d1775a17SDavid Howells {
204d1775a17SDavid Howells struct crypto_aead *krb5enc = crypto_aead_reqtfm(req);
205d1775a17SDavid Howells struct aead_instance *inst = aead_alg_instance(krb5enc);
206d1775a17SDavid Howells struct krb5enc_ctx *ctx = crypto_aead_ctx(krb5enc);
207d1775a17SDavid Howells struct krb5enc_instance_ctx *ictx = aead_instance_ctx(inst);
208d1775a17SDavid Howells struct crypto_ahash *auth = ctx->auth;
209d1775a17SDavid Howells struct krb5enc_request_ctx *areq_ctx = aead_request_ctx(req);
210d1775a17SDavid Howells struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
211d1775a17SDavid Howells u8 *hash = areq_ctx->tail;
212d1775a17SDavid Howells int err;
213d1775a17SDavid Howells
214d1775a17SDavid Howells ahash_request_set_callback(ahreq, aead_request_flags(req),
215d1775a17SDavid Howells krb5enc_encrypt_ahash_done, req);
216d1775a17SDavid Howells ahash_request_set_tfm(ahreq, auth);
217d1775a17SDavid Howells ahash_request_set_crypt(ahreq, req->src, hash, req->assoclen + req->cryptlen);
218d1775a17SDavid Howells
219d1775a17SDavid Howells err = crypto_ahash_digest(ahreq);
220d1775a17SDavid Howells if (err)
221d1775a17SDavid Howells return err;
222d1775a17SDavid Howells
223d1775a17SDavid Howells krb5enc_insert_checksum(req, hash);
224d1775a17SDavid Howells return 0;
225d1775a17SDavid Howells }
226d1775a17SDavid Howells
227d1775a17SDavid Howells /*
228d1775a17SDavid Howells * Process an encryption operation. We can perform the cipher and the hash in
229d1775a17SDavid Howells * parallel, provided the src and dst buffers are separate.
230d1775a17SDavid Howells */
krb5enc_encrypt(struct aead_request * req)231d1775a17SDavid Howells static int krb5enc_encrypt(struct aead_request *req)
232d1775a17SDavid Howells {
233d1775a17SDavid Howells int err;
234d1775a17SDavid Howells
235d1775a17SDavid Howells err = krb5enc_dispatch_encrypt_hash(req);
236d1775a17SDavid Howells if (err < 0)
237d1775a17SDavid Howells return err;
238d1775a17SDavid Howells
239d1775a17SDavid Howells return krb5enc_dispatch_encrypt(req, aead_request_flags(req));
240d1775a17SDavid Howells }
241d1775a17SDavid Howells
krb5enc_verify_hash(struct aead_request * req)242d1775a17SDavid Howells static int krb5enc_verify_hash(struct aead_request *req)
243d1775a17SDavid Howells {
244d1775a17SDavid Howells struct crypto_aead *krb5enc = crypto_aead_reqtfm(req);
245d1775a17SDavid Howells struct aead_instance *inst = aead_alg_instance(krb5enc);
246d1775a17SDavid Howells struct krb5enc_instance_ctx *ictx = aead_instance_ctx(inst);
247d1775a17SDavid Howells struct krb5enc_request_ctx *areq_ctx = aead_request_ctx(req);
248d1775a17SDavid Howells struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
249d1775a17SDavid Howells unsigned int authsize = crypto_aead_authsize(krb5enc);
250d1775a17SDavid Howells u8 *calc_hash = areq_ctx->tail;
251d1775a17SDavid Howells u8 *msg_hash = areq_ctx->tail + authsize;
252d1775a17SDavid Howells
253d1775a17SDavid Howells scatterwalk_map_and_copy(msg_hash, req->src, ahreq->nbytes, authsize, 0);
254d1775a17SDavid Howells
255d1775a17SDavid Howells if (crypto_memneq(msg_hash, calc_hash, authsize))
256d1775a17SDavid Howells return -EBADMSG;
257d1775a17SDavid Howells return 0;
258d1775a17SDavid Howells }
259d1775a17SDavid Howells
krb5enc_decrypt_hash_done(void * data,int err)260d1775a17SDavid Howells static void krb5enc_decrypt_hash_done(void *data, int err)
261d1775a17SDavid Howells {
262d1775a17SDavid Howells struct aead_request *req = data;
263d1775a17SDavid Howells
264*3bfbf5f0SDudu Lu if (!err)
265d1775a17SDavid Howells err = krb5enc_verify_hash(req);
266*3bfbf5f0SDudu Lu aead_request_complete(req, err);
267d1775a17SDavid Howells }
268d1775a17SDavid Howells
269d1775a17SDavid Howells /*
270d1775a17SDavid Howells * Dispatch the hashing of the plaintext after we've done the decryption.
271d1775a17SDavid Howells */
krb5enc_dispatch_decrypt_hash(struct aead_request * req,unsigned int flags)272*3bfbf5f0SDudu Lu static int krb5enc_dispatch_decrypt_hash(struct aead_request *req,
273*3bfbf5f0SDudu Lu unsigned int flags)
274d1775a17SDavid Howells {
275d1775a17SDavid Howells struct crypto_aead *krb5enc = crypto_aead_reqtfm(req);
276d1775a17SDavid Howells struct aead_instance *inst = aead_alg_instance(krb5enc);
277d1775a17SDavid Howells struct krb5enc_ctx *ctx = crypto_aead_ctx(krb5enc);
278d1775a17SDavid Howells struct krb5enc_instance_ctx *ictx = aead_instance_ctx(inst);
279d1775a17SDavid Howells struct krb5enc_request_ctx *areq_ctx = aead_request_ctx(req);
280d1775a17SDavid Howells struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
281d1775a17SDavid Howells struct crypto_ahash *auth = ctx->auth;
282d1775a17SDavid Howells unsigned int authsize = crypto_aead_authsize(krb5enc);
283d1775a17SDavid Howells u8 *hash = areq_ctx->tail;
284d1775a17SDavid Howells int err;
285d1775a17SDavid Howells
286d1775a17SDavid Howells ahash_request_set_tfm(ahreq, auth);
287d1775a17SDavid Howells ahash_request_set_crypt(ahreq, req->dst, hash,
288d1775a17SDavid Howells req->assoclen + req->cryptlen - authsize);
289*3bfbf5f0SDudu Lu ahash_request_set_callback(ahreq, flags,
290d1775a17SDavid Howells krb5enc_decrypt_hash_done, req);
291d1775a17SDavid Howells
292d1775a17SDavid Howells err = crypto_ahash_digest(ahreq);
293d1775a17SDavid Howells if (err < 0)
294d1775a17SDavid Howells return err;
295d1775a17SDavid Howells
296d1775a17SDavid Howells return krb5enc_verify_hash(req);
297d1775a17SDavid Howells }
298d1775a17SDavid Howells
krb5enc_decrypt_done(void * data,int err)299*3bfbf5f0SDudu Lu static void krb5enc_decrypt_done(void *data, int err)
300*3bfbf5f0SDudu Lu {
301*3bfbf5f0SDudu Lu struct aead_request *req = data;
302*3bfbf5f0SDudu Lu
303*3bfbf5f0SDudu Lu if (err)
304*3bfbf5f0SDudu Lu goto out;
305*3bfbf5f0SDudu Lu
306*3bfbf5f0SDudu Lu err = krb5enc_dispatch_decrypt_hash(req, 0);
307*3bfbf5f0SDudu Lu if (err == -EINPROGRESS)
308*3bfbf5f0SDudu Lu return;
309*3bfbf5f0SDudu Lu
310*3bfbf5f0SDudu Lu out:
311*3bfbf5f0SDudu Lu aead_request_complete(req, err);
312*3bfbf5f0SDudu Lu }
313*3bfbf5f0SDudu Lu
314d1775a17SDavid Howells /*
315d1775a17SDavid Howells * Dispatch the decryption of the ciphertext.
316d1775a17SDavid Howells */
krb5enc_dispatch_decrypt(struct aead_request * req)317d1775a17SDavid Howells static int krb5enc_dispatch_decrypt(struct aead_request *req)
318d1775a17SDavid Howells {
319d1775a17SDavid Howells struct crypto_aead *krb5enc = crypto_aead_reqtfm(req);
320d1775a17SDavid Howells struct aead_instance *inst = aead_alg_instance(krb5enc);
321d1775a17SDavid Howells struct krb5enc_ctx *ctx = crypto_aead_ctx(krb5enc);
322d1775a17SDavid Howells struct krb5enc_instance_ctx *ictx = aead_instance_ctx(inst);
323d1775a17SDavid Howells struct krb5enc_request_ctx *areq_ctx = aead_request_ctx(req);
324d1775a17SDavid Howells struct skcipher_request *skreq = (void *)(areq_ctx->tail +
325d1775a17SDavid Howells ictx->reqoff);
326d1775a17SDavid Howells unsigned int authsize = crypto_aead_authsize(krb5enc);
327d1775a17SDavid Howells struct scatterlist *src, *dst;
328d1775a17SDavid Howells
329d1775a17SDavid Howells src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
330d1775a17SDavid Howells dst = src;
331d1775a17SDavid Howells
332d1775a17SDavid Howells if (req->src != req->dst)
333d1775a17SDavid Howells dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
334d1775a17SDavid Howells
335d1775a17SDavid Howells skcipher_request_set_tfm(skreq, ctx->enc);
336d1775a17SDavid Howells skcipher_request_set_callback(skreq, aead_request_flags(req),
337*3bfbf5f0SDudu Lu krb5enc_decrypt_done, req);
338d1775a17SDavid Howells skcipher_request_set_crypt(skreq, src, dst,
339d1775a17SDavid Howells req->cryptlen - authsize, req->iv);
340d1775a17SDavid Howells
341d1775a17SDavid Howells return crypto_skcipher_decrypt(skreq);
342d1775a17SDavid Howells }
343d1775a17SDavid Howells
krb5enc_decrypt(struct aead_request * req)344d1775a17SDavid Howells static int krb5enc_decrypt(struct aead_request *req)
345d1775a17SDavid Howells {
346d1775a17SDavid Howells int err;
347d1775a17SDavid Howells
348d1775a17SDavid Howells err = krb5enc_dispatch_decrypt(req);
349d1775a17SDavid Howells if (err < 0)
350d1775a17SDavid Howells return err;
351d1775a17SDavid Howells
352*3bfbf5f0SDudu Lu return krb5enc_dispatch_decrypt_hash(req, aead_request_flags(req));
353d1775a17SDavid Howells }
354d1775a17SDavid Howells
krb5enc_init_tfm(struct crypto_aead * tfm)355d1775a17SDavid Howells static int krb5enc_init_tfm(struct crypto_aead *tfm)
356d1775a17SDavid Howells {
357d1775a17SDavid Howells struct aead_instance *inst = aead_alg_instance(tfm);
358d1775a17SDavid Howells struct krb5enc_instance_ctx *ictx = aead_instance_ctx(inst);
359d1775a17SDavid Howells struct krb5enc_ctx *ctx = crypto_aead_ctx(tfm);
360d1775a17SDavid Howells struct crypto_ahash *auth;
361d1775a17SDavid Howells struct crypto_skcipher *enc;
362d1775a17SDavid Howells int err;
363d1775a17SDavid Howells
364d1775a17SDavid Howells auth = crypto_spawn_ahash(&ictx->auth);
365d1775a17SDavid Howells if (IS_ERR(auth))
366d1775a17SDavid Howells return PTR_ERR(auth);
367d1775a17SDavid Howells
368d1775a17SDavid Howells enc = crypto_spawn_skcipher(&ictx->enc);
369d1775a17SDavid Howells err = PTR_ERR(enc);
370d1775a17SDavid Howells if (IS_ERR(enc))
371d1775a17SDavid Howells goto err_free_ahash;
372d1775a17SDavid Howells
373d1775a17SDavid Howells ctx->auth = auth;
374d1775a17SDavid Howells ctx->enc = enc;
375d1775a17SDavid Howells
376d1775a17SDavid Howells crypto_aead_set_reqsize(
377d1775a17SDavid Howells tfm,
378d1775a17SDavid Howells sizeof(struct krb5enc_request_ctx) +
379d1775a17SDavid Howells ictx->reqoff + /* Space for two checksums */
380d1775a17SDavid Howells umax(sizeof(struct ahash_request) + crypto_ahash_reqsize(auth),
381d1775a17SDavid Howells sizeof(struct skcipher_request) + crypto_skcipher_reqsize(enc)));
382d1775a17SDavid Howells
383d1775a17SDavid Howells return 0;
384d1775a17SDavid Howells
385d1775a17SDavid Howells err_free_ahash:
386d1775a17SDavid Howells crypto_free_ahash(auth);
387d1775a17SDavid Howells return err;
388d1775a17SDavid Howells }
389d1775a17SDavid Howells
krb5enc_exit_tfm(struct crypto_aead * tfm)390d1775a17SDavid Howells static void krb5enc_exit_tfm(struct crypto_aead *tfm)
391d1775a17SDavid Howells {
392d1775a17SDavid Howells struct krb5enc_ctx *ctx = crypto_aead_ctx(tfm);
393d1775a17SDavid Howells
394d1775a17SDavid Howells crypto_free_ahash(ctx->auth);
395d1775a17SDavid Howells crypto_free_skcipher(ctx->enc);
396d1775a17SDavid Howells }
397d1775a17SDavid Howells
krb5enc_free(struct aead_instance * inst)398d1775a17SDavid Howells static void krb5enc_free(struct aead_instance *inst)
399d1775a17SDavid Howells {
400d1775a17SDavid Howells struct krb5enc_instance_ctx *ctx = aead_instance_ctx(inst);
401d1775a17SDavid Howells
402d1775a17SDavid Howells crypto_drop_skcipher(&ctx->enc);
403d1775a17SDavid Howells crypto_drop_ahash(&ctx->auth);
404d1775a17SDavid Howells kfree(inst);
405d1775a17SDavid Howells }
406d1775a17SDavid Howells
407d1775a17SDavid Howells /*
408d1775a17SDavid Howells * Create an instance of a template for a specific hash and cipher pair.
409d1775a17SDavid Howells */
krb5enc_create(struct crypto_template * tmpl,struct rtattr ** tb)410d1775a17SDavid Howells static int krb5enc_create(struct crypto_template *tmpl, struct rtattr **tb)
411d1775a17SDavid Howells {
412d1775a17SDavid Howells struct krb5enc_instance_ctx *ictx;
413d1775a17SDavid Howells struct skcipher_alg_common *enc;
414d1775a17SDavid Howells struct hash_alg_common *auth;
415d1775a17SDavid Howells struct aead_instance *inst;
416d1775a17SDavid Howells struct crypto_alg *auth_base;
417d1775a17SDavid Howells u32 mask;
418d1775a17SDavid Howells int err;
419d1775a17SDavid Howells
420d1775a17SDavid Howells err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
421d1775a17SDavid Howells if (err) {
422d1775a17SDavid Howells pr_err("attr_type failed\n");
423d1775a17SDavid Howells return err;
424d1775a17SDavid Howells }
425d1775a17SDavid Howells
426d1775a17SDavid Howells inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
427d1775a17SDavid Howells if (!inst)
428d1775a17SDavid Howells return -ENOMEM;
429d1775a17SDavid Howells ictx = aead_instance_ctx(inst);
430d1775a17SDavid Howells
431d1775a17SDavid Howells err = crypto_grab_ahash(&ictx->auth, aead_crypto_instance(inst),
432d1775a17SDavid Howells crypto_attr_alg_name(tb[1]), 0, mask);
433d1775a17SDavid Howells if (err) {
434d1775a17SDavid Howells pr_err("grab ahash failed\n");
435d1775a17SDavid Howells goto err_free_inst;
436d1775a17SDavid Howells }
437d1775a17SDavid Howells auth = crypto_spawn_ahash_alg(&ictx->auth);
438d1775a17SDavid Howells auth_base = &auth->base;
439d1775a17SDavid Howells
440d1775a17SDavid Howells err = crypto_grab_skcipher(&ictx->enc, aead_crypto_instance(inst),
441d1775a17SDavid Howells crypto_attr_alg_name(tb[2]), 0, mask);
442d1775a17SDavid Howells if (err) {
443d1775a17SDavid Howells pr_err("grab skcipher failed\n");
444d1775a17SDavid Howells goto err_free_inst;
445d1775a17SDavid Howells }
446d1775a17SDavid Howells enc = crypto_spawn_skcipher_alg_common(&ictx->enc);
447d1775a17SDavid Howells
448d1775a17SDavid Howells ictx->reqoff = 2 * auth->digestsize;
449d1775a17SDavid Howells
450d1775a17SDavid Howells err = -ENAMETOOLONG;
451d1775a17SDavid Howells if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
452d1775a17SDavid Howells "krb5enc(%s,%s)", auth_base->cra_name,
453d1775a17SDavid Howells enc->base.cra_name) >=
454d1775a17SDavid Howells CRYPTO_MAX_ALG_NAME)
455d1775a17SDavid Howells goto err_free_inst;
456d1775a17SDavid Howells
457d1775a17SDavid Howells if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
458d1775a17SDavid Howells "krb5enc(%s,%s)", auth_base->cra_driver_name,
459d1775a17SDavid Howells enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
460d1775a17SDavid Howells goto err_free_inst;
461d1775a17SDavid Howells
462d1775a17SDavid Howells inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
463d1775a17SDavid Howells auth_base->cra_priority;
464d1775a17SDavid Howells inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
465d1775a17SDavid Howells inst->alg.base.cra_alignmask = enc->base.cra_alignmask;
466d1775a17SDavid Howells inst->alg.base.cra_ctxsize = sizeof(struct krb5enc_ctx);
467d1775a17SDavid Howells
468d1775a17SDavid Howells inst->alg.ivsize = enc->ivsize;
469d1775a17SDavid Howells inst->alg.chunksize = enc->chunksize;
470d1775a17SDavid Howells inst->alg.maxauthsize = auth->digestsize;
471d1775a17SDavid Howells
472d1775a17SDavid Howells inst->alg.init = krb5enc_init_tfm;
473d1775a17SDavid Howells inst->alg.exit = krb5enc_exit_tfm;
474d1775a17SDavid Howells
475d1775a17SDavid Howells inst->alg.setkey = krb5enc_setkey;
476d1775a17SDavid Howells inst->alg.encrypt = krb5enc_encrypt;
477d1775a17SDavid Howells inst->alg.decrypt = krb5enc_decrypt;
478d1775a17SDavid Howells
479d1775a17SDavid Howells inst->free = krb5enc_free;
480d1775a17SDavid Howells
481d1775a17SDavid Howells err = aead_register_instance(tmpl, inst);
482d1775a17SDavid Howells if (err) {
483d1775a17SDavid Howells pr_err("ref failed\n");
484d1775a17SDavid Howells goto err_free_inst;
485d1775a17SDavid Howells }
486d1775a17SDavid Howells
487d1775a17SDavid Howells return 0;
488d1775a17SDavid Howells
489d1775a17SDavid Howells err_free_inst:
490d1775a17SDavid Howells krb5enc_free(inst);
491d1775a17SDavid Howells return err;
492d1775a17SDavid Howells }
493d1775a17SDavid Howells
494d1775a17SDavid Howells static struct crypto_template crypto_krb5enc_tmpl = {
495d1775a17SDavid Howells .name = "krb5enc",
496d1775a17SDavid Howells .create = krb5enc_create,
497d1775a17SDavid Howells .module = THIS_MODULE,
498d1775a17SDavid Howells };
499d1775a17SDavid Howells
crypto_krb5enc_module_init(void)500d1775a17SDavid Howells static int __init crypto_krb5enc_module_init(void)
501d1775a17SDavid Howells {
502d1775a17SDavid Howells return crypto_register_template(&crypto_krb5enc_tmpl);
503d1775a17SDavid Howells }
504d1775a17SDavid Howells
crypto_krb5enc_module_exit(void)505d1775a17SDavid Howells static void __exit crypto_krb5enc_module_exit(void)
506d1775a17SDavid Howells {
507d1775a17SDavid Howells crypto_unregister_template(&crypto_krb5enc_tmpl);
508d1775a17SDavid Howells }
509d1775a17SDavid Howells
510ef93f156SHerbert Xu module_init(crypto_krb5enc_module_init);
511d1775a17SDavid Howells module_exit(crypto_krb5enc_module_exit);
512d1775a17SDavid Howells
513d1775a17SDavid Howells MODULE_LICENSE("GPL");
514d1775a17SDavid Howells MODULE_DESCRIPTION("Simple AEAD wrapper for Kerberos 5 RFC3961");
515d1775a17SDavid Howells MODULE_ALIAS_CRYPTO("krb5enc");
516