blob: 670bf1a01d00e4c3fd1cda495973a78da9ae6db2 [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
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000112static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
113{
114 struct aead_request *req = areq->data;
115 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
163static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
164 int err)
165{
Herbert Xua6976902008-08-23 01:04:06 +1000166 struct aead_request *areq = req->data;
167
Herbert Xu92d95ba2015-07-30 17:53:16 +0800168 if (err)
169 goto out;
Herbert Xue56dd562007-12-10 16:20:24 +0800170
Herbert Xu92d95ba2015-07-30 17:53:16 +0800171 err = crypto_authenc_genicv(areq, 0);
Herbert Xu3c09f172007-08-30 16:24:15 +0800172
Herbert Xu92d95ba2015-07-30 17:53:16 +0800173out:
Herbert Xu180ce7e2010-04-26 09:14:05 +0800174 authenc_request_complete(areq, err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800175}
176
Herbert Xu92d95ba2015-07-30 17:53:16 +0800177static int crypto_authenc_copy_assoc(struct aead_request *req)
178{
179 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
180 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Kees Cook8d605392018-09-18 19:10:51 -0700181 SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800182
Kees Cook8d605392018-09-18 19:10:51 -0700183 skcipher_request_set_sync_tfm(skreq, ctx->null);
Herbert Xu7217d492016-07-12 13:17:34 +0800184 skcipher_request_set_callback(skreq, aead_request_flags(req),
185 NULL, NULL);
186 skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
187 NULL);
188
189 return crypto_skcipher_encrypt(skreq);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800190}
191
Herbert Xu3c09f172007-08-30 16:24:15 +0800192static int crypto_authenc_encrypt(struct aead_request *req)
193{
194 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800195 struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800196 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800197 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
Steffen Klassert50beceb2010-03-03 22:41:08 +0800198 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu7217d492016-07-12 13:17:34 +0800199 struct crypto_skcipher *enc = ctx->enc;
Herbert Xue56dd562007-12-10 16:20:24 +0800200 unsigned int cryptlen = req->cryptlen;
Herbert Xu7217d492016-07-12 13:17:34 +0800201 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
202 ictx->reqoff);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800203 struct scatterlist *src, *dst;
Herbert Xu3c09f172007-08-30 16:24:15 +0800204 int err;
205
Herbert Xu92d95ba2015-07-30 17:53:16 +0800206 src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
207 dst = src;
208
209 if (req->src != req->dst) {
210 err = crypto_authenc_copy_assoc(req);
211 if (err)
212 return err;
213
Herbert Xu92d95ba2015-07-30 17:53:16 +0800214 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
215 }
216
Herbert Xu7217d492016-07-12 13:17:34 +0800217 skcipher_request_set_tfm(skreq, enc);
218 skcipher_request_set_callback(skreq, aead_request_flags(req),
219 crypto_authenc_encrypt_done, req);
220 skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
Herbert Xu3c09f172007-08-30 16:24:15 +0800221
Herbert Xu7217d492016-07-12 13:17:34 +0800222 err = crypto_skcipher_encrypt(skreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800223 if (err)
224 return err;
225
Herbert Xu92d95ba2015-07-30 17:53:16 +0800226 return crypto_authenc_genicv(req, aead_request_flags(req));
Herbert Xue56dd562007-12-10 16:20:24 +0800227}
228
Herbert Xu92d95ba2015-07-30 17:53:16 +0800229static int crypto_authenc_decrypt_tail(struct aead_request *req,
230 unsigned int flags)
Herbert Xue56dd562007-12-10 16:20:24 +0800231{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800232 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
233 struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xue56dd562007-12-10 16:20:24 +0800234 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800235 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
236 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
237 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
Herbert Xu7217d492016-07-12 13:17:34 +0800238 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
239 ictx->reqoff);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800240 unsigned int authsize = crypto_aead_authsize(authenc);
241 u8 *ihash = ahreq->result + authsize;
242 struct scatterlist *src, *dst;
Herbert Xue56dd562007-12-10 16:20:24 +0800243
Herbert Xu92d95ba2015-07-30 17:53:16 +0800244 scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
Herbert Xue56dd562007-12-10 16:20:24 +0800245
Herbert Xu92d95ba2015-07-30 17:53:16 +0800246 if (crypto_memneq(ihash, ahreq->result, authsize))
247 return -EBADMSG;
248
Herbert Xu92d95ba2015-07-30 17:53:16 +0800249 src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
250 dst = src;
251
Harsh Jainc34252f2016-06-29 00:24:43 +0530252 if (req->src != req->dst)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800253 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800254
Herbert Xu7217d492016-07-12 13:17:34 +0800255 skcipher_request_set_tfm(skreq, ctx->enc);
256 skcipher_request_set_callback(skreq, aead_request_flags(req),
257 req->base.complete, req->base.data);
258 skcipher_request_set_crypt(skreq, src, dst,
259 req->cryptlen - authsize, req->iv);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800260
Herbert Xu7217d492016-07-12 13:17:34 +0800261 return crypto_skcipher_decrypt(skreq);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800262}
263
264static void authenc_verify_ahash_done(struct crypto_async_request *areq,
265 int err)
266{
267 struct aead_request *req = areq->data;
268
Herbert Xue56dd562007-12-10 16:20:24 +0800269 if (err)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800270 goto out;
Herbert Xue56dd562007-12-10 16:20:24 +0800271
Herbert Xu92d95ba2015-07-30 17:53:16 +0800272 err = crypto_authenc_decrypt_tail(req, 0);
Herbert Xu3c09f172007-08-30 16:24:15 +0800273
Herbert Xu92d95ba2015-07-30 17:53:16 +0800274out:
275 authenc_request_complete(req, err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800276}
277
278static int crypto_authenc_decrypt(struct aead_request *req)
279{
280 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100281 unsigned int authsize = crypto_aead_authsize(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800282 struct aead_instance *inst = aead_alg_instance(authenc);
283 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
284 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
285 struct crypto_ahash *auth = ctx->auth;
286 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
287 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
288 u8 *hash = areq_ctx->tail;
Herbert Xu3c09f172007-08-30 16:24:15 +0800289 int err;
290
Herbert Xu92d95ba2015-07-30 17:53:16 +0800291 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
292 crypto_ahash_alignmask(auth) + 1);
Herbert Xu481f34a2007-12-04 20:04:21 +1100293
Herbert Xu92d95ba2015-07-30 17:53:16 +0800294 ahash_request_set_tfm(ahreq, auth);
295 ahash_request_set_crypt(ahreq, req->src, hash,
296 req->assoclen + req->cryptlen - authsize);
297 ahash_request_set_callback(ahreq, aead_request_flags(req),
298 authenc_verify_ahash_done, req);
299
300 err = crypto_ahash_digest(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800301 if (err)
302 return err;
303
Herbert Xu92d95ba2015-07-30 17:53:16 +0800304 return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
Herbert Xu3c09f172007-08-30 16:24:15 +0800305}
306
Herbert Xu92d95ba2015-07-30 17:53:16 +0800307static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
Herbert Xu3c09f172007-08-30 16:24:15 +0800308{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800309 struct aead_instance *inst = aead_alg_instance(tfm);
310 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
311 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000312 struct crypto_ahash *auth;
Herbert Xu7217d492016-07-12 13:17:34 +0800313 struct crypto_skcipher *enc;
Kees Cook8d605392018-09-18 19:10:51 -0700314 struct crypto_sync_skcipher *null;
Herbert Xu3c09f172007-08-30 16:24:15 +0800315 int err;
316
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000317 auth = crypto_spawn_ahash(&ictx->auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800318 if (IS_ERR(auth))
319 return PTR_ERR(auth);
320
Eric Biggers60425a82016-10-28 09:52:19 -0700321 enc = crypto_spawn_skcipher(&ictx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800322 err = PTR_ERR(enc);
323 if (IS_ERR(enc))
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000324 goto err_free_ahash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800325
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800326 null = crypto_get_default_null_skcipher();
Herbert Xu92d95ba2015-07-30 17:53:16 +0800327 err = PTR_ERR(null);
328 if (IS_ERR(null))
329 goto err_free_skcipher;
330
Herbert Xu3c09f172007-08-30 16:24:15 +0800331 ctx->auth = auth;
332 ctx->enc = enc;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800333 ctx->null = null;
Richard Hartmannf3542e62010-02-16 20:27:20 +0800334
Herbert Xu92d95ba2015-07-30 17:53:16 +0800335 crypto_aead_set_reqsize(
336 tfm,
Herbert Xu25df9192015-05-11 17:47:53 +0800337 sizeof(struct authenc_request_ctx) +
Herbert Xu92d95ba2015-07-30 17:53:16 +0800338 ictx->reqoff +
Herbert Xu25df9192015-05-11 17:47:53 +0800339 max_t(unsigned int,
Herbert Xu92d95ba2015-07-30 17:53:16 +0800340 crypto_ahash_reqsize(auth) +
341 sizeof(struct ahash_request),
Herbert Xu7217d492016-07-12 13:17:34 +0800342 sizeof(struct skcipher_request) +
343 crypto_skcipher_reqsize(enc)));
Herbert Xu3c09f172007-08-30 16:24:15 +0800344
345 return 0;
346
Herbert Xu92d95ba2015-07-30 17:53:16 +0800347err_free_skcipher:
Herbert Xu7217d492016-07-12 13:17:34 +0800348 crypto_free_skcipher(enc);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000349err_free_ahash:
350 crypto_free_ahash(auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800351 return err;
352}
353
Herbert Xu92d95ba2015-07-30 17:53:16 +0800354static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
Herbert Xu3c09f172007-08-30 16:24:15 +0800355{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800356 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu3c09f172007-08-30 16:24:15 +0800357
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000358 crypto_free_ahash(ctx->auth);
Herbert Xu7217d492016-07-12 13:17:34 +0800359 crypto_free_skcipher(ctx->enc);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800360 crypto_put_default_null_skcipher();
Herbert Xu3c09f172007-08-30 16:24:15 +0800361}
362
Herbert Xu92d95ba2015-07-30 17:53:16 +0800363static void crypto_authenc_free(struct aead_instance *inst)
364{
365 struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
366
367 crypto_drop_skcipher(&ctx->enc);
368 crypto_drop_ahash(&ctx->auth);
369 kfree(inst);
370}
371
372static int crypto_authenc_create(struct crypto_template *tmpl,
373 struct rtattr **tb)
Herbert Xu3c09f172007-08-30 16:24:15 +0800374{
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800375 u32 mask;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800376 struct aead_instance *inst;
Eric Biggers37a861a2020-01-02 19:58:55 -0800377 struct authenc_instance_ctx *ctx;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000378 struct hash_alg_common *auth;
379 struct crypto_alg *auth_base;
Herbert Xu7217d492016-07-12 13:17:34 +0800380 struct skcipher_alg *enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800381 int err;
382
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700383 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
384 if (err)
385 return err;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800386
Herbert Xu3c09f172007-08-30 16:24:15 +0800387 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Herbert Xu3c09f172007-08-30 16:24:15 +0800388 if (!inst)
Eric Biggers37a861a2020-01-02 19:58:55 -0800389 return -ENOMEM;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800390 ctx = aead_instance_ctx(inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800391
Eric Biggers37a861a2020-01-02 19:58:55 -0800392 err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst),
393 crypto_attr_alg_name(tb[1]), 0, mask);
Herbert Xu3c09f172007-08-30 16:24:15 +0800394 if (err)
395 goto err_free_inst;
Eric Biggers37a861a2020-01-02 19:58:55 -0800396 auth = crypto_spawn_ahash_alg(&ctx->auth);
397 auth_base = &auth->base;
Herbert Xu3c09f172007-08-30 16:24:15 +0800398
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800399 err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst),
Eric Biggers37a861a2020-01-02 19:58:55 -0800400 crypto_attr_alg_name(tb[2]), 0, mask);
Herbert Xu3c09f172007-08-30 16:24:15 +0800401 if (err)
Eric Biggers37a861a2020-01-02 19:58:55 -0800402 goto err_free_inst;
Herbert Xu7217d492016-07-12 13:17:34 +0800403 enc = crypto_spawn_skcipher_alg(&ctx->enc);
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800404
Herbert Xu92d95ba2015-07-30 17:53:16 +0800405 ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
406 auth_base->cra_alignmask + 1);
407
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800408 err = -ENAMETOOLONG;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800409 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu7217d492016-07-12 13:17:34 +0800410 "authenc(%s,%s)", auth_base->cra_name,
411 enc->base.cra_name) >=
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800412 CRYPTO_MAX_ALG_NAME)
Eric Biggers37a861a2020-01-02 19:58:55 -0800413 goto err_free_inst;
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800414
Herbert Xu92d95ba2015-07-30 17:53:16 +0800415 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000416 "authenc(%s,%s)", auth_base->cra_driver_name,
Herbert Xu7217d492016-07-12 13:17:34 +0800417 enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
Eric Biggers37a861a2020-01-02 19:58:55 -0800418 goto err_free_inst;
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800419
Herbert Xu7217d492016-07-12 13:17:34 +0800420 inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
Herbert Xu92d95ba2015-07-30 17:53:16 +0800421 auth_base->cra_priority;
Herbert Xu7217d492016-07-12 13:17:34 +0800422 inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800423 inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
Herbert Xu7217d492016-07-12 13:17:34 +0800424 enc->base.cra_alignmask;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800425 inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
Herbert Xu3c09f172007-08-30 16:24:15 +0800426
Herbert Xu7217d492016-07-12 13:17:34 +0800427 inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
428 inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800429 inst->alg.maxauthsize = auth->digestsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800430
Herbert Xu92d95ba2015-07-30 17:53:16 +0800431 inst->alg.init = crypto_authenc_init_tfm;
432 inst->alg.exit = crypto_authenc_exit_tfm;
Herbert Xu3c09f172007-08-30 16:24:15 +0800433
Herbert Xu92d95ba2015-07-30 17:53:16 +0800434 inst->alg.setkey = crypto_authenc_setkey;
435 inst->alg.encrypt = crypto_authenc_encrypt;
436 inst->alg.decrypt = crypto_authenc_decrypt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800437
Herbert Xu92d95ba2015-07-30 17:53:16 +0800438 inst->free = crypto_authenc_free;
439
440 err = aead_register_instance(tmpl, inst);
Eric Biggers37a861a2020-01-02 19:58:55 -0800441 if (err) {
Herbert Xu3c09f172007-08-30 16:24:15 +0800442err_free_inst:
Eric Biggers37a861a2020-01-02 19:58:55 -0800443 crypto_authenc_free(inst);
444 }
445 return err;
Herbert Xu3c09f172007-08-30 16:24:15 +0800446}
447
Herbert Xu3c09f172007-08-30 16:24:15 +0800448static struct crypto_template crypto_authenc_tmpl = {
449 .name = "authenc",
Herbert Xu92d95ba2015-07-30 17:53:16 +0800450 .create = crypto_authenc_create,
Herbert Xu3c09f172007-08-30 16:24:15 +0800451 .module = THIS_MODULE,
452};
453
454static int __init crypto_authenc_module_init(void)
455{
456 return crypto_register_template(&crypto_authenc_tmpl);
457}
458
459static void __exit crypto_authenc_module_exit(void)
460{
461 crypto_unregister_template(&crypto_authenc_tmpl);
462}
463
Eric Biggersc4741b22019-04-11 21:57:42 -0700464subsys_initcall(crypto_authenc_module_init);
Herbert Xu3c09f172007-08-30 16:24:15 +0800465module_exit(crypto_authenc_module_exit);
466
467MODULE_LICENSE("GPL");
468MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
Kees Cook4943ba12014-11-24 16:32:38 -0800469MODULE_ALIAS_CRYPTO("authenc");