This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
EmbData.cc
375 lines (325 loc) · 9.18 KB
/
EmbData.cc
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* steghide 0.5.1 - a steganography program
* Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "AUtils.h"
#include "BinaryIO.h"
#include "BitString.h"
#include "EmbData.h"
#include "error.h"
#include "MCryptPP.h"
#include "MHashPP.h"
#include "common.h"
EmbData::EmbData (MODE m, std::string pp, std::string fn)
: Mode(m), Passphrase(pp), FileName(fn)
{
if (m == EXTRACT) {
NumBitsNeeded = NumBitsRequested = NBitsMagic ;
Version = CodeVersion ;
State = READ_MAGIC ;
Reservoir = BitString() ;
}
}
bool EmbData::finished ()
{
myassert (Mode == EXTRACT) ;
return (State == END) ;
}
unsigned long EmbData::getNumBitsRequested ()
{
myassert (Mode == EXTRACT) ;
return NumBitsRequested ;
}
void EmbData::addBits (BitString addbits)
{
myassert (Mode == EXTRACT) ;
#ifdef DEBUG
printDebug (1, "\nEmbData::addBits called with") ;
printDebug (1, " addbits:") ;
addbits.printDebug (1, 2) ;
printDebug (1, " Reservoir:") ;
Reservoir.printDebug (1, 2) ;
#endif
Reservoir.append (addbits) ;
BitString bits ;
if (Reservoir.getLength() >= NumBitsNeeded) {
bits = Reservoir.cutBits (0, NumBitsNeeded) ; // take exactly the first NumBitsNeeded bits from Reservoir | addbits
}
else { // Reservoir.getLength() < NumBitsNeeded
myassert(false) ;
}
#ifdef DEBUG
printDebug (1, "bits is now:") ;
bits.printDebug (1, 2) ;
#endif
switch (State) {
case READ_MAGIC:
{
#ifdef DEBUG
printDebug (1, "in the READ_MAGIC state") ;
#endif
if (bits.getValue(0, NBitsMagic) == Magic) {
NumBitsNeeded = 1 ;
NumBitsRequested = AUtils::bminus<unsigned long> (NumBitsNeeded, Reservoir.getLength()) ;
State = READ_VERSION ;
}
else {
throw SteghideError (_("could not extract any data with that passphrase!")) ;
}
break ;
}
case READ_VERSION:
{
#ifdef DEBUG
printDebug (1, "in the READ_VERSION state") ;
#endif
if (bits[0] == true) {
Version++ ;
NumBitsNeeded = AUtils::bminus ((UWORD32) 1, Reservoir.getLength()) ;
}
else {
if (Version > CodeVersion) {
throw CorruptDataError (_("attempting to read an embedding of version %d but steghide %s only supports embeddings of version %d."), Version, VERSION, CodeVersion) ;
}
NumBitsNeeded = EncryptionAlgorithm::IRep_size + EncryptionMode::IRep_size ;
NumBitsRequested = AUtils::bminus<unsigned long> (NumBitsNeeded, Reservoir.getLength()) ;
State = READ_ENCINFO ;
}
break ;
}
case READ_ENCINFO: {
#ifdef DEBUG
printDebug (1, "in the READ_ENCINFO state") ;
#endif
unsigned int algo = (unsigned int) bits.getValue (0, EncryptionAlgorithm::IRep_size) ;
if (EncryptionAlgorithm::isValidIntegerRep (algo)) {
EncAlgo.setValue ((EncryptionAlgorithm::IRep) algo) ;
}
unsigned int mode = (unsigned int) bits.getValue (EncryptionAlgorithm::IRep_size, EncryptionMode::IRep_size) ;
if (EncryptionMode::isValidIntegerRep (mode)) {
EncMode.setValue ((EncryptionMode::IRep) mode) ;
}
NumBitsNeeded = NBitsNPlainBits ;
NumBitsRequested = AUtils::bminus<unsigned long> (NumBitsNeeded, Reservoir.getLength()) ;
State = READ_NPLAINBITS ;
#ifndef USE_LIBMCRYPT
if (EncAlgo.getIntegerRep() != EncryptionAlgorithm::NONE) {
throw SteghideError (_(
"The embedded data is encrypted but steghide has been compiled without encryption\n"
"support. To be able to read the embedded data, you have to install libmcrypt\n"
"(http://mcrypt.sourceforge.net/) and recompile steghide.")) ;
}
#endif
break ; }
case READ_NPLAINBITS: {
#ifdef DEBUG
printDebug (1, "in the READ_NPLAINBITS state") ;
#endif
NPlainBits = bits.getValue (0, NBitsNPlainBits) ;
#ifdef USE_LIBMCRYPT
NumBitsNeeded = (UWORD32) MCryptPP::getEncryptedSize (EncAlgo, EncMode, NPlainBits) ;
#else
NumBitsNeeded = NPlainBits ;
#endif
NumBitsRequested = AUtils::bminus<unsigned long> (NumBitsNeeded, Reservoir.getLength()) ;
State = READ_ENCRYPTED ;
break ; }
case READ_ENCRYPTED: {
#ifdef DEBUG
printDebug (1, "in the READ_ENCRYPTED state") ;
#endif
BitString plain ;
#ifdef USE_LIBMCRYPT
if (EncAlgo.getIntegerRep() == EncryptionAlgorithm::NONE) {
plain = bits ;
}
else {
MCryptPP crypto (EncAlgo, EncMode) ;
plain = crypto.decrypt (bits, Passphrase) ;
}
#else
plain = bits ;
#endif
plain.truncate (0, NPlainBits) ; // cut off random padding used to achieve full number of encryption blocks
unsigned long pos = 0 ;
// read Compression (and uncompress)
Compression = ((plain[pos++]) ? 9 : 0) ; // to make compression contain a value that makes sense
#ifdef DEBUG
printDebug (2, " compression: %d\n", plain[pos - 1]) ;
#endif
if (Compression > 0) {
UWORD32 NUncompressedBits = plain.getValue (pos, NBitsNUncompressedBits) ;
#ifdef DEBUG
printDebug (2, " nuncobits: %lu\n", NUncompressedBits) ;
#endif
pos += NBitsNUncompressedBits ;
plain.truncate (pos, plain.getLength()) ;
pos = 0 ;
plain.uncompress (NUncompressedBits) ;
}
// read Checksum
Checksum = plain[pos++] ;
#ifdef DEBUG
printDebug (2, " checksum: %d\n", plain[pos - 1]) ;
#endif
if (Checksum) {
CRC32 = plain.getValue (pos, NBitsCrc32) ;
#ifdef DEBUG
printDebug (2, " crc32: 0x%x\n", CRC32) ;
#endif
pos += NBitsCrc32 ;
}
// read filename
char curchar = '\0' ;
FileName = "" ;
do {
curchar = (char) plain.getValue (pos, 8) ;
if (curchar != '\0') {
FileName += curchar ;
}
pos += 8 ;
} while (curchar != '\0') ;
// extract data
if ((plain.getLength() - pos) % 8 != 0) {
throw CorruptDataError (_("the embedded data has an invalid length.")) ;
}
const unsigned long extdatalen = (plain.getLength() - pos) / 8 ;
Data.resize (extdatalen) ;
for (unsigned int i = 0 ; i < extdatalen ; i++) {
Data[i] = ((BYTE) plain.getValue (pos, 8)) ;
pos += 8 ;
}
NumBitsNeeded = 0 ;
NumBitsRequested = 0 ;
State = END ;
break ; }
case END:
default: {
myassert (0) ;
break ; }
}
}
bool EmbData::checksumOK (void) const
{
// test if checksum is ok
bool ok = true ;
if (Checksum) {
MHashPP hash (MHASH_CRC32) ;
for (std::vector<BYTE>::const_iterator i = Data.begin() ; i != Data.end() ; i++) {
hash << *i ;
}
hash << MHashPP::endhash ;
unsigned long calccrc32 = hash.getHashBits().getValue(0, NBitsCrc32) ;
if (calccrc32 == CRC32) {
ok = true ;
}
else {
ok = false ;
}
}
return ok ;
}
void EmbData::setEncAlgo (EncryptionAlgorithm a)
{
EncAlgo = a ;
}
EncryptionAlgorithm EmbData::getEncAlgo () const
{
return EncAlgo ;
}
void EmbData::setEncMode (EncryptionMode m)
{
EncMode = m ;
}
EncryptionMode EmbData::getEncMode () const
{
return EncMode ;
}
void EmbData::setCompression (int c)
{
Compression = c ;
}
int EmbData::getCompression (void) const
{
return Compression ;
}
void EmbData::setChecksum (bool c)
{
Checksum = c ;
}
bool EmbData::getChecksum (void) const
{
return Checksum ;
}
BitString EmbData::getBitString ()
{
myassert (Mode == EMBED) ;
// assembling data that can be compressed
BitString compr ;
compr.append (Checksum) ;
if (Checksum) {
MHashPP hash (MHASH_CRC32) ;
for (std::vector<BYTE>::iterator i = Data.begin() ; i != Data.end() ; i++) {
hash << *i ;
}
hash << MHashPP::endhash ;
compr.append (hash.getHashBits()) ;
}
compr.append (stripDir (FileName)) ;
compr.append ((BYTE) 0, 8) ; // end of fileame
compr.append (Data) ;
// assembling data that can be encrypted
BitString plain ;
plain.append ((Compression > 0) ? true : false) ;
if (Compression > 0) {
plain.append (compr.getLength(), NBitsNUncompressedBits) ;
compr.compress (Compression) ;
}
plain.append (compr) ;
// put it all together
BitString main ;
main.append(Magic, NBitsMagic) ;
for (unsigned short i = 0 ; i < CodeVersion ; i++) {
main.append(true) ;
}
main.append(false) ; // end of version
main.append((UWORD16) EncAlgo.getIntegerRep(), EncryptionAlgorithm::IRep_size) ;
main.append((UWORD16) EncMode.getIntegerRep(), EncryptionMode::IRep_size) ;
main.append(plain.getLength(), NBitsNPlainBits) ;
#ifdef USE_LIBMCRYPT
if (EncAlgo.getIntegerRep() != EncryptionAlgorithm::NONE) {
MCryptPP crypto (EncAlgo, EncMode) ;
plain = crypto.encrypt (plain, Passphrase) ;
}
#else
myassert (EncAlgo.getIntegerRep() == EncryptionAlgorithm::NONE) ;
#endif
main.append (plain) ;
return main ;
}
std::string EmbData::stripDir (std::string s)
{
unsigned int start = 0 ;
if ((start = s.find_last_of ("/\\")) == std::string::npos) {
start = 0 ;
}
else {
start += 1 ;
}
return s.substr (start, std::string::npos) ;
}