Fork of François Berder Crypto, fixed AES CBC and small rework
Dependents: AES_example shaun_larada Smartage
Fork of Crypto by
Diff: SHA2_32.cpp
- Revision:
- 6:19aa835f2bbb
- Parent:
- 5:06cd9c8afa0b
--- a/SHA2_32.cpp Thu Sep 12 15:08:51 2013 +0000 +++ b/SHA2_32.cpp Thu Sep 12 16:03:43 2013 +0000 @@ -66,33 +66,34 @@ } } -void SHA2_32::update(uint8_t *in, uint32_t length) +void SHA2_32::update(uint8_t *data, uint32_t length) { if(length < 64-bufferLength) { - memcpy(&buffer[bufferLength], in, length); + memcpy(&buffer[bufferLength], data, length); bufferLength += length; totalBufferLength += length; return; } int offset = 64-bufferLength; - memcpy(&buffer[bufferLength], in, offset); + memcpy(&buffer[bufferLength], data, offset); computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer); while(length-offset > 64) { - memcpy(buffer, &in[offset], 64); + memcpy(buffer, &data[offset], 64); computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer); offset += 64; } if(offset > length) offset -= 64; bufferLength = length - offset; - memcpy(buffer, &in[offset], bufferLength); + memcpy(buffer, &data[offset], bufferLength); totalBufferLength += length; } -void SHA2_32::finalize(uint8_t *digest) +void SHA2_32::finalize(uint8_t *hash) { + uint32_t *hash2 = (uint32_t*)hash; uint16_t padding; if(totalBufferLength % 64 < 56) padding = 56 - (totalBufferLength % 64); @@ -119,26 +120,17 @@ memcpy(&buffer[56], &lengthBitHigh, 4); computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); - h0 = __rev(h0); - h1 = __rev(h1); - h2 = __rev(h2); - h3 = __rev(h3); - h4 = __rev(h4); - h5 = __rev(h5); - h6 = __rev(h6); - memcpy(digest, &h0, 4); - memcpy(&digest[4], &h1, 4); - memcpy(&digest[8], &h2, 4); - memcpy(&digest[12], &h3, 4); - memcpy(&digest[16], &h4, 4); - memcpy(&digest[20], &h5, 4); - memcpy(&digest[24], &h6, 4); + hash2[0] = __rev(h0); + hash2[1] = __rev(h1); + hash2[2] = __rev(h2); + hash2[3] = __rev(h3); + hash2[4] = __rev(h4); + hash2[5] = __rev(h5); + hash2[6] = __rev(h6); + if(type == SHA_256) - { - h7 = __rev(h7); - memcpy(&digest[28], &h7, 4); - } + hash2[7] = __rev(h7); // reset state switch(type) @@ -169,6 +161,58 @@ bufferLength = 0; } +void SHA2_32::computeHash(SHA_32_TYPE type, uint8_t *hash, uint8_t *data, uint32_t length) +{ + uint32_t *hash2 = (uint32_t*)hash; + uint32_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3]; + uint32_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7]; + uint64_t lengthBit = length << 3; + uint16_t padding; + if(length % 64 < 56) + padding = 56 - (length % 64); + else + padding = 56 + (64 - (length % 64)); + + while(length >= 64) + { + computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, data); + length -= 64; + data += 64; + } + uint8_t buffer[64]; + memcpy(buffer, data,length); + buffer[length++] = 0x80; + padding--; + if(padding+length == 56) + memset(&buffer[length], 0, padding); + else + { + memset(&buffer[length], 0, 64-length); + computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); + memset(buffer, 0, 56); + } + + uint32_t lengthBitLow = lengthBit; + uint32_t lengthBitHigh = lengthBit >> 32; + lengthBitLow = __rev(lengthBitLow); + memcpy(&buffer[60], &lengthBitLow, 4); + lengthBitHigh = __rev(lengthBitHigh); + memcpy(&buffer[56], &lengthBitHigh, 4); + computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); + + hash2[0] = __rev(h0); + hash2[1] = __rev(h1); + hash2[2] = __rev(h2); + hash2[3] = __rev(h3); + hash2[4] = __rev(h4); + hash2[5] = __rev(h5); + hash2[6] = __rev(h6); + + + if(type == SHA_256) + hash2[7] = __rev(h7); +} + void SHA2_32::computeBlock(uint32_t *h02, uint32_t *h12, uint32_t *h22, @@ -268,64 +312,3 @@ *h72 += h; } -void SHA2_32::computeDigest(SHA_32_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length) -{ - uint32_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3]; - uint32_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7]; - uint64_t lengthBit = length << 3; - uint16_t padding; - if(length % 64 < 56) - padding = 56 - (length % 64); - else - padding = 56 + (64 - (length % 64)); - - while(length >= 64) - { - computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, in); - length -= 64; - in += 64; - } - uint8_t buffer[64]; - memcpy(buffer, in,length); - buffer[length++] = 0x80; - padding--; - if(padding+length == 56) - memset(&buffer[length], 0, padding); - else - { - memset(&buffer[length], 0, 64-length); - computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); - memset(buffer, 0, 56); - } - - uint32_t lengthBitLow = lengthBit; - uint32_t lengthBitHigh = lengthBit >> 32; - lengthBitLow = __rev(lengthBitLow); - memcpy(&buffer[60], &lengthBitLow, 4); - lengthBitHigh = __rev(lengthBitHigh); - memcpy(&buffer[56], &lengthBitHigh, 4); - computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); - - h0 = __rev(h0); - h1 = __rev(h1); - h2 = __rev(h2); - h3 = __rev(h3); - h4 = __rev(h4); - h5 = __rev(h5); - h6 = __rev(h6); - - memcpy(digest, &h0, 4); - memcpy(&digest[4], &h1, 4); - memcpy(&digest[8], &h2, 4); - memcpy(&digest[12], &h3, 4); - memcpy(&digest[16], &h4, 4); - memcpy(&digest[20], &h5, 4); - memcpy(&digest[24], &h6, 4); - - - if(type == SHA_256) - { - h7 = __rev(h7); - memcpy(&digest[28], &h7, 4); - } -}