blob: 3326c7343e8673b6adc6d22292c67aecfa1d55c6 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xu3c09f172007-08-30 16:24:15 +08002/*
3 * Authenc: Simple AEAD wrapper for IPsec
4 *
Herbert Xu92d95ba2015-07-30 17:53:16 +08005 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xu3c09f172007-08-30 16:24:15 +08006 */
7
Herbert Xu68acbf82015-05-11 17:47:41 +08008#include <crypto/internal/aead.h>
Herbert Xu5f7082e2008-08-31 22:21:09 +10009#include <crypto/internal/hash.h>
Herbert Xu9ffde35a2007-12-17 20:12:49 +080010#include <crypto/internal/skcipher.h>
Herbert Xue236d4a2007-11-22 23:11:53 +080011#include <crypto/authenc.h>
Herbert Xu92d95ba2015-07-30 17:53:16 +080012#include <crypto/null.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080013#include <crypto/scatterwalk.h>
Herbert Xu3c09f172007-08-30 16:24:15 +080014#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
Herbert Xue236d4a2007-11-22 23:11:53 +080018#include <linux/rtnetlink.h>
Herbert Xu3c09f172007-08-30 16:24:15 +080019#include <linux/slab.h>
20#include <linux/spinlock.h>
21
Herbert Xu3c09f172007-08-30 16:24:15 +080022struct authenc_instance_ctx {
Steffen Klassertcbdcf802009-08-05 19:35:34 +100023 struct crypto_ahash_spawn auth;
Herbert Xu9ffde35a2007-12-17 20:12:49 +080024 struct crypto_skcipher_spawn enc;
Herbert Xu92d95ba2015-07-30 17:53:16 +080025 unsigned int reqoff;
Herbert Xu3c09f172007-08-30 16:24:15 +080026};
27
28struct crypto_authenc_ctx {
Steffen Klassertcbdcf802009-08-05 19:35:34 +100029 struct crypto_ahash *auth;
Herbert Xu7217d492016-07-12 13:17:34 +080030 struct crypto_skcipher *enc;
Kees Cook8d605392018-09-18 19:10:51 -070031 struct crypto_sync_skcipher *null;
Herbert Xu3c09f172007-08-30 16:24:15 +080032};
33
Steffen Klassertcbdcf802009-08-05 19:35:34 +100034struct authenc_request_ctx {
Herbert Xu92d95ba2015-07-30 17:53:16 +080035 struct scatterlist src[2];
36 struct scatterlist dst[2];
Steffen Klassertcbdcf802009-08-05 19:35:34 +100037 char tail[];
38};
39
Herbert Xu180ce7e2010-04-26 09:14:05 +080040static void authenc_request_complete(struct aead_request *req, int err)
41{
42 if (err != -EINPROGRESS)
43 aead_request_complete(req, err);
44}
45
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020046int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
47 unsigned int keylen)
Herbert Xu3c09f172007-08-30 16:24:15 +080048{
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020049 struct rtattr *rta = (struct rtattr *)key;
Herbert Xue236d4a2007-11-22 23:11:53 +080050 struct crypto_authenc_key_param *param;
Herbert Xu3c09f172007-08-30 16:24:15 +080051
Herbert Xu12dc5e62007-12-10 10:55:21 +080052 if (!RTA_OK(rta, keylen))
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020053 return -EINVAL;
Herbert Xue236d4a2007-11-22 23:11:53 +080054 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020055 return -EINVAL;
Eric Biggers8f9c4692018-12-16 23:23:22 -080056
57 /*
58 * RTA_OK() didn't align the rtattr's payload when validating that it
59 * fits in the buffer. Yet, the keys should start on the next 4-byte
60 * aligned boundary. To avoid confusion, require that the rtattr
61 * payload be exactly the param struct, which has a 4-byte aligned size.
62 */
63 if (RTA_PAYLOAD(rta) != sizeof(*param))
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020064 return -EINVAL;
Eric Biggers8f9c4692018-12-16 23:23:22 -080065 BUILD_BUG_ON(sizeof(*param) % RTA_ALIGNTO);
Herbert Xue236d4a2007-11-22 23:11:53 +080066
67 param = RTA_DATA(rta);
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020068 keys->enckeylen = be32_to_cpu(param->enckeylen);
Herbert Xue236d4a2007-11-22 23:11:53 +080069
Eric Biggers8f9c4692018-12-16 23:23:22 -080070 key += rta->rta_len;
71 keylen -= rta->rta_len;
Herbert Xue236d4a2007-11-22 23:11:53 +080072
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020073 if (keylen < keys->enckeylen)
74 return -EINVAL;
Herbert Xue236d4a2007-11-22 23:11:53 +080075
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020076 keys->authkeylen = keylen - keys->enckeylen;
77 keys->authkey = key;
78 keys->enckey = key + keys->authkeylen;
79
80 return 0;
81}
82EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys);
83
84static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
85 unsigned int keylen)
86{
87 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
88 struct crypto_ahash *auth = ctx->auth;
Herbert Xu7217d492016-07-12 13:17:34 +080089 struct crypto_skcipher *enc = ctx->enc;
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020090 struct crypto_authenc_keys keys;
91 int err = -EINVAL;
92
93 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Eric Biggers674f3682019-12-30 21:19:36 -060094 goto out;
Herbert Xu3c09f172007-08-30 16:24:15 +080095
Steffen Klassertcbdcf802009-08-05 19:35:34 +100096 crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
97 crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
Herbert Xu3c09f172007-08-30 16:24:15 +080098 CRYPTO_TFM_REQ_MASK);
Mathias Krausebc6e2bd2013-10-15 13:49:30 +020099 err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800100 if (err)
101 goto out;
102
Herbert Xu7217d492016-07-12 13:17:34 +0800103 crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
104 crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
105 CRYPTO_TFM_REQ_MASK);
106 err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800107out:
Tudor-Dan Ambarusad2fdcd2018-04-03 09:39:00 +0300108 memzero_explicit(&keys, sizeof(keys));
Herbert Xu3c09f172007-08-30 16:24:15 +0800109 return err;
110}
111
Herbert Xu255e48e2023-02-08 13:58:44 +0800112static void authenc_geniv_ahash_done(void *data, int err)
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000113{
Herbert Xu255e48e2023-02-08 13:58:44 +0800114 struct aead_request *req = data;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000115 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800116 struct aead_instance *inst = aead_alg_instance(authenc);
117 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000118 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800119 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000120
121 if (err)
122 goto out;
123
Herbert Xu92d95ba2015-07-30 17:53:16 +0800124 scatterwalk_map_and_copy(ahreq->result, req->dst,
125 req->assoclen + req->cryptlen,
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000126 crypto_aead_authsize(authenc), 1);
127
128out:
129 aead_request_complete(req, err);
130}
131
Herbert Xu92d95ba2015-07-30 17:53:16 +0800132static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
Herbert Xu3c09f172007-08-30 16:24:15 +0800133{
134 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800135 struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800136 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800137 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000138 struct crypto_ahash *auth = ctx->auth;
139 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800140 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000141 u8 *hash = areq_ctx->tail;
142 int err;
143
144 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
145 crypto_ahash_alignmask(auth) + 1);
146
147 ahash_request_set_tfm(ahreq, auth);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800148 ahash_request_set_crypt(ahreq, req->dst, hash,
149 req->assoclen + req->cryptlen);
150 ahash_request_set_callback(ahreq, flags,
151 authenc_geniv_ahash_done, req);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000152
153 err = crypto_ahash_digest(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800154 if (err)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800155 return err;
Herbert Xu7c3d7032007-12-10 16:15:41 +0800156
Herbert Xu92d95ba2015-07-30 17:53:16 +0800157 scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen,
Herbert Xu7ba683a2007-12-02 18:49:21 +1100158 crypto_aead_authsize(authenc), 1);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800159
Herbert Xu3c09f172007-08-30 16:24:15 +0800160 return 0;
161}
162
Herbert Xu255e48e2023-02-08 13:58:44 +0800163static void crypto_authenc_encrypt_done(void *data, int err)
Herbert Xu3c09f172007-08-30 16:24:15 +0800164{
Herbert Xu255e48e2023-02-08 13:58:44 +0800165 struct aead_request *areq = data;
Herbert Xua6976902008-08-23 01:04:06 +1000166
Herbert Xu92d95ba2015-07-30 17:53:16 +0800167 if (err)
168 goto out;
Herbert Xue56dd562007-12-10 16:20:24 +0800169
Herbert Xu92d95ba2015-07-30 17:53:16 +0800170 err = crypto_authenc_genicv(areq, 0);
Herbert Xu3c09f172007-08-30 16:24:15 +0800171
Herbert Xu92d95ba2015-07-30 17:53:16 +0800172out:
Herbert Xu180ce7e2010-04-26 09:14:05 +0800173 authenc_request_complete(areq, err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800174}
175
Herbert Xu92d95ba2015-07-30 17:53:16 +0800176static int crypto_authenc_copy_assoc(struct aead_request *req)
177{
178 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
179 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Kees Cook8d605392018-09-18 19:10:51 -0700180 SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800181
Kees Cook8d605392018-09-18 19:10:51 -0700182 skcipher_request_set_sync_tfm(skreq, ctx->null);
Herbert Xu7217d492016-07-12 13:17:34 +0800183 skcipher_request_set_callback(skreq, aead_request_flags(req),
184 NULL, NULL);
185 skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
186 NULL);
187
188 return crypto_skcipher_encrypt(skreq);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800189}
190
Herbert Xu3c09f172007-08-30 16:24:15 +0800191static int crypto_authenc_encrypt(struct aead_request *req)
192{
193 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800194 struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800195 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800196 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
Steffen Klassert50beceb2010-03-03 22:41:08 +0800197 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu7217d492016-07-12 13:17:34 +0800198 struct crypto_skcipher *enc = ctx->enc;
Herbert Xue56dd562007-12-10 16:20:24 +0800199 unsigned int cryptlen = req->cryptlen;
Herbert Xu7217d492016-07-12 13:17:34 +0800200 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
201 ictx->reqoff);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800202 struct scatterlist *src, *dst;
Herbert Xu3c09f172007-08-30 16:24:15 +0800203 int err;
204
Herbert Xu92d95ba2015-07-30 17:53:16 +0800205 src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
206 dst = src;
207
208 if (req->src != req->dst) {
209 err = crypto_authenc_copy_assoc(req);
210 if (err)
211 return err;
212
Herbert Xu92d95ba2015-07-30 17:53:16 +0800213 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
214 }
215
Herbert Xu7217d492016-07-12 13:17:34 +0800216 skcipher_request_set_tfm(skreq, enc);
217 skcipher_request_set_callback(skreq, aead_request_flags(req),
218 crypto_authenc_encrypt_done, req);
219 skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
Herbert Xu3c09f172007-08-30 16:24:15 +0800220
Herbert Xu7217d492016-07-12 13:17:34 +0800221 err = crypto_skcipher_encrypt(skreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800222 if (err)
223 return err;
224
Herbert Xu92d95ba2015-07-30 17:53:16 +0800225 return crypto_authenc_genicv(req, aead_request_flags(req));
Herbert Xue56dd562007-12-10 16:20:24 +0800226}
227
Herbert Xu92d95ba2015-07-30 17:53:16 +0800228static int crypto_authenc_decrypt_tail(struct aead_request *req,
229 unsigned int flags)
Herbert Xue56dd562007-12-10 16:20:24 +0800230{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800231 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
232 struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xue56dd562007-12-10 16:20:24 +0800233 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800234 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
235 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
236 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
Herbert Xu7217d492016-07-12 13:17:34 +0800237 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
238 ictx->reqoff);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800239 unsigned int authsize = crypto_aead_authsize(authenc);
240 u8 *ihash = ahreq->result + authsize;
241 struct scatterlist *src, *dst;
Herbert Xue56dd562007-12-10 16:20:24 +0800242
Herbert Xu92d95ba2015-07-30 17:53:16 +0800243 scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
Herbert Xue56dd562007-12-10 16:20:24 +0800244
Herbert Xu92d95ba2015-07-30 17:53:16 +0800245 if (crypto_memneq(ihash, ahreq->result, authsize))
246 return -EBADMSG;
247
Herbert Xu92d95ba2015-07-30 17:53:16 +0800248 src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
249 dst = src;
250
Harsh Jainc34252f2016-06-29 00:24:43 +0530251 if (req->src != req->dst)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800252 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800253
Herbert Xu7217d492016-07-12 13:17:34 +0800254 skcipher_request_set_tfm(skreq, ctx->enc);
Herbert Xu66eae852022-01-19 17:58:40 +1100255 skcipher_request_set_callback(skreq, flags,
Herbert Xu7217d492016-07-12 13:17:34 +0800256 req->base.complete, req->base.data);
257 skcipher_request_set_crypt(skreq, src, dst,
258 req->cryptlen - authsize, req->iv);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800259
Herbert Xu7217d492016-07-12 13:17:34 +0800260 return crypto_skcipher_decrypt(skreq);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800261}
262
Herbert Xu255e48e2023-02-08 13:58:44 +0800263static void authenc_verify_ahash_done(void *data, int err)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800264{
Herbert Xu255e48e2023-02-08 13:58:44 +0800265 struct aead_request *req = data;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800266
Herbert Xue56dd562007-12-10 16:20:24 +0800267 if (err)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800268 goto out;
Herbert Xue56dd562007-12-10 16:20:24 +0800269
Herbert Xu92d95ba2015-07-30 17:53:16 +0800270 err = crypto_authenc_decrypt_tail(req, 0);
Herbert Xu3c09f172007-08-30 16:24:15 +0800271
Herbert Xu92d95ba2015-07-30 17:53:16 +0800272out:
273 authenc_request_complete(req, err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800274}
275
276static int crypto_authenc_decrypt(struct aead_request *req)
277{
278 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100279 unsigned int authsize = crypto_aead_authsize(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800280 struct aead_instance *inst = aead_alg_instance(authenc);
281 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
282 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
283 struct crypto_ahash *auth = ctx->auth;
284 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
285 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
286 u8 *hash = areq_ctx->tail;
Herbert Xu3c09f172007-08-30 16:24:15 +0800287 int err;
288
Herbert Xu92d95ba2015-07-30 17:53:16 +0800289 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
290 crypto_ahash_alignmask(auth) + 1);
Herbert Xu481f34a2007-12-04 20:04:21 +1100291
Herbert Xu92d95ba2015-07-30 17:53:16 +0800292 ahash_request_set_tfm(ahreq, auth);
293 ahash_request_set_crypt(ahreq, req->src, hash,
294 req->assoclen + req->cryptlen - authsize);
295 ahash_request_set_callback(ahreq, aead_request_flags(req),
296 authenc_verify_ahash_done, req);
297
298 err = crypto_ahash_digest(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800299 if (err)
300 return err;
301
Herbert Xu92d95ba2015-07-30 17:53:16 +0800302 return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
Herbert Xu3c09f172007-08-30 16:24:15 +0800303}
304
Herbert Xu92d95ba2015-07-30 17:53:16 +0800305static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
Herbert Xu3c09f172007-08-30 16:24:15 +0800306{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800307 struct aead_instance *inst = aead_alg_instance(tfm);
308 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
309 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000310 struct crypto_ahash *auth;
Herbert Xu7217d492016-07-12 13:17:34 +0800311 struct crypto_skcipher *enc;
Kees Cook8d605392018-09-18 19:10:51 -0700312 struct crypto_sync_skcipher *null;
Herbert Xu3c09f172007-08-30 16:24:15 +0800313 int err;
314
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000315 auth = crypto_spawn_ahash(&ictx->auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800316 if (IS_ERR(auth))
317 return PTR_ERR(auth);
318
Eric Biggers60425a82016-10-28 09:52:19 -0700319 enc = crypto_spawn_skcipher(&ictx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800320 err = PTR_ERR(enc);
321 if (IS_ERR(enc))
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000322 goto err_free_ahash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800323
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800324 null = crypto_get_default_null_skcipher();
Herbert Xu92d95ba2015-07-30 17:53:16 +0800325 err = PTR_ERR(null);
326 if (IS_ERR(null))
327 goto err_free_skcipher;
328
Herbert Xu3c09f172007-08-30 16:24:15 +0800329 ctx->auth = auth;
330 ctx->enc = enc;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800331 ctx->null = null;
Richard Hartmannf3542e62010-02-16 20:27:20 +0800332
Herbert Xu92d95ba2015-07-30 17:53:16 +0800333 crypto_aead_set_reqsize(
334 tfm,
Herbert Xu25df9192015-05-11 17:47:53 +0800335 sizeof(struct authenc_request_ctx) +
Herbert Xu92d95ba2015-07-30 17:53:16 +0800336 ictx->reqoff +
Herbert Xu25df9192015-05-11 17:47:53 +0800337 max_t(unsigned int,
Herbert Xu92d95ba2015-07-30 17:53:16 +0800338 crypto_ahash_reqsize(auth) +
339 sizeof(struct ahash_request),
Herbert Xu7217d492016-07-12 13:17:34 +0800340 sizeof(struct skcipher_request) +
341 crypto_skcipher_reqsize(enc)));
Herbert Xu3c09f172007-08-30 16:24:15 +0800342
343 return 0;
344
Herbert Xu92d95ba2015-07-30 17:53:16 +0800345err_free_skcipher:
Herbert Xu7217d492016-07-12 13:17:34 +0800346 crypto_free_skcipher(enc);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000347err_free_ahash:
348 crypto_free_ahash(auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800349 return err;
350}
351
Herbert Xu92d95ba2015-07-30 17:53:16 +0800352static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
Herbert Xu3c09f172007-08-30 16:24:15 +0800353{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800354 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu3c09f172007-08-30 16:24:15 +0800355
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000356 crypto_free_ahash(ctx->auth);
Herbert Xu7217d492016-07-12 13:17:34 +0800357 crypto_free_skcipher(ctx->enc);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800358 crypto_put_default_null_skcipher();
Herbert Xu3c09f172007-08-30 16:24:15 +0800359}
360
Herbert Xu92d95ba2015-07-30 17:53:16 +0800361static void crypto_authenc_free(struct aead_instance *inst)
362{
363 struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
364
365 crypto_drop_skcipher(&ctx->enc);
366 crypto_drop_ahash(&ctx->auth);
367 kfree(inst);
368}
369
370static int crypto_authenc_create(struct crypto_template *tmpl,
371 struct rtattr **tb)
Herbert Xu3c09f172007-08-30 16:24:15 +0800372{
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800373 u32 mask;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800374 struct aead_instance *inst;
Eric Biggers37a861a2020-01-02 19:58:55 -0800375 struct authenc_instance_ctx *ctx;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000376 struct hash_alg_common *auth;
377 struct crypto_alg *auth_base;
Herbert Xu7217d492016-07-12 13:17:34 +0800378 struct skcipher_alg *enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800379 int err;
380
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700381 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
382 if (err)
383 return err;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800384
Herbert Xu3c09f172007-08-30 16:24:15 +0800385 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Herbert Xu3c09f172007-08-30 16:24:15 +0800386 if (!inst)
Eric Biggers37a861a2020-01-02 19:58:55 -0800387 return -ENOMEM;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800388 ctx = aead_instance_ctx(inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800389
Eric Biggers37a861a2020-01-02 19:58:55 -0800390 err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst),
391 crypto_attr_alg_name(tb[1]), 0, mask);
Herbert Xu3c09f172007-08-30 16:24:15 +0800392 if (err)
393 goto err_free_inst;
Eric Biggers37a861a2020-01-02 19:58:55 -0800394 auth = crypto_spawn_ahash_alg(&ctx->auth);
395 auth_base = &auth->base;
Herbert Xu3c09f172007-08-30 16:24:15 +0800396
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800397 err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst),
Eric Biggers37a861a2020-01-02 19:58:55 -0800398 crypto_attr_alg_name(tb[2]), 0, mask);
Herbert Xu3c09f172007-08-30 16:24:15 +0800399 if (err)
Eric Biggers37a861a2020-01-02 19:58:55 -0800400 goto err_free_inst;
Herbert Xu7217d492016-07-12 13:17:34 +0800401 enc = crypto_spawn_skcipher_alg(&ctx->enc);
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800402
Herbert Xu92d95ba2015-07-30 17:53:16 +0800403 ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
404 auth_base->cra_alignmask + 1);
405
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800406 err = -ENAMETOOLONG;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800407 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu7217d492016-07-12 13:17:34 +0800408 "authenc(%s,%s)", auth_base->cra_name,
409 enc->base.cra_name) >=
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800410 CRYPTO_MAX_ALG_NAME)
Eric Biggers37a861a2020-01-02 19:58:55 -0800411 goto err_free_inst;
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800412
Herbert Xu92d95ba2015-07-30 17:53:16 +0800413 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000414 "authenc(%s,%s)", auth_base->cra_driver_name,
Herbert Xu7217d492016-07-12 13:17:34 +0800415 enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
Eric Biggers37a861a2020-01-02 19:58:55 -0800416 goto err_free_inst;
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800417
Herbert Xu7217d492016-07-12 13:17:34 +0800418 inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
Herbert Xu92d95ba2015-07-30 17:53:16 +0800419 auth_base->cra_priority;
Herbert Xu7217d492016-07-12 13:17:34 +0800420 inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800421 inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
Herbert Xu7217d492016-07-12 13:17:34 +0800422 enc->base.cra_alignmask;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800423 inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
Herbert Xu3c09f172007-08-30 16:24:15 +0800424
Herbert Xu7217d492016-07-12 13:17:34 +0800425 inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
426 inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800427 inst->alg.maxauthsize = auth->digestsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800428
Herbert Xu92d95ba2015-07-30 17:53:16 +0800429 inst->alg.init = crypto_authenc_init_tfm;
430 inst->alg.exit = crypto_authenc_exit_tfm;
Herbert Xu3c09f172007-08-30 16:24:15 +0800431
Herbert Xu92d95ba2015-07-30 17:53:16 +0800432 inst->alg.setkey = crypto_authenc_setkey;
433 inst->alg.encrypt = crypto_authenc_encrypt;
434 inst->alg.decrypt = crypto_authenc_decrypt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800435
Herbert Xu92d95ba2015-07-30 17:53:16 +0800436 inst->free = crypto_authenc_free;
437
438 err = aead_register_instance(tmpl, inst);
Eric Biggers37a861a2020-01-02 19:58:55 -0800439 if (err) {
Herbert Xu3c09f172007-08-30 16:24:15 +0800440err_free_inst:
Eric Biggers37a861a2020-01-02 19:58:55 -0800441 crypto_authenc_free(inst);
442 }
443 return err;
Herbert Xu3c09f172007-08-30 16:24:15 +0800444}
445
Herbert Xu3c09f172007-08-30 16:24:15 +0800446static struct crypto_template crypto_authenc_tmpl = {
447 .name = "authenc",
Herbert Xu92d95ba2015-07-30 17:53:16 +0800448 .create = crypto_authenc_create,
Herbert Xu3c09f172007-08-30 16:24:15 +0800449 .module = THIS_MODULE,
450};
451
452static int __init crypto_authenc_module_init(void)
453{
454 return crypto_register_template(&crypto_authenc_tmpl);
455}
456
457static void __exit crypto_authenc_module_exit(void)
458{
459 crypto_unregister_template(&crypto_authenc_tmpl);
460}
461
Eric Biggersc4741b22019-04-11 21:57:42 -0700462subsys_initcall(crypto_authenc_module_init);
Herbert Xu3c09f172007-08-30 16:24:15 +0800463module_exit(crypto_authenc_module_exit);
464
465MODULE_LICENSE("GPL");
466MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
Kees Cook4943ba12014-11-24 16:32:38 -0800467MODULE_ALIAS_CRYPTO("authenc");