hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/crypto/authenc.c
....@@ -1,13 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Authenc: Simple AEAD wrapper for IPsec
34 *
45 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License as published by the Free
8
- * Software Foundation; either version 2 of the License, or (at your option)
9
- * any later version.
10
- *
116 */
127
138 #include <crypto/internal/aead.h>
....@@ -33,7 +28,7 @@
3328 struct crypto_authenc_ctx {
3429 struct crypto_ahash *auth;
3530 struct crypto_skcipher *enc;
36
- struct crypto_skcipher *null;
31
+ struct crypto_sync_skcipher *null;
3732 };
3833
3934 struct authenc_request_ctx {
....@@ -96,15 +91,12 @@
9691 int err = -EINVAL;
9792
9893 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
99
- goto badkey;
94
+ goto out;
10095
10196 crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
10297 crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
10398 CRYPTO_TFM_REQ_MASK);
10499 err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
105
- crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
106
- CRYPTO_TFM_RES_MASK);
107
-
108100 if (err)
109101 goto out;
110102
....@@ -112,16 +104,9 @@
112104 crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
113105 CRYPTO_TFM_REQ_MASK);
114106 err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
115
- crypto_aead_set_flags(authenc, crypto_skcipher_get_flags(enc) &
116
- CRYPTO_TFM_RES_MASK);
117
-
118107 out:
119108 memzero_explicit(&keys, sizeof(keys));
120109 return err;
121
-
122
-badkey:
123
- crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
124
- goto out;
125110 }
126111
127112 static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
....@@ -193,9 +178,9 @@
193178 {
194179 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
195180 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
196
- SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
181
+ SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
197182
198
- skcipher_request_set_tfm(skreq, ctx->null);
183
+ skcipher_request_set_sync_tfm(skreq, ctx->null);
199184 skcipher_request_set_callback(skreq, aead_request_flags(req),
200185 NULL, NULL);
201186 skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen,
....@@ -268,7 +253,7 @@
268253 dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen);
269254
270255 skcipher_request_set_tfm(skreq, ctx->enc);
271
- skcipher_request_set_callback(skreq, aead_request_flags(req),
256
+ skcipher_request_set_callback(skreq, flags,
272257 req->base.complete, req->base.data);
273258 skcipher_request_set_crypt(skreq, src, dst,
274259 req->cryptlen - authsize, req->iv);
....@@ -326,7 +311,7 @@
326311 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm);
327312 struct crypto_ahash *auth;
328313 struct crypto_skcipher *enc;
329
- struct crypto_skcipher *null;
314
+ struct crypto_sync_skcipher *null;
330315 int err;
331316
332317 auth = crypto_spawn_ahash(&ictx->auth);
....@@ -387,54 +372,34 @@
387372 static int crypto_authenc_create(struct crypto_template *tmpl,
388373 struct rtattr **tb)
389374 {
390
- struct crypto_attr_type *algt;
375
+ u32 mask;
391376 struct aead_instance *inst;
377
+ struct authenc_instance_ctx *ctx;
392378 struct hash_alg_common *auth;
393379 struct crypto_alg *auth_base;
394380 struct skcipher_alg *enc;
395
- struct authenc_instance_ctx *ctx;
396
- const char *enc_name;
397381 int err;
398382
399
- algt = crypto_get_attr_type(tb);
400
- if (IS_ERR(algt))
401
- return PTR_ERR(algt);
402
-
403
- if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
404
- return -EINVAL;
405
-
406
- auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
407
- CRYPTO_ALG_TYPE_AHASH_MASK |
408
- crypto_requires_sync(algt->type, algt->mask));
409
- if (IS_ERR(auth))
410
- return PTR_ERR(auth);
411
-
412
- auth_base = &auth->base;
413
-
414
- enc_name = crypto_attr_alg_name(tb[2]);
415
- err = PTR_ERR(enc_name);
416
- if (IS_ERR(enc_name))
417
- goto out_put_auth;
383
+ err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
384
+ if (err)
385
+ return err;
418386
419387 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
420
- err = -ENOMEM;
421388 if (!inst)
422
- goto out_put_auth;
423
-
389
+ return -ENOMEM;
424390 ctx = aead_instance_ctx(inst);
425391
426
- err = crypto_init_ahash_spawn(&ctx->auth, auth,
427
- aead_crypto_instance(inst));
392
+ err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst),
393
+ crypto_attr_alg_name(tb[1]), 0, mask);
428394 if (err)
429395 goto err_free_inst;
396
+ auth = crypto_spawn_ahash_alg(&ctx->auth);
397
+ auth_base = &auth->base;
430398
431
- crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
432
- err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
433
- crypto_requires_sync(algt->type,
434
- algt->mask));
399
+ err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst),
400
+ crypto_attr_alg_name(tb[2]), 0, mask);
435401 if (err)
436
- goto err_drop_auth;
437
-
402
+ goto err_free_inst;
438403 enc = crypto_spawn_skcipher_alg(&ctx->enc);
439404
440405 ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask,
....@@ -445,15 +410,13 @@
445410 "authenc(%s,%s)", auth_base->cra_name,
446411 enc->base.cra_name) >=
447412 CRYPTO_MAX_ALG_NAME)
448
- goto err_drop_enc;
413
+ goto err_free_inst;
449414
450415 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
451416 "authenc(%s,%s)", auth_base->cra_driver_name,
452417 enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
453
- goto err_drop_enc;
418
+ goto err_free_inst;
454419
455
- inst->alg.base.cra_flags = (auth_base->cra_flags |
456
- enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
457420 inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
458421 auth_base->cra_priority;
459422 inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
....@@ -475,21 +438,11 @@
475438 inst->free = crypto_authenc_free;
476439
477440 err = aead_register_instance(tmpl, inst);
478
- if (err)
479
- goto err_drop_enc;
480
-
481
-out:
482
- crypto_mod_put(auth_base);
483
- return err;
484
-
485
-err_drop_enc:
486
- crypto_drop_skcipher(&ctx->enc);
487
-err_drop_auth:
488
- crypto_drop_ahash(&ctx->auth);
441
+ if (err) {
489442 err_free_inst:
490
- kfree(inst);
491
-out_put_auth:
492
- goto out;
443
+ crypto_authenc_free(inst);
444
+ }
445
+ return err;
493446 }
494447
495448 static struct crypto_template crypto_authenc_tmpl = {
....@@ -508,7 +461,7 @@
508461 crypto_unregister_template(&crypto_authenc_tmpl);
509462 }
510463
511
-module_init(crypto_authenc_module_init);
464
+subsys_initcall(crypto_authenc_module_init);
512465 module_exit(crypto_authenc_module_exit);
513466
514467 MODULE_LICENSE("GPL");