Skip to content

Commit 8b99d2a

Browse files
authored
fix(sha512): use 64-bit counters
1 parent d79561f commit 8b99d2a

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

sha512.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ namespace hmac_hash {
179179
size_t block_nb;
180180
size_t new_len, rem_len, tmp_len;
181181
const uint8_t *shifted_message;
182-
tmp_len = SHA384_512_BLOCK_SIZE - m_len;
182+
tmp_len = SHA384_512_BLOCK_SIZE - static_cast<size_t>(m_len);
183183
rem_len = length < tmp_len ? length : tmp_len;
184-
memcpy(&m_block[m_len], message, rem_len);
184+
memcpy(&m_block[static_cast<size_t>(m_len)], message, rem_len);
185185
if((m_len + length) < SHA384_512_BLOCK_SIZE) {
186186
m_len += length;
187187
return;
@@ -194,7 +194,7 @@ namespace hmac_hash {
194194
rem_len = new_len % SHA384_512_BLOCK_SIZE;
195195
memcpy(m_block, &shifted_message[block_nb << 7], rem_len);
196196
m_len = rem_len;
197-
m_tot_len += (block_nb + 1) << 7;
197+
m_tot_len += static_cast<uint64_t>(block_nb + 1) << 7;
198198
}
199199

200200
void SHA512::finish(uint8_t *digest) {
@@ -203,11 +203,11 @@ namespace hmac_hash {
203203
uint64_t len_b; // message length in bits
204204
size_t i;
205205
block_nb = (1 + ((SHA384_512_BLOCK_SIZE - 9)
206-
< (m_len % SHA384_512_BLOCK_SIZE)));
206+
< (static_cast<size_t>(m_len) % SHA384_512_BLOCK_SIZE)));
207207
len_b = (m_tot_len + m_len) << 3;
208208
pm_len = block_nb << 7;
209-
memset(m_block + m_len, 0, pm_len - m_len);
210-
m_block[m_len] = 0x80;
209+
memset(m_block + static_cast<size_t>(m_len), 0, pm_len - static_cast<size_t>(m_len));
210+
m_block[static_cast<size_t>(m_len)] = 0x80;
211211
SHA2_UNPACK64(len_b, m_block + pm_len - 8);
212212
transform(m_block, block_nb);
213213
for(i = 0 ; i < 8; ++i) {

sha512.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ namespace hmac_hash {
7676

7777
protected:
7878
void transform(const uint8_t *message, size_t block_nb);
79-
size_t m_tot_len;
80-
size_t m_len;
79+
uint64_t m_tot_len;
80+
uint64_t m_len;
8181
uint8_t m_block[2 * SHA384_512_BLOCK_SIZE];
8282
uint64_t m_h[8];
8383
};

test_all.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <gtest/gtest.h>
22
#include <string>
33
#include <stdexcept>
4+
#include <vector>
45
#include <limits>
6+
57
#include "hmac.hpp"
68
#include "hmac_utils.hpp"
79

@@ -26,6 +28,23 @@ TEST(HashTest, SHA512) {
2628
"9375d1abdb644a01955bccad12e2f5c2bd8a3e226187e548d99c559a99461453b980123746753d07c169c22a5d9cc75cb158f0e8d8c0e713559775b5e1391fc4");
2729
}
2830

31+
TEST(HashTest, SHA512LargeInput) {
32+
hmac_hash::SHA512 ctx;
33+
ctx.init();
34+
std::vector<uint8_t> chunk(1024 * 1024, 'a');
35+
for (size_t i = 0; i < 4096; ++i) {
36+
ctx.update(chunk.data(), chunk.size());
37+
}
38+
uint8_t tail = 'b';
39+
ctx.update(&tail, 1);
40+
41+
uint8_t digest[hmac_hash::SHA512::DIGEST_SIZE];
42+
ctx.finish(digest);
43+
std::string result(reinterpret_cast<char*>(digest), hmac_hash::SHA512::DIGEST_SIZE);
44+
EXPECT_EQ(hmac::to_hex(result),
45+
"596d71e02b4eca81f668215d3e9b9e5a143a9c3d8d1981608e0811b20e290961ec2a7e7ecd0e275366cf10aa5f7ab1e052b868c5fa57b6d2bd6e75477b2ecea7");
46+
}
47+
2948
TEST(UtilsTest, ToHex) {
3049
EXPECT_EQ(hmac::to_hex("012345"), "303132333435");
3150
}

0 commit comments

Comments
 (0)