Debian Codegraph @

build-aux/scripts/gen-crypt-hashes-h

raw
1 #! /usr/bin/perl
2 # Written by Zack Weinberg <zackw at panix.com> in 2018 and 2020.
3 # To the extent possible under law, Zack Weinberg has waived all
4 # copyright and related or neighboring rights to this work.
5 #
6 # See https://creativecommons.org/publicdomain/zero/1.0/ for further
7 # details.
8
9 # Generate crypt-hashes.h from lib/hashes.conf and configure settings.
10 # Also responsible for checking lib/hashes.conf for errors.
11 #
12 # Caution: if you change the format of lib/hashes.conf you will
13 # probably need to modify expand-selected-hashes as well as this script.
14
15 use v5.14; # implicit use strict, use feature ':5.14'
16 use warnings FATAL => 'all';
17 use utf8;
18 use open qw(:std :utf8);
19 no if $] >= 5.022, warnings => 'experimental::re_strict';
20 use if $] >= 5.022, re => 'strict';
21
22 use File::Spec::Functions qw(splitpath);
23 use FindBin ();
24 use lib $FindBin::Bin;
25 use BuildCommon qw(
26 enabled_set
27 parse_hashes_conf
28 );
29
30 sub output {
31 my ($basehc, $hashes_enabled, $hconf) = @_;
32 my %enabled = enabled_set($hashes_enabled);
33 my @enabled_hashes;
34
35 print <<"EOT";
36 /* Generated by ${FindBin::Script} from $basehc. DO NOT EDIT. */
37
38 #ifndef _CRYPT_HASHES_H
39 #define _CRYPT_HASHES_H 1
40
41 EOT
42 for my $e (sort { $a->name cmp $b->name } values %{$hconf->hashes}) {
43 my $name = $e->name;
44 my $ena = $enabled{$name} // 0;
45 printf "#define INCLUDE_%-*s %d\n", $hconf->max_namelen, $name, $ena;
46 push @enabled_hashes, $e if $ena;
47 }
48
49 print <<'EOT';
50
51 /* Internal symbol renames for static linkage, see crypt-port.h. */
52 EOT
53 for my $e (@enabled_hashes) {
54 my $name_rn = $e->name . '_rn';
55 printf "#define crypt_%-*s _crypt_crypt_%s\n",
56 $hconf->max_namelen + 5, $name_rn, $name_rn;
57 printf "#define gensalt_%-*s _crypt_gensalt_%s\n",
58 $hconf->max_namelen + 3, $name_rn, $name_rn;
59 }
60
61 print <<'EOT';
62
63 /* Prototypes for hash algorithm entry points. */
64 EOT
65 for my $e (@enabled_hashes) {
66 my $name = $e->name;
67 print <<"EOT";
68 extern void crypt_${name}_rn (const char *, size_t, const char *,
69 size_t, uint8_t *, size_t, void *, size_t);
70 extern void gensalt_${name}_rn (unsigned long,
71 const uint8_t *, size_t, uint8_t *, size_t);
72
73 EOT
74 }
75
76 print <<'EOT';
77 #define HASH_ALGORITHM_TABLE_ENTRIES \
78 EOT
79 # Entries in this table can be in any order _except_ that the hash
80 # whose prefix is the empty string, if it's enabled, must be last.
81 # The simplest way to accomplish this is to sort the prefixes in
82 # descending order of length (and then alphabetically as a
83 # tiebreaker).
84 my @table_hashes = sort {
85 -(length($a->prefix) <=> length($b->prefix))
86 || $a->prefix cmp $b->prefix
87 } @enabled_hashes;
88
89 for my $e (@table_hashes) {
90 my $name_rn = $e->name . '_rn,';
91 my $q_prefix = '"' . $e->prefix . '",';
92 printf " { %-*s %d, crypt_%-*s gensalt_%-*s %2d, %d }, \\\n",
93 $hconf->max_prefixlen + 3, $q_prefix, length($e->prefix),
94 $hconf->max_namelen + 4, $name_rn,
95 $hconf->max_namelen + 4, $name_rn,
96 $e->nrbytes, $e->is_strong;
97 }
98 print " { 0, 0, 0, 0, 0, 0 }\n";
99
100 # The default_candidates array is in decreasing order of strength;
101 # select the first one that's enabled, if any.
102 my $default_prefix;
103 for my $e (@{$hconf->default_candidates}) {
104 if ($enabled{$e->name}) {
105 $default_prefix = $e->prefix;
106 last;
107 }
108 }
109 print <<"EOT" if defined $default_prefix;
110
111 #define HASH_ALGORITHM_DEFAULT "$default_prefix"
112 EOT
113 print <<'EOT';
114
115 #endif /* crypt-hashes.h */
116 EOT
117
118 close STDOUT or die "write error: $!\n";
119 return;
120 }
121
122 #
123 # Main
124 #
125 if (scalar(@ARGV) != 2) {
126 print {*STDERR} "usage: ${FindBin::Script}"
127 . "hashes.conf ,names,of,enabled,hashes,\n";
128 exit 1;
129 }
130 exit 0 if eval {
131 my ($hashes_conf, $hashes_enabled) = @ARGV;
132 my (undef, undef, $basehc) = splitpath($hashes_conf);
133
134 my $hconf = parse_hashes_conf($hashes_conf);
135 output($basehc, $hashes_enabled, $hconf);
136 1;
137 };
138
139 print {*STDERR} "${FindBin::Script}: $@";
140 exit 1;