Coverage Report

Created: 2026-09-28 07:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wget/lib/sha1.c
Line
Count
Source
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-2001, 2003-2006, 2008-2026 Free Software Foundation, Inc.
5
6
   This file is free software: you can redistribute it and/or modify
7
   it under the terms of the GNU Lesser General Public License as
8
   published by the Free Software Foundation; either version 2.1 of the
9
   License, or (at your option) any later version.
10
11
   This file 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 Lesser General Public License for more details.
15
16
   You should have received a copy of the GNU Lesser General Public License
17
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
18
19
/* Written by Scott G. Miller
20
   Credits:
21
      Robert Klep <robert@ilse.nl>  -- Expansion function fix
22
*/
23
24
#include <config.h>
25
26
/* Specification.  */
27
#if HAVE_OPENSSL_SHA1
28
# define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
29
#endif
30
#include "sha1.h"
31
32
#include <stdint.h>
33
#include <string.h>
34
35
#include <byteswap.h>
36
#ifdef WORDS_BIGENDIAN
37
# define SWAP(n) (n)
38
#else
39
0
# define SWAP(n) bswap_32 (n)
40
#endif
41
42
#if ! HAVE_OPENSSL_SHA1
43
44
/* This array contains the bytes used to pad the buffer to the next
45
   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
46
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
47
48
49
/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
50
   initialize it to the start constants of the SHA1 algorithm.  This
51
   must be called before using hash in the call to sha1_hash.  */
52
void
53
sha1_init_ctx (struct sha1_ctx *ctx)
54
0
{
55
0
  ctx->A = 0x67452301;
56
0
  ctx->B = 0xefcdab89;
57
0
  ctx->C = 0x98badcfe;
58
0
  ctx->D = 0x10325476;
59
0
  ctx->E = 0xc3d2e1f0;
60
61
0
  ctx->total[0] = ctx->total[1] = 0;
62
0
  ctx->buflen = 0;
63
0
}
64
65
/* Copy the 4 byte value from v into the memory location pointed to by *cp,
66
   If your architecture allows unaligned access this is equivalent to
67
   * (uint32_t *) cp = v  */
68
static void
69
set_uint32 (char *cp, uint32_t v)
70
0
{
71
0
  memcpy (cp, &v, sizeof v);
72
0
}
73
74
/* Put result from CTX in first 20 bytes following RESBUF.  The result
75
   must be in little endian byte order.  */
76
void *
77
sha1_read_ctx (struct sha1_ctx const *restrict ctx, void *restrict resbuf)
78
0
{
79
0
  char *r = resbuf;
80
0
  set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
81
0
  set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
82
0
  set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
83
0
  set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
84
0
  set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
85
86
0
  return resbuf;
87
0
}
88
89
/* Process the remaining bytes in the internal buffer and the usual
90
   prolog according to the standard and write the result to RESBUF.  */
