From 7b83638f962c30cb6271b5698dc52cdf9b638b48 Mon Sep 17 00:00:00 2001 From: Herbert Xu Date: Fri, 18 Apr 2025 10:59:34 +0800 Subject: [PATCH] crypto: s390/sha1 - Use API partial block handling Use the Crypto API partial block handling. Signed-off-by: Herbert Xu --- arch/s390/crypto/sha.h | 13 +++--- arch/s390/crypto/sha1_s390.c | 20 +++++----- arch/s390/crypto/sha_common.c | 74 +++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 15 deletions(-) diff --git a/arch/s390/crypto/sha.h b/arch/s390/crypto/sha.h index 2bb22db54c31..b8aeb51b2f3d 100644 --- a/arch/s390/crypto/sha.h +++ b/arch/s390/crypto/sha.h @@ -10,27 +10,30 @@ #ifndef _CRYPTO_ARCH_S390_SHA_H #define _CRYPTO_ARCH_S390_SHA_H -#include -#include -#include #include +#include /* must be big enough for the largest SHA variant */ #define SHA3_STATE_SIZE 200 #define CPACF_MAX_PARMBLOCK_SIZE SHA3_STATE_SIZE #define SHA_MAX_BLOCK_SIZE SHA3_224_BLOCK_SIZE +#define S390_SHA_CTX_SIZE offsetof(struct s390_sha_ctx, buf) struct s390_sha_ctx { u64 count; /* message length in bytes */ u32 state[CPACF_MAX_PARMBLOCK_SIZE / sizeof(u32)]; - u8 buf[SHA_MAX_BLOCK_SIZE]; int func; /* KIMD function to use */ - int first_message_part; + bool first_message_part; + u8 buf[SHA_MAX_BLOCK_SIZE]; }; struct shash_desc; int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len); +int s390_sha_update_blocks(struct shash_desc *desc, const u8 *data, + unsigned int len); int s390_sha_final(struct shash_desc *desc, u8 *out); +int s390_sha_finup(struct shash_desc *desc, const u8 *src, unsigned int len, + u8 *out); #endif diff --git a/arch/s390/crypto/sha1_s390.c b/arch/s390/crypto/sha1_s390.c index bc3a22704e09..d229cbd2ba22 100644 --- a/arch/s390/crypto/sha1_s390.c +++ b/arch/s390/crypto/sha1_s390.c @@ -18,12 +18,12 @@ * Copyright (c) Andrew McDonald * Copyright (c) Jean-Francois Dive */ +#include #include -#include -#include -#include #include -#include +#include +#include +#include #include "sha.h" @@ -49,7 +49,6 @@ static int s390_sha1_export(struct shash_desc *desc, void *out) octx->count = sctx->count; memcpy(octx->state, sctx->state, sizeof(octx->state)); - memcpy(octx->buffer, sctx->buf, sizeof(octx->buffer)); return 0; } @@ -60,7 +59,6 @@ static int s390_sha1_import(struct shash_desc *desc, const void *in) sctx->count = ictx->count; memcpy(sctx->state, ictx->state, sizeof(ictx->state)); - memcpy(sctx->buf, ictx->buffer, sizeof(ictx->buffer)); sctx->func = CPACF_KIMD_SHA_1; return 0; } @@ -68,16 +66,18 @@ static int s390_sha1_import(struct shash_desc *desc, const void *in) static struct shash_alg alg = { .digestsize = SHA1_DIGEST_SIZE, .init = s390_sha1_init, - .update = s390_sha_update, - .final = s390_sha_final, + .update = s390_sha_update_blocks, + .finup = s390_sha_finup, .export = s390_sha1_export, .import = s390_sha1_import, - .descsize = sizeof(struct s390_sha_ctx), - .statesize = sizeof(struct sha1_state), + .descsize = S390_SHA_CTX_SIZE, + .statesize = SHA1_STATE_SIZE, .base = { .cra_name = "sha1", .cra_driver_name= "sha1-s390", .cra_priority = 300, + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | + CRYPTO_AHASH_ALG_FINUP_MAX, .cra_blocksize = SHA1_BLOCK_SIZE, .cra_module = THIS_MODULE, } diff --git a/arch/s390/crypto/sha_common.c b/arch/s390/crypto/sha_common.c index 961d7d522af1..013bb37ad3ef 100644 --- a/arch/s390/crypto/sha_common.c +++ b/arch/s390/crypto/sha_common.c @@ -58,6 +58,27 @@ store: } EXPORT_SYMBOL_GPL(s390_sha_update); +int s390_sha_update_blocks(struct shash_desc *desc, const u8 *data, + unsigned int len) +{ + unsigned int bsize = crypto_shash_blocksize(desc->tfm); + struct s390_sha_ctx *ctx = shash_desc_ctx(desc); + unsigned int n; + int fc; + + fc = ctx->func; + if (ctx->first_message_part) + fc |= test_facility(86) ? CPACF_KIMD_NIP : 0; + + /* process as many blocks as possible */ + n = (len / bsize) * bsize; + ctx->count += n; + cpacf_kimd(fc, ctx->state, data, n); + ctx->first_message_part = 0; + return len - n; +} +EXPORT_SYMBOL_GPL(s390_sha_update_blocks); + static int s390_crypto_shash_parmsize(int func) { switch (func) { @@ -132,5 +153,58 @@ int s390_sha_final(struct shash_desc *desc, u8 *out) } EXPORT_SYMBOL_GPL(s390_sha_final); +int s390_sha_finup(struct shash_desc *desc, const u8 *src, unsigned int len, + u8 *out) +{ + struct s390_sha_ctx *ctx = shash_desc_ctx(desc); + int mbl_offset, fc; + u64 bits; + + ctx->count += len; + + bits = ctx->count * 8; + mbl_offset = s390_crypto_shash_parmsize(ctx->func); + if (mbl_offset < 0) + return -EINVAL; + + mbl_offset = mbl_offset / sizeof(u32); + + /* set total msg bit length (mbl) in CPACF parmblock */ + switch (ctx->func) { + case CPACF_KLMD_SHA_1: + case CPACF_KLMD_SHA_256: + memcpy(ctx->state + mbl_offset, &bits, sizeof(bits)); + break; + case CPACF_KLMD_SHA_512: + /* + * the SHA512 parmblock has a 128-bit mbl field, clear + * high-order u64 field, copy bits to low-order u64 field + */ + memset(ctx->state + mbl_offset, 0x00, sizeof(bits)); + mbl_offset += sizeof(u64) / sizeof(u32); + memcpy(ctx->state + mbl_offset, &bits, sizeof(bits)); + break; + case CPACF_KLMD_SHA3_224: + case CPACF_KLMD_SHA3_256: + case CPACF_KLMD_SHA3_384: + case CPACF_KLMD_SHA3_512: + break; + default: + return -EINVAL; + } + + fc = ctx->func; + fc |= test_facility(86) ? CPACF_KLMD_DUFOP : 0; + if (ctx->first_message_part) + fc |= CPACF_KLMD_NIP; + cpacf_klmd(fc, ctx->state, src, len); + + /* copy digest to out */ + memcpy(out, ctx->state, crypto_shash_digestsize(desc->tfm)); + + return 0; +} +EXPORT_SYMBOL_GPL(s390_sha_finup); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("s390 SHA cipher common functions"); -- 2.50.1