]> git.ipfire.org Git - thirdparty/rng-tools.git/blame - rdrand_asm.S
rdrand_asm.S: Use #ifdef instead of #if defined()
[thirdparty/rng-tools.git] / rdrand_asm.S
CommitLineData
2a014536 1/*
94f03c9a 2 * Copyright (c) 2011-2014, Intel Corporation
2a014536
BH
3 * Authors: Fenghua Yu <fenghua.yu@intel.com>,
4 * H. Peter Anvin <hpa@linux.intel.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
795666e6
PA
21#if defined(__i386__) || defined(__x86_64__)
22
2a014536
BH
23#define ENTRY(x) \
24 .balign 64 ; \
25 .globl x ; \
26x:
27
28#define ENDPROC(x) \
29 .size x, .-x ; \
30 .type x, @function
31
32#define RDRAND_RETRY_LIMIT 10
33
795666e6 34#ifdef __x86_64__
2a014536
BH
35
36ENTRY(x86_rdrand_nlong)
371:
38 mov $RDRAND_RETRY_LIMIT, %eax
392:
40 .byte 0x48,0x0f,0xc7,0xf2 /* rdrand %rdx */
41 jnc 3f
42 mov %rdx, (%rdi)
43 add $8, %rdi
44 sub $1, %esi
45 jnz 1b
46 ret
473:
48 sub $1, %eax
49 rep;nop
50 jnz 2b
51 ret
52ENDPROC(x86_rdrand_nlong)
53
54#define SETPTR(var,ptr) leaq var(%rip),ptr
55#define PTR0 %rdi
56#define PTR1 %rsi
57#define PTR2 %rcx
58#define NPTR2 1 /* %rcx = %r1, only 0-7 valid here */
59
60#elif defined(__i386__)
61
62ENTRY(x86_rdrand_nlong)
63 push %ebp
64 mov %esp, %ebp
65 push %edi
66 movl 8(%ebp), %ecx
67 movl 12(%ebp), %edx
681:
69 mov $RDRAND_RETRY_LIMIT, %eax
702:
71 .byte 0x0f,0xc7,0xf7 /* rdrand %edi */
72 jnc 3f
73 mov %edi, (%ecx)
74 add $4, %ecx
75 sub $1, %edx
76 jnz 2b
77 pop %edi
78 pop %ebp
79 ret
803:
81 sub $1, %eax
82 rep;nop
83 jnz 2b
84 pop %edi
85 pop %ebp
86 ret
87ENDPROC(x86_rdrand_nlong)
88
89#define SETPTR(var,ptr) movl $(var),ptr
90#define PTR0 %eax
91#define PTR1 %edx
92#define PTR2 %ecx
93#define NPTR2 1 /* %rcx = %r1 */
94
95#endif
96
2a014536 97ENTRY(x86_aes_mangle)
795666e6 98#ifdef __i386__
2a014536
BH
99 push %ebp
100 mov %esp, %ebp
101 movl 8(%ebp), %eax
102 movl 12(%ebp), %edx
103#endif
104
105 SETPTR(aes_round_keys, PTR2)
106
107 movdqa (0*16)(PTR0), %xmm0
108 movdqa (1*16)(PTR0), %xmm1
109 movdqa (2*16)(PTR0), %xmm2
110 movdqa (3*16)(PTR0), %xmm3
111 movdqa (4*16)(PTR0), %xmm4
112 movdqa (5*16)(PTR0), %xmm5
113 movdqa (6*16)(PTR0), %xmm6
114 movdqa (7*16)(PTR0), %xmm7
115
116 pxor (0*16)(PTR1), %xmm0
117 pxor (1*16)(PTR1), %xmm1
118 pxor (2*16)(PTR1), %xmm2
119 pxor (3*16)(PTR1), %xmm3
120 pxor (4*16)(PTR1), %xmm4
121 pxor (5*16)(PTR1), %xmm5
122 pxor (6*16)(PTR1), %xmm6
123 pxor (7*16)(PTR1), %xmm7
124
125 .rept 10
126 .byte 0x66,0x0f,0x38,0xdc,0x00+NPTR2 /* aesenc (PTR2), %xmm0 */
127 .byte 0x66,0x0f,0x38,0xdc,0x08+NPTR2 /* aesenc (PTR2), %xmm1 */
128 .byte 0x66,0x0f,0x38,0xdc,0x10+NPTR2 /* aesenc (PTR2), %xmm2 */
129 .byte 0x66,0x0f,0x38,0xdc,0x18+NPTR2 /* aesenc (PTR2), %xmm3 */
130 .byte 0x66,0x0f,0x38,0xdc,0x20+NPTR2 /* aesenc (PTR2), %xmm4 */
131 .byte 0x66,0x0f,0x38,0xdc,0x28+NPTR2 /* aesenc (PTR2), %xmm5 */
132 .byte 0x66,0x0f,0x38,0xdc,0x30+NPTR2 /* aesenc (PTR2), %xmm6 */
133 .byte 0x66,0x0f,0x38,0xdc,0x38+NPTR2 /* aesenc (PTR2), %xmm7 */
134 add $16, PTR2
135 .endr
136
137 .byte 0x66,0x0f,0x38,0xdd,0x00+NPTR2 /* aesenclast (PTR2), %xmm0 */
138 .byte 0x66,0x0f,0x38,0xdd,0x08+NPTR2 /* aesenclast (PTR2), %xmm1 */
139 .byte 0x66,0x0f,0x38,0xdd,0x10+NPTR2 /* aesenclast (PTR2), %xmm2 */
140 .byte 0x66,0x0f,0x38,0xdd,0x18+NPTR2 /* aesenclast (PTR2), %xmm3 */
141 .byte 0x66,0x0f,0x38,0xdd,0x20+NPTR2 /* aesenclast (PTR2), %xmm4 */
142 .byte 0x66,0x0f,0x38,0xdd,0x28+NPTR2 /* aesenclast (PTR2), %xmm5 */
143 .byte 0x66,0x0f,0x38,0xdd,0x30+NPTR2 /* aesenclast (PTR2), %xmm6 */
144 .byte 0x66,0x0f,0x38,0xdd,0x38+NPTR2 /* aesenclast (PTR2), %xmm7 */
145
146 movdqa %xmm0, (0*16)(PTR0)
147 movdqa %xmm1, (1*16)(PTR0)
148 movdqa %xmm2, (2*16)(PTR0)
149 movdqa %xmm3, (3*16)(PTR0)
150 movdqa %xmm4, (4*16)(PTR0)
151 movdqa %xmm5, (5*16)(PTR0)
152 movdqa %xmm6, (6*16)(PTR0)
153 movdqa %xmm7, (7*16)(PTR0)
154
155 movdqa %xmm0, (0*16)(PTR1)
156 movdqa %xmm1, (1*16)(PTR1)
157 movdqa %xmm2, (2*16)(PTR1)
158 movdqa %xmm3, (3*16)(PTR1)
159 movdqa %xmm4, (4*16)(PTR1)
160 movdqa %xmm5, (5*16)(PTR1)
161 movdqa %xmm6, (6*16)(PTR1)
162 movdqa %xmm7, (7*16)(PTR1)
163
795666e6 164#ifdef __i386__
2a014536
BH
165 pop %ebp
166#endif
167 ret
168ENDPROC(x86_aes_mangle)
2a014536 169
94f03c9a
PA
170/* aeskeygenassist $imm,%xmm0,%xmm1 */
171#define AESKEYGENASSIST(imm) .byte 0x66,0x0f,0x3a,0xdf,0xc8,imm
172
173ENTRY(x86_aes_expand_key)
795666e6 174#ifdef __i386__
94f03c9a
PA
175 push %ebp
176 mov %esp, %ebp
177 movl 8(%ebp), %eax
178#endif
179
180 SETPTR(aes_round_keys, PTR1)
181 movdqu (PTR0), %xmm0
182 movdqa %xmm0, (PTR1) /* First slot = the plain key */
183 add $16, PTR1
184
185 AESKEYGENASSIST(0x01)
186 call 1f
187 AESKEYGENASSIST(0x02)
188 call 1f
189 AESKEYGENASSIST(0x04)
190 call 1f
191 AESKEYGENASSIST(0x08)
192 call 1f
193 AESKEYGENASSIST(0x10)
194 call 1f
195 AESKEYGENASSIST(0x20)
196 call 1f
197 AESKEYGENASSIST(0x40)
198 call 1f
199 AESKEYGENASSIST(0x80)
200 call 1f
201 AESKEYGENASSIST(0x1b)
202 call 1f
203 AESKEYGENASSIST(0x36)
204 call 1f
205
795666e6 206#ifdef __i386__
94f03c9a
PA
207 pop %ebp
208#endif
209 ret
2a014536 210
94f03c9a
PA
2111:
212 pshufd $0xff, %xmm1, %xmm1
213 movdqa %xmm0, %xmm2
214 pslldq $4, %xmm2
215 pxor %xmm2, %xmm0
216 pslldq $4, %xmm2
217 pxor %xmm2, %xmm0
218 pslldq $4, %xmm2
219 pxor %xmm2, %xmm0
220 pxor %xmm1, %xmm0
221 movdqa %xmm0, (PTR1)
222 add $16, PTR1
223 ret
224
225ENDPROC(x86_aes_expand_key)
226
227 .bss
228 .balign 64
229aes_round_keys:
230 .space 11*16
231 .size aes_round_keys, .-aes_round_keys
795666e6 232
2a014536 233#endif /* i386 or x86_64 */
b8579105
PA
234
235/*
236 * This is necessary to keep the whole executable
237 * from needing a writable stack.
238 */
239 .section .note.GNU-stack,"",%progbits