Skip to content

Commit

Permalink
use array-based load_be/le in MD hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
reneme committed Jul 22, 2024
1 parent a66e252 commit bb653e9
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 548 deletions.
55 changes: 22 additions & 33 deletions src/lib/hash/md4/md4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,40 +79,29 @@ void MD4::compress_n(digest_type& digest, std::span<const uint8_t> input, size_t

BufferSlicer in(input);

std::array<uint32_t, 16> M;

for(size_t i = 0; i != blocks; ++i) {
const auto block = in.take(block_bytes).data();

uint32_t M00 = load_le<uint32_t>(block, 0);
uint32_t M01 = load_le<uint32_t>(block, 1);
uint32_t M02 = load_le<uint32_t>(block, 2);
uint32_t M03 = load_le<uint32_t>(block, 3);
uint32_t M04 = load_le<uint32_t>(block, 4);
uint32_t M05 = load_le<uint32_t>(block, 5);
uint32_t M06 = load_le<uint32_t>(block, 6);
uint32_t M07 = load_le<uint32_t>(block, 7);
uint32_t M08 = load_le<uint32_t>(block, 8);
uint32_t M09 = load_le<uint32_t>(block, 9);
uint32_t M10 = load_le<uint32_t>(block, 10);
uint32_t M11 = load_le<uint32_t>(block, 11);
uint32_t M12 = load_le<uint32_t>(block, 12);
uint32_t M13 = load_le<uint32_t>(block, 13);
uint32_t M14 = load_le<uint32_t>(block, 14);
uint32_t M15 = load_le<uint32_t>(block, 15);

FF4(A, B, C, D, M00, M01, M02, M03);
FF4(A, B, C, D, M04, M05, M06, M07);
FF4(A, B, C, D, M08, M09, M10, M11);
FF4(A, B, C, D, M12, M13, M14, M15);

GG4(A, B, C, D, M00, M04, M08, M12);
GG4(A, B, C, D, M01, M05, M09, M13);
GG4(A, B, C, D, M02, M06, M10, M14);
GG4(A, B, C, D, M03, M07, M11, M15);

HH4(A, B, C, D, M00, M08, M04, M12);
HH4(A, B, C, D, M02, M10, M06, M14);
HH4(A, B, C, D, M01, M09, M05, M13);
HH4(A, B, C, D, M03, M11, M07, M15);
load_le(M, in.take<block_bytes>());

// clang-format off

FF4(A, B, C, D, M[ 0], M[ 1], M[ 2], M[ 3]);
FF4(A, B, C, D, M[ 4], M[ 5], M[ 6], M[ 7]);
FF4(A, B, C, D, M[ 8], M[ 9], M[10], M[11]);
FF4(A, B, C, D, M[12], M[13], M[14], M[15]);

GG4(A, B, C, D, M[ 0], M[ 4], M[ 8], M[12]);
GG4(A, B, C, D, M[ 1], M[ 5], M[ 9], M[13]);
GG4(A, B, C, D, M[ 2], M[ 6], M[10], M[14]);
GG4(A, B, C, D, M[ 3], M[ 7], M[11], M[15]);

HH4(A, B, C, D, M[ 0], M[ 8], M[ 4], M[12]);
HH4(A, B, C, D, M[ 2], M[10], M[ 6], M[14]);
HH4(A, B, C, D, M[ 1], M[ 9], M[ 5], M[13]);
HH4(A, B, C, D, M[ 3], M[11], M[ 7], M[15]);

// clang-format on

A = (digest[0] += A);
B = (digest[1] += B);
Expand Down
100 changes: 52 additions & 48 deletions src/lib/hash/md5/md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,75 +66,79 @@ void MD5::compress_n(MD5::digest_type& digest, std::span<const uint8_t> input, s
BufferSlicer in(input);

for(size_t i = 0; i != blocks; ++i) {
load_le(M.data(), in.take(block_bytes).data(), M.size());

FF<7>(A, B, C, D, M[0] + 0xD76AA478);
FF<12>(D, A, B, C, M[1] + 0xE8C7B756);
FF<17>(C, D, A, B, M[2] + 0x242070DB);
FF<22>(B, C, D, A, M[3] + 0xC1BDCEEE);
FF<7>(A, B, C, D, M[4] + 0xF57C0FAF);
FF<12>(D, A, B, C, M[5] + 0x4787C62A);
FF<17>(C, D, A, B, M[6] + 0xA8304613);
FF<22>(B, C, D, A, M[7] + 0xFD469501);
FF<7>(A, B, C, D, M[8] + 0x698098D8);
FF<12>(D, A, B, C, M[9] + 0x8B44F7AF);
load_le(M, in.take<block_bytes>());

// clang-format off

FF< 7>(A, B, C, D, M[ 0] + 0xD76AA478);
FF<12>(D, A, B, C, M[ 1] + 0xE8C7B756);
FF<17>(C, D, A, B, M[ 2] + 0x242070DB);
FF<22>(B, C, D, A, M[ 3] + 0xC1BDCEEE);
FF< 7>(A, B, C, D, M[ 4] + 0xF57C0FAF);
FF<12>(D, A, B, C, M[ 5] + 0x4787C62A);
FF<17>(C, D, A, B, M[ 6] + 0xA8304613);
FF<22>(B, C, D, A, M[ 7] + 0xFD469501);
FF< 7>(A, B, C, D, M[ 8] + 0x698098D8);
FF<12>(D, A, B, C, M[ 9] + 0x8B44F7AF);
FF<17>(C, D, A, B, M[10] + 0xFFFF5BB1);
FF<22>(B, C, D, A, M[11] + 0x895CD7BE);
FF<7>(A, B, C, D, M[12] + 0x6B901122);
FF< 7>(A, B, C, D, M[12] + 0x6B901122);
FF<12>(D, A, B, C, M[13] + 0xFD987193);
FF<17>(C, D, A, B, M[14] + 0xA679438E);
FF<22>(B, C, D, A, M[15] + 0x49B40821);

GG<5>(A, B, C, D, M[1] + 0xF61E2562);
GG<9>(D, A, B, C, M[6] + 0xC040B340);
GG< 5>(A, B, C, D, M[ 1] + 0xF61E2562);
GG< 9>(D, A, B, C, M[ 6] + 0xC040B340);
GG<14>(C, D, A, B, M[11] + 0x265E5A51);
GG<20>(B, C, D, A, M[0] + 0xE9B6C7AA);
GG<5>(A, B, C, D, M[5] + 0xD62F105D);
GG<9>(D, A, B, C, M[10] + 0x02441453);
GG<20>(B, C, D, A, M[ 0] + 0xE9B6C7AA);
GG< 5>(A, B, C, D, M[ 5] + 0xD62F105D);
GG< 9>(D, A, B, C, M[10] + 0x02441453);
GG<14>(C, D, A, B, M[15] + 0xD8A1E681);
GG<20>(B, C, D, A, M[4] + 0xE7D3FBC8);
GG<5>(A, B, C, D, M[9] + 0x21E1CDE6);
GG<9>(D, A, B, C, M[14] + 0xC33707D6);
GG<14>(C, D, A, B, M[3] + 0xF4D50D87);
GG<20>(B, C, D, A, M[8] + 0x455A14ED);
GG<5>(A, B, C, D, M[13] + 0xA9E3E905);
GG<9>(D, A, B, C, M[2] + 0xFCEFA3F8);
GG<14>(C, D, A, B, M[7] + 0x676F02D9);
GG<20>(B, C, D, A, M[ 4] + 0xE7D3FBC8);
GG< 5>(A, B, C, D, M[ 9] + 0x21E1CDE6);
GG< 9>(D, A, B, C, M[14] + 0xC33707D6);
GG<14>(C, D, A, B, M[ 3] + 0xF4D50D87);
GG<20>(B, C, D, A, M[ 8] + 0x455A14ED);
GG< 5>(A, B, C, D, M[13] + 0xA9E3E905);
GG< 9>(D, A, B, C, M[ 2] + 0xFCEFA3F8);
GG<14>(C, D, A, B, M[ 7] + 0x676F02D9);
GG<20>(B, C, D, A, M[12] + 0x8D2A4C8A);

HH<4>(A, B, C, D, M[5] + 0xFFFA3942);
HH<11>(D, A, B, C, M[8] + 0x8771F681);
HH< 4>(A, B, C, D, M[ 5] + 0xFFFA3942);
HH<11>(D, A, B, C, M[ 8] + 0x8771F681);
HH<16>(C, D, A, B, M[11] + 0x6D9D6122);
HH<23>(B, C, D, A, M[14] + 0xFDE5380C);
HH<4>(A, B, C, D, M[1] + 0xA4BEEA44);
HH<11>(D, A, B, C, M[4] + 0x4BDECFA9);
HH<16>(C, D, A, B, M[7] + 0xF6BB4B60);
HH< 4>(A, B, C, D, M[ 1] + 0xA4BEEA44);
HH<11>(D, A, B, C, M[ 4] + 0x4BDECFA9);
HH<16>(C, D, A, B, M[ 7] + 0xF6BB4B60);
HH<23>(B, C, D, A, M[10] + 0xBEBFBC70);
HH<4>(A, B, C, D, M[13] + 0x289B7EC6);
HH<11>(D, A, B, C, M[0] + 0xEAA127FA);
HH<16>(C, D, A, B, M[3] + 0xD4EF3085);
HH<23>(B, C, D, A, M[6] + 0x04881D05);
HH<4>(A, B, C, D, M[9] + 0xD9D4D039);
HH< 4>(A, B, C, D, M[13] + 0x289B7EC6);
HH<11>(D, A, B, C, M[ 0] + 0xEAA127FA);
HH<16>(C, D, A, B, M[ 3] + 0xD4EF3085);
HH<23>(B, C, D, A, M[ 6] + 0x04881D05);
HH< 4>(A, B, C, D, M[ 9] + 0xD9D4D039);
HH<11>(D, A, B, C, M[12] + 0xE6DB99E5);
HH<16>(C, D, A, B, M[15] + 0x1FA27CF8);
HH<23>(B, C, D, A, M[2] + 0xC4AC5665);
HH<23>(B, C, D, A, M[ 2] + 0xC4AC5665);

II<6>(A, B, C, D, M[0] + 0xF4292244);
II<10>(D, A, B, C, M[7] + 0x432AFF97);
II< 6>(A, B, C, D, M[ 0] + 0xF4292244);
II<10>(D, A, B, C, M[ 7] + 0x432AFF97);
II<15>(C, D, A, B, M[14] + 0xAB9423A7);
II<21>(B, C, D, A, M[5] + 0xFC93A039);
II<6>(A, B, C, D, M[12] + 0x655B59C3);
II<10>(D, A, B, C, M[3] + 0x8F0CCC92);
II<21>(B, C, D, A, M[ 5] + 0xFC93A039);
II< 6>(A, B, C, D, M[12] + 0x655B59C3);
II<10>(D, A, B, C, M[ 3] + 0x8F0CCC92);
II<15>(C, D, A, B, M[10] + 0xFFEFF47D);
II<21>(B, C, D, A, M[1] + 0x85845DD1);
II<6>(A, B, C, D, M[8] + 0x6FA87E4F);
II<21>(B, C, D, A, M[ 1] + 0x85845DD1);
II< 6>(A, B, C, D, M[ 8] + 0x6FA87E4F);
II<10>(D, A, B, C, M[15] + 0xFE2CE6E0);
II<15>(C, D, A, B, M[6] + 0xA3014314);
II<15>(C, D, A, B, M[ 6] + 0xA3014314);
II<21>(B, C, D, A, M[13] + 0x4E0811A1);
II<6>(A, B, C, D, M[4] + 0xF7537E82);
II< 6>(A, B, C, D, M[ 4] + 0xF7537E82);
II<10>(D, A, B, C, M[11] + 0xBD3AF235);
II<15>(C, D, A, B, M[2] + 0x2AD7D2BB);
II<21>(B, C, D, A, M[9] + 0xEB86D391);
II<15>(C, D, A, B, M[ 2] + 0x2AD7D2BB);
II<21>(B, C, D, A, M[ 9] + 0xEB86D391);

// clang-format off

A = (digest[0] += A);
B = (digest[1] += B);
Expand Down
Loading

0 comments on commit bb653e9

Please sign in to comment.