2 * The white paper of AES-NI instructions can be downloaded from:
3 * http://softwarecommunity.intel.com/isn/downloads/intelavx/AES-Instructions-Set_WP.pdf
5 * Copyright (C) 2008-2010, Intel Corporation
6 * Author: Huang Ying <ying.huang@intel.com>
7 * Vinodh Gopal <vinodh.gopal@intel.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the
22 * - Neither the name of Intel Corporation nor the names of its
23 * contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * $FreeBSD: src/sys/crypto/aesni/aeskeys_amd64.S,v 1.2 2011/03/02 14:56:58 kib Exp $
42 #include <machine/asmacros.h>
46 ENTRY(_key_expansion_128)
49 pshufd $0b11111111,%xmm1,%xmm1
50 shufps $0b00010000,%xmm0,%xmm4
52 shufps $0b10001100,%xmm0,%xmm4
59 END(_key_expansion_128)
61 ENTRY(_key_expansion_192a)
63 pshufd $0b01010101,%xmm1,%xmm1
64 shufps $0b00010000,%xmm0,%xmm4
66 shufps $0b10001100,%xmm0,%xmm4
72 pshufd $0b11111111,%xmm0,%xmm3
76 shufps $0b01000100,%xmm0,%xmm6
78 shufps $0b01001110,%xmm2,%xmm1
79 movaps %xmm1,0x10(%rsi)
83 END(_key_expansion_192a)
85 ENTRY(_key_expansion_192b)
87 pshufd $0b01010101,%xmm1,%xmm1
88 shufps $0b00010000,%xmm0,%xmm4
90 shufps $0b10001100,%xmm0,%xmm4
95 pshufd $0b11111111,%xmm0,%xmm3
102 END(_key_expansion_192b)
104 ENTRY(_key_expansion_256b)
106 pshufd $0b10101010,%xmm1,%xmm1
107 shufps $0b00010000,%xmm2,%xmm4
109 shufps $0b10001100,%xmm2,%xmm4
116 END(_key_expansion_256b)
118 ENTRY(aesni_set_enckey)
120 movups (%rdi),%xmm0 # user key (first 16 bytes)
122 addq $0x10,%rsi # key addr
123 pxor %xmm4,%xmm4 # xmm4 is assumed 0 in _key_expansion_x
127 movups 0x10(%rdi),%xmm2 # other user key
130 // aeskeygenassist $0x1,%xmm2,%xmm1 # round 1
131 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x01
132 call _key_expansion_256a
133 // aeskeygenassist $0x1,%xmm0,%xmm1
134 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x01
135 call _key_expansion_256b
136 // aeskeygenassist $0x2,%xmm2,%xmm1 # round 2
137 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x02
138 call _key_expansion_256a
139 // aeskeygenassist $0x2,%xmm0,%xmm1
140 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x02
141 call _key_expansion_256b
142 // aeskeygenassist $0x4,%xmm2,%xmm1 # round 3
143 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x04
144 call _key_expansion_256a
145 // aeskeygenassist $0x4,%xmm0,%xmm1
146 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x04
147 call _key_expansion_256b
148 // aeskeygenassist $0x8,%xmm2,%xmm1 # round 4
149 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x08
150 call _key_expansion_256a
151 // aeskeygenassist $0x8,%xmm0,%xmm1
152 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x08
153 call _key_expansion_256b
154 // aeskeygenassist $0x10,%xmm2,%xmm1 # round 5
155 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x10
156 call _key_expansion_256a
157 // aeskeygenassist $0x10,%xmm0,%xmm1
158 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x10
159 call _key_expansion_256b
160 // aeskeygenassist $0x20,%xmm2,%xmm1 # round 6
161 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x20
162 call _key_expansion_256a
163 // aeskeygenassist $0x20,%xmm0,%xmm1
164 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x20
165 call _key_expansion_256b
166 // aeskeygenassist $0x40,%xmm2,%xmm1 # round 7
167 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x40
168 call _key_expansion_256a
171 movq 0x10(%rdi),%xmm2 # other user key
172 // aeskeygenassist $0x1,%xmm2,%xmm1 # round 1
173 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x01
174 call _key_expansion_192a
175 // aeskeygenassist $0x2,%xmm2,%xmm1 # round 2
176 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x02
177 call _key_expansion_192b
178 // aeskeygenassist $0x4,%xmm2,%xmm1 # round 3
179 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x04
180 call _key_expansion_192a
181 // aeskeygenassist $0x8,%xmm2,%xmm1 # round 4
182 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x08
183 call _key_expansion_192b
184 // aeskeygenassist $0x10,%xmm2,%xmm1 # round 5
185 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x10
186 call _key_expansion_192a
187 // aeskeygenassist $0x20,%xmm2,%xmm1 # round 6
188 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x20
189 call _key_expansion_192b
190 // aeskeygenassist $0x40,%xmm2,%xmm1 # round 7
191 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x40
192 call _key_expansion_192a
193 // aeskeygenassist $0x80,%xmm2,%xmm1 # round 8
194 .byte 0x66,0x0f,0x3a,0xdf,0xca,0x80
195 call _key_expansion_192b
198 // aeskeygenassist $0x1,%xmm0,%xmm1 # round 1
199 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x01
200 call _key_expansion_128
201 // aeskeygenassist $0x2,%xmm0,%xmm1 # round 2
202 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x02
203 call _key_expansion_128
204 // aeskeygenassist $0x4,%xmm0,%xmm1 # round 3
205 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x04
206 call _key_expansion_128
207 // aeskeygenassist $0x8,%xmm0,%xmm1 # round 4
208 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x08
209 call _key_expansion_128
210 // aeskeygenassist $0x10,%xmm0,%xmm1 # round 5
211 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x10
212 call _key_expansion_128
213 // aeskeygenassist $0x20,%xmm0,%xmm1 # round 6
214 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x20
215 call _key_expansion_128
216 // aeskeygenassist $0x40,%xmm0,%xmm1 # round 7
217 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x40
218 call _key_expansion_128
219 // aeskeygenassist $0x80,%xmm0,%xmm1 # round 8
220 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x80
221 call _key_expansion_128
222 // aeskeygenassist $0x1b,%xmm0,%xmm1 # round 9
223 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x1b
224 call _key_expansion_128
225 // aeskeygenassist $0x36,%xmm0,%xmm1 # round 10
226 .byte 0x66,0x0f,0x3a,0xdf,0xc8,0x36
227 call _key_expansion_128
230 END(aesni_set_enckey)
232 ENTRY(aesni_set_deckey)
243 // aesimc (%rdi),%xmm1
244 .byte 0x66,0x0f,0x38,0xdb,0x0f
255 END(aesni_set_deckey)