]> git.cryptolib.org Git - avr-crypto-lib.git/blob - aes/aes_aleph_keyschedule-asm.S
new and more compact aes
[avr-crypto-lib.git] / aes / aes_aleph_keyschedule-asm.S
1 /* aes_keyschedule-asm */
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_keyschedule-asm.S
21  * \email    bg@nerilex.org
22  * \author   Daniel Otte 
23  * \date     2009-01-09
24  * \license  GPLv3 or later
25  * 
26  */
27
28 #include "avr-asm-macros.S"
29
30 .global aes256_init
31 aes256_init:
32         movw r20, r22
33         ldi r23, hi8(256)
34         ldi r22, lo8(256)
35         rjmp aes_init
36         
37 .global aes192_init
38 aes192_init:
39         movw r20, r22
40         ldi r23, hi8(192)
41         ldi r22, lo8(192)
42         rjmp aes_init
43         
44 .global aes128_init
45 aes128_init:
46         movw r20, r22
47         clr r23
48         ldi r22, 128
49
50 /* 
51 void aes_init(const void *key, uint16_t keysize_b, aes_genctx_t *ctx){
52         uint8_t hi,i,nk, next_nk;
53         uint8_t rc=1;
54         uint8_t tmp[4];
55         nk=keysize_b>>5; / * 4, 6, 8 * /
56         hi=4*(nk+6+1);
57         memcpy(ctx, key, keysize_b/8);
58         next_nk = nk;
59         for(i=nk;i<hi;++i){
60                 *((uint32_t*)tmp) = ((uint32_t*)(ctx->key[0].ks))[i-1];
61                 if(i!=next_nk){
62                         if(nk==8 && i%8==4){
63                                 tmp[0] = pgm_read_byte(aes_sbox+tmp[0]);
64                                 tmp[1] = pgm_read_byte(aes_sbox+tmp[1]);
65                                 tmp[2] = pgm_read_byte(aes_sbox+tmp[2]);
66                                 tmp[3] = pgm_read_byte(aes_sbox+tmp[3]);
67                         }
68                 } else {
69                         next_nk += nk;
70                         aes_rotword(tmp);
71                         tmp[0] = pgm_read_byte(aes_sbox+tmp[0]);
72                         tmp[1] = pgm_read_byte(aes_sbox+tmp[1]);
73                         tmp[2] = pgm_read_byte(aes_sbox+tmp[2]);
74                         tmp[3] = pgm_read_byte(aes_sbox+tmp[3]);
75                         tmp[0] ^= rc;
76                         rc<<=1;
77                 }
78                 ((uint32_t*)(ctx->key[0].ks))[i] = ((uint32_t*)(ctx->key[0].ks))[i-nk]
79                                                    ^ *((uint32_t*)tmp);
80         }
81 }
82 */
83
84 SBOX_SAVE0 = 14
85 SBOX_SAVE1 = 15
86 XRC = 17
87 NK = 22
88 C1 = 18
89 NEXT_NK = 19
90 HI = 23
91 T0 = 20
92 T1 = 21
93 T2 = 24
94 T3 = 25
95 /*
96  * param key:       r24:r25
97  * param keysize_b: r22:r23
98  * param ctx:       r20:r21
99  */
100 .global aes_init
101 aes_init:
102         push_range 14, 17
103         push r28
104         push r29
105         movw r30, r20
106         movw r28, r20
107         movw r26, r24
108         lsr r23
109         ror r22
110         lsr r22
111         lsr r22 /* r22 contains keysize_b/8 */
112         mov C1, r22
113
114 1:      /* copy key to ctx */ 
115         ld r0, X+
116         st Z+, r0
117         dec C1
118         brne 1b
119         
120         lsr NK
121         lsr NK
122         /* NK is now the number of 32-bit words in the supplied key */
123         bst NK, 3 /* set T if NK==8 */
124         mov NEXT_NK, NK
125         mov HI, NK
126         subi HI, -7 /* HI += 7 */
127         lsl HI
128         lsl HI
129         movw r26, r30
130         sbiw r26, 4
131         mov C1, NK
132         ldi XRC, 1
133 1:      
134         ld T0, X+
135         ld T1, X+
136         ld T2, X+
137         ld T3, X+
138         cp NEXT_NK, C1
139         breq 2f 
140         brtc 5f
141         mov r16, C1
142         andi r16, 0x07
143         cpi r16, 0x04
144         brne 5f
145         rcall substitute
146         rjmp 5f
147 2:
148         add NEXT_NK, NK
149         rcall substitute
150         mov r16, T0
151         mov T0, T1
152         mov T1, T2
153         mov T2, T3
154         mov T3, r16
155         eor T0, XRC
156         lsl XRC
157         brcc 3f
158         ldi XRC, 0x1b
159 3:
160 5:      
161         movw r30, r26
162
163         ld r0, Y+
164         eor r0, T0
165         st Z+, r0
166         ld r0, Y+
167         eor r0 ,T1
168         st Z+, r0
169         ld r0, Y+
170         eor r0, T2
171         st Z+, r0
172         ld r0, Y+
173         eor r0, T3
174         st Z+, r0
175         
176 /*
177         st Z+, T0
178         st Z+, T1
179         st Z+, T2
180         st Z+, T3
181 */              
182         
183         inc C1
184         cp C1, HI
185         breq 6f
186         rjmp 1b
187 6:      
188         
189         clt
190         pop r29
191         pop r28
192         pop_range 14, 17
193         ret
194         
195 substitute:
196         ldi r31, hi8(aes_sbox)
197         mov r30, T0
198         lpm T0, Z
199         mov r30, T1
200         lpm T1, Z
201         mov r30, T2
202         lpm T2, Z
203         mov r30, T3
204         lpm T3, Z
205         ret
206         
207