| 1 | # -*-cperl-*- | ||
| 2 | # | ||
| 3 | # Crypt::ECDSA::Blind - Blind ECDSA signatures | ||
| 4 | # Copyright (c) Ashish Gulhati <crypt-ecdsab at hash.neo.tc> | ||
| 5 | # | ||
| 6 | # $Id: lib/Crypt/ECDSA/Blind.pm v1.015 Tue Oct 16 22:40:55 PDT 2018 $ | ||
| 7 | |||
| 8 | package Crypt::ECDSA::Blind; | ||
| 9 | |||
| 10 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Bytes::Random::Secure; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use Math::EllipticCurve::Prime; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Digest::SHA; | ||
| 16 | use vars qw( $VERSION $AUTOLOAD ); | ||
| 17 | |||
| 18 | our ( $VERSION ) = '$Revision: 1.015 $' =~ /\s+([\d\.]+)/; | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my ($class, %arg) = @_; my $dbname = $arg{DB} || '/tmp/ceb.db'; | ||
| 22 | unlink $dbname if $arg{Clobber} and $dbname ne ':memory:'; | ||
| 23 | my $db = DBI->connect("dbi:SQLite:dbname=$dbname", undef, undef, {AutoCommit => 1}); | ||
| 24 | my @tables = $db->tables('%','%','initkeys','TABLE'); | ||
| 25 | unless ($tables[0]) { | ||
| 26 | if ($arg{Create}) { | ||
| 27 | return undef unless $db->do('CREATE TABLE initkeys ( | ||
| 28 | Rp TEXT PRIMARY KEY, | ||
| 29 | k TEXT NOT NULL, | ||
| 30 | issued int NOT NULL | ||
| 31 | );'); | ||
| 32 | return undef unless $db->do('CREATE INDEX idx_initkeys_Rp ON initkeys(Rp);'); | ||
| 33 | } | ||
| 34 | else { | ||
| 35 | return undef; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | @tables = $db->tables('%','%','preinits','TABLE'); | ||
| 39 | unless ($tables[0]) { | ||
| 40 | if ($arg{Create}) { | ||
| 41 | return undef unless $db->do('CREATE TABLE preinits ( | ||
| 42 | Rp TEXT PRIMARY KEY, | ||
| 43 | k TEXT NOT NULL | ||
| 44 | );'); | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | return undef; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | bless { debug => 0, | ||
| 51 | db => $db, | ||
| 52 | curve => Math::EllipticCurve::Prime->from_name('secp256k1') | ||
| 53 | }, $class; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub keygen { # Generate public, private key pair | ||
| 57 | my $self = shift; | ||
| 58 | my $random = Bytes::Random::Secure->new( Bits => 128 ); | ||
| 59 | my $d = _makerandom($self->curve->n); | ||
| 60 | my $Q = $self->curve->g->multiply($d); | ||
| 61 | $self->_diag("keygen(): d: $d, Q: x:" . $Q->x . ', y:' . $Q->y . "\n"); | ||
| 62 | return ((bless {Q => $Q}, 'Crypt::ECDSA::Blind::PubKey'),(bless {d => $d}, 'Crypt::ECDSA::Blind::SecKey')); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub preinit { # Create an init vector in advance | ||
| 66 | my $self = shift; | ||
| 67 | my $count = $self->db->selectcol_arrayref("SELECT count() from preinits;")->[0]; | ||
| 68 | return if $count > 20; | ||
| 69 | my ($k, $Rp, $rp); | ||
| 70 | until ($rp) { | ||
| 71 | $k = _makerandom($self->curve->n); | ||
| 72 | $Rp = $self->curve->g->multiply($k); | ||
| 73 | $rp = $Rp->x; | ||
| 74 | $Rp = _compress($Rp); | ||
| 75 | } | ||
| 76 | $self->db->do("INSERT INTO preinits values ('$Rp','$k');"); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub init { # Return an init vector | ||
| 80 | my $self = shift; | ||
| 81 | my ($k, $Rp, $rp) = $self->_getpreinit; | ||
| 82 | unless ($k) { | ||
| 83 | until ($rp) { | ||
| 84 | $k = _makerandom($self->curve->n); | ||
| 85 | $Rp = $self->curve->g->multiply($k); | ||
| 86 | $rp = $Rp->x; | ||
| 87 | $Rp = _compress($Rp); | ||
| 88 | } | ||
| 89 | $self->_initkey($Rp => $k); | ||
| 90 | } | ||
| 91 | return $Rp; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub request { # Create a signing request | ||
| 95 | my ($self, %arg) = @_; | ||
| 96 | my $n = $self->curve->n; | ||
| 97 | my $Rp = _point_from_hex($arg{Init}); | ||
| 98 | my $rp = $Rp->x->bmod($n); | ||
| 99 | my $A = _makerandom($n); | ||
| 100 | my $B = _makerandom($n); | ||
| 101 | my $R = $Rp->multiply($A)->add($self->curve->g->multiply($B)); | ||
| 102 | my $r = $R->x->bmod($n); | ||
| 103 | my $hasher = Digest::SHA->new('sha256'); | ||
| 104 | $hasher->add($arg{Message}); | ||
| 105 | my $hash = $hasher->hexdigest; $hash =~ s/\s//g; | ||
| 106 | my $H = Math::BigInt->from_hex($hash); | ||
| 107 | my $mp = ($A * $H * $rp * $r->copy->bmodinv($n))->bmod($n); | ||
| 108 | $self->_request($arg{Init} => { R => $R, B => $B, H => $H }); | ||
| 109 | $self->_diag("request(): mp: $mp\n"); | ||
| 110 | return $mp; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub sign { # Create a blind signature | ||
| 114 | my ($self, %arg) = @_; | ||
| 115 | my $n = $self->curve->n; | ||
| 116 | my $Rp = _point_from_hex($arg{Init}); | ||
| 117 | my $rp = $Rp->x->bmod($n); | ||
| 118 | return unless my $k = $self->_initkey($arg{Init}); | ||
| 119 | my $sp = ($arg{Key}->d * $rp + $k * $arg{Message})->bmod($n); | ||
| 120 | $self->_diag("sign(): sp: $sp\n"); | ||
| 121 | return $sp; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub unblind { # Unblind a blind signature | ||
| 125 | my ($self, %arg) = @_; | ||
| 126 | my $n = $self->curve->n; | ||
| 127 | my $Rp = _point_from_hex($arg{Init}); | ||
| 128 | my $rp = $Rp->x->bmod($n); | ||
| 129 | return unless my $req = $self->_request($arg{Init}); | ||
| 130 | my $r = $req->{R}->x->bmod($n); | ||
| 131 | # Check here that sp and rp are in the range (1, n-1) | ||
| 132 | my $s = ($arg{Signature} * $r * $rp->copy->bmodinv($n) + $req->{H} * $req->{B})->bmod($n); | ||
| 133 | $self->_diag("unblind(): s: $s\n"); $s = $s->as_hex; $s =~ s/^0x//; | ||
| 134 | return ( bless { s => $s, | ||
| 135 | R => _compress($req->{R}) | ||
| 136 | }, 'Crypt::ECDSA::Blind::Signature' ); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub verify { # Verify a signature | ||
| 140 | my ($self, %arg) = @_; | ||
| 141 | my $r = $arg{Signature}->R->x->bmod($self->curve->n); | ||
| 142 | my $Q = $arg{Key}->Q; | ||
| 143 | my $hasher = Digest::SHA->new('sha256'); | ||
| 144 | $hasher->add($arg{Message}); | ||
| 145 | my $hash = $hasher->hexdigest; $hash =~ s/\s//g; | ||
| 146 | my $H = Math::BigInt->from_hex($hash); | ||
| 147 | $self->_diag('verify(): s: ' . $arg{Signature}->s . ', R(x): ' . $arg{Signature}->R->x . ", H: $H\n"); | ||
| 148 | my $u1 = $self->curve->g->multiply($arg{Signature}->s); | ||
| 149 | my $u2 = $Q->multiply($r)->add($arg{Signature}->R->multiply($H)); | ||
| 150 | $self->_diag('verify(): u1: ' . $u1->x . ', u2: ' . $u2->x . "\n"); | ||
| 151 | $u1->to_hex eq $u2->to_hex; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _getpreinit { # Get a pre-created init vector | ||
| 155 | my $self = shift; | ||
| 156 | my $timestamp = time; | ||
| 157 | while (1) { | ||
| 158 | my ($k,$Rp) = $self->db->selectrow_array("SELECT k,Rp FROM preinits LIMIT 1;"); | ||
| 159 | return undef unless $k; | ||
| 160 | $self->db->do("DELETE FROM preinits WHERE k='$k';"); | ||
| 161 | next unless $self->db->do("INSERT INTO initkeys values ('$Rp','$k','$timestamp');"); | ||
| 162 | return ($k, $Rp); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | sub _initkey { # Save or destructively retrieve a saved init vector | ||
| 167 | my $self = shift; | ||
| 168 | my $Rp = $_[0]; my $timestamp = time; | ||
| 169 | if ($_[1]) { | ||
| 170 | $self->db->do("INSERT INTO initkeys values ('$Rp','$_[1]','$timestamp');"); | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | my $k = $self->db->selectcol_arrayref("SELECT k from initkeys WHERE Rp='$Rp';")->[0]; | ||
| 174 | $self->db->do("DELETE FROM initkeys WHERE Rp='$Rp';"); | ||
| 175 | return Math::BigInt->new($k); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | sub _request { # Save or destructively retrieve a saved request | ||
| 180 | my $self = shift; | ||
| 181 | my $Rp = $_[0]; my $ret; | ||
| 182 | if ($_[1]) { | ||
| 183 | $self->{Requests}->{$Rp} = $_[1]; | ||
| 184 | } | ||
| 185 | else { | ||
| 186 | $ret = $self->{Requests}->{$Rp}; | ||
| 187 | delete $self->{Requests}->{$Rp}; | ||
| 188 | } | ||
| 189 | return $ret; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub _makerandom { | ||
| 193 | my $n = shift; my $nlen = length($n->as_bin)-2; | ||
| 194 | my $random = Bytes::Random::Secure->new( Bits => 128 ); | ||
| 195 | my $r = 0; | ||
| 196 | $r = Math::BigInt->from_bin($random->string_from('01',$nlen)) until ($r > 1 and $r < $n); | ||
| 197 | return $r; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub _point_from_hex { | ||
| 201 | my $P = Math::EllipticCurve::Prime::Point->from_hex(_decompress(shift)); | ||
| 202 | $P->curve(Math::EllipticCurve::Prime->from_name('secp256k1')); | ||
| 203 | $P; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _decompress { | ||
| 207 | my $Kc = shift; $Kc =~ /^(..)(.*)/; | ||
| 208 | my $i = $1; my $K = '04' . '0' x (64 - length($2)) . $2; my $x = Math::BigInt->from_hex($2); | ||
| 209 | my $curve = Math::EllipticCurve::Prime->from_name('secp256k1'); | ||
| 210 | my ($p, $a, $b) = ($curve->p, $curve->a, $curve->b); | ||
| 211 | my $y = ($x->bmodpow(3,$p)+$a*$x+$b)->bmodpow(($p+1)/4,$p); | ||
| 212 | $y = $p - $y if $i%2 ne $y%2; | ||
| 213 | my $yhex = $y->as_hex; $yhex =~ s/^0x//; | ||
| 214 | $K .= '0' x (64 - length($yhex)) . $yhex; # print "D:$K\n"; | ||
| 215 | return $K; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub _compress { | ||
| 219 | my $K = shift; # print 'C:'. $K->to_hex . "\n"; | ||
| 220 | my $Kc = $K->x->as_hex; $Kc =~ s/^0x//; | ||
| 221 | $Kc = '0' x (64 - length($Kc)) . $Kc; | ||
| 222 | $Kc = ($K->y % 2 ? '03' : '02') . $Kc; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub _diag { | ||
| 226 | my $self = shift; | ||
| 227 | print @_ if $self->debug; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub AUTOLOAD { | ||
| 231 | my $self = shift; (my $auto = $AUTOLOAD) =~ s/.*:://; | ||
| 232 | return if $auto eq 'DESTROY'; | ||
| 233 | if ($auto =~ /^(db|debug)$/x) { | ||
| 234 | $self->{$auto} = shift if (defined $_[0]); | ||
| 235 | } | ||
| 236 | if ($auto =~ /^(curve|db|debug)$/x) { | ||
| 237 | return $self->{$auto}; | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | die "Could not AUTOLOAD method $auto."; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | 1; # End of Crypt::ECDSA::Blind | ||
| 245 | |||
| 246 | package Crypt::ECDSA::Blind::PubKey; | ||
| 247 | |||
| 248 | sub write { | ||
| 249 | 1; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub as_hex { | ||
| 253 | Crypt::ECDSA::Blind::_compress(shift->Q); | ||
| 254 | } | ||
| 255 | |||
| 256 | sub from_hex { | ||
| 257 | bless {Q => Crypt::ECDSA::Blind::_point_from_hex(shift)}, 'Crypt::ECDSA::Blind::PubKey'; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub Q { | ||
| 261 | shift->{Q}; | ||
| 262 | } | ||
| 263 | |||
| 264 | 1; # End of Crypt::ECDSA::Blind::PubKey | ||
| 265 | |||
| 266 | package Crypt::ECDSA::Blind::SecKey; | ||
| 267 | |||
| 268 | sub as_hex { | ||
| 269 | my $d = shift->d->as_hex; $d =~ s/^0x//; | ||
| 270 | $d; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub from_hex { | ||
| 274 | bless {d => Math::BigInt->from_hex(shift)}, 'Crypt::ECDSA::Blind::SecKey'; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub write { | ||
| 278 | 1; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub d { | ||
| 282 | shift->{d}; | ||
| 283 | } | ||
| 284 | |||
| 285 | 1; # End of Crypt::ECDSA::Blind::SecKey | ||
| 286 | |||
| 287 | package Crypt::ECDSA::Blind::Signature; | ||
| 288 | |||
| 289 | sub s { | ||
| 290 | Math::BigInt->from_hex(shift->{'s'}); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub R { | ||
| 294 | Crypt::ECDSA::Blind::_point_from_hex(shift->{R}); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub is_valid { | ||
| 298 | my $self = shift; | ||
| 299 | $self->{R} =~ /^[0-9a-f]+$/ and $self->{s} =~ /^[0-9a-f]+$/; | ||
| 300 | } | ||
| 301 | |||
| 302 | 1; # End of Crypt::ECDSA::Blind::Signature | ||
| 303 | |||
| 304 | __END__ | ||