| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
/** |
|
2
|
|
|
|
|
|
|
* @file arc4.c |
|
3
|
|
|
|
|
|
|
* @version 950bba4 (HEAD -> master) |
|
4
|
|
|
|
|
|
|
* |
|
5
|
|
|
|
|
|
|
* ARC4 stream cipher implementation. |
|
6
|
|
|
|
|
|
|
*/ |
|
7
|
|
|
|
|
|
|
/* |
|
8
|
|
|
|
|
|
|
* Copyright (c) 2013-2017 INSIDE Secure Corporation |
|
9
|
|
|
|
|
|
|
* Copyright (c) PeerSec Networks, 2002-2011 |
|
10
|
|
|
|
|
|
|
* All Rights Reserved |
|
11
|
|
|
|
|
|
|
* |
|
12
|
|
|
|
|
|
|
* The latest version of this code is available at http://www.matrixssl.org |
|
13
|
|
|
|
|
|
|
* |
|
14
|
|
|
|
|
|
|
* This software is open source; you can redistribute it and/or modify |
|
15
|
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by |
|
16
|
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
17
|
|
|
|
|
|
|
* (at your option) any later version. |
|
18
|
|
|
|
|
|
|
* |
|
19
|
|
|
|
|
|
|
* This General Public License does NOT permit incorporating this software |
|
20
|
|
|
|
|
|
|
* into proprietary programs. If you are unable to comply with the GPL, a |
|
21
|
|
|
|
|
|
|
* commercial license for this software may be purchased from INSIDE at |
|
22
|
|
|
|
|
|
|
* http://www.insidesecure.com/ |
|
23
|
|
|
|
|
|
|
* |
|
24
|
|
|
|
|
|
|
* This program is distributed in WITHOUT ANY WARRANTY; without even the |
|
25
|
|
|
|
|
|
|
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
26
|
|
|
|
|
|
|
* See the GNU General Public License for more details. |
|
27
|
|
|
|
|
|
|
* |
|
28
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License |
|
29
|
|
|
|
|
|
|
* along with this program; if not, write to the Free Software |
|
30
|
|
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
31
|
|
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
|
32
|
|
|
|
|
|
|
*/ |
|
33
|
|
|
|
|
|
|
/******************************************************************************/ |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#include "../cryptoImpl.h" |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#ifdef USE_MATRIX_ARC4 |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
/******************************************************************************/ |
|
40
|
|
|
|
|
|
|
/* |
|
41
|
|
|
|
|
|
|
SECURITY |
|
42
|
|
|
|
|
|
|
Some accounts, such as O'Reilly's Secure Programming Cookbook say that no |
|
43
|
|
|
|
|
|
|
more than 2^30 bytes should be processed without rekeying, so we |
|
44
|
|
|
|
|
|
|
enforce that limit here. FYI, this is equal to 1GB of data transferred. |
|
45
|
|
|
|
|
|
|
*/ |
|
46
|
|
|
|
|
|
|
# define ARC4_MAX_BYTES 0x40000000 |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
/******************************************************************************/ |
|
49
|
|
|
|
|
|
|
/* |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
*/ |
|
52
|
0
|
|
|
|
|
|
int32_t psArc4Init(psArc4_t *arc4, const unsigned char *key, uint8_t keylen) |
|
53
|
|
|
|
|
|
|
{ |
|
54
|
|
|
|
|
|
|
unsigned char index1, index2, tmp, *state; |
|
55
|
|
|
|
|
|
|
short counter; |
|
56
|
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
arc4->byteCount = 0; |
|
58
|
0
|
|
|
|
|
|
state = &arc4->state[0]; |
|
59
|
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
for (counter = 0; counter < 256; counter++) |
|
61
|
|
|
|
|
|
|
{ |
|
62
|
0
|
|
|
|
|
|
state[counter] = (unsigned char) counter; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
0
|
|
|
|
|
|
arc4->x = 0; |
|
65
|
0
|
|
|
|
|
|
arc4->y = 0; |
|
66
|
0
|
|
|
|
|
|
index1 = 0; |
|
67
|
0
|
|
|
|
|
|
index2 = 0; |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
for (counter = 0; counter < 256; counter++) |
|
70
|
|
|
|
|
|
|
{ |
|
71
|
0
|
|
|
|
|
|
index2 = (key[index1] + state[counter] + index2) & 0xff; |
|
72
|
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
tmp = state[counter]; |
|
74
|
0
|
|
|
|
|
|
state[counter] = state[index2]; |
|
75
|
0
|
|
|
|
|
|
state[index2] = tmp; |
|
76
|
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
index1 = (index1 + 1) % keylen; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
0
|
|
|
|
|
|
return PS_SUCCESS; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
/******************************************************************************/ |
|
83
|
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
void psArc4(psArc4_t *arc4, const unsigned char *in, |
|
85
|
|
|
|
|
|
|
unsigned char *out, uint32_t len) |
|
86
|
|
|
|
|
|
|
{ |
|
87
|
|
|
|
|
|
|
unsigned char x, y, *state, xorIndex, tmp; |
|
88
|
|
|
|
|
|
|
uint32_t counter; |
|
89
|
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
arc4->byteCount += len; |
|
91
|
0
|
0
|
|
|
|
|
if (arc4->byteCount > ARC4_MAX_BYTES) |
|
92
|
|
|
|
|
|
|
{ |
|
93
|
|
|
|
|
|
|
psTraceCrypto("ARC4 byteCount overrun\n"); |
|
94
|
0
|
0
|
|
|
|
|
psAssert(arc4->byteCount <= ARC4_MAX_BYTES); |
|
95
|
0
|
|
|
|
|
|
memzero_s(arc4, sizeof(psArc4_t)); |
|
96
|
0
|
|
|
|
|
|
return; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
x = arc4->x; |
|
100
|
0
|
|
|
|
|
|
y = arc4->y; |
|
101
|
0
|
|
|
|
|
|
state = &arc4->state[0]; |
|
102
|
0
|
0
|
|
|
|
|
for (counter = 0; counter < len; counter++) |
|
103
|
|
|
|
|
|
|
{ |
|
104
|
0
|
|
|
|
|
|
x = (x + 1) & 0xff; |
|
105
|
0
|
|
|
|
|
|
y = (state[x] + y) & 0xff; |
|
106
|
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
tmp = state[x]; |
|
108
|
0
|
|
|
|
|
|
state[x] = state[y]; |
|
109
|
0
|
|
|
|
|
|
state[y] = tmp; |
|
110
|
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
xorIndex = (state[x] + state[y]) & 0xff; |
|
112
|
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
tmp = in[counter]; |
|
114
|
0
|
|
|
|
|
|
tmp ^= state[xorIndex]; |
|
115
|
0
|
|
|
|
|
|
out[counter] = tmp; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
0
|
|
|
|
|
|
arc4->x = x; |
|
118
|
0
|
|
|
|
|
|
arc4->y = y; |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
void psArc4Clear(psArc4_t *arc4) |
|
122
|
|
|
|
|
|
|
{ |
|
123
|
0
|
|
|
|
|
|
memzero_s(arc4, sizeof(psArc4_t)); |
|
124
|
0
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#endif /* USE_MATRIX_ARC4 */ |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
/******************************************************************************/ |
|
129
|
|
|
|
|
|
|
|