~ubuntu-branches/debian/experimental/lftp/experimental

« back to all changes in this revision

Viewing changes to lib/sha1.c

  • Committer: Package Import Robot
  • Author(s): Noël Köthe
  • Date: 2015-08-21 16:06:22 UTC
  • mfrom: (1.1.20) (24.1.38 sid)
  • Revision ID: package-import@ubuntu.com-20150821160622-lckdmbiqx16wefgy
Tags: 4.6.4-1
new upstream release 2015-08-21

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* sha1.c - Functions to compute SHA1 message digest of files or
2
2
   memory blocks according to the NIST specification FIPS-180-1.
3
3
 
4
 
   Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2008 Free Software
5
 
   Foundation, Inc.
 
4
   Copyright (C) 2000-2001, 2003-2006, 2008-2015 Free Software Foundation, Inc.
6
5
 
7
6
   This program is free software; you can redistribute it and/or modify it
8
7
   under the terms of the GNU General Public License as published by the
15
14
   GNU General Public License for more details.
16
15
 
17
16
   You should have received a copy of the GNU General Public License
18
 
   along with this program; if not, write to the Free Software Foundation,
19
 
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
17
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
20
18
 
21
19
/* Written by Scott G. Miller
22
20
   Credits:
25
23
 
26
24
#include <config.h>
27
25
 
 
26
#if HAVE_OPENSSL_SHA1
 
27
# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
 
28
#endif
28
29
#include "sha1.h"
29
30
 
30
 
#include <stddef.h>
 
31
#include <stdalign.h>
 
32
#include <stdint.h>
 
33
#include <stdlib.h>
31
34
#include <string.h>
32
35
 
33
36
#if USE_UNLOCKED_IO
41
44
    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
42
45
#endif
43
46
 
44
 
#define BLOCKSIZE 4096
 
47
#define BLOCKSIZE 32768
45
48
#if BLOCKSIZE % 64 != 0
46
49
# error "invalid BLOCKSIZE"
47
50
#endif
48
51
 
 
52
#if ! HAVE_OPENSSL_SHA1
49
53
/* This array contains the bytes used to pad the buffer to the next
50
54
   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
51
55
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
70
74
/* Copy the 4 byte value from v into the memory location pointed to by *cp,
71
75
   If your architecture allows unaligned access this is equivalent to
72
76
   * (uint32_t *) cp = v  */
73
 
static inline void
 
77
static void
74
78
set_uint32 (char *cp, uint32_t v)
75
79
{
76
80
  memcpy (cp, &v, sizeof v);
116
120
 
117
121
  return sha1_read_ctx (ctx, resbuf);
118
122
}
 
123
#endif
119
124
 
120
125
/* Compute SHA1 message digest for bytes read from STREAM.  The
121
126
   resulting message digest number will be written into the 16 bytes
124
129
sha1_stream (FILE *stream, void *resblock)
125
130
{
126
131
  struct sha1_ctx ctx;
127
 
  char buffer[BLOCKSIZE + 72];
128
132
  size_t sum;
129
133
 
 
134
  char *buffer = malloc (BLOCKSIZE + 72);
 
135
  if (!buffer)
 
136
    return 1;
 
137
 
130
138
  /* Initialize the computation context.  */
131
139
  sha1_init_ctx (&ctx);
132
140
 
134
142
  while (1)
135
143
    {
136
144
      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
137
 
         computation function processes the whole buffer so that with the
138
 
         next round of the loop another block can be read.  */
 
145
         computation function processes the whole buffer so that with the
 
146
         next round of the loop another block can be read.  */
139
147
      size_t n;
140
148
      sum = 0;
141
149
 
142
150
      /* Read block.  Take care for partial reads.  */
143
151
      while (1)
144
 
        {
145
 
          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
146
 
 
147
 
          sum += n;
148
 
 
149
 
          if (sum == BLOCKSIZE)
150
 
            break;
151
 
 
152
 
          if (n == 0)
153
 
            {
154
 
              /* Check for the error flag IFF N == 0, so that we don't
155
 
                 exit the loop after a partial read due to e.g., EAGAIN
156
 
                 or EWOULDBLOCK.  */
157
 
              if (ferror (stream))
158
 
                return 1;
159
 
              goto process_partial_block;
160
 
            }
161
 
 
162
 
          /* We've read at least one byte, so ignore errors.  But always
163
 
             check for EOF, since feof may be true even though N > 0.
164
 
             Otherwise, we could end up calling fread after EOF.  */
165
 
          if (feof (stream))
166
 
            goto process_partial_block;
167
 
        }
 
152
        {
 
153
          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
154
 
 
155
          sum += n;
 
156
 
 
157
          if (sum == BLOCKSIZE)
 
158
            break;
 
159
 
 
160
          if (n == 0)
 
161
            {
 
162
              /* Check for the error flag IFF N == 0, so that we don't
 
163
                 exit the loop after a partial read due to e.g., EAGAIN
 
164
                 or EWOULDBLOCK.  */
 
165
              if (ferror (stream))
 
166
                {
 
167
                  free (buffer);
 
168
                  return 1;
 
169
                }
 
170
              goto process_partial_block;
 
171
            }
 
172
 
 
173
          /* We've read at least one byte, so ignore errors.  But always
 
174
             check for EOF, since feof may be true even though N > 0.
 
175
             Otherwise, we could end up calling fread after EOF.  */
 
176
          if (feof (stream))
 
177
            goto process_partial_block;
 
178
        }
168
179
 
169
180
      /* Process buffer with BLOCKSIZE bytes.  Note that
170
 
                        BLOCKSIZE % 64 == 0
 
181
                        BLOCKSIZE % 64 == 0
171
182
       */
172
183
      sha1_process_block (buffer, BLOCKSIZE, &ctx);
173
184
    }
