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