aes-encrypt-internal.asm 3.08 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
C nettle, low-level cryptographics library
C 
Niels Möller's avatar
Niels Möller committed
3
C Copyright (C) 2001, 2002, 2005, 2008 Rafael R. Sevilla, Niels Möller
Niels Möller's avatar
Niels Möller committed
4
5
6
7
8
9
10
11
12
13
14
15
16
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
17
18
C the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
C MA 02111-1301, USA.
Niels Möller's avatar
Niels Möller committed
19

20
include_src(<x86_64/aes.m4>)
Niels Möller's avatar
Niels Möller committed
21
22
23
24
25
26

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
46
C Must correspond to an old-style register, for movzb from %ah--%dh to
C work.
47
define(<TMP>,<%rbp>)
48

Niels Möller's avatar
Niels Möller committed
49
50
51
52
53
54
55
	.file "aes-encrypt-internal.asm"
	
	C _aes_encrypt(struct aes_context *ctx, 
	C	       const struct aes_table *T,
	C	       unsigned length, uint8_t *dst,
	C	       uint8_t *src)
	.text
56
	ALIGN(16)
Niels Möller's avatar
Niels Möller committed
57
PROLOGUE(_nettle_aes_encrypt)
58
	W64_ENTRY(5, 0)
59
	test	PARAM_LENGTH, PARAM_LENGTH
Niels Möller's avatar
Niels Möller committed
60
61
62
63
64
65
66
67
68
69
	jz	.Lend

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

70
71
72
	mov	PARAM_DST, DST
	movl	PARAM_LENGTH, BLOCK_COUNT
	shrl	$4, BLOCK_COUNT
Niels Möller's avatar
Niels Möller committed
73
.Lblock_loop:
74
	mov	CTX,KEY
Niels Möller's avatar
Niels Möller committed
75
76
	
	AES_LOAD(SA, SB, SC, SD, SRC, KEY)
77
	add	$16, SRC	C Increment src pointer
Niels Möller's avatar
Niels Möller committed
78
79
80
81
82

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

83
	add	$16,KEY		C  point to next key
84
	ALIGN(16)
Niels Möller's avatar
Niels Möller committed
85
.Lround_loop:
86
87
88
	AES_ROUND(TABLE, SA,SB,SC,SD, TA, TMP)
	AES_ROUND(TABLE, SB,SC,SD,SA, TB, TMP)
	AES_ROUND(TABLE, SC,SD,SA,SB, TC, TMP)
89
	AES_ROUND(TABLE, SD,SA,SB,SC, SD, TMP)
Niels Möller's avatar
Niels Möller committed
90

91
92
93
	movl	TA, SA
	movl	TB, SB
	movl	TC, SC
Niels Möller's avatar
Niels Möller committed
94

95
96
97
98
	xorl	(KEY),SA	C  add current session key to plaintext
	xorl	4(KEY),SB
	xorl	8(KEY),SC
	xorl	12(KEY),SD
Niels Möller's avatar
Niels Möller committed
99

100
	add	$16,KEY	C  point to next key
Niels Möller's avatar
Niels Möller committed
101
102
103
	decl	COUNT
	jnz	.Lround_loop

104
105
106
107
108
	C last round
	AES_FINAL_ROUND(SA,SB,SC,SD, TABLE, TA, TMP)
	AES_FINAL_ROUND(SB,SC,SD,SA, TABLE, TB, TMP)
	AES_FINAL_ROUND(SC,SD,SA,SB, TABLE, TC, TMP)
	AES_FINAL_ROUND(SD,SA,SB,SC, TABLE, SD, TMP)
Niels Möller's avatar
Niels Möller committed
109
110

	C S-box substitution
Niels Möller's avatar
Niels Möller committed
111
	mov	$3, COUNT
Niels Möller's avatar
Niels Möller committed
112
.Lsubst:
113
	AES_SUBST_BYTE(TA,TB,TC,SD, TABLE, TMP)
Niels Möller's avatar
Niels Möller committed
114
115
116
117
118

	decl	COUNT
	jnz	.Lsubst

	C Add last subkey, and store encrypted data
119
	AES_STORE(TA,TB,TC,SD, KEY, DST)
Niels Möller's avatar
Niels Möller committed
120
	
121
	add	$16, DST
122
	decl	BLOCK_COUNT
Niels Möller's avatar
Niels Möller committed
123
124
125
126
127
128
129
130
131
132

	jnz	.Lblock_loop

	pop	%r15	
	pop	%r14
	pop	%r13
	pop	%r12
	pop	%rbp
	pop	%rbx
.Lend:
133
	W64_EXIT(5, 0)
Niels Möller's avatar
Niels Möller committed
134
135
	ret
EPILOGUE(_nettle_aes_encrypt)