| 1 |
#BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net) |
| 2 |
#BSD All rights reserved. |
| 3 |
#BSD |
| 4 |
#BSD Redistribution and use in source and binary forms, with or without |
| 5 |
#BSD modification, are permitted provided that the following conditions |
| 6 |
#BSD are met: |
| 7 |
#BSD |
| 8 |
#BSD - Redistributions of source code must retain the above copyright |
| 9 |
#BSD notice, this list of conditions and the following disclaimer. |
| 10 |
#BSD |
| 11 |
#BSD - Redistributions in binary form must reproduce the above copyright |
| 12 |
#BSD notice, this list of conditions and the following disclaimer in the |
| 13 |
#BSD documentation and/or other materials provided with the distribution. |
| 14 |
#BSD |
| 15 |
#BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
#BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 |
#BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 |
#BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 |
#BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
#BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 |
#BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
#BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
#BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 |
#BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
#BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
#BSD POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
######################################################################## |
| 29 |
|
| 30 |
=item B<ntlm_new> |
| 31 |
|
| 32 |
Params: $username, $password [, $domain, $ntlm_only] |
| 33 |
|
| 34 |
Return: $ntlm_object |
| 35 |
|
| 36 |
Returns a reference to an array (otherwise known as the 'ntlm object') |
| 37 |
which contains the various informations specific to a user/pass combo. |
| 38 |
If $ntlm_only is set to 1, then only the NTLM hash (and not the LanMan |
| 39 |
hash) will be generated. This results in a speed boost, and is typically |
| 40 |
fine for using against IIS servers. |
| 41 |
|
| 42 |
The array contains the following items, in order: |
| 43 |
username, password, domain, lmhash(password), ntlmhash(password) |
| 44 |
|
| 45 |
=cut |
| 46 |
|
| 47 |
sub ntlm_new { |
| 48 |
my ( $user, $pass, $domain, $flag ) = @_; |
| 49 |
$flag ||= 0; |
| 50 |
return undef if ( !defined $user ); |
| 51 |
$pass ||= ''; |
| 52 |
$domain ||= ''; |
| 53 |
my @a = ( "$user", "$pass", "$domain", undef, undef ); |
| 54 |
my $t; |
| 55 |
|
| 56 |
if ( $flag == 0 ) { |
| 57 |
$t = substr( $pass, 0, 14 ); |
| 58 |
$t =~ tr/a-z/A-Z/; |
| 59 |
$t .= "\0" x ( 14 - length($t) ); |
| 60 |
$a[3] = des_E_P16($t); # LanMan password hash |
| 61 |
$a[3] .= "\0" x ( 21 - length( $a[3] ) ); |
| 62 |
} |
| 63 |
|
| 64 |
$t = md4( encode_unicode($pass) ); |
| 65 |
$t =~ s/([a-z0-9]{2})/sprintf("%c",hex($1))/ieg; |
| 66 |
$t .= "\0" x ( 21 - length($t) ); |
| 67 |
$a[4] = $t; # NTLM password hash |
| 68 |
|
| 69 |
&des_cache_reset(); # reset the keys hash |
| 70 |
return \@a; |
| 71 |
} |
| 72 |
|
| 73 |
######################################################################## |
| 74 |
|
| 75 |
sub ntlm_generate_responses { |
| 76 |
my ( $obj, $chal ) = @_; |
| 77 |
return ( undef, undef ) if ( !defined $obj || !defined $chal ); |
| 78 |
return ( undef, undef ) if ( !ref($obj) ); |
| 79 |
my $x = ''; |
| 80 |
$x = des_E_P24( $obj->[3], $chal ) if ( defined $obj->[3] ); |
| 81 |
return ( $x, des_E_P24( $obj->[4], $chal ) ); |
| 82 |
} |
| 83 |
|
| 84 |
######################################################################## |
| 85 |
|
| 86 |
=item B<ntlm_decode_challenge> |
| 87 |
|
| 88 |
Params: $challenge |
| 89 |
|
| 90 |
Return: @challenge_parts |
| 91 |
|
| 92 |
Splits the supplied challenge into the various parts. The returned array |
| 93 |
contains elements in the following order: |
| 94 |
|
| 95 |
unicode_domain, ident, packet_type, domain_len, domain_maxlen, |
| 96 |
domain_offset, flags, challenge_token, reserved, empty, raw_data |
| 97 |
|
| 98 |
=cut |
| 99 |
|
| 100 |
sub ntlm_decode_challenge { |
| 101 |
return undef if ( !defined $_[0] ); |
| 102 |
my $chal = shift; |
| 103 |
my @res; |
| 104 |
|
| 105 |
@res = unpack( 'Z8VvvVVa8a8a8', substr( $chal, 0, 48 ) ); |
| 106 |
push( @res, substr( $chal, 48 ) ); |
| 107 |
unshift( @res, substr( $chal, $res[4], $res[2] ) ); |
| 108 |
return @res; |
| 109 |
} |
| 110 |
|
| 111 |
######################################################################## |
| 112 |
|
| 113 |
sub ntlm_header { |
| 114 |
my ( $s, $h, $o ) = @_; |
| 115 |
my $l = length($s); |
| 116 |
return pack( 'vvV', 0, 0, $o - $h ) if ( $l == 0 ); |
| 117 |
return pack( 'vvV', $l, $l, $o ); |
| 118 |
} |
| 119 |
|
| 120 |
######################################################################## |
| 121 |
|
| 122 |
=item B<ntlm_client> |
| 123 |
|
| 124 |
Params: $ntlm_obj [, $server_challenge] |
| 125 |
|
| 126 |
Return: $response |
| 127 |
|
| 128 |
ntlm_client() is responsible for generating the base64-encoded text you |
| 129 |
include in the HTTP Authorization header. If you call ntlm_client() |
| 130 |
without a $server_challenge, the function will return the initial NTLM |
| 131 |
request packet (message packet #1). You send this to the server, and |
| 132 |
take the server's response (message packet #2) and pass that as |
| 133 |
$server_challenge, causing ntlm_client() to generate the final response |
| 134 |
packet (message packet #3). |
| 135 |
|
| 136 |
Note: $server_challenge is expected to be base64 encoded. |
| 137 |
|
| 138 |
=cut |
| 139 |
|
| 140 |
sub ntlm_client { |
| 141 |
my ( $obj, $p ) = @_; |
| 142 |
my $resp = "NTLMSSP\0"; |
| 143 |
|
| 144 |
return undef if ( !defined $obj || !ref($obj) ); |
| 145 |
|
| 146 |
if ( defined $p && $p ne '' ) { # answer challenge |
| 147 |
$p =~ tr/ \t\r\n//d; |
| 148 |
$p = decode_base64($p); |
| 149 |
my @c = ntlm_decode_challenge($p); |
| 150 |
my $uu = encode_unicode( $obj->[0] ); # username |
| 151 |
$resp .= pack( 'V', 3 ); |
| 152 |
my ( $hl, $hn ) = ntlm_generate_responses( $obj, $c[7] ); # token |
| 153 |
return undef if ( !defined $hl || !defined $hn ); |
| 154 |
my $o = 64; |
| 155 |
$resp .= ntlm_header( $hl, 64, $o ); # LM hash |
| 156 |
$resp .= ntlm_header( $hn, 64, ( $o += length($hl) ) ); # NTLM hash |
| 157 |
$resp .= ntlm_header( $c[0], 64, ( $o += length($hn) ) ); # domain |
| 158 |
$resp .= ntlm_header( $uu, 64, ( $o += length( $c[0] ) ) ); # username |
| 159 |
$resp .= ntlm_header( $uu, 64, ( $o += length($uu) ) ); # workstation |
| 160 |
$resp .= ntlm_header( '', 64, ( $o += length($uu) ) ); # session |
| 161 |
$resp .= pack( 'V', $c[6] ); |
| 162 |
$resp .= $hl . $hn . $c[0] . $uu . $uu; |
| 163 |
|
| 164 |
} |
| 165 |
else { # initiate challenge |
| 166 |
$resp .= pack( 'VV', 1, 0x0000b207 ); |
| 167 |
$resp .= ntlm_header( $obj->[0], 32, 32 ); |
| 168 |
$resp .= ntlm_header( $obj->[2], 32, 32 + length( $obj->[0] ) ); |
| 169 |
$resp .= $obj->[0] . $obj->[2]; |
| 170 |
} |
| 171 |
|
| 172 |
return encode_base64( $resp, '' ); |
| 173 |
} |
| 174 |
|
| 175 |
######################################################################## |
| 176 |
|
| 177 |
sub _ntlm_auth_callback { |
| 178 |
my ( $stream, $hi, $ho, $pflag ) = @_; |
| 179 |
my ( $ntlmobj, $header, $req_pre, $req_post, $aheader, $work, $ecode ); |
| 180 |
my ($rheader); |
| 181 |
$pflag ||= 0; |
| 182 |
|
| 183 |
if ($pflag) { |
| 184 |
$ntlmobj = $$hi{whisker}->{auth_proxy_data}; |
| 185 |
$header = 'Proxy-Authorization'; |
| 186 |
$rheader = 'proxy-authenticate'; |
| 187 |
$ecode = 407; |
| 188 |
$hi->{'Proxy-Connection'} = 'Keep-Alive'; |
| 189 |
} |
| 190 |
else { |
| 191 |
$ntlmobj = $$hi{whisker}->{auth_data}; |
| 192 |
$header = 'Authorization'; |
| 193 |
$rheader = 'www-authenticate'; |
| 194 |
$ecode = 401; |
| 195 |
$hi->{Connection} = 'Keep-Alive'; |
| 196 |
} |
| 197 |
|
| 198 |
$ho->{whisker}->{error} = 'NTLM ' . $header; |
| 199 |
$hi->{$header} = 'NTLM ' . ntlm_client($ntlmobj); |
| 200 |
my $ret = _http_do_request_ex( $stream, $hi, $ho ); |
| 201 |
return $ret if ($ret); |
| 202 |
return 200 if ( $$ho{whisker}->{code} == 200 ); |
| 203 |
return 1 if ( $$ho{whisker}->{code} != $ecode ); |
| 204 |
|
| 205 |
my $thead = utils_find_lowercase_key( $ho, $rheader ); |
| 206 |
return 1 if ( !defined $thead ); |
| 207 |
|
| 208 |
my ( $found, @auths ); |
| 209 |
if ( ref($thead) ) { @auths = @$thead; } |
| 210 |
else { push @auths, $thead; } |
| 211 |
foreach (@auths) { |
| 212 |
$found = $1 if (m/^NTLM (.+)$/); |
| 213 |
} |
| 214 |
return 1 if ( !defined $found ); |
| 215 |
|
| 216 |
$hi->{$header} = 'NTLM ' . ntlm_client( $ntlmobj, $found ); |
| 217 |
push @{ $hi->{whisker}->{header_delete_on_success} }, $header; |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
sub _ntlm_auth_proxy_callback { |
| 222 |
return _ntlm_auth_callback( $_[0], $_[1], $_[2], 1 ); |
| 223 |
} |
| 224 |
|
| 225 |
######################################################################## |
| 226 |
|
| 227 |
{ # start of DES local container ####################################### |
| 228 |
my $generated = 0; |
| 229 |
my $perm1 = [ |
| 230 |
57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, |
| 231 |
10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, |
| 232 |
63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, |
| 233 |
14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 |
| 234 |
]; |
| 235 |
my $perm2 = [ |
| 236 |
14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, |
| 237 |
26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, |
| 238 |
51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 |
| 239 |
]; |
| 240 |
my $perm3 = [ |
| 241 |
58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, |
| 242 |
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, |
| 243 |
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, |
| 244 |
61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 |
| 245 |
]; |
| 246 |
my $perm4 = [ |
| 247 |
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, |
| 248 |
12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, |
| 249 |
22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 |
| 250 |
]; |
| 251 |
my $perm5 = [ |
| 252 |
16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, |
| 253 |
2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 |
| 254 |
]; |
| 255 |
my $perm6 = [ |
| 256 |
40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, |
| 257 |
38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, |
| 258 |
36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, |
| 259 |
34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 |
| 260 |
]; |
| 261 |
my $sc = [ 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 ]; |
| 262 |
|
| 263 |
sub des_E_P16 { |
| 264 |
my ($p14) = @_; |
| 265 |
my $sp8 = [ 0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 ]; |
| 266 |
my $p7 = substr( $p14, 0, 7 ); |
| 267 |
my $p16 = des_smbhash( $sp8, $p7 ); |
| 268 |
$p7 = substr( $p14, 7, 7 ); |
| 269 |
$p16 .= des_smbhash( $sp8, $p7 ); |
| 270 |
return $p16; |
| 271 |
} |
| 272 |
|
| 273 |
sub des_E_P24 { |
| 274 |
my ( $p21, $c8_str ) = @_; |
| 275 |
my @c8 = map { ord($_) } split( //, $c8_str ); |
| 276 |
my $p24 = des_smbhash( \@c8, substr( $p21, 0, 7 ) ); |
| 277 |
$p24 .= des_smbhash( \@c8, substr( $p21, 7, 7 ) ); |
| 278 |
$p24 .= des_smbhash( \@c8, substr( $p21, 14, 7 ) ); |
| 279 |
} |
| 280 |
|
| 281 |
sub des_permute { |
| 282 |
my ( $i, $out, $in, $p, $n ) = ( 0, @_ ); |
| 283 |
foreach $i ( 0 .. ( $n - 1 ) ) { |
| 284 |
$out->[$i] = $in->[ $p->[$i] - 1 ]; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
sub des_lshift { |
| 289 |
my ( $c, $d, $count ) = @_; |
| 290 |
my ( @outc, @outd, $i, $x ); |
| 291 |
while ( $count-- ) { |
| 292 |
push @$c, shift @$c; |
| 293 |
push @$d, shift @$d; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
my %dohash_cache; # cache for key data; saves some cycles |
| 298 |
my %key_cache; # another cache for key data |
| 299 |
|
| 300 |
sub des_cache_reset { |
| 301 |
%dohash_cache = (); |
| 302 |
%key_cache = (); |
| 303 |
} |
| 304 |
|
| 305 |
sub des_dohash { |
| 306 |
my ( $out, $in, $key ) = @_; |
| 307 |
my ( $i, $j, $k, @pk1, @c, @d, @cd, @ki, @pd1, @l, @r, @rl ); |
| 308 |
|
| 309 |
# if(!defined $dohash_cache{$skey}){ |
| 310 |
&des_permute( \@pk1, $key, $perm1, 56 ); |
| 311 |
|
| 312 |
for ( $i = 0 ; $i < 28 ; $i++ ) { |
| 313 |
$c[$i] = $pk1[$i]; |
| 314 |
$d[$i] = $pk1[ $i + 28 ]; |
| 315 |
} |
| 316 |
for ( $i = 0 ; $i < 16 ; $i++ ) { |
| 317 |
my @array; |
| 318 |
&des_lshift( \@c, \@d, $sc->[$i] ); |
| 319 |
@cd = ( @c, @d ); |
| 320 |
&des_permute( \@array, \@cd, $perm2, 48 ); |
| 321 |
$ki[$i] = \@array; |
| 322 |
|
| 323 |
# $dohash_cache{$skey}->[$i]=\@array; |
| 324 |
} |
| 325 |
|
| 326 |
# } else { |
| 327 |
# for($i=0;$i<16;$i++){ |
| 328 |
# $ki[$i]=$dohash_cache{$skey}->[$i];} |
| 329 |
# } |
| 330 |
|
| 331 |
des_dohash2( $in, \@l, \@r, \@ki ); |
| 332 |
|
| 333 |
@rl = ( @r, @l ); |
| 334 |
&des_permute( $out, \@rl, $perm6, 64 ); |
| 335 |
} |
| 336 |
|
| 337 |
sub des_str_to_key { |
| 338 |
my ($str) = @_; |
| 339 |
my ( $i, @key, $out, @str ); |
| 340 |
unshift( @str, ord($_) ) while ( $_ = chop($str) ); |
| 341 |
$key[0] = $str[0] >> 1; |
| 342 |
$key[1] = ( ( $str[0] & 0x01 ) << 6 ) | ( $str[1] >> 2 ); |
| 343 |
$key[2] = ( ( $str[1] & 0x03 ) << 5 ) | ( $str[2] >> 3 ); |
| 344 |
$key[3] = ( ( $str[2] & 0x07 ) << 4 ) | ( $str[3] >> 4 ); |
| 345 |
$key[4] = ( ( $str[3] & 0x0f ) << 3 ) | ( $str[4] >> 5 ); |
| 346 |
$key[5] = ( ( $str[4] & 0x1f ) << 2 ) | ( $str[5] >> 6 ); |
| 347 |
$key[6] = ( ( $str[5] & 0x3f ) << 1 ) | ( $str[6] >> 7 ); |
| 348 |
$key[7] = $str[6] & 0x7f; |
| 349 |
foreach $i ( 0 .. 7 ) { |
| 350 |
$key[$i] = 0xff & ( $key[$i] << 1 ); |
| 351 |
} |
| 352 |
@{ $key_cache{$str} } = @key; |
| 353 |
return \@key; |
| 354 |
} |
| 355 |
|
| 356 |
sub des_smbhash { |
| 357 |
my ( $in, $key ) = @_; |
| 358 |
my $key2; |
| 359 |
|
| 360 |
&des_generate if ( !$generated ); |
| 361 |
if ( defined $key_cache{$key} ) { |
| 362 |
$key2 = $key_cache{$key}; |
| 363 |
} |
| 364 |
else { $key2 = &des_str_to_key($key); } |
| 365 |
|
| 366 |
my ( $i, $div, $mod, @in, @outb, @inb, @keyb, @out ); |
| 367 |
foreach $i ( 0 .. 63 ) { |
| 368 |
$div = int( $i / 8 ); |
| 369 |
$mod = $i % 8; |
| 370 |
$inb[$i] = ( $in->[$div] & ( 1 << ( 7 - ($mod) ) ) ) ? 1 : 0; |
| 371 |
$keyb[$i] = ( $key2->[$div] & ( 1 << ( 7 - ($mod) ) ) ) ? 1 : 0; |
| 372 |
$outb[$i] = 0; |
| 373 |
} |
| 374 |
&des_dohash( \@outb, \@inb, \@keyb ); |
| 375 |
foreach $i ( 0 .. 7 ) { $out[$i] = 0; } |
| 376 |
foreach $i ( 0 .. 63 ) { |
| 377 |
$out[ int( $i / 8 ) ] |= ( 1 << ( 7 - ( $i % 8 ) ) ) |
| 378 |
if ( $outb[$i] ); |
| 379 |
} |
| 380 |
my $out = pack( "C8", @out ); |
| 381 |
|
| 382 |
return $out; |
| 383 |
} |
| 384 |
|
| 385 |
sub des_generate { # really scary dragons here....this code is optimized |
| 386 |
# for speed, and not readability |
| 387 |
my ( $i, $j ); |
| 388 |
my $code = <<EOT; |
| 389 |
{ my \$sbox = [[ |
| 390 |
[14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7],[0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8], |
| 391 |
[4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0],[15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13] |
| 392 |
],[ |
| 393 |
[15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10],[3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5], |
| 394 |
[0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15],[13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9] |
| 395 |
],[ |
| 396 |
[10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8],[13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1], |
| 397 |
[13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7],[1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12] |
| 398 |
],[ |
| 399 |
[7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15],[13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9], |
| 400 |
[10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4],[3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14] |
| 401 |
],[ |
| 402 |
[2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9],[14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6], |
| 403 |
[4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14],[11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3] |
| 404 |
],[ |
| 405 |
[12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11],[10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8], |
| 406 |
[9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6],[4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13] |
| 407 |
],[ |
| 408 |
[4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1],[13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6], |
| 409 |
[1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2],[6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12] |
| 410 |
],[ |
| 411 |
[13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7],[1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2], |
| 412 |
[7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8],[2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11] |
| 413 |
]]; |
| 414 |
EOT |
| 415 |
|
| 416 |
$code .= |
| 417 |
'sub des_dohash2 { my ($in,$l,$r,$ki)=@_; my (@p,$i,$j,$k,$m,$n);'; |
| 418 |
for ( $i = 0 ; $i < 64 ; $i++ ) { |
| 419 |
$code .= "\$p[$i] = \$in->[" . ( $perm3->[$i] - 1 ) . "];\n"; |
| 420 |
} |
| 421 |
for ( $i = 0 ; $i < 32 ; $i++ ) { |
| 422 |
$code .= "\$l->[$i]=\$p[$i]; \$r->[$i]=\$p[" . ( $i + 32 ) . "];\n"; |
| 423 |
} |
| 424 |
$code .= 'for($i=0;$i<16;$i++){ local (@er,@erk,@b,@cb,@pcb,@r2);'; |
| 425 |
for ( $i = 0 ; $i < 48 ; $i++ ) { |
| 426 |
$code .= |
| 427 |
"\$erk[$i]=\$r->[" |
| 428 |
. ( $perm4->[$i] - 1 ) |
| 429 |
. "]^(\$ki->[\$i]->[$i]);\n"; |
| 430 |
} |
| 431 |
for ( $i = 0 ; $i < 8 ; $i++ ) { |
| 432 |
for ( $j = 0 ; $j < 6 ; $j++ ) { |
| 433 |
$code .= "\$b[$i][$j]=\$erk[" . ( $i * 6 + $j ) . "];\n"; |
| 434 |
} |
| 435 |
} |
| 436 |
for ( $i = 0 ; $i < 8 ; $i++ ) { |
| 437 |
$code .= "\$m=(\$b[$i][0]<<1)|\$b[$i][5];\n"; |
| 438 |
$code .= |
| 439 |
"\$n=(\$b[$i][1]<<3)|(\$b[$i][2]<<2)|(\$b[$i][3]<<1)|\$b[$i][4];\n"; |
| 440 |
for ( $j = 0 ; $j < 4 ; $j++ ) { |
| 441 |
$code .= |
| 442 |
"\$b[$i][$j]=(\$sbox->[$i][\$m][\$n]&" |
| 443 |
. ( 1 << ( 3 - $j ) ) |
| 444 |
. ")?1:0;\n"; |
| 445 |
} |
| 446 |
} |
| 447 |
for ( $i = 0 ; $i < 8 ; $i++ ) { |
| 448 |
for ( $j = 0 ; $j < 4 ; $j++ ) { |
| 449 |
$code .= "\$cb[" . ( $i * 4 + $j ) . "]=\$b[$i][$j];\n"; |
| 450 |
} |
| 451 |
} |
| 452 |
for ( $i = 0 ; $i < 32 ; $i++ ) { |
| 453 |
$code .= "\$pcb[$i]=\$cb[" . ( $perm5->[$i] - 1 ) . "];\n"; |
| 454 |
} |
| 455 |
for ( $i = 0 ; $i < 32 ; $i++ ) { |
| 456 |
$code .= "\$r2[$i]=(\$l->[$i])^\$pcb[$i];\n"; |
| 457 |
} |
| 458 |
for ( $i = 0 ; $i < 32 ; $i++ ) { |
| 459 |
$code .= "\$l->[$i]=\$r->[$i]; \$r->[$i]=\$r2[$i];\n"; |
| 460 |
} |
| 461 |
$code .= '}}}'; |
| 462 |
|
| 463 |
eval "$code"; |
| 464 |
print "DEBUG: $code\n\n"; |
| 465 |
$generated++; |
| 466 |
} |
| 467 |
|
| 468 |
} ##### end of DES container ################################################ |
| 469 |
|