Skip to contentSkip to conversation

crypto: arm64: CE: implement export/import

PATCH In review Corentin Labbe 3 participants 3 messages Feb 24, 2020 – Mar 6, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 crypto: arm64: CE: implement export/import

When an ahash algorithm fallback to another ahash and that fallback is shaXXX-CE, doing export/import lead to error like this:
alg: ahash: sha1-sun8i-ce export() overran state buffer on test vector 0, cfg=\"import/export\"

This is due to the descsize of shaxxx-ce being larger than struct shaxxx_state off by an u32.
For fixing this, let's implement export/import which rip the finalize variant instead of using generic export/import.

Fixes 6ba6c74dfc6b ("arm64/crypto: SHA-224/SHA-256 using ARMv8 Crypto Extensions")
Fixes 2c98833a42cd ("arm64/crypto: SHA-1 using ARMv8 Crypto Extensions")

---
Changes since v1:
- memcpy directly &sctx->sst instead of sctx. As suggested by Eric Biggers

 arch/arm64/crypto/sha1-ce-glue.c | 20 ++++++++++++++++++++
 arch/arm64/crypto/sha2-ce-glue.c | 23 +++++++++++++++++++++++
 2 files changed, 43 insertions(+)
arch/arm64/crypto/sha1-ce-glue.c +20 -0
index 63c875d3314b..565ef604ca04 100644
@@ -91,12 +91,32 @@ static int sha1_ce_final(struct shash_desc *desc, u8 *out)
 	return sha1_base_finish(desc, out);
 }
 
