A few final changes for the 3.0.6 release
[pacman.git] / lib / libalpm / sha1.c
blobfd5f1e4adc2cd5555f9749f88f4463d90e16989f
1 /* sha.c - Functions to compute SHA1 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-1.
4 Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This program 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 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by Scott G. Miller
21 Credits:
22 Robert Klep <robert@ilse.nl> -- Expansion function fix
25 #include "config.h"
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <libintl.h>
32 /* libalpm */
33 #include "sha1.h"
34 #include "alpm.h"
35 #include "log.h"
36 #include "util.h"
39 Not-swap is a macro that does an endian swap on architectures that are
40 big-endian, as SHA needs some data in a little-endian format
43 #ifdef WORDS_BIGENDIAN
44 # define NOTSWAP(n) (n)
45 # define SWAP(n) \
46 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
47 #else
48 # define NOTSWAP(n) \
49 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
50 # define SWAP(n) (n)
51 #endif
53 #define BLOCKSIZE 4096
54 /* Ensure that BLOCKSIZE is a multiple of 64. */
55 #if BLOCKSIZE % 64 != 0
56 /* FIXME-someday (soon?): use #error instead of this kludge. */
57 "invalid BLOCKSIZE"
58 #endif
60 /* This array contains the bytes used to pad the buffer to the next
61 64-byte boundary. (RFC 1321, 3.1: Step 1) */
62 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
65 /* Starting with the result of former calls of this function (or the
66 initialization function update the context for the next LEN bytes
67 starting at BUFFER.
68 It is necessary that LEN is a multiple of 64!!! */
69 static void sha_process_block (const void *buffer, size_t len,
70 struct sha_ctx *ctx);
72 /* Starting with the result of former calls of this function (or the
73 initialization function update the context for the next LEN bytes
74 starting at BUFFER.
75 It is NOT required that LEN is a multiple of 64. */
76 static void sha_process_bytes (const void *buffer, size_t len,
77 struct sha_ctx *ctx);
79 /* Put result from CTX in first 20 bytes following RESBUF. The result is
80 always in little endian byte order, so that a byte-wise output yields
81 to the wanted ASCII representation of the message digest.
83 IMPORTANT: On some systems it is required that RESBUF is correctly
84 aligned for a 32 bits value. */
85 static void *sha_read_ctx (const struct sha_ctx *ctx, void *resbuf);
88 Takes a pointer to a 160 bit block of data (five 32 bit ints) and
89 intializes it to the start constants of the SHA1 algorithm. This
90 must be called before using hash in the call to sha_hash
92 static void
93 sha_init_ctx (struct sha_ctx *ctx)
95 ctx->A = 0x67452301;
96 ctx->B = 0xefcdab89;
97 ctx->C = 0x98badcfe;
98 ctx->D = 0x10325476;
99 ctx->E = 0xc3d2e1f0;
101 ctx->total[0] = ctx->total[1] = 0;
102 ctx->buflen = 0;
105 /* Put result from CTX in first 20 bytes following RESBUF. The result
106 must be in little endian byte order.
108 IMPORTANT: On some systems it is required that RESBUF is correctly
109 aligned for a 32 bits value. */
110 static void *
111 sha_read_ctx (const struct sha_ctx *ctx, void *resbuf)
113 ((sha_uint32 *) resbuf)[0] = NOTSWAP (ctx->A);
114 ((sha_uint32 *) resbuf)[1] = NOTSWAP (ctx->B);
115 ((sha_uint32 *) resbuf)[2] = NOTSWAP (ctx->C);
116 ((sha_uint32 *) resbuf)[3] = NOTSWAP (ctx->D);
117 ((sha_uint32 *) resbuf)[4] = NOTSWAP (ctx->E);
119 return resbuf;
122 /* Process the remaining bytes in the internal buffer and the usual
123 prolog according to the standard and write the result to RESBUF.
125 IMPORTANT: On some systems it is required that RESBUF is correctly
126 aligned for a 32 bits value. */
127 static void *
128 sha_finish_ctx (struct sha_ctx *ctx, void *resbuf)
130 /* Take yet unprocessed bytes into account. */
131 sha_uint32 bytes = ctx->buflen;
132 size_t pad;
134 /* Now count remaining bytes. */
135 ctx->total[0] += bytes;
136 if (ctx->total[0] < bytes)
137 ++ctx->total[1];
139 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
140 memcpy (&ctx->buffer[bytes], fillbuf, pad);
142 /* Put the 64-bit file length in *bits* at the end of the buffer. */
143 *(sha_uint32 *) &ctx->buffer[bytes + pad + 4] = NOTSWAP (ctx->total[0] << 3);
144 *(sha_uint32 *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) |
145 (ctx->total[0] >> 29));
147 /* Process last bytes. */
148 sha_process_block (ctx->buffer, bytes + pad + 8, ctx);
150 return sha_read_ctx (ctx, resbuf);
153 static void
154 sha_process_bytes (const void *buffer, size_t len, struct sha_ctx *ctx)
156 /* When we already have some bits in our internal buffer concatenate
157 both inputs first. */
158 if (ctx->buflen != 0)
160 size_t left_over = ctx->buflen;
161 size_t add = 128 - left_over > len ? len : 128 - left_over;
163 memcpy (&ctx->buffer[left_over], buffer, add);
164 ctx->buflen += add;
166 if (ctx->buflen > 64)
168 sha_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
170 ctx->buflen &= 63;
171 /* The regions in the following copy operation cannot overlap. */
172 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
173 ctx->buflen);
176 buffer = (const char *) buffer + add;
177 len -= add;
180 /* Process available complete blocks. */
181 if (len >= 64)
183 #if !_STRING_ARCH_unaligned
184 /* To check alignment gcc has an appropriate operator. Other
185 compilers don't. */
186 # if __GNUC__ >= 2
187 # define UNALIGNED_P(p) (((sha_uintptr) p) % __alignof__ (sha_uint32) != 0)
188 # else
189 # define UNALIGNED_P(p) (((sha_uintptr) p) % sizeof (sha_uint32) != 0)
190 # endif
191 if (UNALIGNED_P (buffer))
192 while (len > 64)
194 sha_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
195 buffer = (const char *) buffer + 64;
196 len -= 64;
198 else
199 #endif
201 sha_process_block (buffer, len & ~63, ctx);
202 buffer = (const char *) buffer + (len & ~63);
203 len &= 63;
207 /* Move remaining bytes in internal buffer. */
208 if (len > 0)
210 size_t left_over = ctx->buflen;
212 memcpy (&ctx->buffer[left_over], buffer, len);
213 left_over += len;
214 if (left_over >= 64)
216 sha_process_block (ctx->buffer, 64, ctx);
217 left_over -= 64;
218 memcpy (ctx->buffer, &ctx->buffer[64], left_over);
220 ctx->buflen = left_over;
224 /* --- Code below is the primary difference between md5.c and sha.c --- */
226 /* SHA1 round constants */
227 #define K1 0x5a827999L
228 #define K2 0x6ed9eba1L
229 #define K3 0x8f1bbcdcL
230 #define K4 0xca62c1d6L
232 /* Round functions. Note that F2 is the same as F4. */
233 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
234 #define F2(B,C,D) (B ^ C ^ D)
235 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
236 #define F4(B,C,D) (B ^ C ^ D)
238 /* Process LEN bytes of BUFFER, accumulating context into CTX.
239 It is assumed that LEN % 64 == 0.
240 Most of this code comes from GnuPG's cipher/sha1.c. */
242 static void
243 sha_process_block (const void *buffer, size_t len, struct sha_ctx *ctx)
245 const sha_uint32 *words = buffer;
246 size_t nwords = len / sizeof (sha_uint32);
247 const sha_uint32 *endp = words + nwords;
248 sha_uint32 x[16];
249 sha_uint32 a = ctx->A;
250 sha_uint32 b = ctx->B;
251 sha_uint32 c = ctx->C;
252 sha_uint32 d = ctx->D;
253 sha_uint32 e = ctx->E;
255 /* First increment the byte count. RFC 1321 specifies the possible
256 length of the file up to 2^64 bits. Here we only compute the
257 number of bytes. Do a double word increment. */
258 ctx->total[0] += len;
259 if (ctx->total[0] < len)
260 ++ctx->total[1];
262 #define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
263 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
264 , (x[I&0x0f] = rol(tm, 1)) )
266 #define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
267 + F( B, C, D ) \
268 + K \
269 + M; \
270 B = rol( B, 30 ); \
271 } while(0)
273 while (words < endp)
275 sha_uint32 tm;
276 int t;
277 /* FIXME: see sha1.c for a better implementation. */
278 for (t = 0; t < 16; t++)
280 x[t] = NOTSWAP (*words);
281 words++;
284 R( a, b, c, d, e, F1, K1, x[ 0] );
285 R( e, a, b, c, d, F1, K1, x[ 1] );
286 R( d, e, a, b, c, F1, K1, x[ 2] );
287 R( c, d, e, a, b, F1, K1, x[ 3] );
288 R( b, c, d, e, a, F1, K1, x[ 4] );
289 R( a, b, c, d, e, F1, K1, x[ 5] );
290 R( e, a, b, c, d, F1, K1, x[ 6] );
291 R( d, e, a, b, c, F1, K1, x[ 7] );
292 R( c, d, e, a, b, F1, K1, x[ 8] );
293 R( b, c, d, e, a, F1, K1, x[ 9] );
294 R( a, b, c, d, e, F1, K1, x[10] );
295 R( e, a, b, c, d, F1, K1, x[11] );
296 R( d, e, a, b, c, F1, K1, x[12] );
297 R( c, d, e, a, b, F1, K1, x[13] );
298 R( b, c, d, e, a, F1, K1, x[14] );
299 R( a, b, c, d, e, F1, K1, x[15] );
300 R( e, a, b, c, d, F1, K1, M(16) );
301 R( d, e, a, b, c, F1, K1, M(17) );
302 R( c, d, e, a, b, F1, K1, M(18) );
303 R( b, c, d, e, a, F1, K1, M(19) );
304 R( a, b, c, d, e, F2, K2, M(20) );
305 R( e, a, b, c, d, F2, K2, M(21) );
306 R( d, e, a, b, c, F2, K2, M(22) );
307 R( c, d, e, a, b, F2, K2, M(23) );
308 R( b, c, d, e, a, F2, K2, M(24) );
309 R( a, b, c, d, e, F2, K2, M(25) );
310 R( e, a, b, c, d, F2, K2, M(26) );
311 R( d, e, a, b, c, F2, K2, M(27) );
312 R( c, d, e, a, b, F2, K2, M(28) );
313 R( b, c, d, e, a, F2, K2, M(29) );
314 R( a, b, c, d, e, F2, K2, M(30) );
315 R( e, a, b, c, d, F2, K2, M(31) );
316 R( d, e, a, b, c, F2, K2, M(32) );
317 R( c, d, e, a, b, F2, K2, M(33) );
318 R( b, c, d, e, a, F2, K2, M(34) );
319 R( a, b, c, d, e, F2, K2, M(35) );
320 R( e, a, b, c, d, F2, K2, M(36) );
321 R( d, e, a, b, c, F2, K2, M(37) );
322 R( c, d, e, a, b, F2, K2, M(38) );
323 R( b, c, d, e, a, F2, K2, M(39) );
324 R( a, b, c, d, e, F3, K3, M(40) );
325 R( e, a, b, c, d, F3, K3, M(41) );
326 R( d, e, a, b, c, F3, K3, M(42) );
327 R( c, d, e, a, b, F3, K3, M(43) );
328 R( b, c, d, e, a, F3, K3, M(44) );
329 R( a, b, c, d, e, F3, K3, M(45) );
330 R( e, a, b, c, d, F3, K3, M(46) );
331 R( d, e, a, b, c, F3, K3, M(47) );
332 R( c, d, e, a, b, F3, K3, M(48) );
333 R( b, c, d, e, a, F3, K3, M(49) );
334 R( a, b, c, d, e, F3, K3, M(50) );
335 R( e, a, b, c, d, F3, K3, M(51) );
336 R( d, e, a, b, c, F3, K3, M(52) );
337 R( c, d, e, a, b, F3, K3, M(53) );
338 R( b, c, d, e, a, F3, K3, M(54) );
339 R( a, b, c, d, e, F3, K3, M(55) );
340 R( e, a, b, c, d, F3, K3, M(56) );
341 R( d, e, a, b, c, F3, K3, M(57) );
342 R( c, d, e, a, b, F3, K3, M(58) );
343 R( b, c, d, e, a, F3, K3, M(59) );
344 R( a, b, c, d, e, F4, K4, M(60) );
345 R( e, a, b, c, d, F4, K4, M(61) );
346 R( d, e, a, b, c, F4, K4, M(62) );
347 R( c, d, e, a, b, F4, K4, M(63) );
348 R( b, c, d, e, a, F4, K4, M(64) );
349 R( a, b, c, d, e, F4, K4, M(65) );
350 R( e, a, b, c, d, F4, K4, M(66) );
351 R( d, e, a, b, c, F4, K4, M(67) );
352 R( c, d, e, a, b, F4, K4, M(68) );
353 R( b, c, d, e, a, F4, K4, M(69) );
354 R( a, b, c, d, e, F4, K4, M(70) );
355 R( e, a, b, c, d, F4, K4, M(71) );
356 R( d, e, a, b, c, F4, K4, M(72) );
357 R( c, d, e, a, b, F4, K4, M(73) );
358 R( b, c, d, e, a, F4, K4, M(74) );
359 R( a, b, c, d, e, F4, K4, M(75) );
360 R( e, a, b, c, d, F4, K4, M(76) );
361 R( d, e, a, b, c, F4, K4, M(77) );
362 R( c, d, e, a, b, F4, K4, M(78) );
363 R( b, c, d, e, a, F4, K4, M(79) );
365 a = ctx->A += a;
366 b = ctx->B += b;
367 c = ctx->C += c;
368 d = ctx->D += d;
369 e = ctx->E += e;
373 /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
374 rights reserved.
376 RSA Data Security, Inc. makes no representations concerning either
377 the merchantability of this software or the suitability of this
378 software for any particular purpose. It is provided "as is"
379 without express or implied warranty of any kind.
381 These notices must be retained in any copies of any part of this
382 documentation and/or software.
386 char* _alpm_SHAFile(char *filename) {
387 FILE *file;
388 struct sha_ctx context;
389 int len, i;
390 char hex[3];
391 unsigned char buffer[1024], digest[20];
392 char *ret;
394 ALPM_LOG_FUNC;
396 if((file = fopen(filename, "rb")) == NULL) {
397 _alpm_log(PM_LOG_ERROR, _("sha1: %s can't be opened\n"), filename);
398 } else {
399 sha_init_ctx(&context);
400 while((len = fread(buffer, 1, 1024, file))) {
401 sha_process_bytes(buffer, len, &context);
403 sha_finish_ctx(&context, digest);
404 fclose(file);
406 ret = (char*)malloc(41);
407 ret[0] = '\0';
408 for(i = 0; i < 20; i++) {
409 snprintf(hex, 3, "%02x", digest[i]);
410 strncat(ret, hex, 2);
412 _alpm_log(PM_LOG_DEBUG, _("sha1(%s) = %s"), filename, ret);
413 return(ret);
416 return(NULL);
419 /* vim: set ts=2 sw=2 noet: */