lib/VOMS/Lite/RSAHelper.pm

filter: Statementstmt 93.1% 81 / 87 Branchbran 50.0% 6 / 12 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 5 totaltotal 79.6% 98 / 123 CC 15 SCAR 19.1
CC
15
Cov
85%
rsadecrypt
11.1
rsaenc
6.1
rsaencrypt
2.0
CRAP 6.8
« lib/VOMS/Lite/ASN1Helper.pm ↑ Summary script/cert-init.pl »
1 package VOMS::Lite::RSAHelper;
2  
3 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Math::BigInt lib => 'GMP';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 @ISA = qw(Exporter);
10 1 %EXPORT_TAGS = ( );
11 1 @EXPORT_OK = qw( rsasign rsaencrypt rsaverify rsadecrypt );
12 1 @EXPORT = ( );
13 1 $VERSION = '0.20';
14  
15 ###############################################
16  
17 5 ▶sub rsasign { # use private key to encrypt
Subroutine rsasign: called CC=1 SCAR=0.0
Pod undocumented
18 5 return &rsaenc( "01", @_);
19 }
20  
21 ###############################################
22  
23 0 ▶sub rsaencrypt { # use public key to encrypt
Subroutine rsaencrypt: not called CC=1 SCAR=6.9
Pod undocumented
24 0 return &rsaenc( "02", @_);
25 }
26  
27 ###############################################
28  
29 2 ▶sub rsaverify {
Subroutine rsaverify: called CC=1 SCAR=0.0
Pod undocumented
30 2 return rsadecrypt( @_ );
31 }
32  
33 ###############################################
34  
35 2 ▶sub rsadecrypt {
Subroutine rsadecrypt: called CC=10 SCAR=24.1
Pod undocumented
36 2 my ($EDhex,$chex,$nhex)=@_;
37  
38 # Even up hex lengths into whole octets
39 2 $chex=~s/^.(..)*$/0$&/;
40 2 $nhex=~s/^.(..)*$/0$&/;
41 2 $EDhex=~s/^.(..)*$/0$&/;
42 2 my $khex=length($nhex);
43  
44 # Length of modulus and Data in octets
45 2 my $k=$khex/2;
46 2 my $EDlen=length($EDhex)/2;
47  
48 # Create Integer representing Data
49 2 my $x=Math::BigInt->bzero();
50 2 foreach (split(//,$EDhex)) {
51 256 $x->bmul(16);
52 256 $x->badd(hex($_));
53 }
54  
55 # Create Integer representing Modulus
56 2 my $n=Math::BigInt->bzero();
57 2 foreach (split(//,$nhex)) {
58 260 $n->bmul(16);
59 260 $n->badd(hex($_));
60 }
61  
62 # Create Integer representing Exponent
63 2 my $c=Math::BigInt->bzero();
64 2 foreach (split(//,$chex)) {
65 12 $c->bmul(16);
66 12 $c->badd(hex($_));
67 }
68  
69 # Do Big RSA Maths y = x^c mod n
70 2 my $y=Math::BigInt->bzero();
71 2 $y = $x->bmodpow($c,$n);
72  
73 # Get Encrypted Data Character String
74 2 my $Dhex=$y->as_hex();
75 2 $Dhex=~s/^0x//;
76 2 $Dhex=~s/^.(..)*$/0$&/; # Even up the length
77  
78 2 ▶ if ( length($Dhex) < ($khex-4) ) { # short string: BT must be 00 (NB RFC difference should be ($khex-2) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $Dhex < $khex - 4) { } 0 2 2
79 0 return $Dhex;
80 }
81 else { # long string: BT is any one of 00, 01 and 02
82 2 my $BT=substr($Dhex,0,2);
83 2 $Dhex=substr($Dhex,2);
84 2 ▶ if ( $BT eq "00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BT eq '00') { } 0 2 2
85 0 ▶ until ( substr($Dhex,0,2) ne '00' || $Dhex eq "" ) { $Dhex=substr($Dhex,2); }
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($Dhex, 0, 2) ne "00" or $Dhex eq "") 0 0 0
Condition0%0/3
substr($Dhex, 0, 2) ne "00" or $Dhex eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
substr($Dhex, 0, 2) ne "00" or $Dhex eq ""
substr($Dhex, 0, 2) ne "00" $Dhex eq ""
Truth table0%0/3
A or B
A= substr($Dhex, 0, 2) ne "00"B= $Dhex eq ""
ABresult
1X1
011
000
86 }
87 else { # BT = 01 or 02
88 2 ▶ until ( substr($Dhex,0,2) eq '00' || $Dhex eq "" ) { $Dhex=substr($Dhex,2); }
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($Dhex, 0, 2) eq "00" or $Dhex eq "") 52 2 54
Condition66%2/3
substr($Dhex, 0, 2) eq "00" or $Dhex eq ""
l!l&&r!l&&!r
2052
MC/DC50%1/2
substr($Dhex, 0, 2) eq "00" or $Dhex eq ""
substr($Dhex, 0, 2) eq "00" $Dhex eq ""
Truth table66%2/3
A or B
A= substr($Dhex, 0, 2) eq "00"B= $Dhex eq ""
ABresult
1X1
011
000
89 2 $Dhex=substr($Dhex,2);
90 }
91 }
92 2 return $Dhex;
93 }
94  
95 ###############################################
96  
97 5 ▶sub rsaenc { #RSA Algorythm as per RFC2313 (with tweak for openssl verification stuff)
Subroutine rsaenc: called CC=6 SCAR=18.0
Pod undocumented
98  
99 # Get block type, Data, HexKey, HexModulus
100 5 my ($BT,$Dhex,$chex,$nhex)=@_;
101  
102 # Even up hex lengths into whole octets
103 5 $chex=~s/^.(..)*$/0$&/;
104 5 $nhex=~s/^.(..)*$/0$&/;
105 5 $Dhex=~s/^.(..)*$/0$&/;
106 5 my $khex=length($nhex);
107  
108 # Length of modulus and Data in octets
109 5 my $k=$khex/2;
110 5 my $Dlen=length($Dhex)/2;
111  
112 # Barf if datalen is too long for RSA
113 5 ▶ ( $Dlen > ($k - 11) ) && die "Too much data to encrypt!";
Branch: 1 branch
BranchTrueFalseTotal
$Dlen > $k - 11 and die "Too much data to encrypt!" 0 5 5
114  
115 # Padding for signing (why - 4 and not - 3 as per RFC I don't know)
116 5 my $PS="ff" x ( $k - 4 - $Dlen);
117  
118 # If encrypting alter padding to random
119 0 ▶ if ( $BT eq "02" ) { $PS=~s/../unpack('H2',pack('i',int(rand(255)+1)))/ge; }
Branch: 1 branch
BranchTrueFalseTotal
if ($BT eq "02") 0 5 5
120  
121 # Make Encryption Block. EB = 00 || BT || PS || 00 || D
122 5 my $EB='00'.$BT.$PS.'00'.$Dhex;
123  
124 # Create Integer representing Data
125 5 my $x=Math::BigInt->bzero();
126 5 foreach (split(//,$EB)) {
127 640 $x->bmul(16);
128 640 $x->badd(hex($_));
129 }
130  
131 # Create Integer representing Modulus
132 5 my $n=Math::BigInt->bzero();
133 5 foreach (split(//,$nhex)) {
134 650 $n->bmul(16);
135 650 $n->badd(hex($_));
136 }
137  
138 # Create Integer representing Exponent
139 5 my $c=Math::BigInt->bzero();
140 5 foreach (split(//,$chex)) {
141 646 $c->bmul(16);
142 646 $c->badd(hex($_));
143 }
144  
145 # Do Big RSA Maths y = x^c mod n
146 5 my $y=Math::BigInt->bzero();
147 5 $y = $x->bmodpow($c,$n);
148  
149 # Get Encrypted Data Character String
150 5 my $ED=$y->as_hex();
151 5 $ED=~s/^0x//;
152 5 $ED=~s/^.(..)*$/0$&/;
153
154 # Send Hex Data back
155 5 return $ED;
156 }
157  
158 1 1;
159 __END__
« lib/VOMS/Lite/ASN1Helper.pm ↑ Summary script/cert-init.pl »