blob: 977047af54d82ce8f7ee1339968a614e20deeaec [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,
Ronald Cron3d817ad2024-06-14 08:43:28 +02006# or generate_errors.pl crypto_include_dir tls_include_dir data_dir error_file
Bence Szépkúti700ee442020-05-26 00:33:31 +02007#
Bence Szépkúti1e148272020-08-07 13:07:28 +02008# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00009# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker9d781402011-05-09 16:17:09 +000010
11use strict;
Gilles Peskine58887ba2021-08-02 22:53:40 +020012use warnings;
Paul Bakker9d781402011-05-09 16:17:09 +000013
Ronald Cron3d817ad2024-06-14 08:43:28 +020014my ($crypto_include_dir, $tls_include_dir, $data_dir, $error_file);
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020015
16if( @ARGV ) {
Ronald Cron3d817ad2024-06-14 08:43:28 +020017 die "Invalid number of arguments" if scalar @ARGV != 4;
18 ($crypto_include_dir, $tls_include_dir, $data_dir, $error_file) = @ARGV;
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020019
Ronald Cron3d817ad2024-06-14 08:43:28 +020020 -d $crypto_include_dir or die "No such directory: $crypto_include_dir\n";
21 -d $tls_include_dir or die "No such directory: $tls_include_dir\n";
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020022 -d $data_dir or die "No such directory: $data_dir\n";
23} else {
Ronald Cron3d817ad2024-06-14 08:43:28 +020024 $crypto_include_dir = 'tf-psa-crypto/drivers/builtin/include/mbedtls';
25 $tls_include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020026 $data_dir = 'scripts/data_files';
Harry Ramseyd2bcdba2024-10-11 12:04:57 +010027 $error_file = 'library/error.c';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020028
Ronald Cron3d817ad2024-06-14 08:43:28 +020029 unless( -d $crypto_include_dir && -d $tls_include_dir && -d $data_dir ) {
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020030 chdir '..' or die;
Ronald Cron3d817ad2024-06-14 08:43:28 +020031 -d $crypto_include_dir && -d $tls_include_dir && -d $data_dir
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020032 or die "Without arguments, must be run from root or scripts\n"
33 }
34}
35
Paul Bakker9d781402011-05-09 16:17:09 +000036my $error_format_file = $data_dir.'/error.fmt';
37
TRodziewicz10e8cf52021-05-31 17:58:57 +020038my @low_level_modules = qw( AES ARIA ASN1 BASE64 BIGNUM
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020039 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
Harry Ramseyc44f67b2024-10-16 21:46:44 +010040 ENTROPY ERROR GCM HKDF HMAC_DRBG LMS MD5
Manuel Pégourié-Gonnard6a3b8772025-06-19 12:14:02 +020041 NET PBKDF2 PLATFORM POLY1305 RIPEMD160
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020042 SHA1 SHA256 SHA512 SHA3 THREADING );
Valerio Settiddc4b042025-02-12 13:49:32 +010043my @high_level_modules = qw( CIPHER ECP MD
Gilles Peskine92143272018-01-25 23:26:24 +010044 PEM PK PKCS12 PKCS5
Nayna Jainca07f062020-06-12 18:44:04 +000045 RSA SSL X509 PKCS7 );
Paul Bakker9d781402011-05-09 16:17:09 +000046
Paul Bakker9d781402011-05-09 16:17:09 +000047undef $/;
48
Gilles Peskinea4d32732021-04-23 09:44:59 +020049open(FORMAT_FILE, '<:crlf', "$error_format_file") or die "Opening error format file '$error_format_file': $!";
Paul Bakker9d781402011-05-09 16:17:09 +000050my $error_format = <FORMAT_FILE>;
51close(FORMAT_FILE);
52
Ronald Cron3d817ad2024-06-14 08:43:28 +020053my @files = glob qq("$crypto_include_dir/*.h");
54push(@files, glob qq("$tls_include_dir/*.h"));
Gilles Peskine5241f852020-05-25 12:21:22 +020055my @necessary_include_files;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010056my @matches;
57foreach my $file (@files) {
Thomas Daubney33878ed2023-01-09 18:17:02 +000058 open(FILE, '<:crlf', $file) or die("$0: $file: $!");
Gilles Peskine47b09562021-07-26 18:39:53 +020059 my $content = <FILE>;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010060 close FILE;
Gilles Peskine47b09562021-07-26 18:39:53 +020061 my $found = 0;
62 while ($content =~ m[
Gilles Peskine7f8e2772021-07-26 19:30:08 +020063 # Both the before-comment and the after-comment are optional.
64 # Only the comment content is a regex capture group. The comment
65 # start and end parts are outside the capture group.
66 (?:/\*[*!](?!<) # Doxygen before-comment start
67 ((?:[^*]|\*+[^*/])*) # $1: Comment content (no */ inside)
68 \*/)? # Comment end
69 \s*\#\s*define\s+(MBEDTLS_ERR_\w+) # $2: name
70 \s+\-(0[Xx][0-9A-Fa-f]+)\s* # $3: value (without the sign)
71 (?:/\*[*!]< # Doxygen after-comment start
72 ((?:[^*]|\*+[^*/])*) # $4: Comment content (no */ inside)
73 \*/)? # Comment end
Gilles Peskine47b09562021-07-26 18:39:53 +020074 ]gsx) {
75 my ($before, $name, $value, $after) = ($1, $2, $3, $4);
76 # Discard Doxygen comments that are coincidentally present before
77 # an error definition but not attached to it. This is ad hoc, based
78 # on what actually matters (or mattered at some point).
Gilles Peskine58887ba2021-08-02 22:53:40 +020079 undef $before if defined($before) && $before =~ /\s*\\name\s/s;
Gilles Peskine47b09562021-07-26 18:39:53 +020080 die "Description neither before nor after $name in $file\n"
81 if !defined($before) && !defined($after);
82 die "Description both before and after $name in $file\n"
83 if defined($before) && defined($after);
84 my $description = (defined($before) ? $before : $after);
85 $description =~ s/^\s+//;
Gilles Peskine05aa5432021-07-26 18:45:22 +020086 $description =~ s/\n( *\*)? */ /g;
Gilles Peskine47b09562021-07-26 18:39:53 +020087 $description =~ s/\.?\s+$//;
88 push @matches, [$name, $value, $description];
89 ++$found;
90 }
91 if ($found) {
92 my $include_name = $file;
93 $include_name =~ s!.*/!!;
Harry Ramseyc44f67b2024-10-16 21:46:44 +010094 $include_name = "error.h" if ($include_name eq "error_common.h");
Gilles Peskine47b09562021-07-26 18:39:53 +020095 push @necessary_include_files, $include_name;
96 }
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010097}
Paul Bakker9d781402011-05-09 16:17:09 +000098
Gabor Mezei588769c2025-04-24 12:11:26 +020099my @ll_old_define = ("", "", "");
100my @hl_old_define = ("", "", "");
Paul Bakker9d781402011-05-09 16:17:09 +0000101
102my $ll_code_check = "";
103my $hl_code_check = "";
104
105my $headers = "";
Gilles Peskine5241f852020-05-25 12:21:22 +0200106my %included_headers;
Paul Bakker9d781402011-05-09 16:17:09 +0000107
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000108my %error_codes_seen;
109
Gilles Peskine47b09562021-07-26 18:39:53 +0200110foreach my $match (@matches)
Paul Bakker9d781402011-05-09 16:17:09 +0000111{
Gilles Peskine47b09562021-07-26 18:39:53 +0200112 my ($error_name, $error_code, $description) = @$match;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000113
114 die "Duplicated error code: $error_code ($error_name)\n"
115 if( $error_codes_seen{$error_code}++ );
116
Paul Bakker9d781402011-05-09 16:17:09 +0000117 $description =~ s/\\/\\\\/g;
Paul Bakker9d781402011-05-09 16:17:09 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
Paul Bakker9d781402011-05-09 16:17:09 +0000120
121 # Fix faulty ones
122 $module_name = "BIGNUM" if ($module_name eq "MPI");
Paul Bakker880ac7e2011-11-27 14:50:49 +0000123 $module_name = "CTR_DRBG" if ($module_name eq "CTR");
Manuel Pégourié-Gonnardcf383672014-02-01 10:22:21 +0100124 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
Paul Bakker9d781402011-05-09 16:17:09 +0000125
126 my $define_name = $module_name;
Gilles Peskine1bf45e12020-02-26 18:28:23 +0100127 $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
Paul Bakkerdceecd82011-11-15 16:38:34 +0000128 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
Gilles Peskine314bc892020-02-26 18:28:25 +0100129 $define_name = "SSL_TLS" if ($define_name eq "SSL");
Paul Bakkercff68422013-09-15 20:43:33 +0200130 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
Nayna Jainca07f062020-06-12 18:44:04 +0000131 $define_name = "PKCS7" if ($define_name eq "PKCS7");
Gabor Mezei588769c2025-04-24 12:11:26 +0200132 $define_name = "ALG_SHA3_224,ALG_SHA3_256,ALG_SHA3_384,ALG_SHA3_512"
133 if ($define_name eq "SHA3");
134
135 my $define_prefix = "MBEDTLS_";
136 $define_prefix = "PSA_WANT_" if ($module_name eq "SHA3");
137
138 my $define_suffix = "_C";
139 $define_suffix = "" if ($module_name eq "SHA3");
Paul Bakker9d781402011-05-09 16:17:09 +0000140
141 my $include_name = $module_name;
142 $include_name =~ tr/A-Z/a-z/;
Paul Bakker9d781402011-05-09 16:17:09 +0000143
Gilles Peskine1fcf7212020-02-26 18:26:46 +0100144 # Fix faulty ones
145 $include_name = "net_sockets" if ($module_name eq "NET");
146
Gilles Peskine5241f852020-05-25 12:21:22 +0200147 $included_headers{"${include_name}.h"} = $module_name;
148
Paul Bakker9d781402011-05-09 16:17:09 +0000149 my $found_ll = grep $_ eq $module_name, @low_level_modules;
150 my $found_hl = grep $_ eq $module_name, @high_level_modules;
151 if (!$found_ll && !$found_hl)
152 {
Manuel Pégourié-Gonnard48573f82015-08-06 17:24:56 +0200153 printf("Error: Do not know how to handle: $module_name\n");
Paul Bakker9d781402011-05-09 16:17:09 +0000154 exit 1;
155 }
156
157 my $code_check;
158 my $old_define;
159 my $white_space;
Paul Bakkercff68422013-09-15 20:43:33 +0200160 my $first;
Paul Bakker9d781402011-05-09 16:17:09 +0000161
162 if ($found_ll)
163 {
164 $code_check = \$ll_code_check;
Gabor Mezei588769c2025-04-24 12:11:26 +0200165 $old_define = \@ll_old_define;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700166 $white_space = ' ';
Paul Bakker9d781402011-05-09 16:17:09 +0000167 }
168 else
169 {
170 $code_check = \$hl_code_check;
Gabor Mezei588769c2025-04-24 12:11:26 +0200171 $old_define = \@hl_old_define;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700172 $white_space = ' ';
Paul Bakker9d781402011-05-09 16:17:09 +0000173 }
174
Gabor Mezei588769c2025-04-24 12:11:26 +0200175 my $old_define_name = \${$old_define}[0];
176 my $old_define_prefix = \${$old_define}[1];
177 my $old_define_suffix = \${$old_define}[2];
178
179 if ($define_name ne ${$old_define_name})
Paul Bakker9d781402011-05-09 16:17:09 +0000180 {
Gabor Mezei588769c2025-04-24 12:11:26 +0200181 if (${$old_define_name} ne "")
Paul Bakker9d781402011-05-09 16:17:09 +0000182 {
Paul Bakkercff68422013-09-15 20:43:33 +0200183 ${$code_check} .= "#endif /* ";
184 $first = 0;
Gabor Mezei588769c2025-04-24 12:11:26 +0200185 foreach my $dep (split(/,/, ${$old_define_name}))
Paul Bakkercff68422013-09-15 20:43:33 +0200186 {
Gabor Mezei588769c2025-04-24 12:11:26 +0200187 ${$code_check} .= " || \n " if ($first++);
188 ${$code_check} .= "${$old_define_prefix}${dep}${$old_define_suffix}";
Paul Bakkercff68422013-09-15 20:43:33 +0200189 }
190 ${$code_check} .= " */\n\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000191 }
192
Paul Bakkercff68422013-09-15 20:43:33 +0200193 ${$code_check} .= "#if ";
194 $headers .= "#if " if ($include_name ne "");
195 $first = 0;
196 foreach my $dep (split(/,/, ${define_name}))
197 {
Gabor Mezei588769c2025-04-24 12:11:26 +0200198 ${$code_check} .= " || \\\n " if ($first);
199 $headers .= " || \\\n " if ($first++);
Paul Bakkercff68422013-09-15 20:43:33 +0200200
Gabor Mezei588769c2025-04-24 12:11:26 +0200201 ${$code_check} .= "defined(${define_prefix}${dep}${define_suffix})";
202 $headers .= "defined(${define_prefix}${dep}${define_suffix})"
203 if ($include_name ne "");
Paul Bakkercff68422013-09-15 20:43:33 +0200204 }
205 ${$code_check} .= "\n";
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000206 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
Paul Bakker9d781402011-05-09 16:17:09 +0000207 "#endif\n\n" if ($include_name ne "");
Gabor Mezei588769c2025-04-24 12:11:26 +0200208 ${$old_define_name} = $define_name;
209 ${$old_define_prefix} = $define_prefix;
210 ${$old_define_suffix} = $define_suffix;
Paul Bakker9d781402011-05-09 16:17:09 +0000211 }
212
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700213 ${$code_check} .= "${white_space}case -($error_name):\n".
Gaurav Aggarwalcabde252020-04-22 08:13:25 -0700214 "${white_space} return( \"$module_name - $description\" );\n"
Paul Bakker9d781402011-05-09 16:17:09 +0000215};
216
Gabor Mezei588769c2025-04-24 12:11:26 +0200217if ($ll_old_define[0] ne "")
Paul Bakker9d781402011-05-09 16:17:09 +0000218{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200219 $ll_code_check .= "#endif /* ";
220 my $first = 0;
Gabor Mezei588769c2025-04-24 12:11:26 +0200221 foreach my $dep (split(/,/, $ll_old_define[0]))
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200222 {
Gabor Mezei588769c2025-04-24 12:11:26 +0200223 $ll_code_check .= " || \n " if ($first++);
224 $ll_code_check .= "${ll_old_define[1]}${dep}${ll_old_define[2]}";
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200225 }
226 $ll_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000227}
Gabor Mezei588769c2025-04-24 12:11:26 +0200228if ($hl_old_define[0] ne "")
Paul Bakker9d781402011-05-09 16:17:09 +0000229{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200230 $hl_code_check .= "#endif /* ";
231 my $first = 0;
Gabor Mezei588769c2025-04-24 12:11:26 +0200232 foreach my $dep (split(/,/, $hl_old_define[0]))
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200233 {
Gabor Mezei588769c2025-04-24 12:11:26 +0200234 $hl_code_check .= " || \n " if ($first++);
235 $hl_code_check .= "${hl_old_define[1]}${dep}${hl_old_define[2]}";
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200236 }
237 $hl_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000238}
239
240$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
Gabor Mezei72cc7bb2025-04-24 16:26:37 +0200241$error_format =~ s/ *LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
242$error_format =~ s/ *HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
Paul Bakker9d781402011-05-09 16:17:09 +0000243
244open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
245print ERROR_FILE $error_format;
246close(ERROR_FILE);
Gilles Peskine5241f852020-05-25 12:21:22 +0200247
248my $errors = 0;
249for my $include_name (@necessary_include_files)
250{
251 if (not $included_headers{$include_name})
252 {
253 print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n";
254 ++$errors;
255 }
256}
257
258exit !!$errors;