hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/crypto/xcbc.c
....@@ -1,23 +1,12 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Copyright (C)2006 USAGI/WIDE Project
3
- *
4
- * This program is free software; you can redistribute it and/or modify
5
- * it under the terms of the GNU General Public License as published by
6
- * the Free Software Foundation; either version 2 of the License, or
7
- * (at your option) any later version.
8
- *
9
- * This program is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- * GNU General Public License for more details.
13
- *
14
- * You should have received a copy of the GNU General Public License
15
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
164 *
175 * Author:
186 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
197 */
208
9
+#include <crypto/internal/cipher.h>
2110 #include <crypto/internal/hash.h>
2211 #include <linux/err.h>
2312 #include <linux/kernel.h>
....@@ -57,15 +46,17 @@
5746 u8 ctx[];
5847 };
5948
49
+#define XCBC_BLOCKSIZE 16
50
+
6051 static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
6152 const u8 *inkey, unsigned int keylen)
6253 {
6354 unsigned long alignmask = crypto_shash_alignmask(parent);
6455 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
65
- int bs = crypto_shash_blocksize(parent);
6656 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
6757 int err = 0;
68
- u8 key1[bs];
58
+ u8 key1[XCBC_BLOCKSIZE];
59
+ int bs = sizeof(key1);
6960
7061 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
7162 return err;
....@@ -177,7 +168,7 @@
177168 {
178169 struct crypto_cipher *cipher;
179170 struct crypto_instance *inst = (void *)tfm->__crt_alg;
180
- struct crypto_spawn *spawn = crypto_instance_ctx(inst);
171
+ struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
181172 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
182173
183174 cipher = crypto_spawn_cipher(spawn);
....@@ -198,36 +189,34 @@
198189 static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
199190 {
200191 struct shash_instance *inst;
192
+ struct crypto_cipher_spawn *spawn;
201193 struct crypto_alg *alg;
202194 unsigned long alignmask;
195
+ u32 mask;
203196 int err;
204197
205
- err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
198
+ err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
206199 if (err)
207200 return err;
208201
209
- alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
210
- CRYPTO_ALG_TYPE_MASK);
211
- if (IS_ERR(alg))
212
- return PTR_ERR(alg);
202
+ inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
203
+ if (!inst)
204
+ return -ENOMEM;
205
+ spawn = shash_instance_ctx(inst);
213206
214
- switch(alg->cra_blocksize) {
215
- case 16:
216
- break;
217
- default:
218
- goto out_put_alg;
219
- }
220
-
221
- inst = shash_alloc_instance("xcbc", alg);
222
- err = PTR_ERR(inst);
223
- if (IS_ERR(inst))
224
- goto out_put_alg;
225
-
226
- err = crypto_init_spawn(shash_instance_ctx(inst), alg,
227
- shash_crypto_instance(inst),
228
- CRYPTO_ALG_TYPE_MASK);
207
+ err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
208
+ crypto_attr_alg_name(tb[1]), 0, mask);
229209 if (err)
230
- goto out_free_inst;
210
+ goto err_free_inst;
211
+ alg = crypto_spawn_cipher_alg(spawn);
212
+
213
+ err = -EINVAL;
214
+ if (alg->cra_blocksize != XCBC_BLOCKSIZE)
215
+ goto err_free_inst;
216
+
217
+ err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
218
+ if (err)
219
+ goto err_free_inst;
231220
232221 alignmask = alg->cra_alignmask | 3;
233222 inst->alg.base.cra_alignmask = alignmask;
....@@ -252,21 +241,19 @@
252241 inst->alg.final = crypto_xcbc_digest_final;
253242 inst->alg.setkey = crypto_xcbc_digest_setkey;
254243
244
+ inst->free = shash_free_singlespawn_instance;
245
+
255246 err = shash_register_instance(tmpl, inst);
256247 if (err) {
257
-out_free_inst:
258
- shash_free_instance(shash_crypto_instance(inst));
248
+err_free_inst:
249
+ shash_free_singlespawn_instance(inst);
259250 }
260
-
261
-out_put_alg:
262
- crypto_mod_put(alg);
263251 return err;
264252 }
265253
266254 static struct crypto_template crypto_xcbc_tmpl = {
267255 .name = "xcbc",
268256 .create = xcbc_create,
269
- .free = shash_free_instance,
270257 .module = THIS_MODULE,
271258 };
272259
....@@ -280,9 +267,10 @@
280267 crypto_unregister_template(&crypto_xcbc_tmpl);
281268 }
282269
283
-module_init(crypto_xcbc_module_init);
270
+subsys_initcall(crypto_xcbc_module_init);
284271 module_exit(crypto_xcbc_module_exit);
285272
286273 MODULE_LICENSE("GPL");
287274 MODULE_DESCRIPTION("XCBC keyed hash algorithm");
288275 MODULE_ALIAS_CRYPTO("xcbc");
276
+MODULE_IMPORT_NS(CRYPTO_INTERNAL);