File Coverage

blib/lib/Authen/NTLM/DES.pm
Criterion Covered Total %
statement 101 102 99.0
branch 8 8 100.0
condition n/a
subroutine 9 9 100.0
pod 0 8 0.0
total 118 127 92.9


line stmt bran cond sub pod time code
1             #!/usr/local/bin/perl
2             #
3             # This is an implementation of part of the DES specification. According
4             # to the code this is ported from, this code does NOT enable 2-way
5             # encryption and is, hence, not a cypher and does not appear to come
6             # under any export restrictions on such.
7             #
8             package Authen::NTLM::DES;
9              
10 4     4   29 use vars qw($VERSION @ISA @EXPORT);
  4         9  
  4         23563  
11             require Exporter;
12              
13             $VERSION = "1.02";
14             @ISA = qw(Exporter);
15             @EXPORT = qw(E_P16 E_P24);
16              
17             my ($loop, $loop2);
18             $loop = 0;
19             $loop2 = 0;
20              
21             my $perm1 = [57, 49, 41, 33, 25, 17, 9,
22             1, 58, 50, 42, 34, 26, 18,
23             10, 2, 59, 51, 43, 35, 27,
24             19, 11, 3, 60, 52, 44, 36,
25             63, 55, 47, 39, 31, 23, 15,
26             7, 62, 54, 46, 38, 30, 22,
27             14, 6, 61, 53, 45, 37, 29,
28             21, 13, 5, 28, 20, 12, 4];
29             my $perm2 = [14, 17, 11, 24, 1, 5,
30             3, 28, 15, 6, 21, 10,
31             23, 19, 12, 4, 26, 8,
32             16, 7, 27, 20, 13, 2,
33             41, 52, 31, 37, 47, 55,
34             30, 40, 51, 45, 33, 48,
35             44, 49, 39, 56, 34, 53,
36             46, 42, 50, 36, 29, 32];
37             my $perm3 = [58, 50, 42, 34, 26, 18, 10, 2,
38             60, 52, 44, 36, 28, 20, 12, 4,
39             62, 54, 46, 38, 30, 22, 14, 6,
40             64, 56, 48, 40, 32, 24, 16, 8,
41             57, 49, 41, 33, 25, 17, 9, 1,
42             59, 51, 43, 35, 27, 19, 11, 3,
43             61, 53, 45, 37, 29, 21, 13, 5,
44             63, 55, 47, 39, 31, 23, 15, 7];
45             my $perm4 = [32, 1, 2, 3, 4, 5,
46             4, 5, 6, 7, 8, 9,
47             8, 9, 10, 11, 12, 13,
48             12, 13, 14, 15, 16, 17,
49             16, 17, 18, 19, 20, 21,
50             20, 21, 22, 23, 24, 25,
51             24, 25, 26, 27, 28, 29,
52             28, 29, 30, 31, 32, 1];
53             my $perm5 = [16, 7, 20, 21, 29, 12, 28, 17,
54             1, 15, 23, 26, 5, 18, 31, 10,
55             2, 8, 24, 14, 32, 27, 3, 9,
56             19, 13, 30, 6, 22, 11, 4, 25];
57             my $perm6 = [40, 8, 48, 16, 56, 24, 64, 32,
58             39, 7, 47, 15, 55, 23, 63, 31,
59             38, 6, 46, 14, 54, 22, 62, 30,
60             37, 5, 45, 13, 53, 21, 61, 29,
61             36, 4, 44, 12, 52, 20, 60, 28,
62             35, 3, 43, 11, 51, 19, 59, 27,
63             34, 2, 42, 10, 50, 18, 58, 26,
64             33, 1, 41, 9, 49, 17, 57, 25];
65             my $sc = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
66             my $sbox = [
67             [
68             [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
69             [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
70             [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
71             [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]
72             ],
73             [
74             [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
75             [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
76             [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
77             [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]
78             ],
79             [
80             [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
81             [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
82             [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
83             [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]
84             ],
85             [
86             [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
87             [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
88             [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
89             [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]
90             ],
91             [
92             [2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9],
93             [14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6],
94             [4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14],
95             [11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3]
96             ],
97             [
98             [12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11],
99             [10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8],
100             [9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6],
101             [4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13]
102             ],
103             [
104             [4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1],
105             [13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6],
106             [1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2],
107             [6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12]
108             ],
109             [
110             [13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7],
111             [1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2],
112             [7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8],
113             [2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11]
114             ]
115             ];
116              
117             sub E_P16
118             {
119 2     2 0 6 my ($p14) = @_;
120 2         17 my $sp8 = [0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25];
121              
122 2         6 my $p7 = substr($p14, 0, 7);
123 2         11 my $p16 = smbhash($sp8, $p7);
124 2         9 $p7 = substr($p14, 7, 7);
125 2         8 $p16 .= smbhash($sp8, $p7);
126 2         22 return $p16;
127             }
128              
129             sub E_P24
130             {
131 4     4 0 10 my ($p21, $c8_str) = @_;
132 4         24 my @c8 = map {ord($_)} split(//, $c8_str);
  32         55  
133 4         29 my $p24 = smbhash(\@c8, substr($p21, 0, 7));
134 4         42 $p24 .= smbhash(\@c8, substr($p21, 7, 7));
135 4         51 $p24 .= smbhash(\@c8, substr($p21, 14, 7));
136             }
137              
138             sub permute
139             {
140 816     816 0 1420 my ($out, $in, $p, $n) = @_;
141 816         884 my $i;
142              
143 816         1531 foreach $i (0..($n-1))
144             {
145 35712         114283 $out->[$i] = $in->[$p->[$i]-1];
146             }
147             }
148              
149             sub lshift
150             {
151 512     512 0 876 my ($d, $count, $n) = @_;
152 512         551 my (@out, $i);
153              
154 512         890 foreach $i (0..($n-1))
155             {
156 14336         33855 $out[$i] = $d->[($i+$count)%$n];
157             }
158 512         1671 foreach $i (0..($n-1))
159             {
160 14336         34731 $d->[$i] = $out[$i];
161             }
162             }
163              
164             sub xor
165             {
166 512     512 0 823 my ($out, $in1, $in2, $n) = @_;
167 512         590 my $i;
168              
169 512         885 foreach $i (0..($n-1))
170             {
171 20480         39293 $out->[$i] = $in1->[$i]^$in2->[$i];
172             }
173             }
174              
175             sub dohash
176             {
177 16     16 0 35 my ($out, $in, $key) = @_;
178 16         28 my ($i, $j, $k, @pk1, @c, @d, @cd,
179             @ki, @pd1, @l, @r, @rl);
180              
181 16         51 &permute(\@pk1, $key, $perm1, 56);
182              
183 16         45 foreach $i (0..27)
184             {
185 448         522 $c[$i] = $pk1[$i];
186 448         623 $d[$i] = $pk1[$i+28];
187             }
188 16         46 foreach $i (0..15)
189             {
190 256         285 my @array;
191 256         1241 &lshift(\@c, $sc->[$i], 28);
192 256         700 &lshift(\@d, $sc->[$i], 28);
193 256         1655 @cd = (@c, @d);
194 256         3350 &permute(\@array, \@cd, $perm2, 48);
195 256         730 $ki[$i] = \@array;
196             }
197 16         58 &permute(\@pd1, $in, $perm3, 64);
198              
199 16         55 foreach $j (0..31)
200             {
201 512         592 $l[$j] = $pd1[$j];
202 512         753 $r[$j] = $pd1[$j+32];
203             }
204              
205 16         53 foreach $i (0..15)
206             {
207 256         750 local (@er, @erk, @b, @cb, @pcb, @r2);
208 256         634 permute(\@er, \@r, $perm4, 48);
209 256         807 &xor(\@erk, \@er, $ki[$i], 48);
210 256         570 foreach $j (0..7)
211             {
212 2048         4336 foreach $k (0..5)
213             {
214 12288         42046 $b[$j][$k] = $erk[$j*6+$k];
215             }
216             }
217 256         455 foreach $j (0..7)
218             {
219 2048         2386 local ($m, $n);
220 2048         6201 $m = ($b[$j][0]<<1) | $b[$j][5];
221 2048         3934 $n = ($b[$j][1]<<3) | ($b[$j][2]<<2) | ($b[$j][3]<<1) | $b[$j][4];
222 2048         2819 foreach $k (0..3)
223             {
224 8192 100       38695 $b[$j][$k] = ($sbox->[$j][$m][$n] & (1<<(3-$k)))? 1: 0;
225             }
226             }
227 256         442 foreach $j (0..7)
228             {
229 2048         2748 foreach $k (0..3)
230             {
231 8192         20861 $cb[$j*4+$k] = $b[$j][$k];
232             }
233             }
234 256         737 &permute(\@pcb, \@cb, $perm5, 32);
235 256         787 &xor(\@r2, \@l, \@pcb, 32);
236 256         540 foreach $j (0..31)
237             {
238 8192         17398 $l[$j] = $r[$j];
239 8192         24355 $r[$j] = $r2[$j];
240             }
241             }
242 16         120 @rl = (@r, @l);
243 16         58 &permute($out, \@rl, $perm6, 64);
244             }
245              
246             sub str_to_key
247             {
248 16     16 0 32 my ($str) = @_;
249 16         30 my $i;
250             my @key;
251 0         0 my $out;
252 16         93 my @str = map {ord($_)} split(//, $str);
  112         203  
253 16         60 $key[0] = $str[0]>>1;
254 16         61 $key[1] = (($str[0]&0x01)<<6) | ($str[1]>>2);
255 16         46 $key[2] = (($str[1]&0x03)<<5) | ($str[2]>>3);
256 16         45 $key[3] = (($str[2]&0x07)<<4) | ($str[3]>>4);
257 16         49 $key[4] = (($str[3]&0x0f)<<3) | ($str[4]>>5);
258 16         46 $key[5] = (($str[4]&0x1f)<<2) | ($str[5]>>6);
259 16         43 $key[6] = (($str[5]&0x3f)<<1) | ($str[6]>>7);
260 16         29 $key[7] = $str[6]&0x7f;
261 16         48 foreach $i (0..7)
262             {
263 128         211 $key[$i] = 0xff&($key[$i]<<1);
264             }
265 16         67 return \@key;
266             }
267              
268             sub smbhash
269             {
270 16     16 0 72 my ($in, $key) = @_;
271              
272 16         60 my $key2 = &str_to_key($key);
273 16         35 my ($i, $div, $mod, @in, @outb, @inb, @keyb, @out);
274 16         34 foreach $i (0..63)
275             {
276 1024         1186 $div = int($i/8); $mod = $i%8;
  1024         1059  
277 1024 100       2112 $inb[$i] = ($in->[$div] & (1<<(7-($mod))))? 1: 0;
278 1024 100       3881 $keyb[$i] = ($key2->[$div] & (1<<(7-($mod))))? 1: 0;
279 1024         1403 $outb[$i] = 0;
280             }
281 16         125 &dohash(\@outb, \@inb, \@keyb);
282 16         98 foreach $i (0..7)
283             {
284 128         196 $out[$i] = 0;
285             }
286 16         61 foreach $i (0..63)
287             {
288 1024 100       2215 $out[int($i/8)] |= (1<<(7-($i%8))) if ($outb[$i]);
289             }
290 16         120 my $out = pack("C8", @out);
291 16         218 return $out;
292             }
293              
294             1;