]> git.cryptolib.org Git - avr-crypto-lib.git/blob - arcfour-asm.S
5fbf2a271837374b8d7c9f3f651bae9dfd189d0b
[avr-crypto-lib.git] / arcfour-asm.S
1 /* arcfour-asm.S */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  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 /* 
21  * File:        arcfour-asm.S
22  * Author:      Daniel Otte
23  * Date:        2006-07-06
24  * License:     GPLv3 or later
25  * Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
26  * 
27  */
28  
29 #include <avr/io.h>
30
31
32 .macro push_ p1:req, p2:vararg
33         push \p1
34 .ifnb \p2       
35         push_ \p2
36 .endif
37 .endm
38
39 .macro pop_ p1:req, p2:vararg
40         pop \p1
41 .ifnb \p2       
42         pop_ \p2
43 .endif
44 .endm
45
46 .macro push_range from:req, to:req
47         push \from
48 .if     \to-\from
49         push_range "(\from+1)",\to
50 .endif          
51 .endm
52
53 .macro pop_range from:req, to:req
54         pop \to
55 .if     \to-\from
56         pop_range \from,"(\to-1)"       
57 .endif
58 .endm
59
60 .macro stack_alloc size:req, reg1=r30, reg2=r31
61         in \reg1, _SFR_IO_ADDR(SPL)
62         in \reg2, _SFR_IO_ADDR(SPH)
63         sbiw r30, \size 
64         out  _SFR_IO_ADDR(SPH), \reg2
65         out  _SFR_IO_ADDR(SPL), \reg1
66 .endm
67
68 .macro stack_free size:req, reg1=r30, reg2=r31
69         in \reg1, _SFR_IO_ADDR(SPL)
70         in \reg2, _SFR_IO_ADDR(SPH)
71         adiw r30, \size 
72         out  _SFR_IO_ADDR(SPH), \reg2
73         out  _SFR_IO_ADDR(SPL), \reg1
74 .endm
75  
76  /* +---+---+---------------------+
77  *  | i | j | ......<256>........ |
78  *  +---+---+---------------------+
79  */
80  
81 .global arcfour_init
82 /*
83  *== arcfour_init ==
84  *  this function initialises the context
85  * param1: 16-bit pointer to the key
86  *      given in r24:r25
87  * param2: 8-bit integer indicating keylength in byte
88  *      given in r22
89  * param3: 16-bit pointer to a ctx struct
90  *      given in r20:r21
91  */
92 arcfour_init:
93         push_ r28, r29
94         movw r26, r20   /* X points to ctx */
95         movw r30, r24   /* Z points to key */
96         st X+, r1
97         st X+, r1       /* X points to S */
98         movw r20, r26   /* store pointer to S in r21:r20 */
99         
100 1:              
101         st X+, r1 
102         inc r1
103         brne 1b
104         
105         movw r26, r20
106         add r22, r30         /* r18 is keyindex counter */
107         clr r0
108         clr r19
109 2:
110         ld r23, X
111         ld r18, Z+
112         add r19, r18
113         add r19, r23
114         movw r28, r20   /* load pointer to S in Y */
115         add r28, r19
116         adc r29, r1
117         ld r18, Y
118         st Y,  r23
119         st X+, r18
120         cp r30, r22
121         brne 3f
122         movw r30, r24
123 3:              
124         inc r0
125         brne 2b 
126         pop_ r29, r28
127         ret
128
129 /*
130 uint8_t arcfour_gen(arcfour_ctx_t *c){
131         uint8_t t;
132         c->i++;
133         c->j += c->s[c->i];
134         t = c->s[c->j];
135         c->s[c->j] = c->s[c->i];
136         c->s[c->i] = t;
137         return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
138 }
139 */
140 .global arcfour_gen
141
142 ;== arcfour_gen ==
143 ;  this function initialises the context
144 ; param1: 16-bit pointer to a ctx struct
145 ;       given in r25,r24
146
147 arcfour_gen:
148         movw r26, r24
149         ld r18, X
150         inc r18
151         st X+, r18
152         movw r30, r26
153         ld r19, X+
154         add r26, r18
155         adc r27, r1
156         ld r20, X
157         add r19, r20
158         st Z+, r19              /* i,j loaded&saved; X->S[i]; Z->S[0]; r20=S[i] */
159         add r30, r19
160         adc r31, r1
161         ld r21, Z               /* X->S[i]; Z->S[j]; r20=S[i]; r21=S[j]*/
162         st Z, r20
163         st X, r21
164         add r20, r21
165         adiw r24, 2
166         movw r26, r24 /* X and Z point to S */
167         add r26, r20
168         adc r27, r1
169         ld r24, X
170         clr r25
171         ret
172
173
174
175
176
177