Skip to content

Commit 992b706

Browse files
ifranzkihcahca
authored andcommitted
s390/sha3: Fix SHA3 selftests failures
Since commit "s390/sha3: Support sha3 performance enhancements" the selftests of the sha3_256_s390 and sha3_512_s390 kernel digests sometimes fail with: alg: shash: sha3-256-s390 test failed (wrong result) on test vector 3, cfg="import/export" alg: self-tests for sha3-256 using sha3-256-s390 failed (rc=-22) or with alg: ahash: sha3-256-s390 test failed (wrong result) on test vector 3, cfg="digest misaligned splits crossing pages" alg: self-tests for sha3-256 using sha3-256-s390 failed (rc=-22) The first failure is because the newly introduced context field 'first_message_part' is not copied during export and import operations. Because of that the value of 'first_message_part' is more or less random after an import into a newly allocated context and may or may not fit to the state of the imported SHA3 operation, causing an invalid hash when it does not fit. Save the 'first_message_part' field in the currently unused field 'partial' of struct sha3_state, even though the meaning of 'partial' is not exactly the same as 'first_message_part'. For the caller the returned state blob is opaque and it must only be ensured that the state can be imported later on by the module that exported it. The second failure is when on entry of s390_sha_update() the flag 'first_message_part' is on, and kimd is called in the first 'if (index)' block as well as in the second 'if (len >= bsize)' block. In this case, the 'first_message_part' is turned off after the first kimd, but the function code incorrectly retains the NIP flag. Reset the NIP flag after the first kimd unconditionally besides turning 'first_message_part' off. Reported-by: Marc Hartmayer <[email protected]> Fixes: 88c02b3 ("s390/sha3: Support sha3 performance enhancements") Reviewed-by: Harald Freudenberger <[email protected]> Reviewed-by: Holger Dengler <[email protected]> Reviewed-by: Joerg Schmidbauer <[email protected]> Signed-off-by: Ingo Franzki <[email protected]> Signed-off-by: Heiko Carstens <[email protected]>
1 parent fd19755 commit 992b706

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

arch/s390/crypto/sha3_256_s390.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static int sha3_256_export(struct shash_desc *desc, void *out)
3838
octx->rsiz = sctx->count;
3939
memcpy(octx->st, sctx->state, sizeof(octx->st));
4040
memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
41+
octx->partial = sctx->first_message_part;
4142

4243
return 0;
4344
}
@@ -50,6 +51,7 @@ static int sha3_256_import(struct shash_desc *desc, const void *in)
5051
sctx->count = ictx->rsiz;
5152
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
5253
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
54+
sctx->first_message_part = ictx->partial;
5355
sctx->func = CPACF_KIMD_SHA3_256;
5456

5557
return 0;
@@ -63,6 +65,7 @@ static int sha3_224_import(struct shash_desc *desc, const void *in)
6365
sctx->count = ictx->rsiz;
6466
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
6567
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
68+
sctx->first_message_part = ictx->partial;
6669
sctx->func = CPACF_KIMD_SHA3_224;
6770

6871
return 0;

arch/s390/crypto/sha3_512_s390.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static int sha3_512_export(struct shash_desc *desc, void *out)
3939

4040
memcpy(octx->st, sctx->state, sizeof(octx->st));
4141
memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
42+
octx->partial = sctx->first_message_part;
4243

4344
return 0;
4445
}
@@ -54,6 +55,7 @@ static int sha3_512_import(struct shash_desc *desc, const void *in)
5455

5556
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
5657
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
58+
sctx->first_message_part = ictx->partial;
5759
sctx->func = CPACF_KIMD_SHA3_512;
5860

5961
return 0;
@@ -70,6 +72,7 @@ static int sha3_384_import(struct shash_desc *desc, const void *in)
7072

7173
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
7274
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
75+
sctx->first_message_part = ictx->partial;
7376
sctx->func = CPACF_KIMD_SHA3_384;
7477

7578
return 0;

arch/s390/crypto/sha_common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len)
3636
memcpy(ctx->buf + index, data, bsize - index);
3737
cpacf_kimd(fc, ctx->state, ctx->buf, bsize);
3838
ctx->first_message_part = 0;
39+
fc &= ~CPACF_KIMD_NIP;
3940
data += bsize - index;
4041
len -= bsize - index;
4142
index = 0;

0 commit comments

Comments
 (0)