blob: 9efef20454cba0281c9e08c9ec02da4eae0e3665 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * SHA1 Secure Hash Algorithm.
5 *
6 * Derived from cryptoapi implementation, adapted for in-place
7 * scatterlist interface.
8 *
9 * Copyright (c) Alan Smithee.
10 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
11 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 *
18 */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080019#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/cryptohash.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110024#include <linux/types.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080025#include <crypto/sha.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/byteorder.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028struct sha1_ctx {
29 u64 count;
30 u32 state[5];
31 u8 buffer[64];
32};
33
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080034static int sha1_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080036 struct sha1_ctx *sctx = shash_desc_ctx(desc);
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 static const struct sha1_ctx initstate = {
39 0,
Jan Glauber5265eeb2007-10-09 22:43:13 +080040 { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 { 0, }
42 };
43
44 *sctx = initstate;
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080045
46 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080049static int sha1_update(struct shash_desc *desc, const u8 *data,
Herbert Xu6c2bb982006-05-16 22:09:29 +100050 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080052 struct sha1_ctx *sctx = shash_desc_ctx(desc);
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110053 unsigned int partial, done;
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110054 const u8 *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110056 partial = sctx->count & 0x3f;
57 sctx->count += len;
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110058 done = 0;
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110059 src = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110061 if ((partial + len) > 63) {
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110062 u32 temp[SHA_WORKSPACE_WORDS];
63
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110064 if (partial) {
Herbert Xubcb0ad22005-12-21 19:01:58 +080065 done = -partial;
66 memcpy(sctx->buffer + partial, data, done + 64);
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110067 src = sctx->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110069
70 do {
71 sha_transform(sctx->state, src, temp);
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110072 done += 64;
73 src = data + done;
74 } while (done + 63 < len);
Nicolas Pitrecfa8d172005-11-13 10:47:20 +110075
76 memset(temp, 0, sizeof(temp));
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110077 partial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Nicolas Pitre9d70a6c2005-11-13 10:59:54 +110079 memcpy(sctx->buffer + partial, src, len - done);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080080
81 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84
85/* Add padding and return the message digest. */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080086static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080088 struct sha1_ctx *sctx = shash_desc_ctx(desc);
Herbert Xu06ace7a2005-10-30 21:25:15 +110089 __be32 *dst = (__be32 *)out;
90 u32 i, index, padlen;
91 __be64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 static const u8 padding[64] = { 0x80, };
93
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110094 bits = cpu_to_be64(sctx->count << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /* Pad out to 56 mod 64 */
Nicolas Pitrefa9b98f2005-11-13 11:17:33 +110097 index = sctx->count & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080099 sha1_update(desc, padding, padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* Append length */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800102 sha1_update(desc, (const u8 *)&bits, sizeof(bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* Store state in digest */
Herbert Xu06ace7a2005-10-30 21:25:15 +1100105 for (i = 0; i < 5; i++)
106 dst[i] = cpu_to_be32(sctx->state[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* Wipe context */
109 memset(sctx, 0, sizeof *sctx);
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800110
111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800114static struct shash_alg alg = {
115 .digestsize = SHA1_DIGEST_SIZE,
116 .init = sha1_init,
117 .update = sha1_update,
118 .final = sha1_final,
119 .descsize = sizeof(struct sha1_ctx),
120 .base = {
121 .cra_name = "sha1",
122 .cra_driver_name= "sha1-generic",
123 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
124 .cra_blocksize = SHA1_BLOCK_SIZE,
125 .cra_module = THIS_MODULE,
126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800129static int __init sha1_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800131 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800134static void __exit sha1_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +0800136 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800139module_init(sha1_generic_mod_init);
140module_exit(sha1_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142MODULE_LICENSE("GPL");
143MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +1000144
Sebastian Siewiorad5d2782007-10-08 11:45:10 +0800145MODULE_ALIAS("sha1");