-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslss.cc
More file actions
282 lines (254 loc) · 7.89 KB
/
slss.cc
File metadata and controls
282 lines (254 loc) · 7.89 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
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
#include "slss.hh"
#include "aont.hh"
#include "gfm.hh"
#include <cstdio>
#include <iostream>
#include <libgen.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <vector>
static void rtfm_aont(const std::string & prog, const bool copying = false)
__attribute__((noreturn));
static void rtfm_gfm(const std::string & prog, const bool copying = false)
__attribute__((noreturn));
static void rtfm_slss(const std::string & prog, const bool copying = false)
__attribute__((noreturn));
bool ends_with(std::string const & str,
std::string const & end)
{
return ((end.size() <= str.size())
&&
std::equal(end.rbegin(), end.rend(), str.rbegin()));
}
static void rtfm(const std::string & prog, const bool copying = false)
{
std::cerr <<
" " << prog << " Copyright (C) 2025 Thomas Sprinkmeier\n\n";
if (copying)
{
std::cerr <<
" This program is free software: you can redistribute it and/or modify\n"
" it under the terms of the GNU General Public License as published by\n"
" the Free Software Foundation, either version 3 of the License, or\n"
" (at your option) any later version.\n";
}
else
{
std::cerr <<
" This program is distributed in the hope that it will be useful,\n"
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
" GNU General Public License for more details.\n";
}
std::cerr <<
"\n"
" You should have received a copy of the GNU General Public License\n"
" along with this program. If not, see <https://www.gnu.org/licenses/>.\n"
"\n"
" run \"" << prog << " show [warranty|copying]\" for details.\n"
<< std::endl;
}
static void rtfm_aont(const std::string & prog, const bool copying)
{
rtfm(prog, copying);
std::cerr <<
"\t" << prog << " [-]\n"
"\t\tencrypt STDIN to STDOUT\n"
<< std::endl;
std::cerr <<
"\t" << prog << " STUB\n"
"\t\tencrypt STUB (if it exists) or STDIN to STUB.aont\n"
<< std::endl;
std::cerr <<
"\t" << prog << " STUB.aont\n"
"\t\tdecrypt STUB.aont to STUB\n"
<< std::endl;
exit(1);
}
static void rtfm_gfm(const std::string & prog, const bool copying)
{
rtfm(prog, copying);
std::cerr <<
prog << " STUB [NUM_SHARES NUM_REQUIRED]\n"
"\tSTUB filename stub for files to split or recover\n"
"\tNUM_SHARES number of shares to create\n"
"\tNUM_REQUIRED number of shares required to recover\n"
<< std::endl;
exit(1);
}
static void rtfm_slss(const std::string & prog, const bool copying)
{
rtfm(prog, copying);
std::cerr <<
prog << " STUB [NUM_SHARES NUM_REQUIRED]\n"
"\tSTUB filename stub for files to encrypt and split\n"
"\t or recover and decrypt\n"
"\tNUM_SHARES number of shares to create\n"
"\tNUM_REQUIRED number of shares required to recover\n"
<< std::endl;
exit(1);
}
void attest(bool test, const char * epilogue, ...)
{
if (test)
{
return;
}
va_list ap;
va_start(ap, epilogue);
fprintf(stderr, "\n");
vfprintf(stderr, epilogue, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
static void ParseNUMs(const std::vector<std::string> & args,
int & numShares,
int & numRequired)
{
attest(args.size() == 3, "expecting 3 arguments");
numShares = std::stoi(args[1]);
numRequired = std::stoi(args[2]);
// limiting to 240 shares.
// it makes no sense for only 1 share to be required to recover.
// it also makes no sense is _all_ shares are required to recover, so
// 2 <= numRequired < numShares <= 240
attest((numShares >= 2) && (numShares <= 240),
"You must specify between 3 and 240 shares");
attest((numRequired >= 2) && (numRequired < numShares),
"You must specify between 2 and numShares (%d) required shares", numShares);
}
static void run_aont(const std::vector<std::string> & args)
{
// streaming STDIN to STDOUT?
const bool stream = ((args.size() == 0) ||
((args.size() == 1) &&
(args[0] == "-")));
if (stream)
{
std::cerr << "encrypt STDIN to STDOUT" << std::endl;
encrypt();
exit(0);
}
// if not streaming we expect a single parameter
if (args.size() != 1)
{
return;
}
const std::string stub(args[0]);
// determine mode based on provided filename
if (ends_with(stub, ".aont"))
{
const std::string plaintext(stub.substr(0, stub.size()-5));
std::cerr << "decrypt " << stub << " to " << plaintext
<< std::endl;
decrypt(stub, plaintext);
exit(0);
}
const std::string encrypted = stub + ".aont";
if (access(stub.c_str(), R_OK) == 0)
{
std::cerr << "encrypt " << stub << " to " << encrypted
<< std::endl;
encrypt(stub, encrypted);
exit(0);
}
std::cerr << "encrypt STDIN to " << encrypted
<< std::endl;
encrypt(STDIN_FILENO, encrypted);
exit(0);
}
int main(int argc, char ** argv)
{
attest((argc >= 1) && (argv != NULL) && (argv[0] != NULL),
"main(%d,%p): INVALID", argc, argv);
// extract program name and arguments
const std::string prog(argv[0]);
const std::vector<std::string> args(argv + 1, argv + argc);
// mode (gfm, aont, slss or show license)
const bool RunAsGFM = ends_with(prog, "gfm");
const bool RunAsAONT = ends_with(prog, "aont");
const bool show = ((args.size() == 2) &&
(args[0] == "show"));
const bool copying = (show &&
(*args[1].c_str() == 'c'));
// AONT mode?
if (RunAsAONT)
{
run_aont(args);
rtfm_aont(prog, copying);
}
// not AONT mode, so it's either SLSS or GFM
// single parameter is recovery mode
if (args.size() == 1)
{
const std::string stub(args[0]);
if (RunAsGFM)
{
std::cerr << "recovering " << stub << std::endl;
RecoverData(stub);
}
else
{
const size_t len = stub.size();
// does stub "already have aont" extension?
const bool aha = ends_with(stub, ".aont");
const std::string proc(stub + (aha ? "" : ".aont"));
const std::string plaintext(stub.substr(0,len-(aha ? 5 : 0)));
std::cerr << "recovering and decrypting " << plaintext
<< std::endl;
RecoverData(proc);
decrypt(proc, plaintext);
}
exit(0);
}
// three parameters is generation mode
if (args.size() == 3)
{
const std::string stub(args[0]);
// parse and check number of shares and number required
int numShares;
int numRequired;
ParseNUMs(args, numShares, numRequired);
// conmvert to number of data and number of parity
const uint8_t numData = numRequired;
const uint8_t numParity = numShares - numRequired;
if (RunAsGFM)
{
std::cerr << "split " << stub << " into " << numShares
<< " shares of which " << numRequired
<< " are required to recover "
<< std::endl;
CreateParity(numData, numParity, stub);
}
else
{
const std::string encrypted = stub + ".aont";
if (access(stub.c_str(), R_OK) == 0)
{
std::cerr << "split " << stub << " into " << numShares
<< " encrypted shares of which " << numRequired
<< " are required to recover "
<< std::endl;
encrypt(stub, encrypted);
}
else
{
std::cerr << "split STDIN into " << numShares
<< " encrypted shares of which " << numRequired
<< " are required to recover "
<< std::endl;
encrypt(STDIN_FILENO, encrypted);
}
CreateParity(numData, numParity, encrypted);
}
exit(0);
}
// wrong number of arguments...
if (RunAsGFM)
{
rtfm_gfm(prog, copying);
}
rtfm_slss(prog, copying);
}