OpenSSL/Algorithm/ECC.pm

filter: Statementstmt 93.7% 151 / 161 Branchbran 63.0% 29 / 46 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 88.8% 16 / 18 Podpod 50.0% 2 / 4 totaltotal 79.0% 204 / 258 CC 30 SCAR 21.1
CC
30
Cov
83%
verify
16.0
sign
11.8
_generate_k
4.1
CRAP 8.2
« OpenSSL/Algorithm/RSA.pm ↑ Summary OpenSSL/Util/PEM.pm »
1 package Crypt::JWS::OpenSSL::Algorithm::ECC;
2 5 $Crypt::JWS::OpenSSL::Algorithm::ECC::VERSION = '0.005';
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 with qw(
5 Crypt::JWS::OpenSSL::Role::Algorithm
6 Crypt::JWS::OpenSSL::Role::Encoder
7 );
8 5 ▶use Crypt::OpenSSL::Random 0.15;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Crypt::OpenSSL::EC 1.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Crypt::OpenSSL::ECDSA 0.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Crypt::OpenSSL::Bignum 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Crypt::OpenSSL::Bignum::CTX;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 my $ECC_ALGO_MAP = {
18 'ES256' => [ \&Digest::SHA::sha256, 'prime256v1', 32, 415, qr!\x04\x20(.{32})!s, \&Digest::SHA::hmac_sha256 ],
19 'ES384' => [ \&Digest::SHA::sha384, 'secp384r1', 48, 715, qr!\x04\x30(.{48})!s, \&Digest::SHA::hmac_sha384 ],
20 'ES512' => [ \&Digest::SHA::sha512, 'secp521r1', 66, 716, qr!\x04\x42(.{66})!s, \&Digest::SHA::hmac_sha512 ],
21 };
22  
23 0 ▶sub can_do_non_deterministic { 1 }
Subroutine can_do_non_deterministic: not called CC=1 SCAR=6.9
Pod undocumented
24 0 ▶sub can_do_deterministic { 1 }
Subroutine can_do_deterministic: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 36 ▶sub sign {
Subroutine sign: called CC=11 SCAR=24.7
Pod documented
27 36 my( $self, $request) = parse_params(@_);
28 36 my $algorithm = $request->{algorithm};
29 36 my ($hash_method, $curve, $coord_len, $nid, $priv_key_qr, $hmac_method, $qlen ) = @{ $ECC_ALGO_MAP->{$algorithm} };
30
31 36 my $key_ref = ref($request->{key});
32
33 36 my $hex_private_key;
34
35 36 ▶ if ( $key_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_ref) { } 6 30 36
36 6 ▶ if ( $key_ref eq 'HASH' && exists($request->{key}->{d}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_ref eq 'HASH' and exists $request->{'key'}{'d'}) { } 6 0 6
Condition33%1/3
$key_ref eq "HASH" and exists $request->{"key"}{"d"}
!ll&&!rl&&r
006
MC/DC0%0/2
$key_ref eq "HASH" and exists $request->{"key"}{"d"}
$key_ref eq "HASH" exists $request->{"key"}{"d"}
Truth table33%1/3
A and B
A= $key_ref eq "HASH"B= exists $request->{"key"}{"d"}
ABresult
0X0
100
111
37 6 my $keybin = $self->decode_jwk_element($request->{key}->{d});
38 6 $hex_private_key = unpack("H*", $keybin);
39 } else {
40 0 croak 'failed to parse private key';
41 }
42 } else {
43 30 $hex_private_key = $self->_load_private_key_as_hex($request->{key}, $priv_key_qr );
44 }
45
46 36 ▶ unless( $hex_private_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hex_private_key) 0 36 36
47 0 croak "Could not extract a ${coord_len}-byte private key scalar from the private key";
48 }
49
50 36 my $ecc_key = Crypt::OpenSSL::EC::EC_KEY::new_by_curve_name($nid);
51 36 ▶ die "Failed to initialize EC_KEY for curve NID $curve" unless $ecc_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $ecc_key 0 36 36
52
53 36 ▶ unless(Crypt::OpenSSL::EC::EC_KEY::set_private_key(
Branch: 1 branch
BranchTrueFalseTotal
unless (&Crypt::OpenSSL::EC::EC_KEY::set_private_key($ecc_key, scalar "Crypt::OpenSSL::Bignum"->new_from_hex($hex_private_key))) 0 36 36
54 $ecc_key, Crypt::OpenSSL::Bignum->new_from_hex($hex_private_key)
55 )
56 ) {
57 0 croak "Failed to set private key on the EC_KEY object.";
58 }
59
60 36 my $digest = $hash_method->($request->{message});
61
62 36 my $sig_obj;
63
64 36 ▶ if ( $request->{non_deterministic} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{'non_deterministic'}) { } 18 18 36
65 18 ▶ $sig_obj = Crypt::OpenSSL::ECDSA::ECDSA_do_sign($digest, $ecc_key)
Branch: 1 branch
BranchTrueFalseTotal
$sig_obj = &Crypt::OpenSSL::ECDSA::ECDSA_do_sign($digest, $ecc_key) or croak("Signing failed") 18 0 18
66 or croak "Signing failed";
67 } else {
68 18 my $group = Crypt::OpenSSL::EC::EC_GROUP::new_by_curve_name($nid);
69 18 my $ctx = Crypt::OpenSSL::Bignum::CTX->new();
70 18 my $q = Crypt::OpenSSL::Bignum->new;
71 18 ▶ Crypt::OpenSSL::EC::EC_GROUP::get_order($group, $q, $ctx)
Branch: 1 branch
BranchTrueFalseTotal
&Crypt::OpenSSL::EC::EC_GROUP::get_order($group, $q, $ctx) or croak("failed to initialize curve order") 18 0 18
72 or croak 'failed to initialize curve order';
73
74 18 my $k = _generate_k(
75 $q->to_hex,
76 $hex_private_key,
77 $digest,
78 $hmac_method,
79 $coord_len,
80 $q->num_bits
81 );
82
83 # Calculate kinv
84 18 my $q_minus_two = $q->sub(Crypt::OpenSSL::Bignum->new_from_word(2));
85 18 my $kinv = $k->mod_exp($q_minus_two, $q, $ctx);
86
87 # Calculate rp
88 18 my $R = Crypt::OpenSSL::EC::EC_POINT::new($group);
89 18 Crypt::OpenSSL::EC::EC_POINT::mul($group, $R, $k, \0, \0, $ctx);
90
91 18 my $rx = Crypt::OpenSSL::Bignum->new();
92 18 my $ry = Crypt::OpenSSL::Bignum->new();
93
94 18 Crypt::OpenSSL::EC::EC_POINT::get_affine_coordinates_GFp($group, $R, $rx, $ry, $ctx);
95
96 18 my (undef, $rp) = $rx->div($q, $ctx);
97
98 18 ▶ $sig_obj = Crypt::OpenSSL::ECDSA::ECDSA_do_sign_ex($digest, $kinv, $rp, $ecc_key)
Branch: 1 branch
BranchTrueFalseTotal
$sig_obj = &Crypt::OpenSSL::ECDSA::ECDSA_do_sign_ex($digest, $kinv, $rp, $ecc_key) or croak("Signing failed") 18 0 18
99 or croak "Signing failed";
100 }
101
102 36 my $r_oct = $sig_obj->get_r();
103 36 my $s_oct = $sig_obj->get_s();
104
105 # pack r and s to correct length with leading bytes
106
107 36 $r_oct = pack("a$coord_len", ("\x00" x ($coord_len - length($r_oct))) . $r_oct);
108 36 $s_oct = pack("a$coord_len", ("\x00" x ($coord_len - length($s_oct))) . $s_oct);
109
110 36 my $signature = $r_oct . $s_oct;
111
112 36 return $signature;
113 }
114  
115 24 ▶sub verify {
Subroutine verify: called CC=13 SCAR=27.7
Pod documented
116 24 my( $self, $request) = parse_params(@_);
117
118 24 my $algorithm = $request->{algorithm};
119
120 24 my ($hash_method, $curve, $coord_len, $nid, $priv_key_qr, $hmac_method ) = @{ $ECC_ALGO_MAP->{$algorithm} };
121
122 24 my $key_ref = ref($request->{key});
123
124 24 my ( $pub_x_bin, $pub_y_bin );
125
126 24 ▶ if ($key_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_ref) { } 6 18 24
127 6 ▶ if ($key_ref eq 'HASH' && exists($request->{key}->{x}) && exists($request->{key}->{y})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_ref eq "HASH" and exists $request->{"key"}{"x"} and exists $request->{"key"}{"y"}) 6 0 6
Condition33%2/6
$key_ref eq "HASH" and exists $request->{"key"}{"x"} and exists $request->{"key"}{"y"}
!ll&&!rl&&r
006
$key_ref eq "HASH" and exists $request->{"key"}{"x"}
!ll&&!rl&&r
006
MC/DC0%0/3
$key_ref eq "HASH" and exists $request->{"key"}{"x"} and exists $request->{"key"}{"y"}
$key_ref eq "HASH" exists $request->{"key"}{"x"} exists $request->{"key"}{"y"}
Truth table25%1/4
A and B and C
A= $key_ref eq "HASH"B= exists $request->{"key"}{"x"}C= exists $request->{"key"}{"y"}
ABCresult
0XX0
10X0
1100
1111
128 6 $pub_x_bin = $self->decode_jwk_element($request->{key}->{x});
129 6 $pub_y_bin = $self->decode_jwk_element($request->{key}->{y});
130 6 ▶ $pub_x_bin = undef unless( length($pub_x_bin) == $coord_len);
Branch: 1 branch
BranchTrueFalseTotal
unless length $pub_x_bin == $coord_len 0 6 6
131 6 ▶ $pub_y_bin = undef unless( length($pub_y_bin) == $coord_len);
Branch: 1 branch
BranchTrueFalseTotal
unless length $pub_y_bin == $coord_len 0 6 6
132 }
133 } else {
134 18 my $p_key_bytes = $self->decode_pem($request->{key});
135 18 my $raw_pub_points = substr($p_key_bytes, - ( $coord_len * 2 ));
136 18 $pub_x_bin = substr($raw_pub_points, 0, $coord_len);
137 18 $pub_y_bin = substr($raw_pub_points, $coord_len, $coord_len);
138 }
139
140 24 ▶ unless( $pub_x_bin && $pub_y_bin ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pub_x_bin and $pub_y_bin) 0 24 24
Condition33%1/3
$pub_x_bin and $pub_y_bin
!ll&&!rl&&r
0024
MC/DC0%0/2
$pub_x_bin and $pub_y_bin
$pub_x_bin $pub_y_bin
Truth table33%1/3
A and B
A= $pub_x_binB= $pub_y_bin
ABresult
0X0
100
111
141 0 croak 'failed to parse public key';
142 }
143
144 24 my $digest = $hash_method->($request->{message});
145
146 24 my $group = Crypt::OpenSSL::EC::EC_GROUP::new_by_curve_name($nid);
147 24 my $ecc_key = Crypt::OpenSSL::EC::EC_KEY::new();
148 24 Crypt::OpenSSL::EC::EC_KEY::set_group($ecc_key, $group);
149
150 # Populate Public Key Point
151 24 my $x_bn = Crypt::OpenSSL::Bignum->new_from_bin($pub_x_bin);
152 24 my $y_bn = Crypt::OpenSSL::Bignum->new_from_bin($pub_y_bin);
153 24 my $point = Crypt::OpenSSL::EC::EC_POINT::new($group);
154
155 24 my $ctx = Crypt::OpenSSL::Bignum::CTX->new();
156
157 24 ▶ Crypt::OpenSSL::EC::EC_POINT::set_affine_coordinates_GFp($group, $point, $x_bn, $y_bn, $ctx)
Branch: 1 branch
BranchTrueFalseTotal
&Crypt::OpenSSL::EC::EC_POINT::set_affine_coordinates_GFp($group, $point, $x_bn, $y_bn, $ctx) or croak("Failed to map point coordinates") 24 0 24
158 || croak "Failed to map point coordinates";
159 24 ▶ Crypt::OpenSSL::EC::EC_KEY::set_public_key($ecc_key, $point)
Branch: 1 branch
BranchTrueFalseTotal
&Crypt::OpenSSL::EC::EC_KEY::set_public_key($ecc_key, $point) or croak("Failed to assign public key") 24 0 24
160 || croak "Failed to assign public key";
161
162 24 my $r_bin = substr($request->{signature}, 0, $coord_len);
163 24 my $s_bin = substr($request->{signature}, $coord_len, $coord_len);
164
165 24 my $ecdsa_sig = Crypt::OpenSSL::ECDSA::ECDSA_SIG->new();
166
167 24 $ecdsa_sig->set_r($r_bin);
168 24 $ecdsa_sig->set_s($s_bin);
169
170 24 my $result = Crypt::OpenSSL::ECDSA::ECDSA_do_verify( $digest, $ecdsa_sig, $ecc_key );
171
172 24 ▶ if ( $result && $result eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $result eq '1') { } 24 0 24
Condition33%1/3
$result and $result eq 1
!ll&&!rl&&r
0024
MC/DC0%0/2
$result and $result eq 1
$result $result eq 1
Truth table33%1/3
A and B
A= $resultB= $result eq 1
ABresult
0X0
100
111
173 24 return 1;
174 } else {
175 0 return 0;
176 }
177 }
178  
179 30 ▶sub _load_private_key_as_hex {
Subroutine _load_private_key_as_hex: called CC=2 SCAR=7.0
180 30 my($self, $pem, $rx) = @_;
181 30 my $raw_key = $self->decode_pem($pem);
182 30 my $hex_private_key;
183 30 ▶ if( $raw_key =~ $rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_key =~ /$rx/s) 30 0 30
184 30 $hex_private_key = unpack("H*", $1);
185 }
186 30 return $hex_private_key;
187 }
188  
189 18 ▶sub _generate_k {
Subroutine _generate_k: called CC=4 SCAR=14.0
190 18 my ($curve_order_hex, $private_key_hex, $digest, $hmac_method, $bytelen, $bitlen) = @_;
191 18 my $order = Crypt::OpenSSL::Bignum->new_from_hex($curve_order_hex);
192 18 my $key = Crypt::OpenSSL::Bignum->new_from_hex($private_key_hex);
193
194 18 my $curve = {
195 q => $order,
196 bitlen => $bitlen,
197 bytelen => $bytelen,
198 };
199
200 18 my $privkey_bytes = $key->to_bin();
201
202 18 substr( $privkey_bytes, 0, 0, "\x00" x ($curve->{'bytelen'} - length $privkey_bytes) );
203
204 18 my $hash_len = length $digest;
205
206 # Initialize cryptographic components V and K
207 18 my $V = "\x01" x $hash_len;
208 18 my $K = "\x00" x $hash_len;
209
210 18 my $digest_octets = _bits2octets($digest, $curve );
211
212 # Spin the HMAC state using the raw key string components
213 18 $K = $hmac_method->( $V . "\x00" . $privkey_bytes . $digest_octets, $K );
214 18 $V = $hmac_method->( $V, $K );
215 18 $K = $hmac_method->( $V . "\x01" . $privkey_bytes . $digest_octets, $K );
216 18 $V = $hmac_method->( $V, $K );
217
218 # a Bignum with a value of 1
219 18 my $bn_one = Crypt::OpenSSL::Bignum->one;
220
221 # Generation loop
222
223 18 while (1) {
224 18 my $T = '';
225 18 while (1) {
226 24 $V = $hmac_method->( $V, $K );
227 24 $T .= $V;
228 24 ▶ last if length($T) * 8 >= $curve->{'bitlen'};
Branch: 1 branch
BranchTrueFalseTotal
if length($T) * 8 >= $curve->{"bitlen"} 18 6 24
229 }
230
231 18 my $k_candidate = _bits2int($T, $curve);
232
233 ## rfc6979 allowed range between 1 and q -1
234 ##
235 ## $bn_a->cmp($bn_b);
236 ## returns:
237 ## -1 if $bn_a < bn_b
238 ## 0 if $bn_a == bn_b
239 ## 1 if $bn_a > bn_b
240
241 18 ▶ if ( $k_candidate->cmp($bn_one) > -1 # $k_candidate is equal to or greater than 1
Branch: 1 branch
BranchTrueFalseTotal
if ($k_candidate->cmp($bn_one) > -1 and $k_candidate->cmp($curve->{"q"}) == -1) 18 0 18
Condition33%1/3
$k_candidate->cmp($bn_one) > -1 and $k_candidate->cmp($curve->{"q"}) == -1
!ll&&!rl&&r
0018
MC/DC0%0/2
$k_candidate->cmp($bn_one) > -1 and $k_candidate->cmp($curve->{"q"}) == -1
$k_candidate->cmp($bn_one) > -1 $k_candidate->cmp($curve->{"q"}) == -1
Truth table33%1/3
A and B
A= $k_candidate->cmp($bn_one) > -1B= $k_candidate->cmp($curve->{"q"}) == -1
ABresult
0X0
100
111
242 && $k_candidate->cmp($curve->{'q'}) == -1 ) # $k_candidate is less than q
243 {
244 18 return $k_candidate;
245 }
246
247 # If outside boundaries, stir the HMAC key ring state and repeat
248 0 $K = $hmac_method->( $V . "\x00", $K );
249 0 $V = $hmac_method->( $V, $K );
250 }
251 }
252  
253  
254 ## Bit String to Integer
255 ## bits2int
256 ## rfc6979 section-2.3.2
257  
258 36 ▶sub _bits2int {
Subroutine _bits2int: called CC=2 SCAR=6.9
259 36 my ( $bytes, $curve ) = @_;
260 36 my $bignum = Crypt::OpenSSL::Bignum->new_from_bin( $bytes );
261 36 my $blen = 8 * length($bytes);
262 36 ▶ if ($curve->{'bitlen'} < $blen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curve->{"bitlen"} < $blen) 6 30 36
263 6 my $rshift = $blen - $curve->{'bitlen'};
264 6 return $bignum->rshift($rshift);
265 }
266 30 return $bignum;
267 }
268  
269 ## Integer to Octet String
270 ## int2octets
271 ## rfc6979 section-2.3.3
272  
273 18 ▶sub _int2octets {
Subroutine _int2octets: called CC=3 SCAR=11.3
274 18 my ( $octets, $curve ) = @_;
275 18 ▶ if (length($octets) > $curve->{'bytelen'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $octets > $curve->{'bytelen'}) { } 0 18 18
elsif (length $octets < $curve->{'bytelen'}) { } 6 12 18
276 0 substr( $octets, 0, -$curve->{'bytelen'} ) = '';
277 }
278 elsif (length($octets) < $curve->{'bytelen'}) {
279 6 substr( $octets, 0, 0, "\x00" x ($curve->{'bytelen'} - length $octets) );
280 }
281 18 return $octets;
282 }
283  
284 ## Bit String to Octet String
285 ## bits2octets
286 ## rfc6979 section-2.3.4
287  
288 18 ▶sub _bits2octets {
Subroutine _bits2octets: called CC=1 SCAR=0.0
289 18 my ( $bits, $curve ) = @_;
290 18 my $z1 = _bits2int($bits, $curve);
291 18 my $ctx = Crypt::OpenSSL::Bignum::CTX->new;
292 18 my $z2 = $z1->copy->mod($curve->{'q'}, $ctx );
293 18 return _int2octets($z2->to_bin, $curve);
294 }
295  
296 5 1;
297  
298 __END__
« OpenSSL/Algorithm/RSA.pm ↑ Summary OpenSSL/Util/PEM.pm »