1210409Skib/*-
2210409Skib* The white paper of AES-NI instructions can be downloaded from:
3210409Skib *   http://softwarecommunity.intel.com/isn/downloads/intelavx/AES-Instructions-Set_WP.pdf
4210409Skib *
5210409Skib * Copyright (C) 2008-2010, Intel Corporation
6210409Skib *    Author: Huang Ying <ying.huang@intel.com>
7210409Skib *            Vinodh Gopal <vinodh.gopal@intel.com>
8210409Skib *            Kahraman Akdemir
9210409Skib *
10210409Skib * Redistribution and use in source and binary forms, with or without
11210409Skib * modification, are permitted provided that the following
12210409Skib * conditions are met:
13210409Skib *
14210409Skib * - Redistributions of source code must retain the above copyright
15210409Skib *   notice, this list of conditions and the following disclaimer.
16210409Skib *
17210409Skib * - Redistributions in binary form must reproduce the above copyright
18210409Skib *   notice, this list of conditions and the following disclaimer in the
19210409Skib *   documentation and/or other materials provided with the
20210409Skib *   distribution.
21210409Skib *
22210409Skib * - Neither the name of Intel Corporation nor the names of its
23210409Skib *   contributors may be used to endorse or promote products
24210409Skib *   derived from this software without specific prior written
25210409Skib *   permission.
26210409Skib *
27210409Skib * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28210409Skib * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29210409Skib * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30210409Skib * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31210409Skib * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32210409Skib * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33210409Skib * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34210409Skib * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35210409Skib * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36210409Skib * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37210409Skib * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38210409Skib */
39210409Skib
40210409Skib#include <machine/asmacros.h>
41210409Skib
42210409Skib	.text
43210409Skib
44210409SkibENTRY(_key_expansion_128)
45210409Skib_key_expansion_256a:
46210409Skib	.cfi_startproc
47210409Skib	pshufd	$0b11111111,%xmm1,%xmm1
48210409Skib	shufps	$0b00010000,%xmm0,%xmm4
49210409Skib	pxor	%xmm4,%xmm0
50210409Skib	shufps	$0b10001100,%xmm0,%xmm4
51210409Skib	pxor	%xmm4,%xmm0
52210409Skib	pxor	%xmm1,%xmm0
53210409Skib	movaps	%xmm0,(%rsi)
54210409Skib	addq	$0x10,%rsi
55210409Skib	retq
56210409Skib	.cfi_endproc
57210409SkibEND(_key_expansion_128)
58210409Skib
59210409SkibENTRY(_key_expansion_192a)
60210409Skib	.cfi_startproc
61210409Skib	pshufd	$0b01010101,%xmm1,%xmm1
62210409Skib	shufps	$0b00010000,%xmm0,%xmm4
63210409Skib	pxor	%xmm4,%xmm0
64210409Skib	shufps	$0b10001100,%xmm0,%xmm4
65210409Skib	pxor	%xmm4,%xmm0
66210409Skib	pxor	%xmm1,%xmm0
67210409Skib	movaps	%xmm2,%xmm5
68210409Skib	movaps	%xmm2,%xmm6
69210409Skib	pslldq	$4,%xmm5
70210409Skib	pshufd	$0b11111111,%xmm0,%xmm3
71210409Skib	pxor	%xmm3,%xmm2
72210409Skib	pxor	%xmm5,%xmm2
73210409Skib	movaps	%xmm0,%xmm1
74210409Skib	shufps	$0b01000100,%xmm0,%xmm6
75210409Skib	movaps	%xmm6,(%rsi)
76210409Skib	shufps	$0b01001110,%xmm2,%xmm1
77210409Skib	movaps	%xmm1,0x10(%rsi)
78210409Skib	addq	$0x20,%rsi
79210409Skib	retq
80210409Skib	.cfi_endproc
81210409SkibEND(_key_expansion_192a)
82210409Skib
83210409SkibENTRY(_key_expansion_192b)
84210409Skib	.cfi_startproc
85210409Skib	pshufd	$0b01010101,%xmm1,%xmm1
86210409Skib	shufps	$0b00010000,%xmm0,%xmm4
87210409Skib	pxor	%xmm4,%xmm0
88210409Skib	shufps	$0b10001100,%xmm0,%xmm4
89210409Skib	pxor	%xmm4,%xmm0
90210409Skib	pxor	%xmm1,%xmm0
91210409Skib	movaps	%xmm2,%xmm5
92210409Skib	pslldq	$4,%xmm5
93210409Skib	pshufd	$0b11111111,%xmm0,%xmm3
94210409Skib	pxor	%xmm3,%xmm2
95210409Skib	pxor	%xmm5,%xmm2
96210409Skib	movaps	%xmm0,(%rsi)
97210409Skib	addq	$0x10,%rsi
98210409Skib	retq
99210409Skib	.cfi_endproc
100210409SkibEND(_key_expansion_192b)
101210409Skib
102210409SkibENTRY(_key_expansion_256b)
103210409Skib	.cfi_startproc
104210409Skib	pshufd	$0b10101010,%xmm1,%xmm1
105210409Skib	shufps	$0b00010000,%xmm2,%xmm4
106210409Skib	pxor	%xmm4,%xmm2
107210409Skib	shufps	$0b10001100,%xmm2,%xmm4
108210409Skib	pxor	%xmm4,%xmm2
109210409Skib	pxor	%xmm1,%xmm2
110210409Skib	movaps	%xmm2,(%rsi)
111210409Skib	addq	$0x10,%rsi
112210409Skib	retq
113210409Skib	.cfi_endproc
114210409SkibEND(_key_expansion_256b)
115210409Skib
116210409SkibENTRY(aesni_set_enckey)
117210409Skib	.cfi_startproc
118210409Skib	movups	(%rdi),%xmm0		# user key (first 16 bytes)
119210409Skib	movaps	%xmm0,(%rsi)
120210409Skib	addq	$0x10,%rsi		# key addr
121210409Skib	pxor	%xmm4,%xmm4		# xmm4 is assumed 0 in _key_expansion_x
122210409Skib	cmpl	$12,%edx
123210409Skib	jb	.Lenc_key128
124210409Skib	je	.Lenc_key192
125210409Skib	movups	0x10(%rdi),%xmm2	# other user key
126210409Skib	movaps	%xmm2,(%rsi)
127210409Skib	addq	$0x10,%rsi
128255187Sjmg	aeskeygenassist $0x1,%xmm2,%xmm1	# round 1
129210409Skib	call	_key_expansion_256a
130255187Sjmg	aeskeygenassist $0x1,%xmm0,%xmm1
131210409Skib	call	_key_expansion_256b
132255187Sjmg	aeskeygenassist $0x2,%xmm2,%xmm1	# round 2
133210409Skib	call	_key_expansion_256a
134255187Sjmg	aeskeygenassist $0x2,%xmm0,%xmm1
135210409Skib	call	_key_expansion_256b
136255187Sjmg	aeskeygenassist $0x4,%xmm2,%xmm1	# round 3
137210409Skib	call	_key_expansion_256a
138255187Sjmg	aeskeygenassist $0x4,%xmm0,%xmm1
139210409Skib	call	_key_expansion_256b
140255187Sjmg	aeskeygenassist $0x8,%xmm2,%xmm1	# round 4
141210409Skib	call	_key_expansion_256a
142255187Sjmg	aeskeygenassist $0x8,%xmm0,%xmm1
143210409Skib	call	_key_expansion_256b
144255187Sjmg	aeskeygenassist $0x10,%xmm2,%xmm1	# round 5
145210409Skib	call	_key_expansion_256a
146255187Sjmg	aeskeygenassist $0x10,%xmm0,%xmm1
147210409Skib	call	_key_expansion_256b
148255187Sjmg	aeskeygenassist $0x20,%xmm2,%xmm1	# round 6
149210409Skib	call	_key_expansion_256a
150255187Sjmg	aeskeygenassist $0x20,%xmm0,%xmm1
151210409Skib	call	_key_expansion_256b
152255187Sjmg	aeskeygenassist $0x40,%xmm2,%xmm1	# round 7
153210409Skib	call	_key_expansion_256a
154210409Skib	retq
155210409Skib.Lenc_key192:
156210409Skib	movq	0x10(%rdi),%xmm2		# other user key
157255187Sjmg	aeskeygenassist $0x1,%xmm2,%xmm1	# round 1
158210409Skib	call	_key_expansion_192a
159255187Sjmg	aeskeygenassist $0x2,%xmm2,%xmm1	# round 2
160210409Skib	call	_key_expansion_192b
161255187Sjmg	aeskeygenassist $0x4,%xmm2,%xmm1	# round 3
162210409Skib	call	_key_expansion_192a
163255187Sjmg	aeskeygenassist $0x8,%xmm2,%xmm1	# round 4
164210409Skib	call	_key_expansion_192b
165255187Sjmg	aeskeygenassist $0x10,%xmm2,%xmm1	# round 5
166210409Skib	call	_key_expansion_192a
167255187Sjmg	aeskeygenassist $0x20,%xmm2,%xmm1	# round 6
168210409Skib	call	_key_expansion_192b
169255187Sjmg	aeskeygenassist $0x40,%xmm2,%xmm1	# round 7
170210409Skib	call	_key_expansion_192a
171255187Sjmg	aeskeygenassist $0x80,%xmm2,%xmm1	# round 8
172210409Skib	call	_key_expansion_192b
173210409Skib	retq
174210409Skib.Lenc_key128:
175255187Sjmg	aeskeygenassist $0x1,%xmm0,%xmm1	# round 1
176210409Skib	call	_key_expansion_128
177255187Sjmg	aeskeygenassist $0x2,%xmm0,%xmm1	# round 2
178210409Skib	call	_key_expansion_128
179255187Sjmg	aeskeygenassist $0x4,%xmm0,%xmm1	# round 3
180210409Skib	call	_key_expansion_128
181255187Sjmg	aeskeygenassist $0x8,%xmm0,%xmm1	# round 4
182210409Skib	call	_key_expansion_128
183255187Sjmg	aeskeygenassist $0x10,%xmm0,%xmm1	# round 5
184210409Skib	call	_key_expansion_128
185255187Sjmg	aeskeygenassist $0x20,%xmm0,%xmm1	# round 6
186210409Skib	call	_key_expansion_128
187255187Sjmg	aeskeygenassist $0x40,%xmm0,%xmm1	# round 7
188210409Skib	call	_key_expansion_128
189255187Sjmg	aeskeygenassist $0x80,%xmm0,%xmm1	# round 8
190210409Skib	call	_key_expansion_128
191255187Sjmg	aeskeygenassist $0x1b,%xmm0,%xmm1	# round 9
192210409Skib	call	_key_expansion_128
193255187Sjmg	aeskeygenassist $0x36,%xmm0,%xmm1	# round 10
194210409Skib	call	_key_expansion_128
195210409Skib	retq
196210409Skib	.cfi_endproc
197210409SkibEND(aesni_set_enckey)
198210409Skib
199210409SkibENTRY(aesni_set_deckey)
200210409Skib	.cfi_startproc
201210409Skib	movslq	%edx,%rax
202210409Skib	shlq	$4,%rax
203210409Skib	addq	%rax,%rdi
204210409Skib	movdqa	(%rdi),%xmm0
205210409Skib	movdqa	%xmm0,(%rsi)
206210409Skib	decl	%edx
207210409Skib1:
208210409Skib	addq	$0x10,%rsi
209210409Skib	subq	$0x10,%rdi
210255187Sjmg	aesimc	(%rdi),%xmm1
211210409Skib	movdqa	%xmm1,(%rsi)
212210409Skib	decl	%edx
213210409Skib	jne	1b
214210409Skib
215210409Skib	addq	$0x10,%rsi
216210409Skib	subq	$0x10,%rdi
217210409Skib	movdqa	(%rdi),%xmm0
218210409Skib	movdqa	%xmm0,(%rsi)
219210409Skib	retq
220210409Skib	.cfi_endproc
221210409SkibEND(aesni_set_deckey)
222210409Skib
223210409Skib	.ident	"$FreeBSD$"
224