]> git.cryptolib.org Git - avr-crypto-lib.git/blob - arcfour-asm.S
78d0491ed0c78b3001d3b5ea28fe3a4cb66e4aae
[avr-crypto-lib.git] / arcfour-asm.S
1 /* 
2  * File:                arcfour-asm.S
3  * Author:      Daniel Otte
4  * Date:        07.06.2006
5  * License: GPL
6  * Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
7  * 
8  */
9  
10  /* +---+---+---------------------+
11  *  | i | j | ......<256>........ |
12  *  +---+---+---------------------+
13  */
14  
15 .global arcfour_init
16
17 ;== arcfour_init ==
18 ;  this function initialises the context
19 ; param1: 16-bit pointer to a ctx struct
20 ;       given in r25,r24
21 ; param2: 16-bit pointer to a key
22 ;       given in r23,r22
23 ; param1: 8-bit integer indicating keylength in byte
24 ;       given in        r20
25
26 arcfour_init:
27         push r29
28         push r28
29         push r2
30         
31         movw r26, r24   /* X points to ctx */
32         movw r30, r22   /* Z points to key */
33         st X+, r1
34         st X+, r1               /* X points to S */
35         
36 1:              
37         st X+, r1 
38         inc r1
39         brne 1b
40         
41         adiw r24, 2             /* r24:r25 points to S */
42         clr r21                 /* r21 is j */
43         mov r18, r20            /* r18 is keyindex counter */
44         clr r0
45 2:
46         movw r26, r24
47         ld r19, Z+
48         add r21, r19            /* j+= key[i%length] */
49         
50         add r26, r1
51         adc r27, r0
52         ld r19, X
53         add r21, r19            /* j += S[i] */
54         
55         dec r18         /* check the key-index counter */
56         brne 3f
57         movw r30, r22
58         mov r18, r20
59 3:      /* now swap(S[i], S[j]) */ /* r19 is still S[i] */
60         movw r28, r24 
61         add r28, r21
62         adc r29, r0             /* Y points to S[j]*/
63         ld r2, Y
64         st Y, r19
65         st X, r2        
66         inc r1
67         brne 2b 
68         
69         pop r2
70         pop r28
71         pop r29
72         ret
73
74 /*
75 uint8_t arcfour_gen(arcfour_ctx_t *c){
76         uint8_t t;
77         c->i++;
78         c->j += c->s[c->i];
79         t = c->s[c->j];
80         c->s[c->j] = c->s[c->i];
81         c->s[c->i] = t;
82         return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
83 }
84 */
85 .global arcfour_gen
86
87 ;== arcfour_gen ==
88 ;  this function initialises the context
89 ; param1: 16-bit pointer to a ctx struct
90 ;       given in r25,r24
91
92 arcfour_gen:
93         movw r26, r24
94         ld r18, X
95         inc r18
96         st X+, r18
97         movw r30, r26
98         ld r19, X+
99         add r26, r18
100         adc r27, r1
101         ld r20, X
102         add r19, r20
103         st Z+, r19              /* i,j loaded&saved; X->S[i]; Z->S[0]; r20=S[i] */
104         add r30, r19
105         adc r31, r1
106         ld r21, Z               /* X->S[i]; Z->S[j]; r20=S[i]; r21=S[j]*/
107         st Z, r20
108         st X, r21
109         add r20, r21
110         adiw r24, 2
111         movw r26, r24 /* X and Z point to S */
112         add r26, r20
113         adc r27, r1
114         ld r24, X
115         clr r25
116         ret
117
118
119
120
121
122