Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions sha512.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ namespace hmac_hash {
size_t block_nb;
size_t new_len, rem_len, tmp_len;
const uint8_t *shifted_message;
tmp_len = SHA384_512_BLOCK_SIZE - m_len;
tmp_len = SHA384_512_BLOCK_SIZE - static_cast<size_t>(m_len);
rem_len = length < tmp_len ? length : tmp_len;
memcpy(&m_block[m_len], message, rem_len);
memcpy(&m_block[static_cast<size_t>(m_len)], message, rem_len);
if((m_len + length) < SHA384_512_BLOCK_SIZE) {
m_len += length;
return;
Expand All @@ -194,7 +194,7 @@ namespace hmac_hash {
rem_len = new_len % SHA384_512_BLOCK_SIZE;
memcpy(m_block, &shifted_message[block_nb << 7], rem_len);
m_len = rem_len;
m_tot_len += (block_nb + 1) << 7;
m_tot_len += static_cast<uint64_t>(block_nb + 1) << 7;
}

void SHA512::finish(uint8_t *digest) {
Expand All @@ -203,11 +203,11 @@ namespace hmac_hash {
uint64_t len_b; // message length in bits
size_t i;
block_nb = (1 + ((SHA384_512_BLOCK_SIZE - 9)
< (m_len % SHA384_512_BLOCK_SIZE)));
< (static_cast<size_t>(m_len) % SHA384_512_BLOCK_SIZE)));
len_b = (m_tot_len + m_len) << 3;
pm_len = block_nb << 7;
memset(m_block + m_len, 0, pm_len - m_len);
m_block[m_len] = 0x80;
memset(m_block + static_cast<size_t>(m_len), 0, pm_len - static_cast<size_t>(m_len));
m_block[static_cast<size_t>(m_len)] = 0x80;
SHA2_UNPACK64(len_b, m_block + pm_len - 8);
transform(m_block, block_nb);
for(i = 0 ; i < 8; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions sha512.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ namespace hmac_hash {

protected:
void transform(const uint8_t *message, size_t block_nb);
size_t m_tot_len;
size_t m_len;
uint64_t m_tot_len;
uint64_t m_len;
uint8_t m_block[2 * SHA384_512_BLOCK_SIZE];
uint64_t m_h[8];
};
Expand Down
19 changes: 19 additions & 0 deletions test_all.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <gtest/gtest.h>
#include <string>
#include <stdexcept>
#include <vector>
#include <limits>

#include "hmac.hpp"
#include "hmac_utils.hpp"

Expand All @@ -26,6 +28,23 @@ TEST(HashTest, SHA512) {
"9375d1abdb644a01955bccad12e2f5c2bd8a3e226187e548d99c559a99461453b980123746753d07c169c22a5d9cc75cb158f0e8d8c0e713559775b5e1391fc4");
}

TEST(HashTest, SHA512LargeInput) {
hmac_hash::SHA512 ctx;
ctx.init();
std::vector<uint8_t> chunk(1024 * 1024, 'a');
for (size_t i = 0; i < 4096; ++i) {
ctx.update(chunk.data(), chunk.size());
}
uint8_t tail = 'b';
ctx.update(&tail, 1);

uint8_t digest[hmac_hash::SHA512::DIGEST_SIZE];
ctx.finish(digest);
std::string result(reinterpret_cast<char*>(digest), hmac_hash::SHA512::DIGEST_SIZE);
EXPECT_EQ(hmac::to_hex(result),
"596d71e02b4eca81f668215d3e9b9e5a143a9c3d8d1981608e0811b20e290961ec2a7e7ecd0e275366cf10aa5f7ab1e052b868c5fa57b6d2bd6e75477b2ecea7");
}

TEST(UtilsTest, ToHex) {
EXPECT_EQ(hmac::to_hex("012345"), "303132333435");
}
Expand Down
Loading