.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * 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/>. |
---|
16 | 4 | * |
---|
17 | 5 | * Author: |
---|
18 | 6 | * Kazunori Miyazawa <miyazawa@linux-ipv6.org> |
---|
19 | 7 | */ |
---|
20 | 8 | |
---|
| 9 | +#include <crypto/internal/cipher.h> |
---|
21 | 10 | #include <crypto/internal/hash.h> |
---|
22 | 11 | #include <linux/err.h> |
---|
23 | 12 | #include <linux/kernel.h> |
---|
.. | .. |
---|
57 | 46 | u8 ctx[]; |
---|
58 | 47 | }; |
---|
59 | 48 | |
---|
| 49 | +#define XCBC_BLOCKSIZE 16 |
---|
| 50 | + |
---|
60 | 51 | static int crypto_xcbc_digest_setkey(struct crypto_shash *parent, |
---|
61 | 52 | const u8 *inkey, unsigned int keylen) |
---|
62 | 53 | { |
---|
63 | 54 | unsigned long alignmask = crypto_shash_alignmask(parent); |
---|
64 | 55 | struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent); |
---|
65 | | - int bs = crypto_shash_blocksize(parent); |
---|
66 | 56 | u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1); |
---|
67 | 57 | int err = 0; |
---|
68 | | - u8 key1[bs]; |
---|
| 58 | + u8 key1[XCBC_BLOCKSIZE]; |
---|
| 59 | + int bs = sizeof(key1); |
---|
69 | 60 | |
---|
70 | 61 | if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen))) |
---|
71 | 62 | return err; |
---|
.. | .. |
---|
177 | 168 | { |
---|
178 | 169 | struct crypto_cipher *cipher; |
---|
179 | 170 | 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); |
---|
181 | 172 | struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
---|
182 | 173 | |
---|
183 | 174 | cipher = crypto_spawn_cipher(spawn); |
---|
.. | .. |
---|
198 | 189 | static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
---|
199 | 190 | { |
---|
200 | 191 | struct shash_instance *inst; |
---|
| 192 | + struct crypto_cipher_spawn *spawn; |
---|
201 | 193 | struct crypto_alg *alg; |
---|
202 | 194 | unsigned long alignmask; |
---|
| 195 | + u32 mask; |
---|
203 | 196 | int err; |
---|
204 | 197 | |
---|
205 | | - err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH); |
---|
| 198 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask); |
---|
206 | 199 | if (err) |
---|
207 | 200 | return err; |
---|
208 | 201 | |
---|
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); |
---|
213 | 206 | |
---|
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); |
---|
229 | 209 | 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; |
---|
231 | 220 | |
---|
232 | 221 | alignmask = alg->cra_alignmask | 3; |
---|
233 | 222 | inst->alg.base.cra_alignmask = alignmask; |
---|
.. | .. |
---|
252 | 241 | inst->alg.final = crypto_xcbc_digest_final; |
---|
253 | 242 | inst->alg.setkey = crypto_xcbc_digest_setkey; |
---|
254 | 243 | |
---|
| 244 | + inst->free = shash_free_singlespawn_instance; |
---|
| 245 | + |
---|
255 | 246 | err = shash_register_instance(tmpl, inst); |
---|
256 | 247 | if (err) { |
---|
257 | | -out_free_inst: |
---|
258 | | - shash_free_instance(shash_crypto_instance(inst)); |
---|
| 248 | +err_free_inst: |
---|
| 249 | + shash_free_singlespawn_instance(inst); |
---|
259 | 250 | } |
---|
260 | | - |
---|
261 | | -out_put_alg: |
---|
262 | | - crypto_mod_put(alg); |
---|
263 | 251 | return err; |
---|
264 | 252 | } |
---|
265 | 253 | |
---|
266 | 254 | static struct crypto_template crypto_xcbc_tmpl = { |
---|
267 | 255 | .name = "xcbc", |
---|
268 | 256 | .create = xcbc_create, |
---|
269 | | - .free = shash_free_instance, |
---|
270 | 257 | .module = THIS_MODULE, |
---|
271 | 258 | }; |
---|
272 | 259 | |
---|
.. | .. |
---|
280 | 267 | crypto_unregister_template(&crypto_xcbc_tmpl); |
---|
281 | 268 | } |
---|
282 | 269 | |
---|
283 | | -module_init(crypto_xcbc_module_init); |
---|
| 270 | +subsys_initcall(crypto_xcbc_module_init); |
---|
284 | 271 | module_exit(crypto_xcbc_module_exit); |
---|
285 | 272 | |
---|
286 | 273 | MODULE_LICENSE("GPL"); |
---|
287 | 274 | MODULE_DESCRIPTION("XCBC keyed hash algorithm"); |
---|
288 | 275 | MODULE_ALIAS_CRYPTO("xcbc"); |
---|
| 276 | +MODULE_IMPORT_NS(CRYPTO_INTERNAL); |
---|