OpenPGP/Util.pm

filter: Statementstmt 78.5% 55 / 70 Branchbran 76.9% 20 / 26 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 86.6% 13 / 15 Podpod 70.0% 7 / 10 totaltotal 78.4% 102 / 130 CC 17 SCAR 14.4
CC
17
Cov
78%
canonical_text
12.0
bigint2bin
6.0
get_random_bigint
4.5
CRAP 4.2
« OpenPGP/Message.pm ↑ Summary OpenPGP/S2k.pm »
1 package Crypt::OpenPGP::Util;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 16 our $VERSION = '1.21'; # VERSION
6  
7 # For some reason, FastCalc causes problems. Restrict to one of these 3 backends
8 16 ▶use Math::BigInt only => 'Pari,GMP,Calc';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 our @EXPORT_OK = qw( bitsize bin2bigint bin2mp bigint2bin mp2bin mod_exp mod_inverse
12 dash_escape dash_unescape canonical_text );
13  
14 35 ▶sub bitsize {
Subroutine bitsize: called CC=1 SCAR=0.0
Pod documented
15 35 my $bigint = Math::BigInt->new($_[0]);
16 35 return $bigint->bfloor($bigint->blog(2)) + 1;
17 }
18  
19 30 ▶sub bin2bigint { $_[0] ? Math::BigInt->new('0x' . unpack 'H*', $_[0]) : 0 }
Subroutine bin2bigint: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 29 1 30
20  
21 16 *bin2mp = \&bin2bigint;
22  
23 37 ▶sub bigint2bin {
Subroutine bigint2bin: called CC=6 SCAR=17.9
Pod documented
24 37 my($p) = @_;
25
26 37 $p = _ensure_bigint($p);
27
28 37 my $base = _ensure_bigint(1) << _ensure_bigint(4*8);
29 37 my $res = '';
30 37 ▶ while ($p != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p != 0) 349 37 386
31 349 my $r = $p % $base;
32 349 $p = ($p-$r) / $base;
33 349 my $buf = pack 'N', $r;
34 349 ▶ if ($p == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p == 0) 35 314 349
35 35 ▶ $buf = $r >= 16777216 ? $buf :
Branch: 3 branches
BranchTrueFalseTotal
$r >= 16777216 ? : 16 19 35
$r >= 65536 ? : 8 11 19
$r >= 256 ? : 2 9 11
36 $r >= 65536 ? substr($buf, -3, 3) :
37 $r >= 256 ? substr($buf, -2, 2) :
38 substr($buf, -1, 1);
39 }
40 349 $res = $buf . $res;
41 }
42 37 $res;
43 }
44  
45 16 *mp2bin = \&bigint2bin;
46  
47 1 ▶sub mod_exp {
Subroutine mod_exp: called CC=1 SCAR=0.0
Pod documented
48 1 my($a, $exp, $n) = @_;
49
50 1 $a = _ensure_bigint($a);
51
52 1 $a->copy->bmodpow($exp, $n);
53 }
54  
55 1 ▶sub mod_inverse {
Subroutine mod_inverse: called CC=1 SCAR=0.0
Pod documented
56 1 my($a, $n) = @_;
57
58 1 $a = _ensure_bigint($a);
59  
60 1 $a->copy->bmodinv($n);
61 }
62  
63 0 ▶sub dash_escape {
Subroutine dash_escape: not called CC=1 SCAR=6.9
Pod documented
64 0 my($data) = @_;
65 0 $data =~ s/^-/- -/mg;
66 0 $data;
67 }
68  
69 1 ▶sub dash_unescape {
Subroutine dash_unescape: called CC=1 SCAR=0.0
Pod undocumented
70 1 my($data) = @_;
71 1 $data =~ s/^-\s//mg;
72 1 $data;
73 }
74  
75 0 ▶sub canonical_text {
Subroutine canonical_text: not called CC=3 SCAR=24.8
Pod documented
76 0 my($text) = @_;
77 0 my @lines = split /\n/, $text, -1;
78 0 for my $l (@lines) {
79 ## pgp2 and pgp5 do not trim trailing whitespace from "canonical text"
80 ## signatures, only from cleartext signatures.
81 ## See:
82 ## http://cert.uni-stuttgart.de/archive/ietf-openpgp/2000/01/msg00033.html
83 0 ▶ if ($Crypt::OpenPGP::Globals::Trim_trailing_ws) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Crypt::OpenPGP::Globals::Trim_trailing_ws) { } 0 0 0
84 0 $l =~ s/[ \t\r\n]*$//;
85 } else {
86 0 $l =~ s/[\r\n]*$//;
87 }
88 }
89 0 join "\r\n", @lines;
90 }
91  
92  
93 113 ▶sub _ensure_bigint {
Subroutine _ensure_bigint: called CC=4 SCAR=13.9
94 113 my $num = shift;
95  
96 113 ▶ if ($num && (! ref $num || ! $num->isa('Math::BigInt'))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num and not ref $num && $num->isa("Math::BigInt")) 83 30 113
Condition83%5/6
$num and not ref $num && $num->isa("Math::BigInt")
!ll&&!rl&&r
22883
ref $num && $num->isa('Math::BigInt')
!ll&&!rl&&r
83028
MC/DC66%2/3
$num and not ref $num && $num->isa("Math::BigInt")
$num ref $num $num->isa('Math::BigInt')
Truth table75%3/4
A and not(B && C)
A= $numB= ref $numC= $num->isa('Math::BigInt')
ABCresult
0XX0
1110
10X1
1101
97 83 $num = Math::BigInt->new($num);
98 }
99
100 113 return $num;
101 }
102  
103 10 ▶sub get_random_bytes {
Subroutine get_random_bytes: called CC=3 SCAR=13.3
Pod undocumented
104 10 my $length = shift;
105 10 ▶ if (eval 'require Crypt::Random; 1;') {
Branch: 2 branches
BranchTrueFalseTotal
if (eval 'require Crypt::Random; 1;') { } 0 10 10
elsif (eval 'require Bytes::Random::Secure; 1;') { } 10 0 10
106 0 return Crypt::Random::makerandom_octet( Length => $length);
107 }
108 elsif (eval 'require Bytes::Random::Secure; 1;') {
109 10 return Bytes::Random::Secure::random_bytes($length);
110 }
111 else {
112 0 die "No random source available!";
113 }
114 }
115  
116 11 ▶sub get_random_bigint {
Subroutine get_random_bigint: called CC=4 SCAR=15.0
Pod undocumented
117 11 my $bits = shift;
118 11 ▶ if (eval 'require Crypt::Random; 1;') {
Branch: 2 branches
BranchTrueFalseTotal
if (eval 'require Crypt::Random; 1;') { } 0 11 11
elsif (eval 'require Bytes::Random::Secure; 1;') { } 11 0 11
119 0 my $pari = Crypt::Random::makerandom( Size => $bits, Strength => 0 );
120 0 return Math::BigInt->new($pari);
121 }
122 elsif (eval 'require Bytes::Random::Secure; 1;') {
123 11 my $hex = Bytes::Random::Secure::random_bytes_hex(int(($bits + 7) / 8));
124 11 my $val = Math::BigInt->new("0x$hex");
125 # Get exactly the correct number of bits.
126 11 ▶ $val->brsft(8 - ($bits & 7)) if ($bits & 7);
Branch: 1 branch
BranchTrueFalseTotal
if $bits & 7 9 2 11
127 # Make sure the top bit is set.
128 11 $val->bior(Math::BigInt->bone->blsft($bits-1));
129 11 return $val;
130 }
131 else {
132 0 die "No random source available!";
133 }
134 }
135  
136 16 1;
137 __END__
« OpenPGP/Message.pm ↑ Summary OpenPGP/S2k.pm »