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