summaryrefslogtreecommitdiff
path: root/rc4.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>1999-10-27 13:42:43 +1000
committerDamien Miller <djm@mindrot.org>1999-10-27 13:42:43 +1000
commitd4a8b7e34dd619a4debf9a206c81db26d1402ea6 (patch)
treea47d770a2f790f40d18b0982d4e55fa7cfb1fa3b /rc4.c
Initial revision
Diffstat (limited to 'rc4.c')
-rw-r--r--rc4.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/rc4.c b/rc4.c
new file mode 100644
index 000000000..a426188aa
--- /dev/null
+++ b/rc4.c
@@ -0,0 +1,105 @@
1/*! \file rc4.c
2 \brief Source file for RC4 stream cipher routines
3 \author Damien Miller <djm@mindrot.org>
4 \version 0.0.0
5 \date 1999
6
7 A simple implementation of the RC4 stream cipher, based on the
8 description given in _Bruce Schneier's_ "Applied Cryptography"
9 2nd edition.
10
11 Copyright 1999 Damien Miller
12
13 Permission is hereby granted, free of charge, to any person
14 obtaining a copy of this software and associated documentation
15 files (the "Software"), to deal in the Software without
16 restriction, including without limitation the rights to use, copy,
17 modify, merge, publish, distribute, sublicense, and/or sell copies
18 of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be
22 included in all copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
25 KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
26 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
27 AND NONINFRINGEMENT. IN NO EVENT SHALL DAMIEN MILLER BE LIABLE
28 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
32 \warning None of these functions clears its memory after use. It
33 \warning is the responsability of the calling routines to ensure
34 \warning that any sensitive data (keystream, key or plaintext) is
35 \warning properly erased after use.
36
37 \warning The name "RC4" is trademarked in the United States,
38 \warning you may need to use "RC4 compatible" or "ARC4"
39 \warning (Alleged RC4).
40*/
41
42/* $Id: rc4.c,v 1.1.1.1 1999/10/26 05:48:13 damien Exp $ */
43
44#include "rc4.h"
45
46
47void rc4_key(rc4_t *r, unsigned char *key, int len)
48{
49 int t;
50
51 for(r->i = 0; r->i < 256; r->i++)
52 r->s[r->i] = r->i;
53
54 r->j = 0;
55 for(r->i = 0; r->i < 256; r->i++)
56 {
57 r->j = (r->j + r->s[r->i] + key[r->i % len]) % 256;
58 t = r->s[r->i];
59 r->s[r->i] = r->s[r->j];
60 r->s[r->j] = t;
61 }
62 r->i = r->j = 0;
63}
64
65void rc4_crypt(rc4_t *r, unsigned char *plaintext, int len)
66{
67 int t;
68 int c;
69
70 c = 0;
71 while(c < len)
72 {
73 r->i = (r->i + 1) % 256;
74 r->j = (r->j + r->s[r->i]) % 256;
75 t = r->s[r->i];
76 r->s[r->i] = r->s[r->j];
77 r->s[r->j] = t;
78
79 t = (r->s[r->i] + r->s[r->j]) % 256;
80
81 plaintext[c] ^= r->s[t];
82 c++;
83 }
84}
85
86void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len)
87{
88 int t;
89 int c;
90
91 c = 0;
92 while(c < len)
93 {
94 r->i = (r->i + 1) % 256;
95 r->j = (r->j + r->s[r->i]) % 256;
96 t = r->s[r->i];
97 r->s[r->i] = r->s[r->j];
98 r->s[r->j] = t;
99
100 t = (r->s[r->i] + r->s[r->j]) % 256;
101
102 buffer[c] = r->s[t];
103 c++;
104 }
105}