1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Authenc: Simple AEAD wrapper for IPsec
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <crypto/internal/aead.h>
9*4882a593Smuzhiyun #include <crypto/internal/hash.h>
10*4882a593Smuzhiyun #include <crypto/internal/skcipher.h>
11*4882a593Smuzhiyun #include <crypto/authenc.h>
12*4882a593Smuzhiyun #include <crypto/null.h>
13*4882a593Smuzhiyun #include <crypto/scatterwalk.h>
14*4882a593Smuzhiyun #include <linux/err.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/rtnetlink.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/spinlock.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun struct authenc_instance_ctx {
23*4882a593Smuzhiyun struct crypto_ahash_spawn auth;
24*4882a593Smuzhiyun struct crypto_skcipher_spawn enc;
25*4882a593Smuzhiyun unsigned int reqoff;
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun struct crypto_authenc_ctx {
29*4882a593Smuzhiyun struct crypto_ahash *auth;
30*4882a593Smuzhiyun struct crypto_skcipher *enc;
31*4882a593Smuzhiyun struct crypto_sync_skcipher *null;
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun struct authenc_request_ctx {
35*4882a593Smuzhiyun struct scatterlist src[2];
36*4882a593Smuzhiyun struct scatterlist dst[2];
37*4882a593Smuzhiyun char tail[];
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
authenc_request_complete(struct aead_request * req,int err)40*4882a593Smuzhiyun static void authenc_request_complete(struct aead_request *req, int err)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun if (err != -EINPROGRESS)
43*4882a593Smuzhiyun aead_request_complete(req, err);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
crypto_authenc_extractkeys(struct crypto_authenc_keys * keys,const u8 * key,unsigned int keylen)46*4882a593Smuzhiyun int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
47*4882a593Smuzhiyun unsigned int keylen)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun struct rtattr *rta = (struct rtattr *)key;
50*4882a593Smuzhiyun struct crypto_authenc_key_param *param;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun if (!RTA_OK(rta, keylen))
53*4882a593Smuzhiyun return -EINVAL;
54*4882a593Smuzhiyun if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
55*4882a593Smuzhiyun return -EINVAL;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * RTA_OK() didn't align the rtattr's payload when validating that it
59*4882a593Smuzhiyun * fits in the buffer. Yet, the keys should start on the next 4-byte
60*4882a593Smuzhiyun * aligned boundary. To avoid confusion, require that the rtattr
61*4882a593Smuzhiyun * payload be exactly the param struct, which has a 4-byte aligned size.
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun if (RTA_PAYLOAD(rta) != sizeof(*param))
64*4882a593Smuzhiyun return -EINVAL;
65*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(*param) % RTA_ALIGNTO);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun param = RTA_DATA(rta);
68*4882a593Smuzhiyun keys->enckeylen = be32_to_cpu(param->enckeylen);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun key += rta->rta_len;
71*4882a593Smuzhiyun keylen -= rta->rta_len;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun if (keylen < keys->enckeylen)
74*4882a593Smuzhiyun return -EINVAL;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun keys->authkeylen = keylen - keys->enckeylen;
77*4882a593Smuzhiyun keys->authkey = key;
78*4882a593Smuzhiyun keys->enckey = key + keys->authkeylen;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun return 0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys);
83*4882a593Smuzhiyun
crypto_authenc_setkey(struct crypto_aead * authenc,const u8 * key,unsigned int keylen)84*4882a593Smuzhiyun static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
85*4882a593Smuzhiyun unsigned int keylen)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
88*4882a593Smuzhiyun struct crypto_ahash *auth = ctx->auth;
89*4882a593Smuzhiyun struct crypto_skcipher *enc = ctx->enc;
90*4882a593Smuzhiyun struct crypto_authenc_keys keys;
91*4882a593Smuzhiyun int err = -EINVAL;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
94*4882a593Smuzhiyun goto out;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
97*4882a593Smuzhiyun crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
98*4882a593Smuzhiyun CRYPTO_TFM_REQ_MASK);
99*4882a593Smuzhiyun err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
100*4882a593Smuzhiyun if (err)
101*4882a593Smuzhiyun goto out;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
104*4882a593Smuzhiyun crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
105*4882a593Smuzhiyun CRYPTO_TFM_REQ_MASK);
106*4882a593Smuzhiyun err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
107*4882a593Smuzhiyun out:
108*4882a593Smuzhiyun memzero_explicit(&keys, sizeof(keys));
109*4882a593Smuzhiyun return err;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
authenc_geniv_ahash_done(struct crypto_async_request * areq,int err)112*4882a593Smuzhiyun static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun struct aead_request *req = areq->data;
115*4882a593Smuzhiyun struct crypto_aead *authenc = crypto_aead_reqtfm(req);
116*4882a593Smuzhiyun struct aead_instance *inst = aead_alg_instance(authenc);
117*4882a593Smuzhiyun struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
118*4882a593Smuzhiyun struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
119*4882a593Smuzhiyun struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (err)
122*4882a593Smuzhiyun goto out;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun scatterwalk_map_and_copy(ahreq->result, req->dst,
125*4882a593Smuzhiyun req->assoclen + req->cryptlen,
126*4882a593Smuzhiyun crypto_aead_authsize(authenc), 1);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun out:
129*4882a593Smuzhiyun aead_request_complete(req, err);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
crypto_authenc_genicv(struct aead_request * req,unsigned int flags)132*4882a593Smuzhiyun static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun struct crypto_aead *authenc = crypto_aead_reqtfm(req);
135*4882a593Smuzhiyun struct aead_instance *inst = aead_alg_instance(authenc);
136*4882a593Smuzhiyun struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
137*4882a593Smuzhiyun struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
138*4882a593Smuzhiyun struct crypto_ahash *auth = ctx->auth;
139*4882a593Smuzhiyun struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
140*4882a593Smuzhiyun struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
141*4882a593Smuzhiyun u8 *hash = areq_ctx->tail;
142*4882a593Smuzhiyun int err;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
145*4882a593Smuzhiyun crypto_ahash_alignmask(auth) + 1);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun ahash_request_set_tfm(ahreq, auth);
148*4882a593Smuzhiyun ahash_request_set_crypt(ahreq, req->dst, hash,
149*4882a593Smuzhiyun req->assoclen + req->cryptlen);
150*4882a593Smuzhiyun ahash_request_set_callback(ahreq, flags,
151*4882a593Smuzhiyun authenc_geniv_ahash_done, req);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun err = crypto_ahash_digest(ahreq);
154*4882a593Smuzhiyun if (err)
155*4882a593Smuzhiyun return err;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen,
158*4882a593Smuzhiyun crypto_aead_authsize(authenc), 1);
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun return 0;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
crypto_authenc_encrypt_done(struct crypto_async_request * req,int err)163*4882a593Smuzhiyun static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
164*4882a593Smuzhiyun int err)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun struct aead_request *areq = req->data;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun if (err)
169*4882a593Smuzhiyun goto out;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun err = crypto_authenc_genicv(areq, 0);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun out:
174*4882a593Smuzhiyun authenc_request_complete(areq, err);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
crypto_authenc_copy_assoc(struct aead_request * req)177*4882a593Smuzhiyun static int crypto_authenc_copy_assoc(struct aead_request *req)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun struct crypto_aead *authenc = crypto_aead_reqtfm(req);
180*4882a593Smuzhiyun struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
181*4882a593Smuzhiyun SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun skcipher_request_set_sync_tfm(skreq, ctx->null);
184*4882a593Smuzhiyun skcipher_request_set_callback(skreq, aead_request_flags(req),
185*4882a593Smuzhiyun NULL, NULL);
186*4882a593Smuzhiyun skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
187*4882a593Smuzhiyun NULL);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun return crypto_skcipher_encrypt(skreq);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
crypto_authenc_encrypt(struct aead_request * req)192*4882a593Smuzhiyun static int crypto_authenc_encrypt(struct aead_request *req)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun struct crypto_aead *authenc = crypto_aead_reqtfm(req);
195*4882a593Smuzhiyun struct aead_instance *inst = aead_alg_instance(authenc);
196*4882a593Smuzhiyun struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
197*4882a593Smuzhiyun struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
198*4882a593Smuzhiyun struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
199*4882a593Smuzhiyun struct crypto_skcipher *enc = ctx->enc;
200*4882a593Smuzhiyun unsigned int cryptlen = req->cryptlen;
201*4882a593Smuzhiyun struct skcipher_request *skreq = (void *)(areq_ctx->tail +
202*4882a593Smuzhiyun ictx->reqoff);
203*4882a593Smuzhiyun struct scatterlist *src, *dst;
204*4882a593Smuzhiyun int err;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
207*4882a593Smuzhiyun dst = src;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (req->src != req->dst) {
210*4882a593Smuzhiyun err = crypto_authenc_copy_assoc(req);
211*4882a593Smuzhiyun if (err)
212*4882a593Smuzhiyun return err;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun skcipher_request_set_tfm(skreq, enc);
218*4882a593Smuzhiyun skcipher_request_set_callback(skreq, aead_request_flags(req),
219*4882a593Smuzhiyun crypto_authenc_encrypt_done, req);
220*4882a593Smuzhiyun skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun err = crypto_skcipher_encrypt(skreq);
223*4882a593Smuzhiyun if (err)
224*4882a593Smuzhiyun return err;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun return crypto_authenc_genicv(req, aead_request_flags(req));
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
crypto_authenc_decrypt_tail(struct aead_request * req,unsigned int flags)229*4882a593Smuzhiyun static int crypto_authenc_decrypt_tail(struct aead_request *req,
230*4882a593Smuzhiyun unsigned int flags)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun struct crypto_aead *authenc = crypto_aead_reqtfm(req);
233*4882a593Smuzhiyun struct aead_instance *inst = aead_alg_instance(authenc);
234*4882a593Smuzhiyun struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
235*4882a593Smuzhiyun struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
236*4882a593Smuzhiyun struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
237*4882a593Smuzhiyun struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
238*4882a593Smuzhiyun struct skcipher_request *skreq = (void *)(areq_ctx->tail +
239*4882a593Smuzhiyun ictx->reqoff);
240*4882a593Smuzhiyun unsigned int authsize = crypto_aead_authsize(authenc);
241*4882a593Smuzhiyun u8 *ihash = ahreq->result + authsize;
242*4882a593Smuzhiyun struct scatterlist *src, *dst;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun if (crypto_memneq(ihash, ahreq->result, authsize))
247*4882a593Smuzhiyun return -EBADMSG;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
250*4882a593Smuzhiyun dst = src;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun if (req->src != req->dst)
253*4882a593Smuzhiyun dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun skcipher_request_set_tfm(skreq, ctx->enc);
256*4882a593Smuzhiyun skcipher_request_set_callback(skreq, flags,
257*4882a593Smuzhiyun req->base.complete, req->base.data);
258*4882a593Smuzhiyun skcipher_request_set_crypt(skreq, src, dst,
259*4882a593Smuzhiyun req->cryptlen - authsize, req->iv);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun return crypto_skcipher_decrypt(skreq);
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
authenc_verify_ahash_done(struct crypto_async_request * areq,int err)264*4882a593Smuzhiyun static void authenc_verify_ahash_done(struct crypto_async_request *areq,
265*4882a593Smuzhiyun int err)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun struct aead_request *req = areq->data;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun if (err)
270*4882a593Smuzhiyun goto out;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun err = crypto_authenc_decrypt_tail(req, 0);
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun out:
275*4882a593Smuzhiyun authenc_request_complete(req, err);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
crypto_authenc_decrypt(struct aead_request * req)278*4882a593Smuzhiyun static int crypto_authenc_decrypt(struct aead_request *req)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun struct crypto_aead *authenc = crypto_aead_reqtfm(req);
281*4882a593Smuzhiyun unsigned int authsize = crypto_aead_authsize(authenc);
282*4882a593Smuzhiyun struct aead_instance *inst = aead_alg_instance(authenc);
283*4882a593Smuzhiyun struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
284*4882a593Smuzhiyun struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
285*4882a593Smuzhiyun struct crypto_ahash *auth = ctx->auth;
286*4882a593Smuzhiyun struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
287*4882a593Smuzhiyun struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
288*4882a593Smuzhiyun u8 *hash = areq_ctx->tail;
289*4882a593Smuzhiyun int err;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
292*4882a593Smuzhiyun crypto_ahash_alignmask(auth) + 1);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun ahash_request_set_tfm(ahreq, auth);
295*4882a593Smuzhiyun ahash_request_set_crypt(ahreq, req->src, hash,
296*4882a593Smuzhiyun req->assoclen + req->cryptlen - authsize);
297*4882a593Smuzhiyun ahash_request_set_callback(ahreq, aead_request_flags(req),
298*4882a593Smuzhiyun authenc_verify_ahash_done, req);
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun err = crypto_ahash_digest(ahreq);
301*4882a593Smuzhiyun if (err)
302*4882a593Smuzhiyun return err;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
crypto_authenc_init_tfm(struct crypto_aead * tfm)307*4882a593Smuzhiyun static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun struct aead_instance *inst = aead_alg_instance(tfm);
310*4882a593Smuzhiyun struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
311*4882a593Smuzhiyun struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
312*4882a593Smuzhiyun struct crypto_ahash *auth;
313*4882a593Smuzhiyun struct crypto_skcipher *enc;
314*4882a593Smuzhiyun struct crypto_sync_skcipher *null;
315*4882a593Smuzhiyun int err;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun auth = crypto_spawn_ahash(&ictx->auth);
318*4882a593Smuzhiyun if (IS_ERR(auth))
319*4882a593Smuzhiyun return PTR_ERR(auth);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun enc = crypto_spawn_skcipher(&ictx->enc);
322*4882a593Smuzhiyun err = PTR_ERR(enc);
323*4882a593Smuzhiyun if (IS_ERR(enc))
324*4882a593Smuzhiyun goto err_free_ahash;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun null = crypto_get_default_null_skcipher();
327*4882a593Smuzhiyun err = PTR_ERR(null);
328*4882a593Smuzhiyun if (IS_ERR(null))
329*4882a593Smuzhiyun goto err_free_skcipher;
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun ctx->auth = auth;
332*4882a593Smuzhiyun ctx->enc = enc;
333*4882a593Smuzhiyun ctx->null = null;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun crypto_aead_set_reqsize(
336*4882a593Smuzhiyun tfm,
337*4882a593Smuzhiyun sizeof(struct authenc_request_ctx) +
338*4882a593Smuzhiyun ictx->reqoff +
339*4882a593Smuzhiyun max_t(unsigned int,
340*4882a593Smuzhiyun crypto_ahash_reqsize(auth) +
341*4882a593Smuzhiyun sizeof(struct ahash_request),
342*4882a593Smuzhiyun sizeof(struct skcipher_request) +
343*4882a593Smuzhiyun crypto_skcipher_reqsize(enc)));
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun return 0;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun err_free_skcipher:
348*4882a593Smuzhiyun crypto_free_skcipher(enc);
349*4882a593Smuzhiyun err_free_ahash:
350*4882a593Smuzhiyun crypto_free_ahash(auth);
351*4882a593Smuzhiyun return err;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
crypto_authenc_exit_tfm(struct crypto_aead * tfm)354*4882a593Smuzhiyun static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun crypto_free_ahash(ctx->auth);
359*4882a593Smuzhiyun crypto_free_skcipher(ctx->enc);
360*4882a593Smuzhiyun crypto_put_default_null_skcipher();
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
crypto_authenc_free(struct aead_instance * inst)363*4882a593Smuzhiyun static void crypto_authenc_free(struct aead_instance *inst)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun crypto_drop_skcipher(&ctx->enc);
368*4882a593Smuzhiyun crypto_drop_ahash(&ctx->auth);
369*4882a593Smuzhiyun kfree(inst);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
crypto_authenc_create(struct crypto_template * tmpl,struct rtattr ** tb)372*4882a593Smuzhiyun static int crypto_authenc_create(struct crypto_template *tmpl,
373*4882a593Smuzhiyun struct rtattr **tb)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun u32 mask;
376*4882a593Smuzhiyun struct aead_instance *inst;
377*4882a593Smuzhiyun struct authenc_instance_ctx *ctx;
378*4882a593Smuzhiyun struct hash_alg_common *auth;
379*4882a593Smuzhiyun struct crypto_alg *auth_base;
380*4882a593Smuzhiyun struct skcipher_alg *enc;
381*4882a593Smuzhiyun int err;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
384*4882a593Smuzhiyun if (err)
385*4882a593Smuzhiyun return err;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
388*4882a593Smuzhiyun if (!inst)
389*4882a593Smuzhiyun return -ENOMEM;
390*4882a593Smuzhiyun ctx = aead_instance_ctx(inst);
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst),
393*4882a593Smuzhiyun crypto_attr_alg_name(tb[1]), 0, mask);
394*4882a593Smuzhiyun if (err)
395*4882a593Smuzhiyun goto err_free_inst;
396*4882a593Smuzhiyun auth = crypto_spawn_ahash_alg(&ctx->auth);
397*4882a593Smuzhiyun auth_base = &auth->base;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst),
400*4882a593Smuzhiyun crypto_attr_alg_name(tb[2]), 0, mask);
401*4882a593Smuzhiyun if (err)
402*4882a593Smuzhiyun goto err_free_inst;
403*4882a593Smuzhiyun enc = crypto_spawn_skcipher_alg(&ctx->enc);
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
406*4882a593Smuzhiyun auth_base->cra_alignmask + 1);
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun err = -ENAMETOOLONG;
409*4882a593Smuzhiyun if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
410*4882a593Smuzhiyun "authenc(%s,%s)", auth_base->cra_name,
411*4882a593Smuzhiyun enc->base.cra_name) >=
412*4882a593Smuzhiyun CRYPTO_MAX_ALG_NAME)
413*4882a593Smuzhiyun goto err_free_inst;
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
416*4882a593Smuzhiyun "authenc(%s,%s)", auth_base->cra_driver_name,
417*4882a593Smuzhiyun enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
418*4882a593Smuzhiyun goto err_free_inst;
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
421*4882a593Smuzhiyun auth_base->cra_priority;
422*4882a593Smuzhiyun inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
423*4882a593Smuzhiyun inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
424*4882a593Smuzhiyun enc->base.cra_alignmask;
425*4882a593Smuzhiyun inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
428*4882a593Smuzhiyun inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
429*4882a593Smuzhiyun inst->alg.maxauthsize = auth->digestsize;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun inst->alg.init = crypto_authenc_init_tfm;
432*4882a593Smuzhiyun inst->alg.exit = crypto_authenc_exit_tfm;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun inst->alg.setkey = crypto_authenc_setkey;
435*4882a593Smuzhiyun inst->alg.encrypt = crypto_authenc_encrypt;
436*4882a593Smuzhiyun inst->alg.decrypt = crypto_authenc_decrypt;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun inst->free = crypto_authenc_free;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun err = aead_register_instance(tmpl, inst);
441*4882a593Smuzhiyun if (err) {
442*4882a593Smuzhiyun err_free_inst:
443*4882a593Smuzhiyun crypto_authenc_free(inst);
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun return err;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun static struct crypto_template crypto_authenc_tmpl = {
449*4882a593Smuzhiyun .name = "authenc",
450*4882a593Smuzhiyun .create = crypto_authenc_create,
451*4882a593Smuzhiyun .module = THIS_MODULE,
452*4882a593Smuzhiyun };
453*4882a593Smuzhiyun
crypto_authenc_module_init(void)454*4882a593Smuzhiyun static int __init crypto_authenc_module_init(void)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun return crypto_register_template(&crypto_authenc_tmpl);
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
crypto_authenc_module_exit(void)459*4882a593Smuzhiyun static void __exit crypto_authenc_module_exit(void)
460*4882a593Smuzhiyun {
461*4882a593Smuzhiyun crypto_unregister_template(&crypto_authenc_tmpl);
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun subsys_initcall(crypto_authenc_module_init);
465*4882a593Smuzhiyun module_exit(crypto_authenc_module_exit);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun MODULE_LICENSE("GPL");
468*4882a593Smuzhiyun MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
469*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("authenc");
470