line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
4
|
|
286955
|
use strict; |
|
4
|
|
|
|
|
26
|
|
|
4
|
|
|
|
|
112
|
|
42
|
4
|
|
|
4
|
|
25
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
80
|
|
43
|
|
|
|
|
|
|
|
44
|
4
|
|
|
4
|
|
653
|
use Crypt::Perl::BigInt (); |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
57
|
|
45
|
4
|
|
|
4
|
|
1210
|
use Crypt::Perl::Math (); |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
71
|
|
46
|
4
|
|
|
4
|
|
23
|
use Crypt::Perl::RNG (); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
47
|
|
47
|
4
|
|
|
4
|
|
737
|
use Crypt::Perl::ECDSA::EC::DB (); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
50
|
|
48
|
4
|
|
|
4
|
|
1328
|
use Crypt::Perl::ECDSA::EC::Curve (); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
117
|
|
49
|
4
|
|
|
4
|
|
1421
|
use Crypt::Perl::ECDSA::PrivateKey (); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
998
|
|
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
|
|
|
49
|
0
|
187658
|
my ($curve_name) = @_; |
55
|
|
|
|
|
|
|
|
56
|
49
|
|
|
|
|
693
|
my $key_parts = _generate( |
57
|
|
|
|
|
|
|
Crypt::Perl::ECDSA::EC::DB::get_curve_data_by_name($curve_name), |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
49
|
|
|
|
|
1537
|
return Crypt::Perl::ECDSA::PrivateKey->new_by_curve_name($key_parts, $curve_name); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
*by_name = *by_curve_name; #legacy |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub by_explicit_curve { |
66
|
0
|
|
|
0
|
0
|
0
|
my ($curve_hr) = @_; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
0
|
my $key_parts = _generate($curve_hr); |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
0
|
return Crypt::Perl::ECDSA::PrivateKey->new($key_parts, $curve_hr); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
#from generateKeyPairHex() in jsrsasign |
74
|
|
|
|
|
|
|
sub _generate { |
75
|
49
|
|
|
49
|
|
171
|
my ($curve_hr) = @_; |
76
|
|
|
|
|
|
|
|
77
|
49
|
|
|
|
|
165
|
my $biN = $curve_hr->{'n'}; |
78
|
|
|
|
|
|
|
|
79
|
49
|
|
|
|
|
370
|
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
|
|
|
|
|
153
|
my $curve = Crypt::Perl::ECDSA::EC::Curve->new( @{$curve_hr}{'p', 'a', 'b'} ); |
|
49
|
|
|
|
|
469
|
|
85
|
|
|
|
|
|
|
|
86
|
49
|
|
|
|
|
118
|
my $G = $curve->decode_point( @{$curve_hr}{'gx','gy'}); |
|
49
|
|
|
|
|
228
|
|
87
|
|
|
|
|
|
|
|
88
|
49
|
|
|
|
|
255
|
my $epPub = $G->multiply($biPrv); |
89
|
49
|
|
|
|
|
198
|
my $biX = $epPub->get_x()->to_bigint(); |
90
|
49
|
|
|
|
|
263
|
my $biY = $epPub->get_y()->to_bigint(); |
91
|
|
|
|
|
|
|
|
92
|
49
|
|
|
|
|
265
|
my $key_hex_len = 2 * Crypt::Perl::Math::ceil( $curve->keylen() / 8 ); |
93
|
|
|
|
|
|
|
|
94
|
49
|
|
|
|
|
145
|
my ($hx, $hy) = map { substr( $_->as_hex(), 2 ) } $biX, $biY; |
|
98
|
|
|
|
|
42205
|
|
95
|
|
|
|
|
|
|
|
96
|
49
|
|
|
|
|
40801
|
$_ = sprintf "%0${key_hex_len}s", $_ for ($hx, $hy); |
97
|
|
|
|
|
|
|
|
98
|
49
|
|
|
|
|
432
|
my $biPub = Crypt::Perl::BigInt->from_hex("04$hx$hy"); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return { |
101
|
49
|
|
|
|
|
105108
|
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
|
|
|
|
|
|
|
1; |