Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / x86 / aes-decrypt-internal.asm
blob64e59283b225ec5c21f99770f1f79f60104828c5
1 C nettle, low-level cryptographics library
2 C
3 C Copyright (C) 2001, 2002, 2005 Rafael R. Sevilla, Niels Möller
4 C
5 C The nettle library is free software; you can redistribute it and/or modify
6 C it under the terms of the GNU Lesser General Public License as published by
7 C the Free Software Foundation; either version 2.1 of the License, or (at your
8 C option) any later version.
9 C
10 C The nettle library is distributed in the hope that it will be useful, but
11 C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 C or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13 C License for more details.
15 C You should have received a copy of the GNU Lesser General Public License
16 C along with the nettle library; see the file COPYING.LIB. If not, write to
17 C the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 C MA 02111-1301, USA.
20 include_src(<x86/aes.m4>)
22 C Register usage:
24 C AES state
25 define(<SA>,<%eax>)
26 define(<SB>,<%ebx>)
27 define(<SC>,<%ecx>)
28 define(<SD>,<%edx>)
30 C Primary use of these registers. They're also used temporarily for other things.
31 define(<T>,<%ebp>)
32 define(<TMP>,<%edi>)
33 define(<KEY>,<%esi>)
35 define(<FRAME_CTX>, <40(%esp)>)
36 define(<FRAME_TABLE>, <44(%esp)>)
37 define(<FRAME_LENGTH>, <48(%esp)>)
38 define(<FRAME_DST>, <52(%esp)>)
39 define(<FRAME_SRC>, <56(%esp)>)
41 define(<FRAME_KEY>, <16(%esp)>)
42 define(<FRAME_COUNT>, <12(%esp)>)
43 define(<TA>, <8(%esp)>)
44 define(<TB>, <4(%esp)>)
45 define(<TC>, <(%esp)>)
47 C The aes state is kept in %eax, %ebx, %ecx and %edx
49 C %esi is used as temporary, to point to the input, and to the
50 C subkeys, etc.
52 C %ebp is used as the round counter, and as a temporary in the final round.
54 C %edi is a temporary, often used as an accumulator.
56 .file "aes-decrypt-internal.asm"
58 C _aes_decrypt(struct aes_context *ctx,
59 C const struct aes_table *T,
60 C unsigned length, uint8_t *dst,
61 C uint8_t *src)
62 .text
63 ALIGN(16)
64 PROLOGUE(_nettle_aes_decrypt)
65 C save all registers that need to be saved
66 pushl %ebx C 20(%esp)
67 pushl %ebp C 16(%esp)
68 pushl %esi C 12(%esp)
69 pushl %edi C 8(%esp)
71 subl $20, %esp C loop counter and save area for the key pointer
73 movl FRAME_LENGTH, %ebp
74 testl %ebp,%ebp
75 jz .Lend
77 shrl $4, FRAME_LENGTH
79 .Lblock_loop:
80 movl FRAME_CTX,KEY C address of context struct ctx
82 movl FRAME_SRC,TMP C address of plaintext
83 AES_LOAD(SA, SB, SC, SD, TMP, KEY)
84 addl $16, FRAME_SRC C Increment src pointer
85 movl FRAME_TABLE, T
87 C get number of rounds to do from ctx struct
88 movl AES_NROUNDS (KEY),TMP
89 subl $1,TMP
91 C Loop counter on stack
92 movl TMP, FRAME_COUNT
94 addl $16,KEY C point to next key
95 movl KEY,FRAME_KEY
96 ALIGN(16)
97 .Lround_loop:
98 AES_ROUND(T, SA,SD,SC,SB, TMP, KEY)
99 movl TMP, TA
101 AES_ROUND(T, SB,SA,SD,SC, TMP, KEY)
102 movl TMP, TB
104 AES_ROUND(T, SC,SB,SA,SD, TMP, KEY)
105 movl TMP, TC
107 AES_ROUND(T, SD,SC,SB,SA, SD, KEY)
109 movl TA, SA
110 movl TB, SB
111 movl TC, SC
113 movl FRAME_KEY, KEY
115 xorl (KEY),SA C add current session key to plaintext
116 xorl 4(KEY),SB
117 xorl 8(KEY),SC
118 xorl 12(KEY),SD
119 addl $16,FRAME_KEY C point to next key
120 decl FRAME_COUNT
121 jnz .Lround_loop
123 C last round
125 AES_FINAL_ROUND(SA,SD,SC,SB,T, TMP, KEY)
126 movl TMP, TA
128 AES_FINAL_ROUND(SB,SA,SD,SC,T, TMP, KEY)
129 movl TMP, TB
131 AES_FINAL_ROUND(SC,SB,SA,SD,T, TMP, KEY)
132 movl TMP, TC
134 AES_FINAL_ROUND(SD,SC,SB,SA,T, SD, KEY)
136 movl TA, SA
137 movl TB, SB
138 movl TC, SC
140 C Inverse S-box substitution
141 mov $3,TMP
142 .Lsubst:
143 AES_SUBST_BYTE(SA,SB,SC,SD,T, KEY)
145 decl TMP
146 jnz .Lsubst
148 C Add last subkey, and store decrypted data
149 movl FRAME_DST,TMP
150 movl FRAME_KEY, KEY
151 AES_STORE(SA,SB,SC,SD, KEY, TMP)
153 addl $16, FRAME_DST C Increment destination pointer
154 decl FRAME_LENGTH
156 jnz .Lblock_loop
158 .Lend:
159 addl $20, %esp
160 popl %edi
161 popl %esi
162 popl %ebp
163 popl %ebx
165 EPILOGUE(_nettle_aes_decrypt)