180
191
 
181
192
  /* Construct result in desired memory.  */
182
193
  sha1_finish_ctx (&ctx, resblock);
 
194
  free (buffer);
183
195
  return 0;
184
196
}
185
197
 
 
198
#if ! HAVE_OPENSSL_SHA1
186
199
/* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
187
200
   result is always in little endian byte order, so that a byte-wise
188
201
   output yields to the wanted ASCII representation of the message
216
229
      ctx->buflen += add;
217
230
 
218
231
      if (ctx->buflen > 64)
219
 
        {
220
 
          sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 
232
        {
 
233
          sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
221
234
 
222
 
          ctx->buflen &= 63;
223
 
          /* The regions in the following copy operation cannot overlap.  */
224
 
          memcpy (ctx->buffer,
225
 
                  &((char *) ctx->buffer)[(left_over + add) & ~63],
226
 
                  ctx->buflen);
227
 
        }
 
235
          ctx->buflen &= 63;
 
236
          /* The regions in the following copy operation cannot overlap.  */
 
237
          memcpy (ctx->buffer,
 
238
                  &((char *) ctx->buffer)[(left_over + add) & ~63],
 
239
                  ctx->buflen);
 
240
        }
228
241
 
229
242
      buffer = (const char *) buffer + add;
230
243
      len -= add;
234
247
  if (len >= 64)
235
248
    {
236
249
#if !_STRING_ARCH_unaligned
237
 
# define alignof(type) offsetof (struct { char c; type x; }, x)
238
 
# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
 
250
# define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
239
251
      if (UNALIGNED_P (buffer))
240
 
        while (len > 64)
241
 
          {
242
 
            sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
243
 
            buffer = (const char *) buffer + 64;
244
 
            len -= 64;
245
 
          }
 
252
        while (len > 64)
 
253
          {
 
254
            sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
 
255
            buffer = (const char *) buffer + 64;
 
256
            len -= 64;
 
257
          }
246
258
      else
247
259
#endif
248
 
        {
249
 
          sha1_process_block (buffer, len & ~63, ctx);
250
 
          buffer = (const char *) buffer + (len & ~63);
251
 
          len &= 63;
252
 
        }
 
260
        {
 
261
          sha1_process_block (buffer, len & ~63, ctx);
 
262
          buffer = (const char *) buffer + (len & ~63);
 
263
          len &= 63;
 
264
        }
253
265
    }
254
266
 
255
267
  /* Move remaining bytes in internal buffer.  */
260
272
      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
261
273
      left_over += len;
262
274
      if (left_over >= 64)
263
 
        {
264
 
          sha1_process_block (ctx->buffer, 64, ctx);
265
 
          left_over -= 64;
266
 
          memcpy (ctx->buffer, &ctx->buffer[16], left_over);
267
 
        }
 
275
        {
 
276
          sha1_process_block (ctx->buffer, 64, ctx);
 
277
          left_over -= 64;
 
278
          memcpy (ctx->buffer, &ctx->buffer[16], left_over);
 
279
        }
268
280
      ctx->buflen = left_over;
269
281
    }
270
282
}
299
311
  uint32_t c = ctx->C;
300
312
  uint32_t d = ctx->D;
301
313
  uint32_t e = ctx->E;
 
314
  uint32_t lolen = len;
302
315
 
303
316
  /* First increment the byte count.  RFC 1321 specifies the possible
304
317
     length of the file up to 2^64 bits.  Here we only compute the
305
318
     number of bytes.  Do a double word increment.  */
306
 
  ctx->total[0] += len;
307
 
  if (ctx->total[0] < len)
308
 
    ++ctx->total[1];
 
319
  ctx->total[0] += lolen;
 
320
  ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
309
321
 
310
322
#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
311
323
 
312
324
#define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
313
 
                    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
314
 
               , (x[I&0x0f] = rol(tm, 1)) )
 
325
                    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
 
326
               , (x[I&0x0f] = rol(tm, 1)) )
315
327
 
316
328
#define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
317
 
                                      + F( B, C, D )  \
318
 
                                      + K             \
319
 
                                      + M;            \
320
 
                                 B = rol( B, 30 );    \
321
 
                               } while(0)
 
329
                                      + F( B, C, D )  \
 
330
                                      + K             \
 
331
                                      + M;            \
 
332
                                 B = rol( B, 30 );    \
 
333
                               } while(0)
322
334
 
323
335
  while (words < endp)
324
336
    {
325
337
      uint32_t tm;
326
338
      int t;
327
339
      for (t = 0; t < 16; t++)
328
 
        {
329
 
          x[t] = SWAP (*words);
330
 
          words++;
331
 
        }
 
340
        {
 
341
          x[t] = SWAP (*words);
 
342
          words++;
 
343
        }
332
344
 
333
345
      R( a, b, c, d, e, F1, K1, x[ 0] );
334
346
      R( e, a, b, c, d, F1, K1, x[ 1] );
418
430
      e = ctx->E += e;
419
431
    }
420
432
}
 
433
#endif