+static int sha1_ce_export(struct shash_desc *desc, void *out)
+{
+	struct sha1_ce_state *sctx = shash_desc_ctx(desc);
+
+	memcpy(out, &sctx->sst, sizeof(struct sha1_state));
+	return 0;
+}
+
+static int sha1_ce_import(struct shash_desc *desc, const void *in)
+{
+	struct sha1_ce_state *sctx = shash_desc_ctx(desc);
+
+	memcpy(&sctx->sst, in, sizeof(struct sha1_state));
+	sctx->finalize = 0;
+	return 0;
+}
+
 static struct shash_alg alg = {
 	.init			= sha1_base_init,
 	.update			= sha1_ce_update,
 	.final			= sha1_ce_final,
 	.finup			= sha1_ce_finup,
+	.import			= sha1_ce_import,
+	.export			= sha1_ce_export,
 	.descsize		= sizeof(struct sha1_ce_state),
+	.statesize		= sizeof(struct sha1_state),
 	.digestsize		= SHA1_DIGEST_SIZE,
 	.base			= {
 		.cra_name		= "sha1",
arch/arm64/crypto/sha2-ce-glue.c +23 -0
index a8e67bafba3d..9450d19b9e6e 100644
@@ -109,12 +109,32 @@ static int sha256_ce_final(struct shash_desc *desc, u8 *out)
 	return sha256_base_finish(desc, out);
 }
 
+static int sha256_ce_export(struct shash_desc *desc, void *out)
+{
+	struct sha256_ce_state *sctx = shash_desc_ctx(desc);
+
+	memcpy(out, &sctx->sst, sizeof(struct sha256_state));
+	return 0;
+}
+
+static int sha256_ce_import(struct shash_desc *desc, const void *in)
+{
+	struct sha256_ce_state *sctx = shash_desc_ctx(desc);
+
+	memcpy(&sctx->sst, in, sizeof(struct sha256_state));
+	sctx->finalize = 0;
+	return 0;
+}
+
 static struct shash_alg algs[] = { {
 	.init			= sha224_base_init,
 	.update			= sha256_ce_update,
 	.final			= sha256_ce_final,
 	.finup			= sha256_ce_finup,
+	.export			= sha256_ce_export,
+	.import			= sha256_ce_import,
 	.descsize		= sizeof(struct sha256_ce_state),
+	.statesize		= sizeof(struct sha256_state),
 	.digestsize		= SHA224_DIGEST_SIZE,
 	.base			= {
 		.cra_name		= "sha224",
@@ -128,7 +148,10 @@ static struct shash_alg algs[] = { {
 	.update			= sha256_ce_update,
 	.final			= sha256_ce_final,
 	.finup			= sha256_ce_finup,
+	.export			= sha256_ce_export,
+	.import			= sha256_ce_import,
 	.descsize		= sizeof(struct sha256_ce_state),
+	.statesize		= sizeof(struct sha256_state),
 	.digestsize		= SHA256_DIGEST_SIZE,
 	.base			= {
 		.cra_name		= "sha256",
Signature · 1 line
2.24.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Corentin Labbe

When an ahash algorithm fallback to another ahash and that fallback is shaXXX-CE, doing export/import lead to error like this:
alg: ahash: sha1-sun8i-ce export() overran state buffer on test vector 0, cfg=\"import/export\"

This is due to the descsize of shaxxx-ce being larger than struct shaxxx_state off by an u32.
For fixing this, let's implement export/import which rip the finalize variant instead of using generic export/import.

Quoted 4 lines
Fixes 6ba6c74dfc6b ("arm64/crypto: SHA-224/SHA-256 using ARMv8 Crypto Extensions")
Fixes 2c98833a42cd ("arm64/crypto: SHA-1 using ARMv8 Crypto Extensions")
Quoted 96 lines

---
Changes since v1:
- memcpy directly &sctx->sst instead of sctx. As suggested by Eric Biggers

 arch/arm64/crypto/sha1-ce-glue.c | 20 ++++++++++++++++++++
 arch/arm64/crypto/sha2-ce-glue.c | 23 +++++++++++++++++++++++
 2 files changed, 43 insertions(+)
arch/arm64/crypto/sha1-ce-glue.c +20 -0
index 63c875d3314b..565ef604ca04 100644
@@ -91,12 +91,32 @@ static int sha1_ce_final(struct shash_desc *desc, u8 *out)
        return sha1_base_finish(desc, out);
 }
 
+static int sha1_ce_export(struct shash_desc *desc, void *out)
+{
+       struct sha1_ce_state *sctx = shash_desc_ctx(desc);
+
+       memcpy(out, &sctx->sst, sizeof(struct sha1_state));
+       return 0;
+}
+
+static int sha1_ce_import(struct shash_desc *desc, const void *in)
+{
+       struct sha1_ce_state *sctx = shash_desc_ctx(desc);
+
+       memcpy(&sctx->sst, in, sizeof(struct sha1_state));
+       sctx->finalize = 0;
+       return 0;
+}
+
 static struct shash_alg alg = {
        .init                   = sha1_base_init,
        .update                 = sha1_ce_update,
        .final                  = sha1_ce_final,
        .finup                  = sha1_ce_finup,
+       .import                 = sha1_ce_import,
+       .export                 = sha1_ce_export,
        .descsize               = sizeof(struct sha1_ce_state),
+       .statesize              = sizeof(struct sha1_state),
        .digestsize             = SHA1_DIGEST_SIZE,
        .base                   = {
                .cra_name               = "sha1",
arch/arm64/crypto/sha2-ce-glue.c +23 -1
index a8e67bafba3d..9450d19b9e6e 100644
@@ -109,12 +109,32 @@ static int sha256_ce_final(struct shash_desc *desc, u8 *out)
        return sha256_base_finish(desc, out);
 }
 
+static int sha256_ce_export(struct shash_desc *desc, void *out)
+{
+       struct sha256_ce_state *sctx = shash_desc_ctx(desc);
+
+       memcpy(out, &sctx->sst, sizeof(struct sha256_state));
+       return 0;
+}
+
+static int sha256_ce_import(struct shash_desc *desc, const void *in)
+{
+       struct sha256_ce_state *sctx = shash_desc_ctx(desc);
+
+       memcpy(&sctx->sst, in, sizeof(struct sha256_state));
+       sctx->finalize = 0;
+       return 0;
+}
+
 static struct shash_alg algs[] = { {
        .init                   = sha224_base_init,
        .update                 = sha256_ce_update,
        .final                  = sha256_ce_final,
        .finup                  = sha256_ce_finup,
+       .export                 = sha256_ce_export,
+       .import                 = sha256_ce_import,
        .descsize               = sizeof(struct sha256_ce_state),
+       .statesize              = sizeof(struct sha256_state),
        .digestsize             = SHA224_DIGEST_SIZE,
        .base                   = {
                .cra_name               = "sha224",
@@ -128,7 +148,10 @@ static struct shash_alg algs[] = { {
        .update                 = sha256_ce_update,
        .final                  = sha256_ce_final,
        .finup                  = sha256_ce_finup,
+       .export                 = sha256_ce_export,
+       .import                 = sha256_ce_import,
        .descsize               = sizeof(struct sha256_ce_state),
+       .statesize              = sizeof(struct sha256_state),
        .digestsize             = SHA256_DIGEST_SIZE,
        .base                   = {
                .cra_name               = "sha256",
--

2.24.1

maintainer
#2 lore raw
In reply to Corentin Labbe
Quoted 12 lines · Corentin Labbe

When an ahash algorithm fallback to another ahash and that fallback is shaXXX-CE, doing export/import lead to error like this:
alg: ahash: sha1-sun8i-ce export() overran state buffer on test vector 0, cfg=\"import/export\"

This is due to the descsize of shaxxx-ce being larger than struct shaxxx_state off by an u32.
For fixing this, let's implement export/import which rip the finalize variant instead of using generic export/import.

Fixes 6ba6c74dfc6b ("arm64/crypto: SHA-224/SHA-256 using ARMv8 Crypto Extensions")
Fixes 2c98833a42cd ("arm64/crypto: SHA-1 using ARMv8 Crypto Extensions")
Quoted 8 lines

---
Changes since v1:
- memcpy directly &sctx->sst instead of sctx. As suggested by Eric Biggers

 arch/arm64/crypto/sha1-ce-glue.c | 20 ++++++++++++++++++++
 arch/arm64/crypto/sha2-ce-glue.c | 23 +++++++++++++++++++++++
 2 files changed, 43 insertions(+)

Patch applied. Thanks.

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