"Fossies" - the Fresh Open Source Software Archive 
Member "libksba-1.5.0/tests/sha1.c" (18 Nov 2020, 8712 Bytes) of package /linux/privat/libksba-1.5.0.tar.bz2:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "sha1.c":
1.4.0_vs_1.5.0.
1 /* sha1.c - SHA1 hash function
2 * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
20 */
21
22
23 /* This is a simplified SHA01 versions taken from the libgrypt one.
24 We need this for some tests (e.g. OCSP).
25 */
26
27 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32
33 #ifndef HAVE_TYPE_U32
34 #undef u32 /* maybe there is a macro with this name */
35 #if SIZEOF_UNSIGNED_INT == 4
36 typedef unsigned int u32;
37 #elif SIZEOF_UNSIGNED_LONG == 4
38 typedef unsigned long u32;
39 #else
40 #error no typedef for u32
41 #endif
42 #define HAVE_TYPE_U32
43 #endif
44
45 typedef struct
46 {
47 u32 h0,h1,h2,h3,h4;
48 u32 nblocks;
49 unsigned char buf[64];
50 int count;
51 } sha1_context_t;
52
53
54 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
55
56
57 static void
58 sha1_init (void *context)
59 {
60 sha1_context_t *hd = context;
61
62 hd->h0 = 0x67452301;
63 hd->h1 = 0xefcdab89;
64 hd->h2 = 0x98badcfe;
65 hd->h3 = 0x10325476;
66 hd->h4 = 0xc3d2e1f0;
67 hd->nblocks = 0;
68 hd->count = 0;
69 }
70
71
72 /****************
73 * Transform the message X which consists of 16 32-bit-words
74 */
75 static void
76 transform( sha1_context_t *hd, unsigned char *data )
77 {
78 register u32 a,b,c,d,e,tm;
79 u32 x[16];
80
81 /* Get values from the chaining vars. */
82 a = hd->h0;
83 b = hd->h1;
84 c = hd->h2;
85 d = hd->h3;
86 e = hd->h4;
87
88 #ifdef WORDS_BIGENDIAN
89 memcpy( x, data, 64 );
90 #else
91 {
92 int i;
93 unsigned char *p2;
94 for(i=0, p2=(unsigned char*)x; i < 16; i++, p2 += 4 )
95 {
96 p2[3] = *data++;
97 p2[2] = *data++;
98 p2[1] = *data++;
99 p2[0] = *data++;
100 }
101 }
102 #endif
103
104
105 #define K1 0x5A827999L
106 #define K2 0x6ED9EBA1L
107 #define K3 0x8F1BBCDCL
108 #define K4 0xCA62C1D6L
109 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
110 #define F2(x,y,z) ( x ^ y ^ z )
111 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
112 #define F4(x,y,z) ( x ^ y ^ z )
113
114
115 #define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
116 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
117 , (x[i&0x0f] = rol(tm, 1)) )
118
119 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
120 + f( b, c, d ) \
121 + k \
122 + m; \
123 b = rol( b, 30 ); \
124 } while(0)
125 R( a, b, c, d, e, F1, K1, x[ 0] );
126 R( e, a, b, c, d, F1, K1, x[ 1] );
127 R( d, e, a, b, c, F1, K1, x[ 2] );
128 R( c, d, e, a, b, F1, K1, x[ 3] );
129 R( b, c, d, e, a, F1, K1, x[ 4] );
130 R( a, b, c, d, e, F1, K1, x[ 5] );
131 R( e, a, b, c, d, F1, K1, x[ 6] );
132 R( d, e, a, b, c, F1, K1, x[ 7] );
133 R( c, d, e, a, b, F1, K1, x[ 8] );
134 R( b, c, d, e, a, F1, K1, x[ 9] );
135 R( a, b, c, d, e, F1, K1, x[10] );
136 R( e, a, b, c, d, F1, K1, x[11] );
137 R( d, e, a, b, c, F1, K1, x[12] );
138 R( c, d, e, a, b, F1, K1, x[13] );
139 R( b, c, d, e, a, F1, K1, x[14] );
140 R( a, b, c, d, e, F1, K1, x[15] );
141 R( e, a, b, c, d, F1, K1, M(16) );
142 R( d, e, a, b, c, F1, K1, M(17) );
143 R( c, d, e, a, b, F1, K1, M(18) );
144 R( b, c, d, e, a, F1, K1, M(19) );
145 R( a, b, c, d, e, F2, K2, M(20) );
146 R( e, a, b, c, d, F2, K2, M(21) );
147 R( d, e, a, b, c, F2, K2, M(22) );
148 R( c, d, e, a, b, F2, K2, M(23) );
149 R( b, c, d, e, a, F2, K2, M(24) );
150 R( a, b, c, d, e, F2, K2, M(25) );
151 R( e, a, b, c, d, F2, K2, M(26) );
152 R( d, e, a, b, c, F2, K2, M(27) );
153 R( c, d, e, a, b, F2, K2, M(28) );
154 R( b, c, d, e, a, F2, K2, M(29) );
155 R( a, b, c, d, e, F2, K2, M(30) );
156 R( e, a, b, c, d, F2, K2, M(31) );
157 R( d, e, a, b, c, F2, K2, M(32) );
158 R( c, d, e, a, b, F2, K2, M(33) );
159 R( b, c, d, e, a, F2, K2, M(34) );
160 R( a, b, c, d, e, F2, K2, M(35) );
161 R( e, a, b, c, d, F2, K2, M(36) );
162 R( d, e, a, b, c, F2, K2, M(37) );
163 R( c, d, e, a, b, F2, K2, M(38) );
164 R( b, c, d, e, a, F2, K2, M(39) );
165 R( a, b, c, d, e, F3, K3, M(40) );
166 R( e, a, b, c, d, F3, K3, M(41) );
167 R( d, e, a, b, c, F3, K3, M(42) );
168 R( c, d, e, a, b, F3, K3, M(43) );
169 R( b, c, d, e, a, F3, K3, M(44) );
170 R( a, b, c, d, e, F3, K3, M(45) );
171 R( e, a, b, c, d, F3, K3, M(46) );
172 R( d, e, a, b, c, F3, K3, M(47) );
173 R( c, d, e, a, b, F3, K3, M(48) );
174 R( b, c, d, e, a, F3, K3, M(49) );
175 R( a, b, c, d, e, F3, K3, M(50) );
176 R( e, a, b, c, d, F3, K3, M(51) );
177 R( d, e, a, b, c, F3, K3, M(52) );
178 R( c, d, e, a, b, F3, K3, M(53) );
179 R( b, c, d, e, a, F3, K3, M(54) );
180 R( a, b, c, d, e, F3, K3, M(55) );
181 R( e, a, b, c, d, F3, K3, M(56) );
182 R( d, e, a, b, c, F3, K3, M(57) );
183 R( c, d, e, a, b, F3, K3, M(58) );
184 R( b, c, d, e, a, F3, K3, M(59) );
185 R( a, b, c, d, e, F4, K4, M(60) );
186 R( e, a, b, c, d, F4, K4, M(61) );
187 R( d, e, a, b, c, F4, K4, M(62) );
188 R( c, d, e, a, b, F4, K4, M(63) );
189 R( b, c, d, e, a, F4, K4, M(64) );
190 R( a, b, c, d, e, F4, K4, M(65) );
191 R( e, a, b, c, d, F4, K4, M(66) );
192 R( d, e, a, b, c, F4, K4, M(67) );
193 R( c, d, e, a, b, F4, K4, M(68) );
194 R( b, c, d, e, a, F4, K4, M(69) );
195 R( a, b, c, d, e, F4, K4, M(70) );
196 R( e, a, b, c, d, F4, K4, M(71) );
197 R( d, e, a, b, c, F4, K4, M(72) );
198 R( c, d, e, a, b, F4, K4, M(73) );
199 R( b, c, d, e, a, F4, K4, M(74) );
200 R( a, b, c, d, e, F4, K4, M(75) );
201 R( e, a, b, c, d, F4, K4, M(76) );
202 R( d, e, a, b, c, F4, K4, M(77) );
203 R( c, d, e, a, b, F4, K4, M(78) );
204 R( b, c, d, e, a, F4, K4, M(79) );
205
206 /* Update chaining vars. */
207 hd->h0 += a;
208 hd->h1 += b;
209 hd->h2 += c;
210 hd->h3 += d;
211 hd->h4 += e;
212 }
213
214
215 /* Update the message digest with the contents
216 * of INBUF with length INLEN.
217 */
218 static void
219 sha1_write( void *context, unsigned char *inbuf, size_t inlen)
220 {
221 sha1_context_t *hd = context;
222
223 if( hd->count == 64 ) /* flush the buffer */
224 {
225 transform( hd, hd->buf );
226 hd->count = 0;
227 hd->nblocks++;
228 }
229 if( !inbuf )
230 return;
231
232 if( hd->count )
233 {
234 for( ; inlen && hd->count < 64; inlen-- )
235 hd->buf[hd->count++] = *inbuf++;
236 sha1_write( hd, NULL, 0 );
237 if( !inlen )
238 return;
239 }
240
241 while( inlen >= 64 )
242 {
243 transform( hd, inbuf );
244 hd->count = 0;
245 hd->nblocks++;
246 inlen -= 64;
247 inbuf += 64;
248 }
249 for( ; inlen && hd->count < 64; inlen-- )
250 hd->buf[hd->count++] = *inbuf++;
251 }
252
253
254 /* The routine final terminates the computation and
255 * returns the digest.
256 * The handle is prepared for a new cycle, but adding bytes to the
257 * handle will the destroy the returned buffer.
258 * Returns: 20 bytes representing the digest.
259 */
260
261 static void
262 sha1_final(void *context)
263 {
264 sha1_context_t *hd = context;
265
266 u32 t, msb, lsb;
267 unsigned char *p;
268
269 sha1_write(hd, NULL, 0); /* flush */;
270
271 t = hd->nblocks;
272 /* multiply by 64 to make a byte count */
273 lsb = t << 6;
274 msb = t >> 26;
275 /* add the count */
276 t = lsb;
277 if( (lsb += hd->count) < t )
278 msb++;
279 /* multiply by 8 to make a bit count */
280 t = lsb;
281 lsb <<= 3;
282 msb <<= 3;
283 msb |= t >> 29;
284
285 if( hd->count < 56 ) /* enough room */
286 {
287 hd->buf[hd->count++] = 0x80; /* pad */
288 while( hd->count < 56 )
289 hd->buf[hd->count++] = 0; /* pad */
290 }
291 else /* need one extra block */
292 {
293 hd->buf[hd->count++] = 0x80; /* pad character */
294 while( hd->count < 64 )
295 hd->buf[hd->count++] = 0;
296 sha1_write(hd, NULL, 0); /* flush */;
297 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
298 }
299 /* append the 64 bit count */
300 hd->buf[56] = msb >> 24;
301 hd->buf[57] = msb >> 16;
302 hd->buf[58] = msb >> 8;
303 hd->buf[59] = msb ;
304 hd->buf[60] = lsb >> 24;
305 hd->buf[61] = lsb >> 16;
306 hd->buf[62] = lsb >> 8;
307 hd->buf[63] = lsb ;
308 transform( hd, hd->buf );
309
310 p = hd->buf;
311 #ifdef WORDS_BIGENDIAN
312 #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
313 #else /* little endian */
314 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
315 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
316 #endif
317 X(0);
318 X(1);
319 X(2);
320 X(3);
321 X(4);
322 #undef X
323
324 }
325
326 void
327 sha1_hash_buffer (char *outbuf, const char *buffer, size_t length)
328 {
329 sha1_context_t hd;
330
331 sha1_init (&hd);
332 sha1_write (&hd, (unsigned char *)buffer, length);
333 sha1_final (&hd);
334 memcpy (outbuf, hd.buf, 20);
335 }