]> git.ipfire.org Git - thirdparty/rng-tools.git/blob - rdrand_asm.S
Fix the AES keys so that they are correct and match the comments.
[thirdparty/rng-tools.git] / rdrand_asm.S
1 /*
2 * Copyright (c) 2011, Intel Corporation
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
21 #define ENTRY(x) \
22 .balign 64 ; \
23 .globl x ; \
24 x:
25
26 #define ENDPROC(x) \
27 .size x, .-x ; \
28 .type x, @function
29
30 #define RDRAND_RETRY_LIMIT 10
31
32 #if defined(__x86_64__)
33
34 ENTRY(x86_rdrand_nlong)
35 1:
36 mov $RDRAND_RETRY_LIMIT, %eax
37 2:
38 .byte 0x48,0x0f,0xc7,0xf2 /* rdrand %rdx */
39 jnc 3f
40 mov %rdx, (%rdi)
41 add $8, %rdi
42 sub $1, %esi
43 jnz 1b
44 ret
45 3:
46 sub $1, %eax
47 rep;nop
48 jnz 2b
49 ret
50 ENDPROC(x86_rdrand_nlong)
51
52 #define SETPTR(var,ptr) leaq var(%rip),ptr
53 #define PTR0 %rdi
54 #define PTR1 %rsi
55 #define PTR2 %rcx
56 #define NPTR2 1 /* %rcx = %r1, only 0-7 valid here */
57
58 #elif defined(__i386__)
59
60 ENTRY(x86_rdrand_nlong)
61 push %ebp
62 mov %esp, %ebp
63 push %edi
64 movl 8(%ebp), %ecx
65 movl 12(%ebp), %edx
66 1:
67 mov $RDRAND_RETRY_LIMIT, %eax
68 2:
69 .byte 0x0f,0xc7,0xf7 /* rdrand %edi */
70 jnc 3f
71 mov %edi, (%ecx)
72 add $4, %ecx
73 sub $1, %edx
74 jnz 2b
75 pop %edi
76 pop %ebp
77 ret
78 3:
79 sub $1, %eax
80 rep;nop
81 jnz 2b
82 pop %edi
83 pop %ebp
84 ret
85 ENDPROC(x86_rdrand_nlong)
86
87 #define SETPTR(var,ptr) movl $(var),ptr
88 #define PTR0 %eax
89 #define PTR1 %edx
90 #define PTR2 %ecx
91 #define NPTR2 1 /* %rcx = %r1 */
92
93 #endif
94
95 #if defined(__i386__) || defined(__x86_64__)
96
97 ENTRY(x86_aes_mangle)
98 #if defined(__i386__)
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
164 #if defined(__i386__)
165 pop %ebp
166 #endif
167 ret
168 ENDPROC(x86_aes_mangle)
169 /*
170 * AES round keys for an arbitrary key:
171 * 00102030405060708090A0B0C0D0E0F0
172 */
173 .section ".rodata","a"
174 .balign 16
175 aes_round_keys:
176 .long 0x30201000, 0x70605040, 0xB0A09080, 0xF0E0D0C0
177 .long 0x8AACF171, 0xFACCA131, 0x4A6C31B1, 0xBA8CE171
178 .long 0x2958958B, 0xD39434BA, 0x99F8050B, 0x2374E47A
179 .long 0xF37E07E6, 0x20EA335C, 0xB9123657, 0x9A66D22D
180 .long 0x2BC6345B, 0x0B2C0707, 0xB23E3150, 0x2858E37D
181 .long 0xD4F25E5A, 0xDFDE595D, 0x6DE0680D, 0x45B88B70
182 .long 0x859C3247, 0x5A426B1A, 0x37A20317, 0x721A8867
183 .long 0x00DC90C3, 0x5A9EFBD9, 0x6D3CF8CE, 0x1F2670A9
184 .long 0xD31C6712, 0x89829CCB, 0xE4BE6405, 0xFB9814AC
185 .long 0x421321F3, 0xCB91BD38, 0x2F2FD93D, 0xD4B7CD91
186 .long 0xC35B8878, 0x08CA3540, 0x27E5EC7D, 0xF35221EC
187 .size aes_round_keys, .-aes_round_keys
188
189 .bss
190 .balign 16
191 aes_fwd_state:
192 .space 16
193 .size aes_fwd_state, .-aes_fwd_state
194
195 #endif /* i386 or x86_64 */
196
197 /*
198 * This is necessary to keep the whole executable
199 * from needing a writable stack.
200 */
201 .section .note.GNU-stack,"",%progbits