blob: 998a107741b592c1ee5bf62519c86c941ea40d56 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*! \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
Damien Millere413cba1999-10-28 14:12:54 +100044#include "config.h"
45
46#ifndef HAVE_ARC4RANDOM
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "rc4.h"
48
49
50void rc4_key(rc4_t *r, unsigned char *key, int len)
51{
52 int t;
53
54 for(r->i = 0; r->i < 256; r->i++)
55 r->s[r->i] = r->i;
56
57 r->j = 0;
58 for(r->i = 0; r->i < 256; r->i++)
59 {
60 r->j = (r->j + r->s[r->i] + key[r->i % len]) % 256;
61 t = r->s[r->i];
62 r->s[r->i] = r->s[r->j];
63 r->s[r->j] = t;
64 }
65 r->i = r->j = 0;
66}
67
68void rc4_crypt(rc4_t *r, unsigned char *plaintext, int len)
69{
70 int t;
71 int c;
72
73 c = 0;
74 while(c < len)
75 {
76 r->i = (r->i + 1) % 256;
77 r->j = (r->j + r->s[r->i]) % 256;
78 t = r->s[r->i];
79 r->s[r->i] = r->s[r->j];
80 r->s[r->j] = t;
81
82 t = (r->s[r->i] + r->s[r->j]) % 256;
83
84 plaintext[c] ^= r->s[t];
85 c++;
86 }
87}
88
89void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len)
90{
91 int t;
92 int c;
93
94 c = 0;
95 while(c < len)
96 {
97 r->i = (r->i + 1) % 256;
98 r->j = (r->j + r->s[r->i]) % 256;
99 t = r->s[r->i];
100 r->s[r->i] = r->s[r->j];
101 r->s[r->j] = t;
102
103 t = (r->s[r->i] + r->s[r->j]) % 256;
104
105 buffer[c] = r->s[t];
106 c++;
107 }
108}
Damien Millere413cba1999-10-28 14:12:54 +1000109#endif /* !HAVE_ARC4RANDOM */