]> git.cryptolib.org Git - avr-crypto-lib.git/blob - arcfour/arcfour-asm.S
44cf6e23531a24d2b5773c49e30a650f94d13eb9
[avr-crypto-lib.git] / arcfour / arcfour-asm.S
1 /* arcfour-asm.S */
2 /*
3     This file is part of the AVR-Crypto-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 #include "avr-asm-macros.S"
31  
32  /* +---+---+---------------------+
33  *  | i | j | ......<256>........ |
34  *  +---+---+---------------------+
35  */
36  
37 .global arcfour_init
38 /*
39  *== arcfour_init ==
40  *  this function initialises the context
41  * param1: 16-bit pointer to the key
42  *      given in r24:r25
43  * param2: 8-bit integer indicating keylength in bits
44  *      given in r22:r23
45  * param3: 16-bit pointer to a ctx struct
46  *      given in r20:r21
47  */
48 arcfour_init:
49         push_ r28, r29
50         movw r26, r20   /* X points to ctx */
51         movw r30, r24   /* Z points to key */
52         st X+, r1
53         st X+, r1       /* X points to S */
54         movw r20, r26   /* store pointer to S in r21:r20 */
55         lsr r23
56         ror r22
57         lsr r23
58         ror r22
59         lsr r23
60         ror r22
61 1:              
62         st X+, r1 
63         inc r1
64         brne 1b
65         
66         movw r26, r20
67         add r22, r30         /* r18 is keyindex counter */
68         clr r0
69         clr r19
70 2:
71         ld r23, X
72         ld r18, Z+
73         add r19, r18
74         add r19, r23
75         movw r28, r20   /* load pointer to S in Y */
76         add r28, r19
77         adc r29, r1
78         ld r18, Y
79         st Y,  r23
80         st X+, r18
81         cp r30, r22
82         brne 3f
83         movw r30, r24
84 3:              
85         inc r0
86         brne 2b 
87         pop_ r29, r28
88         ret
89
90 /*
91 uint8_t arcfour_gen(arcfour_ctx_t *c){
92         uint8_t t;
93         c->i++;
94         c->j += c->s[c->i];
95         t = c->s[c->j];
96         c->s[c->j] = c->s[c->i];
97         c->s[c->i] = t;
98         return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
99 }
100 */
101 .global arcfour_gen
102
103 ;== arcfour_gen ==
104 ; this function generates a keystream byte
105 ; param1: 16-bit pointer to a ctx struct
106 ;       given in r25,r24
107
108 arcfour_gen:
109         movw r26, r24
110         ld r18, X
111         inc r18
112         st X+, r18
113         movw r30, r26
114         ld r19, X+
115         add r26, r18
116         adc r27, r1
117         ld r20, X
118         add r19, r20
119         st Z+, r19              /* i,j loaded&saved; X->S[i]; Z->S[0]; r20=S[i] */
120         add r30, r19
121         adc r31, r1
122         ld r21, Z               /* X->S[i]; Z->S[j]; r20=S[i]; r21=S[j] */
123         st Z, r20
124         st X, r21
125         add r20, r21
126         adiw r24, 2
127         movw r26, r24 /* X and Z point to S */
128         add r26, r20
129         adc r27, r1
130         ld r24, X
131         clr r25
132         ret
133
134
135
136
137
138