Coverage Report

Created: 2026-02-16 07:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibCrypto/Hash/SHA2.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
3
 * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <AK/CPUFeatures.h>
9
#include <AK/Platform.h>
10
#include <AK/SIMD.h>
11
#include <AK/SIMDExtras.h>
12
#include <AK/Types.h>
13
#include <LibCrypto/Hash/SHA2.h>
14
15
namespace Crypto::Hash {
16
0
constexpr static auto ROTRIGHT(u32 a, size_t b) { return (a >> b) | (a << (32 - b)); }
17
0
constexpr static auto CH(u32 x, u32 y, u32 z) { return (x & y) ^ (z & ~x); }
18
0
constexpr static auto MAJ(u32 x, u32 y, u32 z) { return (x & y) ^ (x & z) ^ (y & z); }
19
0
constexpr static auto EP0(u32 x) { return ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22); }
20
0
constexpr static auto EP1(u32 x) { return ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25); }
21
0
constexpr static auto SIGN0(u32 x) { return ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ (x >> 3); }
22
0
constexpr static auto SIGN1(u32 x) { return ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ (x >> 10); }
23
24
75.0M
constexpr static auto ROTRIGHT(u64 a, size_t b) { return (a >> b) | (a << (64 - b)); }
25
8.15M
constexpr static auto CH(u64 x, u64 y, u64 z) { return (x & y) ^ (z & ~x); }
26
8.15M
constexpr static auto MAJ(u64 x, u64 y, u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
27
8.15M
constexpr static auto EP0(u64 x) { return ROTRIGHT(x, 28) ^ ROTRIGHT(x, 34) ^ ROTRIGHT(x, 39); }
28
8.15M
constexpr static auto EP1(u64 x) { return ROTRIGHT(x, 14) ^ ROTRIGHT(x, 18) ^ ROTRIGHT(x, 41); }
29
6.52M
constexpr static auto SIGN0(u64 x) { return ROTRIGHT(x, 1) ^ ROTRIGHT(x, 8) ^ (x >> 7); }
30
6.52M
constexpr static auto SIGN1(u64 x) { return ROTRIGHT(x, 19) ^ ROTRIGHT(x, 61) ^ (x >> 6); }
31
32
template<>
33
void SHA256::transform_impl<CPUFeatures::None>()
34
0
{
35
0
    auto& data = m_data_buffer;
36
37
0
    u32 m[16];
38
39
0
    size_t i = 0;
40
0
    for (size_t j = 0; i < 16; ++i, j += 4) {
41
0
        m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | data[j + 3];
42
0
    }
43
44
0
    auto a = m_state[0], b = m_state[1],
45
0
         c = m_state[2], d = m_state[3],
46
0
         e = m_state[4], f = m_state[5],
47
0
         g = m_state[6], h = m_state[7];
48
49
0
    for (i = 0; i < Rounds; ++i) {
50
0
        if (i >= 16)
51
0
            m[i % 16] = SIGN1(m[(i - 2) % 16]) + m[(i - 7) % 16] + SIGN0(m[(i - 15) % 16]) + m[(i - 16) % 16];
52
53
0
        auto temp0 = h + EP1(e) + CH(e, f, g) + SHA256Constants::RoundConstants[i] + m[i % 16];
54
0
        auto temp1 = EP0(a) + MAJ(a, b, c);
55
0
        h = g;
56
0
        g = f;
57
0
        f = e;
58
0
        e = d + temp0;
59
0
        d = c;
60
0
        c = b;
61
0
        b = a;
62
0
        a = temp0 + temp1;
63
0
    }
64
65
0
    m_state[0] += a;
66
0
    m_state[1] += b;
67
0
    m_state[2] += c;
68
0
    m_state[3] += d;
69
0
    m_state[4] += e;
70
0
    m_state[5] += f;
71
0
    m_state[6] += g;
72
0
    m_state[7] += h;
73
0
}
74
75
// Note: The SHA extension was introduced with
76
//       Intel Goldmont (SSE4.2), Ice Lake (AVX512), Rocket Lake (AVX512), and AMD Zen (AVX2)
77
//       So it's safe to assume that if we have SHA we have at least SSE4.2
78
//      ~https://en.wikipedia.org/wiki/Intel_SHA_extensions
79
#if AK_CAN_CODEGEN_FOR_X86_SHA && AK_CAN_CODEGEN_FOR_X86_SSE42
80
template<>
81
[[gnu::target("sha,sse4.2")]] void SHA256::transform_impl<CPUFeatures::X86_SHA | CPUFeatures::X86_SSE42>()
82
99.7k
{
83
99.7k
    using AK::SIMD::i32x4, AK::SIMD::u32x4;
84
85
99.7k
    auto& state = m_state;
86
99.7k
    auto& data = m_data_buffer;
87
88
99.7k
    u32x4 states[2] {};
89
99.7k
    states[0] = AK::SIMD::load_unaligned<u32x4>(&state[0]);
90
99.7k
    states[1] = AK::SIMD::load_unaligned<u32x4>(&state[4]);
91
99.7k
    auto tmp = u32x4 { states[0][1], states[0][0], states[0][3], states[0][2] };
92
99.7k
    states[1] = u32x4 { states[1][3], states[1][2], states[1][1], states[1][0] };
93
99.7k
    states[0] = u32x4 { states[1][2], states[1][3], tmp[0], tmp[1] };
94
99.7k
    states[1] = u32x4 { states[1][0], states[1][1], tmp[2], tmp[3] };
95
96
99.7k
    u32x4 msgs[4] {};
97
99.7k
    u32x4 old[2] { states[0], states[1] };
98
1.69M
    for (int i = 0; i != 16; ++i) {
99
1.59M
        u32x4 msg {};
100
1.59M
        if (i < 4) {
101
399k
            msgs[i] = AK::SIMD::load_unaligned<u32x4>(&data[i * 16]);
102
399k
            msgs[i] = AK::SIMD::elementwise_byte_reverse(msgs[i]);
103
399k
            tmp = AK::SIMD::load_unaligned<u32x4>(&SHA256Constants::RoundConstants[i * 4]);
104
399k
            msg = msgs[i] + tmp;
105
1.19M
        } else {
106
1.19M
            msgs[(i + 0) % 4] = bit_cast<u32x4>(__builtin_ia32_sha256msg1(bit_cast<i32x4>(msgs[(i + 0) % 4]), bit_cast<i32x4>(msgs[(i + 1) % 4])));
107
1.19M
            tmp = u32x4 { msgs[(i + 2) % 4][1], msgs[(i + 2) % 4][2], msgs[(i + 2) % 4][3], msgs[(i + 3) % 4][0] };
108
1.19M
            msgs[(i + 0) % 4] += tmp;
109
1.19M
            msgs[(i + 0) % 4] = bit_cast<u32x4>(__builtin_ia32_sha256msg2(bit_cast<i32x4>(msgs[(i + 0) % 4]), bit_cast<i32x4>(msgs[(i + 3) % 4])));
110
1.19M
            tmp = AK::SIMD::load_unaligned<u32x4>(&SHA256Constants::RoundConstants[i * 4]);
111
1.19M
            msg = msgs[(i + 0) % 4] + tmp;
112
1.19M
        }
113
1.59M
        states[1] = bit_cast<u32x4>(__builtin_ia32_sha256rnds2(bit_cast<i32x4>(states[1]), bit_cast<i32x4>(states[0]), bit_cast<i32x4>(msg)));
114
1.59M
        msg = u32x4 { msg[2], msg[3], 0, 0 };
115
1.59M
        states[0] = bit_cast<u32x4>(__builtin_ia32_sha256rnds2(bit_cast<i32x4>(states[0]), bit_cast<i32x4>(states[1]), bit_cast<i32x4>(msg)));
116
1.59M
    }
117
99.7k
    states[0] += old[0];
118
99.7k
    states[1] += old[1];
119
120
99.7k
    tmp = u32x4 { states[0][3], states[0][2], states[0][1], states[0][0] };
121
99.7k
    states[1] = u32x4 { states[1][1], states[1][0], states[1][3], states[1][2] };
122
99.7k
    states[0] = u32x4 { tmp[0], tmp[1], states[1][2], states[1][3] };
123
99.7k
    states[1] = u32x4 { tmp[2], tmp[3], states[1][0], states[1][1] };
124
99.7k
    AK::SIMD::store_unaligned(&state[0], states[0]);
125
99.7k
    AK::SIMD::store_unaligned(&state[4], states[1]);
126
99.7k
}
127
#endif
128
129
8
decltype(SHA256::transform_dispatched) SHA256::transform_dispatched = [] {
130
8
    CPUFeatures features = detect_cpu_features();
131
132
8
    if constexpr (is_valid_feature(CPUFeatures::X86_SHA | CPUFeatures::X86_SSE42)) {
133
8
        if (has_flag(features, CPUFeatures::X86_SHA | CPUFeatures::X86_SSE42))
134
8
            return &SHA256::transform_impl<CPUFeatures::X86_SHA | CPUFeatures::X86_SSE42>;
135
8
    }
136
137
0
    return &SHA256::transform_impl<CPUFeatures::None>;
138
8
}();
139
140
template<size_t BlockSize, typename Callback>
141
void update_buffer(u8* buffer, u8 const* input, size_t length, size_t& data_length, Callback callback)
142
272
{
143
201k
    while (length > 0) {
144
201k
        size_t copy_bytes = AK::min(length, BlockSize - data_length);
145
201k
        __builtin_memcpy(buffer + data_length, input, copy_bytes);
146
201k
        input += copy_bytes;
147
201k
        length -= copy_bytes;
148
201k
        data_length += copy_bytes;
149
201k
        if (data_length == BlockSize) {
150
201k
            callback();
151
201k
            data_length = 0;
152
201k
        }
153
201k
    }
154
272
}
SHA2.cpp:void Crypto::Hash::update_buffer<64ul, Crypto::Hash::SHA256::update(unsigned char const*, unsigned long)::$_0>(unsigned char*, unsigned char const*, unsigned long, unsigned long&, Crypto::Hash::SHA256::update(unsigned char const*, unsigned long)::$_0)
Line
Count
Source
142
88
{
143
99.8k
    while (length > 0) {
144
99.7k
        size_t copy_bytes = AK::min(length, BlockSize - data_length);
145
99.7k
        __builtin_memcpy(buffer + data_length, input, copy_bytes);
146
99.7k
        input += copy_bytes;
147
99.7k
        length -= copy_bytes;
148
99.7k
        data_length += copy_bytes;
149
99.7k
        if (data_length == BlockSize) {
150
99.6k
            callback();
151
99.6k
            data_length = 0;
152
99.6k
        }
153
99.7k
    }
154
88
}
SHA2.cpp:void Crypto::Hash::update_buffer<128ul, Crypto::Hash::SHA384::update(unsigned char const*, unsigned long)::$_0>(unsigned char*, unsigned char const*, unsigned long, unsigned long&, Crypto::Hash::SHA384::update(unsigned char const*, unsigned long)::$_0)
Line
Count
Source
142
92
{
143
50.9k
    while (length > 0) {
144
50.9k
        size_t copy_bytes = AK::min(length, BlockSize - data_length);
145
50.9k
        __builtin_memcpy(buffer + data_length, input, copy_bytes);
146
50.9k
        input += copy_bytes;
147
50.9k
        length -= copy_bytes;
148
50.9k
        data_length += copy_bytes;
149
50.9k
        if (data_length == BlockSize) {
150
50.8k
            callback();
151
50.8k
            data_length = 0;
152
50.8k
        }
153
50.9k
    }
154
92
}
SHA2.cpp:void Crypto::Hash::update_buffer<128ul, Crypto::Hash::SHA512::update(unsigned char const*, unsigned long)::$_0>(unsigned char*, unsigned char const*, unsigned long, unsigned long&, Crypto::Hash::SHA512::update(unsigned char const*, unsigned long)::$_0)
Line
Count
Source
142
92
{
143
50.9k
    while (length > 0) {
144
50.9k
        size_t copy_bytes = AK::min(length, BlockSize - data_length);
145
50.9k
        __builtin_memcpy(buffer + data_length, input, copy_bytes);
146
50.9k
        input += copy_bytes;
147
50.9k
        length -= copy_bytes;
148
50.9k
        data_length += copy_bytes;
149
50.9k
        if (data_length == BlockSize) {
150
50.8k
            callback();
151
50.8k
            data_length = 0;
152
50.8k
        }
153
50.9k
    }
154
92
}
155
156
void SHA256::update(u8 const* message, size_t length)
157
88
{
158
99.6k
    update_buffer<BlockSize>(m_data_buffer, message, length, m_data_length, [&]() {
159
99.6k
        transform();
160
99.6k
        m_bit_length += BlockSize * 8;
161
99.6k
    });
162
88
}
163
164
SHA256::DigestType SHA256::digest()
165
88
{
166
88
    auto digest = peek();
167
88
    reset();
168
88
    return digest;
169
88
}
170
171
SHA256::DigestType SHA256::peek()
172
88
{
173
88
    DigestType digest;
174
88
    size_t i = m_data_length;
175
176
88
    if (i < FinalBlockDataSize) {
177
37
        m_data_buffer[i++] = 0x80;
178
1.49k
        while (i < FinalBlockDataSize)
179
1.45k
            m_data_buffer[i++] = 0x00;
180
51
    } else {
181
        // First, complete a block with some padding.
182
51
        m_data_buffer[i++] = 0x80;
183
194
        while (i < BlockSize)
184
143
            m_data_buffer[i++] = 0x00;
185
51
        transform();
186
187
        // Then start another block with BlockSize - 8 bytes of zeros
188
51
        __builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
189
51
    }
190
191
    // append total message length
192
88
    m_bit_length += m_data_length * 8;
193
88
    m_data_buffer[BlockSize - 1] = m_bit_length;
194
88
    m_data_buffer[BlockSize - 2] = m_bit_length >> 8;
195
88
    m_data_buffer[BlockSize - 3] = m_bit_length >> 16;
196
88
    m_data_buffer[BlockSize - 4] = m_bit_length >> 24;
197
88
    m_data_buffer[BlockSize - 5] = m_bit_length >> 32;
198
88
    m_data_buffer[BlockSize - 6] = m_bit_length >> 40;
199
88
    m_data_buffer[BlockSize - 7] = m_bit_length >> 48;
200
88
    m_data_buffer[BlockSize - 8] = m_bit_length >> 56;
201
202
88
    transform();
203
204
    // SHA uses big-endian and we assume little-endian
205
    // FIXME: looks like a thing for AK::NetworkOrdered,
206
    //        but that doesn't support shifting operations
207
440
    for (i = 0; i < 4; ++i) {
208
352
        digest.data[i + 0] = (m_state[0] >> (24 - i * 8)) & 0x000000ff;
209
352
        digest.data[i + 4] = (m_state[1] >> (24 - i * 8)) & 0x000000ff;
210
352
        digest.data[i + 8] = (m_state[2] >> (24 - i * 8)) & 0x000000ff;
211
352
        digest.data[i + 12] = (m_state[3] >> (24 - i * 8)) & 0x000000ff;
212
352
        digest.data[i + 16] = (m_state[4] >> (24 - i * 8)) & 0x000000ff;
213
352
        digest.data[i + 20] = (m_state[5] >> (24 - i * 8)) & 0x000000ff;
214
352
        digest.data[i + 24] = (m_state[6] >> (24 - i * 8)) & 0x000000ff;
215
352
        digest.data[i + 28] = (m_state[7] >> (24 - i * 8)) & 0x000000ff;
216
352
    }
217
88
    return digest;
218
88
}
219
220
inline void SHA384::transform(u8 const* data)
221
50.9k
{
222
50.9k
    u64 m[16];
223
224
50.9k
    size_t i = 0;
225
866k
    for (size_t j = 0; i < 16; ++i, j += 8) {
226
815k
        m[i] = ((u64)data[j] << 56) | ((u64)data[j + 1] << 48) | ((u64)data[j + 2] << 40) | ((u64)data[j + 3] << 32) | ((u64)data[j + 4] << 24) | ((u64)data[j + 5] << 16) | ((u64)data[j + 6] << 8) | (u64)data[j + 7];
227
815k
    }
228
229
50.9k
    auto a = m_state[0], b = m_state[1],
230
50.9k
         c = m_state[2], d = m_state[3],
231
50.9k
         e = m_state[4], f = m_state[5],
232
50.9k
         g = m_state[6], h = m_state[7];
233
234
4.12M
    for (i = 0; i < Rounds; ++i) {
235
4.07M
        if (i >= 16)
236
3.26M
            m[i % 16] = SIGN1(m[(i - 2) % 16]) + m[(i - 7) % 16] + SIGN0(m[(i - 15) % 16]) + m[(i - 16) % 16];
237
        // Note : SHA384 uses the SHA512 constants.
238
4.07M
        auto temp0 = h + EP1(e) + CH(e, f, g) + SHA512Constants::RoundConstants[i] + m[i % 16];
239
4.07M
        auto temp1 = EP0(a) + MAJ(a, b, c);
240
4.07M
        h = g;
241
4.07M
        g = f;
242
4.07M
        f = e;
243
4.07M
        e = d + temp0;
244
4.07M
        d = c;
245
4.07M
        c = b;
246
4.07M
        b = a;
247
4.07M
        a = temp0 + temp1;
248
4.07M
    }
249
250
50.9k
    m_state[0] += a;
251
50.9k
    m_state[1] += b;
252
50.9k
    m_state[2] += c;
253
50.9k
    m_state[3] += d;
254
50.9k
    m_state[4] += e;
255
50.9k
    m_state[5] += f;
256
50.9k
    m_state[6] += g;
257
50.9k
    m_state[7] += h;
258
50.9k
}
259
260
void SHA384::update(u8 const* message, size_t length)
261
92
{
262
50.8k
    update_buffer<BlockSize>(m_data_buffer, message, length, m_data_length, [&]() {
263
50.8k
        transform(m_data_buffer);
264
50.8k
        m_bit_length += BlockSize * 8;
265
50.8k
    });
266
92
}
267
268
SHA384::DigestType SHA384::digest()
269
92
{
270
92
    auto digest = peek();
271
92
    reset();
272
92
    return digest;
273
92
}
274
275
SHA384::DigestType SHA384::peek()
276
92
{
277
92
    DigestType digest;
278
92
    size_t i = m_data_length;
279
280
92
    if (i < FinalBlockDataSize) {
281
40
        m_data_buffer[i++] = 0x80;
282
3.10k
        while (i < FinalBlockDataSize)
283
3.06k
            m_data_buffer[i++] = 0x00;
284
52
    } else {
285
        // First, complete a block with some padding.
286
52
        m_data_buffer[i++] = 0x80;
287
377
        while (i < BlockSize)
288
325
            m_data_buffer[i++] = 0x00;
289
52
        transform(m_data_buffer);
290
291
        // Then start another block with BlockSize - 8 bytes of zeros
292
52
        __builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
293
52
    }
294
295
    // append total message length
296
92
    m_bit_length += m_data_length * 8;
297
92
    m_data_buffer[BlockSize - 1] = m_bit_length;
298
92
    m_data_buffer[BlockSize - 2] = m_bit_length >> 8;
299
92
    m_data_buffer[BlockSize - 3] = m_bit_length >> 16;
300
92
    m_data_buffer[BlockSize - 4] = m_bit_length >> 24;
301
92
    m_data_buffer[BlockSize - 5] = m_bit_length >> 32;
302
92
    m_data_buffer[BlockSize - 6] = m_bit_length >> 40;
303
92
    m_data_buffer[BlockSize - 7] = m_bit_length >> 48;
304
92
    m_data_buffer[BlockSize - 8] = m_bit_length >> 56;
305
    // FIXME: Theoretically we should keep track of the number of bits as a u128, now we can only hash up to 2 EiB.
306
92
    m_data_buffer[BlockSize - 9] = 0;
307
92
    m_data_buffer[BlockSize - 10] = 0;
308
92
    m_data_buffer[BlockSize - 11] = 0;
309
92
    m_data_buffer[BlockSize - 12] = 0;
310
92
    m_data_buffer[BlockSize - 13] = 0;
311
92
    m_data_buffer[BlockSize - 14] = 0;
312
92
    m_data_buffer[BlockSize - 15] = 0;
313
92
    m_data_buffer[BlockSize - 16] = 0;
314
315
92
    transform(m_data_buffer);
316
317
    // SHA uses big-endian and we assume little-endian
318
    // FIXME: looks like a thing for AK::NetworkOrdered,
319
    //        but that doesn't support shifting operations
320
828
    for (i = 0; i < 8; ++i) {
321
736
        digest.data[i + 0] = (m_state[0] >> (56 - i * 8)) & 0x000000ff;
322
736
        digest.data[i + 8] = (m_state[1] >> (56 - i * 8)) & 0x000000ff;
323
736
        digest.data[i + 16] = (m_state[2] >> (56 - i * 8)) & 0x000000ff;
324
736
        digest.data[i + 24] = (m_state[3] >> (56 - i * 8)) & 0x000000ff;
325
736
        digest.data[i + 32] = (m_state[4] >> (56 - i * 8)) & 0x000000ff;
326
736
        digest.data[i + 40] = (m_state[5] >> (56 - i * 8)) & 0x000000ff;
327
736
    }
328
92
    return digest;
329
92
}
330
331
inline void SHA512::transform(u8 const* data)
332
50.9k
{
333
50.9k
    u64 m[16];
334
335
50.9k
    size_t i = 0;
336
866k
    for (size_t j = 0; i < 16; ++i, j += 8) {
337
815k
        m[i] = ((u64)data[j] << 56) | ((u64)data[j + 1] << 48) | ((u64)data[j + 2] << 40) | ((u64)data[j + 3] << 32) | ((u64)data[j + 4] << 24) | ((u64)data[j + 5] << 16) | ((u64)data[j + 6] << 8) | (u64)data[j + 7];
338
815k
    }
339
340
50.9k
    auto a = m_state[0], b = m_state[1],
341
50.9k
         c = m_state[2], d = m_state[3],
342
50.9k
         e = m_state[4], f = m_state[5],
343
50.9k
         g = m_state[6], h = m_state[7];
344
345
4.12M
    for (i = 0; i < Rounds; ++i) {
346
4.07M
        if (i >= 16)
347
3.26M
            m[i % 16] = SIGN1(m[(i - 2) % 16]) + m[(i - 7) % 16] + SIGN0(m[(i - 15) % 16]) + m[(i - 16) % 16];
348
349
4.07M
        auto temp0 = h + EP1(e) + CH(e, f, g) + SHA512Constants::RoundConstants[i] + m[i % 16];
350
4.07M
        auto temp1 = EP0(a) + MAJ(a, b, c);
351
4.07M
        h = g;
352
4.07M
        g = f;
353
4.07M
        f = e;
354
4.07M
        e = d + temp0;
355
4.07M
        d = c;
356
4.07M
        c = b;
357
4.07M
        b = a;
358
4.07M
        a = temp0 + temp1;
359
4.07M
    }
360
361
50.9k
    m_state[0] += a;
362
50.9k
    m_state[1] += b;
363
50.9k
    m_state[2] += c;
364
50.9k
    m_state[3] += d;
365
50.9k
    m_state[4] += e;
366
50.9k
    m_state[5] += f;
367
50.9k
    m_state[6] += g;
368
50.9k
    m_state[7] += h;
369
50.9k
}
370
371
void SHA512::update(u8 const* message, size_t length)
372
92
{
373
50.8k
    update_buffer<BlockSize>(m_data_buffer, message, length, m_data_length, [&]() {
374
50.8k
        transform(m_data_buffer);
375
50.8k
        m_bit_length += BlockSize * 8;
376
50.8k
    });
377
92
}
378
379
SHA512::DigestType SHA512::digest()
380
92
{
381
92
    auto digest = peek();
382
92
    reset();
383
92
    return digest;
384
92
}
385
386
SHA512::DigestType SHA512::peek()
387
92
{
388
92
    DigestType digest;
389
92
    size_t i = m_data_length;
390
391
92
    if (i < FinalBlockDataSize) {
392
40
        m_data_buffer[i++] = 0x80;
393
3.10k
        while (i < FinalBlockDataSize)
394
3.06k
            m_data_buffer[i++] = 0x00;
395
52
    } else {
396
        // First, complete a block with some padding.
397
52
        m_data_buffer[i++] = 0x80;
398
377
        while (i < BlockSize)
399
325
            m_data_buffer[i++] = 0x00;
400
52
        transform(m_data_buffer);
401
402
        // Then start another block with BlockSize - 8 bytes of zeros
403
52
        __builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
404
52
    }
405
406
    // append total message length
407
92
    m_bit_length += m_data_length * 8;
408
92
    m_data_buffer[BlockSize - 1] = m_bit_length;
409
92
    m_data_buffer[BlockSize - 2] = m_bit_length >> 8;
410
92
    m_data_buffer[BlockSize - 3] = m_bit_length >> 16;
411
92
    m_data_buffer[BlockSize - 4] = m_bit_length >> 24;
412
92
    m_data_buffer[BlockSize - 5] = m_bit_length >> 32;
413
92
    m_data_buffer[BlockSize - 6] = m_bit_length >> 40;
414
92
    m_data_buffer[BlockSize - 7] = m_bit_length >> 48;
415
92
    m_data_buffer[BlockSize - 8] = m_bit_length >> 56;
416
    // FIXME: Theoretically we should keep track of the number of bits as a u128, now we can only hash up to 2 EiB.
417
92
    m_data_buffer[BlockSize - 9] = 0;
418
92
    m_data_buffer[BlockSize - 10] = 0;
419
92
    m_data_buffer[BlockSize - 11] = 0;
420
92
    m_data_buffer[BlockSize - 12] = 0;
421
92
    m_data_buffer[BlockSize - 13] = 0;
422
92
    m_data_buffer[BlockSize - 14] = 0;
423
92
    m_data_buffer[BlockSize - 15] = 0;
424
92
    m_data_buffer[BlockSize - 16] = 0;
425
426
92
    transform(m_data_buffer);
427
428
    // SHA uses big-endian and we assume little-endian
429
    // FIXME: looks like a thing for AK::NetworkOrdered,
430
    //        but that doesn't support shifting operations
431
828
    for (i = 0; i < 8; ++i) {
432
736
        digest.data[i + 0] = (m_state[0] >> (56 - i * 8)) & 0x000000ff;
433
736
        digest.data[i + 8] = (m_state[1] >> (56 - i * 8)) & 0x000000ff;
434
736
        digest.data[i + 16] = (m_state[2] >> (56 - i * 8)) & 0x000000ff;
435
736
        digest.data[i + 24] = (m_state[3] >> (56 - i * 8)) & 0x000000ff;
436
736
        digest.data[i + 32] = (m_state[4] >> (56 - i * 8)) & 0x000000ff;
437
736
        digest.data[i + 40] = (m_state[5] >> (56 - i * 8)) & 0x000000ff;
438
736
        digest.data[i + 48] = (m_state[6] >> (56 - i * 8)) & 0x000000ff;
439
736
        digest.data[i + 56] = (m_state[7] >> (56 - i * 8)) & 0x000000ff;
440
736
    }
441
92
    return digest;
442
92
}
443
}