| 1 | #!/usr/local/bin/perl | ||||||||||
| 2 | # | ||||||||||
| 3 | # This is a partial implentation of the MD4 checksum code. | ||||||||||
| 4 | # | ||||||||||
| 5 | # NOTE | ||||||||||
| 6 | # | ||||||||||
| 7 | # The function &add() in this module is required as we need to be | ||||||||||
| 8 | # able to add 32bit integers ignoring overflow. The C code this is | ||||||||||
| 9 | # based on does this because it uses the underlying hardware to | ||||||||||
| 10 | # perform the required addition however we need to be more careful | ||||||||||
| 11 | # as Perl will overflow an int and produce a result of 0xffffffff | ||||||||||
| 12 | # which is not very useful. The &add() function splits its arguments | ||||||||||
| 13 | # into two shorts and adds these carrying overflow from the low short | ||||||||||
| 14 | # to the high short and ignoring carry from the high short. Not | ||||||||||
| 15 | # exactly efficient, but it works and is fast enough for the purposes | ||||||||||
| 16 | # of this implementation | ||||||||||
| 17 | # | ||||||||||
| 18 | |||||||||||
| 19 | package Authen::NTLM::MD4; | ||||||||||
| 20 | |||||||||||
| 21 | 4 | ▶ | use vars qw($VERSION @ISA @EXPORT); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 4 | require Exporter; | |||||||||
| 23 | |||||||||||
| 24 | 4 | $VERSION = "1.02"; | |||||||||
| 25 | 4 | @ISA = qw(Exporter); | |||||||||
| 26 | 4 | @EXPORT = qw(mdfour); | |||||||||
| 27 | |||||||||||
| 28 | 4 | my ($A, $B, $C, $D); | |||||||||
| 29 | 4 | my (@X, $M); | |||||||||
| 30 | |||||||||||
| 31 | sub mdfour | ||||||||||
| 32 | 4 | ▶ | { | ||||||||
|
Subroutine
mdfour: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||
| 33 | 4 | my ($in) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 4 | my ($i, $pos); | |||||||||
| 36 | 4 | my $len = length($in); | |||||||||
| 37 | 4 | my $b = $len * 8; | |||||||||
| 38 | 4 | $in .= "\0"x128; | |||||||||
| 39 | 4 | $A = 0x67452301; | |||||||||
| 40 | 4 | $B = 0xefcdab89; | |||||||||
| 41 | 4 | $C = 0x98badcfe; | |||||||||
| 42 | 4 | $D = 0x10325476; | |||||||||
| 43 | 4 | $pos = 0; | |||||||||
| 44 | 4 | ▶ | while ($len > 64) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | { | ||||||||||
| 46 | 0 | ©64(substr($in, $pos, 64)); | |||||||||
| 47 | 0 | &mdfour64; | |||||||||
| 48 | 0 | $pos += 64; | |||||||||
| 49 | 0 | $len -= 64; | |||||||||
| 50 | } | ||||||||||
| 51 | 4 | my $buf = substr($in, $pos, $len); | |||||||||
| 52 | 4 | $buf .= sprintf "%c", 0x80; | |||||||||
| 53 | 4 | ▶ | if ($len <= 55) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | { | ||||||||||
| 55 | 4 | $buf .= "\0"x(55-$len); | |||||||||
| 56 | 4 | $buf .= pack("V", $b); | |||||||||
| 57 | 4 | $buf .= "\0"x4; | |||||||||
| 58 | 4 | ©64($buf); | |||||||||
| 59 | 4 | &mdfour64; | |||||||||
| 60 | } | ||||||||||
| 61 | else | ||||||||||
| 62 | { | ||||||||||
| 63 | 0 | $buf .= "\0"x(120-$len); | |||||||||
| 64 | 0 | $buf .= pack("V", $b); | |||||||||
| 65 | 0 | $buf .= "\0"x4; | |||||||||
| 66 | 0 | ©64(substr($buf, 0, 64)); | |||||||||
| 67 | 0 | &mdfour64; | |||||||||
| 68 | 0 | ©64(substr($buf, 64, 64)); | |||||||||
| 69 | 0 | &mdfour64; | |||||||||
| 70 | } | ||||||||||
| 71 | 4 | my $out = pack("VVVV", $A, $B, $C, $D); | |||||||||
| 72 | 4 | return $out; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | sub F | ||||||||||
| 76 | 64 | ▶ | { | ||||||||
|
Subroutine
F: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 77 | 64 | my ($X, $Y, $Z) = @_; | |||||||||
| 78 | 64 | my $res = ($X&$Y) | ((~$X)&$Z); | |||||||||
| 79 | 64 | return $res; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | sub G | ||||||||||
| 83 | 64 | ▶ | { | ||||||||
|
Subroutine
G: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | 64 | my ($X, $Y, $Z) = @_; | |||||||||
| 85 | |||||||||||
| 86 | 64 | return ($X&$Y) | ($X&$Z) | ($Y&$Z); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | sub H | ||||||||||
| 90 | 64 | ▶ | { | ||||||||
|
Subroutine
H: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 91 | 64 | my ($X, $Y, $Z) = @_; | |||||||||
| 92 | |||||||||||
| 93 | 64 | return $X^$Y^$Z; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | sub lshift | ||||||||||
| 97 | 192 | ▶ | { | ||||||||
|
Subroutine
lshift: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 98 | 192 | my ($x, $s) = @_; | |||||||||
| 99 | |||||||||||
| 100 | 192 | $x &= 0xffffffff; | |||||||||
| 101 | 192 | return (($x<<$s)&0xffffffff) | ($x>>(32-$s)); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | sub ROUND1 | ||||||||||
| 105 | 64 | ▶ | { | ||||||||
|
Subroutine
ROUND1: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 106 | 64 | my ($a, $b, $c, $d, $k, $s) = @_; | |||||||||
| 107 | 64 | my $e = &add($a, &F($b, $c, $d), $X[$k]); | |||||||||
| 108 | 64 | return &lshift($e, $s); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | sub ROUND2 | ||||||||||
| 112 | 64 | ▶ | { | ||||||||
|
Subroutine
ROUND2: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 113 | 64 | my ($a, $b, $c, $d, $k, $s) = @_; | |||||||||
| 114 | |||||||||||
| 115 | 64 | my $e = &add($a, &G($b, $c, $d), $X[$k], 0x5a827999); | |||||||||
| 116 | 64 | return &lshift($e, $s); | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | sub ROUND3 | ||||||||||
| 120 | 64 | ▶ | { | ||||||||
|
Subroutine
ROUND3: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 121 | 64 | my ($a, $b, $c, $d, $k, $s) = @_; | |||||||||
| 122 | |||||||||||
| 123 | 64 | my $e = &add($a, &H($b, $c, $d), $X[$k], 0x6ed9eba1); | |||||||||
| 124 | 64 | return &lshift($e, $s); | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | sub mdfour64 | ||||||||||
| 128 | 4 | ▶ | { | ||||||||
|
Subroutine
mdfour64: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 129 | 4 | my ($i, $AA, $BB, $CC, $DD); | |||||||||
| 130 | 4 | @X = unpack("N16", $M); | |||||||||
| 131 | 4 | $AA = $A; | |||||||||
| 132 | 4 | $BB = $B; | |||||||||
| 133 | 4 | $CC = $C; | |||||||||
| 134 | 4 | $DD = $D; | |||||||||
| 135 | |||||||||||
| 136 | 4 | $A = &ROUND1($A,$B,$C,$D, 0, 3); $D = &ROUND1($D,$A,$B,$C, 1, 7); | |||||||||
| 137 | 4 | $C = &ROUND1($C,$D,$A,$B, 2,11); $B = &ROUND1($B,$C,$D,$A, 3,19); | |||||||||
| 138 | 4 | $A = &ROUND1($A,$B,$C,$D, 4, 3); $D = &ROUND1($D,$A,$B,$C, 5, 7); | |||||||||
| 139 | 4 | $C = &ROUND1($C,$D,$A,$B, 6,11); $B = &ROUND1($B,$C,$D,$A, 7,19); | |||||||||
| 140 | 4 | $A = &ROUND1($A,$B,$C,$D, 8, 3); $D = &ROUND1($D,$A,$B,$C, 9, 7); | |||||||||
| 141 | 4 | $C = &ROUND1($C,$D,$A,$B,10,11); $B = &ROUND1($B,$C,$D,$A,11,19); | |||||||||
| 142 | 4 | $A = &ROUND1($A,$B,$C,$D,12, 3); $D = &ROUND1($D,$A,$B,$C,13, 7); | |||||||||
| 143 | 4 | $C = &ROUND1($C,$D,$A,$B,14,11); $B = &ROUND1($B,$C,$D,$A,15,19); | |||||||||
| 144 | |||||||||||
| 145 | 4 | $A = &ROUND2($A,$B,$C,$D, 0, 3); $D = &ROUND2($D,$A,$B,$C, 4, 5); | |||||||||
| 146 | 4 | $C = &ROUND2($C,$D,$A,$B, 8, 9); $B = &ROUND2($B,$C,$D,$A,12,13); | |||||||||
| 147 | 4 | $A = &ROUND2($A,$B,$C,$D, 1, 3); $D = &ROUND2($D,$A,$B,$C, 5, 5); | |||||||||
| 148 | 4 | $C = &ROUND2($C,$D,$A,$B, 9, 9); $B = &ROUND2($B,$C,$D,$A,13,13); | |||||||||
| 149 | 4 | $A = &ROUND2($A,$B,$C,$D, 2, 3); $D = &ROUND2($D,$A,$B,$C, 6, 5); | |||||||||
| 150 | 4 | $C = &ROUND2($C,$D,$A,$B,10, 9); $B = &ROUND2($B,$C,$D,$A,14,13); | |||||||||
| 151 | 4 | $A = &ROUND2($A,$B,$C,$D, 3, 3); $D = &ROUND2($D,$A,$B,$C, 7, 5); | |||||||||
| 152 | 4 | $C = &ROUND2($C,$D,$A,$B,11, 9); $B = &ROUND2($B,$C,$D,$A,15,13); | |||||||||
| 153 | |||||||||||
| 154 | 4 | $A = &ROUND3($A,$B,$C,$D, 0, 3); $D = &ROUND3($D,$A,$B,$C, 8, 9); | |||||||||
| 155 | 4 | $C = &ROUND3($C,$D,$A,$B, 4,11); $B = &ROUND3($B,$C,$D,$A,12,15); | |||||||||
| 156 | 4 | $A = &ROUND3($A,$B,$C,$D, 2, 3); $D = &ROUND3($D,$A,$B,$C,10, 9); | |||||||||
| 157 | 4 | $C = &ROUND3($C,$D,$A,$B, 6,11); $B = &ROUND3($B,$C,$D,$A,14,15); | |||||||||
| 158 | 4 | $A = &ROUND3($A,$B,$C,$D, 1, 3); $D = &ROUND3($D,$A,$B,$C, 9, 9); | |||||||||
| 159 | 4 | $C = &ROUND3($C,$D,$A,$B, 5,11); $B = &ROUND3($B,$C,$D,$A,13,15); | |||||||||
| 160 | 4 | $A = &ROUND3($A,$B,$C,$D, 3, 3); $D = &ROUND3($D,$A,$B,$C,11, 9); | |||||||||
| 161 | 4 | $C = &ROUND3($C,$D,$A,$B, 7,11); $B = &ROUND3($B,$C,$D,$A,15,15); | |||||||||
| 162 | |||||||||||
| 163 | 4 | $A = &add($A, $AA); $B = &add($B, $BB); | |||||||||
| 164 | 4 | $C = &add($C, $CC); $D = &add($D, $DD); | |||||||||
| 165 | 4 | $A &= 0xffffffff; $B &= 0xffffffff; | |||||||||
| 166 | 4 | $C &= 0xffffffff; $D &= 0xffffffff; | |||||||||
| 167 | 4 | map {$_ = 0} @X; | |||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | sub copy64 | ||||||||||
| 171 | 4 | ▶ | { | ||||||||
|
Subroutine
copy64: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 172 | 4 | my ($in) = @_; | |||||||||
| 173 | |||||||||||
| 174 | 4 | $M = pack("V16", unpack("N16", $in)); | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | # see note at top of this file about this function | ||||||||||
| 178 | sub add | ||||||||||
| 179 | 208 | ▶ | { | ||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 180 | 208 | my (@nums) = @_; | |||||||||
| 181 | 208 | my ($r_low, $r_high, $n_low, $l_high); | |||||||||
| 182 | 208 | my $num; | |||||||||
| 183 | 208 | $r_low = $r_high = 0; | |||||||||
| 184 | 208 | foreach $num (@nums) | |||||||||
| 185 | { | ||||||||||
| 186 | 736 | $n_low = $num & 0xffff; | |||||||||
| 187 | 736 | $n_high = ($num&0xffff0000)>>16; | |||||||||
| 188 | 736 | $r_low += $n_low; | |||||||||
| 189 | 736 | ▶ | ($r_low&0xf0000) && $r_high++; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 736 | $r_low &= 0xffff; | |||||||||
| 191 | 736 | $r_high += $n_high; | |||||||||
| 192 | 736 | $r_high &= 0xffff; | |||||||||
| 193 | } | ||||||||||
| 194 | 208 | return ($r_high<<16)|$r_low; | |||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | 4 | 1; | |||||||||