Skip to content

Commit cc8fc5a

Browse files
committed
additional options to build RSA packets
1 parent 961b277 commit cc8fc5a

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

etc/make_small_lib.pl

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
# OS dependent path separator
1313
my $sep = File::Spec->catfile('', '');
1414

15+
# Minimum needed to do RSA
16+
my %rsa_functions = (
17+
rsa_functions => "mp_shrink,mp_lcm,s_mp_prime_random_ex,mp_invmod,mp_gcd,mp_mod," .
18+
"mp_mulmod,mp_addmod,mp_exptmod,mp_set_u32,mp_init_multi," .
19+
"mp_clear_multi,mp_unsigned_bin_size,mp_to_unsigned_bin," .
20+
"mp_mod_d,mp_prime_rabin_miller_trials,s_mp_reverse"
21+
);
22+
23+
1524
# A list of the functions that trade space for speed.
1625
# They will be removed from the list if the option "-n" is given
1726
my %fast_functions = (
@@ -295,7 +304,7 @@ sub gather_dependencies
295304

296305
sub start
297306
{
298-
my ($sd, $td, $no, $id, $cm, $ch) = @_;
307+
my ($sd, $td, $no, $id, $mr, $cm, $ch) = @_;
299308

300309
my %depmap;
301310
my %user_functions;
@@ -310,7 +319,15 @@ sub start
310319
or die $td.$sep . "tommath.h not found, please check path to LibTomMath sources\n";
311320

312321
%depmap = gather_functions($td);
313-
%user_functions = gather_functions($sd);
322+
if($mr > 0) {
323+
# For simplicity, reuse the already working mechanic.
324+
%user_functions = %rsa_functions;
325+
# We want it small, hence no space-grabbing optimizations.
326+
$no = 1;
327+
}
328+
else {
329+
%user_functions = gather_functions($sd);
330+
}
314331

315332
# TODO: The chance is high that there is a proper Perl way to do it.
316333
if($id == 1) {
@@ -375,10 +392,16 @@ sub start
375392
@tmp = ();
376393
}
377394

395+
# Handle functions in files with filenames that do not follow the scheme bn(s_)?_mp([a-z_0-9])+.c
396+
378397
# If we use fast multiplication/division we need the cutoffs, too. They are in bn_cutoffs.c
379398
if (any {/kara|toom/} @dependency_list) {
380399
push @dependency_list, "cutoffs";
381400
}
401+
# if we do anything with primes we need the primes-table which is in bn_prime_tab.c
402+
if (any {/prime/} @dependency_list) {
403+
push @dependency_list, "prime_tab";
404+
}
382405

383406
# Change makefiles (and MSVC's project file)
384407
if ($cm == 1) {
@@ -394,6 +417,21 @@ sub start
394417
if ( ($ch == 1) || ( ($cm + $ch) == 0 )) {
395418
write_header($td, @dependency_list);
396419
}
420+
# Add "#define BN_MP_DIV_SMALL" to "tommath_superclass.h" for the RSA minimal set.
421+
if($mr == 1) {
422+
my $tsch = $td . $sep . "tommath_superclass.h";
423+
my $define = "/* LibTomMath, multiple-precision integer library -- Tom St Denis */\n";
424+
$define = $define . "/* SPDX-License-Identifier: Unlicense */\n";
425+
$define = $define . "#define BN_MP_DIV_SMALL\n";
426+
# make it even smaller
427+
if($mr == 2) {
428+
$define = $define . "#undef BN_S_MP_MUL_DIGS_C\n";
429+
$define = $define . "#undef BN_S_MP_SQR_C\n";
430+
$define = $define . "#undef BN_MP_MONTGOMERY_REDUCE_C\n";
431+
}
432+
write_file($tsch, $define);
433+
}
434+
397435
return 1;
398436
}
399437

@@ -404,37 +442,63 @@ sub die_usage {
404442
$0 -t OR $0 --tommath-dir=dir [./libtommath]
405443
$0 -n OR $0 --no-optimization [with optimizations]
406444
$0 -d OR $0 --include-deprecated [no]
445+
$0 -r OR $0 --rsa [no]
446+
$0 -R OR $0 --reduced-rsa [no]
407447
$0 -m OR $0 --change-makefiles [no]
408448
$0 -c OR $0 --change-headers [default]
409449
410450
The option --source-dir accepts a directory or a single file.
411451
452+
The option --rsa builds the bare minimum to do RSA. It also defines
453+
454+
BN_MP_DIV_SMALL
455+
456+
in "tommath_superclass.h".
457+
458+
The option --reduced-rsa does the same as --rsa but it also removes the
459+
functions
460+
461+
s_mp_mul_digs
462+
s_mp_sqr
463+
mp_montgomery_reduce
464+
465+
which makes the maximum length of RSA keys dependent on the size of MP_WARRAY.
466+
The resulting limits are small, even on 64-bit architectures and this option
467+
is therefore not recommended for use in the open.
468+
412469
EOO
413470
}
414471

415472
my $source_dir = "";
416473
my $tommath_dir = "libtommath" . $sep;
417-
# The option of parsing a dir/file given to $source_dir makes a dedicated config-file
418-
# option obsolete.
474+
# The option of parsing a dir/file given to $source_dir makes a dedicated
475+
# config-file obsolete.
419476
# my $config_file = "";
420477
my $no_optimizations = 0;
421478
my $include_deprecated = 0;
479+
my $minimum_rsa = 0;
480+
my $reduced_rsa = 0;
422481
my $change_makefiles = 0;
423482
my $change_headers = 0;
424483

425484
GetOptions( "s|source-dir=s" => \$source_dir,
426485
"t|tommath-dir=s" => \$tommath_dir,
427486
"n|no-optimizations" => \$no_optimizations,
428487
"d|include-deprecated" => \$include_deprecated,
488+
"r|rsa" => \$minimum_rsa,
489+
"R|reduced-rsa" => \$reduced_rsa,
429490
"m|change-makefiles" => \$change_makefiles,
430491
"c|change-headers" => \$change_headers,
431492
"h|help" => \my $help
432493
) or die_usage;
433494

495+
$minimum_rsa = 2 if $reduced_rsa == 1;
496+
434497
my $exit_value = start($source_dir,
435498
$tommath_dir,
436499
$no_optimizations,
437500
$include_deprecated,
501+
$minimum_rsa,
438502
$change_makefiles,
439503
$change_headers);
440504
# TODO: checks&balances&cleanup

0 commit comments

Comments
 (0)