2 * SHA1 Secure Hash Algorithm.
4 * Derived by Martin Mares from Linux Kernel implementation, which is:
6 * Copyright (c) Alan Smithee.
7 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
8 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
15 * SHA transform algorithm originally taken from code written by
16 * Peter Gutmann, and placed in the public domain.
24 static inline u32 rol32(u32 x, uns bits)
26 return (x << bits) | (x >> (32 - bits));
29 #define SHA_WORKSPACE_WORDS 80
31 /* The SHA f()-functions. */
33 #define f1(x,y,z) (z ^ (x & (y ^ z))) /* x ? y : z */
34 #define f2(x,y,z) (x ^ y ^ z) /* XOR */
35 #define f3(x,y,z) ((x & y) + (z & (x ^ y))) /* majority */
37 /* The SHA Mysterious Constants */
39 #define K1 0x5A827999L /* Rounds 0-19: sqrt(2) * 2^30 */
40 #define K2 0x6ED9EBA1L /* Rounds 20-39: sqrt(3) * 2^30 */
41 #define K3 0x8F1BBCDCL /* Rounds 40-59: sqrt(5) * 2^30 */
42 #define K4 0xCA62C1D6L /* Rounds 60-79: sqrt(10) * 2^30 */
45 * sha_transform: single block SHA1 transform
47 * @digest: 160 bit digest to update
48 * @data: 512 bits of data to hash
49 * @W: 80 words of workspace (see note)
51 * This function generates a SHA1 digest for a single 512-bit block.
52 * Be warned, it does not handle padding and message digest, do not
53 * confuse it with the full FIPS 180-1 digest algorithm for variable
56 * Note: If the hash is security sensitive, the caller should be sure
57 * to clear the workspace. This is left to the caller to avoid
58 * unnecessary clears between chained hashing operations.
60 static void sha_transform(u32 *digest, const u8 *in, u32 *W)
62 u32 a, b, c, d, e, t, i;
64 for (i = 0; i < 16; i++)
65 W[i] = (in[4*i] << 24) | (in[4*i+1] << 16) | (in[4*i+2] << 8) | in[4*i+3];
67 for (i = 0; i < 64; i++)
68 W[i+16] = rol32(W[i+13] ^ W[i+8] ^ W[i+2] ^ W[i], 1);
76 for (i = 0; i < 20; i++) {
77 t = f1(b, c, d) + K1 + rol32(a, 5) + e + W[i];
78 e = d; d = c; c = rol32(b, 30); b = a; a = t;
81 for (; i < 40; i ++) {
82 t = f2(b, c, d) + K2 + rol32(a, 5) + e + W[i];
83 e = d; d = c; c = rol32(b, 30); b = a; a = t;
86 for (; i < 60; i ++) {
87 t = f3(b, c, d) + K3 + rol32(a, 5) + e + W[i];
88 e = d; d = c; c = rol32(b, 30); b = a; a = t;
91 for (; i < 80; i ++) {
92 t = f2(b, c, d) + K4 + rol32(a, 5) + e + W[i];
93 e = d; d = c; c = rol32(b, 30); b = a; a = t;
103 void sha1_init(struct sha1_ctx *sctx)
105 static const struct sha1_ctx initstate = {
107 { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 },
114 void sha1_update(struct sha1_ctx *sctx, const u8 *data, unsigned int len)
117 u32 temp[SHA_WORKSPACE_WORDS];
119 j = (sctx->count >> 3) & 0x3f;
120 sctx->count += len << 3;
122 if ((j + len) > 63) {
123 memcpy(&sctx->buffer[j], data, (i = 64-j));
124 sha_transform(sctx->state, sctx->buffer, temp);
125 for ( ; i + 63 < len; i += 64) {
126 sha_transform(sctx->state, &data[i], temp);
131 memcpy(&sctx->buffer[j], &data[i], len - i);
135 /* Add padding and return the message digest. */
136 void sha1_final(struct sha1_ctx *sctx, u8 *out)
138 u32 i, j, index, padlen;
141 static const u8 padding[64] = { 0x80, };
144 bits[7] = 0xff & t; t>>=8;
145 bits[6] = 0xff & t; t>>=8;
146 bits[5] = 0xff & t; t>>=8;
147 bits[4] = 0xff & t; t>>=8;
148 bits[3] = 0xff & t; t>>=8;
149 bits[2] = 0xff & t; t>>=8;
150 bits[1] = 0xff & t; t>>=8;
153 /* Pad out to 56 mod 64 */
154 index = (sctx->count >> 3) & 0x3f;
155 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
156 sha1_update(sctx, padding, padlen);
159 sha1_update(sctx, bits, sizeof bits);
161 /* Store state in digest */
162 for (i = j = 0; i < 5; i++, j += 4) {
163 u32 t2 = sctx->state[i];
164 out[j+3] = t2 & 0xff; t2>>=8;
165 out[j+2] = t2 & 0xff; t2>>=8;
166 out[j+1] = t2 & 0xff; t2>>=8;