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