3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
24 * License: GPLv3 or later
25 * Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
32 .macro push_ p1:req, p2:vararg
39 .macro pop_ p1:req, p2:vararg
46 .macro push_range from:req, to:req
49 push_range "(\from+1)",\to
53 .macro pop_range from:req, to:req
56 pop_range \from,"(\to-1)"
60 .macro stack_alloc size:req, reg1=r30, reg2=r31
61 in \reg1, _SFR_IO_ADDR(SPL)
62 in \reg2, _SFR_IO_ADDR(SPH)
64 out _SFR_IO_ADDR(SPH), \reg2
65 out _SFR_IO_ADDR(SPL), \reg1
68 .macro stack_free size:req, reg1=r30, reg2=r31
69 in \reg1, _SFR_IO_ADDR(SPL)
70 in \reg2, _SFR_IO_ADDR(SPH)
72 out _SFR_IO_ADDR(SPH), \reg2
73 out _SFR_IO_ADDR(SPL), \reg1
76 /* +---+---+---------------------+
77 * | i | j | ......<256>........ |
78 * +---+---+---------------------+
84 * this function initialises the context
85 * param1: 16-bit pointer to the key
87 * param2: 8-bit integer indicating keylength in byte
89 * param3: 16-bit pointer to a ctx struct
94 movw r26, r20 /* X points to ctx */
95 movw r30, r24 /* Z points to key */
97 st X+, r1 /* X points to S */
98 movw r20, r26 /* store pointer to S in r21:r20 */
106 clr r18 /* r18 is keyindex counter */
114 movw r28, r20 /* load pointer to S in Y */
132 uint8_t arcfour_gen(arcfour_ctx_t *c){
137 c->s[c->j] = c->s[c->i];
139 return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
145 ; this function initialises the context
146 ; param1: 16-bit pointer to a ctx struct
160 st Z+, r19 /* i,j loaded&saved; X->S[i]; Z->S[0]; r20=S[i] */
163 ld r21, Z /* X->S[i]; Z->S[j]; r20=S[i]; r21=S[j]*/
168 movw r26, r24 /* X and Z point to S */