comparison libtomcrypt/helper.pl @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long;
7 use File::Find 'find';
8 use File::Basename 'basename';
9 use File::Glob 'bsd_glob';
10
11 sub read_file {
12 my $f = shift;
13 open my $fh, "<", $f or die "FATAL: read_rawfile() cannot open file '$f': $!";
14 binmode $fh;
15 return do { local $/; <$fh> };
16 }
17
18 sub write_file {
19 my ($f, $data) = @_;
20 die "FATAL: write_file() no data" unless defined $data;
21 open my $fh, ">", $f or die "FATAL: write_file() cannot open file '$f': $!";
22 binmode $fh;
23 print $fh $data or die "FATAL: write_file() cannot write to '$f': $!";
24 close $fh or die "FATAL: write_file() cannot close '$f': $!";
25 return;
26 }
27
28 sub check_source {
29 my @all_files = (bsd_glob("makefile*"), bsd_glob("*.sh"), bsd_glob("*.pl"));
30 find({ wanted=>sub { push @all_files, $_ if -f $_ }, no_chdir=>1 }, qw/src tests demos/);
31
32 my $fails = 0;
33 for my $file (sort @all_files) {
34 next unless $file =~ /\.(c|h|pl|py|sh)$/ || basename($file) =~ /^makefile/i;
35 my $troubles = {};
36 my $lineno = 1;
37 my $content = read_file($file);
38 push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/;
39 for my $l (split /\n/, $content) {
40 push @{$troubles->{merge_conflict}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
41 push @{$troubles->{trailing_space}}, $lineno if $l =~ / $/;
42 push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i;
43 push @{$troubles->{non_ascii_char}}, $lineno if $l =~ /[^[:ascii:]]/;
44 push @{$troubles->{cpp_comment}}, $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\// || $l =~ /\/\/\s/);
45 # in ./src we prefer using XMEMCPY, XMALLOC, XFREE ...
46 push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
47 push @{$troubles->{unwanted_malloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmalloc\s*\(/;
48 push @{$troubles->{unwanted_realloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\brealloc\s*\(/;
49 push @{$troubles->{unwanted_calloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bcalloc\s*\(/;
50 push @{$troubles->{unwanted_free}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bfree\s*\(/;
51 push @{$troubles->{unwanted_memset}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemset\s*\(/;
52 push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
53 push @{$troubles->{unwanted_memmove}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemmove\s*\(/;
54 push @{$troubles->{unwanted_memcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcmp\s*\(/;
55 push @{$troubles->{unwanted_strcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrcmp\s*\(/;
56 push @{$troubles->{unwanted_clock}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bclock\s*\(/;
57 push @{$troubles->{unwanted_qsort}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bqsort\s*\(/;
58 push @{$troubles->{sizeof_no_brackets}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bsizeof\s*[^\(]/;
59 if ($file =~ m|src/.*\.c$| &&
60 $file !~ m|src/ciphers/.*\.c$| &&
61 $file !~ m|src/hashes/.*\.c$| &&
62 $file !~ m|src/math/.+_desc.c$| &&
63 $file !~ m|src/stream/sober128/sober128_stream.c$| &&
64 $l =~ /^static(\s+[a-zA-Z0-9_]+)+\s+([^_][a-zA-Z0-9_]+)\s*\(/) {
65 push @{$troubles->{staticfunc_name}}, "$lineno($2)";
66 }
67 $lineno++;
68 }
69 for my $k (sort keys %$troubles) {
70 warn "[$k] $file line:" . join(",", @{$troubles->{$k}}) . "\n";
71 $fails++;
72 }
73 }
74
75 warn( $fails > 0 ? "check-source: FAIL $fails\n" : "check-source: PASS\n" );
76 return $fails;
77 }
78
79 sub check_defines {
80 my $fails = 0;
81 my $cust_h = read_file("src/headers/tomcrypt_custom.h");
82 my $cryp_c = read_file("src/misc/crypt/crypt.c");
83 $cust_h =~ s|/\*.*?\*/||sg; # remove comments
84 $cryp_c =~ s|/\*.*?\*/||sg; # remove comments
85 my %def = map { $_ => 1 } map { my $x = $_; $x =~ s/^\s*#define\s+(LTC_\S+).*$/$1/; $x } grep { /^\s*#define\s+LTC_\S+/ } split /\n/, $cust_h;
86 for my $d (sort keys %def) {
87 next if $d =~ /^LTC_(DH\d+|ECC\d+|ECC_\S+|MPI|MUTEX_\S+\(x\)|NO_\S+)$/;
88 warn "$d missing in src/misc/crypt/crypt.c\n" and $fails++ if $cryp_c !~ /\Q$d\E/;
89 }
90 warn( $fails > 0 ? "check-defines: FAIL $fails\n" : "check-defines: PASS\n" );
91 return $fails;
92 }
93
94 sub check_descriptor {
95 my $which = shift;
96 my $what = shift;
97 my @src;
98 my @descriptors;
99 find({ wanted => sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir=>1 }, "./src/${which}/");
100 for my $f (@src) {
101 my @n = map { my $x = $_; $x =~ s/^.*?ltc_${what}_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_${what}_descriptor/ } split /\n/, read_file($f);
102 push @descriptors, @n if @n;
103 }
104 my $fails = 0;
105 for my $d (@descriptors) {
106 for my $f ("./src/misc/crypt/crypt_register_all_${which}.c") {
107 my $txt = read_file($f);
108 warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
109 }
110 }
111 for my $d (@descriptors) {
112 for my $f ("./tests/test.c") {
113 my $txt = read_file($f);
114 warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
115 }
116 }
117 my $name = sprintf("%-17s", "check-${which}:");
118 warn( $fails > 0 ? "${name}FAIL $fails\n" : "${name}PASS\n" );
119 return $fails;
120 }
121
122 sub check_descriptors {
123 my $fails = 0;
124 $fails = $fails + check_descriptor("ciphers", "cipher");
125 $fails = $fails + check_descriptor("hashes", "hash");
126 $fails = $fails + check_descriptor("prngs", "prng");
127 return $fails;
128 }
129
130 sub check_comments {
131 my $fails = 0;
132 my $first_comment = <<'MARKER';
133 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
134 *
135 * LibTomCrypt is a library that provides various cryptographic
136 * algorithms in a highly modular and flexible manner.
137 *
138 * The library is free for all purposes without any express
139 * guarantee it works.
140 */
141 MARKER
142 my $last_comment = <<'MARKER';
143 /* ref: $Format:%D$ */
144 /* git commit: $Format:%H$ */
145 /* commit time: $Format:%ai$ */
146 MARKER
147 my @all_files;
148 find({ wanted=> sub { push @all_files, $_ if $_ =~ /\.(c|h)$/ }, no_chdir=>1 }, 'demos', 'src', 'tests');
149 for my $f (@all_files) {
150 my $txt = read_file($f);
151 if ($txt !~ /^\Q$first_comment\E/s) {
152 warn "[first_comment] $f\n";
153 $fails++;
154 }
155 if ($txt !~ /\Q$last_comment\E\s*$/s) {
156 warn "[last_comment] $f\n";
157 $fails++;
158 }
159 }
160 warn( $fails > 0 ? "check-comments: FAIL $fails\n" : "check-comments: PASS\n" );
161 return $fails;
162 }
163
164 sub prepare_variable {
165 my ($varname, @list) = @_;
166 my $output = "$varname=";
167 my $len = length($output);
168 foreach my $obj (sort @list) {
169 $len = $len + length $obj;
170 $obj =~ s/\*/\$/;
171 if ($len > 100) {
172 $output .= "\\\n";
173 $len = length $obj;
174 }
175 $output .= $obj . ' ';
176 }
177 $output =~ s/ $//;
178 return $output;
179 }
180
181 sub prepare_msvc_files_xml {
182 my ($all, $exclude_re, $targets) = @_;
183 my $last = [];
184 my $depth = 2;
185
186 # sort files in the same order as visual studio (ugly, I know)
187 my @parts = ();
188 for my $orig (@$all) {
189 my $p = $orig;
190 $p =~ s|/|/~|g;
191 $p =~ s|/~([^/]+)$|/$1|g;
192 # now we have: 'src/pk/rsa/rsa_verify_hash.c' > 'src/~pk/~rsa/rsa_verify_hash.c'
193 my @l = map { sprintf "% -99s", $_ } split /\//, $p;
194 push @parts, [ $orig, join(':', @l) ];
195 }
196 my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } @parts;
197
198 my $files = "<Files>\r\n";
199 for my $full (@sorted) {
200 my @items = split /\//, $full; # split by '/'
201 $full =~ s|/|\\|g; # replace '/' bt '\'
202 shift @items; # drop first one (src)
203 pop @items; # drop last one (filename.ext)
204 my $current = \@items;
205 if (join(':', @$current) ne join(':', @$last)) {
206 my $common = 0;
207 $common++ while ($last->[$common] && $current->[$common] && $last->[$common] eq $current->[$common]);
208 my $back = @$last - $common;
209 if ($back > 0) {
210 $files .= ("\t" x --$depth) . "</Filter>\r\n" for (1..$back);
211 }
212 my $fwd = [ @$current ]; splice(@$fwd, 0, $common);
213 for my $i (0..scalar(@$fwd) - 1) {
214 $files .= ("\t" x $depth) . "<Filter\r\n";
215 $files .= ("\t" x $depth) . "\tName=\"$fwd->[$i]\"\r\n";
216 $files .= ("\t" x $depth) . "\t>\r\n";
217 $depth++;
218 }
219 $last = $current;
220 }
221 $files .= ("\t" x $depth) . "<File\r\n";
222 $files .= ("\t" x $depth) . "\tRelativePath=\"$full\"\r\n";
223 $files .= ("\t" x $depth) . "\t>\r\n";
224 if ($full =~ $exclude_re) {
225 for (@$targets) {
226 $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
227 $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
228 $files .= ("\t" x $depth) . "\t\tExcludedFromBuild=\"true\"\r\n";
229 $files .= ("\t" x $depth) . "\t\t>\r\n";
230 $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
231 $files .= ("\t" x $depth) . "\t\t\tName=\"VCCLCompilerTool\"\r\n";
232 $files .= ("\t" x $depth) . "\t\t\tAdditionalIncludeDirectories=\"\"\r\n";
233 $files .= ("\t" x $depth) . "\t\t\tPreprocessorDefinitions=\"\"\r\n";
234 $files .= ("\t" x $depth) . "\t\t/>\r\n";
235 $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
236 }
237 }
238 ########### aes_enc "hack" disabled - discussion: https://github.com/libtom/libtomcrypt/pull/158
239 # if ($full eq 'src\ciphers\aes\aes.c') { #hack
240 # my %cmd = (
241 # 'Debug|Win32' => [ 'Debug/aes.obj;Debug/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Debug/libtomcrypt.pch&quot; /YX /Fo&quot;Debug/&quot; /Fd&quot;Debug/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Debug/libtomcrypt.pch&quot; /YX /Fo&quot;Debug/aes_enc.obj&quot; /Fd&quot;Debug/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;' ],
242 # 'Release|Win32' => [ 'Release/aes.obj;Release/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Release/libtomcrypt.pch&quot; /YX /Fo&quot;Release/&quot; /Fd&quot;Release/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Release/libtomcrypt.pch&quot; /YX /Fo&quot;Release/aes_enc.obj&quot; /Fd&quot;Release/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;' ],
243 # );
244 # for (@$targets) {
245 # next unless $cmd{$_};
246 # $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
247 # $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
248 # $files .= ("\t" x $depth) . "\t\t>\r\n";
249 # $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
250 # $files .= ("\t" x $depth) . "\t\t\tName=\"VCCustomBuildTool\"\r\n";
251 # $files .= ("\t" x $depth) . "\t\t\tCommandLine=\"$cmd{$_}[1]\"\r\n";
252 # $files .= ("\t" x $depth) . "\t\t\tOutputs=\"$cmd{$_}[0]\"\r\n";
253 # $files .= ("\t" x $depth) . "\t\t/>\r\n";
254 # $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
255 # }
256 # }
257 $files .= ("\t" x $depth) . "</File>\r\n";
258 }
259 $files .= ("\t" x --$depth) . "</Filter>\r\n" for (@$last);
260 $files .= "\t</Files>";
261 return $files;
262 }
263
264 sub patch_file {
265 my ($content, @variables) = @_;
266 for my $v (@variables) {
267 if ($v =~ /^([A-Z0-9_]+)\s*=.*$/si) {
268 my $name = $1;
269 $content =~ s/\n\Q$name\E\b.*?[^\\]\n/\n$v\n/s;
270 }
271 else {
272 die "patch_file failed: " . substr($v, 0, 30) . "..";
273 }
274 }
275 return $content;
276 }
277
278 sub version_from_tomcrypt_h {
279 my $h = read_file(shift);
280 if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(.*)"/s) {
281 return "VERSION_PC=$1.$2.$3", "VERSION_LT=1:1", "VERSION=$1.$2.$3$4", "PROJECT_NUMBER=$1.$2.$3$4";
282 }
283 else {
284 die "#define SCRYPT not found in tomcrypt.h";
285 }
286 }
287
288 sub process_makefiles {
289 my $write = shift;
290 my $changed_count = 0;
291 my @c = ();
292 find({ no_chdir => 1, wanted => sub { push @c, $_ if -f $_ && $_ =~ /\.c$/ && $_ !~ /tab.c$/ } }, 'src');
293 my @h = ();
294 find({ no_chdir => 1, wanted => sub { push @h, $_ if -f $_ && $_ =~ /\.h$/ && $_ !~ /dh_static.h$/ } }, 'src');
295 my @all = ();
296 find({ no_chdir => 1, wanted => sub { push @all, $_ if -f $_ && $_ =~ /\.(c|h)$/ } }, 'src');
297 my @t = qw();
298 find({ no_chdir => 1, wanted => sub { push @t, $_ if $_ =~ /(common|no_prng|_tests?|test).c$/ } }, 'tests');
299
300 my @o = sort ('src/ciphers/aes/aes_enc.o', map { my $x = $_; $x =~ s/\.c$/.o/; $x } @c);
301 my $var_o = prepare_variable("OBJECTS", @o);
302 my $var_h = prepare_variable("HEADERS", (sort @h));
303 (my $var_obj = $var_o) =~ s/\.o\b/.obj/sg;
304
305 my $var_to = prepare_variable("TOBJECTS", sort map { my $x = $_; $x =~ s/\.c$/.o/; $x } @t);
306 (my $var_tobj = $var_to) =~ s/\.o\b/.obj/sg;
307
308 my @ver_version = version_from_tomcrypt_h("src/headers/tomcrypt.h");
309
310 # update MSVC project files
311 my $msvc_files = prepare_msvc_files_xml(\@all, qr/tab\.c$/, ['Debug|Win32', 'Release|Win32', 'Debug|x64', 'Release|x64']);
312 for my $m (qw/libtomcrypt_VS2008.vcproj/) {
313 my $old = read_file($m);
314 my $new = $old;
315 $new =~ s|<Files>.*</Files>|$msvc_files|s;
316 if ($old ne $new) {
317 write_file($m, $new) if $write;
318 warn "changed: $m\n";
319 $changed_count++;
320 }
321 }
322
323 # update OBJECTS + HEADERS in makefile*
324 for my $m (qw/ makefile makefile.shared makefile.unix makefile.mingw makefile.msvc makefile_include.mk doc\/Doxyfile /) {
325 my $old = read_file($m);
326 my $new = $m eq 'makefile.msvc' ? patch_file($old, $var_obj, $var_h, $var_tobj, @ver_version)
327 : patch_file($old, $var_o, $var_h, $var_to, @ver_version);
328 if ($old ne $new) {
329 write_file($m, $new) if $write;
330 warn "changed: $m\n";
331 $changed_count++;
332 }
333 }
334
335 if ($write) {
336 return 0; # no failures
337 }
338 else {
339 warn( $changed_count > 0 ? "check-makefiles: FAIL $changed_count\n" : "check-makefiles: PASS\n" );
340 return $changed_count;
341 }
342 }
343
344 sub die_usage {
345 die <<"MARKER";
346 usage: $0 -s OR $0 --check-source
347 $0 -c OR $0 --check-descriptors
348 $0 -d OR $0 --check-defines
349 $0 -o OR $0 --check-comments
350 $0 -m OR $0 --check-makefiles
351 $0 -a OR $0 --check-all
352 $0 -u OR $0 --update-makefiles
353 $0 --fixupind crypt.ind
354 MARKER
355 }
356
357 GetOptions( "s|check-source" => \my $check_source,
358 "c|check-descriptors" => \my $check_descriptors,
359 "d|check-defines" => \my $check_defines,
360 "o|check-comments" => \my $check_comments,
361 "m|check-makefiles" => \my $check_makefiles,
362 "a|check-all" => \my $check_all,
363 "u|update-makefiles" => \my $update_makefiles,
364 "f|fixupind=s" => \my $fixupind,
365 "h|help" => \my $help
366 ) or die_usage;
367
368 if ($fixupind) {
369 my $txt = read_file($fixupind);
370 $txt =~ s/^([^\n]*\n)/$1\n\\addcontentsline{toc}{chapter}{Index}\n/s;
371 write_file($fixupind, $txt);
372 exit 0;
373 }
374
375 my $failure;
376 $failure ||= check_source() if $check_all || $check_source;
377 $failure ||= check_defines() if $check_all || $check_defines;
378 $failure ||= check_descriptors() if $check_all || $check_descriptors;
379 $failure ||= check_comments() if $check_all || $check_comments;
380 $failure ||= process_makefiles(0) if $check_all || $check_makefiles;
381 $failure ||= process_makefiles(1) if $update_makefiles;
382
383 die_usage unless defined $failure;
384 exit $failure ? 1 : 0;