Skip to content

Commit 6f90ba7

Browse files
committed
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>
1 parent b333c27 commit 6f90ba7

File tree

3 files changed

+54
-73
lines changed

3 files changed

+54
-73
lines changed

arch/s390/crypto/sha3_256_s390.c

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@
88
* Copyright IBM Corp. 2019
99
* Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com)
1010
*/
11+
#include <asm/cpacf.h>
1112
#include <crypto/internal/hash.h>
12-
#include <linux/init.h>
13-
#include <linux/module.h>
14-
#include <linux/cpufeature.h>
1513
#include <crypto/sha3.h>
16-
#include <asm/cpacf.h>
14+
#include <linux/cpufeature.h>
15+
#include <linux/errno.h>
16+
#include <linux/kernel.h>
17+
#include <linux/module.h>
18+
#include <linux/string.h>
1719

1820
#include "sha.h"
1921

2022
static int sha3_256_init(struct shash_desc *desc)
2123
{
2224
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
2325

24-
if (!test_facility(86)) /* msa 12 */
26+
sctx->first_message_part = test_facility(86);
27+
if (!sctx->first_message_part)
2528
memset(sctx->state, 0, sizeof(sctx->state));
2629
sctx->count = 0;
2730
sctx->func = CPACF_KIMD_SHA3_256;
28-
sctx->first_message_part = 1;
2931

3032
return 0;
3133
}
@@ -35,11 +37,11 @@ static int sha3_256_export(struct shash_desc *desc, void *out)
3537
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
3638
struct sha3_state *octx = out;
3739

38-
octx->rsiz = sctx->count;
40+
if (sctx->first_message_part) {
41+
memset(sctx->state, 0, sizeof(sctx->state));
42+
sctx->first_message_part = 0;
43+
}
3944
memcpy(octx->st, sctx->state, sizeof(octx->st));
40-
memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
41-
octx->partial = sctx->first_message_part;
42-
4345
return 0;
4446
}
4547

@@ -48,10 +50,9 @@ static int sha3_256_import(struct shash_desc *desc, const void *in)
4850
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
4951
const struct sha3_state *ictx = in;
5052

51-
sctx->count = ictx->rsiz;
53+
sctx->count = 0;
5254
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
53-
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
54-
sctx->first_message_part = ictx->partial;
55+
sctx->first_message_part = 0;
5556
sctx->func = CPACF_KIMD_SHA3_256;
5657

5758
return 0;
@@ -60,30 +61,26 @@ static int sha3_256_import(struct shash_desc *desc, const void *in)
6061
static int sha3_224_import(struct shash_desc *desc, const void *in)
6162
{
6263
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
63-
const struct sha3_state *ictx = in;
6464

65-
sctx->count = ictx->rsiz;
66-
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
67-
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
68-
sctx->first_message_part = ictx->partial;
65+
sha3_256_import(desc, in);
6966
sctx->func = CPACF_KIMD_SHA3_224;
70-
7167
return 0;
7268
}
7369

