| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * CMAC: Cipher Block Mode for Authentication |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 8 | 9 | * Based on crypto/xcbc.c: |
|---|
| 9 | 10 | * Copyright © 2006 USAGI/WIDE Project, |
|---|
| 10 | 11 | * 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 | | - * |
|---|
| 17 | 12 | */ |
|---|
| 18 | 13 | |
|---|
| 14 | +#include <crypto/internal/cipher.h> |
|---|
| 19 | 15 | #include <crypto/internal/hash.h> |
|---|
| 20 | 16 | #include <linux/err.h> |
|---|
| 21 | 17 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 206 | 202 | { |
|---|
| 207 | 203 | struct crypto_cipher *cipher; |
|---|
| 208 | 204 | 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); |
|---|
| 210 | 206 | struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
|---|
| 211 | 207 | |
|---|
| 212 | 208 | cipher = crypto_spawn_cipher(spawn); |
|---|
| .. | .. |
|---|
| 227 | 223 | static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb) |
|---|
| 228 | 224 | { |
|---|
| 229 | 225 | struct shash_instance *inst; |
|---|
| 226 | + struct crypto_cipher_spawn *spawn; |
|---|
| 230 | 227 | struct crypto_alg *alg; |
|---|
| 231 | 228 | unsigned long alignmask; |
|---|
| 229 | + u32 mask; |
|---|
| 232 | 230 | int err; |
|---|
| 233 | 231 | |
|---|
| 234 | | - err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH); |
|---|
| 232 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask); |
|---|
| 235 | 233 | if (err) |
|---|
| 236 | 234 | return err; |
|---|
| 237 | 235 | |
|---|
| 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); |
|---|
| 242 | 246 | |
|---|
| 243 | 247 | switch (alg->cra_blocksize) { |
|---|
| 244 | 248 | case 16: |
|---|
| .. | .. |
|---|
| 246 | 250 | break; |
|---|
| 247 | 251 | default: |
|---|
| 248 | 252 | err = -EINVAL; |
|---|
| 249 | | - goto out_put_alg; |
|---|
| 253 | + goto err_free_inst; |
|---|
| 250 | 254 | } |
|---|
| 251 | 255 | |
|---|
| 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); |
|---|
| 260 | 257 | if (err) |
|---|
| 261 | | - goto out_free_inst; |
|---|
| 258 | + goto err_free_inst; |
|---|
| 262 | 259 | |
|---|
| 263 | 260 | alignmask = alg->cra_alignmask; |
|---|
| 264 | 261 | inst->alg.base.cra_alignmask = alignmask; |
|---|
| .. | .. |
|---|
| 285 | 282 | inst->alg.final = crypto_cmac_digest_final; |
|---|
| 286 | 283 | inst->alg.setkey = crypto_cmac_digest_setkey; |
|---|
| 287 | 284 | |
|---|
| 285 | + inst->free = shash_free_singlespawn_instance; |
|---|
| 286 | + |
|---|
| 288 | 287 | err = shash_register_instance(tmpl, inst); |
|---|
| 289 | 288 | if (err) { |
|---|
| 290 | | -out_free_inst: |
|---|
| 291 | | - shash_free_instance(shash_crypto_instance(inst)); |
|---|
| 289 | +err_free_inst: |
|---|
| 290 | + shash_free_singlespawn_instance(inst); |
|---|
| 292 | 291 | } |
|---|
| 293 | | - |
|---|
| 294 | | -out_put_alg: |
|---|
| 295 | | - crypto_mod_put(alg); |
|---|
| 296 | 292 | return err; |
|---|
| 297 | 293 | } |
|---|
| 298 | 294 | |
|---|
| 299 | 295 | static struct crypto_template crypto_cmac_tmpl = { |
|---|
| 300 | 296 | .name = "cmac", |
|---|
| 301 | 297 | .create = cmac_create, |
|---|
| 302 | | - .free = shash_free_instance, |
|---|
| 303 | 298 | .module = THIS_MODULE, |
|---|
| 304 | 299 | }; |
|---|
| 305 | 300 | |
|---|
| .. | .. |
|---|
| 313 | 308 | crypto_unregister_template(&crypto_cmac_tmpl); |
|---|
| 314 | 309 | } |
|---|
| 315 | 310 | |
|---|
| 316 | | -module_init(crypto_cmac_module_init); |
|---|
| 311 | +subsys_initcall(crypto_cmac_module_init); |
|---|
| 317 | 312 | module_exit(crypto_cmac_module_exit); |
|---|
| 318 | 313 | |
|---|
| 319 | 314 | MODULE_LICENSE("GPL"); |
|---|
| 320 | 315 | MODULE_DESCRIPTION("CMAC keyed hash algorithm"); |
|---|
| 321 | 316 | MODULE_ALIAS_CRYPTO("cmac"); |
|---|
| 317 | +MODULE_IMPORT_NS(CRYPTO_INTERNAL); |
|---|