blob: b4c014e3f1e27e2182f4ae637ad0ee25f5749c20 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02002
3# Generate error.c
Paul Bakker9d781402011-05-09 16:17:09 +00004#
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02005# Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments,
6# or generate_errors.pl include_dir data_dir error_file
Paul Bakker9d781402011-05-09 16:17:09 +00007
8use strict;
9
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020010my ($include_dir, $data_dir, $error_file);
11
12if( @ARGV ) {
13 die "Invalid number of arguments" if scalar @ARGV != 3;
14 ($include_dir, $data_dir, $error_file) = @ARGV;
15
16 -d $include_dir or die "No such directory: $include_dir\n";
17 -d $data_dir or die "No such directory: $data_dir\n";
18} else {
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020020 $data_dir = 'scripts/data_files';
21 $error_file = 'library/error.c';
22
23 unless( -d $include_dir && -d $data_dir ) {
24 chdir '..' or die;
25 -d $include_dir && -d $data_dir
26 or die "Without arguments, must be run from root or scripts\n"
27 }
28}
29
Paul Bakker9d781402011-05-09 16:17:09 +000030my $error_format_file = $data_dir.'/error.fmt';
31
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000032my @low_level_modules = qw( AES ARC4 ARIA ASN1 BASE64 BIGNUM BLOWFISH
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020033 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
Janos Follath60f6b642019-12-03 15:55:56 +000034 ENTROPY ERROR GCM HKDF HMAC_DRBG MD2 MD4 MD5
Jaeden Amero356acc82019-02-21 13:55:25 +000035 OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
Gilles Peskine92143272018-01-25 23:26:24 +010036 SHA1 SHA256 SHA512 THREADING XTEA );
37my @high_level_modules = qw( CIPHER DHM ECP MD
38 PEM PK PKCS12 PKCS5
Jaeden Amero43a450c2019-02-21 13:55:05 +000039 RSA );
Paul Bakker9d781402011-05-09 16:17:09 +000040
41my $line_separator = $/;
42undef $/;
43
44open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
45my $error_format = <FORMAT_FILE>;
46close(FORMAT_FILE);
47
48$/ = $line_separator;
49
Andres Amaya Garciad2da6222017-10-17 21:23:15 +010050my @files = <$include_dir/*.h>;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010051my @matches;
52foreach my $file (@files) {
53 open(FILE, "$file");
Andres Amaya Garcia69944b12017-10-17 21:24:56 +010054 my @grep_res = grep(/^\s*#define\s+MBEDTLS_ERR_\w+\s+\-0x[0-9A-Fa-f]+/, <FILE>);
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010055 push(@matches, @grep_res);
56 close FILE;
57}
Paul Bakker9d781402011-05-09 16:17:09 +000058
59my $ll_old_define = "";
60my $hl_old_define = "";
61
62my $ll_code_check = "";
63my $hl_code_check = "";
64
65my $headers = "";
66
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +000067my %error_codes_seen;
68
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010069
70foreach my $line (@matches)
Paul Bakker9d781402011-05-09 16:17:09 +000071{
Paul Bakker36713e82013-09-17 13:25:29 +020072 next if ($line =~ /compat-1.2.h/);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 my ($error_name, $error_code) = $line =~ /(MBEDTLS_ERR_\w+)\s+\-(0x\w+)/;
Paul Bakker9d781402011-05-09 16:17:09 +000074 my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +000075
76 die "Duplicated error code: $error_code ($error_name)\n"
77 if( $error_codes_seen{$error_code}++ );
78
Paul Bakker9d781402011-05-09 16:17:09 +000079 $description =~ s/\\/\\\\/g;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +000080 if ($description eq "") {
81 $description = "DESCRIPTION MISSING";
82 warn "Missing description for $error_name\n";
83 }
Paul Bakker9d781402011-05-09 16:17:09 +000084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
Paul Bakker9d781402011-05-09 16:17:09 +000086
87 # Fix faulty ones
88 $module_name = "BIGNUM" if ($module_name eq "MPI");
Paul Bakker880ac7e2011-11-27 14:50:49 +000089 $module_name = "CTR_DRBG" if ($module_name eq "CTR");
Manuel Pégourié-Gonnardcf383672014-02-01 10:22:21 +010090 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
Paul Bakker9d781402011-05-09 16:17:09 +000091
92 my $define_name = $module_name;
Paul Bakkerdceecd82011-11-15 16:38:34 +000093 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
Paul Bakkercff68422013-09-15 20:43:33 +020094 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
Paul Bakker9d781402011-05-09 16:17:09 +000095
96 my $include_name = $module_name;
97 $include_name =~ tr/A-Z/a-z/;
98 $include_name = "" if ($include_name eq "asn1");
99
Paul Bakker9d781402011-05-09 16:17:09 +0000100 my $found_ll = grep $_ eq $module_name, @low_level_modules;
101 my $found_hl = grep $_ eq $module_name, @high_level_modules;
102 if (!$found_ll && !$found_hl)
103 {
Manuel Pégourié-Gonnard48573f82015-08-06 17:24:56 +0200104 printf("Error: Do not know how to handle: $module_name\n");
Paul Bakker9d781402011-05-09 16:17:09 +0000105 exit 1;
106 }
107
108 my $code_check;
109 my $old_define;
110 my $white_space;
Paul Bakkercff68422013-09-15 20:43:33 +0200111 my $first;
Paul Bakker9d781402011-05-09 16:17:09 +0000112
113 if ($found_ll)
114 {
115 $code_check = \$ll_code_check;
116 $old_define = \$ll_old_define;
117 $white_space = ' ';
118 }
119 else
120 {
121 $code_check = \$hl_code_check;
122 $old_define = \$hl_old_define;
123 $white_space = ' ';
124 }
125
126 if ($define_name ne ${$old_define})
127 {
128 if (${$old_define} ne "")
129 {
Paul Bakkercff68422013-09-15 20:43:33 +0200130 ${$code_check} .= "#endif /* ";
131 $first = 0;
132 foreach my $dep (split(/,/, ${$old_define}))
133 {
134 ${$code_check} .= " || " if ($first++);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 ${$code_check} .= "MBEDTLS_${dep}_C";
Paul Bakkercff68422013-09-15 20:43:33 +0200136 }
137 ${$code_check} .= " */\n\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000138 }
139
Paul Bakkercff68422013-09-15 20:43:33 +0200140 ${$code_check} .= "#if ";
141 $headers .= "#if " if ($include_name ne "");
142 $first = 0;
143 foreach my $dep (split(/,/, ${define_name}))
144 {
145 ${$code_check} .= " || " if ($first);
146 $headers .= " || " if ($first++);
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
149 $headers .= "defined(MBEDTLS_${dep}_C)" if
Paul Bakkercff68422013-09-15 20:43:33 +0200150 ($include_name ne "");
151 }
152 ${$code_check} .= "\n";
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000153 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
Paul Bakker9d781402011-05-09 16:17:09 +0000154 "#endif\n\n" if ($include_name ne "");
155 ${$old_define} = $define_name;
156 }
157
Jaeden Amerob58ff952019-02-21 13:53:31 +0000158 ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
159 "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n"
Paul Bakker9d781402011-05-09 16:17:09 +0000160};
161
162if ($ll_old_define ne "")
163{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200164 $ll_code_check .= "#endif /* ";
165 my $first = 0;
166 foreach my $dep (split(/,/, $ll_old_define))
167 {
168 $ll_code_check .= " || " if ($first++);
169 $ll_code_check .= "MBEDTLS_${dep}_C";
170 }
171 $ll_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000172}
173if ($hl_old_define ne "")
174{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200175 $hl_code_check .= "#endif /* ";
176 my $first = 0;
177 foreach my $dep (split(/,/, $hl_old_define))
178 {
179 $hl_code_check .= " || " if ($first++);
180 $hl_code_check .= "MBEDTLS_${dep}_C";
181 }
182 $hl_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000183}
184
185$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
186$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
187$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
188
189open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
190print ERROR_FILE $error_format;
191close(ERROR_FILE);