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