7470
static struct shash_alg sha3_256_alg = {
7571
.digestsize = SHA3_256_DIGEST_SIZE, /* = 32 */
7672
.init = sha3_256_init,
77-
.update = s390_sha_update,
78-
.final = s390_sha_final,
73+
.update = s390_sha_update_blocks,
74+
.finup = s390_sha_finup,
7975
.export = sha3_256_export,
8076
.import = sha3_256_import,
81-
.descsize = sizeof(struct s390_sha_ctx),
82-
.statesize = sizeof(struct sha3_state),
77+
.descsize = S390_SHA_CTX_SIZE,
78+
.statesize = SHA3_STATE_SIZE,
8379
.base = {
8480
.cra_name = "sha3-256",
8581
.cra_driver_name = "sha3-256-s390",
8682
.cra_priority = 300,
83+
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
8784
.cra_blocksize = SHA3_256_BLOCK_SIZE,
8885
.cra_module = THIS_MODULE,
8986
}
@@ -93,28 +90,25 @@ static int sha3_224_init(struct shash_desc *desc)
9390
{
9491
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
9592

96-
if (!test_facility(86)) /* msa 12 */
97-
memset(sctx->state, 0, sizeof(sctx->state));
98-
sctx->count = 0;
93+
sha3_256_init(desc);
9994
sctx->func = CPACF_KIMD_SHA3_224;
100-
sctx->first_message_part = 1;
101-
10295
return 0;
10396
}
10497

10598
static struct shash_alg sha3_224_alg = {
10699
.digestsize = SHA3_224_DIGEST_SIZE,
107100
.init = sha3_224_init,
108-
.update = s390_sha_update,
109-
.final = s390_sha_final,
101+
.update = s390_sha_update_blocks,
102+
.finup = s390_sha_finup,
110103
.export = sha3_256_export, /* same as for 256 */
111104
.import = sha3_224_import, /* function code different! */
112-
.descsize = sizeof(struct s390_sha_ctx),
113-
.statesize = sizeof(struct sha3_state),
105+
.descsize = S390_SHA_CTX_SIZE,
106+
.statesize = SHA3_STATE_SIZE,
114107
.base = {
115108
.cra_name = "sha3-224",
116109
.cra_driver_name = "sha3-224-s390",
117110
.cra_priority = 300,
111+
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
118112
.cra_blocksize = SHA3_224_BLOCK_SIZE,
119113
.cra_module = THIS_MODULE,
120114
}

arch/s390/crypto/sha3_512_s390.c

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77
* Copyright IBM Corp. 2019
88
* Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com)
99
*/
10+
#include <asm/cpacf.h>
1011
#include <crypto/internal/hash.h>
11-
#include <linux/init.h>
12-
#include <linux/module.h>
13-
#include <linux/cpufeature.h>
1412
#include <crypto/sha3.h>
15-
#include <asm/cpacf.h>
13+
#include <linux/cpufeature.h>
14+
#include <linux/errno.h>
15+
#include <linux/kernel.h>
16+
#include <linux/module.h>
17+
#include <linux/string.h>
1618

1719
#include "sha.h"
1820

1921
static int sha3_512_init(struct shash_desc *desc)
2022
{
2123
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
2224

23-
if (!test_facility(86)) /* msa 12 */
25+
sctx->first_message_part = test_facility(86);
26+
if (!sctx->first_message_part)
2427
memset(sctx->state, 0, sizeof(sctx->state));
2528
sctx->count = 0;
2629
sctx->func = CPACF_KIMD_SHA3_512;
27-
sctx->first_message_part = 1;
2830

2931
return 0;
3032
}
@@ -34,13 +36,12 @@ static int sha3_512_export(struct shash_desc *desc, void *out)
3436
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
3537
struct sha3_state *octx = out;
3638

37-
octx->rsiz = sctx->count;
38-
octx->rsizw = sctx->count >> 32;
3939

40+
if (sctx->first_message_part) {
41+
memset(sctx->state, 0, sizeof(sctx->state));
42+
sctx->first_message_part = 0;
43+
}
4044
memcpy(octx->st, sctx->state, sizeof(octx->st));
41-
memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
42-
octx->partial = sctx->first_message_part;
43-
4445
return 0;
4546
}
4647

@@ -49,13 +50,9 @@ static int sha3_512_import(struct shash_desc *desc, const void *in)
4950
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
5051
const struct sha3_state *ictx = in;
5152

52-
if (unlikely(ictx->rsizw))
53-
return -ERANGE;
54-
sctx->count = ictx->rsiz;
55-
53+
sctx->count = 0;
5654
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
57-
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
58-
sctx->first_message_part = ictx->partial;
55+
sctx->first_message_part = 0;
5956
sctx->func = CPACF_KIMD_SHA3_512;
6057

