diff --git a/TestVectors/sm3.txt b/TestVectors/sm3.txt index 227a0486..caa970d4 100644 --- a/TestVectors/sm3.txt +++ b/TestVectors/sm3.txt @@ -10,10 +10,10 @@ Digest: 00000000 62eeedd9 d1f2d46b dc10e4e2 4167c487 5cf2f7a2 297da02b 8f4ba8e0 Test: NotVerify # Source: SM3 Hash function, https://tools.ietf.org/html/draft-shen-sm3-hash -Comment: Appendix B, test vector 1 +Comment: Appendix B, test vector 2 Message: 61626364 61626364 61626364 61626364 61626364 61626364 61626364 61626364 \ 61626364 61626364 61626364 61626364 61626364 61626364 61626364 61626364 Digest: debe9ff9 2275b8a1 38604889 c18e5a4d 6fdb70e5 387e5765 293dcba3 9c0c5732 Test: Verify Digest: 00000000 2275b8a1 38604889 c18e5a4d 6fdb70e5 387e5765 293dcba3 9c0c5732 -Test: NotVerify \ No newline at end of file +Test: NotVerify diff --git a/sm3.cpp b/sm3.cpp index 12e18cb0..dda6f0b1 100644 --- a/sm3.cpp +++ b/sm3.cpp @@ -73,19 +73,18 @@ inline word32 SM3_E(word32 W0, word32 W7, word32 W13, word32 W3, word32 W10) return P1(W0 ^ W7 ^ rotlFixed(W13, 15)) ^ rotlFixed(W3, 7) ^ W10; } -static size_t SM3_HashMultipleBlocks_CXX(word32 *state, const word32 *input, size_t length) +static size_t SM3_HashMultipleBlocks_CXX(word32 *state, const word32 *data, size_t length) { - CRYPTOPP_ASSERT(input); + CRYPTOPP_ASSERT(data); word32 A = state[0], B = state[1], C = state[2], D = state[3]; word32 E = state[4], F = state[5], G = state[6], H = state[7]; - size_t blocks = length / SM3::BLOCKSIZE; - for(size_t i = 0; i < blocks; ++i) + while (length >= SM3::BLOCKSIZE) { // Reverse bytes on LittleEndian; align pointer on BigEndian typedef GetBlock InBlock; - InBlock iblk(input); + InBlock iblk(data); word32 W00, W01, W02, W03, W04, W05, W06, W07, W08, W09, W10, W11, W12, W13, W14, W15; iblk(W00)(W01)(W02)(W03)(W04)(W05)(W06)(W07)(W08)(W09)(W10)(W11)(W12)(W13)(W14)(W15); @@ -216,10 +215,11 @@ static size_t SM3_HashMultipleBlocks_CXX(word32 *state, const word32 *input, siz G = (state[6] ^= G); H = (state[7] ^= H); - input += SM3::BLOCKSIZE/sizeof(word32); + data += SM3::BLOCKSIZE/sizeof(word32); + length -= SM3::BLOCKSIZE; } - return length & (SM3::BLOCKSIZE-1); + return length; } ANONYMOUS_NAMESPACE_END diff --git a/sm3.h b/sm3.h index 9a21f88a..c9d093cf 100644 --- a/sm3.h +++ b/sm3.h @@ -29,23 +29,21 @@ public: //! \param state the state of the hash //! \details InitState sets a state array to SHA256 initial values //! \details Hashes which derive from IteratedHashWithStaticTransform provide static - //! member functions InitState and Transform. External classes, like SEAL and MDC, + //! member functions InitState() and Transform(). External classes, like SEAL and MDC, //! can initialize state with a user provided key and operate the hash on the data //! with the user supplied state. - //! \note On Intel platforms the state array must be 16-byte aligned for SSE2. static void InitState(HashWordType *state); //! \brief Operate the hash //! \param digest the state of the hash //! \param data the data to be digested - //! \details Transform operates the hash on data. When the call is invoked - //! digest holds initial state. Upon return digest holds the hash - //! or updated state. + //! \details Transform() operates the hash on data. When the call is invoked + //! digest holds initial or current state. Upon return digest holds + //! the hash or updated state. //! \details Hashes which derive from IteratedHashWithStaticTransform provide static - //! member functions InitState and Transform. External classes, like SEAL and MDC, + //! member functions InitState() and Transform(). External classes, like SEAL and MDC, //! can initialize state with a user provided key and operate the hash on the data //! with the user supplied state. - //! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2. static void Transform(HashWordType *digest, const HashWordType *data); //! \brief The algorithm name