2 * SHA transform algorithm, originally taken from code written by
3 * Peter Gutmann, and placed in the public domain.
6 #include <asm/unaligned.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/cryptohash.h>
11 /* The SHA f()-functions. */
13 #define f1(x,y,z) (z ^ (x & (y ^ z))) /* x ? y : z */
14 #define f2(x,y,z) (x ^ y ^ z) /* XOR */
15 #define f3(x,y,z) ((x & y) + (z & (x ^ y))) /* majority */
17 /* The SHA Mysterious Constants */
19 #define K1 0x5A827999L /* Rounds 0-19: sqrt(2) * 2^30 */
20 #define K2 0x6ED9EBA1L /* Rounds 20-39: sqrt(3) * 2^30 */
21 #define K3 0x8F1BBCDCL /* Rounds 40-59: sqrt(5) * 2^30 */
22 #define K4 0xCA62C1D6L /* Rounds 60-79: sqrt(10) * 2^30 */
25 * sha_transform: single block SHA1 transform
27 * @digest: 160 bit digest to update
28 * @data: 512 bits of data to hash
29 * @W: 80 words of workspace (see note)
31 * This function generates a SHA1 digest for a single 512-bit block.
32 * Be warned, it does not handle padding and message digest, do not
33 * confuse it with the full FIPS 180-1 digest algorithm for variable
36 * Note: If the hash is security sensitive, the caller should be sure
37 * to clear the workspace. This is left to the caller to avoid
38 * unnecessary clears between chained hashing operations.
40 void sha_transform(__u32 *digest, const char *in, __u32 *W)
42 __u32 a, b, c, d, e, t, i;
44 for (i = 0; i < 16; i++)
45 W[i] = be32_to_cpu(get_unaligned((const __be32 *)in+i));
47 for (i = 0; i < 64; i++)
48 W[i+16] = rol32(W[i+13] ^ W[i+8] ^ W[i+2] ^ W[i], 1);
56 for (i = 0; i < 20; i++) {
57 t = f1(b, c, d) + K1 + rol32(a, 5) + e + W[i];
58 e = d; d = c; c = rol32(b, 30); b = a; a = t;
61 for (; i < 40; i ++) {
62 t = f2(b, c, d) + K2 + rol32(a, 5) + e + W[i];
63 e = d; d = c; c = rol32(b, 30); b = a; a = t;
66 for (; i < 60; i ++) {
67 t = f3(b, c, d) + K3 + rol32(a, 5) + e + W[i];
68 e = d; d = c; c = rol32(b, 30); b = a; a = t;
71 for (; i < 80; i ++) {
72 t = f2(b, c, d) + K4 + rol32(a, 5) + e + W[i];
73 e = d; d = c; c = rol32(b, 30); b = a; a = t;
82 EXPORT_SYMBOL(sha_transform);
85 * sha_init: initialize the vectors for a SHA1 digest
87 * @buf: vector to initialize
89 void sha_init(__u32 *buf)