12
12
# OS dependent path separator
13
13
my $sep = File::Spec-> catfile(' ' , ' ' );
14
14
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
+
15
24
# A list of the functions that trade space for speed.
16
25
# They will be removed from the list if the option "-n" is given
17
26
my %fast_functions = (
@@ -295,7 +304,7 @@ sub gather_dependencies
295
304
296
305
sub start
297
306
{
298
- my ($sd , $td , $no , $id , $cm , $ch ) = @_ ;
307
+ my ($sd , $td , $no , $id , $mr , $ cm , $ch ) = @_ ;
299
308
300
309
my %depmap ;
301
310
my %user_functions ;
@@ -310,7 +319,15 @@ sub start
310
319
or die $td .$sep . " tommath.h not found, please check path to LibTomMath sources\n " ;
311
320
312
321
%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
+ }
314
331
315
332
# TODO: The chance is high that there is a proper Perl way to do it.
316
333
if ($id == 1) {
@@ -375,10 +392,16 @@ sub start
375
392
@tmp = ();
376
393
}
377
394
395
+ # Handle functions in files with filenames that do not follow the scheme bn(s_)?_mp([a-z_0-9])+.c
396
+
378
397
# If we use fast multiplication/division we need the cutoffs, too. They are in bn_cutoffs.c
379
398
if (any {/ kara|toom/ } @dependency_list ) {
380
399
push @dependency_list , " cutoffs" ;
381
400
}
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
+ }
382
405
383
406
# Change makefiles (and MSVC's project file)
384
407
if ($cm == 1) {
@@ -394,6 +417,21 @@ sub start
394
417
if ( ($ch == 1) || ( ($cm + $ch ) == 0 )) {
395
418
write_header($td , @dependency_list );
396
419
}
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
+
397
435
return 1;
398
436
}
399
437
@@ -404,37 +442,63 @@ sub die_usage {
404
442
$0 -t OR $0 --tommath-dir=dir [./libtommath]
405
443
$0 -n OR $0 --no-optimization [with optimizations]
406
444
$0 -d OR $0 --include-deprecated [no]
445
+ $0 -r OR $0 --rsa [no]
446
+ $0 -R OR $0 --reduced-rsa [no]
407
447
$0 -m OR $0 --change-makefiles [no]
408
448
$0 -c OR $0 --change-headers [default]
409
449
410
450
The option --source-dir accepts a directory or a single file.
411
451
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
+
412
469
EOO
413
470
}
414
471
415
472
my $source_dir = " " ;
416
473
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.
419
476
# my $config_file = "";
420
477
my $no_optimizations = 0;
421
478
my $include_deprecated = 0;
479
+ my $minimum_rsa = 0;
480
+ my $reduced_rsa = 0;
422
481
my $change_makefiles = 0;
423
482
my $change_headers = 0;
424
483
425
484
GetOptions( " s|source-dir=s" => \$source_dir ,
426
485
" t|tommath-dir=s" => \$tommath_dir ,
427
486
" n|no-optimizations" => \$no_optimizations ,
428
487
" d|include-deprecated" => \$include_deprecated ,
488
+ " r|rsa" => \$minimum_rsa ,
489
+ " R|reduced-rsa" => \$reduced_rsa ,
429
490
" m|change-makefiles" => \$change_makefiles ,
430
491
" c|change-headers" => \$change_headers ,
431
492
" h|help" => \my $help
432
493
) or die_usage;
433
494
495
+ $minimum_rsa = 2 if $reduced_rsa == 1;
496
+
434
497
my $exit_value = start($source_dir ,
435
498
$tommath_dir ,
436
499
$no_optimizations ,
437
500
$include_deprecated ,
501
+ $minimum_rsa ,
438
502
$change_makefiles ,
439
503
$change_headers );
440
504
# TODO: checks&balances&cleanup
0 commit comments