├── readme.txt ├── masm └── rewolf_md5.inc ├── fasm └── rewolf_md5.inc ├── nasm └── rewolf_md5.inc └── gas └── rewolf_md5.s /readme.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | Name....: MD5 Message-Digest Algorithm 3 | Author..: ReWolf 4 | Rel.Date: 18.XII.2004 (VII.2011 update) 5 | 6 | 7 | e.mail..: rewolf@rewolf.pl 8 | www.....: http://rewolf.pl 9 | -------------------------------------------------------------------------------- 10 | 11 | MD5 algorithm implementation. See rewolf_md5.(inc/s) for more details. 12 | 13 | \masm\ implementation for MASM 14 | \fasm\ implementation for FASM (adapted by Reverend) 15 | \gas\ implementation for GNU Assembler (adapted by Hannes Beinert) 16 | \nasm\ implementation for NASM/YASM (adapted by Ange Albertini) 17 | 18 | 19 | Additional info: 20 | 21 | Implementation from \gas\ directory is heavily commented by Hannes Beinert, so 22 | probably it might be useful to study for beginners to figure out what exactly is 23 | happening on each stage of algorithm. 24 | 25 | -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- /masm/rewolf_md5.inc: -------------------------------------------------------------------------------- 1 | comment ~ 2 | ---------------------------------------------------------------------------- 3 | | The MD5 Message-Digest Algorithm | 4 | ---------------------------------------------------------------------------- 5 | | Description: | 6 | | ============ | 7 | | | 8 | | The MD5 algorithm is designed to be quite fast on 32-bit machines. In | 9 | | addition, the MD5 algorithm does not require any large substitution | 10 | | tables, the algorithm can be coded quite compactly. | 11 | | | 12 | | The MD5 algorithm is an extension of the MD4 message-digest algorithm | 13 | | 1,2]. MD5 is slightly slower than MD4, but is more "conservative" in | 14 | | design. MD5 was designed because it was felt that MD4 was perhaps | 15 | | being adopted for use more quickly than justified by the existing | 16 | | critical review, because MD4 was designed to be exceptionally fast, | 17 | | it is "at the edge" in terms of risking successful cryptanalytic | 18 | | attack. MD5 backs off a bit, giving up a little in speed for a much | 19 | | greater likelihood of ultimate security. It incorporates some | 20 | | suggestions made by various reviewers, and contains additional | 21 | | optimizations. The MD5 algorithm is being placed in the public domain | 22 | | for review and possible adoption as a standard. | 23 | | | 24 | ---------------------------------------------------------------------------- 25 | | Implementation based on rfc1321 (fully rewritten in asm, not ripped :))| 26 | ---------------------------------------------------------------------------- 27 | | Usage: | 28 | | ====== | 29 | | | 30 | | Simply include this file to your project: | 31 | | exp: include \..path..\rewolf_md5.inc | 32 | | | 33 | | Target compiler...: MASM | 34 | | Calling convention: | 35 | | | 36 | | push size of datablock | 37 | | push offset datablock | 38 | | push offset destHash | 39 | | call _rwf_md5 | 40 | | | 41 | | datablock -> (input) -> buffer that contains data to hash | 42 | | destHash -> (output) -> 16-bytes buffer for hashed data | 43 | | | 44 | | Modified registers: none | 45 | | Stack is automatically cleared | 46 | ---------------------------------------------------------------------------- 47 | | Coder.: ReWolf^HTB | 48 | | Date..: 17.XII.2004 | 49 | | E-mail: rewolf@poczta.onet.pl | 50 | | WWW...: http://www.rewolf.prv.pl | 51 | ---------------------------------------------------------------------------- 52 | ~ 53 | 54 | S11 = 7 55 | S12 = 12 56 | S13 = 17 57 | S14 = 22 58 | S21 = 5 59 | S22 = 9 60 | S23 = 14 61 | S24 = 20 62 | S31 = 4 63 | S32 = 11 64 | S33 = 16 65 | S34 = 23 66 | S41 = 6 67 | S42 = 10 68 | S43 = 15 69 | S44 = 21 70 | 71 | FF macro a,b,c,d,k,s,i 72 | mov edi,b 73 | mov ebp,b 74 | and edi,c 75 | not ebp 76 | and ebp,d 77 | or edi,ebp 78 | lea a,dword ptr [a+edi+i] 79 | add a,dword ptr [esi+k*4] 80 | rol a,s 81 | add a,b 82 | endm FF 83 | 84 | GG macro a,b,c,d,k,s,i 85 | mov edi,d 86 | mov ebp,d 87 | and edi,b 88 | not ebp 89 | and ebp,c 90 | or edi,ebp 91 | lea a,dword ptr [a+edi+i] 92 | add a,dword ptr [esi+k*4] 93 | rol a,s 94 | add a,b 95 | endm GG 96 | 97 | HH macro a,b,c,d,k,s,i 98 | mov ebp,b 99 | xor ebp,c 100 | xor ebp,d 101 | lea a,dword ptr [a+ebp+i] 102 | add a,dword ptr [esi+k*4] 103 | rol a,s 104 | add a,b 105 | endm HH 106 | 107 | II macro a,b,c,d,k,s,i 108 | mov ebp,d 109 | not ebp 110 | or ebp,b 111 | xor ebp,c 112 | lea a,dword ptr [a+ebp+i] 113 | add a,dword ptr [esi+k*4] 114 | rol a,s 115 | add a,b 116 | endm II 117 | 118 | .code 119 | _rwf_md5 proc 120 | pushad 121 | mov esi,dword ptr [esp+04h+8*4] 122 | mov dword ptr [esi], 067452301h 123 | mov dword ptr [esi+04h], 0efcdab89h 124 | mov dword ptr [esi+08h], 098badcfeh 125 | mov dword ptr [esi+0Ch], 010325476h 126 | mov eax,dword ptr [esp+0Ch+8*4] 127 | push eax 128 | xor edx,edx 129 | mov ecx,64 130 | div ecx 131 | inc eax 132 | pop edx 133 | sub esp,64 134 | mov ebx,esp 135 | mov esi,dword ptr [esp+08h+24*4] 136 | xchg eax,edx 137 | _n0: 138 | mov edi,ebx 139 | dec edx 140 | jne _n1 141 | test eax,eax 142 | js _nD 143 | mov byte ptr [ebx+eax],80h 144 | jmp _nC 145 | _nD: 146 | xor eax,eax 147 | dec eax 148 | _nC: 149 | mov ecx,64 150 | sub ecx,eax 151 | add edi,eax 152 | push eax 153 | xor eax,eax 154 | inc edi 155 | dec ecx 156 | rep stosb 157 | pop eax 158 | test eax,eax 159 | js _nB 160 | cmp eax,56 161 | jnb _nE 162 | _nB: 163 | push eax 164 | mov eax,dword ptr [esp+0Ch+25*4] 165 | push edx 166 | xor edx,edx 167 | mov ecx,8 168 | mul ecx 169 | mov dword ptr [ebx+56],eax 170 | mov dword ptr [ebx+60],edx 171 | pop edx 172 | pop eax 173 | jmp _n1 174 | _nE: 175 | inc edx 176 | _n1: 177 | test eax,eax 178 | js _nA 179 | cmp eax,64 180 | jnb _n2 181 | jmp _n10 182 | _nA: 183 | xor eax,eax 184 | _n10: 185 | mov ecx,eax 186 | jmp _n3 187 | _n2: 188 | mov ecx,64 189 | _n3: 190 | mov edi,ebx 191 | rep movsb 192 | push eax 193 | push edx 194 | push ebx 195 | push esi 196 | lea esi,dword ptr [esp+10h] 197 | mov edi,dword ptr [esp+4+28*4] 198 | push edi 199 | mov eax,dword ptr [edi] 200 | mov ebx,dword ptr [edi+04h] 201 | mov ecx,dword ptr [edi+08h] 202 | mov edx,dword ptr [edi+0Ch] 203 | 204 | FF eax, ebx, ecx, edx, 0, S11, 0d76aa478h 205 | FF edx, eax, ebx, ecx, 1, S12, 0e8c7b756h 206 | FF ecx, edx, eax, ebx, 2, S13, 0242070dbh 207 | FF ebx, ecx, edx, eax, 3, S14, 0c1bdceeeh 208 | FF eax, ebx, ecx, edx, 4, S11, 0f57c0fafh 209 | FF edx, eax, ebx, ecx, 5, S12, 04787c62ah 210 | FF ecx, edx, eax, ebx, 6, S13, 0a8304613h 211 | FF ebx, ecx, edx, eax, 7, S14, 0fd469501h 212 | FF eax, ebx, ecx, edx, 8, S11, 0698098d8h 213 | FF edx, eax, ebx, ecx, 9, S12, 08b44f7afh 214 | FF ecx, edx, eax, ebx, 10, S13, 0ffff5bb1h 215 | FF ebx, ecx, edx, eax, 11, S14, 0895cd7beh 216 | FF eax, ebx, ecx, edx, 12, S11, 06b901122h 217 | FF edx, eax, ebx, ecx, 13, S12, 0fd987193h 218 | FF ecx, edx, eax, ebx, 14, S13, 0a679438eh 219 | FF ebx, ecx, edx, eax, 15, S14, 049b40821h 220 | 221 | GG eax, ebx, ecx, edx, 1, S21, 0f61e2562h 222 | GG edx, eax, ebx, ecx, 6, S22, 0c040b340h 223 | GG ecx, edx, eax, ebx,11, S23, 0265e5a51h 224 | GG ebx, ecx, edx, eax, 0, S24, 0e9b6c7aah 225 | GG eax, ebx, ecx, edx, 5, S21, 0d62f105dh 226 | GG edx, eax, ebx, ecx,10, S22, 002441453h 227 | GG ecx, edx, eax, ebx,15, S23, 0d8a1e681h 228 | GG ebx, ecx, edx, eax, 4, S24, 0e7d3fbc8h 229 | GG eax, ebx, ecx, edx, 9, S21, 021e1cde6h 230 | GG edx, eax, ebx, ecx,14, S22, 0c33707d6h 231 | GG ecx, edx, eax, ebx, 3, S23, 0f4d50d87h 232 | GG ebx, ecx, edx, eax, 8, S24, 0455a14edh 233 | GG eax, ebx, ecx, edx,13, S21, 0a9e3e905h 234 | GG edx, eax, ebx, ecx, 2, S22, 0fcefa3f8h 235 | GG ecx, edx, eax, ebx, 7, S23, 0676f02d9h 236 | GG ebx, ecx, edx, eax,12, S24, 08d2a4c8ah 237 | 238 | HH eax, ebx, ecx, edx, 5, S31, 0fffa3942h 239 | HH edx, eax, ebx, ecx, 8, S32, 08771f681h 240 | HH ecx, edx, eax, ebx,11, S33, 06d9d6122h 241 | HH ebx, ecx, edx, eax,14, S34, 0fde5380ch 242 | HH eax, ebx, ecx, edx, 1, S31, 0a4beea44h 243 | HH edx, eax, ebx, ecx, 4, S32, 04bdecfa9h 244 | HH ecx, edx, eax, ebx, 7, S33, 0f6bb4b60h 245 | HH ebx, ecx, edx, eax,10, S34, 0bebfbc70h 246 | HH eax, ebx, ecx, edx,13, S31, 0289b7ec6h 247 | HH edx, eax, ebx, ecx, 0, S32, 0eaa127fah 248 | HH ecx, edx, eax, ebx, 3, S33, 0d4ef3085h 249 | HH ebx, ecx, edx, eax, 6, S34, 004881d05h 250 | HH eax, ebx, ecx, edx, 9, S31, 0d9d4d039h 251 | HH edx, eax, ebx, ecx,12, S32, 0e6db99e5h 252 | HH ecx, edx, eax, ebx,15, S33, 01fa27cf8h 253 | HH ebx, ecx, edx, eax, 2, S34, 0c4ac5665h 254 | 255 | II eax, ebx, ecx, edx, 0, S41, 0f4292244h 256 | II edx, eax, ebx, ecx, 7, S42, 0432aff97h 257 | II ecx, edx, eax, ebx,14, S43, 0ab9423a7h 258 | II ebx, ecx, edx, eax, 5, S44, 0fc93a039h 259 | II eax, ebx, ecx, edx,12, S41, 0655b59c3h 260 | II edx, eax, ebx, ecx, 3, S42, 08f0ccc92h 261 | II ecx, edx, eax, ebx,10, S43, 0ffeff47dh 262 | II ebx, ecx, edx, eax, 1, S44, 085845dd1h 263 | II eax, ebx, ecx, edx, 8, S41, 06fa87e4fh 264 | II edx, eax, ebx, ecx,15, S42, 0fe2ce6e0h 265 | II ecx, edx, eax, ebx, 6, S43, 0a3014314h 266 | II ebx, ecx, edx, eax,13, S44, 04e0811a1h 267 | II eax, ebx, ecx, edx, 4, S41, 0f7537e82h 268 | II edx, eax, ebx, ecx,11, S42, 0bd3af235h 269 | II ecx, edx, eax, ebx, 2, S43, 02ad7d2bbh 270 | II ebx, ecx, edx, eax, 9, S44, 0eb86d391h 271 | 272 | pop edi 273 | add dword ptr [edi],eax 274 | add dword ptr [edi+04h],ebx 275 | add dword ptr [edi+08h],ecx 276 | add dword ptr [edi+0Ch],edx 277 | pop esi 278 | pop ebx 279 | pop edx 280 | pop eax 281 | sub eax,64 282 | test edx,edx 283 | jne _n0 284 | add esp,64 285 | popad 286 | ret 12 287 | _rwf_md5 endp 288 | -------------------------------------------------------------------------------- /fasm/rewolf_md5.inc: -------------------------------------------------------------------------------- 1 | ;---------------------------------------------------------------------------- 2 | ;| The MD5 Message-Digest Algorithm | 3 | ;---------------------------------------------------------------------------- 4 | ;| Description: | 5 | ;| ============ | 6 | ;| | 7 | ;| The MD5 algorithm is designed to be quite fast on 32-bit machines. In | 8 | ;| addition, the MD5 algorithm does not require any large substitution | 9 | ;| tables, the algorithm can be coded quite compactly. | 10 | ;| | 11 | ;| The MD5 algorithm is an extension of the MD4 message-digest algorithm | 12 | ;| 1,2]. MD5 is slightly slower than MD4, but is more "conservative" in | 13 | ;| design. MD5 was designed because it was felt that MD4 was perhaps | 14 | ;| being adopted for use more quickly than justified by the existing | 15 | ;| critical review, because MD4 was designed to be exceptionally fast, | 16 | ;| it is "at the edge" in terms of risking successful cryptanalytic | 17 | ;| attack. MD5 backs off a bit, giving up a little in speed for a much | 18 | ;| greater likelihood of ultimate security. It incorporates some | 19 | ;| suggestions made by various reviewers, and contains additional | 20 | ;| optimizations. The MD5 algorithm is being placed in the public domain | 21 | ;| for review and possible adoption as a standard. | 22 | ;| | 23 | ;---------------------------------------------------------------------------- 24 | ;| Implementation based on rfc1321 (fully rewritten in asm, not ripped :))| 25 | ;---------------------------------------------------------------------------- 26 | ;| Usage: | 27 | ;| ====== | 28 | ;| | 29 | ;| Simply include this file to your project: | 30 | ;| exp: include \..path..\rewolf_md5.inc | 31 | ;| | 32 | ;| Target compiler...: FASM | 33 | ;| Calling convention: | 34 | ;| | 35 | ;| push size of datablock | 36 | ;| push offset datablock | 37 | ;| push offset destHash | 38 | ;| call _rwf_md5 | 39 | ;| | 40 | ;| datablock -> (input) -> buffer that contains data to hash | 41 | ;| destHash -> (output) -> 16-bytes buffer for hashed data | 42 | ;| | 43 | ;| Modified registers: none | 44 | ;| Stack is automatically cleared | 45 | ;---------------------------------------------------------------------------- 46 | ;| Coder.: ReWolf^HTB | 47 | ;| Date..: 17.XII.2004 | 48 | ;| E-mail: rewolf@poczta.onet.pl | 49 | ;| WWW...: http://www.rewolf.prv.pl | 50 | ;---------------------------------------------------------------------------- 51 | ;| Adaptation for FASM: Reverend^HTB+RAG | 52 | ;---------------------------------------------------------------------------- 53 | 54 | S11 = 7 55 | S12 = 12 56 | S13 = 17 57 | S14 = 22 58 | S21 = 5 59 | S22 = 9 60 | S23 = 14 61 | S24 = 20 62 | S31 = 4 63 | S32 = 11 64 | S33 = 16 65 | S34 = 23 66 | S41 = 6 67 | S42 = 10 68 | S43 = 15 69 | S44 = 21 70 | 71 | macro FF a,b,c,d,k,s,i { 72 | mov edi,b 73 | mov ebp,b 74 | and edi,c 75 | not ebp 76 | and ebp,d 77 | or edi,ebp 78 | lea a,dword [a+edi+i] 79 | add a,dword [esi+k*4] 80 | rol a,s 81 | add a,b 82 | } 83 | 84 | macro GG a,b,c,d,k,s,i { 85 | mov edi,d 86 | mov ebp,d 87 | and edi,b 88 | not ebp 89 | and ebp,c 90 | or edi,ebp 91 | lea a,dword [a+edi+i] 92 | add a,dword [esi+k*4] 93 | rol a,s 94 | add a,b 95 | } 96 | 97 | macro HH a,b,c,d,k,s,i { 98 | mov ebp,b 99 | xor ebp,c 100 | xor ebp,d 101 | lea a,dword [a+ebp+i] 102 | add a,dword [esi+k*4] 103 | rol a,s 104 | add a,b 105 | } 106 | 107 | macro II a,b,c,d,k,s,i { 108 | mov ebp,d 109 | not ebp 110 | or ebp,b 111 | xor ebp,c 112 | lea a,dword [a+ebp+i] 113 | add a,dword [esi+k*4] 114 | rol a,s 115 | add a,b 116 | } 117 | 118 | proc _rwf_md5 119 | pushad 120 | mov esi,dword [esp+04h+8*4] 121 | mov dword [esi], 067452301h 122 | mov dword [esi+04h], 0efcdab89h 123 | mov dword [esi+08h], 098badcfeh 124 | mov dword [esi+0Ch], 010325476h 125 | mov eax,dword [esp+0Ch+8*4] 126 | push eax 127 | xor edx,edx 128 | mov ecx,64 129 | div ecx 130 | inc eax 131 | pop edx 132 | sub esp,64 133 | mov ebx,esp 134 | mov esi,dword [esp+08h+24*4] 135 | xchg eax,edx 136 | _n0: 137 | mov edi,ebx 138 | dec edx 139 | jne _n1 140 | test eax,eax 141 | js _nD 142 | mov byte [ebx+eax],80h 143 | jmp _nC 144 | _nD: 145 | xor eax,eax 146 | dec eax 147 | _nC: 148 | mov ecx,64 149 | sub ecx,eax 150 | add edi,eax 151 | push eax 152 | xor eax,eax 153 | inc edi 154 | dec ecx 155 | rep stosb 156 | pop eax 157 | test eax,eax 158 | js _nB 159 | cmp eax,56 160 | jnb _nE 161 | _nB: 162 | push eax 163 | mov eax,dword [esp+0Ch+25*4] 164 | push edx 165 | xor edx,edx 166 | mov ecx,8 167 | mul ecx 168 | mov dword [ebx+56],eax 169 | mov dword [ebx+60],edx 170 | pop edx 171 | pop eax 172 | jmp _n1 173 | _nE: 174 | inc edx 175 | _n1: 176 | test eax,eax 177 | js _nA 178 | cmp eax,64 179 | jnb _n2 180 | jmp _n10 181 | _nA: 182 | xor eax,eax 183 | _n10: 184 | mov ecx,eax 185 | jmp _n3 186 | _n2: 187 | mov ecx,64 188 | _n3: 189 | mov edi,ebx 190 | rep movsb 191 | push eax 192 | push edx 193 | push ebx 194 | push esi 195 | lea esi,dword [esp+10h] 196 | mov edi,dword [esp+4+28*4] 197 | push edi 198 | mov eax,dword [edi] 199 | mov ebx,dword [edi+04h] 200 | mov ecx,dword [edi+08h] 201 | mov edx,dword [edi+0Ch] 202 | 203 | FF eax, ebx, ecx, edx, 0, S11, 0d76aa478h 204 | FF edx, eax, ebx, ecx, 1, S12, 0e8c7b756h 205 | FF ecx, edx, eax, ebx, 2, S13, 0242070dbh 206 | FF ebx, ecx, edx, eax, 3, S14, 0c1bdceeeh 207 | FF eax, ebx, ecx, edx, 4, S11, 0f57c0fafh 208 | FF edx, eax, ebx, ecx, 5, S12, 04787c62ah 209 | FF ecx, edx, eax, ebx, 6, S13, 0a8304613h 210 | FF ebx, ecx, edx, eax, 7, S14, 0fd469501h 211 | FF eax, ebx, ecx, edx, 8, S11, 0698098d8h 212 | FF edx, eax, ebx, ecx, 9, S12, 08b44f7afh 213 | FF ecx, edx, eax, ebx, 10, S13, 0ffff5bb1h 214 | FF ebx, ecx, edx, eax, 11, S14, 0895cd7beh 215 | FF eax, ebx, ecx, edx, 12, S11, 06b901122h 216 | FF edx, eax, ebx, ecx, 13, S12, 0fd987193h 217 | FF ecx, edx, eax, ebx, 14, S13, 0a679438eh 218 | FF ebx, ecx, edx, eax, 15, S14, 049b40821h 219 | 220 | GG eax, ebx, ecx, edx, 1, S21, 0f61e2562h 221 | GG edx, eax, ebx, ecx, 6, S22, 0c040b340h 222 | GG ecx, edx, eax, ebx,11, S23, 0265e5a51h 223 | GG ebx, ecx, edx, eax, 0, S24, 0e9b6c7aah 224 | GG eax, ebx, ecx, edx, 5, S21, 0d62f105dh 225 | GG edx, eax, ebx, ecx,10, S22, 002441453h 226 | GG ecx, edx, eax, ebx,15, S23, 0d8a1e681h 227 | GG ebx, ecx, edx, eax, 4, S24, 0e7d3fbc8h 228 | GG eax, ebx, ecx, edx, 9, S21, 021e1cde6h 229 | GG edx, eax, ebx, ecx,14, S22, 0c33707d6h 230 | GG ecx, edx, eax, ebx, 3, S23, 0f4d50d87h 231 | GG ebx, ecx, edx, eax, 8, S24, 0455a14edh 232 | GG eax, ebx, ecx, edx,13, S21, 0a9e3e905h 233 | GG edx, eax, ebx, ecx, 2, S22, 0fcefa3f8h 234 | GG ecx, edx, eax, ebx, 7, S23, 0676f02d9h 235 | GG ebx, ecx, edx, eax,12, S24, 08d2a4c8ah 236 | 237 | HH eax, ebx, ecx, edx, 5, S31, 0fffa3942h 238 | HH edx, eax, ebx, ecx, 8, S32, 08771f681h 239 | HH ecx, edx, eax, ebx,11, S33, 06d9d6122h 240 | HH ebx, ecx, edx, eax,14, S34, 0fde5380ch 241 | HH eax, ebx, ecx, edx, 1, S31, 0a4beea44h 242 | HH edx, eax, ebx, ecx, 4, S32, 04bdecfa9h 243 | HH ecx, edx, eax, ebx, 7, S33, 0f6bb4b60h 244 | HH ebx, ecx, edx, eax,10, S34, 0bebfbc70h 245 | HH eax, ebx, ecx, edx,13, S31, 0289b7ec6h 246 | HH edx, eax, ebx, ecx, 0, S32, 0eaa127fah 247 | HH ecx, edx, eax, ebx, 3, S33, 0d4ef3085h 248 | HH ebx, ecx, edx, eax, 6, S34, 004881d05h 249 | HH eax, ebx, ecx, edx, 9, S31, 0d9d4d039h 250 | HH edx, eax, ebx, ecx,12, S32, 0e6db99e5h 251 | HH ecx, edx, eax, ebx,15, S33, 01fa27cf8h 252 | HH ebx, ecx, edx, eax, 2, S34, 0c4ac5665h 253 | 254 | II eax, ebx, ecx, edx, 0, S41, 0f4292244h 255 | II edx, eax, ebx, ecx, 7, S42, 0432aff97h 256 | II ecx, edx, eax, ebx,14, S43, 0ab9423a7h 257 | II ebx, ecx, edx, eax, 5, S44, 0fc93a039h 258 | II eax, ebx, ecx, edx,12, S41, 0655b59c3h 259 | II edx, eax, ebx, ecx, 3, S42, 08f0ccc92h 260 | II ecx, edx, eax, ebx,10, S43, 0ffeff47dh 261 | II ebx, ecx, edx, eax, 1, S44, 085845dd1h 262 | II eax, ebx, ecx, edx, 8, S41, 06fa87e4fh 263 | II edx, eax, ebx, ecx,15, S42, 0fe2ce6e0h 264 | II ecx, edx, eax, ebx, 6, S43, 0a3014314h 265 | II ebx, ecx, edx, eax,13, S44, 04e0811a1h 266 | II eax, ebx, ecx, edx, 4, S41, 0f7537e82h 267 | II edx, eax, ebx, ecx,11, S42, 0bd3af235h 268 | II ecx, edx, eax, ebx, 2, S43, 02ad7d2bbh 269 | II ebx, ecx, edx, eax, 9, S44, 0eb86d391h 270 | 271 | pop edi 272 | add dword [edi],eax 273 | add dword [edi+04h],ebx 274 | add dword [edi+08h],ecx 275 | add dword [edi+0Ch],edx 276 | pop esi 277 | pop ebx 278 | pop edx 279 | pop eax 280 | sub eax,64 281 | test edx,edx 282 | jne _n0 283 | add esp,64 284 | popad 285 | ret 0Ch 286 | endp 287 | -------------------------------------------------------------------------------- /nasm/rewolf_md5.inc: -------------------------------------------------------------------------------- 1 | ;---------------------------------------------------------------------------- 2 | ;| The MD5 Message-Digest Algorithm | 3 | ;---------------------------------------------------------------------------- 4 | ;| Description: | 5 | ;| ============ | 6 | ;| | 7 | ;| The MD5 algorithm is designed to be quite fast on 32-bit machines. In | 8 | ;| addition, the MD5 algorithm does not require any large substitution | 9 | ;| tables, the algorithm can be coded quite compactly. | 10 | ;| | 11 | ;| The MD5 algorithm is an extension of the MD4 message-digest algorithm | 12 | ;| 1,2]. MD5 is slightly slower than MD4, but is more "conservative" in | 13 | ;| design. MD5 was designed because it was felt that MD4 was perhaps | 14 | ;| being adopted for use more quickly than justified by the existing | 15 | ;| critical review, because MD4 was designed to be exceptionally fast, | 16 | ;| it is "at the edge" in terms of risking successful cryptanalytic | 17 | ;| attack. MD5 backs off a bit, giving up a little in speed for a much | 18 | ;| greater likelihood of ultimate security. It incorporates some | 19 | ;| suggestions made by various reviewers, and contains additional | 20 | ;| optimizations. The MD5 algorithm is being placed in the public domain | 21 | ;| for review and possible adoption as a standard. | 22 | ;| | 23 | ;---------------------------------------------------------------------------- 24 | ;| Implementation based on rfc1321 (fully rewritten in asm, not ripped :))| 25 | ;---------------------------------------------------------------------------- 26 | ;| Usage: | 27 | ;| ====== | 28 | ;| | 29 | ;| Simply include this file to your project: | 30 | ;| exp: include \..path..\rewolf_md5.inc | 31 | ;| | 32 | ;| Target compiler...: NASM-YASM | 33 | ;| Calling convention: | 34 | ;| | 35 | ;| push size of datablock | 36 | ;| push datablock | 37 | ;| push destHash | 38 | ;| call _rwf_md5 | 39 | ;| | 40 | ;| datablock -> (input) -> buffer that contains data to hash | 41 | ;| destHash -> (output) -> 16-bytes buffer for hashed data | 42 | ;| | 43 | ;| Modified registers: none | 44 | ;| Stack is automatically cleared | 45 | ;---------------------------------------------------------------------------- 46 | ;| Coder.: ReWolf^HTB | 47 | ;| Date..: 17.XII.2004 | 48 | ;| E-mail: rewolf@poczta.onet.pl | 49 | ;| WWW...: http://www.rewolf.prv.pl | 50 | ;---------------------------------------------------------------------------- 51 | ;| Adaptation for NASM/YASM: Ange Albertini | 52 | ;---------------------------------------------------------------------------- 53 | 54 | S11 equ 7 55 | S12 equ 12 56 | S13 equ 17 57 | S14 equ 22 58 | S21 equ 5 59 | S22 equ 9 60 | S23 equ 14 61 | S24 equ 20 62 | S31 equ 4 63 | S32 equ 11 64 | S33 equ 16 65 | S34 equ 23 66 | S41 equ 6 67 | S42 equ 10 68 | S43 equ 15 69 | S44 equ 21 70 | 71 | %macro FF 7 ;a,b,c,d,k,s,i 72 | mov edi,%2 73 | mov ebp,%2 74 | and edi,%3 75 | not ebp 76 | and ebp,%4 77 | or edi,ebp 78 | lea %1,dword [%1+edi+%7] 79 | add %1,dword [esi+%5*4] 80 | rol %1,%6 81 | add %1,%2 82 | %endmacro 83 | 84 | %macro GG 7 85 | mov edi,%4 86 | mov ebp,%4 87 | and edi,%2 88 | not ebp 89 | and ebp,%3 90 | or edi,ebp 91 | lea %1,dword [%1+edi+%7] 92 | add %1,dword [esi+%5*4] 93 | rol %1,%6 94 | add %1,%2 95 | %endmacro 96 | 97 | %macro HH 7 98 | mov ebp,%2 99 | xor ebp,%3 100 | xor ebp,%4 101 | lea %1,dword [%1+ebp+%7] 102 | add %1,dword [esi+%5*4] 103 | rol %1,%6 104 | add %1,%2 105 | %endmacro 106 | 107 | %macro II 7 108 | mov ebp,%4 109 | not ebp 110 | or ebp,%2 111 | xor ebp,%3 112 | lea %1,dword [%1+ebp+%7] 113 | add %1,dword [esi+%5*4] 114 | rol %1,%6 115 | add %1,%2 116 | %endmacro 117 | 118 | _rwf_md5: 119 | pushad 120 | mov esi,dword [esp+04h+8*4] 121 | mov dword [esi], 067452301h 122 | mov dword [esi+04h], 0efcdab89h 123 | mov dword [esi+08h], 098badcfeh 124 | mov dword [esi+0Ch], 010325476h 125 | mov eax,dword [esp+0Ch+8*4] 126 | push eax 127 | xor edx,edx 128 | mov ecx,64 129 | div ecx 130 | inc eax 131 | pop edx 132 | sub esp,64 133 | mov ebx,esp 134 | mov esi,dword [esp+08h+24*4] 135 | xchg eax,edx 136 | _n0: 137 | mov edi,ebx 138 | dec edx 139 | jne _n1 140 | test eax,eax 141 | js _nD 142 | mov byte [ebx+eax],80h 143 | jmp _nC 144 | _nD: 145 | xor eax,eax 146 | dec eax 147 | _nC: 148 | mov ecx,64 149 | sub ecx,eax 150 | add edi,eax 151 | push eax 152 | xor eax,eax 153 | inc edi 154 | dec ecx 155 | rep stosb 156 | pop eax 157 | test eax,eax 158 | js _nB 159 | cmp eax,56 160 | jnb _nE 161 | _nB: 162 | push eax 163 | mov eax,dword [esp+0Ch+25*4] 164 | push edx 165 | xor edx,edx 166 | mov ecx,8 167 | mul ecx 168 | mov dword [ebx+56],eax 169 | mov dword [ebx+60],edx 170 | pop edx 171 | pop eax 172 | jmp _n1 173 | _nE: 174 | inc edx 175 | _n1: 176 | test eax,eax 177 | js _nA 178 | cmp eax,64 179 | jnb _n2 180 | jmp _n10 181 | _nA: 182 | xor eax,eax 183 | _n10: 184 | mov ecx,eax 185 | jmp _n3 186 | _n2: 187 | mov ecx,64 188 | _n3: 189 | mov edi,ebx 190 | rep movsb 191 | push eax 192 | push edx 193 | push ebx 194 | push esi 195 | lea esi,dword [esp+10h] 196 | mov edi,dword [esp+4+28*4] 197 | push edi 198 | mov eax,dword [edi] 199 | mov ebx,dword [edi+04h] 200 | mov ecx,dword [edi+08h] 201 | mov edx,dword [edi+0Ch] 202 | 203 | FF eax, ebx, ecx, edx, 0, S11, 0d76aa478h 204 | FF edx, eax, ebx, ecx, 1, S12, 0e8c7b756h 205 | FF ecx, edx, eax, ebx, 2, S13, 0242070dbh 206 | FF ebx, ecx, edx, eax, 3, S14, 0c1bdceeeh 207 | FF eax, ebx, ecx, edx, 4, S11, 0f57c0fafh 208 | FF edx, eax, ebx, ecx, 5, S12, 04787c62ah 209 | FF ecx, edx, eax, ebx, 6, S13, 0a8304613h 210 | FF ebx, ecx, edx, eax, 7, S14, 0fd469501h 211 | FF eax, ebx, ecx, edx, 8, S11, 0698098d8h 212 | FF edx, eax, ebx, ecx, 9, S12, 08b44f7afh 213 | FF ecx, edx, eax, ebx, 10, S13, 0ffff5bb1h 214 | FF ebx, ecx, edx, eax, 11, S14, 0895cd7beh 215 | FF eax, ebx, ecx, edx, 12, S11, 06b901122h 216 | FF edx, eax, ebx, ecx, 13, S12, 0fd987193h 217 | FF ecx, edx, eax, ebx, 14, S13, 0a679438eh 218 | FF ebx, ecx, edx, eax, 15, S14, 049b40821h 219 | 220 | GG eax, ebx, ecx, edx, 1, S21, 0f61e2562h 221 | GG edx, eax, ebx, ecx, 6, S22, 0c040b340h 222 | GG ecx, edx, eax, ebx,11, S23, 0265e5a51h 223 | GG ebx, ecx, edx, eax, 0, S24, 0e9b6c7aah 224 | GG eax, ebx, ecx, edx, 5, S21, 0d62f105dh 225 | GG edx, eax, ebx, ecx,10, S22, 002441453h 226 | GG ecx, edx, eax, ebx,15, S23, 0d8a1e681h 227 | GG ebx, ecx, edx, eax, 4, S24, 0e7d3fbc8h 228 | GG eax, ebx, ecx, edx, 9, S21, 021e1cde6h 229 | GG edx, eax, ebx, ecx,14, S22, 0c33707d6h 230 | GG ecx, edx, eax, ebx, 3, S23, 0f4d50d87h 231 | GG ebx, ecx, edx, eax, 8, S24, 0455a14edh 232 | GG eax, ebx, ecx, edx,13, S21, 0a9e3e905h 233 | GG edx, eax, ebx, ecx, 2, S22, 0fcefa3f8h 234 | GG ecx, edx, eax, ebx, 7, S23, 0676f02d9h 235 | GG ebx, ecx, edx, eax,12, S24, 08d2a4c8ah 236 | 237 | HH eax, ebx, ecx, edx, 5, S31, 0fffa3942h 238 | HH edx, eax, ebx, ecx, 8, S32, 08771f681h 239 | HH ecx, edx, eax, ebx,11, S33, 06d9d6122h 240 | HH ebx, ecx, edx, eax,14, S34, 0fde5380ch 241 | HH eax, ebx, ecx, edx, 1, S31, 0a4beea44h 242 | HH edx, eax, ebx, ecx, 4, S32, 04bdecfa9h 243 | HH ecx, edx, eax, ebx, 7, S33, 0f6bb4b60h 244 | HH ebx, ecx, edx, eax,10, S34, 0bebfbc70h 245 | HH eax, ebx, ecx, edx,13, S31, 0289b7ec6h 246 | HH edx, eax, ebx, ecx, 0, S32, 0eaa127fah 247 | HH ecx, edx, eax, ebx, 3, S33, 0d4ef3085h 248 | HH ebx, ecx, edx, eax, 6, S34, 004881d05h 249 | HH eax, ebx, ecx, edx, 9, S31, 0d9d4d039h 250 | HH edx, eax, ebx, ecx,12, S32, 0e6db99e5h 251 | HH ecx, edx, eax, ebx,15, S33, 01fa27cf8h 252 | HH ebx, ecx, edx, eax, 2, S34, 0c4ac5665h 253 | 254 | II eax, ebx, ecx, edx, 0, S41, 0f4292244h 255 | II edx, eax, ebx, ecx, 7, S42, 0432aff97h 256 | II ecx, edx, eax, ebx,14, S43, 0ab9423a7h 257 | II ebx, ecx, edx, eax, 5, S44, 0fc93a039h 258 | II eax, ebx, ecx, edx,12, S41, 0655b59c3h 259 | II edx, eax, ebx, ecx, 3, S42, 08f0ccc92h 260 | II ecx, edx, eax, ebx,10, S43, 0ffeff47dh 261 | II ebx, ecx, edx, eax, 1, S44, 085845dd1h 262 | II eax, ebx, ecx, edx, 8, S41, 06fa87e4fh 263 | II edx, eax, ebx, ecx,15, S42, 0fe2ce6e0h 264 | II ecx, edx, eax, ebx, 6, S43, 0a3014314h 265 | II ebx, ecx, edx, eax,13, S44, 04e0811a1h 266 | II eax, ebx, ecx, edx, 4, S41, 0f7537e82h 267 | II edx, eax, ebx, ecx,11, S42, 0bd3af235h 268 | II ecx, edx, eax, ebx, 2, S43, 02ad7d2bbh 269 | II ebx, ecx, edx, eax, 9, S44, 0eb86d391h 270 | 271 | pop edi 272 | add dword [edi],eax 273 | add dword [edi+04h],ebx 274 | add dword [edi+08h],ecx 275 | add dword [edi+0Ch],edx 276 | pop esi 277 | pop ebx 278 | pop edx 279 | pop eax 280 | sub eax,64 281 | test edx,edx 282 | jne _n0 283 | add esp,64 284 | popad 285 | ret 0Ch 286 | -------------------------------------------------------------------------------- /gas/rewolf_md5.s: -------------------------------------------------------------------------------- 1 | # 2 | #--------------------------------------------------------------------------- 3 | # The MD5 Message-Digest Algorithm | 4 | #--------------------------------------------------------------------------- 5 | # Description: | 6 | # ============ | 7 | # | 8 | # The MD5 algorithm is designed to be quite fast on 32-bit machines. In | 9 | # addition, the MD5 algorithm does not require any large substitution | 10 | # tables, the algorithm can be coded quite compactly. | 11 | # | 12 | # The MD5 algorithm is an extension of the MD4 message-digest algorithm | 13 | # 1,2]. MD5 is slightly slower than MD4, but is more "conservative" in | 14 | # design. MD5 was designed because it was felt that MD4 was perhaps | 15 | # being adopted for use more quickly than justified by the existing | 16 | # critical review, because MD4 was designed to be exceptionally fast, | 17 | # it is "at the edge" in terms of risking successful cryptanalytic | 18 | # attack. MD5 backs off a bit, giving up a little in speed for a much | 19 | # greater likelihood of ultimate security. It incorporates some | 20 | # suggestions made by various reviewers, and contains additional | 21 | # optimizations. The MD5 algorithm is being placed in the public domain | 22 | # for review and possible adoption as a standard. | 23 | # | 24 | #--------------------------------------------------------------------------- 25 | # Implementation based on rfc1321 (fully rewritten in asm, not ripped :))| 26 | #--------------------------------------------------------------------------- 27 | # Usage: | 28 | # ====== | 29 | # | 30 | # Simply include this file to your project: | 31 | # exp: include \..path..\rewolf_md5.s | 32 | # | 33 | # Target compiler...: GNU ASM | 34 | # Calling convention: | 35 | # | 36 | # push size of datablock | 37 | # push offset datablock | 38 | # push offset destHash | 39 | # call _rwf_md5 | 40 | # | 41 | # datablock -> (input) -> buffer that contains data to hash | 42 | # destHash -> (output) -> 16-bytes buffer for hashed data | 43 | # | 44 | # Modified registers: none | 45 | # Stack is automatically cleared | 46 | #--------------------------------------------------------------------------- 47 | # Coder.: ReWolf^HTB | 48 | # Date..: 17.XII.2004 | 49 | # E-mail: rewolf@poczta.onet.pl | 50 | # WWW...: http://www.rewolf.prv.pl | 51 | #--------------------------------------------------------------------------- 52 | # Adaptation for GNU Assembler: Hannes Beinert (21-Jun-11) | 53 | #--------------------------------------------------------------------------- 54 | 55 | S11 = 7 56 | S12 = 12 57 | S13 = 17 58 | S14 = 22 59 | S21 = 5 60 | S22 = 9 61 | S23 = 14 62 | S24 = 20 63 | S31 = 4 64 | S32 = 11 65 | S33 = 16 66 | S34 = 23 67 | S41 = 6 68 | S42 = 10 69 | S43 = 15 70 | S44 = 21 71 | 72 | # 73 | # Define macros to implement auxiliary functions as 74 | # described in RFC1321 (cf 3.4) 75 | # 76 | 77 | .macro FF a,b,c,d,k,s,i 78 | mov \b, %edi 79 | mov \b, %ebp 80 | and \c, %edi 81 | not %ebp 82 | and \d, %ebp 83 | or %ebp, %edi 84 | leal \i(\a, %edi,), \a 85 | addl \k*4(%esi), \a 86 | rol $\s, \a 87 | add \b, \a 88 | .endm 89 | 90 | .macro GG a,b,c,d,k,s,i 91 | mov \d, %edi 92 | mov \d, %ebp 93 | and \b, %edi 94 | not %ebp 95 | and \c, %ebp 96 | or %ebp, %edi 97 | leal \i(\a, %edi,), \a 98 | addl \k*4(%esi), \a 99 | rol $\s, \a 100 | add \b, \a 101 | .endm 102 | 103 | .macro HH a,b,c,d,k,s,i 104 | mov \b, %ebp 105 | xor \c, %ebp 106 | xor \d, %ebp 107 | leal \i(\a, %ebp,), \a 108 | addl \k*4(%esi), \a 109 | rol $\s, \a 110 | add \b, \a 111 | .endm 112 | 113 | .macro II a,b,c,d,k,s,i 114 | mov \d, %ebp 115 | not %ebp 116 | or \b, %ebp 117 | xor \c, %ebp 118 | leal \i(\a, %ebp,), \a 119 | addl \k*4(%esi), \a 120 | rol $\s, \a 121 | add \b, \a 122 | .endm 123 | 124 | # 125 | # md5 Main Entry Point 126 | # -------------------- 127 | # 128 | # Calling convention: 129 | # +----------------------------+ 130 | # esp+16: + Length(Input message) | 131 | # +----------------------------+ 132 | # esp+08 + Address(Input message) | 133 | # +----------------------------+ 134 | # esp+04: + Address(MD5 buffer) | 135 | # +----------------------------+ 136 | # esp: + Return address | 137 | # +----------------------------+ 138 | # 139 | # NB: Some comments refer to steps in the MD5 algorithm detailed in RFC1321 to 140 | # help annotate the goings-on. 141 | # 142 | 143 | ARG_MDBADR = 0x04 # Stack offset to A(MD buffer) 144 | ARG_MSGADR = 0x08 # Stack offset to A(Input message) 145 | ARG_MSGLEN = 0x0C # Stack offset to L(Input message) 146 | 147 | .text 148 | .global _rwf_md5 149 | 150 | _rwf_md5: 151 | pushal 152 | 153 | movl ARG_MDBADR + 8*4(%esp), %esi # esi = arg A(MD buffer) 154 | movl $0x067452301, (%esi) # Initialize MD buffer (cf 3.3) 155 | movl $0x0EFCDAB89, 0x04(%esi) # Magic numbers from RFC 156 | movl $0x098BADCFE, 0x08(%esi) 157 | movl $0x010325476, 0x0C(%esi) 158 | 159 | # 160 | # Take each 512-bit chunk of the input 161 | # buffer and process it into the digest. 162 | # When we get to the last chunk, we will 163 | # append the padding bits & input length 164 | # to obtain another complete 512b chunk. 165 | # 166 | # NB: If the input data is a precise 167 | # multiple of 512b, then the last chunk 168 | # will consist only of padding & length. 169 | # 170 | 171 | movl ARG_MSGLEN + 8*4(%esp), %eax # eax = arg L(input buffer) 172 | push %eax 173 | xor %edx, %edx # Calculate input length in 512-bit chunks + 1 174 | movl $64, %ecx 175 | div %ecx 176 | inc %eax # eax = Number of chunks + 1 177 | pop %edx 178 | subl $64, %esp # Reserve chunk buffer on stack (16*4B = 512b) 179 | mov %esp, %ebx # ebx = A(chunk buffer) 180 | movl ARG_MSGADR+(8+16)*4(%esp), %esi # esi = arg A(input buffer) 181 | xchg %edx, %eax # eax = L(input); edx = # 512b chunks + 1 182 | 183 | # 184 | # Start a new message chunk 185 | # 186 | # There are four cases we must handle: 187 | # 188 | # Message Padding Length 189 | # ------- ------- ------ 190 | # 1. Full: 64 0 0 191 | # 2. Partial: 56-63 8-1 0 192 | # 3. Partial: 1-55 55-1 8 193 | # 4. Empty: 0 56 8 194 | # 195 | # NB: The first padding byte is required & special (0x80), while 196 | # the remaining padding bytes are zero and optional. Hence, the 197 | # last chunk must have enough space for at least 1 padding byte, 198 | # plus the 8-byte message length. 199 | # 200 | # In the following code, the following register assignments are 201 | # generally maintained: 202 | # 203 | # eax = Remaining number of bytes in message 204 | # ebx = A(chunk buffer on stack) 205 | # edx = Number of remaining message chunks 206 | # esi = A(current position in input message buffer) 207 | # 208 | _n0: 209 | mov %ebx, %edi # edi = A(chunk buffer) 210 | dec %edx # Any more message chunks to process? 211 | jne _n1 # Jump if we're not done 212 | test %eax, %eax # Last or second to last chunk. Have we added pad? 213 | js _nD # Jump if we've already added 1st pad byte 214 | movb $0x80, (%ebx,%eax,) # Append initial padding after data (cf 3.1) 215 | jmp _nC # Go process partial chunk 216 | 217 | # 218 | # We are dealing with either the last, or the second to 219 | # last chunk. In other words, either a partial chunk, 220 | # or an empty chunk. Upon entry to this section: 221 | # 222 | # If... Then... 223 | # ------------- -------------------------------------- 224 | # eax < 0 Empty block, 1st pad was in last chunk 225 | # eax = 0 Empty block, 1st pad in *this* chunk 226 | # 1 <= eax <=63 Partial block, 1st pad in this chunk 227 | # 228 | _nD: 229 | xor %eax, %eax # We've already added the pad byte 230 | dec %eax # Jigger eax so we clear the entire chunk 231 | _nC: 232 | mov $64, %ecx # Full message chunk is 64-bytes, or 512-bits 233 | sub %eax, %ecx # ecx = L(padding bytes in chunk) 234 | add %eax, %edi # edi = A(first padding byte) 235 | push %eax 236 | xor %eax, %eax 237 | inc %edi # edi = A(second padding byte) 238 | dec %ecx # ecx = L(padding bytes to be cleared) 239 | rep stosb # Clear padding bytes (fill ecx bytes @edi with al) 240 | pop %eax # eax = L(message bytes remaining) 241 | test %eax, %eax # Was the 1st padding byte appended in last chunk? 242 | js _nB # Jump if yes, and go append message length 243 | cmp $56, %eax # Enough space to append length to partial chunk? 244 | jnb _nE # Jump if not enough. We need another chunk. 245 | 246 | # 247 | # Append message length into current 248 | # message chunk, as per RFC... 249 | # 250 | _nB: 251 | push %eax 252 | movl ARG_MSGLEN+(8+16+1)*4(%esp), %eax # eax = arg L(Input message) 253 | push %edx 254 | xor %edx, %edx 255 | movl $8, %ecx 256 | mul %ecx # edx:eax = Total number of bits in message 257 | mov %eax, 56(%ebx) # Append message length to message (cf 3.2) 258 | mov %edx, 60(%ebx) # Low order, then high order 259 | pop %edx # Restore edx = remaining message chunks 260 | pop %eax # Restore eax = remaining message bytes 261 | jmp _n1 # Now fill in message data into this chunk 262 | 263 | # 264 | # Complete the current message chunk by 265 | # copying in message data from the current 266 | # position in the message buffer. 267 | # 268 | _nE: 269 | inc %edx # We need another empty chunk for message length 270 | _n1: 271 | test %eax, %eax # Do we have more message data? 272 | js _nA # Jump if no more data 273 | cmp $64, %eax # Do we have at least 512-bits of data? 274 | jnb _n2 # Jump if we have a full chunk 275 | jmp _n10 # This is will be a partial chunk 276 | _nA: 277 | xor %eax, %eax # eax = 0; Remaining message bytes 278 | _n10: 279 | mov %eax, %ecx # ecx = L(message bytes to process) 280 | jmp _n3 281 | _n2: 282 | mov $64, %ecx # ecx = 64 bytes; Process a full 512b chunk 283 | _n3: 284 | mov %ebx, %edi # edi = A(chunk buffer) 285 | rep movsb # Copy message to chunk buffer (ecx bytes from esi -> edi) 286 | push %eax 287 | push %edx 288 | push %ebx # ??? Isn't ebx == A(chunk buffer)? Why leal needed? 289 | push %esi 290 | leal 0x10(%esp), %esi # esi = A(chunk buffer); Recover values after rep 291 | movl ARG_MDBADR+(4+16+8)*4(%esp), %edi # edi = A(MD buffer) 292 | push %edi 293 | 294 | # 295 | # Process message chunk by performing 296 | # four rounds of state transformations. 297 | # (cf 3.4) 298 | # 299 | 300 | movl (%edi), %eax # Load current MD buffer into registers 301 | movl 0x04(%edi), %ebx 302 | movl 0x08(%edi), %ecx 303 | movl 0x0C(%edi), %edx 304 | 305 | FF %eax, %ebx, %ecx, %edx, 0, S11, 0x0d76aa478 # Round 1 306 | FF %edx, %eax, %ebx, %ecx, 1, S12, 0x0e8c7b756 307 | FF %ecx, %edx, %eax, %ebx, 2, S13, 0x0242070db 308 | FF %ebx, %ecx, %edx, %eax, 3, S14, 0x0c1bdceee 309 | FF %eax, %ebx, %ecx, %edx, 4, S11, 0x0f57c0faf 310 | FF %edx, %eax, %ebx, %ecx, 5, S12, 0x04787c62a 311 | FF %ecx, %edx, %eax, %ebx, 6, S13, 0x0a8304613 312 | FF %ebx, %ecx, %edx, %eax, 7, S14, 0x0fd469501 313 | FF %eax, %ebx, %ecx, %edx, 8, S11, 0x0698098d8 314 | FF %edx, %eax, %ebx, %ecx, 9, S12, 0x08b44f7af 315 | FF %ecx, %edx, %eax, %ebx, 10, S13, 0x0ffff5bb1 316 | FF %ebx, %ecx, %edx, %eax, 11, S14, 0x0895cd7be 317 | FF %eax, %ebx, %ecx, %edx, 12, S11, 0x06b901122 318 | FF %edx, %eax, %ebx, %ecx, 13, S12, 0x0fd987193 319 | FF %ecx, %edx, %eax, %ebx, 14, S13, 0x0a679438e 320 | FF %ebx, %ecx, %edx, %eax, 15, S14, 0x049b40821 321 | 322 | GG %eax, %ebx, %ecx, %edx, 1, S21, 0x0f61e2562 # Round 2 323 | GG %edx, %eax, %ebx, %ecx, 6, S22, 0x0c040b340 324 | GG %ecx, %edx, %eax, %ebx, 11, S23, 0x0265e5a51 325 | GG %ebx, %ecx, %edx, %eax, 0, S24, 0x0e9b6c7aa 326 | GG %eax, %ebx, %ecx, %edx, 5, S21, 0x0d62f105d 327 | GG %edx, %eax, %ebx, %ecx, 10, S22, 0x002441453 328 | GG %ecx, %edx, %eax, %ebx, 15, S23, 0x0d8a1e681 329 | GG %ebx, %ecx, %edx, %eax, 4, S24, 0x0e7d3fbc8 330 | GG %eax, %ebx, %ecx, %edx, 9, S21, 0x021e1cde6 331 | GG %edx, %eax, %ebx, %ecx, 14, S22, 0x0c33707d6 332 | GG %ecx, %edx, %eax, %ebx, 3, S23, 0x0f4d50d87 333 | GG %ebx, %ecx, %edx, %eax, 8, S24, 0x0455a14ed 334 | GG %eax, %ebx, %ecx, %edx, 13, S21, 0x0a9e3e905 335 | GG %edx, %eax, %ebx, %ecx, 2, S22, 0x0fcefa3f8 336 | GG %ecx, %edx, %eax, %ebx, 7, S23, 0x0676f02d9 337 | GG %ebx, %ecx, %edx, %eax, 12, S24, 0x08d2a4c8a 338 | 339 | HH %eax, %ebx, %ecx, %edx, 5, S31, 0x0fffa3942 # Round 3 340 | HH %edx, %eax, %ebx, %ecx, 8, S32, 0x08771f681 341 | HH %ecx, %edx, %eax, %ebx, 11, S33, 0x06d9d6122 342 | HH %ebx, %ecx, %edx, %eax, 14, S34, 0x0fde5380c 343 | HH %eax, %ebx, %ecx, %edx, 1, S31, 0x0a4beea44 344 | HH %edx, %eax, %ebx, %ecx, 4, S32, 0x04bdecfa9 345 | HH %ecx, %edx, %eax, %ebx, 7, S33, 0x0f6bb4b60 346 | HH %ebx, %ecx, %edx, %eax, 10, S34, 0x0bebfbc70 347 | HH %eax, %ebx, %ecx, %edx, 13, S31, 0x0289b7ec6 348 | HH %edx, %eax, %ebx, %ecx, 0, S32, 0x0eaa127fa 349 | HH %ecx, %edx, %eax, %ebx, 3, S33, 0x0d4ef3085 350 | HH %ebx, %ecx, %edx, %eax, 6, S34, 0x004881d05 351 | HH %eax, %ebx, %ecx, %edx, 9, S31, 0x0d9d4d039 352 | HH %edx, %eax, %ebx, %ecx, 12, S32, 0x0e6db99e5 353 | HH %ecx, %edx, %eax, %ebx, 15, S33, 0x01fa27cf8 354 | HH %ebx, %ecx, %edx, %eax, 2, S34, 0x0c4ac5665 355 | 356 | II %eax, %ebx, %ecx, %edx, 0, S41, 0x0f4292244 # Round 4 357 | II %edx, %eax, %ebx, %ecx, 7, S42, 0x0432aff97 358 | II %ecx, %edx, %eax, %ebx, 14, S43, 0x0ab9423a7 359 | II %ebx, %ecx, %edx, %eax, 5, S44, 0x0fc93a039 360 | II %eax, %ebx, %ecx, %edx, 12, S41, 0x0655b59c3 361 | II %edx, %eax, %ebx, %ecx, 3, S42, 0x08f0ccc92 362 | II %ecx, %edx, %eax, %ebx, 10, S43, 0x0ffeff47d 363 | II %ebx, %ecx, %edx, %eax, 1, S44, 0x085845dd1 364 | II %eax, %ebx, %ecx, %edx, 8, S41, 0x06fa87e4f 365 | II %edx, %eax, %ebx, %ecx, 15, S42, 0x0fe2ce6e0 366 | II %ecx, %edx, %eax, %ebx, 6, S43, 0x0a3014314 367 | II %ebx, %ecx, %edx, %eax, 13, S44, 0x04e0811a1 368 | II %eax, %ebx, %ecx, %edx, 4, S41, 0x0f7537e82 369 | II %edx, %eax, %ebx, %ecx, 11, S42, 0x0bd3af235 370 | II %ecx, %edx, %eax, %ebx, 2, S43, 0x02ad7d2bb 371 | II %ebx, %ecx, %edx, %eax, 9, S44, 0x0eb86d391 372 | 373 | pop %edi # Restore edi = A(MD buffer) 374 | add %eax, (%edi) # Update digest w/results of rounds 375 | add %ebx, 0x04(%edi) 376 | add %ecx, 0x08(%edi) 377 | add %edx, 0x0C(%edi) 378 | 379 | pop %esi # esi = A(current position in message) 380 | pop %ebx # ebx = A(chunk buffer) 381 | pop %edx # edx = Number chunks remaining 382 | pop %eax # eax = Number message bytes remaining 383 | 384 | sub $64, %eax # We've just finished 64-bytes of message 385 | test %edx, %edx # Do we have more message chunks left? 386 | jne _n0 # Jump if we have more 387 | 388 | add $64, %esp # Nope. All done. Deallocate chunk buffer 389 | popal # Restore registers 390 | ret $12 # Return to papa 391 | 392 | .end 393 | --------------------------------------------------------------------------------