]> git.cryptolib.org Git - avr-crypto-lib.git/blob - aes/aes_aleph_enc-asm.S
new and more compact aes
[avr-crypto-lib.git] / aes / aes_aleph_enc-asm.S
1 /* aes_enc-asm.S */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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    bg@nerilex.org
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 xtime:
32         lsl r24
33         brcc 1f
34         eor r24, r27
35 1:
36         ret
37
38 shift_offset_table:
39         .byte 12,  8,  4,  0
40         .byte  9,  5,  1, 13
41         .byte  6,  2, 14, 10
42         .byte  3, 15, 11,  7
43
44 .global aes256_enc
45 aes256_enc:
46         ldi r20, 14
47         rjmp aes_encrypt_core
48
49 .global aes192_enc
50 aes192_enc:
51         ldi r20, 12
52         rjmp aes_encrypt_core
53
54 .global aes128_enc
55 aes128_enc:
56         ldi r20, 10
57
58 /*
59   void aes_encrypt_core(aes_cipher_state_t *state, const aes_genctx_t *ks, uint8_t rounds)
60 */
61 /*
62  * param state:  r24:r25
63  * param ks:     r22:r23
64  * param rounds: r20   
65  */
66
67 .global aes_encrypt_core
68 aes_encrypt_core:
69         push r3
70         push r16
71         push r17
72         push r28
73         push r29
74         mov r3, r20
75         clt
76         movw r28, r24
77 x:
78         movw r24, r28
79 key_add:
80
81         clr r21
82         ldi r20, 16
83         call memxor
84         movw r22, r26 /* switch to next roundkey; r26 points after the end of src after memxor ;-) */
85
86         brtc sub_shift_bytes
87 4:
88         pop r29
89         pop r28
90         pop r17
91         pop r16
92         pop r3
93         ret
94
95 sub_shift_bytes:
96         ldi r30, lo8(shift_offset_table)
97         ldi r31, hi8(shift_offset_table)
98         ldi r20, 4   /* load counter for columns (rows in spec) */
99         movw r24, r28
100 1:
101         ldi r21, 4
102 2:
103         ld r16, Y
104         adiw r28, 4
105         push r16
106         dec r21
107         brne 2b
108
109         ldi r21, 4
110 2:
111         pop r16
112         movw r26, r24
113         lpm r0, Z+
114         add r26, r0
115         adc r27, r1
116         st X, r16
117         dec r21
118         brne 2b
119
120         sbiw r28, 15
121
122         dec r20
123         brne 1b
124
125         sbiw r28, 4 /* set Y back to the start of state */
126
127         dec r3
128         brne mix_rows
129         set
130
131 mix_rows:
132         ldi r31, hi8(aes_sbox)
133         ldi r27, 0x1B
134         ldi r20, 4
135 1:
136         ldd r30, Y+0
137         lpm r16, Z
138         ldd r30, Y+1
139         lpm r17, Z
140         ldd r30, Y+2
141         lpm r18, Z
142         ldd r30, Y+3
143         lpm r19, Z
144
145         brts 2f
146         mov r26, r16
147
148         mov r24, r16
149         eor r24, r17
150
151         mov r21, r24
152         eor r21, r18
153         eor r21, r19
154
155         rcall xtime
156         eor r16, r24
157         eor r16, r21
158
159         mov r24, r17
160         eor r24, r18
161         rcall xtime
162         eor r17, r24
163         eor r17, r21
164
165         mov r24, r18
166         eor r24, r19
167         rcall xtime
168         eor r18, r24
169         eor r18, r21
170
171         mov r24, r19
172         eor r24, r26
173         rcall xtime
174         eor r19, r24
175         eor r19, r21
176 2:
177         st Y+, r16
178         st Y+, r17
179         st Y+, r18
180         st Y+, r19
181         dec r20
182         brne 1b
183         sbiw r28, 16
184         rjmp x
185