| 1 | package Crypt::Perl::ECDSA::EC::DB; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Crypt::Perl::ECDSA::EC::DB - Interface to this module’s CurvesDB datastore | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $oid = Crypt::Perl::ECDSA::EC::DB::get_oid_for_curve_name('prime256v1'); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | my $data_hr = Crypt::Perl::ECDSA::EC::DB::get_curve_data_by_oid('1.2.840.10045.3.1.7'); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | my $name = Crypt::Perl::ECDSA::EC::DB::get_curve_name_by_data( | ||||||||||||||||||||||||||||
| 16 | p => ..., #isa Crypt::Perl::BigInt | ||||||||||||||||||||||||||||
| 17 | a => ..., #isa Crypt::Perl::BigInt | ||||||||||||||||||||||||||||
| 18 | b => ..., #isa Crypt::Perl::BigInt | ||||||||||||||||||||||||||||
| 19 | n => ..., #isa Crypt::Perl::BigInt | ||||||||||||||||||||||||||||
| 20 | h => ..., #isa Crypt::Perl::BigInt | ||||||||||||||||||||||||||||
| 21 | gx => ..., #isa Crypt::Perl::BigInt | ||||||||||||||||||||||||||||
| 22 | gy => ..., #isa Crypt::Perl::BigInt | ||||||||||||||||||||||||||||
| 23 | seed => ..., #isa Crypt::Perl::BigInt, optional | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | #The opposite query from the preceding. | ||||||||||||||||||||||||||||
| 27 | my $data_hr = Crypt::Perl::ECDSA::EC::DB::get_curve_data_by_name('prime256v1'); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DISCUSSION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | This interface is undocumented for now. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 7 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 7 | ▶ | use Crypt::Perl::BigInt (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 7 | ▶ | use Crypt::Perl::X (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 44 | # p = prime | ||||||||||||||||||||||||||||
| 45 | # generator (uncompressed) = \x04 . gx . gy | ||||||||||||||||||||||||||||
| 46 | # n = order | ||||||||||||||||||||||||||||
| 47 | # h = cofactor | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | # a and b fit into the general form for an elliptic curve: | ||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||
| 51 | # y^2 = x^3 + ax + b | ||||||||||||||||||||||||||||
| 52 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | #“h” is determinable from the other curve parameters | ||||||||||||||||||||||||||||
| 55 | #and should not be considered necessary to match. | ||||||||||||||||||||||||||||
| 56 | 7 | ▶ | use constant CURVE_EQUIVALENCY => qw( p a b n gx gy ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 7 | ▶ | use constant GETTER_CURVE_ORDER => ( CURVE_EQUIVALENCY(), 'h', 'seed' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub get_oid_for_curve_name { | ||||||||||||||||||||||||||||
| 61 | 298 | ▶ | my ($name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_oid_for_curve_name: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 298 | my $name_alt = $name; | |||||||||||||||||||||||||||
| 64 | 298 | $name_alt =~ tr<-><_>; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 298 | require Crypt::Perl::ECDSA::EC::CurvesDB; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 298 | my $translator_cr = Crypt::Perl::ECDSA::EC::CurvesDB->can("OID_$name_alt"); | |||||||||||||||||||||||||||
| 69 | 298 | ▶ | die Crypt::Perl::X::create('ECDSA::NoCurveForName', $name) if !$translator_cr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 298 | return $translator_cr->(); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub get_curve_name_by_data { | ||||||||||||||||||||||||||||
| 75 | 39 | ▶ | my ($data_hr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_curve_name_by_data: called CC=11 SCAR=24.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 39 | my %hex_data = map { $_ => substr( $data_hr->{$_}->as_hex(), 2 ) } keys %$data_hr; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 39 | require Crypt::Perl::ECDSA::EC::CurvesDB; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 39 | my $ns = \%Crypt::Perl::ECDSA::EC::CurvesDB::; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | NS_KEY: | ||||||||||||||||||||||||||||
| 84 | 39 | for my $key ( sort keys %$ns ) { | |||||||||||||||||||||||||||
| 85 | 3560 | ▶ | next if substr($key, 0, 4) ne 'OID_'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1416 | my $oid; | |||||||||||||||||||||||||||
| 88 | 1416 | ▶ | if ('SCALAR' eq ref $ns->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | 1353 | $oid = ${ $ns->{$key} }; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 63 | elsif ( *{$ns->{$key}}{'CODE'} ) { | |||||||||||||||||||||||||||
| 92 | 63 | $oid = $ns->{$key}->(); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 0 | next; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | #Avoid creating extra BigInt objects. | ||||||||||||||||||||||||||||
| 99 | 1416 | my $db_hex_data_hr; | |||||||||||||||||||||||||||
| 100 | try { | ||||||||||||||||||||||||||||
| 101 | 1416 | ▶ | $db_hex_data_hr = _get_curve_hex_data_by_oid($oid); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | catch { | ||||||||||||||||||||||||||||
| 104 | 528 | ▶ | if ( !try { $_->isa('Crypt::Perl::X::ECDSA::NoCurveForOID') } ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | local $@ = $_; | |||||||||||||||||||||||||||
| 106 | 176 | die; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 1416 | }; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1416 | ▶ | next if !$db_hex_data_hr; #i.e., if we have no params for the OID | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 888 | for my $k ( CURVE_EQUIVALENCY() ) { | |||||||||||||||||||||||||||
| 113 | 1083 | ▶ | next NS_KEY if $hex_data{$k} ne $db_hex_data_hr->{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | #We got a match! | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 39 | my $name = substr($key, 4); # strip leading “OID_” | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | #We store dashes as underscores so we can use the namespace. | ||||||||||||||||||||||||||||
| 121 | #Hopefully no curve OID name will ever contain an underscore!! | ||||||||||||||||||||||||||||
| 122 | 39 | $name =~ tr<_><->; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | #… but let’s make sure the extras (cofactor and seed) are correct, | ||||||||||||||||||||||||||||
| 125 | #if given. Note that all curves have cofactor == 1 except secp112r2 and | ||||||||||||||||||||||||||||
| 126 | #secp128r2, both of which have cofactor == 4. | ||||||||||||||||||||||||||||
| 127 | # | ||||||||||||||||||||||||||||
| 128 | 39 | for my $k ( qw( h seed ) ) { | |||||||||||||||||||||||||||
| 129 | 78 | ▶ | if ( defined $hex_data{$k} && $hex_data{$k} ne $db_hex_data_hr->{$k} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hex_data{$k} and $hex_data{$k} ne $db_hex_data_hr->{$k}
MC/DC0%0/2
defined $hex_data{$k} and $hex_data{$k} ne $db_hex_data_hr->{$k}
defined $hex_data{$k}
$hex_data{$k} ne $db_hex_data_hr->{$k}
Truth table66%2/3
A and B
A= defined $hex_data{$k}B= $hex_data{$k} ne $db_hex_data_hr->{$k}
| |||||||||||||||||||||||||||||
| 130 | 0 | die Crypt::Perl::X::create('Generic', "Curve parameters match “$name”, but “$k” ($hex_data{$k}) does not match expected value ($db_hex_data_hr->{$k})!"); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 39 | return $name; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | die Crypt::Perl::X::create('ECDSA::NoCurveForParameters', %hex_data); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub get_curve_data_by_name { | ||||||||||||||||||||||||||||
| 141 | 215 | ▶ | my ($name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_curve_data_by_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 215 | my $oid = get_oid_for_curve_name($name); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 215 | return get_curve_data_by_oid( $oid ); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | #This returns the same information as | ||||||||||||||||||||||||||||
| 149 | #Crypt::Perl::ECDSA::ECParameters::normalize(). | ||||||||||||||||||||||||||||
| 150 | sub get_curve_data_by_oid { | ||||||||||||||||||||||||||||
| 151 | 530 | ▶ | my ($oid) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_curve_data_by_oid: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 530 | my $data_hr = _get_curve_hex_data_by_oid($oid); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 330 | $_ = Crypt::Perl::BigInt->from_hex($_) for values %$data_hr; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 330 | return $data_hr; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub _get_curve_hex_data_by_oid { | ||||||||||||||||||||||||||||
| 161 | 1946 | ▶ | my ($oid) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_curve_hex_data_by_oid: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1946 | my $const = "CURVE_$oid"; | |||||||||||||||||||||||||||
| 164 | 1946 | $const =~ tr<.><_>; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1946 | require Crypt::Perl::ECDSA::EC::CurvesDB; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 1946 | my $getter_cr = Crypt::Perl::ECDSA::EC::CurvesDB->can($const); | |||||||||||||||||||||||||||
| 169 | 1946 | ▶ | die Crypt::Perl::X::create('ECDSA::NoCurveForOID', $oid) if !$getter_cr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1218 | my %data; | |||||||||||||||||||||||||||
| 172 | 1218 | @data{ GETTER_CURVE_ORDER() } = $getter_cr->(); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1218 | ▶ | delete $data{'seed'} if !$data{'seed'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 1218 | return \%data; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | sub _upgrade_hex_to_bigint { | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my ($data_hr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_upgrade_hex_to_bigint: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | $_ = Crypt::Perl::BigInt->from_hex($_) for @{$data_hr}{ GETTER_CURVE_ORDER() }; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | return; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 7 | 1; | |||||||||||||||||||||||||||