Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Crypto_light by
Diff: SHA2_64.cpp
- Revision:
- 5:06cd9c8afa0b
- Parent:
- 4:0da19393bd57
- Child:
- 6:19aa835f2bbb
--- a/SHA2_64.cpp Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA2_64.cpp Thu Sep 12 15:08:51 2013 +0000
@@ -15,8 +15,8 @@
static uint64_t revWord(uint64_t w)
{
- return __rev((w & 0xFFFFFFFF00000000) >> 32)
- | ((uint64_t)(__rev(w & 0x00000000FFFFFFFF)) << 32);
+ return __rev(w >> 32)
+ | ((uint64_t)(__rev(w)) << 32);
}
#define ROTL(W,N) (((W) << (N)) | ((W) >> (64-(N))))
@@ -65,7 +65,7 @@
}
}
-void SHA2_64::add(uint8_t *in, uint32_t length)
+void SHA2_64::update(uint8_t *in, uint32_t length)
{
if(length < 128-bufferLength)
{
@@ -90,24 +90,31 @@
totalBufferLength += length;
}
-void SHA2_64::computeDigest(uint8_t *digest)
+void SHA2_64::finalize(uint8_t *digest)
{
- uint16_t padding;
+ uint64_t lengthBit = totalBufferLength << 3;
+ uint32_t padding;
if(totalBufferLength % 128 < 112)
padding = 112 - (totalBufferLength % 128);
else
padding = 112 + (128 - (totalBufferLength % 128));
- uint8_t val = 0x80;
- add(&val, 1);
- val = 0;
- for(int i = 0; i < padding-1; ++i)
- add(&val,1);
- totalBufferLength -= padding;
- uint64_t lengthBit = 0;
- add((uint8_t*)&lengthBit, 8);
- lengthBit = (totalBufferLength - 8) * 8;
+
+ buffer[bufferLength++] = 0x80;
+ padding--;
+ if(padding+bufferLength == 112)
+ memset(&buffer[bufferLength], 0, padding);
+ else
+ {
+ memset(&buffer[bufferLength], 0, 64-bufferLength);
+ computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+ memset(buffer, 0, 112);
+ }
+
lengthBit = revWord(lengthBit);
- add((uint8_t*)&lengthBit, 8);
+ memcpy(&buffer[120], &lengthBit, 8);
+ memset(&buffer[112], 0, 8);
+ computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+
h0 = revWord(h0);
h1 = revWord(h1);
@@ -330,7 +337,7 @@
{
memset(&buffer[length], 0, 128-length);
computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
- memset(buffer, 0, length);
+ memset(buffer, 0, 112);
}
lengthBit = revWord(lengthBit);
