| 1 | 26 | ▶ | use v5.40; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 26 | ▶ | use feature 'class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 26 | ▶ | no warnings qw[experimental::class experimental::builtin]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use Net::BitTorrent::Emitter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 26 | class Net::BitTorrent::Protocol::MSE::KeyExchange v2.1.1 : isa(Net::BitTorrent::Emitter) { | |||||||||||||||||||||||||||
| 6 | 26 | ▶ | use Digest::SHA qw[sha1]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 26 | ▶ | use Crypt::URandom qw[urandom]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 26 | ▶ | use Math::BigInt try => 'GMP'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # Parameters | ||||||||||||||||||||||||||||
| 11 | 26 | field $infohash : param : reader; | |||||||||||||||||||||||||||
| 12 | 26 | field $is_initiator : param : reader; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Internal state | ||||||||||||||||||||||||||||
| 15 | 26 | field $private_key; | |||||||||||||||||||||||||||
| 16 | 26 | field $public_key : reader; | |||||||||||||||||||||||||||
| 17 | 26 | field $shared_secret; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Cipher state | ||||||||||||||||||||||||||||
| 20 | 26 | field $encrypt_rc4 : reader; | |||||||||||||||||||||||||||
| 21 | 26 | field $decrypt_rc4 : reader; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Store the initial state (post-discard) for the decryptor | ||||||||||||||||||||||||||||
| 24 | # to optimize the scan_for_vc loop. | ||||||||||||||||||||||||||||
| 25 | 26 | field $decrypt_restore_point; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # 768-bit Safe Prime (Big Endian) | ||||||||||||||||||||||||||||
| 28 | 26 | my $P_STR | |||||||||||||||||||||||||||
| 29 | = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' . | ||||||||||||||||||||||||||||
| 30 | '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' . | ||||||||||||||||||||||||||||
| 31 | '4FE1356D6D51C245E485B576625E7EC6F44C42E9A63A36210000000000090563'; | ||||||||||||||||||||||||||||
| 32 | 51 | ▶ | ADJUST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 51 | my $p = Math::BigInt->from_hex($P_STR); | |||||||||||||||||||||||||||
| 34 | 51 | my $g = Math::BigInt->new(2); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Private Key: Random 160 bits | ||||||||||||||||||||||||||||
| 37 | 51 | $private_key = Math::BigInt->from_bytes( urandom(20) ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Public Key: Y = G^X mod P | ||||||||||||||||||||||||||||
| 40 | 51 | my $pub_val = $g->copy->bmodpow( $private_key, $p ); | |||||||||||||||||||||||||||
| 41 | 51 | $public_key = $self->_int_to_bytes($pub_val); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 60 | ▶ | method _int_to_bytes ($num) { | ||||||||||||||||||||||||||
|
Subroutine
_int_to_bytes: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||
| 45 | 60 | my $hex = $num->to_hex; | |||||||||||||||||||||||||||
| 46 | 60 | $hex =~ s/^0x//i; | |||||||||||||||||||||||||||
| 47 | 60 | ▶ | $hex = "0$hex" if length($hex) % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 60 | my $bin = pack( 'H*', $hex ); | |||||||||||||||||||||||||||
| 49 | 60 | ▶ | if ( length($bin) < 96 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 50 | 0 | $bin = ( "\0" x ( 96 - length($bin) ) ) . $bin; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | elsif ( length($bin) > 96 ) { | ||||||||||||||||||||||||||||
| 53 | 0 | $bin = substr( $bin, -96 ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 60 | return $bin; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 10 | ▶ | method compute_secret ($remote_pub_bytes) { | ||||||||||||||||||||||||||
|
Subroutine
compute_secret: called CC=4 SCAR=14.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 10 | ▶ | if ( length($remote_pub_bytes) != 96 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 1 | $self->_emit_log( 'fatal', 'Remote public key must be 96 bytes' ); | |||||||||||||||||||||||||||
| 61 | 0 | return undef; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 9 | my $p = Math::BigInt->from_hex($P_STR); | |||||||||||||||||||||||||||
| 64 | 9 | my $remote_val = Math::BigInt->from_bytes($remote_pub_bytes); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Reject degenerate keys that defeat forward secrecy (RFC 2631 / FIPS 186-4) | ||||||||||||||||||||||||||||
| 67 | 9 | my $two = Math::BigInt->new(2); | |||||||||||||||||||||||||||
| 68 | 9 | my $p_minus_2 = $p->copy->bsub($two); | |||||||||||||||||||||||||||
| 69 | 9 | ▶ | if ( $remote_val < $two || $remote_val > $p_minus_2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$remote_val < $two or $remote_val > $p_minus_2
MC/DC0%0/2
$remote_val < $two or $remote_val > $p_minus_2
$remote_val < $two
$remote_val > $p_minus_2
Truth table33%1/3
A or B
A= $remote_val < $twoB= $remote_val > $p_minus_2
| |||||||||||||||||||||||||||||
| 70 | 0 | $self->_emit_log( 'fatal', 'Remote public key out of valid range [2, P-2]' ); | |||||||||||||||||||||||||||
| 71 | 0 | return undef; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # S = Y_remote ^ X_local mod P | ||||||||||||||||||||||||||||
| 75 | 9 | my $s_val = $remote_val->copy->bmodpow( $private_key, $p ); | |||||||||||||||||||||||||||
| 76 | 9 | $shared_secret = $self->_int_to_bytes($s_val); | |||||||||||||||||||||||||||
| 77 | 9 | return $shared_secret; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 2 | ▶ | method get_secret () { return $shared_secret } | ||||||||||||||||||||||||||
|
Subroutine
get_secret: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | method get_sync_data ( $override_ih = undef ) { | ||||||||||||||||||||||||||
|
Subroutine
get_sync_data: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 3 | ▶ | my $ih = $override_ih // $infohash; | ||||||||||||||||||||||||||
|
Condition33%1/3
$override_ih // $infohash
MC/DC0%0/2
$override_ih // $infohash
$override_ih
$infohash
Truth table33%1/3
A // B
A= $override_ihB= $infohash
| |||||||||||||||||||||||||||||
| 83 | 3 | ▶ | return undef unless $ih; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 3 | my $s = $shared_secret; | |||||||||||||||||||||||||||
| 85 | 3 | my $sk = $ih; | |||||||||||||||||||||||||||
| 86 | 3 | my $req1_hash = sha1( 'req1' . $s ); | |||||||||||||||||||||||||||
| 87 | 3 | my $req2_hash = sha1( 'req2' . $sk ); | |||||||||||||||||||||||||||
| 88 | 3 | my $req3_hash = sha1( 'req3' . $s ); | |||||||||||||||||||||||||||
| 89 | 3 | my $xor_mask = $req2_hash^.$req3_hash; | |||||||||||||||||||||||||||
| 90 | 3 | return ( $req1_hash, $xor_mask ); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 3 | ▶ | method verify_skey ( $xor_block, $candidate_ih ) { | ||||||||||||||||||||||||||
|
Subroutine
verify_skey: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 3 | my $s = $shared_secret; | |||||||||||||||||||||||||||
| 95 | 3 | my $req3_hash = sha1( 'req3' . $s ); | |||||||||||||||||||||||||||
| 96 | 3 | my $target_req2 = $xor_block^.$req3_hash; | |||||||||||||||||||||||||||
| 97 | 3 | my $check = sha1( 'req2' . $candidate_ih ); | |||||||||||||||||||||||||||
| 98 | # | ||||||||||||||||||||||||||||
| 99 | 3 | my $ok = 0; | |||||||||||||||||||||||||||
| 100 | 3 | for my $i ( 0 .. 19 ) { | |||||||||||||||||||||||||||
| 101 | 60 | $ok |= ord( substr( $check, $i, 1 ) ) ^ ord( substr( $target_req2, $i, 1 ) ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 3 | return $ok == 0; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | ▶ | method init_rc4 ($ih) { | ||||||||||||||||||||||||||
|
Subroutine
init_rc4: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 3 | $infohash = $ih; | |||||||||||||||||||||||||||
| 108 | 3 | my $keyA = sha1( 'keyA' . $shared_secret . $infohash ); | |||||||||||||||||||||||||||
| 109 | 3 | my $keyB = sha1( 'keyB' . $shared_secret . $infohash ); | |||||||||||||||||||||||||||
| 110 | 3 | my ( $key_enc, $key_dec ); | |||||||||||||||||||||||||||
| 111 | 3 | ▶ | if ($is_initiator) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | $key_enc = $keyA; | |||||||||||||||||||||||||||
| 113 | 2 | $key_dec = $keyB; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 1 | $key_enc = $keyB; | |||||||||||||||||||||||||||
| 117 | 1 | $key_dec = $keyA; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Initialize Encryptor | ||||||||||||||||||||||||||||
| 121 | 3 | $encrypt_rc4 = Net::BitTorrent::Protocol::MSE::RC4->new( key => $key_enc ); | |||||||||||||||||||||||||||
| 122 | 3 | $encrypt_rc4->discard(1024); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Initialize Decryptor | ||||||||||||||||||||||||||||
| 125 | 3 | $decrypt_rc4 = Net::BitTorrent::Protocol::MSE::RC4->new( key => $key_dec ); | |||||||||||||||||||||||||||
| 126 | 3 | $decrypt_rc4->discard(1024); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Save state for efficient scanning | ||||||||||||||||||||||||||||
| 129 | 3 | $decrypt_restore_point = $decrypt_rc4->snapshot(); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | ▶ | method scan_for_vc ($buffer) { | ||||||||||||||||||||||||||
|
Subroutine
scan_for_vc: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 4 | my $limit = length($buffer) - 8; | |||||||||||||||||||||||||||
| 134 | 4 | ▶ | $limit = 512 if $limit > 512; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Use a temporary RC4 instance to avoid messing up the main decryptor | ||||||||||||||||||||||||||||
| 137 | # during the brute force attempts. | ||||||||||||||||||||||||||||
| 138 | 4 | my $trial_rc4 = Net::BitTorrent::Protocol::MSE::RC4->new( key => 'dummy' ); | |||||||||||||||||||||||||||
| 139 | 4 | for my $offset ( 0 .. $limit ) { | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Restore state to the "post-discard" point instantly (no math involved) | ||||||||||||||||||||||||||||
| 142 | 1396 | $trial_rc4->restore($decrypt_restore_point); | |||||||||||||||||||||||||||
| 143 | 1396 | my $ciphertext = substr( $buffer, $offset, 8 ); | |||||||||||||||||||||||||||
| 144 | 1396 | my $plaintext = $trial_rc4->crypt($ciphertext); | |||||||||||||||||||||||||||
| 145 | 1396 | ▶ | if ( $plaintext eq "\0\0\0\0\0\0\0\0" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # Found it! We need to ensure the MAIN decryptor is now | ||||||||||||||||||||||||||||
| 148 | # advanced by these 8 bytes so subsequent calls work. | ||||||||||||||||||||||||||||
| 149 | # However, the padding (0..$offset) is plaintext garbage. | ||||||||||||||||||||||||||||
| 150 | # We skip the padding, then decrypt the VC to sync state. | ||||||||||||||||||||||||||||
| 151 | # Note: The caller handles substr($buffer, ...) logic. | ||||||||||||||||||||||||||||
| 152 | # We just return the offset found. | ||||||||||||||||||||||||||||
| 153 | # The caller MUST call $decrypt_rc4->crypt($vc_bytes) | ||||||||||||||||||||||||||||
| 154 | # to sync the main object. | ||||||||||||||||||||||||||||
| 155 | 1 | return $offset; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 3 | return -1; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Pure Perl RC4 Implementation | ||||||||||||||||||||||||||||
| 163 | 26 | class Net::BitTorrent::Protocol::MSE::RC4 v2.1.1 : isa(Net::BitTorrent::Emitter) { | |||||||||||||||||||||||||||
| 164 | 26 | field @S; | |||||||||||||||||||||||||||
| 165 | 26 | field $x = 0; | |||||||||||||||||||||||||||
| 166 | 26 | field $y = 0; | |||||||||||||||||||||||||||
| 167 | 26 | field $key : param; | |||||||||||||||||||||||||||
| 168 | 10 | ▶ | ADJUST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 169 | # KSA (Key Scheduling Algorithm) | ||||||||||||||||||||||||||||
| 170 | 10 | @S = 0 .. 255; | |||||||||||||||||||||||||||
| 171 | 10 | my $len = length($key); | |||||||||||||||||||||||||||
| 172 | 10 | my @k = unpack( 'C*', $key ); | |||||||||||||||||||||||||||
| 173 | 10 | my $j = 0; | |||||||||||||||||||||||||||
| 174 | 10 | for my $i ( 0 .. 255 ) { | |||||||||||||||||||||||||||
| 175 | 2560 | $j = ( $j + $S[$i] + $k[ $i % $len ] ) & 0xFF; | |||||||||||||||||||||||||||
| 176 | 2560 | @S[ $i, $j ] = @S[ $j, $i ]; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 6 | ▶ | method discard ($bytes) { | ||||||||||||||||||||||||||
|
Subroutine
discard: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Discard loop (PRGA without output) | ||||||||||||||||||||||||||||
| 183 | 6 | for ( 1 .. $bytes ) { | |||||||||||||||||||||||||||
| 184 | 6144 | $x = ( $x + 1 ) & 0xFF; | |||||||||||||||||||||||||||
| 185 | 6144 | $y = ( $y + $S[$x] ) & 0xFF; | |||||||||||||||||||||||||||
| 186 | 6144 | @S[ $x, $y ] = @S[ $y, $x ]; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 1406 | ▶ | method crypt ($data) { | ||||||||||||||||||||||||||
|
Subroutine
crypt: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 191 | 1406 | my $out = ''; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # Use split for simple iteration over bytes | ||||||||||||||||||||||||||||
| 194 | 1406 | for my $c ( split //, $data ) { | |||||||||||||||||||||||||||
| 195 | 11274 | $x = ( $x + 1 ) & 0xFF; | |||||||||||||||||||||||||||
| 196 | 11274 | $y = ( $y + $S[$x] ) & 0xFF; | |||||||||||||||||||||||||||
| 197 | 11274 | @S[ $x, $y ] = @S[ $y, $x ]; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # String XOR (^.) available in 5.40 | ||||||||||||||||||||||||||||
| 200 | 11274 | $out .= $c^. chr( $S[ ( $S[$x] + $S[$y] ) & 0xFF ] ); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | 1406 | return $out; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # Create a lightweight state snapshot (Array ref + 2 ints) | ||||||||||||||||||||||||||||
| 206 | 3 | ▶ | method snapshot () { | ||||||||||||||||||||||||||
|
Subroutine
snapshot: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 207 | 3 | return [ [@S], $x, $y ]; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # Restore state from snapshot | ||||||||||||||||||||||||||||
| 211 | 1396 | ▶ | method restore ($snap) { | ||||||||||||||||||||||||||
|
Subroutine
restore: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 212 | 1396 | @S = $snap->[0]->@*; | |||||||||||||||||||||||||||
| 213 | 1396 | $x = $snap->[1]; | |||||||||||||||||||||||||||
| 214 | 1396 | $y = $snap->[2]; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | 26 | } 1; | |||||||||||||||||||||||||||