Coverage Report

Created: 2025-12-31 08:15

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-2025 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 (const struct sha1_ctx *ctx, void *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 *ctx, void *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 (const char *buffer, size_t len, void *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 (const void *buffer, size_t len, struct sha1_ctx *ctx)
136
0
{
137
  /* When we already have some bits in our internal buffer concatenate
138
     both inputs first.  */
139
0
  if (ctx->buflen != 0)
140
0
    {
141
0
      size_t left_over = ctx->buflen;
142
0
      size_t add = 128 - left_over > len ? len : 128 - left_over;
143
144
0
      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
145
0
      ctx->buflen += add;
146
147
0
      if (ctx->buflen > 64)
148
0
        {
149
0
          sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
150
151
0
          ctx->buflen &= 63;
152
          /* The regions in the following copy operation cannot overlap,
153
             because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
154
0
          memcpy (ctx->buffer,
155
0
                  &((char *) ctx->buffer)[(left_over + add) & ~63],
156
0
                  ctx->buflen);
157
0
        }
158
159
0
      buffer = (const char *) buffer + add;
160
0
      len -= add;
161
0
    }
162
163
  /* Process available complete blocks.  */
164
0
  if (len >= 64)
165
0
    {
166
0
#if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
167
0
# define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
168
0
      if (UNALIGNED_P (buffer))
169
0
        while (len > 64)
170
0
          {
171
0
            sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
172
0
            buffer = (const char *) buffer + 64;
173
0
            len -= 64;
174
0
          }
175
0
      else
176
0
#endif
177
0
        {
178
0
          sha1_process_block (buffer, len & ~63, ctx);
179
0
          buffer = (const char *) buffer + (len & ~63);
180
0
          len &= 63;
181
0
        }
182
0
    }
183
184
  /* Move remaining bytes in internal buffer.  */
185
0
  if (len > 0)
186
0
    {
187
0
      size_t left_over = ctx->buflen;
188
189
0
      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
190
0
      left_over += len;
191
0
      if (left_over >= 64)
192
0
        {
193
0
          sha1_process_block (ctx->buffer, 64, ctx);
194
0
          left_over -= 64;
195
          /* The regions in the following copy operation cannot overlap,
196
             because left_over ≤ 64.  */
197
0
          memcpy (ctx->buffer, &ctx->buffer[16], left_over);
198
0
        }
199
0
      ctx->buflen = left_over;
200
0
    }
201
0
}
202
203
/* --- Code below is the primary difference between md5.c and sha1.c --- */
204
205
/* SHA1 round constants */
206
#define K1 0x5a827999
207
#define K2 0x6ed9eba1
208
#define K3 0x8f1bbcdc
209
#define K4 0xca62c1d6
210
211
/* Round functions.  Note that F2 is the same as F4.  */
212
0
#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
213
0
#define F2(B,C,D) (B ^ C ^ D)
214
0
#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
215
0
#define F4(B,C,D) (B ^ C ^ D)
216
217
/* Process LEN bytes of BUFFER, accumulating context into CTX.
218
   It is assumed that LEN % 64 == 0.
219
   Most of this code comes from GnuPG's cipher/sha1.c.  */
220
221
void
222
sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
223
0
{
224
0
  const uint32_t *words = buffer;
225
0
  size_t nwords = len / sizeof (uint32_t);
226
0
  const uint32_t *endp = words + nwords;
227
0
  uint32_t x[16];
228
0
  uint32_t a = ctx->A;
229
0
  uint32_t b = ctx->B;
230
0
  uint32_t c = ctx->C;
231
0
  uint32_t d = ctx->D;
232
0
  uint32_t e = ctx->E;
233
0
  uint32_t lolen = len;
234
235
  /* First increment the byte count.  RFC 1321 specifies the possible
236
     length of the file up to 2^64 bits.  Here we only compute the
237
     number of bytes.  Do a double word increment.  */
238
0
  ctx->total[0] += lolen;
239
0
  ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
240
241
0
#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
242
243
0
#define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
244
0
                    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
245
0
               , (x[I&0x0f] = rol(tm, 1)) )
246
247
0
#define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
248
0
                                      + F( B, C, D )  \
249
0
                                      + K             \
250
0
                                      + M;            \
251
0
                                 B = rol( B, 30 );    \
252
0
                               } while(0)
253
254
0
  while (words < endp)
255
0
    {
256
0
      for (int t = 0; t < 16; t++)
257
0
        {
258
0
          x[t] = SWAP (*words);
259
0
          words++;
260
0
        }
261
262
0
      uint32_t tm;
263
264
0
      R( a, b, c, d, e, F1, K1, x[ 0] );
265
0
      R( e, a, b, c, d, F1, K1, x[ 1] );
266
0
      R( d, e, a, b, c, F1, K1, x[ 2] );
267
0
      R( c, d, e, a, b, F1, K1, x[ 3] );
268
0
      R( b, c, d, e, a, F1, K1, x[ 4] );
269
0
      R( a, b, c, d, e, F1, K1, x[ 5] );
270
0
      R( e, a, b, c, d, F1, K1, x[ 6] );
271
0
      R( d, e, a, b, c, F1, K1, x[ 7] );
272
0
      R( c, d, e, a, b, F1, K1, x[ 8] );
273
0
      R( b, c, d, e, a, F1, K1, x[ 9] );
274
0
      R( a, b, c, d, e, F1, K1, x[10] );
275
0
      R( e, a, b, c, d, F1, K1, x[11] );
276
0
      R( d, e, a, b, c, F1, K1, x[12] );
277
0
      R( c, d, e, a, b, F1, K1, x[13] );
278
0
      R( b, c, d, e, a, F1, K1, x[14] );
279
0
      R( a, b, c, d, e, F1, K1, x[15] );
280
0
      R( e, a, b, c, d, F1, K1, M(16) );
281
0
      R( d, e, a, b, c, F1, K1, M(17) );
282
0
      R( c, d, e, a, b, F1, K1, M(18) );
283
0
      R( b, c, d, e, a, F1, K1, M(19) );
284
0
      R( a, b, c, d, e, F2, K2, M(20) );
285
0
      R( e, a, b, c, d, F2, K2, M(21) );
286
0
      R( d, e, a, b, c, F2, K2, M(22) );
287
0
      R( c, d, e, a, b, F2, K2, M(23) );
288
0
      R( b, c, d, e, a, F2, K2, M(24) );
289
0
      R( a, b, c, d, e, F2, K2, M(25) );
290
0
      R( e, a, b, c, d, F2, K2, M(26) );
291
0
      R( d, e, a, b, c, F2, K2, M(27) );
292
0
      R( c, d, e, a, b, F2, K2, M(28) );
293
0
      R( b, c, d, e, a, F2, K2, M(29) );
294
0
      R( a, b, c, d, e, F2, K2, M(30) );
295
0
      R( e, a, b, c, d, F2, K2, M(31) );
296
0
      R( d, e, a, b, c, F2, K2, M(32) );
297
0
      R( c, d, e, a, b, F2, K2, M(33) );
298
0
      R( b, c, d, e, a, F2, K2, M(34) );
299
0
      R( a, b, c, d, e, F2, K2, M(35) );
300
0
      R( e, a, b, c, d, F2, K2, M(36) );
301
0
      R( d, e, a, b, c, F2, K2, M(37) );
302
0
      R( c, d, e, a, b, F2, K2, M(38) );
303
0
      R( b, c, d, e, a, F2, K2, M(39) );
304
0
      R( a, b, c, d, e, F3, K3, M(40) );
305
0
      R( e, a, b, c, d, F3, K3, M(41) );
306
0
      R( d, e, a, b, c, F3, K3, M(42) );
307
0
      R( c, d, e, a, b, F3, K3, M(43) );
308
0
      R( b, c, d, e, a, F3, K3, M(44) );
309
0
      R( a, b, c, d, e, F3, K3, M(45) );
310
0
      R( e, a, b, c, d, F3, K3, M(46) );
311
0
      R( d, e, a, b, c, F3, K3, M(47) );
312
0
      R( c, d, e, a, b, F3, K3, M(48) );
313
0
      R( b, c, d, e, a, F3, K3, M(49) );
314
0
      R( a, b, c, d, e, F3, K3, M(50) );
315
0
      R( e, a, b, c, d, F3, K3, M(51) );
316
0
      R( d, e, a, b, c, F3, K3, M(52) );
317
0
      R( c, d, e, a, b, F3, K3, M(53) );
318
0
      R( b, c, d, e, a, F3, K3, M(54) );
319
0
      R( a, b, c, d, e, F3, K3, M(55) );
320
0
      R( e, a, b, c, d, F3, K3, M(56) );
321
0
      R( d, e, a, b, c, F3, K3, M(57) );
322
0
      R( c, d, e, a, b, F3, K3, M(58) );
323
0
      R( b, c, d, e, a, F3, K3, M(59) );
324
0
      R( a, b, c, d, e, F4, K4, M(60) );
325
0
      R( e, a, b, c, d, F4, K4, M(61) );
326
0
      R( d, e, a, b, c, F4, K4, M(62) );
327
0
      R( c, d, e, a, b, F4, K4, M(63) );
328
0
      R( b, c, d, e, a, F4, K4, M(64) );
329
0
      R( a, b, c, d, e, F4, K4, M(65) );
330
0
      R( e, a, b, c, d, F4, K4, M(66) );
331
0
      R( d, e, a, b, c, F4, K4, M(67) );
332
0
      R( c, d, e, a, b, F4, K4, M(68) );
333
0
      R( b, c, d, e, a, F4, K4, M(69) );
334
0
      R( a, b, c, d, e, F4, K4, M(70) );
335
0
      R( e, a, b, c, d, F4, K4, M(71) );
336
0
      R( d, e, a, b, c, F4, K4, M(72) );
337
0
      R( c, d, e, a, b, F4, K4, M(73) );
338
0
      R( b, c, d, e, a, F4, K4, M(74) );
339
0
      R( a, b, c, d, e, F4, K4, M(75) );
340
0
      R( e, a, b, c, d, F4, K4, M(76) );
341
0
      R( d, e, a, b, c, F4, K4, M(77) );
342
0
      R( c, d, e, a, b, F4, K4, M(78) );
343
0
      R( b, c, d, e, a, F4, K4, M(79) );
344
345
0
      a = ctx->A += a;
346
0
      b = ctx->B += b;
347
0
      c = ctx->C += c;
348
0
      d = ctx->D += d;
349
0
      e = ctx->E += e;
350
0
    }
351
0
}
352
353
#endif
354
355
/*
356
 * Hey Emacs!
357
 * Local Variables:
358
 * coding: utf-8
359
 * End:
360
 */