crypto: s390/sha3 - Use API partial block handling

Use the Crypto API partial block handling.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
pull/1250/head
Herbert Xu 2025-04-18 11:00:13 +08:00
parent b333c273ab
commit 6f90ba7065
3 changed files with 56 additions and 75 deletions

View File

@ -8,12 +8,14 @@
* Copyright IBM Corp. 2019 * Copyright IBM Corp. 2019
* Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com) * Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com)
*/ */
#include <crypto/internal/hash.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cpufeature.h>
#include <crypto/sha3.h>
#include <asm/cpacf.h> #include <asm/cpacf.h>
#include <crypto/internal/hash.h>
#include <crypto/sha3.h>
#include <linux/cpufeature.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include "sha.h" #include "sha.h"
@ -21,11 +23,11 @@ static int sha3_256_init(struct shash_desc *desc)
{ {
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
if (!test_facility(86)) /* msa 12 */ sctx->first_message_part = test_facility(86);
if (!sctx->first_message_part)
memset(sctx->state, 0, sizeof(sctx->state)); memset(sctx->state, 0, sizeof(sctx->state));
sctx->count = 0; sctx->count = 0;
sctx->func = CPACF_KIMD_SHA3_256; sctx->func = CPACF_KIMD_SHA3_256;
sctx->first_message_part = 1;
return 0; return 0;
} }
@ -35,11 +37,11 @@ static int sha3_256_export(struct shash_desc *desc, void *out)
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
struct sha3_state *octx = out; struct sha3_state *octx = out;
octx->rsiz = sctx->count; if (sctx->first_message_part) {
memset(sctx->state, 0, sizeof(sctx->state));
sctx->first_message_part = 0;
}
memcpy(octx->st, sctx->state, sizeof(octx->st)); memcpy(octx->st, sctx->state, sizeof(octx->st));
memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
octx->partial = sctx->first_message_part;
return 0; return 0;
} }
@ -48,10 +50,9 @@ static int sha3_256_import(struct shash_desc *desc, const void *in)
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
const struct sha3_state *ictx = in; const struct sha3_state *ictx = in;
sctx->count = ictx->rsiz; sctx->count = 0;
memcpy(sctx->state, ictx->st, sizeof(ictx->st)); memcpy(sctx->state, ictx->st, sizeof(ictx->st));
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf)); sctx->first_message_part = 0;
sctx->first_message_part = ictx->partial;
sctx->func = CPACF_KIMD_SHA3_256; sctx->func = CPACF_KIMD_SHA3_256;
return 0; return 0;
@ -60,30 +61,26 @@ static int sha3_256_import(struct shash_desc *desc, const void *in)
static int sha3_224_import(struct shash_desc *desc, const void *in) static int sha3_224_import(struct shash_desc *desc, const void *in)
{ {
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
const struct sha3_state *ictx = in;
sctx->count = ictx->rsiz; sha3_256_import(desc, in);
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
sctx->first_message_part = ictx->partial;
sctx->func = CPACF_KIMD_SHA3_224; sctx->func = CPACF_KIMD_SHA3_224;
return 0; return 0;
} }
static struct shash_alg sha3_256_alg = { static struct shash_alg sha3_256_alg = {
.digestsize = SHA3_256_DIGEST_SIZE, /* = 32 */ .digestsize = SHA3_256_DIGEST_SIZE, /* = 32 */
.init = sha3_256_init, .init = sha3_256_init,
.update = s390_sha_update, .update = s390_sha_update_blocks,
.final = s390_sha_final, .finup = s390_sha_finup,
.export = sha3_256_export, .export = sha3_256_export,
.import = sha3_256_import, .import = sha3_256_import,
.descsize = sizeof(struct s390_sha_ctx), .descsize = S390_SHA_CTX_SIZE,
.statesize = sizeof(struct sha3_state), .statesize = SHA3_STATE_SIZE,
.base = { .base = {
.cra_name = "sha3-256", .cra_name = "sha3-256",
.cra_driver_name = "sha3-256-s390", .cra_driver_name = "sha3-256-s390",
.cra_priority = 300, .cra_priority = 300,
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
.cra_blocksize = SHA3_256_BLOCK_SIZE, .cra_blocksize = SHA3_256_BLOCK_SIZE,
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
} }
@ -93,28 +90,25 @@ static int sha3_224_init(struct shash_desc *desc)
{ {
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
if (!test_facility(86)) /* msa 12 */ sha3_256_init(desc);
memset(sctx->state, 0, sizeof(sctx->state));
sctx->count = 0;
sctx->func = CPACF_KIMD_SHA3_224; sctx->func = CPACF_KIMD_SHA3_224;
sctx->first_message_part = 1;
return 0; return 0;
} }
static struct shash_alg sha3_224_alg = { static struct shash_alg sha3_224_alg = {
.digestsize = SHA3_224_DIGEST_SIZE, .digestsize = SHA3_224_DIGEST_SIZE,
.init = sha3_224_init, .init = sha3_224_init,
.update = s390_sha_update, .update = s390_sha_update_blocks,
.final = s390_sha_final, .finup = s390_sha_finup,
.export = sha3_256_export, /* same as for 256 */ .export = sha3_256_export, /* same as for 256 */
.import = sha3_224_import, /* function code different! */ .import = sha3_224_import, /* function code different! */
.descsize = sizeof(struct s390_sha_ctx), .descsize = S390_SHA_CTX_SIZE,
.statesize = sizeof(struct sha3_state), .statesize = SHA3_STATE_SIZE,
.base = { .base = {
.cra_name = "sha3-224", .cra_name = "sha3-224",
.cra_driver_name = "sha3-224-s390", .cra_driver_name = "sha3-224-s390",
.cra_priority = 300, .cra_priority = 300,
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
.cra_blocksize = SHA3_224_BLOCK_SIZE, .cra_blocksize = SHA3_224_BLOCK_SIZE,
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
} }

View File

@ -7,12 +7,14 @@
* Copyright IBM Corp. 2019 * Copyright IBM Corp. 2019
* Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com) * Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com)
*/ */
#include <crypto/internal/hash.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cpufeature.h>
#include <crypto/sha3.h>
#include <asm/cpacf.h> #include <asm/cpacf.h>
#include <crypto/internal/hash.h>
#include <crypto/sha3.h>
#include <linux/cpufeature.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include "sha.h" #include "sha.h"
@ -20,11 +22,11 @@ static int sha3_512_init(struct shash_desc *desc)
{ {
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
if (!test_facility(86)) /* msa 12 */ sctx->first_message_part = test_facility(86);
if (!sctx->first_message_part)
memset(sctx->state, 0, sizeof(sctx->state)); memset(sctx->state, 0, sizeof(sctx->state));
sctx->count = 0; sctx->count = 0;
sctx->func = CPACF_KIMD_SHA3_512; sctx->func = CPACF_KIMD_SHA3_512;
sctx->first_message_part = 1;
return 0; return 0;
} }
@ -34,13 +36,12 @@ static int sha3_512_export(struct shash_desc *desc, void *out)
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
struct sha3_state *octx = out; struct sha3_state *octx = out;
octx->rsiz = sctx->count;
octx->rsizw = sctx->count >> 32;
if (sctx->first_message_part) {
memset(sctx->state, 0, sizeof(sctx->state));
sctx->first_message_part = 0;
}
memcpy(octx->st, sctx->state, sizeof(octx->st)); memcpy(octx->st, sctx->state, sizeof(octx->st));
memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
octx->partial = sctx->first_message_part;
return 0; return 0;
} }
@ -49,13 +50,9 @@ static int sha3_512_import(struct shash_desc *desc, const void *in)
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
const struct sha3_state *ictx = in; const struct sha3_state *ictx = in;
if (unlikely(ictx->rsizw)) sctx->count = 0;
return -ERANGE;
sctx->count = ictx->rsiz;
memcpy(sctx->state, ictx->st, sizeof(ictx->st)); memcpy(sctx->state, ictx->st, sizeof(ictx->st));
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf)); sctx->first_message_part = 0;
sctx->first_message_part = ictx->partial;
sctx->func = CPACF_KIMD_SHA3_512; sctx->func = CPACF_KIMD_SHA3_512;
return 0; return 0;
@ -64,33 +61,26 @@ static int sha3_512_import(struct shash_desc *desc, const void *in)
static int sha3_384_import(struct shash_desc *desc, const void *in) static int sha3_384_import(struct shash_desc *desc, const void *in)
{ {
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
const struct sha3_state *ictx = in;
if (unlikely(ictx->rsizw)) sha3_512_import(desc, in);
return -ERANGE;
sctx->count = ictx->rsiz;
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
sctx->first_message_part = ictx->partial;
sctx->func = CPACF_KIMD_SHA3_384; sctx->func = CPACF_KIMD_SHA3_384;
return 0; return 0;
} }
static struct shash_alg sha3_512_alg = { static struct shash_alg sha3_512_alg = {
.digestsize = SHA3_512_DIGEST_SIZE, .digestsize = SHA3_512_DIGEST_SIZE,
.init = sha3_512_init, .init = sha3_512_init,
.update = s390_sha_update, .update = s390_sha_update_blocks,
.final = s390_sha_final, .finup = s390_sha_finup,
.export = sha3_512_export, .export = sha3_512_export,
.import = sha3_512_import, .import = sha3_512_import,
.descsize = sizeof(struct s390_sha_ctx), .descsize = S390_SHA_CTX_SIZE,
.statesize = sizeof(struct sha3_state), .statesize = SHA3_STATE_SIZE,
.base = { .base = {
.cra_name = "sha3-512", .cra_name = "sha3-512",
.cra_driver_name = "sha3-512-s390", .cra_driver_name = "sha3-512-s390",
.cra_priority = 300, .cra_priority = 300,
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
.cra_blocksize = SHA3_512_BLOCK_SIZE, .cra_blocksize = SHA3_512_BLOCK_SIZE,
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
} }
@ -102,28 +92,25 @@ static int sha3_384_init(struct shash_desc *desc)
{ {
struct s390_sha_ctx *sctx = shash_desc_ctx(desc); struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
if (!test_facility(86)) /* msa 12 */ sha3_512_init(desc);
memset(sctx->state, 0, sizeof(sctx->state));
sctx->count = 0;
sctx->func = CPACF_KIMD_SHA3_384; sctx->func = CPACF_KIMD_SHA3_384;
sctx->first_message_part = 1;
return 0; return 0;
} }
static struct shash_alg sha3_384_alg = { static struct shash_alg sha3_384_alg = {
.digestsize = SHA3_384_DIGEST_SIZE, .digestsize = SHA3_384_DIGEST_SIZE,
.init = sha3_384_init, .init = sha3_384_init,
.update = s390_sha_update, .update = s390_sha_update_blocks,
.final = s390_sha_final, .finup = s390_sha_finup,
.export = sha3_512_export, /* same as for 512 */ .export = sha3_512_export, /* same as for 512 */
.import = sha3_384_import, /* function code different! */ .import = sha3_384_import, /* function code different! */
.descsize = sizeof(struct s390_sha_ctx), .descsize = S390_SHA_CTX_SIZE,
.statesize = sizeof(struct sha3_state), .statesize = SHA3_STATE_SIZE,
.base = { .base = {
.cra_name = "sha3-384", .cra_name = "sha3-384",
.cra_driver_name = "sha3-384-s390", .cra_driver_name = "sha3-384-s390",
.cra_priority = 300, .cra_priority = 300,
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
.cra_blocksize = SHA3_384_BLOCK_SIZE, .cra_blocksize = SHA3_384_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct s390_sha_ctx), .cra_ctxsize = sizeof(struct s390_sha_ctx),
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,

View File

@ -29,7 +29,7 @@ int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len)
fc = ctx->func; fc = ctx->func;
if (ctx->first_message_part) if (ctx->first_message_part)
fc |= test_facility(86) ? CPACF_KIMD_NIP : 0; fc |= CPACF_KIMD_NIP;
/* process one stored block */ /* process one stored block */
if (index) { if (index) {
@ -68,7 +68,7 @@ int s390_sha_update_blocks(struct shash_desc *desc, const u8 *data,
fc = ctx->func; fc = ctx->func;
if (ctx->first_message_part) if (ctx->first_message_part)
fc |= test_facility(86) ? CPACF_KIMD_NIP : 0; fc |= CPACF_KIMD_NIP;
/* process as many blocks as possible */ /* process as many blocks as possible */
n = (len / bsize) * bsize; n = (len / bsize) * bsize;