debug in trajectory manager
[aversive.git] / modules / crypto / rc4 / rc4.c
1 /* Droids-corp, Eirbot, Microb Technology 2005 - Zer0
2  * Inspired by the ARC4 implementation by Christophe Devine
3  * The original licence is below
4  * Implementation for RC4
5  */
6
7 /** \file rc4.c
8  *  \brief Implementation for the RC4 module.
9  *
10  *  \todo Test the module.
11  *
12  *  \test No tests for the moment.
13  *
14  * This module provides RC4 cryptographic functions.
15  */
16
17 /*
18  *  An implementation of the ARC4 algorithm
19  *
20  *  Copyright (C) 2001-2003  Christophe Devine
21  *
22  *  This program is free software; you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License as published by
24  *  the Free Software Foundation; either version 2 of the License, or
25  *  (at your option) any later version.
26  *
27  *  This program is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  You should have received a copy of the GNU General Public License
33  *  along with this program; if not, write to the Free Software
34  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35  */
36
37 #include <aversive.h>
38 #include "rc4.h"
39
40 struct rc4_state s;
41
42
43
44 void rc4_init(uint8_t *key,  uint8_t length )
45 {
46   uint8_t i, j, k, a;
47   uint8_t * m;
48
49   s.x = 0;
50   s.y = 0;
51   m = s.m;
52   
53   i=0;
54   m[i] = i;
55   for( i = 1; i !=0 ; i++ )
56     {
57       m[i] = i;
58     }
59   
60   j = k = 0;
61
62
63   i=0;
64   a = m[i];
65   j = (uint8_t) ( j + a + key[k] );
66   m[i] = m[j]; m[j] = a;
67   if( ++k >= length ) k = 0;
68
69   for( i = 1; i != 0 ; i++ )
70     {
71       a = m[i];
72       j = (uint8_t) ( j + a + key[k] );
73       m[i] = m[j]; m[j] = a;
74       if( ++k >= length ) k = 0;
75     }
76 }
77
78
79
80 uint8_t rc4_crypt_char(uint8_t data)
81
82     uint8_t a, b;
83
84     s.x = (uint8_t) ( s.x + 1 ); 
85     a = s.m[s.x];
86     s.y = (uint8_t) ( s.y + a );
87     s.m[s.x] = b = s.m[s.y];
88     s.m[s.y] = a;
89     data ^= s.m[(uint8_t) ( a + b )];
90
91     return data;
92 }
93