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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 | //
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "RC4Encrypt.h"
#include <common/MD5Sum.h>
#include <stdexcept>
CRC4EncryptableBuffer::CRC4EncryptableBuffer() : m_encrypted(false), m_hasKey(false), m_key()
{
}
CRC4EncryptableBuffer::~CRC4EncryptableBuffer()
{
}
void CRC4EncryptableBuffer::Append(const uint8* buffer, int n)<--- Derived function 'CRC4EncryptableBuffer::Append'
{
wxASSERT(!m_encrypted);
if (!m_encrypted) {
CMemFile::Append(buffer, n);
} else {
throw std::runtime_error(
"(CRC4EncryptableBuffer::Append): "
"Tried to append data to an encrypted buffer.");
}
}
void CRC4EncryptableBuffer::Encrypt()
{
wxASSERT(!m_encrypted);
RC4Crypt(GetRawBuffer(), GetRawBuffer(), GetLength());
m_encrypted = true;
}
void CRC4EncryptableBuffer::RC4Crypt( const uint8 *pachIn, uint8 *pachOut, uint32 nLen)
{
wxASSERT( m_hasKey && nLen > 0 );
if (m_hasKey) {
uint8 byX = m_key.byX;;
uint8 byY = m_key.byY;
uint8* pabyState = &m_key.abyState[0];;
for (uint32 i = 0; i < nLen; ++i) {
uint8 byXorIndex;
byX = (byX + 1) % 256;
byY = (pabyState[byX] + byY) % 256;
std::swap(pabyState[byX], pabyState[byY]);
byXorIndex = (pabyState[byX] + pabyState[byY]) % 256;
if (pachIn != NULL) {
pachOut[i] = pachIn[i] ^ pabyState[byXorIndex];
}
}
m_key.byX = byX;
m_key.byY = byY;
} else {
throw std::runtime_error(
"(CRC4EncryptableBuffer::RC4Crypt): "
"Encrypt() has been called without a previous call"
"to SetKey().");
}
}
uint8 *CRC4EncryptableBuffer::Detach()
{
int n = GetLength();
uint8 *ret = new uint8[n];
memcpy(ret, GetRawBuffer(), n);
ResetData();
m_encrypted = false;
return ret;
}
void CRC4EncryptableBuffer::SetKey(const MD5Sum& keyhash, bool bSkipDiscard)
{
wxASSERT(!m_hasKey);
if (!m_hasKey) {
m_hasKey = true;
RC4CreateKey( keyhash.GetRawHash(), 16, bSkipDiscard);
} else {
throw std::runtime_error( "(CRC4EncryptableBuffer::SetKey): SetKey() has been called twice.");
}
}
void CRC4EncryptableBuffer::RC4CreateKey(const uint8* pachKeyData, uint32 nLen, bool bSkipDiscard)
{
uint8 index1;
uint8 index2;
uint8* pabyState;
pabyState= &m_key.abyState[0];<--- Address of variable taken here.
for (int i = 0; i < 256; ++i) {
pabyState[i] = (uint8)i;<--- The address of variable 'm_key.abyState' might be accessed at non-zero index.
}
m_key.byX = 0;
m_key.byY = 0;
index1 = 0;
index2 = 0;
for (int i = 0; i < 256; ++i) {
index2 = (pachKeyData[index1] + pabyState[i] + index2) % 256;
std::swap(pabyState[i], pabyState[index2]);
index1 = (uint8)((index1 + 1) % nLen);
}
if (!bSkipDiscard) {
RC4Crypt(NULL, NULL, 1024);
}
}
void CRC4EncryptableBuffer::ResetData()
{
m_encrypted = false;
// Not touching the keys.
CMemFile::ResetData();
}
void CRC4EncryptableBuffer::FullReset()
{
ResetData();
m_hasKey = false;
memset(&m_key, 0, sizeof(RC4_Key_Struct));
}
|