hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/crypto/cmac.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * CMAC: Cipher Block Mode for Authentication
34 *
....@@ -8,14 +9,9 @@
89 * Based on crypto/xcbc.c:
910 * Copyright © 2006 USAGI/WIDE Project,
1011 * Author: Kazunori Miyazawa <miyazawa@linux-ipv6.org>
11
- *
12
- * This program is free software; you can redistribute it and/or modify
13
- * it under the terms of the GNU General Public License as published by
14
- * the Free Software Foundation; either version 2 of the License, or
15
- * (at your option) any later version.
16
- *
1712 */
1813
14
+#include <crypto/internal/cipher.h>
1915 #include <crypto/internal/hash.h>
2016 #include <linux/err.h>
2117 #include <linux/kernel.h>
....@@ -206,7 +202,7 @@
206202 {
207203 struct crypto_cipher *cipher;
208204 struct crypto_instance *inst = (void *)tfm->__crt_alg;
209
- struct crypto_spawn *spawn = crypto_instance_ctx(inst);
205
+ struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
210206 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
211207
212208 cipher = crypto_spawn_cipher(spawn);
....@@ -227,18 +223,26 @@
227223 static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
228224 {
229225 struct shash_instance *inst;
226
+ struct crypto_cipher_spawn *spawn;
230227 struct crypto_alg *alg;
231228 unsigned long alignmask;
229
+ u32 mask;
232230 int err;
233231
234
- err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
232
+ err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
235233 if (err)
236234 return err;
237235
238
- alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
239
- CRYPTO_ALG_TYPE_MASK);
240
- if (IS_ERR(alg))
241
- return PTR_ERR(alg);
236
+ inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
237
+ if (!inst)
238
+ return -ENOMEM;
239
+ spawn = shash_instance_ctx(inst);
240
+
241
+ err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
242
+ crypto_attr_alg_name(tb[1]), 0, mask);
243
+ if (err)
244
+ goto err_free_inst;
245
+ alg = crypto_spawn_cipher_alg(spawn);
242246
243247 switch (alg->cra_blocksize) {
244248 case 16:
....@@ -246,19 +250,12 @@
246250 break;
247251 default:
248252 err = -EINVAL;
249
- goto out_put_alg;
253
+ goto err_free_inst;
250254 }
251255
252
- inst = shash_alloc_instance("cmac", alg);
253
- err = PTR_ERR(inst);
254
- if (IS_ERR(inst))
255
- goto out_put_alg;
256
-
257
- err = crypto_init_spawn(shash_instance_ctx(inst), alg,
258
- shash_crypto_instance(inst),
259
- CRYPTO_ALG_TYPE_MASK);
256
+ err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
260257 if (err)
261
- goto out_free_inst;
258
+ goto err_free_inst;
262259
263260 alignmask = alg->cra_alignmask;
264261 inst->alg.base.cra_alignmask = alignmask;
....@@ -285,21 +282,19 @@
285282 inst->alg.final = crypto_cmac_digest_final;
286283 inst->alg.setkey = crypto_cmac_digest_setkey;
287284
285
+ inst->free = shash_free_singlespawn_instance;
286
+
288287 err = shash_register_instance(tmpl, inst);
289288 if (err) {
290
-out_free_inst:
291
- shash_free_instance(shash_crypto_instance(inst));
289
+err_free_inst:
290
+ shash_free_singlespawn_instance(inst);
292291 }
293
-
294
-out_put_alg:
295
- crypto_mod_put(alg);
296292 return err;
297293 }
298294
299295 static struct crypto_template crypto_cmac_tmpl = {
300296 .name = "cmac",
301297 .create = cmac_create,
302
- .free = shash_free_instance,
303298 .module = THIS_MODULE,
304299 };
305300
....@@ -313,9 +308,10 @@
313308 crypto_unregister_template(&crypto_cmac_tmpl);
314309 }
315310
316
-module_init(crypto_cmac_module_init);
311
+subsys_initcall(crypto_cmac_module_init);
317312 module_exit(crypto_cmac_module_exit);
318313
319314 MODULE_LICENSE("GPL");
320315 MODULE_DESCRIPTION("CMAC keyed hash algorithm");
321316 MODULE_ALIAS_CRYPTO("cmac");
317
+MODULE_IMPORT_NS(CRYPTO_INTERNAL);