~ubuntu-branches/debian/sid/dico/sid

« back to all changes in this revision

Viewing changes to gnu/sha1.c

  • Committer: Bazaar Package Importer
  • Author(s): أحمد المحمودي (Ahmed El-Mahmoudy)
  • Date: 2010-07-08 12:08:56 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100708120856-nsqktvovscghtbcm
* New upstream release (Closes: #588403)
* Refreshed patch dicoweb-debian.diff
* Removed patches that are applied upstream:
  0001-Speed-up-output-procedure-in-dictorg.patch,
  improve_dicoweb.patch
* debian/rules: Upstream renamed settings.py to settings-sample.py,
  hence install settings-sample.py in /etc/dicoweb/
* debian/control:
  + Bumped Standards-Version to 3.9.0, no changes needed.
  + Drop XB-Python-Version fields and ${python:Provides}, as they are not
    needed.
  + Set DMUA (after approval of Marc Dequènes (Duck) <duck@debian.org>)
  + Remove libdico from Depends field of dico & dicod, it should be
    added automatically.
  + Remove python-dev from B-D, since python-all-dev is there
  + Add libldap2-dev to B-D to compile LDAP module.
  + Add list of included modules in dicod extended description.
* debian/dicod.conf:
  + Match everything strategy is disabled by not loading the stratall
    module.
  + Added a commented section to enable substr strategy.
* debian/dicod.install: Install ldap, substr & stratall modules in dicod
* debian/dico-module-*.install: For pluggable modules, only install the .so
  files.
* debian/dico-dev.install: Do not install .la file
  (See: http://wiki.debian.org/ReleaseGoals/LAFileRemoval)
* debian/watch: Add check for bzip2'ed tarballs
* Renamed libdico package due to bumped sonames
* debian/libdico1.symbols: Updated symbols
* Merge dictorg & outline modules into dicod package.
* debian/dicod.conf: Enable dictorg module by default (Closes: #588402).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- buffer-read-only: t -*- vi: set ro: */
 
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
 
3
/* sha1.c - Functions to compute SHA1 message digest of files or
 
4
   memory blocks according to the NIST specification FIPS-180-1.
 
5
 
 
6
   Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2008, 2009, 2010 Free
 
7
   Software Foundation, Inc.
 
8
 
 
9
   This program is free software; you can redistribute it and/or modify it
 
10
   under the terms of the GNU General Public License as published by the
 
11
   Free Software Foundation; either version 3, or (at your option) any
 
12
   later version.
 
13
 
 
14
   This program is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
   GNU General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU General Public License
 
20
   along with this program; if not, write to the Free Software Foundation,
 
21
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
22
 
 
23
/* Written by Scott G. Miller
 
24
   Credits:
 
25
      Robert Klep <robert@ilse.nl>  -- Expansion function fix
 
26
*/
 
27
 
 
28
#include <config.h>
 
29
 
 
30
#include "sha1.h"
 
31
 
 
32
#include <stddef.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
 
 
36
#if USE_UNLOCKED_IO
 
37
# include "unlocked-io.h"
 
38
#endif
 
39
 
 
40
#ifdef WORDS_BIGENDIAN
 
41
# define SWAP(n) (n)
 
42
#else
 
43
# define SWAP(n) \
 
44
    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
 
45
#endif
 
46
 
 
47
#define BLOCKSIZE 32768
 
48
#if BLOCKSIZE % 64 != 0
 
49
# error "invalid BLOCKSIZE"
 
50
#endif
 
51
 
 
52
/* This array contains the bytes used to pad the buffer to the next
 
53
   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
 
54
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
 
55
 
 
56
 
 
57
/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
 
58
   initialize it to the start constants of the SHA1 algorithm.  This
 
59
   must be called before using hash in the call to sha1_hash.  */
 
60
void
 
61
sha1_init_ctx (struct sha1_ctx *ctx)
 
62
{
 
63
  ctx->A = 0x67452301;
 
64
  ctx->B = 0xefcdab89;
 
65
  ctx->C = 0x98badcfe;
 
66
  ctx->D = 0x10325476;
 
67
  ctx->E = 0xc3d2e1f0;
 
68
 
 
69
  ctx->total[0] = ctx->total[1] = 0;
 
70
  ctx->buflen = 0;
 
71
}
 
72
 
 
73
/* Copy the 4 byte value from v into the memory location pointed to by *cp,
 
74
   If your architecture allows unaligned access this is equivalent to
 
75
   * (uint32_t *) cp = v  */
 
76
static inline void
 
77
set_uint32 (char *cp, uint32_t v)
 
78
{
 
79
  memcpy (cp, &v, sizeof v);
 
80
}
 
81
 
 
82
/* Put result from CTX in first 20 bytes following RESBUF.  The result
 
83
   must be in little endian byte order.  */
 
84
void *
 
85
sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
 
86
{
 
87
  char *r = resbuf;
 
88
  set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
 
89
  set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
 
90
  set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
 
91
  set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
 
92
  set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
 
93
 
 
94
  return resbuf;
 
95
}
 
96
 
 
97
/* Process the remaining bytes in the internal buffer and the usual
 
98
   prolog according to the standard and write the result to RESBUF.  */
 
99
void *
 
100
sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
 
101
{
 
102
  /* Take yet unprocessed bytes into account.  */
 
103
  uint32_t bytes = ctx->buflen;
 
104
  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
 
105
 
 
106
  /* Now count remaining bytes.  */
 
107
  ctx->total[0] += bytes;
 
108
  if (ctx->total[0] < bytes)
 
109
    ++ctx->total[1];
 
110
 
 
111
  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
 
112
  ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
 
113
  ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
 
114
 
 
115
  memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
 
116
 
 
117
  /* Process last bytes.  */
 
118
  sha1_process_block (ctx->buffer, size * 4, ctx);
 
119
 
 
120
  return sha1_read_ctx (ctx, resbuf);
 
121
}
 
122
 
 
123
/* Compute SHA1 message digest for bytes read from STREAM.  The
 
124
   resulting message digest number will be written into the 16 bytes
 
125
   beginning at RESBLOCK.  */
 
126
int
 
127
sha1_stream (FILE *stream, void *resblock)
 
128
{
 
129
  struct sha1_ctx ctx;
 
130
  size_t sum;
 
131
 
 
132
  char *buffer = malloc (BLOCKSIZE + 72);
 
133
  if (!buffer)
 
134
    return 1;
 
135
 
 
136
  /* Initialize the computation context.  */
 
137
  sha1_init_ctx (&ctx);
 
138
 
 
139
  /* Iterate over full file contents.  */
 
140
  while (1)
 
141
    {
 
142
      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
 
143
         computation function processes the whole buffer so that with the
 
144
         next round of the loop another block can be read.  */
 
145
      size_t n;
 
146
      sum = 0;
 
147
 
 
148
      /* Read block.  Take care for partial reads.  */
 
149
      while (1)
 
150
        {
 
151
          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
152
 
 
153
          sum += n;
 
154
 
 
155
          if (sum == BLOCKSIZE)
 
156
            break;
 
157
 
 
158
          if (n == 0)
 
159
            {
 
160
              /* Check for the error flag IFF N == 0, so that we don't
 
161
                 exit the loop after a partial read due to e.g., EAGAIN
 
162
                 or EWOULDBLOCK.  */
 
163
              if (ferror (stream))
 
164
                {
 
165
                  free (buffer);
 
166
                  return 1;
 
167
                }
 
168
              goto process_partial_block;
 
169
            }
 
170
 
 
171
          /* We've read at least one byte, so ignore errors.  But always
 
172
             check for EOF, since feof may be true even though N > 0.
 
173
             Otherwise, we could end up calling fread after EOF.  */
 
174
          if (feof (stream))
 
175
            goto process_partial_block;
 
176
        }
 
177
 
 
178
      /* Process buffer with BLOCKSIZE bytes.  Note that
 
179
                        BLOCKSIZE % 64 == 0
 
180
       */
 
181
      sha1_process_block (buffer, BLOCKSIZE, &ctx);
 
182
    }
 
183
 
 
184
 process_partial_block:;
 
185
 
 
186
  /* Process any remaining bytes.  */
 
187
  if (sum > 0)
 
188
    sha1_process_bytes (buffer, sum, &ctx);
 
189
 
 
190
  /* Construct result in desired memory.  */
 
191
  sha1_finish_ctx (&ctx, resblock);
 
192
  free (buffer);
 
193
  return 0;
 
194
}
 
195
 
 
196
/* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
 
197
   result is always in little endian byte order, so that a byte-wise
 
198
   output yields to the wanted ASCII representation of the message
 
199
   digest.  */
 
200
void *
 
201
sha1_buffer (const char *buffer, size_t len, void *resblock)
 
202
{
 
203
  struct sha1_ctx ctx;
 
204
 
 
205
  /* Initialize the computation context.  */
 
206
  sha1_init_ctx (&ctx);
 
207
 
 
208
  /* Process whole buffer but last len % 64 bytes.  */
 
209
  sha1_process_bytes (buffer, len, &ctx);
 
210
 
 
211
  /* Put result in desired memory area.  */
 
212
  return sha1_finish_ctx (&ctx, resblock);
 
213
}
 
214
 
 
215
void
 
216
sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
 
217
{
 
218
  /* When we already have some bits in our internal buffer concatenate
 
219
     both inputs first.  */
 
220
  if (ctx->buflen != 0)
 
221
    {
 
222
      size_t left_over = ctx->buflen;
 
223
      size_t add = 128 - left_over > len ? len : 128 - left_over;
 
224
 
 
225
      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
 
226
      ctx->buflen += add;
 
227
 
 
228
      if (ctx->buflen > 64)
 
229
        {
 
230
          sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 
231
 
 
232
          ctx->buflen &= 63;
 
233
          /* The regions in the following copy operation cannot overlap.  */
 
234
          memcpy (ctx->buffer,
 
235
                  &((char *) ctx->buffer)[(left_over + add) & ~63],
 
236
                  ctx->buflen);
 
237
        }
 
238
 
 
239
      buffer = (const char *) buffer + add;
 
240
      len -= add;
 
241
    }
 
242
 
 
243
  /* Process available complete blocks.  */
 
244
  if (len >= 64)
 
245
    {
 
246
#if !_STRING_ARCH_unaligned
 
247
# define alignof(type) offsetof (struct { char c; type x; }, x)
 
248
# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
 
249
      if (UNALIGNED_P (buffer))
 
250
        while (len > 64)
 
251
          {
 
252
            sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
 
253
            buffer = (const char *) buffer + 64;
 
254
            len -= 64;
 
255
          }
 
256
      else
 
257
#endif
 
258
        {
 
259
          sha1_process_block (buffer, len & ~63, ctx);
 
260
          buffer = (const char *) buffer + (len & ~63);
 
261
          len &= 63;
 
262
        }
 
263
    }
 
264
 
 
265
  /* Move remaining bytes in internal buffer.  */
 
266
  if (len > 0)
 
267
    {
 
268
      size_t left_over = ctx->buflen;
 
269
 
 
270
      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
 
271
      left_over += len;
 
272
      if (left_over >= 64)
 
273
        {
 
274
          sha1_process_block (ctx->buffer, 64, ctx);
 
275
          left_over -= 64;
 
276
          memcpy (ctx->buffer, &ctx->buffer[16], left_over);
 
277
        }
 
278
      ctx->buflen = left_over;
 
279
    }
 
280
}
 
281
 
 
282
/* --- Code below is the primary difference between md5.c and sha1.c --- */
 
283
 
 
284
/* SHA1 round constants */
 
285
#define K1 0x5a827999
 
286
#define K2 0x6ed9eba1
 
287
#define K3 0x8f1bbcdc
 
288
#define K4 0xca62c1d6
 
289
 
 
290
/* Round functions.  Note that F2 is the same as F4.  */
 
291
#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
 
292
#define F2(B,C,D) (B ^ C ^ D)
 
293
#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
 
294
#define F4(B,C,D) (B ^ C ^ D)
 
295
 
 
296
/* Process LEN bytes of BUFFER, accumulating context into CTX.
 
297
   It is assumed that LEN % 64 == 0.
 
298
   Most of this code comes from GnuPG's cipher/sha1.c.  */
 
299
 
 
300
void
 
301
sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
 
302
{
 
303
  const uint32_t *words = buffer;
 
304
  size_t nwords = len / sizeof (uint32_t);
 
305
  const uint32_t *endp = words + nwords;
 
306
  uint32_t x[16];
 
307
  uint32_t a = ctx->A;
 
308
  uint32_t b = ctx->B;
 
309
  uint32_t c = ctx->C;
 
310
  uint32_t d = ctx->D;
 
311
  uint32_t e = ctx->E;
 
312
 
 
313
  /* First increment the byte count.  RFC 1321 specifies the possible
 
314
     length of the file up to 2^64 bits.  Here we only compute the
 
315
     number of bytes.  Do a double word increment.  */
 
316
  ctx->total[0] += len;
 
317
  if (ctx->total[0] < len)
 
318
    ++ctx->total[1];
 
319
 
 
320
#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
 
321
 
 
322
#define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
 
323
                    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
 
324
               , (x[I&0x0f] = rol(tm, 1)) )
 
325
 
 
326
#define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
 
327
                                      + F( B, C, D )  \
 
328
                                      + K             \
 
329
                                      + M;            \
 
330
                                 B = rol( B, 30 );    \
 
331
                               } while(0)
 
332
 
 
333
  while (words < endp)
 
334
    {
 
335
      uint32_t tm;
 
336
      int t;
 
337
      for (t = 0; t < 16; t++)
 
338
        {
 
339
          x[t] = SWAP (*words);
 
340
          words++;
 
341
        }
 
342
 
 
343
      R( a, b, c, d, e, F1, K1, x[ 0] );
 
344
      R( e, a, b, c, d, F1, K1, x[ 1] );
 
345
      R( d, e, a, b, c, F1, K1, x[ 2] );
 
346
      R( c, d, e, a, b, F1, K1, x[ 3] );
 
347
      R( b, c, d, e, a, F1, K1, x[ 4] );
 
348
      R( a, b, c, d, e, F1, K1, x[ 5] );
 
349
      R( e, a, b, c, d, F1, K1, x[ 6] );
 
350
      R( d, e, a, b, c, F1, K1, x[ 7] );
 
351
      R( c, d, e, a, b, F1, K1, x[ 8] );
 
352
      R( b, c, d, e, a, F1, K1, x[ 9] );
 
353
      R( a, b, c, d, e, F1, K1, x[10] );
 
354
      R( e, a, b, c, d, F1, K1, x[11] );
 
355
      R( d, e, a, b, c, F1, K1, x[12] );
 
356
      R( c, d, e, a, b, F1, K1, x[13] );
 
357
      R( b, c, d, e, a, F1, K1, x[14] );
 
358
      R( a, b, c, d, e, F1, K1, x[15] );
 
359
      R( e, a, b, c, d, F1, K1, M(16) );
 
360
      R( d, e, a, b, c, F1, K1, M(17) );
 
361
      R( c, d, e, a, b, F1, K1, M(18) );
 
362
      R( b, c, d, e, a, F1, K1, M(19) );
 
363
      R( a, b, c, d, e, F2, K2, M(20) );
 
364
      R( e, a, b, c, d, F2, K2, M(21) );
 
365
      R( d, e, a, b, c, F2, K2, M(22) );
 
366
      R( c, d, e, a, b, F2, K2, M(23) );
 
367
      R( b, c, d, e, a, F2, K2, M(24) );
 
368
      R( a, b, c, d, e, F2, K2, M(25) );
 
369
      R( e, a, b, c, d, F2, K2, M(26) );
 
370
      R( d, e, a, b, c, F2, K2, M(27) );
 
371
      R( c, d, e, a, b, F2, K2, M(28) );
 
372
      R( b, c, d, e, a, F2, K2, M(29) );
 
373
      R( a, b, c, d, e, F2, K2, M(30) );
 
374
      R( e, a, b, c, d, F2, K2, M(31) );
 
375
      R( d, e, a, b, c, F2, K2, M(32) );
 
376
      R( c, d, e, a, b, F2, K2, M(33) );
 
377
      R( b, c, d, e, a, F2, K2, M(34) );
 
378
      R( a, b, c, d, e, F2, K2, M(35) );
 
379
      R( e, a, b, c, d, F2, K2, M(36) );
 
380
      R( d, e, a, b, c, F2, K2, M(37) );
 
381
      R( c, d, e, a, b, F2, K2, M(38) );
 
382
      R( b, c, d, e, a, F2, K2, M(39) );
 
383
      R( a, b, c, d, e, F3, K3, M(40) );
 
384
      R( e, a, b, c, d, F3, K3, M(41) );
 
385
      R( d, e, a, b, c, F3, K3, M(42) );
 
386
      R( c, d, e, a, b, F3, K3, M(43) );
 
387
      R( b, c, d, e, a, F3, K3, M(44) );
 
388
      R( a, b, c, d, e, F3, K3, M(45) );
 
389
      R( e, a, b, c, d, F3, K3, M(46) );
 
390
      R( d, e, a, b, c, F3, K3, M(47) );
 
391
      R( c, d, e, a, b, F3, K3, M(48) );
 
392
      R( b, c, d, e, a, F3, K3, M(49) );
 
393
      R( a, b, c, d, e, F3, K3, M(50) );
 
394
      R( e, a, b, c, d, F3, K3, M(51) );
 
395
      R( d, e, a, b, c, F3, K3, M(52) );
 
396
      R( c, d, e, a, b, F3, K3, M(53) );
 
397
      R( b, c, d, e, a, F3, K3, M(54) );
 
398
      R( a, b, c, d, e, F3, K3, M(55) );
 
399
      R( e, a, b, c, d, F3, K3, M(56) );
 
400
      R( d, e, a, b, c, F3, K3, M(57) );
 
401
      R( c, d, e, a, b, F3, K3, M(58) );
 
402
      R( b, c, d, e, a, F3, K3, M(59) );
 
403
      R( a, b, c, d, e, F4, K4, M(60) );
 
404
      R( e, a, b, c, d, F4, K4, M(61) );
 
405
      R( d, e, a, b, c, F4, K4, M(62) );
 
406
      R( c, d, e, a, b, F4, K4, M(63) );
 
407
      R( b, c, d, e, a, F4, K4, M(64) );
 
408
      R( a, b, c, d, e, F4, K4, M(65) );
 
409
      R( e, a, b, c, d, F4, K4, M(66) );
 
410
      R( d, e, a, b, c, F4, K4, M(67) );
 
411
      R( c, d, e, a, b, F4, K4, M(68) );
 
412
      R( b, c, d, e, a, F4, K4, M(69) );
 
413
      R( a, b, c, d, e, F4, K4, M(70) );
 
414
      R( e, a, b, c, d, F4, K4, M(71) );
 
415
      R( d, e, a, b, c, F4, K4, M(72) );
 
416
      R( c, d, e, a, b, F4, K4, M(73) );
 
417
      R( b, c, d, e, a, F4, K4, M(74) );
 
418
      R( a, b, c, d, e, F4, K4, M(75) );
 
419
      R( e, a, b, c, d, F4, K4, M(76) );
 
420
      R( d, e, a, b, c, F4, K4, M(77) );
 
421
      R( c, d, e, a, b, F4, K4, M(78) );
 
422
      R( b, c, d, e, a, F4, K4, M(79) );
 
423
 
 
424
      a = ctx->A += a;
 
425
      b = ctx->B += b;
 
426
      c = ctx->C += c;
 
427
      d = ctx->D += d;
 
428
      e = ctx->E += e;
 
429
    }
 
430
}