forked from dmbaturin/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otpgen.pl
executable file
·317 lines (243 loc) · 8.87 KB
/
otpgen.pl
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
#!/usr/bin/perl
# One-time pad generator
# Copyright (c) 2011 Daniil Baturin <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies
# or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use Getopt::Long;
use MIME::Base64;
###################### Settings ############################
############################################################
# The following settings define page look,
# feel free to change them
#################### LaTeX settings ########################
# Text inserted after any data line
$TEX_LINE_BREAK = " \\\\\n";
# Text inserted before new page
$TEX_PAGE_BREAK = "\\pagebreak\n";
# Page tempate
# _SERIAL_ is replaced with page serial number,
# _DATA_ is replaced with key data
$TEX_PAGE = <<END;
\\begin{center}
SERIAL: _SERIAL_ \\\\
\\ \\\\
_DATA_
\\ \\\\
DESTROY IMMEDIATELY AFTER USE
\\end{center}
END
# Text inserted before all generated data
$TEX_BEGIN = <<END;
\\documentclass[a4paper,10pt]{article}
\\usepackage[utf8x]{inputenc}
\\begin{document}
\\pagestyle{empty}
\\ttfamily
END
# Text inserted after all data
$TEX_END = <<END;
\\end{document}
END
#################### Plaintext settings #####################
$TXT_LINE_BREAK = "\n";
$TXT_PAGE_BREAK = "-------------\n";
$TXT_PAGE = <<END;
SERIAL: _SERIAL_
_DATA_
DESTROY IMMEDIATELY AFTER USE
END
$TXT_BEGIN = "";
$TXT_END = "";
##################### Other settings #######################
# Whether to use insecure deletion command (rm)
$USE_RM = 0;
############################################################
$help = <<END;
Usage: $0 --in=<FILE> --out=<FILE> --group-width=<NUM> --page-width=<NUM>
--page-height=<NUM> --pages=<NUM> --plain-text --keep-source
--in=<FILE> File to be used as randomness source. On Linux typically /dev/random (secure)
or /dev/urandom (insecure).
Default is /dev/urandom.
--out=<FILE> Output file name
If --plain-text option is not specified
.pdf extension will be added automatically.
This option is mandatory.
--group-width=<NUM> Size of numbers group. Default is 5.
--page-width=<NUM> Page width in groups. Default is 8.
--page-height=<NUM> Page height in lines. Default is 16.
--pages=<NUM> Number of pages. Default is 100.
--plain-text Produce plaintext output instead of PDF
(does not require pdflatex).
Default is to use LaTeX.
--keep-source Do not remove LaTeX source before quit.
Report bugs and suggestions to daniil\@baturin.org
Distributed under terms of MIT license.
END
## Check options and dependencies
if ($#ARGV < 0) { # No arguments given
print $help;
exit(0);
}
GetOptions(
"in=s" => \$inputFile,
"out=s" => \$outputFile,
"group-width=s" => \$groupWidth,
"page-width=s" => \$pageWidth,
"page-height=s" => \$pageHeight,
"pages=s" => \$pages,
"plain-text" => \$plaintext,
"keep-source" => \$keepsource,
"debug" => \$debug
);
# Check options
die("Error: Input file $inputFile does not exist or is not readable!\n") unless !defined($inputFile) || (-r $inputFile);
die("Error: Output file $outputFile is not writable!\n") if (-e $outputFile && !-w $outputFile);
if (!defined($plaintext)) {
# LaTeX uses file.tex if both file.tex and file exist
die("Error: Output file $outputFile.tex already exists!\n") if (-e $outputFile.".tex");
}
die("Error: Group width is not a positive number!") unless !defined($groupWidth) || ($groupWidth > 0);
die("Error: Page width is not a positive number!") unless !defined($pageWidth) || ($pageWidth > 0);
die("Error: Page height is not a positive number!") unless !defined($pageWidth) || ($pageHeight > 0);
die("Error: Number of pages is not positive!") unless !defined($pages) || ($pages > 0);
# Set defaults
$inputFile = "/dev/urandom" unless defined $inputFile;
if (!defined($groupWidth)) {
$groupWidth = 5;
}
if (!defined($pageWidth)) {
$pageWidth = 8;
}
if (!defined($pageHeight)) {
$pageHeight = 16;
}
if (!defined($pages)) {
$pages = 100;
}
# Check for OpenSSL binary
system("which openssl 2>&1 >/dev/null");
die("Error: OpenSSL is not installed or is not in your PATH!") unless $? == 0;
# Check for pdflatex
system("which pdflatex 2>&1 >/dev/null");
if ($? != 0) {
die("Error: pdflatex is not installed or is not in your PATH!") unless defined($plaintext);
}
# Check for shred
system("which shred 2>&1 >/dev/null");
if ($? != 0) {
print "Warning: secure deletion tool (shred) is not available!";
$USE_RM = 1;
}
# Set output templates
if ($plaintext) {
$LINE_BREAK = $TXT_LINE_BREAK;
$PAGE_BREAK = $TXT_PAGE_BREAK;
$PAGE = $TXT_PAGE;
$BEGIN = $TXT_BEGIN;
$END = $TXT_END;
} else {
$LINE_BREAK = $TEX_LINE_BREAK;
$PAGE_BREAK = $TEX_PAGE_BREAK;
$PAGE = $TEX_PAGE;
$BEGIN = $TEX_BEGIN;
$END = $TEX_END;
}
## Obtain random data
if ($inputFile eq "/dev/urandom") {
print "Warning: Using insecure random number generator! \
\
/dev/urandom is fast, but potentially insecure. \
If you need nearly perfect security you should use /dev/random that \
produces truly random numbers gathered from external sources such as \
disk, keyboard or mouse usage so generation process may take hours or \
even days depending on data size.\n\n";
}
# Add $pages extra lines to pick page serial numbers from
$size = ($groupWidth * $pageWidth * $pageHeight * $pages) + $pages;
print "Random data size: ".$size."\n" if $debug;
# Make a key for AES
$key = encode_base64(`dd if=$inputFile bs=42 count=1 2>/dev/null`);
print "Key: ".$key."\n" if $debug;
# Read random bytes and pass them through AES to improve
# statistical uniformity of the sequence
print "Obtaining random numbers.\n";
$random = `dd if=$inputFile bs=1 count=$size 2>/dev/null | openssl enc -aes256 -pass pass:$key`;
die ("Error: coult not obtain random numbers!") unless $? == 0;
# OpenSSL precedes encrypted text with "Salted__" string so we need to remove it
$random =~ s/Salted__//;
print "Raw random data: ".encode_base64($random)."\n" if $debug;
# Turn random data string into digits
@randomArray = split(//, $random);
$randomDigits = "";
foreach (@randomArray) {
$randomDigits .= ord($_);
};
print $randomDigits."\n" if $debug;
# Split random digits into space delimited groups
$randomDigits =~ s/(\d{$groupWidth})/$1 /g;
## Split into lines
$randomDigits =~ s/((\d{$groupWidth} ){$pageWidth})/$1\n/g;
# Cut out extra lines
$lines = ($pageHeight * $pages) + $pages;
print "Lines: $lines\n" if $debug;
$randomDigits =~ m/(((((\d{$groupWidth} ){$pageWidth})\n)){$lines})/;
$randomDigits = $1;
# Split into pages
@randomLines = split(/\n/, $randomDigits);
$position = 0;
$pageNumber = 1;
$pagesData = "";
while ($pageNumber <= $pages) {
$randomLines[$position] =~ m/(\d{$groupWidth})/;
$serial = $1;
$position++;
$pageLine = 1;
$pageText = "";
while ($pageLine <= $pageHeight) {
$pageText = $pageText . $randomLines[$position] . $LINE_BREAK;
$pageLine++;
$position++;
}
$pageLine = 1;
$page = $PAGE;
$page =~ s/_SERIAL_/$serial/;
$page =~ s/_DATA_/$pageText/;
$pagesData = $pagesData . $page . $PAGE_BREAK ."\n";
$pageNumber++;
}
$output = $BEGIN . $pagesData . $END;
# Write generated data to file
open(HANDLE, ">$outputFile");
print "Writing output file.\n";
print HANDLE $output or die("Error: Could not write to $outputFile!\n");
close(HANDLE);
if (!$plaintext) {
print "Compiling the PDF.\n";
system("pdflatex $outputFile 2>&1 >/dev/null");
die("Error: Could not compile a PDF!") unless $? == 0;
if (!defined($keepsource)) {
print "Cleaning up LaTeX source and logs.\n";
if (!$USE_RM) {
system("shred -zu $outputFile");
} else {
system("rm -f $outputFile");
}
system("rm -f $outputFile.log $outputFile.aux");
}
}
print "Done!\n";
exit(0);