6158
return 0;
@@ -64,33 +61,26 @@ static int sha3_512_import(struct shash_desc *desc, const void *in)
6461
static int sha3_384_import(struct shash_desc *desc, const void *in)
6562
{
6663
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
67-
const struct sha3_state *ictx = in;
6864

69-
if (unlikely(ictx->rsizw))
70-
return -ERANGE;
71-
sctx->count = ictx->rsiz;
72-
73-
memcpy(sctx->state, ictx->st, sizeof(ictx->st));
74-
memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
75-
sctx->first_message_part = ictx->partial;
65+
sha3_512_import(desc, in);
7666
sctx->func = CPACF_KIMD_SHA3_384;
77-
7867
return 0;
7968
}
8069

8170
static struct shash_alg sha3_512_alg = {
8271
.digestsize = SHA3_512_DIGEST_SIZE,
8372
.init = sha3_512_init,
84-
.update = s390_sha_update,
85-
.final = s390_sha_final,
73+
.update = s390_sha_update_blocks,
74+
.finup = s390_sha_finup,
8675
.export = sha3_512_export,
8776
.import = sha3_512_import,
88-
.descsize = sizeof(struct s390_sha_ctx),
89-
.statesize = sizeof(struct sha3_state),
77+
.descsize = S390_SHA_CTX_SIZE,
78+
.statesize = SHA3_STATE_SIZE,
9079
.base = {
9180
.cra_name = "sha3-512",
9281
.cra_driver_name = "sha3-512-s390",
9382
.cra_priority = 300,
83+
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
9484
.cra_blocksize = SHA3_512_BLOCK_SIZE,
9585
.cra_module = THIS_MODULE,
9686
}
@@ -102,28 +92,25 @@ static int sha3_384_init(struct shash_desc *desc)
10292
{
10393
struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
10494

105-
if (!test_facility(86)) /* msa 12 */
106-
memset(sctx->state, 0, sizeof(sctx->state));
107-
sctx->count = 0;
95+
sha3_512_init(desc);
10896
sctx->func = CPACF_KIMD_SHA3_384;
109-
sctx->first_message_part = 1;
110-
11197
return 0;
11298
}
11399

114100
static struct shash_alg sha3_384_alg = {
115101
.digestsize = SHA3_384_DIGEST_SIZE,
116102
.init = sha3_384_init,
117-
.update = s390_sha_update,
118-
.final = s390_sha_final,
103+
.update = s390_sha_update_blocks,
104+
.finup = s390_sha_finup,
119105
.export = sha3_512_export, /* same as for 512 */
120106
.import = sha3_384_import, /* function code different! */
121-
.descsize = sizeof(struct s390_sha_ctx),
122-
.statesize = sizeof(struct sha3_state),
107+
.descsize = S390_SHA_CTX_SIZE,
108+
.statesize = SHA3_STATE_SIZE,
123109
.base = {
124110
.cra_name = "sha3-384",
125111
.cra_driver_name = "sha3-384-s390",
126112
.cra_priority = 300,
113+
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
127114
.cra_blocksize = SHA3_384_BLOCK_SIZE,
128115
.cra_ctxsize = sizeof(struct s390_sha_ctx),
129116
.cra_module = THIS_MODULE,

arch/s390/crypto/sha_common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int s390_sha_update(struct shash_desc *desc, const u8 *data, unsigned int len)
2929

3030
fc = ctx->func;
3131
if (ctx->first_message_part)
32-
fc |= test_facility(86) ? CPACF_KIMD_NIP : 0;
32+
fc |= CPACF_KIMD_NIP;
3333

3434
/* process one stored block */
3535
if (index) {
@@ -68,7 +68,7 @@ int s390_sha_update_blocks(struct shash_desc *desc, const u8 *data,
6868

6969
fc = ctx->func;
7070
if (ctx->first_message_part)
71-
fc |= test_facility(86) ? CPACF_KIMD_NIP : 0;
71+
fc |= CPACF_KIMD_NIP;
7272

7373
/* process as many blocks as possible */
7474
n = (len / bsize) * bsize;

0 commit comments

Comments
 (0)