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