sha512_generic.c (f9e2bca6c22d75a289a349f869701214d63b5060) sha512_generic.c (bd9d20dba182ce4541b16b083eccd30fb252b9f4)
1/* SHA-512 code by Jean-Luc Cooke <jlcooke@certainkey.com>
2 *
3 * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
4 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
5 * Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 */
1/* SHA-512 code by Jean-Luc Cooke <jlcooke@certainkey.com>
2 *
3 * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
4 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
5 * Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 */
13
13#include <crypto/internal/hash.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/mm.h>
17#include <linux/init.h>
18#include <linux/crypto.h>
19#include <linux/types.h>
20#include <crypto/sha.h>
21#include <linux/percpu.h>

--- 111 unchanged lines hidden (view full) ---

133 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
134
135 /* erase our data */
136 a = b = c = d = e = f = g = h = t1 = t2 = 0;
137 memset(W, 0, sizeof(__get_cpu_var(msg_schedule)));
138 put_cpu_var(msg_schedule);
139}
140
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/mm.h>
17#include <linux/init.h>
18#include <linux/crypto.h>
19#include <linux/types.h>
20#include <crypto/sha.h>
21#include <linux/percpu.h>

--- 111 unchanged lines hidden (view full) ---

133 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
134
135 /* erase our data */
136 a = b = c = d = e = f = g = h = t1 = t2 = 0;
137 memset(W, 0, sizeof(__get_cpu_var(msg_schedule)));
138 put_cpu_var(msg_schedule);
139}
140
141static void
142sha512_init(struct crypto_tfm *tfm)
141static int
142sha512_init(struct shash_desc *desc)
143{
143{
144 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
144 struct sha512_ctx *sctx = shash_desc_ctx(desc);
145 sctx->state[0] = SHA512_H0;
146 sctx->state[1] = SHA512_H1;
147 sctx->state[2] = SHA512_H2;
148 sctx->state[3] = SHA512_H3;
149 sctx->state[4] = SHA512_H4;
150 sctx->state[5] = SHA512_H5;
151 sctx->state[6] = SHA512_H6;
152 sctx->state[7] = SHA512_H7;
153 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
145 sctx->state[0] = SHA512_H0;
146 sctx->state[1] = SHA512_H1;
147 sctx->state[2] = SHA512_H2;
148 sctx->state[3] = SHA512_H3;
149 sctx->state[4] = SHA512_H4;
150 sctx->state[5] = SHA512_H5;
151 sctx->state[6] = SHA512_H6;
152 sctx->state[7] = SHA512_H7;
153 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
154
155 return 0;
154}
155
156}
157
156static void
157sha384_init(struct crypto_tfm *tfm)
158static int
159sha384_init(struct shash_desc *desc)
158{
160{
159 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
161 struct sha512_ctx *sctx = shash_desc_ctx(desc);
160 sctx->state[0] = SHA384_H0;
161 sctx->state[1] = SHA384_H1;
162 sctx->state[2] = SHA384_H2;
163 sctx->state[3] = SHA384_H3;
164 sctx->state[4] = SHA384_H4;
165 sctx->state[5] = SHA384_H5;
166 sctx->state[6] = SHA384_H6;
167 sctx->state[7] = SHA384_H7;
168 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
162 sctx->state[0] = SHA384_H0;
163 sctx->state[1] = SHA384_H1;
164 sctx->state[2] = SHA384_H2;
165 sctx->state[3] = SHA384_H3;
166 sctx->state[4] = SHA384_H4;
167 sctx->state[5] = SHA384_H5;
168 sctx->state[6] = SHA384_H6;
169 sctx->state[7] = SHA384_H7;
170 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
171
172 return 0;
169}
170
173}
174
171static void
172sha512_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
175static int
176sha512_update(struct shash_desc *desc, const u8 *data, unsigned int len)
173{
177{
174 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
178 struct sha512_ctx *sctx = shash_desc_ctx(desc);
175
176 unsigned int i, index, part_len;
177
178 /* Compute number of bytes mod 128 */
179 index = (unsigned int)((sctx->count[0] >> 3) & 0x7F);
180
181 /* Update number of bits */
182 if ((sctx->count[0] += (len << 3)) < (len << 3)) {

--- 15 unchanged lines hidden (view full) ---

198
199 index = 0;
200 } else {
201 i = 0;
202 }
203
204 /* Buffer remaining input */
205 memcpy(&sctx->buf[index], &data[i], len - i);
179
180 unsigned int i, index, part_len;
181
182 /* Compute number of bytes mod 128 */
183 index = (unsigned int)((sctx->count[0] >> 3) & 0x7F);
184
185 /* Update number of bits */
186 if ((sctx->count[0] += (len << 3)) < (len << 3)) {

--- 15 unchanged lines hidden (view full) ---

202
203 index = 0;
204 } else {
205 i = 0;
206 }
207
208 /* Buffer remaining input */
209 memcpy(&sctx->buf[index], &data[i], len - i);
210
211 return 0;
206}
207
212}
213
208static void
209sha512_final(struct crypto_tfm *tfm, u8 *hash)
214static int
215sha512_final(struct shash_desc *desc, u8 *hash)
210{
216{
211 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
217 struct sha512_ctx *sctx = shash_desc_ctx(desc);
212 static u8 padding[128] = { 0x80, };
213 __be64 *dst = (__be64 *)hash;
214 __be32 bits[4];
215 unsigned int index, pad_len;
216 int i;
217
218 /* Save number of bits */
219 bits[3] = cpu_to_be32(sctx->count[0]);
220 bits[2] = cpu_to_be32(sctx->count[1]);
221 bits[1] = cpu_to_be32(sctx->count[2]);
222 bits[0] = cpu_to_be32(sctx->count[3]);
223
224 /* Pad out to 112 mod 128. */
225 index = (sctx->count[0] >> 3) & 0x7f;
226 pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
218 static u8 padding[128] = { 0x80, };
219 __be64 *dst = (__be64 *)hash;
220 __be32 bits[4];
221 unsigned int index, pad_len;
222 int i;
223
224 /* Save number of bits */
225 bits[3] = cpu_to_be32(sctx->count[0]);
226 bits[2] = cpu_to_be32(sctx->count[1]);
227 bits[1] = cpu_to_be32(sctx->count[2]);
228 bits[0] = cpu_to_be32(sctx->count[3]);
229
230 /* Pad out to 112 mod 128. */
231 index = (sctx->count[0] >> 3) & 0x7f;
232 pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
227 sha512_update(tfm, padding, pad_len);
233 sha512_update(desc, padding, pad_len);
228
229 /* Append length (before padding) */
234
235 /* Append length (before padding) */
230 sha512_update(tfm, (const u8 *)bits, sizeof(bits));
236 sha512_update(desc, (const u8 *)bits, sizeof(bits));
231
232 /* Store state in digest */
233 for (i = 0; i < 8; i++)
234 dst[i] = cpu_to_be64(sctx->state[i]);
235
236 /* Zeroize sensitive information. */
237 memset(sctx, 0, sizeof(struct sha512_ctx));
237
238 /* Store state in digest */
239 for (i = 0; i < 8; i++)
240 dst[i] = cpu_to_be64(sctx->state[i]);
241
242 /* Zeroize sensitive information. */
243 memset(sctx, 0, sizeof(struct sha512_ctx));
244
245 return 0;
238}
239
246}
247
240static void sha384_final(struct crypto_tfm *tfm, u8 *hash)
248static int sha384_final(struct shash_desc *desc, u8 *hash)
241{
249{
242 u8 D[64];
250 u8 D[64];
243
251
244 sha512_final(tfm, D);
252 sha512_final(desc, D);
245
253
246 memcpy(hash, D, 48);
247 memset(D, 0, 64);
254 memcpy(hash, D, 48);
255 memset(D, 0, 64);
256
257 return 0;
248}
249
258}
259
250static struct crypto_alg sha512 = {
251 .cra_name = "sha512",
252 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
253 .cra_blocksize = SHA512_BLOCK_SIZE,
254 .cra_ctxsize = sizeof(struct sha512_ctx),
255 .cra_module = THIS_MODULE,
256 .cra_alignmask = 3,
257 .cra_list = LIST_HEAD_INIT(sha512.cra_list),
258 .cra_u = { .digest = {
259 .dia_digestsize = SHA512_DIGEST_SIZE,
260 .dia_init = sha512_init,
261 .dia_update = sha512_update,
262 .dia_final = sha512_final }
263 }
260static struct shash_alg sha512 = {
261 .digestsize = SHA512_DIGEST_SIZE,
262 .init = sha512_init,
263 .update = sha512_update,
264 .final = sha512_final,
265 .descsize = sizeof(struct sha512_ctx),
266 .base = {
267 .cra_name = "sha512",
268 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
269 .cra_blocksize = SHA512_BLOCK_SIZE,
270 .cra_module = THIS_MODULE,
271 }
264};
265
272};
273
266static struct crypto_alg sha384 = {
267 .cra_name = "sha384",
268 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
269 .cra_blocksize = SHA384_BLOCK_SIZE,
270 .cra_ctxsize = sizeof(struct sha512_ctx),
271 .cra_alignmask = 3,
272 .cra_module = THIS_MODULE,
273 .cra_list = LIST_HEAD_INIT(sha384.cra_list),
274 .cra_u = { .digest = {
275 .dia_digestsize = SHA384_DIGEST_SIZE,
276 .dia_init = sha384_init,
277 .dia_update = sha512_update,
278 .dia_final = sha384_final }
279 }
274static struct shash_alg sha384 = {
275 .digestsize = SHA384_DIGEST_SIZE,
276 .init = sha384_init,
277 .update = sha512_update,
278 .final = sha384_final,
279 .descsize = sizeof(struct sha512_ctx),
280 .base = {
281 .cra_name = "sha384",
282 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
283 .cra_blocksize = SHA384_BLOCK_SIZE,
284 .cra_module = THIS_MODULE,
285 }
280};
281
282static int __init sha512_generic_mod_init(void)
283{
284 int ret = 0;
285
286};
287
288static int __init sha512_generic_mod_init(void)
289{
290 int ret = 0;
291
286 if ((ret = crypto_register_alg(&sha384)) < 0)
292 if ((ret = crypto_register_shash(&sha384)) < 0)
287 goto out;
293 goto out;
288 if ((ret = crypto_register_alg(&sha512)) < 0)
289 crypto_unregister_alg(&sha384);
294 if ((ret = crypto_register_shash(&sha512)) < 0)
295 crypto_unregister_shash(&sha384);
290out:
291 return ret;
292}
293
294static void __exit sha512_generic_mod_fini(void)
295{
296out:
297 return ret;
298}
299
300static void __exit sha512_generic_mod_fini(void)
301{
296 crypto_unregister_alg(&sha384);
297 crypto_unregister_alg(&sha512);
302 crypto_unregister_shash(&sha384);
303 crypto_unregister_shash(&sha512);
298}
299
300module_init(sha512_generic_mod_init);
301module_exit(sha512_generic_mod_fini);
302
303MODULE_LICENSE("GPL");
304MODULE_DESCRIPTION("SHA-512 and SHA-384 Secure Hash Algorithms");
305
306MODULE_ALIAS("sha384");
307MODULE_ALIAS("sha512");
304}
305
306module_init(sha512_generic_mod_init);
307module_exit(sha512_generic_mod_fini);
308
309MODULE_LICENSE("GPL");
310MODULE_DESCRIPTION("SHA-512 and SHA-384 Secure Hash Algorithms");
311
312MODULE_ALIAS("sha384");
313MODULE_ALIAS("sha512");