.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * AMD Cryptographic Coprocessor (CCP) AES CMAC crypto API support |
---|
3 | 4 | * |
---|
4 | | - * Copyright (C) 2013 Advanced Micro Devices, Inc. |
---|
| 5 | + * Copyright (C) 2013,2018 Advanced Micro Devices, Inc. |
---|
5 | 6 | * |
---|
6 | 7 | * Author: Tom Lendacky <thomas.lendacky@amd.com> |
---|
7 | | - * |
---|
8 | | - * This program is free software; you can redistribute it and/or modify |
---|
9 | | - * it under the terms of the GNU General Public License version 2 as |
---|
10 | | - * published by the Free Software Foundation. |
---|
11 | 8 | */ |
---|
12 | 9 | |
---|
13 | 10 | #include <linux/module.h> |
---|
.. | .. |
---|
264 | 261 | ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm)); |
---|
265 | 262 | u64 k0_hi, k0_lo, k1_hi, k1_lo, k2_hi, k2_lo; |
---|
266 | 263 | u64 rb_hi = 0x00, rb_lo = 0x87; |
---|
| 264 | + struct crypto_aes_ctx aes; |
---|
267 | 265 | __be64 *gk; |
---|
268 | 266 | int ret; |
---|
269 | 267 | |
---|
.. | .. |
---|
278 | 276 | ctx->u.aes.type = CCP_AES_TYPE_256; |
---|
279 | 277 | break; |
---|
280 | 278 | default: |
---|
281 | | - crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
---|
282 | 279 | return -EINVAL; |
---|
283 | 280 | } |
---|
284 | 281 | ctx->u.aes.mode = alg->mode; |
---|
.. | .. |
---|
287 | 284 | ctx->u.aes.key_len = 0; |
---|
288 | 285 | |
---|
289 | 286 | /* Set the key for the AES cipher used to generate the keys */ |
---|
290 | | - ret = crypto_cipher_setkey(ctx->u.aes.tfm_cipher, key, key_len); |
---|
| 287 | + ret = aes_expandkey(&aes, key, key_len); |
---|
291 | 288 | if (ret) |
---|
292 | 289 | return ret; |
---|
293 | 290 | |
---|
294 | 291 | /* Encrypt a block of zeroes - use key area in context */ |
---|
295 | 292 | memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key)); |
---|
296 | | - crypto_cipher_encrypt_one(ctx->u.aes.tfm_cipher, ctx->u.aes.key, |
---|
297 | | - ctx->u.aes.key); |
---|
| 293 | + aes_encrypt(&aes, ctx->u.aes.key, ctx->u.aes.key); |
---|
| 294 | + memzero_explicit(&aes, sizeof(aes)); |
---|
298 | 295 | |
---|
299 | 296 | /* Generate K1 and K2 */ |
---|
300 | 297 | k0_hi = be64_to_cpu(*((__be64 *)ctx->u.aes.key)); |
---|
.. | .. |
---|
339 | 336 | { |
---|
340 | 337 | struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); |
---|
341 | 338 | struct crypto_ahash *ahash = __crypto_ahash_cast(tfm); |
---|
342 | | - struct crypto_cipher *cipher_tfm; |
---|
343 | 339 | |
---|
344 | 340 | ctx->complete = ccp_aes_cmac_complete; |
---|
345 | 341 | ctx->u.aes.key_len = 0; |
---|
346 | 342 | |
---|
347 | 343 | crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_aes_cmac_req_ctx)); |
---|
348 | 344 | |
---|
349 | | - cipher_tfm = crypto_alloc_cipher("aes", 0, |
---|
350 | | - CRYPTO_ALG_ASYNC | |
---|
351 | | - CRYPTO_ALG_NEED_FALLBACK); |
---|
352 | | - if (IS_ERR(cipher_tfm)) { |
---|
353 | | - pr_warn("could not load aes cipher driver\n"); |
---|
354 | | - return PTR_ERR(cipher_tfm); |
---|
355 | | - } |
---|
356 | | - ctx->u.aes.tfm_cipher = cipher_tfm; |
---|
357 | | - |
---|
358 | 345 | return 0; |
---|
359 | | -} |
---|
360 | | - |
---|
361 | | -static void ccp_aes_cmac_cra_exit(struct crypto_tfm *tfm) |
---|
362 | | -{ |
---|
363 | | - struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); |
---|
364 | | - |
---|
365 | | - if (ctx->u.aes.tfm_cipher) |
---|
366 | | - crypto_free_cipher(ctx->u.aes.tfm_cipher); |
---|
367 | | - ctx->u.aes.tfm_cipher = NULL; |
---|
368 | 346 | } |
---|
369 | 347 | |
---|
370 | 348 | int ccp_register_aes_cmac_algs(struct list_head *head) |
---|
.. | .. |
---|
400 | 378 | snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "cmac(aes)"); |
---|
401 | 379 | snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "cmac-aes-ccp"); |
---|
402 | 380 | base->cra_flags = CRYPTO_ALG_ASYNC | |
---|
| 381 | + CRYPTO_ALG_ALLOCATES_MEMORY | |
---|
403 | 382 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
---|
404 | 383 | CRYPTO_ALG_NEED_FALLBACK; |
---|
405 | 384 | base->cra_blocksize = AES_BLOCK_SIZE; |
---|
406 | 385 | base->cra_ctxsize = sizeof(struct ccp_ctx); |
---|
407 | 386 | base->cra_priority = CCP_CRA_PRIORITY; |
---|
408 | 387 | base->cra_init = ccp_aes_cmac_cra_init; |
---|
409 | | - base->cra_exit = ccp_aes_cmac_cra_exit; |
---|
410 | 388 | base->cra_module = THIS_MODULE; |
---|
411 | 389 | |
---|
412 | 390 | ret = crypto_register_ahash(alg); |
---|