]> git.cryptolib.org Git - avr-crypto-lib.git/blob - aes/aes_enc-asm.S
new and more compact aes
[avr-crypto-lib.git] / aes / aes_enc-asm.S
1 /* aes_enc-asm.S */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008, 2009  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * \file     aes_enc-asm.S
21  * \email    daniel.otte@rub.de
22  * \author   Daniel Otte 
23  * \date     2009-01-10
24  * \license  GPLv3 or later
25  * 
26  */
27
28 #include "avr-asm-macros.S"
29
30
31 /*
32  * param a: r24
33  * param b: r22
34  * param reducer: r0
35  */
36 A = 28
37 B = 29
38 P = 0
39 xREDUCER = 25
40
41 .global aes256_enc
42 aes256_enc:
43         ldi r20, 14
44         rjmp aes_encrypt_core
45
46 .global aes192_enc
47 aes192_enc:
48         ldi r20, 12
49         rjmp aes_encrypt_core
50
51 .global aes128_enc
52 aes128_enc:
53         ldi r20, 10
54
55
56 /*
57   void aes_encrypt_core(aes_cipher_state_t *state, const aes_genctx_t *ks, uint8_t rounds)
58 */
59 T0= 2
60 T1= 3
61 T2= 4
62 T3= 5
63 SBOX_SAVE0 = 6
64 SBOX_SAVE1 = 7
65 ST00 =  8
66 ST01 =  9
67 ST02 = 10
68 ST03 = 11
69 ST10 = 12
70 ST11 = 13
71 ST12 = 14
72 ST13 = 15
73 ST20 = 16
74 ST21 = 17
75 ST22 = 18
76 ST23 = 19
77 ST30 = 20
78 ST31 = 21
79 ST32 = 22
80 ST33 = 23
81 CTR = 24
82 /*
83  * param state:  r24:r25
84  * param ks:     r22:r23
85  * param rounds: r20   
86  */
87 .global aes_encrypt_core
88 aes_encrypt_core:
89         push_range 2, 17
90         push r28
91         push r29
92         push r24
93         push r25
94         movw r26, r22
95         movw r30, r24
96         mov  CTR, r20
97         clt
98         
99         .irp row, 0, 1, 2, 3
100                 .irp col, 0, 1, 2, 3
101                         ld ST\row\col, Z+
102                 .endr
103         .endr
104         
105         ldi xREDUCER, 0x1b /* load reducer */
106         ldi r31, hi8(aes_sbox)
107         
108         /* key whitening */
109 1:
110
111         .irp row, 0, 1, 2, 3
112                 .irp col, 0, 1, 2, 3
113                         ld r0, X+
114                         eor ST\row\col, r0
115                 .endr
116         .endr
117         
118         brtc 2f
119 exit:
120         pop r31
121         pop r30
122         .irp row, 0, 1, 2, 3
123                         .irp col, 0, 1, 2, 3
124                         st Z+, ST\row\col
125                 .endr
126         .endr
127         pop r29
128         pop r28
129         pop_range 2, 17
130         ret
131
132 2:      dec CTR
133         brne 3f
134         set
135 3:
136
137         /* encryption loop */ 
138
139         /* SBOX substitution and shifting */
140         mov r30, ST00
141         lpm ST00, Z
142         mov r30, ST10
143         lpm ST10, Z
144         mov r30, ST20
145         lpm ST20, Z
146         mov r30, ST30
147         lpm ST30, Z
148
149         mov r30, ST01
150         lpm T0, Z
151         mov r30, ST11
152         lpm ST01, Z
153         mov r30, ST21
154         lpm ST11, Z
155         mov r30, ST31
156         lpm ST21, Z
157         mov ST31, T0
158
159         mov r30, ST02
160         lpm T0, Z
161         mov r30, ST12
162         lpm T1, Z
163         mov r30, ST22
164         lpm ST02, Z
165         mov r30, ST32
166         lpm ST12, Z
167         mov ST22, T0
168         mov ST32, T1
169
170         mov r30, ST03
171         lpm T0, Z
172         mov r30, ST33
173         lpm ST03, Z
174         mov r30, ST23
175         lpm ST33, Z
176         mov r30, ST13
177         lpm ST23, Z
178         mov ST13, T0
179                 
180         /* mixcols (or rows in our case) */
181         brtc 2f
182         rjmp 1b
183 2:      
184  /* mixrows */
185  .irp row, 0, 1, 2, 3
186         mov r0, ST\row\()2
187         eor r0, ST\row\()3
188         mov T2, r0
189
190         mov T0, ST\row\()0
191         eor ST\row\()0, ST\row\()1
192         eor r0, ST\row\()0
193         lsl ST\row\()0
194         brcc 3f
195         eor ST\row\()0, xREDUCER
196 3:      eor ST\row\()0, r0
197         eor ST\row\()0, T0
198
199         mov T1, ST\row\()1
200         eor T1, ST\row\()2
201         lsl T1
202         brcc 3f
203         eor T1, xREDUCER
204 3:      eor T1, r0
205         eor ST\row\()1, T1
206
207         lsl T2
208         brcc 3f
209         eor T2, xREDUCER
210 3:  eor T2, r0
211         eor ST\row\()2, T2
212
213         eor T0, ST\row\()3
214         lsl T0
215         brcc 3f
216         eor T0, xREDUCER
217 3:      eor T0, r0
218         eor ST\row\()3, T0
219
220 .endr
221         /* mix colums (rows) done */
222
223         /* add key*/
224         rjmp 1b
225         
226
227
228
229
230
231
232
233