Coverage Report

Created: 2023-08-28 06:26

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