Browse Source

Support multi-block SHA256 transforms

Extracted from a patch by Wladimir van der Laan.
0.15
Pieter Wuille 7 years ago
parent
commit
4d50f38fe0
  1. 19
      src/crypto/sha256.cpp

19
src/crypto/sha256.cpp

@ -43,9 +43,10 @@ void inline Initialize(uint32_t* s) @@ -43,9 +43,10 @@ void inline Initialize(uint32_t* s)
s[7] = 0x5be0cd19ul;
}
/** Perform one SHA-256 transformation, processing a 64-byte chunk. */
void Transform(uint32_t* s, const unsigned char* chunk)
/** Perform a number of SHA-256 transformations, processing 64-byte chunks. */
void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks)
{
while (blocks--) {
uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7];
uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
@ -125,6 +126,8 @@ void Transform(uint32_t* s, const unsigned char* chunk) @@ -125,6 +126,8 @@ void Transform(uint32_t* s, const unsigned char* chunk)
s[5] += f;
s[6] += g;
s[7] += h;
chunk += 64;
}
}
} // namespace sha256
@ -147,14 +150,14 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len) @@ -147,14 +150,14 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
memcpy(buf + bufsize, data, 64 - bufsize);
bytes += 64 - bufsize;
data += 64 - bufsize;
sha256::Transform(s, buf);
sha256::Transform(s, buf, 1);
bufsize = 0;
}
while (end >= data + 64) {
// Process full chunks directly from the source.
sha256::Transform(s, data);
bytes += 64;
data += 64;
if (end - data >= 64) {
size_t blocks = (end - data) / 64;
sha256::Transform(s, data, blocks);
data += 64 * blocks;
bytes += 64 * blocks;
}
if (end > data) {
// Fill the buffer with what remains.

Loading…
Cancel
Save