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