-
Notifications
You must be signed in to change notification settings - Fork 12
/
md5-arm64-asm.h
215 lines (188 loc) · 7.96 KB
/
md5-arm64-asm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef STR
# define STR_HELPER(x) #x
# define STR(x) STR_HELPER(x)
#endif
#ifdef __aarch64__
#if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
# define REV(R)
#else
# define REV(R) "rev " R ", " R "\n"
#endif
static const uint64_t md5_constants_aarch64[32] __attribute__((aligned(8))) = {
// F
0xe8c7b756d76aa478ULL, 0xc1bdceee242070dbULL,
0x4787c62af57c0fafULL, 0xfd469501a8304613ULL,
0x8b44f7af698098d8ULL, 0x895cd7beffff5bb1ULL,
0xfd9871936b901122ULL, 0x49b40821a679438eULL,
// G
0xc040b340f61e2562ULL, 0xe9b6c7aa265e5a51ULL,
0x02441453d62f105dULL, 0xe7d3fbc8d8a1e681ULL,
0xc33707d621e1cde6ULL, 0x455a14edf4d50d87ULL,
0xfcefa3f8a9e3e905ULL, 0x8d2a4c8a676f02d9ULL,
// H
0x8771f681fffa3942ULL, 0xfde5380c6d9d6122ULL,
0x4bdecfa9a4beea44ULL, 0xbebfbc70f6bb4b60ULL,
0xeaa127fa289b7ec6ULL, 0x04881d05d4ef3085ULL,
0xe6db99e5d9d4d039ULL, 0xc4ac56651fa27cf8ULL,
// I
0x432aff97f4292244ULL, 0xfc93a039ab9423a7ULL,
0x8f0ccc92655b59c3ULL, 0x85845dd1ffeff47dULL,
0xfe2ce6e06fa87e4fULL, 0x4e0811a1a3014314ULL,
0xbd3af235f7537e82ULL, 0xeb86d3912ad7d2bbULL
};
template<typename HT>
struct MD5_STATE {
HT A, B, C, D;
};
static inline __attribute__((always_inline)) void md5_block_std(MD5_STATE<uint32_t>* __restrict__ state, const void* __restrict__ data) {
uint32_t A;
uint32_t B;
uint32_t C;
uint32_t D;
const uint32_t* __restrict__ _in = (const uint32_t* __restrict__)data;
uint32_t tmp1, tmp2;
uint64_t k0, k1;
uint32_t cache[16];
#define ASM_PARAMS(x) [A]x(A), [B]x(B), [C]x(C), [D]x(D), \
[TMP1]x(tmp1), [TMP2]x(tmp2), [k0]x(k0), [k1]x(k1)
#define ROUND_F(IA, A, B, C, D, I, K, KEXTRA, R, LEXTRA) \
REV(I) \
"add %w[" STR(A) "], %w[" STR(IA) "], " I "\n" \
"eor %w[TMP2], %w[" STR(C) "], %w[" STR(D) "]\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[" K "]\n" \
"and %w[TMP2], %w[TMP2], %w[" STR(B) "]\n" \
KEXTRA "\n" \
"eor %w[TMP2], %w[TMP2], %w[" STR(D) "]\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[TMP2]\n" \
LEXTRA "\n" \
"ror %w[" STR(A) "], %w[" STR(A) "], #" STR(R) "\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[" STR(B) "]\n"
#define ROUND_H(A, B, C, D, I, K, KEXTRA, R) \
"eor %w[TMP2], %w[" STR(C) "], %w[" STR(D) "]\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[" K "]\n" \
"eor %w[TMP2], %w[TMP2], %w[" STR(B) "]\n" \
KEXTRA "\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[TMP2]\n" \
"ror %w[" STR(A) "], %w[" STR(A) "], #" STR(R) "\n" \
"add %w[" STR(D) "], %w[" STR(D) "], " I "\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[" STR(B) "]\n"
#define ROUND_I(A, B, C, D, I, K, KEXTRA, R) \
"orn %w[TMP2], %w[" STR(B) "], %w[" STR(D) "]\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[" K "]\n" \
"eor %w[TMP2], %w[TMP2], %w[" STR(C) "]\n" \
KEXTRA "\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[TMP2]\n" \
"ror %w[" STR(A) "], %w[" STR(A) "], #" STR(R) "\n" \
"add %w[" STR(D) "], %w[" STR(D) "], " I "\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[" STR(B) "]\n"
#define ROUND_G(A, B, C, D, I, K, KEXTRA, R) \
"bic %w[TMP2], %w[" STR(C) "], %w[" STR(D) "]\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[" K "]\n" \
"and %w[TMP1], %w[" STR(B) "], %w[" STR(D) "]\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[TMP2]\n" \
KEXTRA "\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[TMP1]\n" \
"ror %w[" STR(A) "], %w[" STR(A) "], #" STR(R) "\n" \
"add %w[" STR(D) "], %w[" STR(D) "], " I "\n" \
"add %w[" STR(A) "], %w[" STR(A) "], %w[" STR(B) "]\n"
#define RF4(i0, i1, i2, i3, i4, i5, kr) \
__asm__( \
ROUND_F(A, A, B, C, D, "%w[cache0]", "k0", "lsr %[k0], %[k0], #32", 25, "ldp %w[cache2], %w[cache3], %[in2]") \
ROUND_F(D, D, A, B, C, "%w[cache1]", "k0", "", 20, "") \
ROUND_F(C, C, D, A, B, "%w[cache2]", "k1", "lsr %[k1], %[k1], #32", 15, "ldp %w[cache4], %w[cache5], %[in4]") \
ROUND_F(B, B, C, D, A, "%w[cache3]", "k1", "ldp %[k0], %[k1], [%[kM], #" STR(kr) "]", 10, "") \
: ASM_PARAMS("+&r"), [cache2]"=&r"(cache[i2]), [cache3]"=&r"(cache[i3]), [cache4]"=&r"(cache[i4]), [cache5]"=&r"(cache[i5]) \
: [in2]"Ump"(_in[i2]), [in4]"Ump"(_in[i4]), [kM]"r"(md5_constants_aarch64), \
[cache0]"r"(cache[i0]), [cache1]"r"(cache[i1]) \
:);
#define RG4(i0, i1, i2, i3, kr) \
__asm__( \
ROUND_G(A, B, C, D, "%w[cache0]", "k0", "lsr %[k0], %[k0], #32", 27) \
ROUND_G(D, A, B, C, "%w[cache1]", "k0", "", 23) \
ROUND_G(C, D, A, B, "%w[cache2]", "k1", "lsr %[k1], %[k1], #32", 18) \
ROUND_G(B, C, D, A, "%w[cache3]", "k1", "ldp %[k0], %[k1], [%[kM], #" STR(kr) "]", 12) \
: ASM_PARAMS("+&r") \
: [kM]"r"(md5_constants_aarch64), [cache0]"r"(cache[i0]), [cache1]"r"(cache[i1]), [cache2]"r"(cache[i2]), [cache3]"r"(cache[i3]) \
:);
#define RH4(i0, i1, i2, i3, kr) \
__asm__( \
ROUND_H(A, B, C, D, "%w[cache0]", "k0", "lsr %[k0], %[k0], #32", 28) \
ROUND_H(D, A, B, C, "%w[cache1]", "k0", "", 21) \
ROUND_H(C, D, A, B, "%w[cache2]", "k1", "lsr %[k1], %[k1], #32", 16) \
ROUND_H(B, C, D, A, "%w[cache3]", "k1", "ldp %[k0], %[k1], [%[kM], #" STR(kr) "]", 9) \
: ASM_PARAMS("+&r") \
: [kM]"r"(md5_constants_aarch64), [cache0]"r"(cache[i0]), [cache1]"r"(cache[i1]), [cache2]"r"(cache[i2]), [cache3]"r"(cache[i3]) \
:);
#define RI4(i0, i1, i2, i3, kr) \
__asm__( \
ROUND_I(A, B, C, D, "%w[cache0]", "k0", "lsr %[k0], %[k0], #32", 26) \
ROUND_I(D, A, B, C, "%w[cache1]", "k0", "", 22) \
ROUND_I(C, D, A, B, "%w[cache2]", "k1", "lsr %[k1], %[k1], #32", 17) \
ROUND_I(B, C, D, A, "%w[cache3]", "k1", "ldp %[k0], %[k1], [%[kM], #" STR(kr) "]", 11) \
: ASM_PARAMS("+&r") \
: [kM]"r"(md5_constants_aarch64), [cache0]"r"(cache[i0]), [cache1]"r"(cache[i1]), [cache2]"r"(cache[i2]), [cache3]"r"(cache[i3]) \
:);
__asm__(
"ldp %w[cache0], %w[cache1], %[i0]\n"
"ldp %[k0], %[k1], [%[kM]]\n"
ROUND_F(IA, A, IB, IC, ID, "%w[cache0]", "k0", "lsr %[k0], %[k0], #32", 25, "ldp %w[cache2], %w[cache3], %[i2]")
ROUND_F(ID, D, A, IB, IC, "%w[cache1]", "k0", "", 20, "")
ROUND_F(IC, C, D, A, IB, "%w[cache2]", "k1", "lsr %[k1], %[k1], #32", 15, "ldp %w[cache4], %w[cache5], %[i4]")
ROUND_F(IB, B, C, D, A, "%w[cache3]", "k1", "ldp %[k0], %[k1], [%[kM], #16]", 10, "")
: ASM_PARAMS("=&r"), [cache0]"=&r"(cache[0]), [cache1]"=&r"(cache[1]), [cache2]"=&r"(cache[2]), [cache3]"=&r"(cache[3]), [cache4]"=&r"(cache[4]), [cache5]"=&r"(cache[5])
: [i0]"Ump"(_in[0]), [i2]"Ump"(_in[2]), [i4]"Ump"(_in[4]),
[kM]"r"(md5_constants_aarch64),
[IA]"r"(state->A), [IB]"r"(state->B), [IC]"r"(state->C), [ID]"r"(state->D)
:);
RF4( 4, 5, 6, 7, 8, 9, 32)
RF4( 8, 9, 10, 11, 12, 13, 48)
__asm__(
ROUND_F(A, A, B, C, D, "%w[cache0]", "k0", "lsr %[k0], %[k0], #32", 25, "ldp %w[cache2], %w[cache3], %[i14]")
ROUND_F(D, D, A, B, C, "%w[cache1]", "k0", "", 20, "")
ROUND_F(C, C, D, A, B, "%w[cache2]", "k1", "lsr %[k1], %[k1], #32", 15, "")
ROUND_F(B, B, C, D, A, "%w[cache3]", "k1", "ldp %[k0], %[k1], [%[kM], #64]", 10, "")
"add %w[A], %w[A], %w[cacheN]\n"
: ASM_PARAMS("+&r"), [cache2]"=&r"(cache[14]), [cache3]"=&r"(cache[15])
: [i14]"Ump"(_in[14]), [kM]"r"(md5_constants_aarch64),
[cache0]"r"(cache[12]), [cache1]"r"(cache[13]), [cacheN]"r"(cache[1])
:);
RG4( 6, 11, 0, 5, 80)
RG4( 10, 15, 4, 9, 96)
RG4( 14, 3, 8,13, 112)
RG4( 2, 7, 12, 5, 128)
RH4( 8, 11, 14, 1, 144)
RH4( 4, 7, 10,13, 160)
RH4( 0, 3, 6, 9, 176)
RH4( 12, 15, 2, 0, 192)
RI4( 7, 14, 5,12, 208)
RI4( 3, 10, 1, 8, 224)
RI4( 15, 6, 13, 4, 240)
__asm__(
ROUND_I(A, B, C, D, "%w[cache0]", "k0", "lsr %[k0], %[k0], #32", 26)
ROUND_I(D, A, B, C, "%w[cache1]", "k0", "", 22)
ROUND_I(C, D, A, B, "%w[cache2]", "k1", "lsr %[k1], %[k1], #32", 17)
// ROUND_I last
"orn %w[TMP2], %w[C], %w[A]\n"
"add %w[B], %w[B], %w[k1]\n"
"eor %w[TMP2], %w[TMP2], %w[D]\n"
"add %w[B], %w[B], %w[TMP2]\n"
"ror %w[B], %w[B], #11\n"
"add %w[B], %w[B], %w[C]\n"
: ASM_PARAMS("+&r")
: [kM]"r"(md5_constants_aarch64), [cache0]"r"(cache[11]), [cache1]"r"(cache[2]), [cache2]"r"(cache[9])
:);
state->A += A;
state->B += B;
state->C += C;
state->D += D;
#undef ROUND_F
#undef ROUND_G
#undef ROUND_H
#undef ROUND_I
#undef RF4
#undef RG4
#undef RH4
#undef RI4
#undef REV
}
#endif // defined(__aarch64__)