blob: 3f0ed9402582026dab4d96e99b40a42fef0020c7 [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)
94 goto badkey;
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);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000100 crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
Herbert Xu3c09f172007-08-30 16:24:15 +0800101 CRYPTO_TFM_RES_MASK);
102
103 if (err)
104 goto out;
105
Herbert Xu7217d492016-07-12 13:17:34 +0800106 crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
107 crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
108 CRYPTO_TFM_REQ_MASK);
109 err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
110 crypto_aead_set_flags(authenc, crypto_skcipher_get_flags(enc) &
Herbert Xu3c09f172007-08-30 16:24:15 +0800111 CRYPTO_TFM_RES_MASK);
112
113out:
Tudor-Dan Ambarusad2fdcd2018-04-03 09:39:00 +0300114 memzero_explicit(&keys, sizeof(keys));
Herbert Xu3c09f172007-08-30 16:24:15 +0800115 return err;
Herbert Xue236d4a2007-11-22 23:11:53 +0800116
117badkey:
118 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
119 goto out;
Herbert Xu3c09f172007-08-30 16:24:15 +0800120}
121
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000122static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
123{
124 struct aead_request *req = areq->data;
125 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800126 struct aead_instance *inst = aead_alg_instance(authenc);
127 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000128 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800129 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000130
131 if (err)
132 goto out;
133
Herbert Xu92d95ba2015-07-30 17:53:16 +0800134 scatterwalk_map_and_copy(ahreq->result, req->dst,
135 req->assoclen + req->cryptlen,
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000136 crypto_aead_authsize(authenc), 1);
137
138out:
139 aead_request_complete(req, err);
140}
141
Herbert Xu92d95ba2015-07-30 17:53:16 +0800142static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags)
Herbert Xu3c09f172007-08-30 16:24:15 +0800143{
144 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800145 struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800146 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800147 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000148 struct crypto_ahash *auth = ctx->auth;
149 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800150 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000151 u8 *hash = areq_ctx->tail;
152 int err;
153
154 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
155 crypto_ahash_alignmask(auth) + 1);
156
157 ahash_request_set_tfm(ahreq, auth);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800158 ahash_request_set_crypt(ahreq, req->dst, hash,
159 req->assoclen + req->cryptlen);
160 ahash_request_set_callback(ahreq, flags,
161 authenc_geniv_ahash_done, req);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000162
163 err = crypto_ahash_digest(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800164 if (err)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800165 return err;
Herbert Xu7c3d7032007-12-10 16:15:41 +0800166
Herbert Xu92d95ba2015-07-30 17:53:16 +0800167 scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen,
Herbert Xu7ba683a2007-12-02 18:49:21 +1100168 crypto_aead_authsize(authenc), 1);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800169
Herbert Xu3c09f172007-08-30 16:24:15 +0800170 return 0;
171}
172
173static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
174 int err)
175{
Herbert Xua6976902008-08-23 01:04:06 +1000176 struct aead_request *areq = req->data;
177
Herbert Xu92d95ba2015-07-30 17:53:16 +0800178 if (err)
179 goto out;
Herbert Xue56dd562007-12-10 16:20:24 +0800180
Herbert Xu92d95ba2015-07-30 17:53:16 +0800181 err = crypto_authenc_genicv(areq, 0);
Herbert Xu3c09f172007-08-30 16:24:15 +0800182
Herbert Xu92d95ba2015-07-30 17:53:16 +0800183out:
Herbert Xu180ce7e2010-04-26 09:14:05 +0800184 authenc_request_complete(areq, err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800185}
186
Herbert Xu92d95ba2015-07-30 17:53:16 +0800187static int crypto_authenc_copy_assoc(struct aead_request *req)
188{
189 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
190 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Kees Cook8d605392018-09-18 19:10:51 -0700191 SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800192
Kees Cook8d605392018-09-18 19:10:51 -0700193 skcipher_request_set_sync_tfm(skreq, ctx->null);
Herbert Xu7217d492016-07-12 13:17:34 +0800194 skcipher_request_set_callback(skreq, aead_request_flags(req),
195 NULL, NULL);
196 skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
197 NULL);
198
199 return crypto_skcipher_encrypt(skreq);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800200}
201
Herbert Xu3c09f172007-08-30 16:24:15 +0800202static int crypto_authenc_encrypt(struct aead_request *req)
203{
204 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800205 struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800206 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800207 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
Steffen Klassert50beceb2010-03-03 22:41:08 +0800208 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu7217d492016-07-12 13:17:34 +0800209 struct crypto_skcipher *enc = ctx->enc;
Herbert Xue56dd562007-12-10 16:20:24 +0800210 unsigned int cryptlen = req->cryptlen;
Herbert Xu7217d492016-07-12 13:17:34 +0800211 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
212 ictx->reqoff);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800213 struct scatterlist *src, *dst;
Herbert Xu3c09f172007-08-30 16:24:15 +0800214 int err;
215
Herbert Xu92d95ba2015-07-30 17:53:16 +0800216 src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
217 dst = src;
218
219 if (req->src != req->dst) {
220 err = crypto_authenc_copy_assoc(req);
221 if (err)
222 return err;
223
Herbert Xu92d95ba2015-07-30 17:53:16 +0800224 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
225 }
226
Herbert Xu7217d492016-07-12 13:17:34 +0800227 skcipher_request_set_tfm(skreq, enc);
228 skcipher_request_set_callback(skreq, aead_request_flags(req),
229 crypto_authenc_encrypt_done, req);
230 skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
Herbert Xu3c09f172007-08-30 16:24:15 +0800231
Herbert Xu7217d492016-07-12 13:17:34 +0800232 err = crypto_skcipher_encrypt(skreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800233 if (err)
234 return err;
235
Herbert Xu92d95ba2015-07-30 17:53:16 +0800236 return crypto_authenc_genicv(req, aead_request_flags(req));
Herbert Xue56dd562007-12-10 16:20:24 +0800237}
238
Herbert Xu92d95ba2015-07-30 17:53:16 +0800239static int crypto_authenc_decrypt_tail(struct aead_request *req,
240 unsigned int flags)
Herbert Xue56dd562007-12-10 16:20:24 +0800241{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800242 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
243 struct aead_instance *inst = aead_alg_instance(authenc);
Herbert Xue56dd562007-12-10 16:20:24 +0800244 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800245 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
246 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
247 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
Herbert Xu7217d492016-07-12 13:17:34 +0800248 struct skcipher_request *skreq = (void *)(areq_ctx->tail +
249 ictx->reqoff);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800250 unsigned int authsize = crypto_aead_authsize(authenc);
251 u8 *ihash = ahreq->result + authsize;
252 struct scatterlist *src, *dst;
Herbert Xue56dd562007-12-10 16:20:24 +0800253
Herbert Xu92d95ba2015-07-30 17:53:16 +0800254 scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0);
Herbert Xue56dd562007-12-10 16:20:24 +0800255
Herbert Xu92d95ba2015-07-30 17:53:16 +0800256 if (crypto_memneq(ihash, ahreq->result, authsize))
257 return -EBADMSG;
258
Herbert Xu92d95ba2015-07-30 17:53:16 +0800259 src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen);
260 dst = src;
261
Harsh Jainc34252f2016-06-29 00:24:43 +0530262 if (req->src != req->dst)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800263 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800264
Herbert Xu7217d492016-07-12 13:17:34 +0800265 skcipher_request_set_tfm(skreq, ctx->enc);
266 skcipher_request_set_callback(skreq, aead_request_flags(req),
267 req->base.complete, req->base.data);
268 skcipher_request_set_crypt(skreq, src, dst,
269 req->cryptlen - authsize, req->iv);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800270
Herbert Xu7217d492016-07-12 13:17:34 +0800271 return crypto_skcipher_decrypt(skreq);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800272}
273
274static void authenc_verify_ahash_done(struct crypto_async_request *areq,
275 int err)
276{
277 struct aead_request *req = areq->data;
278
Herbert Xue56dd562007-12-10 16:20:24 +0800279 if (err)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800280 goto out;
Herbert Xue56dd562007-12-10 16:20:24 +0800281
Herbert Xu92d95ba2015-07-30 17:53:16 +0800282 err = crypto_authenc_decrypt_tail(req, 0);
Herbert Xu3c09f172007-08-30 16:24:15 +0800283
Herbert Xu92d95ba2015-07-30 17:53:16 +0800284out:
285 authenc_request_complete(req, err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800286}
287
288static int crypto_authenc_decrypt(struct aead_request *req)
289{
290 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100291 unsigned int authsize = crypto_aead_authsize(authenc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800292 struct aead_instance *inst = aead_alg_instance(authenc);
293 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
294 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
295 struct crypto_ahash *auth = ctx->auth;
296 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
297 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff);
298 u8 *hash = areq_ctx->tail;
Herbert Xu3c09f172007-08-30 16:24:15 +0800299 int err;
300
Herbert Xu92d95ba2015-07-30 17:53:16 +0800301 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
302 crypto_ahash_alignmask(auth) + 1);
Herbert Xu481f34a2007-12-04 20:04:21 +1100303
Herbert Xu92d95ba2015-07-30 17:53:16 +0800304 ahash_request_set_tfm(ahreq, auth);
305 ahash_request_set_crypt(ahreq, req->src, hash,
306 req->assoclen + req->cryptlen - authsize);
307 ahash_request_set_callback(ahreq, aead_request_flags(req),
308 authenc_verify_ahash_done, req);
309
310 err = crypto_ahash_digest(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800311 if (err)
312 return err;
313
Herbert Xu92d95ba2015-07-30 17:53:16 +0800314 return crypto_authenc_decrypt_tail(req, aead_request_flags(req));
Herbert Xu3c09f172007-08-30 16:24:15 +0800315}
316
Herbert Xu92d95ba2015-07-30 17:53:16 +0800317static int crypto_authenc_init_tfm(struct crypto_aead *tfm)
Herbert Xu3c09f172007-08-30 16:24:15 +0800318{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800319 struct aead_instance *inst = aead_alg_instance(tfm);
320 struct authenc_instance_ctx *ictx = aead_instance_ctx(inst);
321 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000322 struct crypto_ahash *auth;
Herbert Xu7217d492016-07-12 13:17:34 +0800323 struct crypto_skcipher *enc;
Kees Cook8d605392018-09-18 19:10:51 -0700324 struct crypto_sync_skcipher *null;
Herbert Xu3c09f172007-08-30 16:24:15 +0800325 int err;
326
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000327 auth = crypto_spawn_ahash(&ictx->auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800328 if (IS_ERR(auth))
329 return PTR_ERR(auth);
330
Eric Biggers60425a82016-10-28 09:52:19 -0700331 enc = crypto_spawn_skcipher(&ictx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800332 err = PTR_ERR(enc);
333 if (IS_ERR(enc))
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000334 goto err_free_ahash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800335
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800336 null = crypto_get_default_null_skcipher();
Herbert Xu92d95ba2015-07-30 17:53:16 +0800337 err = PTR_ERR(null);
338 if (IS_ERR(null))
339 goto err_free_skcipher;
340
Herbert Xu3c09f172007-08-30 16:24:15 +0800341 ctx->auth = auth;
342 ctx->enc = enc;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800343 ctx->null = null;
Richard Hartmannf3542e62010-02-16 20:27:20 +0800344
Herbert Xu92d95ba2015-07-30 17:53:16 +0800345 crypto_aead_set_reqsize(
346 tfm,
Herbert Xu25df9192015-05-11 17:47:53 +0800347 sizeof(struct authenc_request_ctx) +
Herbert Xu92d95ba2015-07-30 17:53:16 +0800348 ictx->reqoff +
Herbert Xu25df9192015-05-11 17:47:53 +0800349 max_t(unsigned int,
Herbert Xu92d95ba2015-07-30 17:53:16 +0800350 crypto_ahash_reqsize(auth) +
351 sizeof(struct ahash_request),
Herbert Xu7217d492016-07-12 13:17:34 +0800352 sizeof(struct skcipher_request) +
353 crypto_skcipher_reqsize(enc)));
Herbert Xu3c09f172007-08-30 16:24:15 +0800354
355 return 0;
356
Herbert Xu92d95ba2015-07-30 17:53:16 +0800357err_free_skcipher:
Herbert Xu7217d492016-07-12 13:17:34 +0800358 crypto_free_skcipher(enc);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000359err_free_ahash:
360 crypto_free_ahash(auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800361 return err;
362}
363
Herbert Xu92d95ba2015-07-30 17:53:16 +0800364static void crypto_authenc_exit_tfm(struct crypto_aead *tfm)
Herbert Xu3c09f172007-08-30 16:24:15 +0800365{
Herbert Xu92d95ba2015-07-30 17:53:16 +0800366 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu3c09f172007-08-30 16:24:15 +0800367
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000368 crypto_free_ahash(ctx->auth);
Herbert Xu7217d492016-07-12 13:17:34 +0800369 crypto_free_skcipher(ctx->enc);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800370 crypto_put_default_null_skcipher();
Herbert Xu3c09f172007-08-30 16:24:15 +0800371}
372
Herbert Xu92d95ba2015-07-30 17:53:16 +0800373static void crypto_authenc_free(struct aead_instance *inst)
374{
375 struct authenc_instance_ctx *ctx = aead_instance_ctx(inst);
376
377 crypto_drop_skcipher(&ctx->enc);
378 crypto_drop_ahash(&ctx->auth);
379 kfree(inst);
380}
381
382static int crypto_authenc_create(struct crypto_template *tmpl,
383 struct rtattr **tb)
Herbert Xu3c09f172007-08-30 16:24:15 +0800384{
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800385 struct crypto_attr_type *algt;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800386 struct aead_instance *inst;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000387 struct hash_alg_common *auth;
388 struct crypto_alg *auth_base;
Herbert Xu7217d492016-07-12 13:17:34 +0800389 struct skcipher_alg *enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800390 struct authenc_instance_ctx *ctx;
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800391 const char *enc_name;
Herbert Xu3c09f172007-08-30 16:24:15 +0800392 int err;
393
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800394 algt = crypto_get_attr_type(tb);
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800395 if (IS_ERR(algt))
Herbert Xu92d95ba2015-07-30 17:53:16 +0800396 return PTR_ERR(algt);
Herbert Xu3c09f172007-08-30 16:24:15 +0800397
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800398 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xu92d95ba2015-07-30 17:53:16 +0800399 return -EINVAL;
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800400
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000401 auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
Herbert Xu927ef322016-06-29 18:03:46 +0800402 CRYPTO_ALG_TYPE_AHASH_MASK |
403 crypto_requires_sync(algt->type, algt->mask));
Herbert Xu3c09f172007-08-30 16:24:15 +0800404 if (IS_ERR(auth))
Herbert Xu92d95ba2015-07-30 17:53:16 +0800405 return PTR_ERR(auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800406
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000407 auth_base = &auth->base;
408
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800409 enc_name = crypto_attr_alg_name(tb[2]);
410 err = PTR_ERR(enc_name);
411 if (IS_ERR(enc_name))
Herbert Xu3c09f172007-08-30 16:24:15 +0800412 goto out_put_auth;
413
Herbert Xu3c09f172007-08-30 16:24:15 +0800414 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
415 err = -ENOMEM;
416 if (!inst)
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800417 goto out_put_auth;
Herbert Xue236d4a2007-11-22 23:11:53 +0800418
Herbert Xu92d95ba2015-07-30 17:53:16 +0800419 ctx = aead_instance_ctx(inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800420
Herbert Xu92d95ba2015-07-30 17:53:16 +0800421 err = crypto_init_ahash_spawn(&ctx->auth, auth,
422 aead_crypto_instance(inst));
Herbert Xu3c09f172007-08-30 16:24:15 +0800423 if (err)
424 goto err_free_inst;
425
Herbert Xu92d95ba2015-07-30 17:53:16 +0800426 crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
Eric Biggersa35528e2016-10-28 09:51:13 -0700427 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
428 crypto_requires_sync(algt->type,
429 algt->mask));
Herbert Xu3c09f172007-08-30 16:24:15 +0800430 if (err)
431 goto err_drop_auth;
432
Herbert Xu7217d492016-07-12 13:17:34 +0800433 enc = crypto_spawn_skcipher_alg(&ctx->enc);
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800434
Herbert Xu92d95ba2015-07-30 17:53:16 +0800435 ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
436 auth_base->cra_alignmask + 1);
437
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800438 err = -ENAMETOOLONG;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800439 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu7217d492016-07-12 13:17:34 +0800440 "authenc(%s,%s)", auth_base->cra_name,
441 enc->base.cra_name) >=
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800442 CRYPTO_MAX_ALG_NAME)
443 goto err_drop_enc;
444
Herbert Xu92d95ba2015-07-30 17:53:16 +0800445 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000446 "authenc(%s,%s)", auth_base->cra_driver_name,
Herbert Xu7217d492016-07-12 13:17:34 +0800447 enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800448 goto err_drop_enc;
449
Herbert Xu7217d492016-07-12 13:17:34 +0800450 inst->alg.base.cra_flags = (auth_base->cra_flags |
451 enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
452 inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
Herbert Xu92d95ba2015-07-30 17:53:16 +0800453 auth_base->cra_priority;
Herbert Xu7217d492016-07-12 13:17:34 +0800454 inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800455 inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
Herbert Xu7217d492016-07-12 13:17:34 +0800456 enc->base.cra_alignmask;
Herbert Xu92d95ba2015-07-30 17:53:16 +0800457 inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
Herbert Xu3c09f172007-08-30 16:24:15 +0800458
Herbert Xu7217d492016-07-12 13:17:34 +0800459 inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
460 inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800461 inst->alg.maxauthsize = auth->digestsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800462
Herbert Xu92d95ba2015-07-30 17:53:16 +0800463 inst->alg.init = crypto_authenc_init_tfm;
464 inst->alg.exit = crypto_authenc_exit_tfm;
Herbert Xu3c09f172007-08-30 16:24:15 +0800465
Herbert Xu92d95ba2015-07-30 17:53:16 +0800466 inst->alg.setkey = crypto_authenc_setkey;
467 inst->alg.encrypt = crypto_authenc_encrypt;
468 inst->alg.decrypt = crypto_authenc_decrypt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800469
Herbert Xu92d95ba2015-07-30 17:53:16 +0800470 inst->free = crypto_authenc_free;
471
472 err = aead_register_instance(tmpl, inst);
473 if (err)
474 goto err_drop_enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800475
476out:
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000477 crypto_mod_put(auth_base);
Herbert Xu92d95ba2015-07-30 17:53:16 +0800478 return err;
Herbert Xu3c09f172007-08-30 16:24:15 +0800479
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800480err_drop_enc:
481 crypto_drop_skcipher(&ctx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800482err_drop_auth:
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000483 crypto_drop_ahash(&ctx->auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800484err_free_inst:
485 kfree(inst);
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800486out_put_auth:
Herbert Xu3c09f172007-08-30 16:24:15 +0800487 goto out;
488}
489
Herbert Xu3c09f172007-08-30 16:24:15 +0800490static struct crypto_template crypto_authenc_tmpl = {
491 .name = "authenc",
Herbert Xu92d95ba2015-07-30 17:53:16 +0800492 .create = crypto_authenc_create,
Herbert Xu3c09f172007-08-30 16:24:15 +0800493 .module = THIS_MODULE,
494};
495
496static int __init crypto_authenc_module_init(void)
497{
498 return crypto_register_template(&crypto_authenc_tmpl);
499}
500
501static void __exit crypto_authenc_module_exit(void)
502{
503 crypto_unregister_template(&crypto_authenc_tmpl);
504}
505
Eric Biggersc4741b22019-04-11 21:57:42 -0700506subsys_initcall(crypto_authenc_module_init);
Herbert Xu3c09f172007-08-30 16:24:15 +0800507module_exit(crypto_authenc_module_exit);
508
509MODULE_LICENSE("GPL");
510MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
Kees Cook4943ba12014-11-24 16:32:38 -0800511MODULE_ALIAS_CRYPTO("authenc");