diff options
Diffstat (limited to 'arch/s390/crypto/sha256_s390.c')
-rw-r--r-- | arch/s390/crypto/sha256_s390.c | 90 |
1 files changed, 6 insertions, 84 deletions
diff --git a/arch/s390/crypto/sha256_s390.c b/arch/s390/crypto/sha256_s390.c index 2a3d756b35d4..19c03fb6ba7e 100644 --- a/arch/s390/crypto/sha256_s390.c +++ b/arch/s390/crypto/sha256_s390.c | |||
@@ -22,16 +22,11 @@ | |||
22 | #include <crypto/sha.h> | 22 | #include <crypto/sha.h> |
23 | 23 | ||
24 | #include "crypt_s390.h" | 24 | #include "crypt_s390.h" |
25 | 25 | #include "sha.h" | |
26 | struct s390_sha256_ctx { | ||
27 | u64 count; /* message length */ | ||
28 | u32 state[8]; | ||
29 | u8 buf[2 * SHA256_BLOCK_SIZE]; | ||
30 | }; | ||
31 | 26 | ||
32 | static void sha256_init(struct crypto_tfm *tfm) | 27 | static void sha256_init(struct crypto_tfm *tfm) |
33 | { | 28 | { |
34 | struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm); | 29 | struct s390_sha_ctx *sctx = crypto_tfm_ctx(tfm); |
35 | 30 | ||
36 | sctx->state[0] = SHA256_H0; | 31 | sctx->state[0] = SHA256_H0; |
37 | sctx->state[1] = SHA256_H1; | 32 | sctx->state[1] = SHA256_H1; |
@@ -42,79 +37,7 @@ static void sha256_init(struct crypto_tfm *tfm) | |||
42 | sctx->state[6] = SHA256_H6; | 37 | sctx->state[6] = SHA256_H6; |
43 | sctx->state[7] = SHA256_H7; | 38 | sctx->state[7] = SHA256_H7; |
44 | sctx->count = 0; | 39 | sctx->count = 0; |
45 | } | 40 | sctx->func = KIMD_SHA_256; |
46 | |||
47 | static void sha256_update(struct crypto_tfm *tfm, const u8 *data, | ||
48 | unsigned int len) | ||
49 | { | ||
50 | struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm); | ||
51 | unsigned int index; | ||
52 | int ret; | ||
53 | |||
54 | /* how much is already in the buffer? */ | ||
55 | index = sctx->count & 0x3f; | ||
56 | |||
57 | sctx->count += len; | ||
58 | |||
59 | if ((index + len) < SHA256_BLOCK_SIZE) | ||
60 | goto store; | ||
61 | |||
62 | /* process one stored block */ | ||
63 | if (index) { | ||
64 | memcpy(sctx->buf + index, data, SHA256_BLOCK_SIZE - index); | ||
65 | ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf, | ||
66 | SHA256_BLOCK_SIZE); | ||
67 | BUG_ON(ret != SHA256_BLOCK_SIZE); | ||
68 | data += SHA256_BLOCK_SIZE - index; | ||
69 | len -= SHA256_BLOCK_SIZE - index; | ||
70 | } | ||
71 | |||
72 | /* process as many blocks as possible */ | ||
73 | if (len >= SHA256_BLOCK_SIZE) { | ||
74 | ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, data, | ||
75 | len & ~(SHA256_BLOCK_SIZE - 1)); | ||
76 | BUG_ON(ret != (len & ~(SHA256_BLOCK_SIZE - 1))); | ||
77 | data += ret; | ||
78 | len -= ret; | ||
79 | } | ||
80 | |||
81 | store: | ||
82 | /* anything left? */ | ||
83 | if (len) | ||
84 | memcpy(sctx->buf + index , data, len); | ||
85 | } | ||
86 | |||
87 | /* Add padding and return the message digest */ | ||
88 | static void sha256_final(struct crypto_tfm *tfm, u8 *out) | ||
89 | { | ||
90 | struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm); | ||
91 | u64 bits; | ||
92 | unsigned int index, end; | ||
93 | int ret; | ||
94 | |||
95 | /* must perform manual padding */ | ||
96 | index = sctx->count & 0x3f; | ||
97 | end = (index < 56) ? SHA256_BLOCK_SIZE : (2 * SHA256_BLOCK_SIZE); | ||
98 | |||
99 | /* start pad with 1 */ | ||
100 | sctx->buf[index] = 0x80; | ||
101 | |||
102 | /* pad with zeros */ | ||
103 | index++; | ||
104 | memset(sctx->buf + index, 0x00, end - index - 8); | ||
105 | |||
106 | /* append message length */ | ||
107 | bits = sctx->count * 8; | ||
108 | memcpy(sctx->buf + end - 8, &bits, sizeof(bits)); | ||
109 | |||
110 | ret = crypt_s390_kimd(KIMD_SHA_256, sctx->state, sctx->buf, end); | ||
111 | BUG_ON(ret != end); | ||
112 | |||
113 | /* copy digest to out */ | ||
114 | memcpy(out, sctx->state, SHA256_DIGEST_SIZE); | ||
115 | |||
116 | /* wipe context */ | ||
117 | memset(sctx, 0, sizeof *sctx); | ||
118 | } | 41 | } |
119 | 42 | ||
120 | static struct crypto_alg alg = { | 43 | static struct crypto_alg alg = { |
@@ -123,14 +46,14 @@ static struct crypto_alg alg = { | |||
123 | .cra_priority = CRYPT_S390_PRIORITY, | 46 | .cra_priority = CRYPT_S390_PRIORITY, |
124 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, | 47 | .cra_flags = CRYPTO_ALG_TYPE_DIGEST, |
125 | .cra_blocksize = SHA256_BLOCK_SIZE, | 48 | .cra_blocksize = SHA256_BLOCK_SIZE, |
126 | .cra_ctxsize = sizeof(struct s390_sha256_ctx), | 49 | .cra_ctxsize = sizeof(struct s390_sha_ctx), |
127 | .cra_module = THIS_MODULE, | 50 | .cra_module = THIS_MODULE, |
128 | .cra_list = LIST_HEAD_INIT(alg.cra_list), | 51 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
129 | .cra_u = { .digest = { | 52 | .cra_u = { .digest = { |
130 | .dia_digestsize = SHA256_DIGEST_SIZE, | 53 | .dia_digestsize = SHA256_DIGEST_SIZE, |
131 | .dia_init = sha256_init, | 54 | .dia_init = sha256_init, |
132 | .dia_update = sha256_update, | 55 | .dia_update = s390_sha_update, |
133 | .dia_final = sha256_final } } | 56 | .dia_final = s390_sha_final } } |
134 | }; | 57 | }; |
135 | 58 | ||
136 | static int sha256_s390_init(void) | 59 | static int sha256_s390_init(void) |
@@ -150,6 +73,5 @@ module_init(sha256_s390_init); | |||
150 | module_exit(sha256_s390_fini); | 73 | module_exit(sha256_s390_fini); |
151 | 74 | ||
152 | MODULE_ALIAS("sha256"); | 75 | MODULE_ALIAS("sha256"); |
153 | |||
154 | MODULE_LICENSE("GPL"); | 76 | MODULE_LICENSE("GPL"); |
155 | MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm"); | 77 | MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm"); |