]> git.cryptolib.org Git - avr-crypto-lib.git/blob - arcfour/arcfour-asm.S
1ef8218ce971a608ffaf096959dc40fb63141804
[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 byte
44  *      given in r22
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         
56 1:              
57         st X+, r1 
58         inc r1
59         brne 1b
60         
61         movw r26, r20
62         add r22, r30         /* r18 is keyindex counter */
63         clr r0
64         clr r19
65 2:
66         ld r23, X
67         ld r18, Z+
68         add r19, r18
69         add r19, r23
70         movw r28, r20   /* load pointer to S in Y */
71         add r28, r19
72         adc r29, r1
73         ld r18, Y
74         st Y,  r23
75         st X+, r18
76         cp r30, r22
77         brne 3f
78         movw r30, r24
79 3:              
80         inc r0
81         brne 2b 
82         pop_ r29, r28
83         ret
84
85 /*
86 uint8_t arcfour_gen(arcfour_ctx_t *c){
87         uint8_t t;
88         c->i++;
89         c->j += c->s[c->i];
90         t = c->s[c->j];
91         c->s[c->j] = c->s[c->i];
92         c->s[c->i] = t;
93         return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
94 }
95 */
96 .global arcfour_gen
97
98 ;== arcfour_gen ==
99 ;  this function initialises the context
100 ; param1: 16-bit pointer to a ctx struct
101 ;       given in r25,r24
102
103 arcfour_gen:
104         movw r26, r24
105         ld r18, X
106         inc r18
107         st X+, r18
108         movw r30, r26
109         ld r19, X+
110         add r26, r18
111         adc r27, r1
112         ld r20, X
113         add r19, r20
114         st Z+, r19              /* i,j loaded&saved; X->S[i]; Z->S[0]; r20=S[i] */
115         add r30, r19
116         adc r31, r1
117         ld r21, Z               /* X->S[i]; Z->S[j]; r20=S[i]; r21=S[j]*/
118         st Z, r20
119         st X, r21
120         add r20, r21
121         adiw r24, 2
122         movw r26, r24 /* X and Z point to S */
123         add r26, r20
124         adc r27, r1
125         ld r24, X
126         clr r25
127         ret
128
129
130
131
132
133