crypto: hash - Preparation for block-only shash
Subsystems (8)
Herbert Xu
maintainercover crypto: hash - Preparation for block-only shash
This series is based on
https://lore.kernel.org/linux-crypto/[email protected]
Add helpers to use stack requests with any ahash algorithm, with optional dynamic allocation when required for async.
THe rest of the series are miscellaneous fixes in preparation for the next series which will introduce block-only shash.
Herbert Xu (8):
crypto: hash - Add HASH_REQUEST_ON_STACK
crypto: zynqmp-sha - Add locking
crypto: arm64/sha512 - Fix header inclusions
crypto: hash - Update HASH_MAX_DESCSIZE comment
crypto: lib/sm3 - Move sm3 library into lib/crypto
crypto: lib/sm3 - Export generic block function
crypto: sm3-base - Use sm3_init
crypto: cbcmac - Set block size properly
arch/arm64/crypto/Kconfig | 4 +-
arch/arm64/crypto/aes-glue.c | 2 +-
arch/arm64/crypto/sha512-glue.c | 5 +-
arch/arm64/crypto/sm4-ce-glue.c | 2 +-
arch/riscv/crypto/Kconfig | 2 +-
arch/x86/crypto/Kconfig | 2 +-
crypto/Kconfig | 5 +-
crypto/Makefile | 1 -
crypto/ahash.c | 106 ++++++++++++++++++-
crypto/ccm.c | 2 +-
drivers/crypto/inside-secure/safexcel_hash.c | 2 +-
drivers/crypto/xilinx/zynqmp-sha.c | 18 +++-
include/crypto/hash.h | 58 +++++++---
include/crypto/internal/hash.h | 26 ++++-
include/crypto/sm3.h | 1 +
include/crypto/sm3_base.h | 13 +--
lib/crypto/Kconfig | 3 +
lib/crypto/Makefile | 3 +
{crypto => lib/crypto}/sm3.c | 25 +++--
19 files changed, 218 insertions(+), 62 deletions(-)
rename {crypto => lib/crypto}/sm3.c (96%)Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 1/8 crypto: hash - Add HASH_REQUEST_ON_STACK
Allow any ahash to be used with a stack request, with optional dynamic allocation when async is needed. The intended usage is:
HASH_REQUEST_ON_STACK(req, tfm);
...
err = crypto_ahash_digest(req);
/* The request cannot complete synchronously. */
if (err == -EAGAIN) {
/* This will not fail. */
req = HASH_REQUEST_CLONE(req, gfp);
/* Redo operation. */
err = crypto_ahash_digest(req);
}--- crypto/ahash.c | 106 +++++++++++++++++++++++++++++++-- include/crypto/hash.h | 54 +++++++++++++---- include/crypto/internal/hash.h | 26 +++++++- 3 files changed, 171 insertions(+), 15 deletions(-)
crypto/ahash.c +102 -4
@@ -300,6 +300,8 @@ int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,int err; err = alg->setkey (tfm, key, keylen); +if (!err &&ahash_is_async (tfm)) + err =crypto_ahash_setkey (tfm->fb, key, keylen);if (unlikely (err)) {ahash_set_needkey (tfm, alg);return err;
@@ -473,6 +475,8 @@ int crypto_ahash_init(struct ahash_request *req)return crypto_shash_init (prepare_shash_desc (req, tfm));if (crypto_ahash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY )return -ENOKEY ; +if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +return -EAGAIN ;return ahash_do_req_chain (req,crypto_ahash_alg (tfm)->init); }EXPORT_SYMBOL_GPL (crypto_ahash_init);
@@ -520,6 +524,8 @@ int crypto_ahash_update(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_update (req,ahash_request_ctx (req)); +if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +return -EAGAIN ;return ahash_do_req_chain (req,crypto_ahash_alg (tfm)->update); }EXPORT_SYMBOL_GPL (crypto_ahash_update);
@@ -530,6 +536,8 @@ int crypto_ahash_final(struct ahash_request *req)if (likely (tfm->using_shash))return crypto_shash_final (ahash_request_ctx (req), req->result); +if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +return -EAGAIN ;return ahash_do_req_chain (req,crypto_ahash_alg (tfm)->final); }EXPORT_SYMBOL_GPL (crypto_ahash_final);
@@ -540,6 +548,8 @@ int crypto_ahash_finup(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_finup (req,ahash_request_ctx (req)); +if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +return -EAGAIN ;if (!crypto_ahash_alg (tfm)->finup || (!crypto_ahash_req_chain (tfm) &&ahash_request_isvirt (req)))return ahash_def_finup (req);
@@ -611,6 +621,8 @@ int crypto_ahash_digest(struct ahash_request *req)if (likely (tfm->using_shash))return shash_ahash_digest (req,prepare_shash_desc (req, tfm)); +if (ahash_req_on_stack (req) &&ahash_is_async (tfm)) +return -EAGAIN ;if (!crypto_ahash_req_chain (tfm) &&ahash_request_isvirt (req))return ahash_def_digest (req);if (crypto_ahash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY )
@@ -714,26 +726,63 @@ static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm)struct crypto_ahash *hash =__crypto_ahash_cast (tfm);struct ahash_alg *alg =crypto_ahash_alg (hash); - alg->exit_tfm (hash); +if (alg->exit_tfm) + alg->exit_tfm (hash); +else if (tfm->__crt_alg->cra_exit) + tfm->__crt_alg->cra_exit (tfm); + +if (ahash_is_async (hash)) +crypto_free_ahash (hash->fb); }static int crypto_ahash_init_tfm (struct crypto_tfm *tfm) {struct crypto_ahash *hash =__crypto_ahash_cast (tfm);struct ahash_alg *alg =crypto_ahash_alg (hash); +struct crypto_ahash *fb =NULL ; +int err;crypto_ahash_set_statesize (hash, alg->halg.statesize);crypto_ahash_set_reqsize (hash,crypto_tfm_alg_reqsize (tfm)); + hash->fb = hash; +if (tfm->__crt_alg->cra_type == &crypto_shash_type)return crypto_init_ahash_using_shash (tfm); +if (ahash_is_async (hash)) { + fb =crypto_alloc_ahash (crypto_ahash_alg_name (hash), +0 ,CRYPTO_ALG_ASYNC ); +if (IS_ERR (fb)) +return PTR_ERR (fb); + + hash->fb = fb; + } +ahash_set_needkey (hash, alg); -if (alg->exit_tfm) - tfm->exit = crypto_ahash_exit_tfm; + tfm->exit = crypto_ahash_exit_tfm; -return alg->init_tfm ? alg->init_tfm (hash) :0 ; +if (!alg->init_tfm) { +if (!tfm->__crt_alg->cra_init) +return 0 ; + + err = tfm->__crt_alg->cra_init (tfm); +if (err) + goto out_free_sync_hash; + +return 0 ; + } + + err = alg->init_tfm (hash); +if (err) + goto out_free_sync_hash; + +return 0 ; + +out_free_sync_hash: +crypto_free_ahash (fb); +return err; }static unsigned int crypto_ahash_extsize (struct crypto_alg *alg)
@@ -970,5 +1019,54 @@ int ahash_register_instance(struct crypto_template *tmpl, }EXPORT_SYMBOL_GPL (ahash_register_instance); +void ahash_request_free (struct ahash_request *req) +{ +if (unlikely (!req)) +return ; + +if (!ahash_req_on_stack (req)) { +kfree (req); +return ; + } + +ahash_request_zero (req); +} +EXPORT_SYMBOL_GPL (ahash_request_free); + +int crypto_hash_digest (struct crypto_ahash *tfm,const u8 *data, +unsigned int len,u8 *out) +{ +HASH_REQUEST_ON_STACK (req, tfm->fb); +int err; + +ahash_request_set_callback (req,0 ,NULL ,NULL ); +ahash_request_set_virt (req, data, out, len); + err =crypto_ahash_digest (req); + +ahash_request_zero (req); + +return err; +} +EXPORT_SYMBOL_GPL (crypto_hash_digest); + +struct ahash_request *ahash_request_clone (struct ahash_request *req, +size_t total,gfp_t gfp) +{ +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +struct ahash_request *nreq; + + nreq =kmalloc (total, gfp); +if (!nreq) { +ahash_request_set_tfm (req, tfm->fb); + req->base.flags =CRYPTO_TFM_REQ_ON_STACK ; +return req; + } + +memcpy (nreq, req, total); +ahash_request_set_tfm (req, tfm); +return req; +} +EXPORT_SYMBOL_GPL (ahash_request_clone); +MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("Asynchronous cryptographic hash type" );
include/crypto/hash.h +44 -10
113 lines not on this page · raw message
include/crypto/internal/hash.h +25 -1
@@ -11,6 +11,12 @@#include <crypto/algapi.h> #include <crypto/hash.h> +#define HASH_FBREQ_ON_STACK (name, req) \ + char __##name##_req[sizeof(struct ahash_request) + \ + MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \ + struct ahash_request *name = ahash_fbreq_on_stack_init( \ + __##name##_req, (req)) +struct ahash_request ;struct ahash_instance {
@@ -187,7 +193,7 @@ static inline void ahash_request_complete(struct ahash_request *req, int err)static inline u32 ahash_request_flags (struct ahash_request *req) { -return req->base.flags; +return crypto_request_flags (&req->base) & ~CRYPTO_AHASH_REQ_PRIVATE ; } static inline struct crypto_ahash *crypto_spawn_ahash(
@@ -257,5 +263,23 @@ static inline bool crypto_ahash_req_chain(struct crypto_ahash *tfm) return crypto_tfm_req_chain(&tfm->base); } +static inline struct ahash_request *ahash_fbreq_on_stack_init ( +char *buf,struct ahash_request *old) +{ +struct crypto_ahash *tfm =crypto_ahash_reqtfm (old); +struct ahash_request *req = (void *)buf; + +ahash_request_set_tfm (req, tfm->fb); + req->base.flags =CRYPTO_TFM_REQ_ON_STACK ; +ahash_request_set_callback (req,ahash_request_flags (old),NULL ,NULL ); + req->base.flags &= ~CRYPTO_AHASH_REQ_PRIVATE ; + req->base.flags |= old->base.flags &CRYPTO_AHASH_REQ_PRIVATE ; + req->src = old->src; + req->result = old->result; + req->nbytes = old->nbytes; + +return req; +} +#endif /* _CRYPTO_INTERNAL_HASH_H */
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 2/8 crypto: zynqmp-sha - Add locking
In reply to Herbert XuThe hardwrae is only capable of one hash at a time, so add a lock to make sure that it isn't used concurrently.
--- drivers/crypto/xilinx/zynqmp-sha.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/crypto/xilinx/zynqmp-sha.c +14 -4
@@ -3,18 +3,19 @@* Xilinx ZynqMP SHA Driver. * Copyright (c) 2022 Xilinx Inc. */ -#include <linux/cacheflush.h> #include <crypto/hash.h> #include <crypto/internal/hash.h> #include <crypto/sha3.h> -#include <linux/crypto.h> +#include <linux/cacheflush.h> +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/dma-mapping.h> +#include <linux/err.h> #include <linux/firmware/xlnx-zynqmp.h> -#include <linux/init.h> #include <linux/io.h> #include <linux/kernel.h> #include <linux/module.h> +#include <linux/spinlock.h> #include <linux/platform_device.h> #define ZYNQMP_DMA_BIT_MASK 32U
@@ -43,6 +44,8 @@ struct zynqmp_sha_desc_ctx {static dma_addr_t update_dma_addr, final_dma_addr;static char *ubuf, *fbuf; +static DEFINE_SPINLOCK (zynqmp_sha_lock); +static int zynqmp_sha_init_tfm (struct crypto_shash *hash) {const char *fallback_driver_name =crypto_shash_alg_name (hash);
@@ -130,7 +133,8 @@ static int zynqmp_sha_export(struct shash_desc *desc, void *out)return crypto_shash_export (&dctx->fbk_req, out); } -static int zynqmp_sha_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) +static int __zynqmp_sha_digest (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out) {unsigned int remaining_len = len;int update_size;
@@ -165,6 +169,12 @@ static int zynqmp_sha_digest(struct shash_desc *desc, const u8 *data, unsigned ireturn ret; } +static int zynqmp_sha_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) +{ + scoped_guard(spinlock_bh, &zynqmp_sha_lock) +return __zynqmp_sha_digest (desc, data, len, out); +} +static struct zynqmp_sha_drv_ctx sha3_drv_ctx = { .sha3_384 = { .init = zynqmp_sha_init,
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 3/8 crypto: arm64/sha512 - Fix header inclusions
In reply to Herbert XuInstead of relying on linux/module.h being included through the header file sha512_base.h, include it directly.
--- arch/arm64/crypto/sha512-glue.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm64/crypto/sha512-glue.c +2 -3
@@ -6,11 +6,10 @@*/ #include <crypto/internal/hash.h> -#include <linux/types.h> -#include <linux/string.h> +#include <linux/kernel.h> +#include <linux/module.h> #include <crypto/sha2.h> #include <crypto/sha512_base.h> -#include <asm/neon.h> MODULE_DESCRIPTION ("SHA-384/SHA-512 secure hash for arm64" );MODULE_AUTHOR ("Andy Polyakov <[email protected]>" );
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 4/8 crypto: hash - Update HASH_MAX_DESCSIZE comment
In reply to Herbert XuThe biggest context is not sha3_generic (356), but sha-s390 (360).
--- include/crypto/hash.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/crypto/hash.h +2 -2
@@ -166,8 +166,8 @@ struct shash_desc {#define HASH_MAX_DIGESTSIZE 64/* -* Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc' -* containing a 'struct sha3_state'. +* Worst case is hmac(sha-224-s390). Its context is a nested 'shash_desc' +* containing a 'struct s390_sha_ctx'. */ #define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 360)#define MAX_SYNC_HASH_REQSIZE HASH_MAX_DESCSIZE
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 5/8 crypto: lib/sm3 - Move sm3 library into lib/crypto
In reply to Herbert XuMove the sm3 library code into lib/crypto.
---
arch/arm64/crypto/Kconfig | 4 ++--
arch/riscv/crypto/Kconfig | 2 +-
arch/x86/crypto/Kconfig | 2 +-
crypto/Kconfig | 5 +----
crypto/Makefile | 1 -
lib/crypto/Kconfig | 3 +++
lib/crypto/Makefile | 3 +++
{crypto => lib/crypto}/sm3.c | 0
8 files changed, 11 insertions(+), 9 deletions(-)
rename {crypto => lib/crypto}/sm3.c (100%)arch/arm64/crypto/Kconfig +2 -2
@@ -101,7 +101,7 @@ config CRYPTO_SM3_NEON tristate "Hash functions: SM3 (NEON)" depends on KERNEL_MODE_NEON select CRYPTO_HASH - select CRYPTO_SM3 + select CRYPTO_LIB_SM3 help SM3 (ShangMi 3) secure hash function (OSCCA GM/T 0004-2012)
@@ -112,7 +112,7 @@ config CRYPTO_SM3_ARM64_CE tristate "Hash functions: SM3 (ARMv8.2 Crypto Extensions)" depends on KERNEL_MODE_NEON select CRYPTO_HASH - select CRYPTO_SM3 + select CRYPTO_LIB_SM3 help SM3 (ShangMi 3) secure hash function (OSCCA GM/T 0004-2012)
arch/riscv/crypto/Kconfig +1 -1
@@ -61,7 +61,7 @@ config CRYPTO_SM3_RISCV64 tristate "Hash functions: SM3 (ShangMi 3)" depends on 64BIT && RISCV_ISA_V && TOOLCHAIN_HAS_VECTOR_CRYPTO select CRYPTO_HASH - select CRYPTO_SM3 + select CRYPTO_LIB_SM3 help SM3 (ShangMi 3) secure hash function (OSCCA GM/T 0004-2012)
arch/x86/crypto/Kconfig +1 -1
@@ -455,7 +455,7 @@ config CRYPTO_SM3_AVX_X86_64 tristate "Hash functions: SM3 (AVX)" depends on X86 && 64BIT select CRYPTO_HASH - select CRYPTO_SM3 + select CRYPTO_LIB_SM3 help SM3 secure hash function as defined by OSCCA GM/T 0004-2012 SM3
crypto/Kconfig +1 -4
@@ -1012,13 +1012,10 @@ config CRYPTO_SHA3 help SHA-3 secure hash algorithms (FIPS 202, ISO/IEC 10118-3) -config CRYPTO_SM3 - tristate - config CRYPTO_SM3_GENERIC tristate "SM3 (ShangMi 3)" select CRYPTO_HASH - select CRYPTO_SM3 + select CRYPTO_LIB_SM3 help SM3 (ShangMi 3) secure hash function (OSCCA GM/T 0004-2012, ISO/IEC 10118-3)
crypto/Makefile +0 -1
@@ -79,7 +79,6 @@ obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o obj-$(CONFIG_CRYPTO_SHA3) += sha3_generic.o -obj-$(CONFIG_CRYPTO_SM3) += sm3.o obj-$(CONFIG_CRYPTO_SM3_GENERIC) += sm3_generic.o obj-$(CONFIG_CRYPTO_STREEBOG) += streebog_generic.o obj-$(CONFIG_CRYPTO_WP512) += wp512.o
lib/crypto/Kconfig +3 -0
@@ -152,4 +152,7 @@ config CRYPTO_LIB_SHA1 config CRYPTO_LIB_SHA256 tristate +config CRYPTO_LIB_SM3 + tristate + endmenu
lib/crypto/Makefile +3 -0
@@ -60,3 +60,6 @@ endif obj-$(CONFIG_MPILIB) += mpi/ obj-$(CONFIG_CRYPTO_MANAGER_EXTRA_TESTS) += simd.o + +obj-$(CONFIG_CRYPTO_LIB_SM3) += libsm3.o +libsm3-y := sm3.o
lib/crypto/sm3.c renamed from crypto/sm3.c +0 -0
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 6/8 crypto: lib/sm3 - Export generic block function
In reply to Herbert XuExport the generic block function so that it can be used by the Crypto API.
--- include/crypto/sm3.h | 1 + lib/crypto/sm3.c | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 13 deletions(-)
include/crypto/sm3.h +1 -0
@@ -58,6 +58,7 @@ static inline void sm3_init(struct sm3_state *sctx) sctx->count =0 ; } +void sm3_block_generic (struct sm3_state *sctx,u8 const *data,int blocks);void sm3_update (struct sm3_state *sctx,const u8 *data,unsigned int len);void sm3_final (struct sm3_state *sctx,u8 *out);
lib/crypto/sm3.c +12 -13
@@ -166,19 +166,22 @@ static void sm3_transform(struct sm3_state *sctx, u8 const *data, u32 W[16])#undef W1#undef W2 -static inline void sm3_block (struct sm3_state *sctx, -u8 const *data,int blocks,u32 W [16 ]) +void sm3_block_generic (struct sm3_state *sctx,u8 const *data,int blocks) { -while (blocks--) { +u32 W [16 ]; + +do {sm3_transform (sctx, data,W ); data +=SM3_BLOCK_SIZE ; - } + }while (--blocks); + +memzero_explicit (W ,sizeof (W )); } +EXPORT_SYMBOL_GPL (sm3_block_generic);void sm3_update (struct sm3_state *sctx,const u8 *data,unsigned int len) {unsigned int partial = sctx->count %SM3_BLOCK_SIZE ; -u32 W [16 ]; sctx->count += len;
@@ -192,19 +195,17 @@ void sm3_update(struct sm3_state *sctx, const u8 *data, unsigned int len) data += p; len -= p; -sm3_block (sctx, sctx->buffer,1 ,W ); +sm3_block_generic (sctx, sctx->buffer,1 ); } blocks = len /SM3_BLOCK_SIZE ; len %=SM3_BLOCK_SIZE ;if (blocks) { -sm3_block (sctx, data, blocks,W ); +sm3_block_generic (sctx, data, blocks); data += blocks *SM3_BLOCK_SIZE ; } -memzero_explicit (W ,sizeof (W )); - partial =0 ; }if (len)
@@ -218,7 +219,6 @@ void sm3_final(struct sm3_state *sctx, u8 *out)__be64 *bits = (__be64 *)(sctx->buffer + bit_offset);__be32 *digest = (__be32 *)out;unsigned int partial = sctx->count %SM3_BLOCK_SIZE ; -u32 W [16 ];int i; sctx->buffer[partial++] =0x80 ;
@@ -226,18 +226,17 @@ void sm3_final(struct sm3_state *sctx, u8 *out)memset (sctx->buffer + partial,0 ,SM3_BLOCK_SIZE - partial); partial =0 ; -sm3_block (sctx, sctx->buffer,1 ,W ); +sm3_block_generic (sctx, sctx->buffer,1 ); }memset (sctx->buffer + partial,0 , bit_offset - partial); *bits =cpu_to_be64 (sctx->count <<3 ); -sm3_block (sctx, sctx->buffer,1 ,W ); +sm3_block_generic (sctx, sctx->buffer,1 );for (i =0 ; i <8 ; i++)put_unaligned_be32 (sctx->state[i], digest++);/* Zeroize sensitive information. */ -memzero_explicit (W ,sizeof (W ));memzero_explicit (sctx,sizeof (*sctx)); }EXPORT_SYMBOL_GPL (sm3_final);
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 7/8 crypto: sm3-base - Use sm3_init
In reply to Herbert XuRemove the duplicate init code and simply call sm3_init.
--- include/crypto/sm3_base.h | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
include/crypto/sm3_base.h +1 -12
@@ -20,18 +20,7 @@ typedef void (sm3_block_fn)(struct sm3_state *sst, u8 const *src, int blocks);static inline int sm3_base_init (struct shash_desc *desc) { -struct sm3_state *sctx =shash_desc_ctx (desc); - - sctx->state[0 ] =SM3_IVA ; - sctx->state[1 ] =SM3_IVB ; - sctx->state[2 ] =SM3_IVC ; - sctx->state[3 ] =SM3_IVD ; - sctx->state[4 ] =SM3_IVE ; - sctx->state[5 ] =SM3_IVF ; - sctx->state[6 ] =SM3_IVG ; - sctx->state[7 ] =SM3_IVH ; - sctx->count =0 ; - +sm3_init (shash_desc_ctx (desc));return 0 ; }
Signature · 1 line
2.39.5
Herbert Xu
maintainerPATCH 8/8 crypto: cbcmac - Set block size properly
In reply to Herbert XuThe block size of a hash algorithm is meant to be the number of bytes its block function can handle. For cbcmac that should be the block size of the underlying block cipher instead of one.
Set the block size of all cbcmac implementations accordingly.
--- arch/arm64/crypto/aes-glue.c | 2 +- arch/arm64/crypto/sm4-ce-glue.c | 2 +- crypto/ccm.c | 2 +- drivers/crypto/inside-secure/safexcel_hash.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
arch/arm64/crypto/aes-glue.c +1 -1
@@ -1009,7 +1009,7 @@ static struct shash_alg mac_algs[] = { { .base.cra_name = "cbcmac(aes)", .base.cra_driver_name = "cbcmac-aes-" MODE, .base.cra_priority = PRIO, - .base.cra_blocksize = 1, + .base.cra_blocksize = AES_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct mac_tfm_ctx), .base.cra_module = THIS_MODULE,
arch/arm64/crypto/sm4-ce-glue.c +1 -1
@@ -723,7 +723,7 @@ static struct shash_alg sm4_mac_algs[] = { .cra_name = "cbcmac(sm4)", .cra_driver_name = "cbcmac-sm4-ce", .cra_priority = 400, - .cra_blocksize = 1, + .cra_blocksize = SM4_BLOCK_SIZE, .cra_ctxsize = sizeof(struct sm4_mac_tfm_ctx), .cra_module = THIS_MODULE, },
crypto/ccm.c +1 -1
@@ -883,7 +883,7 @@ static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb) goto err_free_inst; inst->alg.base.cra_priority = alg->cra_priority; - inst->alg.base.cra_blocksize =1 ; + inst->alg.base.cra_blocksize = alg->cra_blocksize; inst->alg.digestsize = alg->cra_blocksize; inst->alg.descsize = sizeof(struct cbcmac_desc_ctx) +
drivers/crypto/inside-secure/safexcel_hash.c +1 -1
@@ -2043,7 +2043,7 @@ struct safexcel_alg_template safexcel_alg_cbcmac = { .cra_flags = CRYPTO_ALG_ASYNC |CRYPTO_ALG_ALLOCATES_MEMORY | CRYPTO_ALG_KERN_DRIVER_ONLY, - .cra_blocksize = 1, + .cra_blocksize = AES_BLOCK_SIZE, .cra_ctxsize = sizeof(struct safexcel_ahash_ctx), .cra_init = safexcel_ahash_cra_init, .cra_exit = safexcel_ahash_cra_exit,
Signature · 1 line
2.39.5
Eric Biggers
maintainerQuoted 9 lines · Herbert Xu
This series is based on
https://lore.kernel.org/linux-crypto/[email protected]Add helpers to use stack requests with any ahash algorithm, with optional dynamic allocation when required for async.
THe rest of the series are miscellaneous fixes in preparation for the next series which will introduce block-only shash.
What is a "block-only shash" and why does it make sense?
Based on the name, the fact that patch 8 is fiddling with cra_blocksize for one of the algorithms, and your general preference for trying to make the "Crypto API" solve problems even when it's not the right solution, I'm assuming that you're planning to make crypto/shash.c do the block buffering for all shash algorithms "automatically" using cra_blocksize.
That doesn't actually make sense, though. The more logical way to organize the code, as I think my patchsets converting crc32, crc32c, and poly1305 make very clear, is to have the shash API be implemented using the library API. The library API has to handle arbitrary length inputs anyway, so it has to implement the block buffering anyway. Thus, there's no need for shash to do it at all.
shash merely needs to know the block size and declare it in cra_blocksize for users who want it for some reason (e.g. for padding salts to the block size). shash should not care about the block size in any other way.
It's true that e.g. in the case of poly1305, each arch's library code implements the block buffering. So the redundancy that I think you're trying to solve does exist in the library too. But, the solution to that is to move the block buffering up one level, to poly1305_update(), so that it's shared by all the arches, just like what blake2s already does. I am going to consider doing this, especially as I get around to fixing the other hash algorithms. (If we do this for Poly1305, it would be an additional cleanup on top of my patch series
https://lore.kernel.org/linux-crypto/[email protected]/)
But, this has nothing to do with shash; shash plays no role in the solution.
- Eric
Herbert Xu
maintainerQuoted 2 lines · Eric Biggers
What is a "block-only shash" and why does it make sense?
It's not such a big deal with shash, especially if you use the library plus block function model like sha256.
However, where it does make a big difference is with the drivers.
I'm only doing shash because I want to ensure that the export format is identical between shash and ahash for a given algorithm, so that you can fallback to shash even if you've already done an export.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt