aes-decrypt-internal.asm 3.37 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
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
C nettle, low-level cryptographics library
C 
C Copyright (C) 2001, 2002, 2005, 2008 Rafael R. Sevilla, Niels Mller
C  
C The nettle library is free software; you can redistribute it and/or modify
C it under the terms of the GNU Lesser General Public License as published by
C the Free Software Foundation; either version 2.1 of the License, or (at your
C option) any later version.
C 
C The nettle library is distributed in the hope that it will be useful, but
C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
C or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
C License for more details.
C 
C You should have received a copy of the GNU Lesser General Public License
C along with the nettle library; see the file COPYING.LIB.  If not, write to
C the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
C MA 02111-1307, USA.

include_src(<x86_64/aes.m4>)

C Register usage:

C AES state, use two of them
define(<SA>,<%eax>)
define(<SB>,<%ebx>)
27
28
define(<SC>,<%ecx>)
define(<SD>,<%edx>)
Niels Möller's avatar
Niels Möller committed
29
30
31
32
33
34
35

define(<TA>,<%r10d>)
define(<TB>,<%r11d>)
define(<TC>,<%r12d>)

define(<CTX>,	<%rdi>)
define(<TABLE>,	<%rsi>)
36
37
define(<PARAM_LENGTH>,<%edx>)		C Length is only 32 bits
define(<PARAM_DST>,	<%rcx>)
Niels Möller's avatar
Niels Möller committed
38
39
define(<SRC>,	<%r8>)

40
define(<DST>, <%r9>) 
Niels Möller's avatar
Niels Möller committed
41
42
define(<KEY>,<%r14>)
define(<COUNT>,	<%r15d>)
43
define(<BLOCK_COUNT>, <%r13d>)
Niels Möller's avatar
Niels Möller committed
44

45
define(<TMP>,<%rbp>)
Niels Möller's avatar
Niels Möller committed
46
47
48
49
50
51
52
53
54
55

	.file "aes-decrypt-internal.asm"
	
	C _aes_decrypt(struct aes_context *ctx, 
	C	       const struct aes_table *T,
	C	       unsigned length, uint8_t *dst,
	C	       uint8_t *src)
	.text
	ALIGN(4)
PROLOGUE(_nettle_aes_decrypt)
56
	test	PARAM_LENGTH, PARAM_LENGTH
Niels Möller's avatar
Niels Möller committed
57
58
59
60
61
62
63
64
65
66
	jz	.Lend

        C save all registers that need to be saved
	push	%rbx
	push	%rbp
	push	%r12
	push	%r13
	push	%r14
	push	%r15	

67
68
69
	mov	PARAM_DST, DST
	movl	PARAM_LENGTH, BLOCK_COUNT
	shrl	$4, BLOCK_COUNT
Niels Möller's avatar
Niels Möller committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
.Lblock_loop:
	mov	CTX,KEY
	
	AES_LOAD(SA, SB, SC, SD, SRC, KEY)
	add	$16, SRC	C Increment src pointer

	C  get number of rounds to do from ctx struct	
	movl	AES_NROUNDS (CTX), COUNT
	shrl	$1, COUNT
	subl	$1, COUNT

	add	$16,KEY		C  point to next key
	ALIGN(4)
.Lround_loop:
	AES_ROUND(TABLE, SA,SD,SC,SB, TA, TMP)
	xorl	(KEY), TA

	AES_ROUND(TABLE, SB,SA,SD,SC, TB, TMP)
	xorl	4(KEY),TB

	AES_ROUND(TABLE, SC,SB,SA,SD, TC, TMP)
	xorl	8(KEY),TC

93
94
	AES_ROUND(TABLE, SD,SC,SB,SA, SD, TMP)
	xorl	12(KEY),SD
Niels Möller's avatar
Niels Möller committed
95

96
	AES_ROUND(TABLE, TA,SD,TC,TB, SA, TMP)
Niels Möller's avatar
Niels Möller committed
97
98
	xorl	16(KEY), SA

99
	AES_ROUND(TABLE, TB,TA,SD,TC, SB, TMP)
Niels Möller's avatar
Niels Möller committed
100
101
	xorl	20(KEY),SB

102
	AES_ROUND(TABLE, TC,TB,TA,SD, SC, TMP)
Niels Möller's avatar
Niels Möller committed
103
104
	xorl	24(KEY),SC

105
	AES_ROUND(TABLE, SD,TC,TB,TA, SD, TMP)
Niels Möller's avatar
Niels Möller committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	xorl	28(KEY),SD
	
	add	$32,KEY	C  point to next key
	decl	COUNT
	jnz	.Lround_loop

	C last two rounds

	AES_ROUND(TABLE, SA,SD,SC,SB, TA, TMP)
	xorl	(KEY), TA

	AES_ROUND(TABLE, SB,SA,SD,SC, TB, TMP)
	xorl	4(KEY),TB

	AES_ROUND(TABLE, SC,SB,SA,SD, TC, TMP)
	xorl	8(KEY),TC

123
124
	AES_ROUND(TABLE, SD,SC,SB,SA, SD, TMP)
	xorl	12(KEY),SD
Niels Möller's avatar
Niels Möller committed
125

126
127
128
129
	AES_FINAL_ROUND(TA,SD,TC,TB, TABLE, SA, TMP)
	AES_FINAL_ROUND(TB,TA,SD,TC, TABLE, SB, TMP)
	AES_FINAL_ROUND(TC,TB,TA,SD, TABLE, SC, TMP)
	AES_FINAL_ROUND(SD,TC,TB,TA, TABLE, SD, TMP)
Niels Möller's avatar
Niels Möller committed
130
131
132
133
134
135
136
137
138
139
140
141
142

	C Inverse S-box substitution
	mov	$3, COUNT
.Lsubst:
	AES_SUBST_BYTE(SA,SB,SC,SD, TABLE, TMP)

	decl	COUNT
	jnz	.Lsubst

	C Add last subkey, and store decrypted data
	AES_STORE(SA,SB,SC,SD, KEY, DST)
	
	add	$16, DST
143
	decl	BLOCK_COUNT
Niels Möller's avatar
Niels Möller committed
144
145
146
147
148
149
150
151
152
153
154
155

	jnz	.Lblock_loop

	pop	%r15	
	pop	%r14
	pop	%r13
	pop	%r12
	pop	%rbp
	pop	%rbx
.Lend:
	ret
EPILOGUE(_nettle_aes_decrypt)