Skip to content

Commit

Permalink
crypto: adapt sha256 multi-block transforms with intel avx2
Browse files Browse the repository at this point in the history
-adapt sha256 to conditionally support multi-block transforms if USE_AVX2 is not defined
  • Loading branch information
xanimo committed Apr 23, 2024
1 parent ca56c77 commit 4894ff8
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/crypto/sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ void inline Initialize(uint32_t* s)
s[7] = 0x5be0cd19ul;
}

#ifndef USE_AVX2
/** Perform a number of SHA-256 transformations, processing 64-byte chunks. */
void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks)
#else
/** Perform one SHA-256 transformation, processing a 64-byte chunk. */
void Transform(uint32_t* s, const unsigned char* chunk)
#endif
{
#if defined(USE_ARMV8) || defined(USE_ARMV82)
// entire block is experimental
Expand Down Expand Up @@ -381,7 +386,7 @@ bool SelfTest(TransformType tr) {
return true;
}

TransformType Transform = sha256::Transform;
TransformType Transform = ::Transform;

} // namespace

Expand Down Expand Up @@ -416,15 +421,29 @@ CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
memcpy(buf + bufsize, data, 64 - bufsize);
bytes += 64 - bufsize;
data += 64 - bufsize;
Transform(s, buf, 1);
#ifndef USE_AVX2
sha256::Transform(s, buf, 1);
#else
sha256::Transform(s, buf);
#endif
bufsize = 0;
}

#ifndef USE_AVX2
if (end - data >= 64) {
size_t blocks = (end - data) / 64;
Transform(s, data, blocks);
data += 64 * blocks;
bytes += 64 * blocks;
}
#else
while (end >= data + 64) {
// Process full chunks directly from the source.
sha256::Transform(s, data);
bytes += 64;
data += 64;
}
#endif
if (end > data) {
// Fill the buffer with what remains.
memcpy(buf + bufsize, data, end - data);
Expand Down

0 comments on commit 4894ff8

Please sign in to comment.