-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc4.c
More file actions
121 lines (104 loc) · 3.95 KB
/
rc4.c
File metadata and controls
121 lines (104 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "LibRc4.h"
#include <stdlib.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define SwapBytes( Value1, Value2 ) \
{ \
uint8_t temp = Value1; \
Value1 = Value2; \
Value2 = temp; \
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Initialise
//
// Initialises an RC4 cipher and discards the specified number of first bytes.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Initialise
(
Rc4Context* Context,
void* Key,
uint32_t KeySize,
uint32_t DropN
)
{
uint32_t i;
uint32_t j;
uint32_t n;
// Setup key schedule
for( i=0; i<256; i++ )
{
Context->S[i] = (uint8_t)i;
}
j = 0;
for( i=0; i<256; i++ )
{
j = ( j + Context->S[i] + ((uint8_t*)Key)[i % KeySize] ) % 256;
SwapBytes( Context->S[i], Context->S[j] );
}
i = 0;
j = 0;
// Drop first bytes (if requested)
for( n=0; n<DropN; n++ )
{
i = ( i + 1 ) % 256;
j = ( j + Context->S[i] ) % 256;
SwapBytes( Context->S[i], Context->S[j] );
}
Context->i = i;
Context->j = j;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Output
//
// Outputs the requested number of bytes from the RC4 stream
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Output
(
Rc4Context* Context,
void* Buffer,
uint32_t Size
)
{
uint32_t n;
for( n=0; n<Size; n++ )
{
Context->i = ( Context->i + 1 ) % 256;
Context->j = ( Context->j + Context->S[Context->i] ) % 256;
SwapBytes( Context->S[Context->i], Context->S[Context->j] );
((uint8_t*)Buffer)[n] = Context->S[ (Context->S[Context->i] + Context->S[Context->j]) % 256 ];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Xor
//
// XORs the RC4 stream with an input buffer and puts the results in an output buffer. This is used for encrypting
// and decrypting data. InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Xor
(
Rc4Context* Context,
void* InBuffer,
void* OutBuffer,
uint32_t Size
)
{
uint32_t n;
for( n=0; n<Size; n++ )
{
Context->i = ( Context->i + 1 ) % 256;
Context->j = ( Context->j + Context->S[Context->i] ) % 256;
SwapBytes( Context->S[Context->i], Context->S[Context->j] );
((uint8_t*)OutBuffer)[n] = ((uint8_t*)InBuffer)[n]
^ ( Context->S[ (Context->S[Context->i] + Context->S[Context->j]) % 256 ] );
}
}