2 * SHA-1 Hash Function (FIPS 180-1, RFC 3174)
4 * Based on the code from libgcrypt-1.2.3, which is
5 * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
7 * Adaptation for libucw:
8 * (c) 2008 Martin Mares <mj@ucw.cz>
10 * This software may be freely distributed and used according to the terms
11 * of the GNU Lesser General Public License.
16 #include <ucw/unaligned.h>
21 sha1_init(sha1_context *hd)
33 * Transform the message X which consists of 16 32-bit-words
36 transform(sha1_context *hd, const byte *data)
41 /* Get values from the chaining vars. */
49 memcpy( x, data, 64 );
52 for (int i=0; i<16; i++)
53 x[i] = get_u32_be(data+4*i);
58 #define K1 0x5A827999L
59 #define K2 0x6ED9EBA1L
60 #define K3 0x8F1BBCDCL
61 #define K4 0xCA62C1D6L
62 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
63 #define F2(x,y,z) ( x ^ y ^ z )
64 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
65 #define F4(x,y,z) ( x ^ y ^ z )
68 #define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
69 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
70 , (x[i&0x0f] = ROL(tm, 1)) )
72 #define R(a,b,c,d,e,f,k,m) do { e += ROL( a, 5 ) \
78 R( a, b, c, d, e, F1, K1, x[ 0] );
79 R( e, a, b, c, d, F1, K1, x[ 1] );
80 R( d, e, a, b, c, F1, K1, x[ 2] );
81 R( c, d, e, a, b, F1, K1, x[ 3] );
82 R( b, c, d, e, a, F1, K1, x[ 4] );
83 R( a, b, c, d, e, F1, K1, x[ 5] );
84 R( e, a, b, c, d, F1, K1, x[ 6] );
85 R( d, e, a, b, c, F1, K1, x[ 7] );
86 R( c, d, e, a, b, F1, K1, x[ 8] );
87 R( b, c, d, e, a, F1, K1, x[ 9] );
88 R( a, b, c, d, e, F1, K1, x[10] );
89 R( e, a, b, c, d, F1, K1, x[11] );
90 R( d, e, a, b, c, F1, K1, x[12] );
91 R( c, d, e, a, b, F1, K1, x[13] );
92 R( b, c, d, e, a, F1, K1, x[14] );
93 R( a, b, c, d, e, F1, K1, x[15] );
94 R( e, a, b, c, d, F1, K1, M(16) );
95 R( d, e, a, b, c, F1, K1, M(17) );
96 R( c, d, e, a, b, F1, K1, M(18) );
97 R( b, c, d, e, a, F1, K1, M(19) );
98 R( a, b, c, d, e, F2, K2, M(20) );
99 R( e, a, b, c, d, F2, K2, M(21) );
100 R( d, e, a, b, c, F2, K2, M(22) );
101 R( c, d, e, a, b, F2, K2, M(23) );
102 R( b, c, d, e, a, F2, K2, M(24) );
103 R( a, b, c, d, e, F2, K2, M(25) );
104 R( e, a, b, c, d, F2, K2, M(26) );
105 R( d, e, a, b, c, F2, K2, M(27) );
106 R( c, d, e, a, b, F2, K2, M(28) );
107 R( b, c, d, e, a, F2, K2, M(29) );
108 R( a, b, c, d, e, F2, K2, M(30) );
109 R( e, a, b, c, d, F2, K2, M(31) );
110 R( d, e, a, b, c, F2, K2, M(32) );
111 R( c, d, e, a, b, F2, K2, M(33) );
112 R( b, c, d, e, a, F2, K2, M(34) );
113 R( a, b, c, d, e, F2, K2, M(35) );
114 R( e, a, b, c, d, F2, K2, M(36) );
115 R( d, e, a, b, c, F2, K2, M(37) );
116 R( c, d, e, a, b, F2, K2, M(38) );
117 R( b, c, d, e, a, F2, K2, M(39) );
118 R( a, b, c, d, e, F3, K3, M(40) );
119 R( e, a, b, c, d, F3, K3, M(41) );
120 R( d, e, a, b, c, F3, K3, M(42) );
121 R( c, d, e, a, b, F3, K3, M(43) );
122 R( b, c, d, e, a, F3, K3, M(44) );
123 R( a, b, c, d, e, F3, K3, M(45) );
124 R( e, a, b, c, d, F3, K3, M(46) );
125 R( d, e, a, b, c, F3, K3, M(47) );
126 R( c, d, e, a, b, F3, K3, M(48) );
127 R( b, c, d, e, a, F3, K3, M(49) );
128 R( a, b, c, d, e, F3, K3, M(50) );
129 R( e, a, b, c, d, F3, K3, M(51) );
130 R( d, e, a, b, c, F3, K3, M(52) );
131 R( c, d, e, a, b, F3, K3, M(53) );
132 R( b, c, d, e, a, F3, K3, M(54) );
133 R( a, b, c, d, e, F3, K3, M(55) );
134 R( e, a, b, c, d, F3, K3, M(56) );
135 R( d, e, a, b, c, F3, K3, M(57) );
136 R( c, d, e, a, b, F3, K3, M(58) );
137 R( b, c, d, e, a, F3, K3, M(59) );
138 R( a, b, c, d, e, F4, K4, M(60) );
139 R( e, a, b, c, d, F4, K4, M(61) );
140 R( d, e, a, b, c, F4, K4, M(62) );
141 R( c, d, e, a, b, F4, K4, M(63) );
142 R( b, c, d, e, a, F4, K4, M(64) );
143 R( a, b, c, d, e, F4, K4, M(65) );
144 R( e, a, b, c, d, F4, K4, M(66) );
145 R( d, e, a, b, c, F4, K4, M(67) );
146 R( c, d, e, a, b, F4, K4, M(68) );
147 R( b, c, d, e, a, F4, K4, M(69) );
148 R( a, b, c, d, e, F4, K4, M(70) );
149 R( e, a, b, c, d, F4, K4, M(71) );
150 R( d, e, a, b, c, F4, K4, M(72) );
151 R( c, d, e, a, b, F4, K4, M(73) );
152 R( b, c, d, e, a, F4, K4, M(74) );
153 R( a, b, c, d, e, F4, K4, M(75) );
154 R( e, a, b, c, d, F4, K4, M(76) );
155 R( d, e, a, b, c, F4, K4, M(77) );
156 R( c, d, e, a, b, F4, K4, M(78) );
157 R( b, c, d, e, a, F4, K4, M(79) );
159 /* Update chaining vars. */
169 * Update the message digest with the contents
170 * of INBUF with length INLEN.
173 sha1_update(sha1_context *hd, const byte *inbuf, uns inlen)
175 if( hd->count == 64 ) /* flush the buffer */
177 transform( hd, hd->buf );
186 for( ; inlen && hd->count < 64; inlen-- )
187 hd->buf[hd->count++] = *inbuf++;
188 sha1_update( hd, NULL, 0 );
195 transform( hd, inbuf );
201 for( ; inlen && hd->count < 64; inlen-- )
202 hd->buf[hd->count++] = *inbuf++;
207 * The routine final terminates the computation and
208 * returns the digest.
209 * The handle is prepared for a new cycle, but adding bytes to the
210 * handle will the destroy the returned buffer.
211 * Returns: 20 bytes representing the digest.
215 sha1_final(sha1_context *hd)
220 sha1_update(hd, NULL, 0); /* flush */;
223 /* multiply by 64 to make a byte count */
228 if( (lsb += hd->count) < t )
230 /* multiply by 8 to make a bit count */
236 if( hd->count < 56 ) /* enough room */
238 hd->buf[hd->count++] = 0x80; /* pad */
239 while( hd->count < 56 )
240 hd->buf[hd->count++] = 0; /* pad */
242 else /* need one extra block */
244 hd->buf[hd->count++] = 0x80; /* pad character */
245 while( hd->count < 64 )
246 hd->buf[hd->count++] = 0;
247 sha1_update(hd, NULL, 0); /* flush */;
248 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
250 /* append the 64 bit count */
251 hd->buf[56] = msb >> 24;
252 hd->buf[57] = msb >> 16;
253 hd->buf[58] = msb >> 8;
255 hd->buf[60] = lsb >> 24;
256 hd->buf[61] = lsb >> 16;
257 hd->buf[62] = lsb >> 8;
259 transform( hd, hd->buf );
262 #define X(a) do { put_u32_be(p, hd->h##a); p += 4; } while(0)
274 * Shortcut function which puts the hash value of the supplied buffer
275 * into outbuf which must have a size of 20 bytes.
278 sha1_hash_buffer(byte *outbuf, const byte *buffer, uns length)
283 sha1_update(&hd, buffer, length);
284 memcpy(outbuf, sha1_final(&hd), SHA1_SIZE);
291 #include <ucw/string.h>
300 while ((cnt = read(0, buf, sizeof(buf))) > 0)
301 sha1_update(&hd, buf, cnt);
303 char text[SHA1_HEX_SIZE];
304 mem_to_hex(text, sha1_final(&hd), SHA1_SIZE, 0);