Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmliblzma/liblzma/check/sha256.c
Line
Count
Source
1
// SPDX-License-Identifier: 0BSD
2
3
///////////////////////////////////////////////////////////////////////////////
4
//
5
/// \file       sha256.c
6
/// \brief      SHA-256
7
//
8
//  The C code is based on the public domain SHA-256 code found from
9
//  Crypto++ Library 5.5.1 released in 2007: https://www.cryptopp.com/
10
//  A few minor tweaks have been made in liblzma.
11
//
12
//  Authors:    Wei Dai
13
//              Lasse Collin
14
//
15
///////////////////////////////////////////////////////////////////////////////
16
17
#include "check.h"
18
19
// Rotate a uint32_t. GCC can optimize this to a rotate instruction
20
// at least on x86.
21
static inline uint32_t
22
rotr_32(uint32_t num, unsigned amount)
23
186k
{
24
186k
  return (num >> amount) | (num << (32 - amount));
25
186k
}
26
27
#define blk0(i) (W[i] = conv32be(data[i]))
28
#define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \
29
    + s0(W[(i - 15) & 15]))
30
31
20.7k
#define Ch(x, y, z) (z ^ (x & (y ^ z)))
32
20.7k
#define Maj(x, y, z) ((x & (y ^ z)) + (y & z))
33
34
324
#define a(i) T[(0 - i) & 7]
35
324
#define b(i) T[(1 - i) & 7]
36
324
#define c(i) T[(2 - i) & 7]
37
21.0k
#define d(i) T[(3 - i) & 7]
38
324
#define e(i) T[(4 - i) & 7]
39
324
#define f(i) T[(5 - i) & 7]
40
324
#define g(i) T[(6 - i) & 7]
41
62.5k
#define h(i) T[(7 - i) & 7]
42
43
#define R(i, j, blk) \
44
20.7k
  h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + SHA256_K[i + j] + blk; \
45
20.7k
  d(i) += h(i); \
46
20.7k
  h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
47
5.18k
#define R0(i) R(i, 0, blk0(i))
48
15.5k
#define R2(i) R(i, j, blk2(i))
49
50
20.7k
#define S0(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 9), 11), 2)
51
20.7k
#define S1(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 14), 5), 6)
52
#define s0(x) (rotr_32(x ^ rotr_32(x, 11), 7) ^ (x >> 3))
53
#define s1(x) (rotr_32(x ^ rotr_32(x, 2), 17) ^ (x >> 10))
54
55
56
static const uint32_t SHA256_K[64] = {
57
  0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
58
  0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
59
  0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
60
  0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
61
  0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
62
  0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
63
  0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
64
  0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
65
  0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
66
  0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
67
  0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
68
  0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
69
  0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
70
  0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
71
  0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
72
  0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
73
};
74
75
76
static void
77
transform(uint32_t state[8], const uint32_t data[16])
78
324
{
79
324
  uint32_t W[16];
80
324
  uint32_t T[8];
81
82
  // Copy state[] to working vars.
83
324
  memcpy(T, state, sizeof(T));
84
85
  // The first 16 operations unrolled
86
324
  R0( 0); R0( 1); R0( 2); R0( 3);
87
324
  R0( 4); R0( 5); R0( 6); R0( 7);
88
324
  R0( 8); R0( 9); R0(10); R0(11);
89
324
  R0(12); R0(13); R0(14); R0(15);
90
91
  // The remaining 48 operations partially unrolled
92
1.29k
  for (unsigned int j = 16; j < 64; j += 16) {
93
972
    R2( 0); R2( 1); R2( 2); R2( 3);
94
972
    R2( 4); R2( 5); R2( 6); R2( 7);
95
972
    R2( 8); R2( 9); R2(10); R2(11);
96
972
    R2(12); R2(13); R2(14); R2(15);
97
972
  }
98
99
  // Add the working vars back into state[].
100
324
  state[0] += a(0);
101
324
  state[1] += b(0);
102
324
  state[2] += c(0);
103
324
  state[3] += d(0);
104
324
  state[4] += e(0);
105
324
  state[5] += f(0);
106
324
  state[6] += g(0);
107
324
  state[7] += h(0);
108
324
}
109
110
111
static void
112
process(lzma_check_state *check)
113
324
{
114
324
  transform(check->state.sha256.state, check->buffer.u32);
115
324
  return;
116
324
}
117
118
119
extern void
120
lzma_sha256_init(lzma_check_state *check)
121
3.84k
{
122
3.84k
  static const uint32_t s[8] = {
123
3.84k
    0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
124
3.84k
    0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
125
3.84k
  };
126
127
3.84k
  memcpy(check->state.sha256.state, s, sizeof(s));
128
3.84k
  check->state.sha256.size = 0;
129
130
3.84k
  return;
131
3.84k
}
132
133
134
extern void
135
lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
136
454
{
137
  // Copy the input data into a properly aligned temporary buffer.
138
  // This way we can be called with arbitrarily sized buffers
139
  // (no need to be multiple of 64 bytes), and the code works also
140
  // on architectures that don't allow unaligned memory access.
141
1.12k
  while (size > 0) {
142
668
    const size_t copy_start = check->state.sha256.size & 0x3F;
143
668
    size_t copy_size = 64 - copy_start;
144
668
    if (copy_size > size)
145
454
      copy_size = size;
146
147
668
    memcpy(check->buffer.u8 + copy_start, buf, copy_size);
148
149
668
    buf += copy_size;
150
668
    size -= copy_size;
151
668
    check->state.sha256.size += copy_size;
152
153
668
    if ((check->state.sha256.size & 0x3F) == 0)
154
214
      process(check);
155
668
  }
156
157
454
  return;
158
454
}
159
160
161
extern void
162
lzma_sha256_finish(lzma_check_state *check)
163
108
{
164
  // Add padding as described in RFC 3174 (it describes SHA-1 but
165
  // the same padding style is used for SHA-256 too).
166
108
  size_t pos = check->state.sha256.size & 0x3F;
167
108
  check->buffer.u8[pos++] = 0x80;
168
169
5.47k
  while (pos != 64 - 8) {
170
5.36k
    if (pos == 64) {
171
2
      process(check);
172
2
      pos = 0;
173
2
    }
174
175
5.36k
    check->buffer.u8[pos++] = 0x00;
176
5.36k
  }
177
178
  // Convert the message size from bytes to bits.
179
108
  check->state.sha256.size *= 8;
180
181
108
  check->buffer.u64[(64 - 8) / 8] = conv64be(check->state.sha256.size);
182
183
108
  process(check);
184
185
972
  for (size_t i = 0; i < 8; ++i)
186
864
    check->buffer.u32[i] = conv32be(check->state.sha256.state[i]);
187
188
108
  return;
189
108
}