aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/hash/sha1extractor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/hash/sha1extractor.c')
-rw-r--r--src/plugins/hash/sha1extractor.c475
1 files changed, 0 insertions, 475 deletions
diff --git a/src/plugins/hash/sha1extractor.c b/src/plugins/hash/sha1extractor.c
deleted file mode 100644
index 5e899fd..0000000
--- a/src/plugins/hash/sha1extractor.c
+++ /dev/null
@@ -1,475 +0,0 @@
1/*
2 This file is part of libextractor.
3 (C) 2004 Vidyut Samanta and Christian Grothoff
4
5 libextractor is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 libextractor is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libextractor; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21#include "platform.h"
22#include "extractor.h"
23
24/* sha.c - Functions to compute SHA1 message digest of files or
25 memory blocks according to the NIST specification FIPS-180-1.
26
27 Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
28
29 This program is free software; you can redistribute it and/or modify it
30 under the terms of the GNU General Public License as published by the
31 Free Software Foundation; either version 2, or (at your option) any
32 later version.
33
34 This program is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 GNU General Public License for more details.
38
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software Foundation,
41 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
42
43/* Written by Scott G. Miller
44 Credits:
45 Robert Klep <robert@ilse.nl> -- Expansion function fix
46*/
47
48#ifdef _LIBC
49#include <stdint.h>
50typedef uint32_t md5_uint32;
51typedef uintptr_t md5_uintptr;
52#else
53# define UINT_MAX_32_BITS 4294967295U
54
55# if UINT_MAX == UINT_MAX_32_BITS
56typedef unsigned int md5_uint32;
57# else
58# if USHRT_MAX == UINT_MAX_32_BITS
59typedef unsigned short md5_uint32;
60# else
61# if ULONG_MAX == UINT_MAX_32_BITS
62typedef unsigned long md5_uint32;
63# else
64 /* The following line is intended to evoke an error.
65 Using #error is not portable enough. */
66"Cannot determine unsigned 32-bit data type."
67# endif
68# endif
69# endif
70/* We have to make a guess about the integer type equivalent in size
71 to pointers which should always be correct. */
72typedef unsigned long int md5_uintptr;
73#endif
74
75
76/* Structure to save state of computation between the single steps. */
77struct sha_ctx
78{
79 md5_uint32 A;
80 md5_uint32 B;
81 md5_uint32 C;
82 md5_uint32 D;
83 md5_uint32 E;
84
85 md5_uint32 total[2];
86 md5_uint32 buflen;
87 char buffer[128];
88};
89
90
91/* --- Code below is the primary difference between md5.c and sha.c --- */
92
93/* SHA1 round constants */
94#define K1 0x5a827999L
95#define K2 0x6ed9eba1L
96#define K3 0x8f1bbcdcL
97#define K4 0xca62c1d6L
98
99/* Round functions. Note that F2 is the same as F4. */
100#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
101#define F2(B,C,D) (B ^ C ^ D)
102#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
103#define F4(B,C,D) (B ^ C ^ D)
104
105#define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
106
107/*
108 Not-swap is a macro that does an endian swap on architectures that are
109 big-endian, as SHA needs some data in a little-endian format
110*/
111
112#ifdef WORDS_BIGENDIAN
113# define NOTSWAP(n) (n)
114# define SWAP(n) \
115 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
116#else
117# define NOTSWAP(n) \
118 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
119# define SWAP(n) (n)
120#endif
121
122#define BLOCKSIZE 4096
123/* Ensure that BLOCKSIZE is a multiple of 64. */
124#if BLOCKSIZE % 64 != 0
125/* FIXME-someday (soon?): use #error instead of this kludge. */
126"invalid BLOCKSIZE"
127#endif
128/* This array contains the bytes used to pad the buffer to the next
129 64-byte boundary. (RFC 1321, 3.1: Step 1) */
130static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
131
132
133
134/* Process LEN bytes of BUFFER, accumulating context into CTX.
135 It is assumed that LEN % 64 == 0.
136 Most of this code comes from GnuPG's cipher/sha1.c. */
137
138static void
139sha_process_block (const void *buffer, size_t len, struct sha_ctx *ctx)
140{
141 const md5_uint32 *words = buffer;
142 size_t nwords = len / sizeof (md5_uint32);
143 const md5_uint32 *endp = words + nwords;
144 md5_uint32 x[16];
145 md5_uint32 a = ctx->A;
146 md5_uint32 b = ctx->B;
147 md5_uint32 c = ctx->C;
148 md5_uint32 d = ctx->D;
149 md5_uint32 e = ctx->E;
150
151 /* First increment the byte count. RFC 1321 specifies the possible
152 length of the file up to 2^64 bits. Here we only compute the
153 number of bytes. Do a double word increment. */
154 ctx->total[0] += len;
155 if (ctx->total[0] < len)
156 ++ctx->total[1];
157
158#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
159 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
160 , (x[I&0x0f] = rol(tm, 1)) )
161
162#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
163 + F( B, C, D ) \
164 + K \
165 + M; \
166 B = rol( B, 30 ); \
167 } while(0)
168
169 while (words < endp)
170 {
171 md5_uint32 tm;
172 int t;
173 /* FIXME: see sha1.c for a better implementation. */
174 for (t = 0; t < 16; t++)
175 {
176 x[t] = NOTSWAP (*words);
177 words++;
178 }
179
180 R (a, b, c, d, e, F1, K1, x[0]);
181 R (e, a, b, c, d, F1, K1, x[1]);
182 R (d, e, a, b, c, F1, K1, x[2]);
183 R (c, d, e, a, b, F1, K1, x[3]);
184 R (b, c, d, e, a, F1, K1, x[4]);
185 R (a, b, c, d, e, F1, K1, x[5]);
186 R (e, a, b, c, d, F1, K1, x[6]);
187 R (d, e, a, b, c, F1, K1, x[7]);
188 R (c, d, e, a, b, F1, K1, x[8]);
189 R (b, c, d, e, a, F1, K1, x[9]);
190 R (a, b, c, d, e, F1, K1, x[10]);
191 R (e, a, b, c, d, F1, K1, x[11]);
192 R (d, e, a, b, c, F1, K1, x[12]);
193 R (c, d, e, a, b, F1, K1, x[13]);
194 R (b, c, d, e, a, F1, K1, x[14]);
195 R (a, b, c, d, e, F1, K1, x[15]);
196 R (e, a, b, c, d, F1, K1, M (16));
197 R (d, e, a, b, c, F1, K1, M (17));
198 R (c, d, e, a, b, F1, K1, M (18));
199 R (b, c, d, e, a, F1, K1, M (19));
200 R (a, b, c, d, e, F2, K2, M (20));
201 R (e, a, b, c, d, F2, K2, M (21));
202 R (d, e, a, b, c, F2, K2, M (22));
203 R (c, d, e, a, b, F2, K2, M (23));
204 R (b, c, d, e, a, F2, K2, M (24));
205 R (a, b, c, d, e, F2, K2, M (25));
206 R (e, a, b, c, d, F2, K2, M (26));
207 R (d, e, a, b, c, F2, K2, M (27));
208 R (c, d, e, a, b, F2, K2, M (28));
209 R (b, c, d, e, a, F2, K2, M (29));
210 R (a, b, c, d, e, F2, K2, M (30));
211 R (e, a, b, c, d, F2, K2, M (31));
212 R (d, e, a, b, c, F2, K2, M (32));
213 R (c, d, e, a, b, F2, K2, M (33));
214 R (b, c, d, e, a, F2, K2, M (34));
215 R (a, b, c, d, e, F2, K2, M (35));
216 R (e, a, b, c, d, F2, K2, M (36));
217 R (d, e, a, b, c, F2, K2, M (37));
218 R (c, d, e, a, b, F2, K2, M (38));
219 R (b, c, d, e, a, F2, K2, M (39));
220 R (a, b, c, d, e, F3, K3, M (40));
221 R (e, a, b, c, d, F3, K3, M (41));
222 R (d, e, a, b, c, F3, K3, M (42));
223 R (c, d, e, a, b, F3, K3, M (43));
224 R (b, c, d, e, a, F3, K3, M (44));
225 R (a, b, c, d, e, F3, K3, M (45));
226 R (e, a, b, c, d, F3, K3, M (46));
227 R (d, e, a, b, c, F3, K3, M (47));
228 R (c, d, e, a, b, F3, K3, M (48));
229 R (b, c, d, e, a, F3, K3, M (49));
230 R (a, b, c, d, e, F3, K3, M (50));
231 R (e, a, b, c, d, F3, K3, M (51));
232 R (d, e, a, b, c, F3, K3, M (52));
233 R (c, d, e, a, b, F3, K3, M (53));
234 R (b, c, d, e, a, F3, K3, M (54));
235 R (a, b, c, d, e, F3, K3, M (55));
236 R (e, a, b, c, d, F3, K3, M (56));
237 R (d, e, a, b, c, F3, K3, M (57));
238 R (c, d, e, a, b, F3, K3, M (58));
239 R (b, c, d, e, a, F3, K3, M (59));
240 R (a, b, c, d, e, F4, K4, M (60));
241 R (e, a, b, c, d, F4, K4, M (61));
242 R (d, e, a, b, c, F4, K4, M (62));
243 R (c, d, e, a, b, F4, K4, M (63));
244 R (b, c, d, e, a, F4, K4, M (64));
245 R (a, b, c, d, e, F4, K4, M (65));
246 R (e, a, b, c, d, F4, K4, M (66));
247 R (d, e, a, b, c, F4, K4, M (67));
248 R (c, d, e, a, b, F4, K4, M (68));
249 R (b, c, d, e, a, F4, K4, M (69));
250 R (a, b, c, d, e, F4, K4, M (70));
251 R (e, a, b, c, d, F4, K4, M (71));
252 R (d, e, a, b, c, F4, K4, M (72));
253 R (c, d, e, a, b, F4, K4, M (73));
254 R (b, c, d, e, a, F4, K4, M (74));
255 R (a, b, c, d, e, F4, K4, M (75));
256 R (e, a, b, c, d, F4, K4, M (76));
257 R (d, e, a, b, c, F4, K4, M (77));
258 R (c, d, e, a, b, F4, K4, M (78));
259 R (b, c, d, e, a, F4, K4, M (79));
260
261 a = ctx->A += a;
262 b = ctx->B += b;
263 c = ctx->C += c;
264 d = ctx->D += d;
265 e = ctx->E += e;
266 }
267}
268
269
270
271static void
272sha_process_bytes (const void *buffer, size_t len, struct sha_ctx *ctx)
273{
274 /* When we already have some bits in our internal buffer concatenate
275 both inputs first. */
276 if (ctx->buflen != 0)
277 {
278 size_t left_over = ctx->buflen;
279 size_t add = 128 - left_over > len ? len : 128 - left_over;
280
281 memcpy (&ctx->buffer[left_over], buffer, add);
282 ctx->buflen += add;
283
284 if (ctx->buflen > 64)
285 {
286 sha_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
287
288 ctx->buflen &= 63;
289 /* The regions in the following copy operation cannot overlap. */
290 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
291 ctx->buflen);
292 }
293
294 buffer = (const char *) buffer + add;
295 len -= add;
296 }
297
298 /* Process available complete blocks. */
299 if (len >= 64)
300 {
301#if !_STRING_ARCH_unaligned
302/* To check alignment gcc has an appropriate operator. Other
303 compilers don't. */
304# if __GNUC__ >= 2
305# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
306# else
307# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
308# endif
309 if (UNALIGNED_P (buffer))
310 while (len > 64)
311 {
312 sha_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
313 buffer = (const char *) buffer + 64;
314 len -= 64;
315 }
316 else
317#endif
318 {
319 sha_process_block (buffer, len & ~63, ctx);
320 buffer = (const char *) buffer + (len & ~63);
321 len &= 63;
322 }
323 }
324
325 /* Move remaining bytes in internal buffer. */
326 if (len > 0)
327 {
328 size_t left_over = ctx->buflen;
329
330 memcpy (&ctx->buffer[left_over], buffer, len);
331 left_over += len;
332 if (left_over >= 64)
333 {
334 sha_process_block (ctx->buffer, 64, ctx);
335 left_over -= 64;
336 memcpy (ctx->buffer, &ctx->buffer[64], left_over);
337 }
338 ctx->buflen = left_over;
339 }
340}
341
342
343/*
344 Takes a pointer to a 160 bit block of data (five 32 bit ints) and
345 intializes it to the start constants of the SHA1 algorithm. This
346 must be called before using hash in the call to sha_hash
347*/
348static void
349sha_init_ctx (struct sha_ctx *ctx)
350{
351 ctx->A = 0x67452301;
352 ctx->B = 0xefcdab89;
353 ctx->C = 0x98badcfe;
354 ctx->D = 0x10325476;
355 ctx->E = 0xc3d2e1f0;
356
357 ctx->total[0] = ctx->total[1] = 0;
358 ctx->buflen = 0;
359}
360
361/* Put result from CTX in first 20 bytes following RESBUF. The result
362 must be in little endian byte order.
363
364 IMPORTANT: On some systems it is required that RESBUF is correctly
365 aligned for a 32 bits value. */
366static void *
367sha_read_ctx (const struct sha_ctx *ctx, void *resbuf)
368{
369 ((md5_uint32 *) resbuf)[0] = NOTSWAP (ctx->A);
370 ((md5_uint32 *) resbuf)[1] = NOTSWAP (ctx->B);
371 ((md5_uint32 *) resbuf)[2] = NOTSWAP (ctx->C);
372 ((md5_uint32 *) resbuf)[3] = NOTSWAP (ctx->D);
373 ((md5_uint32 *) resbuf)[4] = NOTSWAP (ctx->E);
374
375 return resbuf;
376}
377
378/* Process the remaining bytes in the internal buffer and the usual
379 prolog according to the standard and write the result to RESBUF.
380
381 IMPORTANT: On some systems it is required that RESBUF is correctly
382 aligned for a 32 bits value. */
383static void *
384sha_finish_ctx (struct sha_ctx *ctx, void *resbuf)
385{
386 /* Take yet unprocessed bytes into account. */
387 md5_uint32 bytes = ctx->buflen;
388 size_t pad;
389
390 /* Now count remaining bytes. */
391 ctx->total[0] += bytes;
392 if (ctx->total[0] < bytes)
393 ++ctx->total[1];
394
395 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
396 memcpy (&ctx->buffer[bytes], fillbuf, pad);
397
398 /* Put the 64-bit file length in *bits* at the end of the buffer. */
399 *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] =
400 NOTSWAP (ctx->total[0] << 3);
401 *(md5_uint32 *) & ctx->buffer[bytes + pad] =
402 NOTSWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
403
404 /* Process last bytes. */
405 sha_process_block (ctx->buffer, bytes + pad + 8, ctx);
406
407 return sha_read_ctx (ctx, resbuf);
408}
409
410
411/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
412 result is always in little endian byte order, so that a byte-wise
413 output yields to the wanted ASCII representation of the message
414 digest. */
415static void *
416sha_buffer (const char *buffer, size_t len, void *resblock)
417{
418 struct sha_ctx ctx;
419
420 /* Initialize the computation context. */
421 sha_init_ctx (&ctx);
422
423 /* Process whole buffer but last len % 64 bytes. */
424 sha_process_bytes (buffer, len, &ctx);
425
426 /* Put result in desired memory area. */
427 return sha_finish_ctx (&ctx, resblock);
428}
429
430
431
432
433
434
435
436static struct EXTRACTOR_Keywords *
437addKeyword (EXTRACTOR_KeywordList * oldhead,
438 const char *phrase, EXTRACTOR_KeywordType type)
439{
440
441 EXTRACTOR_KeywordList *keyword;
442 keyword = (EXTRACTOR_KeywordList *) malloc (sizeof (EXTRACTOR_KeywordList));
443 keyword->next = oldhead;
444 keyword->keyword = strdup (phrase);
445 keyword->keywordType = type;
446 return keyword;
447}
448
449#define DIGEST_BITS 160
450#define DIGEST_HEX_BYTES (DIGEST_BITS / 4)
451#define DIGEST_BIN_BYTES (DIGEST_BITS / 8)
452
453#define MAX_DIGEST_BIN_BYTES DIGEST_BIN_BYTES
454
455struct EXTRACTOR_Keywords *
456libextractor_hash_sha1_extract (const char *filename,
457 char *data,
458 size_t size, struct EXTRACTOR_Keywords *prev)
459{
460 unsigned char bin_buffer[MAX_DIGEST_BIN_BYTES];
461 char hash[8 * MAX_DIGEST_BIN_BYTES];
462 char buf[16];
463 int i;
464
465 sha_buffer (data, size, bin_buffer);
466
467 hash[0] = '\0';
468 for (i = 0; i < DIGEST_HEX_BYTES / 2; i++)
469 {
470 snprintf (buf, 16, "%02x", bin_buffer[i]);
471 strcat (hash, buf);
472 }
473 prev = addKeyword (prev, hash, EXTRACTOR_HASH_SHA1);
474 return prev;
475}