Text file
src/crypto/md5/md5block_amd64.s
1// Original source:
2// http://www.zorinaq.com/papers/md5-amd64.html
3// http://www.zorinaq.com/papers/md5-amd64.tar.bz2
4//
5// Translated from Perl generating GNU assembly into
6// #defines generating 6a assembly by the Go Authors.
7
8//go:build !purego
9
10#include "textflag.h"
11
12// MD5 optimized for AMD64.
13//
14// Author: Marc Bevand <bevand_m (at) epita.fr>
15// Licence: I hereby disclaim the copyright on this code and place it
16// in the public domain.
17
18TEXT ·block(SB),NOSPLIT,$8-32
19 MOVQ dig+0(FP), BP
20 MOVQ p+8(FP), SI
21 MOVQ p_len+16(FP), DX
22 SHRQ $6, DX
23 SHLQ $6, DX
24
25 LEAQ (SI)(DX*1), DI
26 MOVL (0*4)(BP), AX
27 MOVL (1*4)(BP), BX
28 MOVL (2*4)(BP), CX
29 MOVL (3*4)(BP), DX
30 MOVL $0xffffffff, R11
31
32 CMPQ SI, DI
33 JEQ end
34
35loop:
36 MOVL AX, R12
37 MOVL BX, R13
38 MOVL CX, R14
39 MOVL DX, R15
40
41 MOVL (0*4)(SI), R8
42 MOVL DX, R9
43
44#define ROUND1(a, b, c, d, index, const, shift) \
45 XORL c, R9; \
46 ADDL $const, a; \
47 ADDL R8, a; \
48 ANDL b, R9; \
49 XORL d, R9; \
50 MOVL (index*4)(SI), R8; \
51 ADDL R9, a; \
52 ROLL $shift, a; \
53 MOVL c, R9; \
54 ADDL b, a
55
56 ROUND1(AX,BX,CX,DX, 1,0xd76aa478, 7);
57 ROUND1(DX,AX,BX,CX, 2,0xe8c7b756,12);
58 ROUND1(CX,DX,AX,BX, 3,0x242070db,17);
59 ROUND1(BX,CX,DX,AX, 4,0xc1bdceee,22);
60 ROUND1(AX,BX,CX,DX, 5,0xf57c0faf, 7);
61 ROUND1(DX,AX,BX,CX, 6,0x4787c62a,12);
62 ROUND1(CX,DX,AX,BX, 7,0xa8304613,17);
63 ROUND1(BX,CX,DX,AX, 8,0xfd469501,22);
64 ROUND1(AX,BX,CX,DX, 9,0x698098d8, 7);
65 ROUND1(DX,AX,BX,CX,10,0x8b44f7af,12);
66 ROUND1(CX,DX,AX,BX,11,0xffff5bb1,17);
67 ROUND1(BX,CX,DX,AX,12,0x895cd7be,22);
68 ROUND1(AX,BX,CX,DX,13,0x6b901122, 7);
69 ROUND1(DX,AX,BX,CX,14,0xfd987193,12);
70 ROUND1(CX,DX,AX,BX,15,0xa679438e,17);
71 ROUND1(BX,CX,DX,AX, 1,0x49b40821,22);
72
73 MOVL DX, R9
74 MOVL DX, R10
75
76// Uses https://github.com/animetosho/md5-optimisation#dependency-shortcut-in-g-function
77
78#define ROUND2(a, b, c, d, index, const, shift) \
79 XORL R11, R9; \
80 ADDL $const, a; \
81 ADDL R8, a; \
82 ANDL b, R10; \
83 ANDL c, R9; \
84 MOVL (index*4)(SI),R8; \
85 ADDL R9, a; \
86 ADDL R10, a; \
87 MOVL c, R9; \
88 MOVL c, R10; \
89 ROLL $shift, a; \
90 ADDL b, a
91
92 ROUND2(AX,BX,CX,DX, 6,0xf61e2562, 5);
93 ROUND2(DX,AX,BX,CX,11,0xc040b340, 9);
94 ROUND2(CX,DX,AX,BX, 0,0x265e5a51,14);
95 ROUND2(BX,CX,DX,AX, 5,0xe9b6c7aa,20);
96 ROUND2(AX,BX,CX,DX,10,0xd62f105d, 5);
97 ROUND2(DX,AX,BX,CX,15, 0x2441453, 9);
98 ROUND2(CX,DX,AX,BX, 4,0xd8a1e681,14);
99 ROUND2(BX,CX,DX,AX, 9,0xe7d3fbc8,20);
100 ROUND2(AX,BX,CX,DX,14,0x21e1cde6, 5);
101 ROUND2(DX,AX,BX,CX, 3,0xc33707d6, 9);
102 ROUND2(CX,DX,AX,BX, 8,0xf4d50d87,14);
103 ROUND2(BX,CX,DX,AX,13,0x455a14ed,20);
104 ROUND2(AX,BX,CX,DX, 2,0xa9e3e905, 5);
105 ROUND2(DX,AX,BX,CX, 7,0xfcefa3f8, 9);
106 ROUND2(CX,DX,AX,BX,12,0x676f02d9,14);
107 ROUND2(BX,CX,DX,AX, 5,0x8d2a4c8a,20);
108
109 MOVL CX, R9
110
111// Uses https://github.com/animetosho/md5-optimisation#h-function-re-use
112
113#define ROUND3FIRST(a, b, c, d, index, const, shift) \
114 MOVL d, R9; \
115 XORL c, R9; \
116 XORL b, R9; \
117 ADDL $const, a; \
118 ADDL R8, a; \
119 MOVL (index*4)(SI),R8; \
120 ADDL R9, a; \
121 ROLL $shift, a; \
122 ADDL b, a
123
124#define ROUND3(a, b, c, d, index, const, shift) \
125 XORL a, R9; \
126 XORL b, R9; \
127 ADDL $const, a; \
128 ADDL R8, a; \
129 MOVL (index*4)(SI),R8; \
130 ADDL R9, a; \
131 ROLL $shift, a; \
132 ADDL b, a
133
134 ROUND3FIRST(AX,BX,CX,DX, 8,0xfffa3942, 4);
135 ROUND3(DX,AX,BX,CX,11,0x8771f681,11);
136 ROUND3(CX,DX,AX,BX,14,0x6d9d6122,16);
137 ROUND3(BX,CX,DX,AX, 1,0xfde5380c,23);
138 ROUND3(AX,BX,CX,DX, 4,0xa4beea44, 4);
139 ROUND3(DX,AX,BX,CX, 7,0x4bdecfa9,11);
140 ROUND3(CX,DX,AX,BX,10,0xf6bb4b60,16);
141 ROUND3(BX,CX,DX,AX,13,0xbebfbc70,23);
142 ROUND3(AX,BX,CX,DX, 0,0x289b7ec6, 4);
143 ROUND3(DX,AX,BX,CX, 3,0xeaa127fa,11);
144 ROUND3(CX,DX,AX,BX, 6,0xd4ef3085,16);
145 ROUND3(BX,CX,DX,AX, 9, 0x4881d05,23);
146 ROUND3(AX,BX,CX,DX,12,0xd9d4d039, 4);
147 ROUND3(DX,AX,BX,CX,15,0xe6db99e5,11);
148 ROUND3(CX,DX,AX,BX, 2,0x1fa27cf8,16);
149 ROUND3(BX,CX,DX,AX, 0,0xc4ac5665,23);
150
151 MOVL R11, R9
152 XORL DX, R9
153
154#define ROUND4(a, b, c, d, index, const, shift) \
155 ADDL $const, a; \
156 ADDL R8, a; \
157 ORL b, R9; \
158 XORL c, R9; \
159 ADDL R9, a; \
160 MOVL (index*4)(SI),R8; \
161 MOVL $0xffffffff, R9; \
162 ROLL $shift, a; \
163 XORL c, R9; \
164 ADDL b, a
165
166 ROUND4(AX,BX,CX,DX, 7,0xf4292244, 6);
167 ROUND4(DX,AX,BX,CX,14,0x432aff97,10);
168 ROUND4(CX,DX,AX,BX, 5,0xab9423a7,15);
169 ROUND4(BX,CX,DX,AX,12,0xfc93a039,21);
170 ROUND4(AX,BX,CX,DX, 3,0x655b59c3, 6);
171 ROUND4(DX,AX,BX,CX,10,0x8f0ccc92,10);
172 ROUND4(CX,DX,AX,BX, 1,0xffeff47d,15);
173 ROUND4(BX,CX,DX,AX, 8,0x85845dd1,21);
174 ROUND4(AX,BX,CX,DX,15,0x6fa87e4f, 6);
175 ROUND4(DX,AX,BX,CX, 6,0xfe2ce6e0,10);
176 ROUND4(CX,DX,AX,BX,13,0xa3014314,15);
177 ROUND4(BX,CX,DX,AX, 4,0x4e0811a1,21);
178 ROUND4(AX,BX,CX,DX,11,0xf7537e82, 6);
179 ROUND4(DX,AX,BX,CX, 2,0xbd3af235,10);
180 ROUND4(CX,DX,AX,BX, 9,0x2ad7d2bb,15);
181 ROUND4(BX,CX,DX,AX, 0,0xeb86d391,21);
182
183 ADDL R12, AX
184 ADDL R13, BX
185 ADDL R14, CX
186 ADDL R15, DX
187
188 ADDQ $64, SI
189 CMPQ SI, DI
190 JB loop
191
192end:
193 MOVL AX, (0*4)(BP)
194 MOVL BX, (1*4)(BP)
195 MOVL CX, (2*4)(BP)
196 MOVL DX, (3*4)(BP)
197 RET
View as plain text