void ShaFinal(Sha* sha, byte* hash) { byte* local = (byte*)sha->buffer; AddLength(sha, sha->buffLen); /* before adding pads */ local[sha->buffLen++] = 0x80; /* add 1 */ /* pad with zeros */ if (sha->buffLen > SHA_PAD_SIZE) { XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen); sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen; #ifdef LITTLE_ENDIAN_ORDER ByteReverseBytes(local, local, SHA_BLOCK_SIZE); #endif Transform(sha); sha->buffLen = 0; } XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen); /* put lengths in bits */ sha->loLen = sha->loLen << 3; sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + (sha->hiLen << 3); /* store lengths */ #ifdef LITTLE_ENDIAN_ORDER ByteReverseBytes(local, local, SHA_BLOCK_SIZE); #endif /* ! length ordering dependent on digest endian type ! */ XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32)); XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32)); Transform(sha); #ifdef LITTLE_ENDIAN_ORDER ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE); #endif XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE); InitSha(sha); /* reset state */ }
void Md4Final(Md4* md4, byte* hash) { byte* local = (byte*)md4->buffer; AddLength(md4, md4->buffLen); /* before adding pads */ local[md4->buffLen++] = 0x80; /* add 1 */ /* pad with zeros */ if (md4->buffLen > MD4_PAD_SIZE) { XMEMSET(&local[md4->buffLen], 0, MD4_BLOCK_SIZE - md4->buffLen); md4->buffLen += MD4_BLOCK_SIZE - md4->buffLen; #ifdef BIG_ENDIAN_ORDER ByteReverseBytes(local, local, MD4_BLOCK_SIZE); #endif Transform(md4); md4->buffLen = 0; } XMEMSET(&local[md4->buffLen], 0, MD4_PAD_SIZE - md4->buffLen); /* put lengths in bits */ md4->hiLen = (md4->loLen >> (8*sizeof(md4->loLen) - 3)) + (md4->hiLen << 3); md4->loLen = md4->loLen << 3; /* store lengths */ #ifdef BIG_ENDIAN_ORDER ByteReverseBytes(local, local, MD4_BLOCK_SIZE); #endif /* ! length ordering dependent on digest endian type ! */ XMEMCPY(&local[MD4_PAD_SIZE], &md4->loLen, sizeof(word32)); XMEMCPY(&local[MD4_PAD_SIZE + sizeof(word32)], &md4->hiLen, sizeof(word32)); Transform(md4); #ifdef BIG_ENDIAN_ORDER ByteReverseWords(md4->digest, md4->digest, MD4_DIGEST_SIZE); #endif XMEMCPY(hash, md4->digest, MD4_DIGEST_SIZE); InitMd4(md4); /* reset state */ }
void Sha512Final(Sha512* sha512, byte* hash) { byte* local = (byte*)sha512->buffer; AddLength(sha512, sha512->buffLen); /* before adding pads */ local[sha512->buffLen++] = 0x80; /* add 1 */ /* pad with zeros */ if (sha512->buffLen > SHA512_PAD_SIZE) { memset(&local[sha512->buffLen], 0, SHA512_BLOCK_SIZE - sha512->buffLen); sha512->buffLen += SHA512_BLOCK_SIZE - sha512->buffLen; #ifdef LITTLE_ENDIAN_ORDER ByteReverseWords64(sha512->buffer,sha512->buffer,SHA512_BLOCK_SIZE); #endif Transform(sha512); sha512->buffLen = 0; } memset(&local[sha512->buffLen], 0, SHA512_PAD_SIZE - sha512->buffLen); /* put lengths in bits */ sha512->loLen = sha512->loLen << 3; sha512->hiLen = (sha512->loLen >> (8*sizeof(sha512->loLen) - 3)) + (sha512->hiLen << 3); /* store lengths */ #ifdef LITTLE_ENDIAN_ORDER ByteReverseWords64(sha512->buffer, sha512->buffer, SHA512_PAD_SIZE); #endif /* ! length ordering dependent on digest endian type ! */ sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen; sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen; Transform(sha512); #ifdef LITTLE_ENDIAN_ORDER ByteReverseWords64(sha512->digest, sha512->digest, SHA512_DIGEST_SIZE); #endif memcpy(hash, sha512->digest, SHA512_DIGEST_SIZE); InitSha512(sha512); /* reset state */ }
void wc_Md5Final(Md5* md5, byte* hash) { byte* local = (byte*)md5->buffer; AddLength(md5, md5->buffLen); /* before adding pads */ local[md5->buffLen++] = 0x80; /* add 1 */ /* pad with zeros */ if (md5->buffLen > MD5_PAD_SIZE) { XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen); md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen; #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); #endif XTRANSFORM(md5, local); md5->buffLen = 0; } XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen); /* put lengths in bits */ md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) + (md5->hiLen << 3); md5->loLen = md5->loLen << 3; /* store lengths */ #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); #endif /* ! length ordering dependent on digest endian type ! */ XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32)); XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32)); XTRANSFORM(md5, local); #ifdef BIG_ENDIAN_ORDER ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE); #endif XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE); wc_InitMd5(md5); /* reset state */ }
void Sha256Final(Sha256* sha256, byte* hash) { byte* local = (byte*)sha256->buffer; AddLength(sha256, sha256->buffLen); /* before adding pads */ local[sha256->buffLen++] = 0x80; /* add 1 */ /* pad with zeros */ if (sha256->buffLen > SHA256_PAD_SIZE) { memset(&local[sha256->buffLen], 0, SHA256_BLOCK_SIZE - sha256->buffLen); sha256->buffLen += SHA256_BLOCK_SIZE - sha256->buffLen; #ifdef LITTLE_ENDIAN_ORDER ByteReverseBytes(local, local, SHA256_BLOCK_SIZE); #endif Transform(sha256); sha256->buffLen = 0; } memset(&local[sha256->buffLen], 0, SHA256_PAD_SIZE - sha256->buffLen); /* put lengths in bits */ sha256->loLen = sha256->loLen << 3; sha256->hiLen = (sha256->loLen >> (8*sizeof(sha256->loLen) - 3)) + (sha256->hiLen << 3); /* store lengths */ #ifdef LITTLE_ENDIAN_ORDER ByteReverseBytes(local, local, SHA256_BLOCK_SIZE); #endif memcpy(&local[SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32)); memcpy(&local[SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen, sizeof(word32)); Transform(sha256); #ifdef LITTLE_ENDIAN_ORDER ByteReverseWords(sha256->digest, sha256->digest, SHA256_DIGEST_SIZE); #endif memcpy(hash, sha256->digest, SHA256_DIGEST_SIZE); InitSha256(sha256); /* reset state */ }
void Md5Final(Md5* md5, byte* hash) { byte* local = (byte*)md5->buffer; AddLength(md5, md5->buffLen); /* before adding pads */ local[md5->buffLen++] = 0x80; /* add 1 */ /* pad with zeros */ if (md5->buffLen > MD5_PAD_SIZE) { memset(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen); md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen; #ifdef BIG_ENDIAN_ORDER ByteReverseBytes(local, local, MD5_BLOCK_SIZE); #endif Transform(md5); md5->buffLen = 0; } memset(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen); /* put lengths in bits */ md5->loLen = md5->loLen << 3; md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) + (md5->hiLen << 3); /* store lengths */ #ifdef BIG_ENDIAN_ORDER ByteReverseBytes(local, local, MD5_BLOCK_SIZE); #endif memcpy(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32)); memcpy(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32)); Transform(md5); #ifdef BIG_ENDIAN_ORDER ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE); #endif memcpy(hash, md5->digest, MD5_DIGEST_SIZE); InitMd5(md5); /* reset state */ }
// Update digest with data of size len, do in blocks void HASHwithTransform::Update(const byte* data, word32 len) { // do block size increments word32 blockSz = getBlockSize(); byte* local = reinterpret_cast<byte*>(buffer_); while (len) { word32 add = min(len, blockSz - buffLen_); memcpy(&local[buffLen_], data, add); buffLen_ += add; data += add; len -= add; if (buffLen_ == blockSz) { ByteReverseIf(local, local, blockSz, getByteOrder()); Transform(); AddLength(blockSz); buffLen_ = 0; } } }
void Md5Update(Md5* md5, const byte* data, word32 len) { /* do block size increments */ byte* local = (byte*)md5->buffer; while (len) { word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen); XMEMCPY(&local[md5->buffLen], data, add); md5->buffLen += add; data += add; len -= add; if (md5->buffLen == MD5_BLOCK_SIZE) { #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE); #endif XTRANSFORM(md5, local); AddLength(md5, MD5_BLOCK_SIZE); md5->buffLen = 0; } } }
void Md5Update(Md5* md5, const byte* data, word32 len) { /* do block size increments */ byte* local = (byte*)md5->buffer; while (len) { word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen); XMEMCPY(&local[md5->buffLen], data, add); md5->buffLen += add; data += add; len -= add; if (md5->buffLen == MD5_BLOCK_SIZE) { #ifdef BIG_ENDIAN_ORDER ByteReverseBytes(local, local, MD5_BLOCK_SIZE); #endif Transform(md5); AddLength(md5, MD5_BLOCK_SIZE); md5->buffLen = 0; } } }
void ShaUpdate(Sha* sha, const byte* data, word32 len) { /* do block size increments */ byte* local = (byte*)sha->buffer; while (len) { word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen); XMEMCPY(&local[sha->buffLen], data, add); sha->buffLen += add; data += add; len -= add; if (sha->buffLen == SHA_BLOCK_SIZE) { #ifdef LITTLE_ENDIAN_ORDER ByteReverseBytes(local, local, SHA_BLOCK_SIZE); #endif Transform(sha); AddLength(sha, SHA_BLOCK_SIZE); sha->buffLen = 0; } } }
void RipeMdUpdate(RipeMd* ripemd, const byte* data, word32 len) { /* do block size increments */ byte* local = (byte*)ripemd->buffer; while (len) { word32 add = min(len, RIPEMD_BLOCK_SIZE - ripemd->buffLen); XMEMCPY(&local[ripemd->buffLen], data, add); ripemd->buffLen += add; data += add; len -= add; if (ripemd->buffLen == RIPEMD_BLOCK_SIZE) { #ifdef BIG_ENDIAN_ORDER ByteReverseBytes(local, local, RIPEMD_BLOCK_SIZE); #endif Transform(ripemd); AddLength(ripemd, RIPEMD_BLOCK_SIZE); ripemd->buffLen = 0; } } }
void wc_Md4Update(Md4* md4, const byte* data, word32 len) { /* do block size increments */ byte* local = (byte*)md4->buffer; while (len) { word32 add = min(len, MD4_BLOCK_SIZE - md4->buffLen); XMEMCPY(&local[md4->buffLen], data, add); md4->buffLen += add; data += add; len -= add; if (md4->buffLen == MD4_BLOCK_SIZE) { #ifdef BIG_ENDIAN_ORDER ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE); #endif Transform(md4); AddLength(md4, MD4_BLOCK_SIZE); md4->buffLen = 0; } } }
void ShaUpdate(Sha* sha, const byte* data, word32 len) { /* do block size increments */ byte* local = (byte*)sha->buffer; while (len) { word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen); XMEMCPY(&local[sha->buffLen], data, add); sha->buffLen += add; data += add; len -= add; if (sha->buffLen == SHA_BLOCK_SIZE) { #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU) ByteReverseBytes(local, local, SHA_BLOCK_SIZE); #endif XTRANSFORM(sha, local); AddLength(sha, SHA_BLOCK_SIZE); sha->buffLen = 0; } } }
void Sha512Update(Sha512* sha512, const byte* data, word32 len) { /* do block size increments */ byte* local = (byte*)sha512->buffer; while (len) { word32 add = min(len, SHA512_BLOCK_SIZE - sha512->buffLen); memcpy(&local[sha512->buffLen], data, add); sha512->buffLen += add; data += add; len -= add; if (sha512->buffLen == SHA512_BLOCK_SIZE) { #ifdef LITTLE_ENDIAN_ORDER ByteReverseWords64(sha512->buffer, sha512->buffer, SHA512_BLOCK_SIZE); #endif Transform(sha512); AddLength(sha512, SHA512_BLOCK_SIZE); sha512->buffLen = 0; } } }
// Final process, place digest in hash void HASHwithTransform::Final(byte* hash) { word32 blockSz = getBlockSize(); word32 digestSz = getDigestSize(); word32 padSz = getPadSize(); ByteOrder order = getByteOrder(); AddLength(buffLen_); // before adding pads HashLengthType preLoLen = GetBitCountLo(); HashLengthType preHiLen = GetBitCountHi(); byte* local = reinterpret_cast<byte*>(buffer_); local[buffLen_++] = 0x80; // add 1 // pad with zeros if (buffLen_ > padSz) { memset(&local[buffLen_], 0, blockSz - buffLen_); buffLen_ += blockSz - buffLen_; ByteReverseIf(local, local, blockSz, order); Transform(); buffLen_ = 0; } memset(&local[buffLen_], 0, padSz - buffLen_); ByteReverseIf(local, local, blockSz, order); memcpy(&local[padSz], order ? &preHiLen : &preLoLen, sizeof(preLoLen)); memcpy(&local[padSz+4], order ? &preLoLen : &preHiLen, sizeof(preLoLen)); Transform(); ByteReverseIf(digest_, digest_, digestSz, order); memcpy(hash, digest_, digestSz); Init(); // reset state }