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
1921static 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)
6461static 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
8170static 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
114100static 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 ,
0 commit comments