|
| 1 | +; |
| 2 | +; MD5 hash in x64 MASM |
| 3 | +; |
| 4 | +; Copyright (c) 2023 Chong Yeol Nah (MIT License) |
| 5 | +; |
| 6 | +; Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +; this software and associated documentation files (the "Software"), to deal in |
| 8 | +; the Software without restriction, including without limitation the rights to |
| 9 | +; use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | +; the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +; subject to the following conditions: |
| 12 | +; - The above copyright notice and this permission notice shall be included in |
| 13 | +; all copies or substantial portions of the Software. |
| 14 | +; - The Software is provided "as is", without warranty of any kind, express or |
| 15 | +; implied, including but not limited to the warranties of merchantability, |
| 16 | +; fitness for a particular purpose and noninfringement. In no event shall the |
| 17 | +; authors or copyright holders be liable for any claim, damages or other |
| 18 | +; liability, whether in an action of contract, tort or otherwise, arising from, |
| 19 | +; out of or in connection with the Software or the use or other dealings in the |
| 20 | +; Software. |
| 21 | +; |
| 22 | +; |
| 23 | +; Storage usage: |
| 24 | +; Bytes Location Volatile Description |
| 25 | +; 4 eax yes Temporary w-bit word used in the hash computation |
| 26 | +; 8 rcx yes Base address of message block array argument (read-only) |
| 27 | +; 8 rdx yes Base address of hash value array argument (read-only) |
| 28 | +; 4 r8d yes MD5 working variable A |
| 29 | +; 4 r9d yes MD5 working variable B |
| 30 | +; 4 r10d yes MD5 working variable C |
| 31 | +; 4 r11d yes MD5 working variable D |
| 32 | + |
| 33 | + option casemap:none |
| 34 | + |
| 35 | + .const |
| 36 | +ROUND macro i, a, b, c, d, k, s, t |
| 37 | + |
| 38 | +if i LT 16 |
| 39 | + |
| 40 | + ; eax = F(b,c,d) = (b & c) | (!b & d) = d ^ (b & (c ^ d)) |
| 41 | + mov eax, c |
| 42 | + xor eax, d |
| 43 | + and eax, b |
| 44 | + xor eax, d |
| 45 | + |
| 46 | +elseif i LT 32 |
| 47 | + |
| 48 | + ; eax = G(b,c,d) = (b & d) | (c & !d) = c ^ (d & (b ^ c)) |
| 49 | + mov eax, c |
| 50 | + xor eax, b |
| 51 | + and eax, d |
| 52 | + xor eax, c |
| 53 | + |
| 54 | +elseif i LT 48 |
| 55 | + |
| 56 | + ; eax = H(b,c,d) = b ^ c ^ d |
| 57 | + mov eax, c |
| 58 | + xor eax, d |
| 59 | + xor eax, b |
| 60 | + |
| 61 | +else |
| 62 | + |
| 63 | + ; eax = I(b,c,d) = c ^ (b | !d) |
| 64 | + mov eax, d |
| 65 | + not eax |
| 66 | + or eax, b |
| 67 | + xor eax, c |
| 68 | + |
| 69 | +endif |
| 70 | + |
| 71 | + lea a, [eax + a + t] |
| 72 | + add a, [rcx + k*4] |
| 73 | + rol a, s |
| 74 | + add a, b |
| 75 | + endm |
| 76 | + |
| 77 | + .code |
| 78 | + ; void md5_compress(const uint8_t block[64], uint32_t state[4]) |
| 79 | + public md5_compress |
| 80 | +md5_compress proc |
| 81 | + ; Initialize working variables with previous hash value |
| 82 | + mov r8d, [rdx] ; a |
| 83 | + mov r9d, [rdx + 4] ; b |
| 84 | + mov r10d, [rdx + 8] ; c |
| 85 | + mov r11d, [rdx + 12] ; d |
| 86 | + |
| 87 | + ; 64 rounds of hashing |
| 88 | + ROUND 0, r8d, r9d, r10d, r11d, 0, 7, -28955B88h |
| 89 | + ROUND 1, r11d, r8d, r9d, r10d, 1, 12, -173848AAh |
| 90 | + ROUND 2, r10d, r11d, r8d, r9d, 2, 17, 242070DBh |
| 91 | + ROUND 3, r9d, r10d, r11d, r8d, 3, 22, -3E423112h |
| 92 | + ROUND 4, r8d, r9d, r10d, r11d, 4, 7, -0A83F051h |
| 93 | + ROUND 5, r11d, r8d, r9d, r10d, 5, 12, 4787C62Ah |
| 94 | + ROUND 6, r10d, r11d, r8d, r9d, 6, 17, -57CFB9EDh |
| 95 | + ROUND 7, r9d, r10d, r11d, r8d, 7, 22, -02B96AFFh |
| 96 | + ROUND 8, r8d, r9d, r10d, r11d, 8, 7, 698098D8h |
| 97 | + ROUND 9, r11d, r8d, r9d, r10d, 9, 12, -74BB0851h |
| 98 | + ROUND 10, r10d, r11d, r8d, r9d, 10, 17, -0000A44Fh |
| 99 | + ROUND 11, r9d, r10d, r11d, r8d, 11, 22, -76A32842h |
| 100 | + ROUND 12, r8d, r9d, r10d, r11d, 12, 7, 6B901122h |
| 101 | + ROUND 13, r11d, r8d, r9d, r10d, 13, 12, -02678E6Dh |
| 102 | + ROUND 14, r10d, r11d, r8d, r9d, 14, 17, -5986BC72h |
| 103 | + ROUND 15, r9d, r10d, r11d, r8d, 15, 22, 49B40821h |
| 104 | + ROUND 16, r8d, r9d, r10d, r11d, 1, 5, -09E1DA9Eh |
| 105 | + ROUND 17, r11d, r8d, r9d, r10d, 6, 9, -3FBF4CC0h |
| 106 | + ROUND 18, r10d, r11d, r8d, r9d, 11, 14, 265E5A51h |
| 107 | + ROUND 19, r9d, r10d, r11d, r8d, 0, 20, -16493856h |
| 108 | + ROUND 20, r8d, r9d, r10d, r11d, 5, 5, -29D0EFA3h |
| 109 | + ROUND 21, r11d, r8d, r9d, r10d, 10, 9, 02441453h |
| 110 | + ROUND 22, r10d, r11d, r8d, r9d, 15, 14, -275E197Fh |
| 111 | + ROUND 23, r9d, r10d, r11d, r8d, 4, 20, -182C0438h |
| 112 | + ROUND 24, r8d, r9d, r10d, r11d, 9, 5, 21E1CDE6h |
| 113 | + ROUND 25, r11d, r8d, r9d, r10d, 14, 9, -3CC8F82Ah |
| 114 | + ROUND 26, r10d, r11d, r8d, r9d, 3, 14, -0B2AF279h |
| 115 | + ROUND 27, r9d, r10d, r11d, r8d, 8, 20, 455A14EDh |
| 116 | + ROUND 28, r8d, r9d, r10d, r11d, 13, 5, -561C16FBh |
| 117 | + ROUND 29, r11d, r8d, r9d, r10d, 2, 9, -03105C08h |
| 118 | + ROUND 30, r10d, r11d, r8d, r9d, 7, 14, 676F02D9h |
| 119 | + ROUND 31, r9d, r10d, r11d, r8d, 12, 20, -72D5B376h |
| 120 | + ROUND 32, r8d, r9d, r10d, r11d, 5, 4, -0005C6BEh |
| 121 | + ROUND 33, r11d, r8d, r9d, r10d, 8, 11, -788E097Fh |
| 122 | + ROUND 34, r10d, r11d, r8d, r9d, 11, 16, 6D9D6122h |
| 123 | + ROUND 35, r9d, r10d, r11d, r8d, 14, 23, -021AC7F4h |
| 124 | + ROUND 36, r8d, r9d, r10d, r11d, 1, 4, -5B4115BCh |
| 125 | + ROUND 37, r11d, r8d, r9d, r10d, 4, 11, 4BDECFA9h |
| 126 | + ROUND 38, r10d, r11d, r8d, r9d, 7, 16, -0944B4A0h |
| 127 | + ROUND 39, r9d, r10d, r11d, r8d, 10, 23, -41404390h |
| 128 | + ROUND 40, r8d, r9d, r10d, r11d, 13, 4, 289B7EC6h |
| 129 | + ROUND 41, r11d, r8d, r9d, r10d, 0, 11, -155ED806h |
| 130 | + ROUND 42, r10d, r11d, r8d, r9d, 3, 16, -2B10CF7Bh |
| 131 | + ROUND 43, r9d, r10d, r11d, r8d, 6, 23, 04881D05h |
| 132 | + ROUND 44, r8d, r9d, r10d, r11d, 9, 4, -262B2FC7h |
| 133 | + ROUND 45, r11d, r8d, r9d, r10d, 12, 11, -1924661Bh |
| 134 | + ROUND 46, r10d, r11d, r8d, r9d, 15, 16, 1FA27CF8h |
| 135 | + ROUND 47, r9d, r10d, r11d, r8d, 2, 23, -3B53A99Bh |
| 136 | + ROUND 48, r8d, r9d, r10d, r11d, 0, 6, -0BD6DDBCh |
| 137 | + ROUND 49, r11d, r8d, r9d, r10d, 7, 10, 432AFF97h |
| 138 | + ROUND 50, r10d, r11d, r8d, r9d, 14, 15, -546BDC59h |
| 139 | + ROUND 51, r9d, r10d, r11d, r8d, 5, 21, -036C5FC7h |
| 140 | + ROUND 52, r8d, r9d, r10d, r11d, 12, 6, 655B59C3h |
| 141 | + ROUND 53, r11d, r8d, r9d, r10d, 3, 10, -70F3336Eh |
| 142 | + ROUND 54, r10d, r11d, r8d, r9d, 10, 15, -00100B83h |
| 143 | + ROUND 55, r9d, r10d, r11d, r8d, 1, 21, -7A7BA22Fh |
| 144 | + ROUND 56, r8d, r9d, r10d, r11d, 8, 6, 6FA87E4Fh |
| 145 | + ROUND 57, r11d, r8d, r9d, r10d, 15, 10, -01D31920h |
| 146 | + ROUND 58, r10d, r11d, r8d, r9d, 6, 15, -5CFEBCECh |
| 147 | + ROUND 59, r9d, r10d, r11d, r8d, 13, 21, 4E0811A1h |
| 148 | + ROUND 60, r8d, r9d, r10d, r11d, 4, 6, -08AC817Eh |
| 149 | + ROUND 61, r11d, r8d, r9d, r10d, 11, 10, -42C50DCBh |
| 150 | + ROUND 62, r10d, r11d, r8d, r9d, 2, 15, 2AD7D2BBh |
| 151 | + ROUND 63, r9d, r10d, r11d, r8d, 9, 21, -14792C6Fh |
| 152 | + |
| 153 | + ; Compute intermediate hash value |
| 154 | + add [rdx] , r8d |
| 155 | + add [rdx + 4], r9d |
| 156 | + add [rdx + 8], r10d |
| 157 | + add [rdx + 12], r11d |
| 158 | + ret |
| 159 | +md5_compress endp |
| 160 | + end |
0 commit comments