| 1 | #!/usr/bin/perl | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | package HEAT::Crypto; | ||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 1 | ▶ | use XSLoader; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 1 | ▶ | use Digest::SHA; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 1 | ▶ | use Crypt::Mode::CBC; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 1 | ▶ | use Crypt::PRNG qw(random_bytes); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 1 | our $VERSION = '0.06'; | |||||||||||||||||||||
| 15 | 1 | XSLoader::load('HEAT::Crypto', $VERSION); | |||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | 1 | our @EXPORT_OK = qw(hash keyspec keygen priv_to_pub_key | |||||||||||||||||||||
| 19 | shared_key sign verify encrypt decrypt account_id tohex unhex); | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | 1 | my $cbc = Crypt::Mode::CBC->new('AES'); | |||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | sub KEYSIZE() | ||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||
| 25 | 32; | ||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | sub KEYBUFF() | ||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||
| 30 | "\0" x KEYSIZE; | ||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | sub tohex($) | ||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||
| 35 | 0 | ▶ | return undef unless defined $_[0]; | ||||||||||||||||||||
|
Subroutine
tohex: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||
| 36 | 0 | unpack('H*', $_[0]); | |||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | sub unhex($) | ||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||
| 41 | 0 | ▶ | return undef unless defined $_[0]; | ||||||||||||||||||||
|
Subroutine
unhex: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||
| 42 | 0 | pack('H*', $_[0]); | |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | sub hash | ||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||
| 47 | 9 | ▶ | my $sha = Digest::SHA->new(256); | ||||||||||||||||||||
|
Subroutine
hash: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 48 | 9 | $sha->add($_) for @_; | |||||||||||||||||||||
| 49 | 9 | return $sha->digest; | |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | sub keyhash | ||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||
| 54 | 0 | ▶ | my $k = hash(@_); | ||||||||||||||||||||
|
Subroutine
keyhash: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 55 | 0 | _clamp($k); | |||||||||||||||||||||
| 56 | 0 | return $k; | |||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | sub keygen(;$) | ||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||
| 61 | 6 | ▶ | my $k = defined($_[0]) ? keyspec($_[0]) : random_bytes(KEYSIZE); | ||||||||||||||||||||
|
Subroutine
keygen: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||
| 62 | 6 | my $p = KEYBUFF; | |||||||||||||||||||||
| 63 | 6 | my $s = KEYBUFF; | |||||||||||||||||||||
| 64 | |||||||||||||||||||||||
| 65 | 6 | _clamp($k); | |||||||||||||||||||||
| 66 | 6 | _core($p, $s, $k, undef); | |||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | return { | ||||||||||||||||||||||
| 69 | 6 | p => $p, | |||||||||||||||||||||
| 70 | s => $s, | ||||||||||||||||||||||
| 71 | k => $k, | ||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | sub shared_key($$) | ||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||
| 77 | 4 | ▶ | my $k = keyspec($_[0], 1); | ||||||||||||||||||||
|
Subroutine
shared_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 78 | 4 | my $p = keyspec($_[1]); | |||||||||||||||||||||
| 79 | 4 | my $z = KEYBUFF; | |||||||||||||||||||||
| 80 | 4 | _core($z, undef, $k, $p); | |||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | 4 | return $z; | |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | sub sign($$) | ||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||
| 87 | 1 | ▶ | my $k = keyspec($_[0], 1); | ||||||||||||||||||||
|
Subroutine
sign: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||
| 88 | 1 | my $msg = $_[1]; | |||||||||||||||||||||
| 89 | |||||||||||||||||||||||
| 90 | 1 | my $m = hash($msg); | |||||||||||||||||||||
| 91 | 1 | my $r = keygen($k); | |||||||||||||||||||||
| 92 | 1 | my $x = hash($m, $r->{s}); | |||||||||||||||||||||
| 93 | 1 | my $y = keygen($x); | |||||||||||||||||||||
| 94 | 1 | my $h = hash($m, $y->{p}); | |||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | 1 | my $v = KEYBUFF; | |||||||||||||||||||||
| 97 | 1 | ▶ | if (_sign($v, $h, $x, $r->{s})) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 98 | 1 | return $v . $h; | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 0 | return undef; | |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||
| 103 | |||||||||||||||||||||||
| 104 | sub verify($$$) | ||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||
| 106 | 1 | ▶ | my ($s, $m) = @_; | ||||||||||||||||||||
|
Subroutine
verify: called CC=4 SCAR=15.5
Pod
documented
| |||||||||||||||||||||||
| 107 | 1 | my $k = keyspec($_[2]); | |||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | 1 | ▶ | unless (defined $s) { | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 110 | 0 | croak('undefined signature'); | |||||||||||||||||||||
| 111 | } elsif ($s =~ /^[[:xdigit:]]{128}$/) { | ||||||||||||||||||||||
| 112 | 0 | $s = unhex($s); | |||||||||||||||||||||
| 113 | } elsif (length $s != 64) { | ||||||||||||||||||||||
| 114 | 0 | croak('invalid signature: %q', $s); | |||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | 1 | my $v = substr($s, 0, 32); | |||||||||||||||||||||
| 118 | 1 | my $h = substr($s, 32, 64); | |||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | 1 | my $y = KEYBUFF; | |||||||||||||||||||||
| 121 | 1 | _verify($y, $v, $h, $k); | |||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | 1 | return hash(hash($m), $y) eq $h; | |||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||
| 125 | |||||||||||||||||||||||
| 126 | sub keyspec($;$) | ||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||
| 128 | 18 | ▶ | my ($spec, $is_private) = @_; | ||||||||||||||||||||
|
Subroutine
keyspec: called CC=6 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | 18 | ▶ | unless (defined $spec) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 131 | 0 | croak('undefined key spec'); | |||||||||||||||||||||
| 132 | ▶ | } elsif ($is_private && $spec =~ /^([a-z]{3,12}( |\Z)){12}$/) { | |||||||||||||||||||||
|
Condition66%2/3
$is_private and $spec =~ /^([a-z]{3,12}( |\Z)){12}$/
MC/DC0%0/2
$is_private and $spec =~ /^([a-z]{3,12}( |\Z)){12}$/
$is_private
$spec =~ /^([a-z]{3,12}( |\Z)){12}$/
Truth table66%2/3
A and B
A= $is_privateB= $spec =~ /^([a-z]{3,12}( |\Z)){12}$/
| |||||||||||||||||||||||
| 133 | 0 | return keyhash($spec); | |||||||||||||||||||||
| 134 | } elsif (length $spec == KEYSIZE) { | ||||||||||||||||||||||
| 135 | 18 | return $spec; | |||||||||||||||||||||
| 136 | } elsif ($spec =~ /^[[:xdigit:]]{64}$/) { | ||||||||||||||||||||||
| 137 | 0 | return unhex($spec); | |||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||
| 139 | 0 | croak('invalid key spec: %q', $spec); | |||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | sub priv_to_pub_key($) | ||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||
| 145 | 2 | ▶ | my $k = keyspec($_[0], 1); | ||||||||||||||||||||
|
Subroutine
priv_to_pub_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 146 | 2 | my $r = keygen($k); | |||||||||||||||||||||
| 147 | 2 | return $r->{p}; | |||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||
| 149 | |||||||||||||||||||||||
| 150 | sub account_id($) | ||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||
| 152 | 0 | ▶ | my $k = keyspec($_[0]); | ||||||||||||||||||||
|
Subroutine
account_id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 153 | 0 | my $h = hash($k); | |||||||||||||||||||||
| 154 | |||||||||||||||||||||||
| 155 | 0 | my ($id, $t1, $t2) = (0); | |||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | 0 | ▶ | for (my $i = 7; $i >= 0; $i--) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 158 | 0 | $t1 = $id * 256; | |||||||||||||||||||||
| 159 | 0 | $t2 = $t1 + vec($h, $i, 8); | |||||||||||||||||||||
| 160 | 0 | $id = $t2; | |||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | 0 | return $id; | |||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | sub encrypt($$;$) | ||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||
| 168 | 2 | ▶ | my ($data, $k, $p) = @_; | ||||||||||||||||||||
|
Subroutine
encrypt: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||
| 169 | |||||||||||||||||||||||
| 170 | 2 | ▶ | my $key = @_ == 3 ? shared_key($k, $p) : keyspec($k); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 171 | |||||||||||||||||||||||
| 172 | 2 | my $iv = random_bytes(16); | |||||||||||||||||||||
| 173 | 2 | my $nonce = random_bytes(32); | |||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | 2 | ▶ | for (my $i = 0; $i < 32; $i++) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 176 | 64 | vec($key, $i, 8) = vec($key, $i, 8) ^ vec($nonce, $i, 8); | |||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | 2 | my $encrypted = eval { $cbc->encrypt($data, hash($key), $iv) }; | |||||||||||||||||||||
| 180 | 2 | ▶ | return undef if $@; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 181 | |||||||||||||||||||||||
| 182 | 2 | ▶ | return wantarray ? ($nonce, $iv, $encrypted) : $nonce . $iv . $encrypted; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||
| 184 | |||||||||||||||||||||||
| 185 | sub decrypt($$;$) | ||||||||||||||||||||||
| 186 | { | ||||||||||||||||||||||
| 187 | 2 | ▶ | my ($data, $k, $p) = @_; | ||||||||||||||||||||
|
Subroutine
decrypt: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | 2 | ▶ | my $key = @_ == 3 ? shared_key($k, $p) : keyspec($k); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 190 | |||||||||||||||||||||||
| 191 | my ($nonce, $iv, $encrypted) = ref($data) eq 'ARRAY' | ||||||||||||||||||||||
| 192 | 2 | ▶ | ? @{$data} : unpack('a32 a16 a*', $data); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 193 | |||||||||||||||||||||||
| 194 | 2 | ▶ | for (my $i = 0; $i < 32; $i++) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 195 | 64 | vec($key, $i, 8) = vec($key, $i, 8) ^ vec($nonce, $i, 8); | |||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||
| 197 | |||||||||||||||||||||||
| 198 | 2 | my $decrypted = eval { $cbc->decrypt($encrypted, hash($key), $iv) }; | |||||||||||||||||||||
| 199 | 2 | ▶ | return undef if $@; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 200 | |||||||||||||||||||||||
| 201 | 1 | return $decrypted; | |||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||
| 203 | |||||||||||||||||||||||
| 204 | 1 | 1; | |||||||||||||||||||||
| 205 | |||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||