blob: 749527e1b617540260b8e67be4975d8cbc327d22 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Steffen Klasserta5079d02011-03-08 00:04:58 +00002/*
3 * authencesn.c - AEAD wrapper for IPsec with extended sequence numbers,
4 * derived from authenc.c
5 *
6 * Copyright (C) 2010 secunet Security Networks AG
7 * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
Herbert Xu104880a2015-08-07 16:42:59 +08008 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Steffen Klasserta5079d02011-03-08 00:04:58 +00009 */
10
Herbert Xu7fb2a4b2015-05-11 17:47:42 +080011#include <crypto/internal/aead.h>
Steffen Klasserta5079d02011-03-08 00:04:58 +000012#include <crypto/internal/hash.h>
13#include <crypto/internal/skcipher.h>
14#include <crypto/authenc.h>
Herbert Xu104880a2015-08-07 16:42:59 +080015#include <crypto/null.h>
Steffen Klasserta5079d02011-03-08 00:04:58 +000016#include <crypto/scatterwalk.h>
17#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/rtnetlink.h>
22#include <linux/slab.h>
23#include <linux/spinlock.h>
24
25struct authenc_esn_instance_ctx {
26 struct crypto_ahash_spawn auth;
27 struct crypto_skcipher_spawn enc;
28};
29
30struct crypto_authenc_esn_ctx {
31 unsigned int reqoff;
32 struct crypto_ahash *auth;
Herbert Xue75445a2016-07-12 13:17:35 +080033 struct crypto_skcipher *enc;
Kees Cook8d605392018-09-18 19:10:51 -070034 struct crypto_sync_skcipher *null;
Steffen Klasserta5079d02011-03-08 00:04:58 +000035};
36
37struct authenc_esn_request_ctx {
Herbert Xu104880a2015-08-07 16:42:59 +080038 struct scatterlist src[2];
39 struct scatterlist dst[2];
Steffen Klasserta5079d02011-03-08 00:04:58 +000040 char tail[];
41};
42
43static void authenc_esn_request_complete(struct aead_request *req, int err)
44{
45 if (err != -EINPROGRESS)
46 aead_request_complete(req, err);
47}
48
Herbert Xu104880a2015-08-07 16:42:59 +080049static int crypto_authenc_esn_setauthsize(struct crypto_aead *authenc_esn,
50 unsigned int authsize)
51{
52 if (authsize > 0 && authsize < 4)
53 return -EINVAL;
54
55 return 0;
56}
57
Steffen Klasserta5079d02011-03-08 00:04:58 +000058static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *key,
59 unsigned int keylen)
60{
Steffen Klasserta5079d02011-03-08 00:04:58 +000061 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
62 struct crypto_ahash *auth = ctx->auth;
Herbert Xue75445a2016-07-12 13:17:35 +080063 struct crypto_skcipher *enc = ctx->enc;
Mathias Krausefddc2c42013-10-15 13:49:31 +020064 struct crypto_authenc_keys keys;
Steffen Klasserta5079d02011-03-08 00:04:58 +000065 int err = -EINVAL;
66
Mathias Krausefddc2c42013-10-15 13:49:31 +020067 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Eric Biggers674f3682019-12-30 21:19:36 -060068 goto out;
Steffen Klasserta5079d02011-03-08 00:04:58 +000069
70 crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
71 crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc_esn) &
72 CRYPTO_TFM_REQ_MASK);
Mathias Krausefddc2c42013-10-15 13:49:31 +020073 err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
Steffen Klasserta5079d02011-03-08 00:04:58 +000074 crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) &
75 CRYPTO_TFM_RES_MASK);
76
77 if (err)
78 goto out;
79
Herbert Xue75445a2016-07-12 13:17:35 +080080 crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
81 crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) &
Steffen Klasserta5079d02011-03-08 00:04:58 +000082 CRYPTO_TFM_REQ_MASK);
Herbert Xue75445a2016-07-12 13:17:35 +080083 err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
84 crypto_aead_set_flags(authenc_esn, crypto_skcipher_get_flags(enc) &
Steffen Klasserta5079d02011-03-08 00:04:58 +000085 CRYPTO_TFM_RES_MASK);
86
87out:
Tudor-Dan Ambarus31545df2018-04-03 09:39:01 +030088 memzero_explicit(&keys, sizeof(keys));
Steffen Klasserta5079d02011-03-08 00:04:58 +000089 return err;
Steffen Klasserta5079d02011-03-08 00:04:58 +000090}
91
Herbert Xu104880a2015-08-07 16:42:59 +080092static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
93 unsigned int flags)
Steffen Klasserta5079d02011-03-08 00:04:58 +000094{
Steffen Klasserta5079d02011-03-08 00:04:58 +000095 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
96 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
97 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu104880a2015-08-07 16:42:59 +080098 struct crypto_ahash *auth = ctx->auth;
99 u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
100 crypto_ahash_alignmask(auth) + 1);
101 unsigned int authsize = crypto_aead_authsize(authenc_esn);
102 unsigned int assoclen = req->assoclen;
103 unsigned int cryptlen = req->cryptlen;
104 struct scatterlist *dst = req->dst;
105 u32 tmp[2];
Steffen Klasserta5079d02011-03-08 00:04:58 +0000106
Herbert Xu104880a2015-08-07 16:42:59 +0800107 /* Move high-order bits of sequence number back. */
108 scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
109 scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
110 scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000111
Herbert Xu104880a2015-08-07 16:42:59 +0800112 scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
113 return 0;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000114}
115
Steffen Klasserta5079d02011-03-08 00:04:58 +0000116static void authenc_esn_geniv_ahash_done(struct crypto_async_request *areq,
117 int err)
118{
119 struct aead_request *req = areq->data;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000120
Herbert Xu104880a2015-08-07 16:42:59 +0800121 err = err ?: crypto_authenc_esn_genicv_tail(req, 0);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000122 aead_request_complete(req, err);
123}
124
Herbert Xu104880a2015-08-07 16:42:59 +0800125static int crypto_authenc_esn_genicv(struct aead_request *req,
Steffen Klasserta5079d02011-03-08 00:04:58 +0000126 unsigned int flags)
127{
128 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
129 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu104880a2015-08-07 16:42:59 +0800130 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
131 struct crypto_ahash *auth = ctx->auth;
132 u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
133 crypto_ahash_alignmask(auth) + 1);
134 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
135 unsigned int authsize = crypto_aead_authsize(authenc_esn);
136 unsigned int assoclen = req->assoclen;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000137 unsigned int cryptlen = req->cryptlen;
Herbert Xu104880a2015-08-07 16:42:59 +0800138 struct scatterlist *dst = req->dst;
139 u32 tmp[2];
Steffen Klasserta5079d02011-03-08 00:04:58 +0000140
Herbert Xu104880a2015-08-07 16:42:59 +0800141 if (!authsize)
142 return 0;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000143
Herbert Xu104880a2015-08-07 16:42:59 +0800144 /* Move high-order bits of sequence number to the end. */
145 scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
146 scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
147 scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000148
Herbert Xu104880a2015-08-07 16:42:59 +0800149 sg_init_table(areq_ctx->dst, 2);
150 dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000151
Herbert Xu104880a2015-08-07 16:42:59 +0800152 ahash_request_set_tfm(ahreq, auth);
153 ahash_request_set_crypt(ahreq, dst, hash, assoclen + cryptlen);
154 ahash_request_set_callback(ahreq, flags,
155 authenc_esn_geniv_ahash_done, req);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000156
Herbert Xu104880a2015-08-07 16:42:59 +0800157 return crypto_ahash_digest(ahreq) ?:
158 crypto_authenc_esn_genicv_tail(req, aead_request_flags(req));
Steffen Klasserta5079d02011-03-08 00:04:58 +0000159}
160
161
162static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
163 int err)
164{
165 struct aead_request *areq = req->data;
166
Herbert Xu104880a2015-08-07 16:42:59 +0800167 if (!err)
168 err = crypto_authenc_esn_genicv(areq, 0);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000169
170 authenc_esn_request_complete(areq, err);
171}
172
Herbert Xu104880a2015-08-07 16:42:59 +0800173static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
174{
175 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
176 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
Kees Cook8d605392018-09-18 19:10:51 -0700177 SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
Herbert Xu104880a2015-08-07 16:42:59 +0800178
Kees Cook8d605392018-09-18 19:10:51 -0700179 skcipher_request_set_sync_tfm(skreq, ctx->null);
Herbert Xue75445a2016-07-12 13:17:35 +0800180 skcipher_request_set_callback(skreq, aead_request_flags(req),
181 NULL, NULL);
182 skcipher_request_set_crypt(skreq, req->src, req->dst, len, NULL);
183
184 return crypto_skcipher_encrypt(skreq);
Herbert Xu104880a2015-08-07 16:42:59 +0800185}
186
Steffen Klasserta5079d02011-03-08 00:04:58 +0000187static int crypto_authenc_esn_encrypt(struct aead_request *req)
188{
189 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000190 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu104880a2015-08-07 16:42:59 +0800191 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
Herbert Xue75445a2016-07-12 13:17:35 +0800192 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
193 ctx->reqoff);
194 struct crypto_skcipher *enc = ctx->enc;
Herbert Xu104880a2015-08-07 16:42:59 +0800195 unsigned int assoclen = req->assoclen;
196 unsigned int cryptlen = req->cryptlen;
197 struct scatterlist *src, *dst;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000198 int err;
199
Herbert Xu104880a2015-08-07 16:42:59 +0800200 sg_init_table(areq_ctx->src, 2);
201 src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
202 dst = src;
203
204 if (req->src != req->dst) {
205 err = crypto_authenc_esn_copy(req, assoclen);
206 if (err)
207 return err;
208
209 sg_init_table(areq_ctx->dst, 2);
210 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
211 }
212
Herbert Xue75445a2016-07-12 13:17:35 +0800213 skcipher_request_set_tfm(skreq, enc);
214 skcipher_request_set_callback(skreq, aead_request_flags(req),
215 crypto_authenc_esn_encrypt_done, req);
216 skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000217
Herbert Xue75445a2016-07-12 13:17:35 +0800218 err = crypto_skcipher_encrypt(skreq);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000219 if (err)
220 return err;
221
Herbert Xu104880a2015-08-07 16:42:59 +0800222 return crypto_authenc_esn_genicv(req, aead_request_flags(req));
Steffen Klasserta5079d02011-03-08 00:04:58 +0000223}
224
Herbert Xu104880a2015-08-07 16:42:59 +0800225static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
226 unsigned int flags)
Steffen Klasserta5079d02011-03-08 00:04:58 +0000227{
Herbert Xu104880a2015-08-07 16:42:59 +0800228 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
229 unsigned int authsize = crypto_aead_authsize(authenc_esn);
230 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000231 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
Herbert Xue75445a2016-07-12 13:17:35 +0800232 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
233 ctx->reqoff);
Herbert Xu104880a2015-08-07 16:42:59 +0800234 struct crypto_ahash *auth = ctx->auth;
235 u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
236 crypto_ahash_alignmask(auth) + 1);
237 unsigned int cryptlen = req->cryptlen - authsize;
238 unsigned int assoclen = req->assoclen;
239 struct scatterlist *dst = req->dst;
240 u8 *ihash = ohash + crypto_ahash_digestsize(auth);
241 u32 tmp[2];
Steffen Klasserta5079d02011-03-08 00:04:58 +0000242
Herbert Xu41cdf7a2017-07-17 15:32:30 +0800243 if (!authsize)
244 goto decrypt;
245
Herbert Xu104880a2015-08-07 16:42:59 +0800246 /* Move high-order bits of sequence number back. */
247 scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
248 scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
249 scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000250
Herbert Xu104880a2015-08-07 16:42:59 +0800251 if (crypto_memneq(ihash, ohash, authsize))
252 return -EBADMSG;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000253
Herbert Xu41cdf7a2017-07-17 15:32:30 +0800254decrypt:
255
Herbert Xu104880a2015-08-07 16:42:59 +0800256 sg_init_table(areq_ctx->dst, 2);
257 dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
258
Herbert Xue75445a2016-07-12 13:17:35 +0800259 skcipher_request_set_tfm(skreq, ctx->enc);
260 skcipher_request_set_callback(skreq, flags,
261 req->base.complete, req->base.data);
262 skcipher_request_set_crypt(skreq, dst, dst, cryptlen, req->iv);
Herbert Xu104880a2015-08-07 16:42:59 +0800263
Herbert Xue75445a2016-07-12 13:17:35 +0800264 return crypto_skcipher_decrypt(skreq);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000265}
266
Herbert Xu104880a2015-08-07 16:42:59 +0800267static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
268 int err)
Steffen Klasserta5079d02011-03-08 00:04:58 +0000269{
Herbert Xu104880a2015-08-07 16:42:59 +0800270 struct aead_request *req = areq->data;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000271
Herbert Xu104880a2015-08-07 16:42:59 +0800272 err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
Harsh Jaina7773362019-01-03 14:21:05 +0530273 authenc_esn_request_complete(req, err);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000274}
275
276static int crypto_authenc_esn_decrypt(struct aead_request *req)
277{
278 struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
Herbert Xu104880a2015-08-07 16:42:59 +0800279 struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000280 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
Herbert Xu104880a2015-08-07 16:42:59 +0800281 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000282 unsigned int authsize = crypto_aead_authsize(authenc_esn);
Herbert Xu104880a2015-08-07 16:42:59 +0800283 struct crypto_ahash *auth = ctx->auth;
284 u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
285 crypto_ahash_alignmask(auth) + 1);
286 unsigned int assoclen = req->assoclen;
287 unsigned int cryptlen = req->cryptlen;
288 u8 *ihash = ohash + crypto_ahash_digestsize(auth);
289 struct scatterlist *dst = req->dst;
290 u32 tmp[2];
Steffen Klasserta5079d02011-03-08 00:04:58 +0000291 int err;
292
Steffen Klasserta5079d02011-03-08 00:04:58 +0000293 cryptlen -= authsize;
294
Herbert Xu104880a2015-08-07 16:42:59 +0800295 if (req->src != dst) {
296 err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
297 if (err)
298 return err;
299 }
300
301 scatterwalk_map_and_copy(ihash, req->src, assoclen + cryptlen,
302 authsize, 0);
303
304 if (!authsize)
305 goto tail;
306
307 /* Move high-order bits of sequence number to the end. */
308 scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
309 scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
310 scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
311
312 sg_init_table(areq_ctx->dst, 2);
313 dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
314
315 ahash_request_set_tfm(ahreq, auth);
316 ahash_request_set_crypt(ahreq, dst, ohash, assoclen + cryptlen);
317 ahash_request_set_callback(ahreq, aead_request_flags(req),
318 authenc_esn_verify_ahash_done, req);
319
320 err = crypto_ahash_digest(ahreq);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000321 if (err)
322 return err;
323
Herbert Xu104880a2015-08-07 16:42:59 +0800324tail:
325 return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
Steffen Klasserta5079d02011-03-08 00:04:58 +0000326}
327
Herbert Xu104880a2015-08-07 16:42:59 +0800328static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
Steffen Klasserta5079d02011-03-08 00:04:58 +0000329{
Herbert Xu104880a2015-08-07 16:42:59 +0800330 struct aead_instance *inst = aead_alg_instance(tfm);
331 struct authenc_esn_instance_ctx *ictx = aead_instance_ctx(inst);
332 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000333 struct crypto_ahash *auth;
Herbert Xue75445a2016-07-12 13:17:35 +0800334 struct crypto_skcipher *enc;
Kees Cook8d605392018-09-18 19:10:51 -0700335 struct crypto_sync_skcipher *null;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000336 int err;
337
338 auth = crypto_spawn_ahash(&ictx->auth);
339 if (IS_ERR(auth))
340 return PTR_ERR(auth);
341
Eric Biggers60425a82016-10-28 09:52:19 -0700342 enc = crypto_spawn_skcipher(&ictx->enc);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000343 err = PTR_ERR(enc);
344 if (IS_ERR(enc))
345 goto err_free_ahash;
346
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800347 null = crypto_get_default_null_skcipher();
Herbert Xu104880a2015-08-07 16:42:59 +0800348 err = PTR_ERR(null);
349 if (IS_ERR(null))
350 goto err_free_skcipher;
351
Steffen Klasserta5079d02011-03-08 00:04:58 +0000352 ctx->auth = auth;
353 ctx->enc = enc;
Herbert Xu104880a2015-08-07 16:42:59 +0800354 ctx->null = null;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000355
Herbert Xu104880a2015-08-07 16:42:59 +0800356 ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth),
357 crypto_ahash_alignmask(auth) + 1);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000358
Herbert Xu104880a2015-08-07 16:42:59 +0800359 crypto_aead_set_reqsize(
360 tfm,
Herbert Xu6650d092015-05-11 17:47:55 +0800361 sizeof(struct authenc_esn_request_ctx) +
362 ctx->reqoff +
363 max_t(unsigned int,
Herbert Xue75445a2016-07-12 13:17:35 +0800364 crypto_ahash_reqsize(auth) +
365 sizeof(struct ahash_request),
366 sizeof(struct skcipher_request) +
367 crypto_skcipher_reqsize(enc)));
Steffen Klasserta5079d02011-03-08 00:04:58 +0000368
369 return 0;
370
Herbert Xu104880a2015-08-07 16:42:59 +0800371err_free_skcipher:
Herbert Xue75445a2016-07-12 13:17:35 +0800372 crypto_free_skcipher(enc);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000373err_free_ahash:
374 crypto_free_ahash(auth);
375 return err;
376}
377
Herbert Xu104880a2015-08-07 16:42:59 +0800378static void crypto_authenc_esn_exit_tfm(struct crypto_aead *tfm)
Steffen Klasserta5079d02011-03-08 00:04:58 +0000379{
Herbert Xu104880a2015-08-07 16:42:59 +0800380 struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000381
382 crypto_free_ahash(ctx->auth);
Herbert Xue75445a2016-07-12 13:17:35 +0800383 crypto_free_skcipher(ctx->enc);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800384 crypto_put_default_null_skcipher();
Steffen Klasserta5079d02011-03-08 00:04:58 +0000385}
386
Herbert Xu104880a2015-08-07 16:42:59 +0800387static void crypto_authenc_esn_free(struct aead_instance *inst)
388{
389 struct authenc_esn_instance_ctx *ctx = aead_instance_ctx(inst);
390
391 crypto_drop_skcipher(&ctx->enc);
392 crypto_drop_ahash(&ctx->auth);
393 kfree(inst);
394}
395
396static int crypto_authenc_esn_create(struct crypto_template *tmpl,
397 struct rtattr **tb)
Steffen Klasserta5079d02011-03-08 00:04:58 +0000398{
399 struct crypto_attr_type *algt;
Herbert Xu104880a2015-08-07 16:42:59 +0800400 struct aead_instance *inst;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000401 struct hash_alg_common *auth;
402 struct crypto_alg *auth_base;
Herbert Xue75445a2016-07-12 13:17:35 +0800403 struct skcipher_alg *enc;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000404 struct authenc_esn_instance_ctx *ctx;
405 const char *enc_name;
406 int err;
407
408 algt = crypto_get_attr_type(tb);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000409 if (IS_ERR(algt))
Herbert Xu104880a2015-08-07 16:42:59 +0800410 return PTR_ERR(algt);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000411
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800412 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xu104880a2015-08-07 16:42:59 +0800413 return -EINVAL;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000414
415 auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
Herbert Xu927ef322016-06-29 18:03:46 +0800416 CRYPTO_ALG_TYPE_AHASH_MASK |
417 crypto_requires_sync(algt->type, algt->mask));
Steffen Klasserta5079d02011-03-08 00:04:58 +0000418 if (IS_ERR(auth))
Herbert Xu104880a2015-08-07 16:42:59 +0800419 return PTR_ERR(auth);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000420
421 auth_base = &auth->base;
422
423 enc_name = crypto_attr_alg_name(tb[2]);
424 err = PTR_ERR(enc_name);
425 if (IS_ERR(enc_name))
426 goto out_put_auth;
427
428 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
429 err = -ENOMEM;
430 if (!inst)
431 goto out_put_auth;
432
Herbert Xu104880a2015-08-07 16:42:59 +0800433 ctx = aead_instance_ctx(inst);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000434
Herbert Xu104880a2015-08-07 16:42:59 +0800435 err = crypto_init_ahash_spawn(&ctx->auth, auth,
436 aead_crypto_instance(inst));
Steffen Klasserta5079d02011-03-08 00:04:58 +0000437 if (err)
438 goto err_free_inst;
439
Herbert Xu104880a2015-08-07 16:42:59 +0800440 crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
Eric Biggersa35528e2016-10-28 09:51:13 -0700441 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
442 crypto_requires_sync(algt->type,
443 algt->mask));
Steffen Klasserta5079d02011-03-08 00:04:58 +0000444 if (err)
445 goto err_drop_auth;
446
Herbert Xue75445a2016-07-12 13:17:35 +0800447 enc = crypto_spawn_skcipher_alg(&ctx->enc);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000448
449 err = -ENAMETOOLONG;
Herbert Xu104880a2015-08-07 16:42:59 +0800450 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
451 "authencesn(%s,%s)", auth_base->cra_name,
Herbert Xue75445a2016-07-12 13:17:35 +0800452 enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
Steffen Klasserta5079d02011-03-08 00:04:58 +0000453 goto err_drop_enc;
454
Herbert Xu104880a2015-08-07 16:42:59 +0800455 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Steffen Klasserta5079d02011-03-08 00:04:58 +0000456 "authencesn(%s,%s)", auth_base->cra_driver_name,
Herbert Xue75445a2016-07-12 13:17:35 +0800457 enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
Steffen Klasserta5079d02011-03-08 00:04:58 +0000458 goto err_drop_enc;
459
Herbert Xue75445a2016-07-12 13:17:35 +0800460 inst->alg.base.cra_flags = (auth_base->cra_flags |
461 enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
462 inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
Herbert Xu104880a2015-08-07 16:42:59 +0800463 auth_base->cra_priority;
Herbert Xue75445a2016-07-12 13:17:35 +0800464 inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
Herbert Xu104880a2015-08-07 16:42:59 +0800465 inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
Herbert Xue75445a2016-07-12 13:17:35 +0800466 enc->base.cra_alignmask;
Herbert Xu104880a2015-08-07 16:42:59 +0800467 inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000468
Herbert Xue75445a2016-07-12 13:17:35 +0800469 inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
470 inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
Herbert Xu104880a2015-08-07 16:42:59 +0800471 inst->alg.maxauthsize = auth->digestsize;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000472
Herbert Xu104880a2015-08-07 16:42:59 +0800473 inst->alg.init = crypto_authenc_esn_init_tfm;
474 inst->alg.exit = crypto_authenc_esn_exit_tfm;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000475
Herbert Xu104880a2015-08-07 16:42:59 +0800476 inst->alg.setkey = crypto_authenc_esn_setkey;
477 inst->alg.setauthsize = crypto_authenc_esn_setauthsize;
478 inst->alg.encrypt = crypto_authenc_esn_encrypt;
479 inst->alg.decrypt = crypto_authenc_esn_decrypt;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000480
Herbert Xu104880a2015-08-07 16:42:59 +0800481 inst->free = crypto_authenc_esn_free,
482
483 err = aead_register_instance(tmpl, inst);
484 if (err)
485 goto err_drop_enc;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000486
487out:
488 crypto_mod_put(auth_base);
Herbert Xu104880a2015-08-07 16:42:59 +0800489 return err;
Steffen Klasserta5079d02011-03-08 00:04:58 +0000490
491err_drop_enc:
492 crypto_drop_skcipher(&ctx->enc);
493err_drop_auth:
494 crypto_drop_ahash(&ctx->auth);
495err_free_inst:
496 kfree(inst);
497out_put_auth:
Steffen Klasserta5079d02011-03-08 00:04:58 +0000498 goto out;
499}
500
Steffen Klasserta5079d02011-03-08 00:04:58 +0000501static struct crypto_template crypto_authenc_esn_tmpl = {
502 .name = "authencesn",
Herbert Xu104880a2015-08-07 16:42:59 +0800503 .create = crypto_authenc_esn_create,
Steffen Klasserta5079d02011-03-08 00:04:58 +0000504 .module = THIS_MODULE,
505};
506
507static int __init crypto_authenc_esn_module_init(void)
508{
509 return crypto_register_template(&crypto_authenc_esn_tmpl);
510}
511
512static void __exit crypto_authenc_esn_module_exit(void)
513{
514 crypto_unregister_template(&crypto_authenc_esn_tmpl);
515}
516
Eric Biggersc4741b22019-04-11 21:57:42 -0700517subsys_initcall(crypto_authenc_esn_module_init);
Steffen Klasserta5079d02011-03-08 00:04:58 +0000518module_exit(crypto_authenc_esn_module_exit);
519
520MODULE_LICENSE("GPL");
521MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
522MODULE_DESCRIPTION("AEAD wrapper for IPsec with extended sequence numbers");
Kees Cook4943ba12014-11-24 16:32:38 -0800523MODULE_ALIAS_CRYPTO("authencesn");