-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiphash_nasm.asm
More file actions
215 lines (200 loc) · 3.73 KB
/
siphash_nasm.asm
File metadata and controls
215 lines (200 loc) · 3.73 KB
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
section .note.GNU-stack note
align 4
dd 1, 0, 0
db "GNU",0
align 4
bits 64
section .data
align 8
JumpTable_2_4:
dq Last0_2_4
dq Last1_2_4
dq Last2_2_4
dq Last3_2_4
dq Last4_2_4
dq Last5_2_4
dq Last6_2_4
dq Last7_2_4
align 8
JumpTable_4_8:
dq Last0_4_8
dq Last1_4_8
dq Last2_4_8
dq Last3_4_8
dq Last4_4_8
dq Last5_4_8
dq Last6_4_8
dq Last7_4_8
section .text
global siphash_2_4
global siphash_4_8
%macro InitMainValues 0
mov r10, 0x0736f6d6570736575 ; r10 = v0
xor r10, rdx ; r10 = v0 ^ key0
mov r11, 0x0646f72616e646f6d ; r11 = v1
xor r11, rcx ; r11 = v1 ^ key1
mov r8, 0x06c7967656e657261 ; r8 = v2
xor r8, rdx ; r8 = key0 ^ v2
mov r9, 0x07465646279746573 ; r9 = v3
xor r9, rcx ; r9 = key1 ^ v3
%endmacro
%macro SipRound 0
add r10, r11 ; v0 += v1
rol r11, 13 ; v1 <<= 13
xor r11, r10 ; v1 ^= v0
rol r10, 32 ; v0 <<= 32
add r8, r9 ; v2 += v3
rol r9, 16 ; v3 <<= 16
xor r9, r8 ; v3 ^= v2
add r8, r11 ; v2 += v1
rol r11, 17 ; v1 <<= 17
xor r11, r8 ; v1 ^= v2
rol r8, 32 ; v2 <<= 32
add r10, r9 ; v0 += v3
rol r9, 21 ; v3 <<= 21
xor r9, r10 ; v3 ^= v0
%endmacro
siphash_2_4:
; RDI message ptr [byte]
; RSI mess_len qword unsigned
; RDX key0 qword
; RCX key1 qword
; INITIALIZATION
InitMainValues
mov rcx, rsi
shr rcx, 3
jz MessageLoadLoopEnd_2_4
MessageLoadLoop_2_4:
mov rax, qword [rdi]
xor r9, rax
SipRound
SipRound
xor r10, rax
add rdi, 8
dec rcx
jnz MessageLoadLoop_2_4
MessageLoadLoopEnd_2_4:
mov rax, rsi
shl rax, 56
and rsi, 7 ; rsi = mess_len % 8
lea rdx, [rel JumpTable_2_4]
jmp qword [rdx + rsi*8]
Last7_2_4:
movzx rcx, byte [rdi + 6]
shl rcx, 48
or rax, rcx
Last6_2_4:
movzx rcx, byte [rdi + 5]
shl rcx, 40
or rax, rcx
Last5_2_4:
movzx rcx, byte [rdi + 4]
shl rcx, 32
or rax, rcx
Last4_2_4:
movzx rcx, byte [rdi + 3]
shl rcx, 24
or rax, rcx
Last3_2_4:
movzx rcx, byte [rdi + 2]
shl rcx, 16
or rax, rcx
Last2_2_4:
movzx rcx, byte [rdi + 1]
shl rcx, 8
or rax, rcx
Last1_2_4:
movzx rcx, byte [rdi]
or rax, rcx
Last0_2_4:
xor r9, rax
SipRound
SipRound
xor r10, rax
; FINALIZATION
xor r8, 0FFh
SipRound
SipRound
SipRound
SipRound
xor r8, r9
xor r8, r10
xor r8, r11
mov rax, r8
ret
siphash_4_8:
; RDI message ptr [byte]
; RSI mess_len qword unsigned
; R8 RDX key0 qword
; R9 RCX key1 qword
; INITIALIZATION
InitMainValues
mov rcx, rsi
shr rcx, 3
jz MessageLoadLoopEnd_4_8
MessageLoadLoop_4_8:
mov rax, qword [rdi]
xor r9, rax
SipRound
SipRound
SipRound
SipRound
xor r10, rax
add rdi, 8
dec rcx
jnz MessageLoadLoop_4_8
MessageLoadLoopEnd_4_8:
mov rax, rsi
shl rax, 56
and rsi, 7 ; rsi = mess_len % 8
lea rdx, [rel JumpTable_4_8]
jmp qword [rdx + rsi*8]
Last7_4_8:
movzx rcx, byte [rdi + 6]
shl rcx, 48
or rax, rcx
Last6_4_8:
movzx rcx, byte [rdi + 5]
shl rcx, 40
or rax, rcx
Last5_4_8:
movzx rcx, byte [rdi + 4]
shl rcx, 32
or rax, rcx
Last4_4_8:
movzx rcx, byte [rdi + 3]
shl rcx, 24
or rax, rcx
Last3_4_8:
movzx rcx, byte [rdi + 2]
shl rcx, 16
or rax, rcx
Last2_4_8:
movzx rcx, byte [rdi + 1]
shl rcx, 8
or rax, rcx
Last1_4_8:
movzx rcx, byte [rdi]
or rax, rcx
Last0_4_8:
xor r9, rax
SipRound
SipRound
SipRound
SipRound
xor r10, rax
; FINALIZATION
xor r8, 0FFh
SipRound
SipRound
SipRound
SipRound
SipRound
SipRound
SipRound
SipRound
xor r8, r9
xor r8, r10
xor r8, r11
mov rax, r8
ret