]> git.cryptolib.org Git - avr-crypto-lib.git/blob - arcfour.c
863b7ac8782a83519895379d72b9ba7f746dde32
[avr-crypto-lib.git] / arcfour.c
1 /* 
2  * File:                arcfour.c
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 #include <stdint.h>
11 #include "arcfour.h"
12
13 /*
14  * length is length of key in bytes!
15  */
16
17 void arcfour_init(arcfour_ctx_t *c, uint8_t *key, uint8_t length){
18         uint8_t t;
19         unsigned x,y=0;
20         for(x=0; x<= 255; ++x)
21                 c->s[x]=x;
22         
23         for(x=0; x<= 255; ++x){
24                 y += c->s[x] + key[x % length];
25                 y &= 0xff;
26                 t = c->s[y];
27                 c->s[y] = c->s[x];
28                 c->s[x] = t;
29         };
30                 
31         c->i = c->j = 0;
32 }
33
34 uint8_t arcfour_gen(arcfour_ctx_t *c){
35         uint8_t t;
36         c->i++;
37         c->j += c->s[c->i];
38         t = c->s[c->j];
39         c->s[c->j] = c->s[c->i];
40         c->s[c->i] = t;
41         return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
42 }
43