-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cc
250 lines (223 loc) · 6.45 KB
/
main.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
#include <QByteArray>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QStringList>
#include "cipher.h"
#include "secretfile.h"
#include <iostream>
#include <stdexcept>
#ifdef Q_OS_LINUX
# include <fcntl.h>
# include <linux/fs.h>
# include <sys/ioctl.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
//
static
qint64 GetDeviceSize( QString const &path )
{
char const *p = path.toLocal8Bit().constData();
int fd = open( p, O_RDONLY );
qint64 file_size_in_bytes = 0;
ioctl( fd, BLKGETSIZE64, &file_size_in_bytes );
close( fd );
return file_size_in_bytes;
}
#endif /* Q_OS_LINUX */
//
static
void EncryptToDevice( QString const &device, SecretFile const &sf,
QString const &password )
{
QFileInfo fi( device );
if( !fi.exists() )
{
throw std::runtime_error( "output device or file does not exist" );
}
qint64 devSize = fi.size();
#ifdef Q_OS_LINUX
if( devSize < qint64( ClusterSize ) )
{
devSize = GetDeviceSize( device );
}
#endif
if( devSize < qint64( ClusterSize ) )
{
throw std::runtime_error( "output device or file is too small" );
}
QFile file( device );
if( !file.open( QIODevice::WriteOnly ) )
{
throw std::runtime_error( "error opening output device or file "
"for writing" );
}
const quint64 numClusters = quint64( devSize / ClusterSize );
std::cout << "About to write " << numClusters << " clusters to device " <<
qPrintable( device ) << std::endl;
const int secFileClusters = sf.numChunks();
std::cout << "SecretFile is " << secFileClusters << " cluster(s) long, "
"writing about " << ( numClusters / secFileClusters ) << " copies." <<
std::endl;
for( quint64 cluster = 0; cluster < numClusters; ++cluster )
{
QByteArray chunk = sf.getChunkForCluster( cluster );
EncryptData( chunk, password );
if( file.write( chunk ) != chunk.size() )
{
throw std::runtime_error( "write error" );
}
if( ( cluster % 100 ) == 0 )
{
int percent = int( ( cluster * 100 ) / numClusters );
std::cout << "\r" << cluster << ' ' << percent << '%';
std::cout.flush();
}
}
std::cout << "\r" << numClusters << " 100%" << std::endl;
}
//
static
void DecryptFromDevice( QString const &device, SecretFile &sf,
QString const &password )
{
QFileInfo fi( device );
if( !fi.exists() )
{
throw std::runtime_error( "input device or file does not exist" );
}
qint64 devSize = fi.size();
#ifdef Q_OS_LINUX
if( devSize < qint64( ClusterSize ) )
{
devSize = GetDeviceSize( device );
}
#endif
if( devSize < qint64( ClusterSize ) )
{
throw std::runtime_error( "input device or file is too small" );
}
QFile file( device );
if( !file.open( QIODevice::ReadOnly ) )
{
throw std::runtime_error( "error opening output device or file "
"for reading" );
}
const quint32 numClusters = quint32( devSize / ClusterSize );
std::cout << "Device size: " << numClusters << " clusters" << std::endl;
quint32 totalChunks = 0, goodChunks = 0;
QByteArray chunk = file.read( qint64( ClusterSize ) );
while( chunk.size() == ClusterSize )
{
DecryptData( chunk, password );
if( sf.addDataChunk( chunk ) )
{
std::cout << ' ' << totalChunks << " - decrypted\n";
++goodChunks;
if( sf.isFileComplete() )
{
break;
}
}
else
{
if( ( totalChunks % 100 ) == 0 )
{
int percent = int( ( totalChunks * 100 ) / numClusters );
std::cout << "\r" << totalChunks << ' ' << percent << '%';
std::cout.flush();
}
}
++totalChunks;
std::cout.flush();
chunk = file.read( qint64( ClusterSize ) );
}
std::cout << "\nTotal clusters read: " << totalChunks << ", decrypted: " <<
goodChunks << std::endl;
}
//
static
bool ParseCommandLine( QStringList const &args, bool &encrypt, QString &device,
QString &password, QString &secretFile )
{
for( int i = 1; i < args.size(); ++i )
{
if( args[i] == "-h" )
{
return false;
}
if( args[i] == "-s" )
{
++i;
secretFile = args[i];
continue;
}
if( args[i] == "-p" )
{
++i;
password = args[i];
continue;
}
if( args[i] == "-e" )
{
encrypt = true;
continue;
}
if( device.isEmpty() )
{
device = args[i];
}
else
{
return false;
}
}
return !password.isEmpty() && !device.isEmpty() &&
( !secretFile.isEmpty() == encrypt );
}
//
int main( int argc, char *argv[] )
{
QCoreApplication app( argc, argv );
QString device, password, secretFile;
bool encrypt = false;
if( !ParseCommandLine( app.arguments(), encrypt, device, password,
secretFile ) )
{
std::cout << "Usage:\n" << qPrintable( app.arguments()[0] ) <<
" [-h] [-e] -p password [-s secretFile] deviceOrImageFile" <<
std::endl;
std::cout <<
" -h: this help\n"
" -e: encrypt secretFile into device using password (default is "
"to extract an encrypted file from the device)\n"
" -p password: the encryption/decryption password\n"
" -s secretFile: the file to encrypt onto the device\n"
" deviceOrImageFile: where to write the encrypted data (warning: "
"will overwrite any existing data!)" << std::endl;
return 1;
}
try {
SecretFile sf;
if( encrypt )
{
sf.loadFrom( secretFile );
EncryptToDevice( device, sf, password );
}
else
{
DecryptFromDevice( device, sf, password );
sf.saveToSecretFile(); // obtained from the data
std::cout << "Wrote " << sf.filesize() << " bytes to file " <<
qPrintable( sf.filename() ) << std::endl;
}
}
catch( std::runtime_error const &e )
{
std::cout << "ERROR: " << e.what() << std::endl;
return 1;
}
std::cout << "Done." << std::endl;
return 0;
}