| 1 | package Crypt::DSA::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Math::BigInt 1.78 try => 'GMP, Pari'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Crypt::SysRandom qw (random_bytes); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 8 | our $VERSION = '1.24'; #VERSION | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use vars qw( $VERSION @ISA @EXPORT_OK ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 8 | @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 18 | 8 | @EXPORT_OK = qw( bitsize bin2mp mp2bin mod_inverse mod_exp makerandom randombelow isprime ); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | ## Nicked from Crypt::RSA::DataFormat. | ||||||||||||||||||||||||||||
| 22 | ## Copyright (c) 2001, Vipul Ved Prakash. | ||||||||||||||||||||||||||||
| 23 | 163 | ▶ | sub bitsize { | ||||||||||||||||||||||||||
|
Subroutine
bitsize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 163 | length(Math::BigInt->new($_[0])->as_bin) - 2; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 418 | ▶ | sub bin2mp { | ||||||||||||||||||||||||||
|
Subroutine
bin2mp: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 418 | my $s = shift; | |||||||||||||||||||||||||||
| 29 | 418 | ▶ | $s eq '' ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | Math::BigInt->new(0) : | ||||||||||||||||||||||||||||
| 31 | Math::BigInt->new("0b" . unpack("B*", $s)); | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 3 | ▶ | sub mp2bin { | ||||||||||||||||||||||||||
|
Subroutine
mp2bin: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 3 | my $p = Math::BigInt->new(shift); | |||||||||||||||||||||||||||
| 36 | 3 | my $base = Math::BigInt->new(256); | |||||||||||||||||||||||||||
| 37 | 3 | my $res = ''; | |||||||||||||||||||||||||||
| 38 | 3 | ▶ | while ($p != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 41 | my $r = $p % $base; | |||||||||||||||||||||||||||
| 40 | 41 | $p = ($p-$r) / $base; | |||||||||||||||||||||||||||
| 41 | 41 | $res = chr($r) . $res; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 3 | $res; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 6 | ▶ | sub mod_exp { | ||||||||||||||||||||||||||
|
Subroutine
mod_exp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 6 | my($a, $exp, $n) = @_; | |||||||||||||||||||||||||||
| 48 | 6 | $a->copy->bmodpow($exp, $n); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | ▶ | sub mod_inverse { | ||||||||||||||||||||||||||
|
Subroutine
mod_inverse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 3 | my($a, $n) = @_; | |||||||||||||||||||||||||||
| 53 | 3 | $a->copy->bmodinv($n); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub makerandom { | ||||||||||||||||||||||||||
|
Subroutine
makerandom: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 0 | my %param = @_; | |||||||||||||||||||||||||||
| 58 | 0 | my $size = $param{Size}; | |||||||||||||||||||||||||||
| 59 | 0 | my $bytes = int($size / 8) + 1; | |||||||||||||||||||||||||||
| 60 | 0 | my $r = random_bytes($bytes); | |||||||||||||||||||||||||||
| 61 | 0 | my $down = $size - 1; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | $r = unpack 'H*', pack 'B*', '0' x ( $size % 8 ? 8 - $size % 8 : 0 ) . | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | '1' . unpack "b$down", $r; | ||||||||||||||||||||||||||||
| 64 | 0 | Math::BigInt->new('0x' . $r); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Uniform random integer in [0, $n-1] with no modulo bias (rejection | ||||||||||||||||||||||||||||
| 68 | # sampling). Unlike makerandom(), this does NOT force the high bit, so | ||||||||||||||||||||||||||||
| 69 | # it is correct for DSA nonces and private keys, which must be uniform | ||||||||||||||||||||||||||||
| 70 | # in [1, q-1]. makerandom() forces the high bit to obtain an exactly | ||||||||||||||||||||||||||||
| 71 | # N-bit value for prime search, which biases a nonce/key: folding its | ||||||||||||||||||||||||||||
| 72 | # output with "v -= q if v >= q" leaves the band [2^N-q, 2^(N-1)-1] | ||||||||||||||||||||||||||||
| 73 | # unreachable (CWE-330, biased-nonce -> lattice key recovery). | ||||||||||||||||||||||||||||
| 74 | 2 | ▶ | sub randombelow { | ||||||||||||||||||||||||||
|
Subroutine
randombelow: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 2 | my $n = shift; | |||||||||||||||||||||||||||
| 76 | 2 | ▶ | $n = Math::BigInt->new("$n") unless ref($n) eq 'Math::BigInt'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 2 | ▶ | croak "randombelow: argument must be > 0" unless $n > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 2 | my $bits = length($n->as_bin) - 2; | |||||||||||||||||||||||||||
| 79 | 2 | my $bytes = int(($bits + 7) / 8) + 1; # one byte of headroom | |||||||||||||||||||||||||||
| 80 | 2 | my $rmax = Math::BigInt->new(2) ** (8 * $bytes); | |||||||||||||||||||||||||||
| 81 | 2 | my $limit = $rmax - ($rmax % $n); # largest multiple of $n <= rmax | |||||||||||||||||||||||||||
| 82 | 2 | my $r; | |||||||||||||||||||||||||||
| 83 | 2 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | $r = Math::BigInt->new('0x' . unpack('H*', random_bytes($bytes))); | |||||||||||||||||||||||||||
| 85 | } while $r >= $limit; | ||||||||||||||||||||||||||||
| 86 | 2 | $r % $n; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # For testing, let us choose our isprime function: | ||||||||||||||||||||||||||||
| 90 | 8 | *isprime = \&isprime_algorithms_with_perl; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # CSPRNG-drawn Miller-Rabin base, uniform enough in [2, n-2]. A witness | ||||||||||||||||||||||||||||
| 93 | # only has to be a random base in range for the strong-pseudoprime test | ||||||||||||||||||||||||||||
| 94 | # to be sound, so (unlike a secret nonce) a plain draw with a few extra | ||||||||||||||||||||||||||||
| 95 | # bytes of headroom is fine -- the modulo bias is immaterial here. | ||||||||||||||||||||||||||||
| 96 | 158 | ▶ | sub _random_base { | ||||||||||||||||||||||||||
|
Subroutine
_random_base: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 158 | my ($n) = @_; | |||||||||||||||||||||||||||
| 98 | 158 | my $range = $n - 3; # 0 .. n-4 | |||||||||||||||||||||||||||
| 99 | 158 | my $bytes = int(bitsize($n) / 8) + 8; # headroom keeps bias negligible | |||||||||||||||||||||||||||
| 100 | 158 | my $r = Math::BigInt->new('0x' . unpack 'H*', random_bytes($bytes)); | |||||||||||||||||||||||||||
| 101 | 158 | ($r % $range) + 2; # 2 .. n-2 | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # from the book "Mastering Algorithms with Perl" by Jon Orwant, | ||||||||||||||||||||||||||||
| 105 | # Jarkko Hietaniemi, and John Macdonald | ||||||||||||||||||||||||||||
| 106 | 143 | ▶ | sub isprime_algorithms_with_perl { | ||||||||||||||||||||||||||
|
Subroutine
isprime_algorithms_with_perl: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 8 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 143 | my $n = shift; | |||||||||||||||||||||||||||
| 109 | 143 | ▶ | return 0 if $n < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 143 | ▶ | return 1 if $n < 4; # 2 and 3 are prime | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 143 | ▶ | return 0 unless $n % 2; # even n > 2 (also keeps _random_base's | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | # [2, n-2] range non-degenerate) | ||||||||||||||||||||||||||||
| 113 | 143 | my $n1 = $n - 1; | |||||||||||||||||||||||||||
| 114 | 143 | my $one = $n - $n1; # not just 1, but a bigint | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # find the power of two for the top bit of $n1 | ||||||||||||||||||||||||||||
| 117 | 143 | my $p2 = $one; | |||||||||||||||||||||||||||
| 118 | 143 | my $p2index = -1; | |||||||||||||||||||||||||||
| 119 | 143 | ▶ | ++$p2index, $p2 *= 2 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | while $p2 <= $n1; | ||||||||||||||||||||||||||||
| 121 | 143 | $p2 /= 2; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # number of iterations: 5 for 260-bit numbers, go up to 25 for smaller | ||||||||||||||||||||||||||||
| 124 | 143 | my $last_witness = 5; | |||||||||||||||||||||||||||
| 125 | 143 | ▶ | $last_witness += (260 - $p2index) / 13 if $p2index < 260; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 143 | for my $witness_count (1..$last_witness) { | |||||||||||||||||||||||||||
| 128 | # Fresh, independent CSPRNG witness every round. The old code | ||||||||||||||||||||||||||||
| 129 | # accumulated witnesses from int(rand(1024)) -- Perl's predictable | ||||||||||||||||||||||||||||
| 130 | # Mersenne-Twister PRNG, and correlated round-to-round -- which | ||||||||||||||||||||||||||||
| 131 | # both weakens each round and breaks the independence the | ||||||||||||||||||||||||||||
| 132 | # Miller-Rabin error bound assumes. | ||||||||||||||||||||||||||||
| 133 | 158 | my $witness = _random_base($n); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 158 | my $prod = $one; | |||||||||||||||||||||||||||
| 136 | 158 | my $n1bits = $n1; | |||||||||||||||||||||||||||
| 137 | 158 | my $p2next = $p2; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # compute $witness ** ($n - 1) | ||||||||||||||||||||||||||||
| 140 | 158 | while (1) { | |||||||||||||||||||||||||||
| 141 | 58367 | ▶ | my $rootone = $prod == 1 || $prod == $n1; | ||||||||||||||||||||||||||
|
Condition100%3/3
$prod == 1 || $prod == $n1
MC/DC100%2/2
$prod == 1 || $prod == $n1
$prod == 1
$prod == $n1
Truth table100%3/3
A || B
A= $prod == 1B= $prod == $n1
| |||||||||||||||||||||||||||||
| 142 | 58367 | $prod = ($prod * $prod) % $n; | |||||||||||||||||||||||||||
| 143 | 58367 | ▶ | return 0 if $prod == 1 && ! $rootone; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$prod == 1 and not $rootone
MC/DC100%2/2
$prod == 1 and not $rootone
$prod == 1
not $rootone
Truth table100%3/3
A and not(B)
A= $prod == 1B= not $rootone
| |||||||||||||||||||||||||||||
| 144 | 58366 | ▶ | if ($n1bits >= $p2next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 29383 | $prod = ($prod * $witness) % $n; | |||||||||||||||||||||||||||
| 146 | 29383 | $n1bits -= $p2next; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 58366 | ▶ | last if $p2next == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 58209 | $p2next /= 2; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 157 | ▶ | return 0 unless $prod == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 2 | return 1; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub isprime_gp_pari { | ||||||||||||||||||||||||||
|
Subroutine
isprime_gp_pari: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | 0 | my $n = shift; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | my $sn = "$n"; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | die if $sn =~ /\D/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | my $is_prime = `echo "isprime($sn)" | gp -f -q`; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | die "No gp installed?" if $?; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | chomp $is_prime; | |||||||||||||||||||||||||||
| 166 | 0 | return $is_prime; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | sub isprime_paranoid { | ||||||||||||||||||||||||||
|
Subroutine
isprime_paranoid: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 170 | 0 | my $n = shift; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | my $perl = isprime_algorithms_with_perl($n); | |||||||||||||||||||||||||||
| 173 | 0 | my $pari = isprime_gp_pari($n); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | die "Perl vs. PARI don't match on '$n'\n" unless $perl == $pari; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | return $perl; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 8 | 1; | |||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||