lib/Crypt/Perl/ECDSA/Generate.pm

filter: Statementstmt 93.7% 45 / 48 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 2 totaltotal 90.1% 55 / 61 CC 2 SCAR 2.2
CC
2
Cov
94%
by_explicit_curve
2.0
_generate
2.0
BEGIN
1.0
CRAP 1.2
« lib/Crypt/Perl/X509/Extension/subjectKeyIdentifier.pm ↑ Summary lib/Crypt/Perl/X509/Extension/ct_precert_scts.pm »
1 package Crypt::Perl::ECDSA::Generate;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Crypt::Perl::ECDSA::Generate - ECDSA key generation
8  
9 =head1 SYNOPSIS
10  
11 use Crypt::Perl::ECDSA::Generate ();
12  
13 #$prkey is a C::P::E::PrivateKey instance.
14 my $prkey = Crypt::Perl::ECDSA::Generate::by_curve_name('secp521r1');
15  
16 my $signature = $prkey->sign('Hello!');
17  
18 die 'Wut' if $prkey->verify('Hello!', $signature);
19  
20 #You can also, in case it’s useful, do this. It’s probably
21 #only useful if you’re developing a new curve or something … ??
22 my $prkey2 = Crypt::Perl::ECDSA::Generate::by_explicit_curve(
23 {
24 p => Crypt::Perl::BigInt->new(...),
25 a => ...,
26 b => ...,
27 n => ...,
28 h => ...,
29 gx => ...,
30 gy => ...,
31 },
32 );
33  
34 =head1 DISCUSSION
35  
36 Thankfully, this is easy enough on processors that it’s feasible
37 in pure Perl!
38  
39 =cut
40  
41 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 4 ▶use Crypt::Perl::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶use Crypt::Perl::Math ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use Crypt::Perl::RNG ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use Crypt::Perl::ECDSA::EC::DB ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶use Crypt::Perl::ECDSA::EC::Curve ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶use Crypt::Perl::ECDSA::PrivateKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 #The curve name is optional; if given, only the name will be encoded
52 #into the key rather than the explicit curve parameters.
53 sub by_curve_name {
54 49 ▶ my ($curve_name) = @_;
Subroutine by_curve_name: called CC=1 SCAR=0.0
Pod undocumented
55  
56 49 my $key_parts = _generate(
57 Crypt::Perl::ECDSA::EC::DB::get_curve_data_by_name($curve_name),
58 );
59  
60 49 return Crypt::Perl::ECDSA::PrivateKey->new_by_curve_name($key_parts, $curve_name);
61 }
62  
63 4 *by_name = *by_curve_name; #legacy
64  
65 sub by_explicit_curve {
66 0 ▶ my ($curve_hr) = @_;
Subroutine by_explicit_curve: not called CC=1 SCAR=6.9
Pod undocumented
67  
68 0 my $key_parts = _generate($curve_hr);
69  
70 0 return Crypt::Perl::ECDSA::PrivateKey->new($key_parts, $curve_hr);
71 }
72  
73 #from generateKeyPairHex() in jsrsasign
74 sub _generate {
75 49 ▶ my ($curve_hr) = @_;
Subroutine _generate: called CC=2 SCAR=6.9
76  
77 49 my $biN = $curve_hr->{'n'};
78  
79 49 my $biPrv = Crypt::Perl::Math::randint( $biN );
80  
81 #my $G = '04' . join(q<>, map { substr( $_->as_hex(), 2 ) } @{$curve}{'gx','gy'});
82 #$G = Crypt::Perl::BigInt->from_hex($full_g);
83  
84 49 my $curve = Crypt::Perl::ECDSA::EC::Curve->new( @{$curve_hr}{'p', 'a', 'b'} );
85  
86 49 my $G = $curve->decode_point( @{$curve_hr}{'gx','gy'});
87  
88 49 my $epPub = $G->multiply($biPrv);
89 49 my $biX = $epPub->get_x()->to_bigint();
90 49 my $biY = $epPub->get_y()->to_bigint();
91  
92 49 my $key_hex_len = 2 * Crypt::Perl::Math::ceil( $curve->keylen() / 8 );
93  
94 49 my ($hx, $hy) = map { substr( $_->as_hex(), 2 ) } $biX, $biY;
95  
96 49 $_ = sprintf "%0${key_hex_len}s", $_ for ($hx, $hy);
97  
98 49 my $biPub = Crypt::Perl::BigInt->from_hex("04$hx$hy");
99  
100 return {
101 49 version => 0,
102 private => $biPrv,
103 public => $biPub,
104 };
105 }
106  
107 #sub generate {
108 # my ($curve_name) = @_
109 #
110 # my $curve_hr = Crypt::Perl::ECDSA::EC::DB::get_curve_data_by_name($curve_name);
111 #
112 # my $bytes = $curve_hr->{'n'}->as_hex() / 2 - 1;
113 # my $ns2 = $curve_hr->{'n'} - 2;
114 #
115 # do {
116 # my $priv = _gen_bignum($bytes);
117 # next if $priv > $ns2;
118 #
119 # $priv += 1;
120 #
121 # return _key_from_private($curve_hr, $priv);
122 # } while 1;
123 #}
124 #
125 #sub _key_from_private {
126 # return _keypair( $curve_hr, $priv );
127 #}
128 #
129 #sub _keypair {
130 # my ($curve_hr, $priv) = @_;
131 #
132 # $priv %= $curve_hr->{'n'};
133 #
134 # my $full_g = '04' . join(q<>, map { substr( $_->as_hex(), 2 ) } @{$curve}{'gx','gy'});
135 # $full_g = Crypt::Perl::BigInt->from_hex($full_g);
136 #
137 # return {
138 # priv => $priv,
139 # pub => $full_g * $priv,
140 # };
141 #}
142 #
143 #sub _gen_bignum {
144 # my ($bits) = @_;
145 #
146 # return Crypt::Perl::BigInt->from_bin( Crypt::Perl::RNG::bit_string($bits) );
147 #}
148  
149 4 1;
« lib/Crypt/Perl/X509/Extension/subjectKeyIdentifier.pm ↑ Summary lib/Crypt/Perl/X509/Extension/ct_precert_scts.pm »