1 /* sha1.c - SHA1 hash function
2 * Copyright (C) 1998, 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
4 * This file was part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* Borrowed and adapted slightly for use in JTE by Steve McIntyre
21 * <steve@einval.com> October 2010 */
26 * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
28 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
29 * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
33 #include "../config.h"
45 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
46 #define ror(x,n) ( ((x) >> (n)) | ((x) << (32-(n))) )
48 /* A macro to test whether P is properly aligned for an uint32_t type.
49 Note that config.h provides a suitable replacement for uintptr_t if
50 it does not exist in stdint.h. */
51 /* #if __GNUC__ >= 2 */
52 /* # define UINT32_T_ALIGNED_P(p) (!(((uintptr_t)p) % __alignof__ (uint32_t))) */
54 /* # define UINT32_T_ALIGNED_P(p) (!(((uintptr_t)p) % sizeof (uint32_t))) */
57 #define TRANSFORM(x,d,n) transform ((x), (d), (n))
59 void sha1_init_ctx (void *context)
61 SHA1_CONTEXT *hd = context;
73 /* Round function macros. */
74 #define K1 0x5A827999L
75 #define K2 0x6ED9EBA1L
76 #define K3 0x8F1BBCDCL
77 #define K4 0xCA62C1D6L
78 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
79 #define F2(x,y,z) ( x ^ y ^ z )
80 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
81 #define F4(x,y,z) ( x ^ y ^ z )
82 #define M(i) ( tm = x[ i &0x0f] \
86 (x[i&0x0f] = rol(tm, 1)))
87 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
96 * Transform NBLOCKS of each 64 bytes (16 32-bit words) at DATA.
98 static void transform (SHA1_CONTEXT *hd, const unsigned char *data, size_t nblocks)
100 register uint32_t a, b, c, d, e; /* Local copies of the chaining variables. */
101 register uint32_t tm; /* Helper. */
102 uint32_t x[16]; /* The array we work on. */
104 /* Loop over all blocks. */
105 for ( ;nblocks; nblocks--)
107 #ifdef WORDS_BIGENDIAN
108 memcpy (x, data, 64);
115 for(i=0, p=(unsigned char*)x; i < 16; i++, p += 4 )
124 /* Get the values of the chaining variables. */
132 R( a, b, c, d, e, F1, K1, x[ 0] );
133 R( e, a, b, c, d, F1, K1, x[ 1] );
134 R( d, e, a, b, c, F1, K1, x[ 2] );
135 R( c, d, e, a, b, F1, K1, x[ 3] );
136 R( b, c, d, e, a, F1, K1, x[ 4] );
137 R( a, b, c, d, e, F1, K1, x[ 5] );
138 R( e, a, b, c, d, F1, K1, x[ 6] );
139 R( d, e, a, b, c, F1, K1, x[ 7] );
140 R( c, d, e, a, b, F1, K1, x[ 8] );
141 R( b, c, d, e, a, F1, K1, x[ 9] );
142 R( a, b, c, d, e, F1, K1, x[10] );
143 R( e, a, b, c, d, F1, K1, x[11] );
144 R( d, e, a, b, c, F1, K1, x[12] );
145 R( c, d, e, a, b, F1, K1, x[13] );
146 R( b, c, d, e, a, F1, K1, x[14] );
147 R( a, b, c, d, e, F1, K1, x[15] );
148 R( e, a, b, c, d, F1, K1, M(16) );
149 R( d, e, a, b, c, F1, K1, M(17) );
150 R( c, d, e, a, b, F1, K1, M(18) );
151 R( b, c, d, e, a, F1, K1, M(19) );
152 R( a, b, c, d, e, F2, K2, M(20) );
153 R( e, a, b, c, d, F2, K2, M(21) );
154 R( d, e, a, b, c, F2, K2, M(22) );
155 R( c, d, e, a, b, F2, K2, M(23) );
156 R( b, c, d, e, a, F2, K2, M(24) );
157 R( a, b, c, d, e, F2, K2, M(25) );
158 R( e, a, b, c, d, F2, K2, M(26) );
159 R( d, e, a, b, c, F2, K2, M(27) );
160 R( c, d, e, a, b, F2, K2, M(28) );
161 R( b, c, d, e, a, F2, K2, M(29) );
162 R( a, b, c, d, e, F2, K2, M(30) );
163 R( e, a, b, c, d, F2, K2, M(31) );
164 R( d, e, a, b, c, F2, K2, M(32) );
165 R( c, d, e, a, b, F2, K2, M(33) );
166 R( b, c, d, e, a, F2, K2, M(34) );
167 R( a, b, c, d, e, F2, K2, M(35) );
168 R( e, a, b, c, d, F2, K2, M(36) );
169 R( d, e, a, b, c, F2, K2, M(37) );
170 R( c, d, e, a, b, F2, K2, M(38) );
171 R( b, c, d, e, a, F2, K2, M(39) );
172 R( a, b, c, d, e, F3, K3, M(40) );
173 R( e, a, b, c, d, F3, K3, M(41) );
174 R( d, e, a, b, c, F3, K3, M(42) );
175 R( c, d, e, a, b, F3, K3, M(43) );
176 R( b, c, d, e, a, F3, K3, M(44) );
177 R( a, b, c, d, e, F3, K3, M(45) );
178 R( e, a, b, c, d, F3, K3, M(46) );
179 R( d, e, a, b, c, F3, K3, M(47) );
180 R( c, d, e, a, b, F3, K3, M(48) );
181 R( b, c, d, e, a, F3, K3, M(49) );
182 R( a, b, c, d, e, F3, K3, M(50) );
183 R( e, a, b, c, d, F3, K3, M(51) );
184 R( d, e, a, b, c, F3, K3, M(52) );
185 R( c, d, e, a, b, F3, K3, M(53) );
186 R( b, c, d, e, a, F3, K3, M(54) );
187 R( a, b, c, d, e, F3, K3, M(55) );
188 R( e, a, b, c, d, F3, K3, M(56) );
189 R( d, e, a, b, c, F3, K3, M(57) );
190 R( c, d, e, a, b, F3, K3, M(58) );
191 R( b, c, d, e, a, F3, K3, M(59) );
192 R( a, b, c, d, e, F4, K4, M(60) );
193 R( e, a, b, c, d, F4, K4, M(61) );
194 R( d, e, a, b, c, F4, K4, M(62) );
195 R( c, d, e, a, b, F4, K4, M(63) );
196 R( b, c, d, e, a, F4, K4, M(64) );
197 R( a, b, c, d, e, F4, K4, M(65) );
198 R( e, a, b, c, d, F4, K4, M(66) );
199 R( d, e, a, b, c, F4, K4, M(67) );
200 R( c, d, e, a, b, F4, K4, M(68) );
201 R( b, c, d, e, a, F4, K4, M(69) );
202 R( a, b, c, d, e, F4, K4, M(70) );
203 R( e, a, b, c, d, F4, K4, M(71) );
204 R( d, e, a, b, c, F4, K4, M(72) );
205 R( c, d, e, a, b, F4, K4, M(73) );
206 R( b, c, d, e, a, F4, K4, M(74) );
207 R( a, b, c, d, e, F4, K4, M(75) );
208 R( e, a, b, c, d, F4, K4, M(76) );
209 R( d, e, a, b, c, F4, K4, M(77) );
210 R( c, d, e, a, b, F4, K4, M(78) );
211 R( b, c, d, e, a, F4, K4, M(79) );
213 /* Update the chaining variables. */
223 /* Update the message digest with the contents
224 * of INBUF with length INLEN.
226 void sha1_write( void *context, const void *inbuf_arg, size_t inlen)
228 const unsigned char *inbuf = inbuf_arg;
229 SHA1_CONTEXT *hd = context;
232 if (hd->count == 64) /* Flush the buffer. */
234 TRANSFORM( hd, hd->buf, 1 );
243 for (; inlen && hd->count < 64; inlen--)
244 hd->buf[hd->count++] = *inbuf++;
245 sha1_write (hd, NULL, 0);
250 nblocks = inlen / 64;
253 TRANSFORM (hd, inbuf, nblocks);
255 hd->nblocks += nblocks;
256 inlen -= nblocks * 64;
257 inbuf += nblocks * 64;
260 /* Save remaining bytes. */
261 for (; inlen && hd->count < 64; inlen--)
262 hd->buf[hd->count++] = *inbuf++;
266 /* The routine final terminates the computation and
267 * returns the digest.
268 * The handle is prepared for a new cycle, but adding bytes to the
269 * handle will the destroy the returned buffer.
270 * Returns: 20 bytes representing the digest.
273 void sha1_finish_ctx(void *context)
275 SHA1_CONTEXT *hd = context;
277 uint32_t t, msb, lsb;
280 sha1_write(hd, NULL, 0); /* flush */;
283 /* multiply by 64 to make a byte count */
288 if( (lsb += hd->count) < t )
290 /* multiply by 8 to make a bit count */
296 if( hd->count < 56 ) /* enough room */
298 hd->buf[hd->count++] = 0x80; /* pad */
299 while( hd->count < 56 )
300 hd->buf[hd->count++] = 0; /* pad */
302 else /* need one extra block */
304 hd->buf[hd->count++] = 0x80; /* pad character */
305 while( hd->count < 64 )
306 hd->buf[hd->count++] = 0;
307 sha1_write(hd, NULL, 0); /* flush */;
308 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
310 /* append the 64 bit count */
311 hd->buf[56] = msb >> 24;
312 hd->buf[57] = msb >> 16;
313 hd->buf[58] = msb >> 8;
315 hd->buf[60] = lsb >> 24;
316 hd->buf[61] = lsb >> 16;
317 hd->buf[62] = lsb >> 8;
319 TRANSFORM( hd, hd->buf, 1 );
322 #ifdef WORDS_BIGENDIAN
323 #define X(a) do { *(uint32_t*)p = hd->h##a ; p += 4; } while(0)
324 #else /* little endian */
325 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
326 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
337 unsigned char *sha1_read( void *context )
339 SHA1_CONTEXT *hd = context;