1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/crypto.h>
29#include <crypto/sha.h>
30
31#include "crypt_s390.h"
32
33struct s390_sha1_ctx {
34 u64 count;
35 u32 state[5];
36 u8 buf[2 * SHA1_BLOCK_SIZE];
37};
38
39static void sha1_init(struct crypto_tfm *tfm)
40{
41 struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
42
43 sctx->state[0] = SHA1_H0;
44 sctx->state[1] = SHA1_H1;
45 sctx->state[2] = SHA1_H2;
46 sctx->state[3] = SHA1_H3;
47 sctx->state[4] = SHA1_H4;
48 sctx->count = 0;
49}
50
51static void sha1_update(struct crypto_tfm *tfm, const u8 *data,
52 unsigned int len)
53{
54 struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
55 unsigned int index;
56 int ret;
57
58
59 index = sctx->count & 0x3f;
60
61 sctx->count += len;
62
63 if (index + len < SHA1_BLOCK_SIZE)
64 goto store;
65
66
67 if (index) {
68 memcpy(sctx->buf + index, data, SHA1_BLOCK_SIZE - index);
69 ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buf,
70 SHA1_BLOCK_SIZE);
71 BUG_ON(ret != SHA1_BLOCK_SIZE);
72 data += SHA1_BLOCK_SIZE - index;
73 len -= SHA1_BLOCK_SIZE - index;
74 }
75
76
77 if (len >= SHA1_BLOCK_SIZE) {
78 ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, data,
79 len & ~(SHA1_BLOCK_SIZE - 1));
80 BUG_ON(ret != (len & ~(SHA1_BLOCK_SIZE - 1)));
81 data += ret;
82 len -= ret;
83 }
84
85store:
86
87 if (len)
88 memcpy(sctx->buf + index , data, len);
89}
90
91
92static void sha1_final(struct crypto_tfm *tfm, u8 *out)
93{
94 struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
95 u64 bits;
96 unsigned int index, end;
97 int ret;
98
99
100 index = sctx->count & 0x3f;
101 end = (index < 56) ? SHA1_BLOCK_SIZE : (2 * SHA1_BLOCK_SIZE);
102
103
104 sctx->buf[index] = 0x80;
105
106
107 index++;
108 memset(sctx->buf + index, 0x00, end - index - 8);
109
110
111 bits = sctx->count * 8;
112 memcpy(sctx->buf + end - 8, &bits, sizeof(bits));
113
114 ret = crypt_s390_kimd(KIMD_SHA_1, sctx->state, sctx->buf, end);
115 BUG_ON(ret != end);
116
117
118 memcpy(out, sctx->state, SHA1_DIGEST_SIZE);
119
120
121 memset(sctx, 0, sizeof *sctx);
122}
123
124static struct crypto_alg alg = {
125 .cra_name = "sha1",
126 .cra_driver_name= "sha1-s390",
127 .cra_priority = CRYPT_S390_PRIORITY,
128 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
129 .cra_blocksize = SHA1_BLOCK_SIZE,
130 .cra_ctxsize = sizeof(struct s390_sha1_ctx),
131 .cra_module = THIS_MODULE,
132 .cra_list = LIST_HEAD_INIT(alg.cra_list),
133 .cra_u = { .digest = {
134 .dia_digestsize = SHA1_DIGEST_SIZE,
135 .dia_init = sha1_init,
136 .dia_update = sha1_update,
137 .dia_final = sha1_final } }
138};
139
140static int __init init(void)
141{
142 if (!crypt_s390_func_available(KIMD_SHA_1))
143 return -EOPNOTSUPP;
144
145 return crypto_register_alg(&alg);
146}
147
148static void __exit fini(void)
149{
150 crypto_unregister_alg(&alg);
151}
152
153module_init(init);
154module_exit(fini);
155
156MODULE_ALIAS("sha1");
157
158MODULE_LICENSE("GPL");
159MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
160