Convert drbg_lib to use OPENSSL_CTX for its global data
[openssl.git] / apps / progs.pl
1 #! /usr/bin/env perl
2 # Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 # Generate progs.h file by looking for command mains in list of C files
10 # passed on the command line.
11
12 use strict;
13 use warnings;
14 use lib '.';
15 use configdata qw/@disablables %unified_info/;
16
17 my %commands     = ();
18 my $cmdre        = qr/^\s*int\s+([a-z_][a-z0-9_]*)_main\(\s*int\s+argc\s*,/;
19 my $apps_openssl = shift @ARGV;
20 my $YEAR         = [localtime()]->[5] + 1900;
21
22 # because the program apps/openssl has object files as sources, and
23 # they then have the corresponding C files as source, we need to chain
24 # the lookups in %unified_info
25 my @openssl_source =
26     map { @{$unified_info{sources}->{$_}} }
27     grep { /\.o$/ }
28         @{$unified_info{sources}->{$apps_openssl}};
29
30 foreach my $filename (@openssl_source) {
31     open F, $filename or die "Couldn't open $filename: $!\n";
32     foreach ( grep /$cmdre/, <F> ) {
33         my @foo = /$cmdre/;
34         $commands{$1} = 1;
35     }
36     close F;
37 }
38
39 @ARGV = sort keys %commands;
40
41 print <<"EOF";
42 /*
43  * WARNING: do not edit!
44  * Generated by apps/progs.pl
45  *
46  * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
47  *
48  * Licensed under the Apache License 2.0 (the "License").  You may not use
49  * this file except in compliance with the License.  You can obtain a copy
50  * in the file LICENSE in the source distribution or at
51  * https://www.openssl.org/source/license.html
52  */
53
54 #include <openssl/lhash.h>
55 #include "opt.h"
56
57 typedef enum FUNC_TYPE {
58     FT_none, FT_general, FT_md, FT_cipher, FT_pkey,
59     FT_md_alg, FT_cipher_alg
60 } FUNC_TYPE;
61
62 typedef struct function_st {
63     FUNC_TYPE type;
64     const char *name;
65     int (*func)(int argc, char *argv[]);
66     const OPTIONS *help;
67 } FUNCTION;
68
69 DEFINE_LHASH_OF(FUNCTION);
70
71 EOF
72
73 foreach (@ARGV) {
74     printf "extern int %s_main(int argc, char *argv[]);\n", $_;
75 }
76 print "\n";
77
78 foreach (@ARGV) {
79     printf "extern const OPTIONS %s_options[];\n", $_;
80 }
81 print "\n";
82
83 my %cmd_disabler = (
84     ciphers  => "sock",
85     genrsa   => "rsa",
86     rsautl   => "rsa",
87     gendsa   => "dsa",
88     dsaparam => "dsa",
89     gendh    => "dh",
90     dhparam  => "dh",
91     ecparam  => "ec",
92     pkcs12   => "des",
93 );
94
95 print "#ifdef INCLUDE_FUNCTION_TABLE\n";
96 print "static FUNCTION functions[] = {\n";
97 foreach my $cmd ( @ARGV ) {
98     my $str = "    {FT_general, \"$cmd\", ${cmd}_main, ${cmd}_options},\n";
99     if ($cmd =~ /^s_/) {
100         print "#ifndef OPENSSL_NO_SOCK\n${str}#endif\n";
101     } elsif (grep { $cmd eq $_ } @disablables) {
102         print "#ifndef OPENSSL_NO_" . uc($cmd) . "\n${str}#endif\n";
103     } elsif (my $disabler = $cmd_disabler{$cmd}) {
104         print "#ifndef OPENSSL_NO_" . uc($disabler) . "\n${str}#endif\n";
105     } else {
106         print $str;
107     }
108 }
109
110 my %md_disabler = (
111     blake2b512 => "blake2",
112     blake2s256 => "blake2",
113 );
114 foreach my $cmd (
115     "md2", "md4", "md5",
116     "gost",
117     "sha1", "sha224", "sha256", "sha384",
118     "sha512", "sha512-224", "sha512-256",
119     "sha3-224", "sha3-256", "sha3-384", "sha3-512",
120     "shake128", "shake256",
121     "mdc2", "rmd160", "blake2b512", "blake2s256",
122     "sm3"
123 ) {
124     my $str = "    {FT_md, \"$cmd\", dgst_main},\n";
125     if (grep { $cmd eq $_ } @disablables) {
126         print "#ifndef OPENSSL_NO_" . uc($cmd) . "\n${str}#endif\n";
127     } elsif (my $disabler = $md_disabler{$cmd}) {
128         print "#ifndef OPENSSL_NO_" . uc($disabler) . "\n${str}#endif\n";
129     } else {
130         print $str;
131     }
132 }
133
134 my %cipher_disabler = (
135     des3  => "des",
136     desx  => "des",
137     cast5 => "cast",
138 );
139 foreach my $cmd (
140     "aes-128-cbc", "aes-128-ecb",
141     "aes-192-cbc", "aes-192-ecb",
142     "aes-256-cbc", "aes-256-ecb",
143     "aria-128-cbc", "aria-128-cfb",
144     "aria-128-ctr", "aria-128-ecb", "aria-128-ofb",
145     "aria-128-cfb1", "aria-128-cfb8",
146     "aria-192-cbc", "aria-192-cfb",
147     "aria-192-ctr", "aria-192-ecb", "aria-192-ofb",
148     "aria-192-cfb1", "aria-192-cfb8",
149     "aria-256-cbc", "aria-256-cfb",
150     "aria-256-ctr", "aria-256-ecb", "aria-256-ofb",
151     "aria-256-cfb1", "aria-256-cfb8",
152     "camellia-128-cbc", "camellia-128-ecb",
153     "camellia-192-cbc", "camellia-192-ecb",
154     "camellia-256-cbc", "camellia-256-ecb",
155     "base64", "zlib",
156     "des", "des3", "desx", "idea", "seed", "rc4", "rc4-40",
157     "rc2", "bf", "cast", "rc5",
158     "des-ecb", "des-ede", "des-ede3",
159     "des-cbc", "des-ede-cbc","des-ede3-cbc",
160     "des-cfb", "des-ede-cfb","des-ede3-cfb",
161     "des-ofb", "des-ede-ofb","des-ede3-ofb",
162     "idea-cbc","idea-ecb", "idea-cfb", "idea-ofb",
163     "seed-cbc","seed-ecb", "seed-cfb", "seed-ofb",
164     "rc2-cbc", "rc2-ecb", "rc2-cfb","rc2-ofb", "rc2-64-cbc", "rc2-40-cbc",
165     "bf-cbc", "bf-ecb", "bf-cfb", "bf-ofb",
166     "cast5-cbc","cast5-ecb", "cast5-cfb","cast5-ofb",
167     "cast-cbc", "rc5-cbc", "rc5-ecb", "rc5-cfb", "rc5-ofb",
168     "sm4-cbc", "sm4-ecb", "sm4-cfb", "sm4-ofb", "sm4-ctr"
169 ) {
170     my $str = "    {FT_cipher, \"$cmd\", enc_main, enc_options},\n";
171     (my $algo = $cmd) =~ s/-.*//g;
172     if ($cmd eq "zlib") {
173         print "#ifdef ZLIB\n${str}#endif\n";
174     } elsif (grep { $algo eq $_ } @disablables) {
175         print "#ifndef OPENSSL_NO_" . uc($algo) . "\n${str}#endif\n";
176     } elsif (my $disabler = $cipher_disabler{$algo}) {
177         print "#ifndef OPENSSL_NO_" . uc($disabler) . "\n${str}#endif\n";
178     } else {
179         print $str;
180     }
181 }
182
183 print "    {0, NULL, NULL}\n};\n";
184 print "#endif\n";