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