91
void *
92
sha1_finish_ctx (struct sha1_ctx *restrict ctx, void *restrict resbuf)
93
0
{
94
  /* Take yet unprocessed bytes into account.  */
95
0
  uint32_t 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 LEN bytes beginning at BUFFER.  The
116
   result is always in little endian byte order, so that a byte-wise
117
   output yields to the wanted ASCII representation of the message
118
   digest.  */
119
void *
120
sha1_buffer (char const *restrict buffer, size_t len, void *restrict resblock)
121
0
{
122
0
  struct sha1_ctx ctx;
123
124
  /* Initialize the computation context.  */
125
0
  sha1_init_ctx (&ctx);
126
127
  /* Process whole buffer but last len % 64 bytes.  */
128
0
  sha1_process_bytes (buffer, len, &ctx);
129
130
  /* Put result in desired memory area.  */
131
0
  return sha1_finish_ctx (&ctx, resblock);
132
0
}
133
134
void
135
sha1_process_bytes (void const *restrict buffer, size_t len,
136
                    struct sha1_ctx *restrict ctx)
137
0
{
138
  /* When we already have some bits in our internal buffer concatenate
139
     both inputs first.  */
140
0
  if (ctx->buflen != 0)
141
0
    {
142
0
      size_t left_over = ctx->buflen;
143
0
      size_t add = 128 - left_over > len ? len : 128 - left_over;
144
145
0
      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
146
0
      ctx->buflen += add;
147
148
0
      if (ctx->buflen > 64)
149
0
        {
150
0
          sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
151
152
0
          ctx->buflen &= 63;
153
          /* The regions in the following copy operation cannot overlap,
154
             because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
155
0
          memcpy (ctx->buffer,
156
0
                  &((char *) ctx->buffer)[(left_over + add) & ~63],
157
0
                  ctx->buflen);
158
0
        }
159
160
0
      buffer = (const char *) buffer + add;
161
0
      len -= add;
162
0
    }
163
164
  /* Process available complete blocks.  */
165
0
  if (len >= 64)
166
0
    {
167
0
#if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
168
0
# define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
169
0
      if (UNALIGNED_P (buffer))
170
0
        while (len > 64)
171
0
          {
172
0
            sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
173
0
            buffer = (const char *) buffer + 64;
174
0
            len -= 64;
175
0
          }
176
0
      else
177
0
#endif
178
0
        {
179
0
          sha1_process_block (buffer, len & ~63, ctx);
180
0
          buffer = (const char *) buffer + (len & ~63);
181
0
          len &= 63;
182
0
        }
183
0
    }
184
185
  /* Move remaining bytes in internal buffer.  */
186
0
  if (len > 0)
187
0
    {
188
0
      size_t left_over = ctx->buflen;
189
190
0
      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
191
0
      left_over += len;
192
0
      if (left_over >= 64)
193
0
        {
194
0
          sha1_process_block (ctx->buffer, 64, ctx);
195
0
          left_over -= 64;
196
          /* The regions in the following copy operation cannot overlap,
197
             because left_over ≤ 64.  */
198
0
          memcpy (ctx->buffer, &ctx->buffer[16], left_over);
199
0
        }
200
0
      ctx->buflen = left_over;
201
0
    }
202
0
}
203
204
/* --- Code below is the primary difference between md5.c and sha1.c --- */
205
206
/* SHA1 round constants */
207
#define K1 0x5a827999
208
#define K2 0x6ed9eba1
209
#define K3 0x8f1bbcdc
210
#define K4 0xca62c1d6
211
212
/* Round functions.  Note that F2 is the same as F4.  */
213
0
#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
214
0
#define F2(B,C,D) (B ^ C ^ D)
215
0
#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
216
0
#define F4(B,C,D) (B ^ C ^ D)
217
218
/* Process LEN bytes of BUFFER, accumulating context into CTX.
219
   It is assumed that LEN % 64 == 0.
220
   Most of this code comes from GnuPG's cipher/sha1.c.  */
221
222
void
223
sha1_process_block (void const *restrict buffer, size_t len,
224
                    struct sha1_ctx *restrict ctx)
225
0
{
226
0
  const uint32_t *words = buffer;
227
0
  size_t nwords = len / sizeof (uint32_t);
228
0
  const uint32_t *endp = words + nwords;
229
0
  uint32_t x[16];
230
0
  uint32_t a = ctx->A;
231
0
  uint32_t b = ctx->B;
232
0
  uint32_t c = ctx->C;
233
0
  uint32_t d = ctx->D;
234
0
  uint32_t e = ctx->E;
235
0
  uint32_t lolen = len;
236
237
  /* First increment the byte count.  RFC 1321 specifies the possible
238
     length of the file up to 2^64 bits.  Here we only compute the
239
     number of bytes.  Do a double word increment.  */
240
0
  ctx->total[0] += lolen;
241
0
  ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
242
243
0
#define rol(x, n) (((x) << (n)) | ((uint32_t) {(x)} >> (32 - (n))))
244
245
0
#define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
246
0
                    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
247
0
               , (x[I&0x0f] = rol(tm, 1)) )
248
249
0
#define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
250
0
                                      + F( B, C, D )  \
251
0
                                      + K             \
252
0
                                      + M;            \
253
0
                                 B = rol( B, 30 );    \
254
0
                               } while(0)
255
256
0
  while (words < endp)
257
0
    {
258
0
      for (int t = 0; t < 16; t++)
259
0
        {
260
0
          x[t] = SWAP (*words);
261
0
          words++;
262
0
        }
263
264
0
      uint32_t tm;
265
266
0
      R( a, b, c, d, e, F1, K1, x[ 0] );
267
0
      R( e, a, b, c, d, F1, K1, x[ 1] );
268
0
      R( d, e, a, b, c, F1, K1, x[ 2] );
269
0
      R( c, d, e, a, b, F1, K1, x[ 3] );
270
0
      R( b, c, d, e, a, F1, K1, x[ 4] );
271
0
      R( a, b, c, d, e, F1, K1, x[ 5] );
272
0
      R( e, a, b, c, d, F1, K1, x[ 6] );
273
0
      R( d, e, a, b, c, F1, K1, x[ 7] );
274
0
      R( c, d, e, a, b, F1, K1, x[ 8] );
275
0
      R( b, c, d, e, a, F1, K1, x[ 9] );
276
0
      R( a, b, c, d, e, F1, K1, x[10] );
277
0
      R( e, a, b, c, d, F1, K1, x[11] );
278
0
      R( d, e, a, b, c, F1, K1, x[12] );
279
0
      R( c, d, e, a, b, F1, K1, x[13] );
280
0
      R( b, c, d, e, a, F1, K1, x[14] );
281
0
      R( a, b, c, d, e, F1, K1, x[15] );
282
0
      R( e, a, b, c, d, F1, K1, M(16) );
283
0
      R( d, e, a, b, c, F1, K1, M(17) );
284
0
      R( c, d, e, a, b, F1, K1, M(18) );
285
0
      R( b, c, d, e, a, F1, K1, M(19) );
286
0
      R( a, b, c, d, e, F2, K2, M(20) );
287
0
      R( e, a, b, c, d, F2, K2, M(21) );
288
0
      R( d, e, a, b, c, F2, K2, M(22) );
289
0
      R( c, d, e, a, b, F2, K2, M(23) );
290
0
      R( b, c, d, e, a, F2, K2, M(24) );
291
0
      R( a, b, c, d, e, F2, K2, M(25) );
292
0
      R( e, a, b, c, d, F2, K2, M(26) );
293
0
      R( d, e, a, b, c, F2, K2, M(27) );
294
0
      R( c, d, e, a, b, F2, K2, M(28) );
295
0
      R( b, c, d, e, a, F2, K2, M(29) );
296
0
      R( a, b, c, d, e, F2, K2, M(30) );
297
0
      R( e, a, b, c, d, F2, K2, M(31) );
298
0
      R( d, e, a, b, c, F2, K2, M(32) );
299
0
      R( c, d, e, a, b, F2, K2, M(33) );
300
0
      R( b, c, d, e, a, F2, K2, M(34) );
301
0
      R( a, b, c, d, e, F2, K2, M(35) );
302
0
      R( e, a, b, c, d, F2, K2, M(36) );
303
0
      R( d, e, a, b, c, F2, K2, M(37) );
304
0
      R( c, d, e, a, b, F2, K2, M(38) );
305
0
      R( b, c, d, e, a, F2, K2, M(39) );
306
0
      R( a, b, c, d, e, F3, K3, M(40) );
307
0
      R( e, a, b, c, d, F3, K3, M(41) );
308
0
      R( d, e, a, b, c, F3, K3, M(42) );
309
0
      R( c, d, e, a, b, F3, K3, M(43) );
310
0
      R( b, c, d, e, a, F3, K3, M(44) );
311
0
      R( a, b, c, d, e, F3, K3, M(45) );
312
0
      R( e, a, b, c, d, F3, K3, M(46) );
313
0
      R( d, e, a, b, c, F3, K3, M(47) );
314
0
      R( c, d, e, a, b, F3, K3, M(48) );
315
0
      R( b, c, d, e, a, F3, K3, M(49) );
316
0
      R( a, b, c, d, e, F3, K3, M(50) );
317
0
      R( e, a, b, c, d, F3, K3, M(51) );
318
0
      R( d, e, a, b, c, F3, K3, M(52) );
319
0
      R( c, d, e, a, b, F3, K3, M(53) );
320
0
      R( b, c, d, e, a, F3, K3, M(54) );
321
0
      R( a, b, c, d, e, F3, K3, M(55) );
322
0
      R( e, a, b, c, d, F3, K3, M(56) );
323
0
      R( d, e, a, b, c, F3, K3, M(57) );
324
0
      R( c, d, e, a, b, F3, K3, M(58) );
325
0
      R( b, c, d, e, a, F3, K3, M(59) );
326
0
      R( a, b, c, d, e, F4, K4, M(60) );
327
0
      R( e, a, b, c, d, F4, K4, M(61) );
328
0
      R( d, e, a, b, c, F4, K4, M(62) );
329
0
      R( c, d, e, a, b, F4, K4, M(63) );
330
0
      R( b, c, d, e, a, F4, K4, M(64) );
331
0
      R( a, b, c, d, e, F4, K4, M(65) );
332
0
      R( e, a, b, c, d, F4, K4, M(66) );
333
0
      R( d, e, a, b, c, F4, K4, M(67) );
334
0
      R( c, d, e, a, b, F4, K4, M(68) );
335
0
      R( b, c, d, e, a, F4, K4, M(69) );
336
0
      R( a, b, c, d, e, F4, K4, M(70) );
337
0
      R( e, a, b, c, d, F4, K4, M(71) );
338
0
      R( d, e, a, b, c, F4, K4, M(72) );
339
0
      R( c, d, e, a, b, F4, K4, M(73) );
340
0
      R( b, c, d, e, a, F4, K4, M(74) );
341
0
      R( a, b, c, d, e, F4, K4, M(75) );
342
0
      R( e, a, b, c, d, F4, K4, M(76) );
343
0
      R( d, e, a, b, c, F4, K4, M(77) );
344
0
      R( c, d, e, a, b, F4, K4, M(78) );
345
0
      R( b, c, d, e, a, F4, K4, M(79) );
346
347
0
      a = ctx->A += a;
348
0
      b = ctx->B += b;
349
0
      c = ctx->C += c;
350
0
      d = ctx->D += d;
351
0
      e = ctx->E += e;
352
0
    }
353
0
}
354
355
#endif
356
357
/*
358
 * Hey Emacs!
359
 * Local Variables:
360
 * coding: utf-8
361
 * End:
362
 */