Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / x86 / aes-encrypt-internal.asm
1 C nettle, low-level cryptographics library
2
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
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.
14
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.
19
20 include_src(<x86/aes.m4>)
21
22 C Register usage:
23
24 C AES state
25 define(<SA>,<%eax>)
26 define(<SB>,<%ebx>)
27 define(<SC>,<%ecx>)
28 define(<SD>,<%edx>)
29
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>)
34
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)>)
40
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)>)
46
47 C The aes state is kept in %eax, %ebx, %ecx and %edx
48 C
49 C %esi is used as temporary, to point to the input, and to the
50 C subkeys, etc.
51 C
52 C %ebp is used as the round counter, and as a temporary in the final round.
53 C
54 C %edi is a temporary, often used as an accumulator.
55
56         .file "aes-encrypt-internal.asm"
57         
58         C _aes_encrypt(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_encrypt)
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)
70
71         subl    $20, %esp       C  loop counter and save area for the key pointer
72
73         movl    FRAME_LENGTH, %ebp
74         testl   %ebp,%ebp
75         jz      .Lend
76
77         shrl    $4, FRAME_LENGTH
78
79 .Lblock_loop:
80         movl    FRAME_CTX,KEY   C  address of context struct ctx
81         
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
86
87         C  get number of rounds to do from ctx struct   
88         movl    AES_NROUNDS (KEY),TMP
89         subl    $1,TMP
90
91         C Loop counter on stack
92         movl    TMP, FRAME_COUNT
93
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,SB,SC,SD, TMP, KEY)
99         movl    TMP, TA
100
101         AES_ROUND(T, SB,SC,SD,SA, TMP, KEY)
102         movl    TMP, TB
103
104         AES_ROUND(T, SC,SD,SA,SB, TMP, KEY)
105         movl    TMP, TC
106
107         AES_ROUND(T, SD,SA,SB,SC, SD, KEY)
108         
109         movl    TA, SA
110         movl    TB, SB
111         movl    TC, SC
112         
113         movl    FRAME_KEY, KEY
114
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
122
123         C last round
124
125         AES_FINAL_ROUND(SA,SB,SC,SD, T, TMP, KEY)
126         movl    TMP, TA
127
128         AES_FINAL_ROUND(SB,SC,SD,SA, T, TMP, KEY)
129         movl    TMP, TB
130
131         AES_FINAL_ROUND(SC,SD,SA,SB, T, TMP, KEY)
132         movl    TMP, TC
133
134         AES_FINAL_ROUND(SD,SA,SB,SC, T, SD, KEY)
135
136         movl    TA, SA
137         movl    TB, SB
138         movl    TC, SC
139
140         C S-box substitution
141         mov     $3,TMP
142 .Lsubst:
143         AES_SUBST_BYTE(SA,SB,SC,SD, T, KEY)
144
145         decl    TMP
146         jnz     .Lsubst
147
148         C Add last subkey, and store encrypted data
149         movl    FRAME_DST,TMP
150         movl    FRAME_KEY, KEY
151         AES_STORE(SA,SB,SC,SD, KEY, TMP)
152         
153         addl    $16, FRAME_DST          C Increment destination pointer
154         decl    FRAME_LENGTH
155
156         jnz     .Lblock_loop
157
158 .Lend:
159         addl    $20, %esp
160         popl    %edi
161         popl    %esi
162         popl    %ebp
163         popl    %ebx
164         ret
165 EPILOGUE(_nettle_aes_encrypt)