lib/Crypt/Perl/ECDSA/KeyBase.pm

filter: Statementstmt 95.2% 179 / 188 Branchbran 70.0% 14 / 20 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 98.0% 50 / 51 Podpod 0.0% 0 / 11 totaltotal 86.9% 252 / 290 CC 18 SCAR 8.3
CC
18
Cov
91%
_verify
7.5
get_jwa_alg
6.0
_explicit_curve_parameters
5.0
CRAP 2.3
« blib/lib/Crypt/Perl/RSA/PrivateKey.pm ↑ Summary lib/Crypt/Perl/X509/GeneralNames.pm »
1 package Crypt::Perl::ECDSA::KeyBase;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 use parent qw(
9 Crypt::Perl::KeyBase
10 7 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use Crypt::Format ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use Crypt::Perl::ASN1 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Crypt::Perl::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Crypt::Perl::Math ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Crypt::Perl::ECDSA::EC::Curve ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Crypt::Perl::ECDSA::EC::DB ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Crypt::Perl::ECDSA::EC::Point ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Crypt::Perl::ECDSA::ECParameters ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Crypt::Perl::ECDSA::NIST ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use Crypt::Perl::ECDSA::EncodedPoint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use Crypt::Perl::ECDSA::Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Crypt::Perl::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 use constant ASN1_SIGNATURE => q<
27 SEQUENCE {
28 r INTEGER,
29 s INTEGER
30 }
31 7 ▶>;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 7 use constant ASN1_Params => Crypt::Perl::ECDSA::ECParameters::ASN1_ECParameters() . q<
34 EcpkParameters ::= CHOICE {
35 namedCurve OBJECT IDENTIFIER,
36 ecParameters ECParameters
37 }
38 7 ▶>;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 7 ▶use constant _JWK_THUMBPRINT_JSON_ORDER => qw( crv kty x y );
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 7 ▶use constant JWA_DIGEST_prime256v1 => 'sha256';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 7 ▶use constant JWA_DIGEST_secp384r1 => 'sha384';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 7 ▶use constant JWA_DIGEST_secp521r1 => 'sha512';
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 7 ▶use constant JWA_CURVE_ALG_prime256v1 => 'ES256';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 7 ▶use constant JWA_CURVE_ALG_secp384r1 => 'ES384';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 7 ▶use constant JWA_CURVE_ALG_secp521r1 => 'ES512';
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 #Expects $key_parts to be a hash ref:
51 #
52 # version - AFAICT unused
53 # private - BigInt or its byte-string representation
54 # public - ^^
55 #
56 sub new_by_curve_name {
57 55 ▶ my ($class, $key_parts, $curve_name) = @_;
Subroutine new_by_curve_name: called CC=1 SCAR=0.0
Pod undocumented
58  
59 #We could store the curve name on here if looking it up
60 #in to_der_with_curve_name() proves prohibitive.
61 55 return $class->new(
62 $key_parts,
63  
64 #“Fake out” the $curve_parts attribute by recreating
65 #the structure that ASN.1 would give from a named curve.
66 {
67 namedCurve => Crypt::Perl::ECDSA::EC::DB::get_oid_for_curve_name($curve_name),
68 },
69 );
70 }
71  
72 #$msg has to be small enough that the key could have signed it.
73 #It’s probably a digest rather than the original message.
74 sub verify {
75 248 ▶ my ($self, $msg, $sig) = @_;
Subroutine verify: called CC=1 SCAR=0.0
Pod undocumented
76  
77 248 my $struct = Crypt::Perl::ASN1->new()->prepare(ASN1_SIGNATURE)->decode($sig);
78  
79 248 return $self->_verify($msg, @{$struct}{ qw( r s ) });
80 }
81  
82 #cf. RFC 7518, page 8
83 sub verify_jwa {
84 3 ▶ my ($self, $msg, $sig) = @_;
Subroutine verify_jwa: called CC=2 SCAR=6.9
Pod undocumented
85  
86 3 my $dgst_cr = $self->_get_jwk_digest_cr();
87  
88 3 my $half_len = (length $sig) / 2;
89  
90 3 my $r = substr($sig, 0, $half_len);
91 3 my $s = substr($sig, $half_len);
92  
93 3 $_ = Crypt::Perl::BigInt->from_bytes($_) for ($r, $s);
94  
95 3 return $self->_verify($dgst_cr->($msg), $r, $s);
96 }
97  
98 sub to_der_with_curve_name {
99 28 ▶ my ($self, %params) = @_;
Subroutine to_der_with_curve_name: called CC=2 SCAR=7.6
Pod undocumented
100  
101 28 ▶ if ($params{'seed'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"seed"}) 0 28 28
102 0 die Crypt::Perl::X::create('Generic', '“seed” is meaningless to a named-curve export.');
103 }
104  
105 28 return $self->_get_asn1_parts($self->_named_curve_parameters(), %params);
106 }
107  
108 sub to_der_with_explicit_curve {
109 246 ▶ my ($self, @params) = @_;
Subroutine to_der_with_explicit_curve: called CC=1 SCAR=0.0
Pod undocumented
110  
111 246 return $self->_get_asn1_parts($self->_explicit_curve_parameters(@params), @params);
112 }
113  
114 sub to_pem_with_curve_name {
115 12 ▶ my ($self, @params) = @_;
Subroutine to_pem_with_curve_name: called CC=1 SCAR=0.0
Pod undocumented
116  
117 12 my $der = $self->to_der_with_curve_name(@params);
118  
119 12 return Crypt::Format::der2pem($der, $self->_PEM_HEADER());
120 }
121  
122 sub to_pem_with_explicit_curve {
123 236 ▶ my ($self, @params) = @_;
Subroutine to_pem_with_explicit_curve: called CC=1 SCAR=0.0
Pod undocumented
124  
125 236 my $der = $self->to_der_with_explicit_curve(@params);
126  
127 236 return Crypt::Format::der2pem($der, $self->_PEM_HEADER());
128 }
129  
130 sub max_sign_bits {
131 297 ▶ my ($self) = @_;
Subroutine max_sign_bits: called CC=1 SCAR=0.0
Pod undocumented
132  
133 297 return $self->_get_curve_obj()->keylen();
134 }
135  
136 sub get_curve_name {
137 39 ▶ my ($self) = @_;
Subroutine get_curve_name: called CC=1 SCAR=0.0
Pod undocumented
138  
139 39 return Crypt::Perl::ECDSA::EC::DB::get_curve_name_by_data( $self->_curve() );
140 }
141  
142 sub get_struct_for_public_jwk {
143 5 ▶ my ($self) = @_;
Subroutine get_struct_for_public_jwk: called CC=1 SCAR=0.0
Pod undocumented
144  
145 5 my ($xb, $yb) = Crypt::Perl::ECDSA::Utils::split_G_or_public( $self->_decompress_public_point() );
146  
147 5 require MIME::Base64;
148  
149 return {
150 5 kty => 'EC',
151 crv => $self->_get_jwk_curve_name(),
152 x => MIME::Base64::encode_base64url($xb),
153 y => MIME::Base64::encode_base64url($yb),
154 }
155 }
156  
157 sub get_jwa_alg {
158 0 ▶ my ($self) = @_;
Subroutine get_jwa_alg: not called CC=2 SCAR=17.9
Pod undocumented
159  
160 0 my $name = $self->get_curve_name();
161  
162 0 ▶ my $getter_cr = __PACKAGE__->can("JWA_CURVE_ALG_$name") or do {
Branch: 1 branch
BranchTrueFalseTotal
my $getter_cr = __PACKAGE__->can("JWA_CURVE_ALG_$name") or do { my $err = sprintf("\342\200\234%s\342\200\235 knows of no JWA \342\200\234alg\342\200\235 for the curve \342\200\234%s\342\200\235!", ref $self, $name); die Crypt::Perl::X::create("Generic", $err) } 0 0 0
163 0 my $err = sprintf( "“%s” knows of no JWA “alg” for the curve “%s”!", ref($self), $name);
164  
165 0 die Crypt::Perl::X::create('Generic', $err);
166 };
167  
168 0 return $getter_cr->();
169 }
170  
171 #----------------------------------------------------------------------
172  
173 sub _set_public {
174 573 ▶ my ($self, $pub_in) = @_;
Subroutine _set_public: called CC=1 SCAR=0.0
175  
176 573 $self->{'_public'} = Crypt::Perl::ECDSA::EncodedPoint->new($pub_in);
177  
178 573 return;
179 }
180  
181 sub _compress_public_point {
182 7 ▶ my ($self) = @_;
Subroutine _compress_public_point: called CC=1 SCAR=0.0
183  
184 7 return $self->{'_public'}->get_compressed();
185 }
186  
187 sub _decompress_public_point {
188 536 ▶ my ($self) = @_;
Subroutine _decompress_public_point: called CC=1 SCAR=0.0
189  
190 536 return $self->{'_public'}->get_uncompressed( $self->_curve() );
191 }
192  
193 sub _get_jwk_digest_cr {
194 6 ▶ my ($self) = @_;
Subroutine _get_jwk_digest_cr: called CC=2 SCAR=7.6
195  
196 6 my $name = $self->get_curve_name();
197  
198 6 ▶ my $getter_cr = $self->can("JWA_DIGEST_$name") or do {
Branch: 1 branch
BranchTrueFalseTotal
my $getter_cr = $self->can("JWA_DIGEST_$name") or do { my $err = sprintf("\342\200\234%s\342\200\235 knows of no digest to use for JWA with the curve \342\200\234%s\342\200\235!", ref $self, $name); die Crypt::Perl::X::create("Generic", $err) } 0 6 6
199 0 my $err = sprintf( "“%s” knows of no digest to use for JWA with the curve “%s”!", ref($self), $name);
200 0 die Crypt::Perl::X::create('Generic', $err);
201 };
202  
203 6 require Digest::SHA;
204  
205 6 return Digest::SHA->can( $getter_cr->() );
206 }
207  
208 sub _get_jwk_curve_name {
209 5 ▶ my ($self) = @_;
Subroutine _get_jwk_curve_name: called CC=1 SCAR=0.0
210  
211 5 my $name = $self->get_curve_name();
212  
213 5 return Crypt::Perl::ECDSA::NIST::get_nist_for_curve_name($name);
214 }
215  
216 sub _verify {
217 251 ▶ my ($self, $msg, $r, $s) = @_;
Subroutine _verify: called CC=7 SCAR=20.1
218  
219 251 ▶ if ($r->is_positive() && $s->is_positive()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_positive and $s->is_positive) 251 0 251
Condition33%1/3
$r->is_positive and $s->is_positive
!ll&&!rl&&r
00251
MC/DC0%0/2
$r->is_positive and $s->is_positive
$r->is_positive $s->is_positive
Truth table33%1/3
A and B
A= $r->is_positiveB= $s->is_positive
ABresult
0X0
100
111
220 251 my ($x, $y) = Crypt::Perl::ECDSA::Utils::split_G_or_public( $self->_decompress_public_point() );
221 251 $_ = Crypt::Perl::BigInt->from_bytes($_) for ($x, $y);
222  
223 251 my $curve = $self->_get_curve_obj();
224  
225 251 my $Q = Crypt::Perl::ECDSA::EC::Point->new(
226 $curve,
227 $curve->from_bigint($x),
228 $curve->from_bigint($y),
229 );
230  
231 251 my $e = Crypt::Perl::BigInt->from_bytes($msg);
232  
233 #----------------------------------------------------------------------
234  
235 251 my $n = $self->_curve()->{'n'};
236  
237 251 ▶ if ($r->blt($n) && $s->blt($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->blt($n) and $s->blt($n)) 251 0 251
Condition33%1/3
$r->blt($n) and $s->blt($n)
!ll&&!rl&&r
00251
MC/DC0%0/2
$r->blt($n) and $s->blt($n)
$r->blt($n) $s->blt($n)
Truth table33%1/3
A and B
A= $r->blt($n)B= $s->blt($n)
ABresult
0X0
100
111
238 251 my $c = $s->copy()->bmodinv($n);
239  
240 251 my $u1 = $e->copy()->bmul($c)->bmod($n);
241 251 my $u2 = $r->copy()->bmul($c)->bmod($n);
242  
243 251 my $point = $self->_G()->multiply($u1)->add( $Q->multiply($u2) );
244  
245 251 my $v = $point->get_x()->to_bigint()->copy()->bmod($n);
246  
247 251 ▶ return 1 if $v->beq($r);
Branch: 1 branch
BranchTrueFalseTotal
if $v->beq($r) 243 8 251
248 }
249 }
250  
251 8 return 0;
252 }
253  
254 #return isa EC::Point
255 sub _G {
256 496 ▶ my ($self) = @_;
Subroutine _G: called CC=1 SCAR=0.0
257 496 return $self->_get_curve_obj()->decode_point( @{$self->_curve()}{ qw( gx gy ) } );
258 }
259  
260 sub _pad_bytes_for_asn1 {
261 518 ▶ my ($self, $bytes) = @_;
Subroutine _pad_bytes_for_asn1: called CC=1 SCAR=0.0
262  
263 518 return Crypt::Perl::ECDSA::Utils::pad_bytes_for_asn1( $bytes, $self->_curve()->{'p'} );
264 }
265  
266 sub _named_curve_parameters {
267 28 ▶ my ($self) = @_;
Subroutine _named_curve_parameters: called CC=1 SCAR=0.0
268  
269 28 my $curve_name = $self->get_curve_name();
270  
271 return {
272 28 namedCurve => Crypt::Perl::ECDSA::EC::DB::get_oid_for_curve_name($curve_name),
273 };
274 }
275  
276 #The idea is to emulate what Convert::ASN1 gives us as the parse.
277 sub _explicit_curve_parameters {
278 259 ▶ my ($self, %params) = @_;
Subroutine _explicit_curve_parameters: called CC=5 SCAR=16.1
279  
280 259 my $curve_hr = $self->_curve();
281  
282 259 my ($gx, $gy) = map { $_->as_bytes() } @{$curve_hr}{'gx', 'gy'};
283  
284 259 for my $str ( $gx, $gy ) {
285 518 $str = $self->_pad_bytes_for_asn1($str);
286 }
287  
288 my %curve = (
289 a => $curve_hr->{'a'}->as_bytes(),
290 259 b => $curve_hr->{'b'}->as_bytes(),
291 );
292  
293 259 ▶ if ($params{'seed'} && $curve_hr->{'seed'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"seed"} and $curve_hr->{"seed"}) 13 246 259
Condition100%3/3
$params{"seed"} and $curve_hr->{"seed"}
!ll&&!rl&&r
242413
MC/DC100%2/2
$params{"seed"} and $curve_hr->{"seed"}
$params{"seed"} $curve_hr->{"seed"}
Truth table100%3/3
A and B
A= $params{"seed"}B= $curve_hr->{"seed"}
ABresult
0X0
100
111
294  
295 #We don’t care about the bit count. (Right?)
296 13 $curve{'seed'} = $curve_hr->{'seed'}->as_bytes();
297 }
298  
299 259 my $base = "\x{04}$gx$gy";
300 259 ▶ if ( $params{'compressed'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"compressed"}) 4 255 259
301 4 $base = Crypt::Perl::ECDSA::Utils::compress_point($base);
302 }
303  
304 return {
305 ecParameters => {
306 version => 1,
307 fieldID => {
308 fieldType => Crypt::Perl::ECDSA::ECParameters::OID_prime_field(),
309 parameters => {
310 'prime-field' => $curve_hr->{'p'},
311 },
312 },
313 curve => \%curve,
314 base => $base,
315 order => $curve_hr->{'n'},
316 259 cofactor => $curve_hr->{'h'},
317 },
318 };
319 }
320  
321 sub __to_der {
322 274 ▶ my ($self, $macro, $template, $data_hr, %params) = @_;
Subroutine __to_der: called CC=2 SCAR=6.9
323  
324 274 ▶ my $pub_bin = $params{'compressed'} ? $self->_compress_public_point() : $self->_decompress_public_point();
Branch: 1 branch
BranchTrueFalseTotal
$params{'compressed'} ? : 7 267 274
325  
326 274 local $data_hr->{'publicKey'} = $pub_bin;
327  
328 274 require Crypt::Perl::ASN1;
329 274 my $asn1 = Crypt::Perl::ASN1->new()->prepare($template);
330  
331 274 return $asn1->find($macro)->encode( $data_hr );
332 }
333  
334 #return isa EC::Curve
335 sub _get_curve_obj {
336 1044 ▶ my ($self) = @_;
Subroutine _get_curve_obj: called CC=2 SCAR=7.0
337  
338 1044 ▶ return $self->{'_curve_obj'} ||= Crypt::Perl::ECDSA::EC::Curve->new( @{$self->_curve()}{ qw( p a b ) } );
Condition66%2/3
$self->{"_curve_obj"} ||= "Crypt::Perl::ECDSA::EC::Curve"->new(@{$self->_curve;}{"p", "a", "b"})
l!l&&r!l&&!r
7482960
MC/DC0%0/2
$self->{"_curve_obj"} ||= "Crypt::Perl::ECDSA::EC::Curve"->new(@{$self->_curve;}{"p", "a", "b"})
$self->{"_curve_obj"} "Crypt::Perl::ECDSA::EC::Curve"->new(@{$self->_curve;}{"p", "a", "b"})
Truth table66%2/3
A ||= B
A= $self->{"_curve_obj"}B= "Crypt::Perl::ECDSA::EC::Curve"->new(@{$self->_curve;}{"p", "a", "b"})
ABresult
1X1
011
000
339 }
340  
341 sub _add_params {
342 573 ▶ my ($self, $params_struct) = @_;
Subroutine _add_params: called CC=2 SCAR=6.9
343  
344 573 ▶ if (my $params = $params_struct->{'ecParameters'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $params = $params_struct->{'ecParameters'}) { } 258 315 573
345  
346 #Convert::ASN1 returns bit strings as an array of [ $content, $length ].
347 #Since normalize() wants that, we local() it here.
348 #local $params->{'curve'}{'seed'} = [$params->{'curve'}{'seed'}] if $params->{'curve'} && $params->{'curve'}{'seed'};
349  
350 258 $self->{'curve'} = Crypt::Perl::ECDSA::ECParameters::normalize($params);
351 }
352 else {
353 315 $self->{'curve'} = $self->_curve_params_for_OID($params_struct->{'namedCurve'});
354 }
355  
356 333 return $self;
357 }
358  
359 sub _curve_params_for_OID {
360 315 ▶ my ($self, $oid) = @_;
Subroutine _curve_params_for_OID: called CC=1 SCAR=0.0
361  
362 315 return Crypt::Perl::ECDSA::EC::DB::get_curve_data_by_oid($oid);
363 }
364  
365 sub _curve {
366 2682 ▶ my ($self) = @_;
Subroutine _curve: called CC=1 SCAR=0.0
367  
368 2682 return $self->{'curve'};
369 }
370  
371 #----------------------------------------------------------------------
372  
373 7 1;
« blib/lib/Crypt/Perl/RSA/PrivateKey.pm ↑ Summary lib/Crypt/Perl/X509/GeneralNames.pm »