OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / klips / test / east-espiv-01 / examineIV.pl
1 #!/usr/bin/perl
2
3 $inpacket=0;
4 $firstpacket=1;
5
6 sub extractlastcipherblock {
7   my(@bytes_last_two, @bytes_last_three, @bytes, $len, @iv);
8
9 #  print "LAST2: $lastPacketLine2";
10 #  print "LAST3: $lastPacketLine3";
11   @bytes_last_two   = split(/\s+/,$lastPacketLine2);
12   @bytes_last_three = split(/\s+/,$lastPacketLine3);
13   
14   # get rid of offset
15   shift @bytes_last_two;
16   shift @bytes_last_three;
17
18   # get rid of text at right
19   pop @bytes_last_two;
20   pop @bytes_last_three;
21
22   @bytes = (@bytes_last_three, @bytes_last_two);
23   
24   # now skip the last 12 bytes as the AUTH MAC.
25   # sizes halved because bytes are presented as 16-items
26   $len = $#bytes;
27 #  print "EXTRACT: ".($len-9)."-".($len-6)." of ",join('|',@bytes)."\n";
28   @cbcbytes = @bytes[($len-9)..($len-6)];
29   
30   $cbc = join('',@cbcbytes);
31   $myiv= $first4IV.$last4IV;
32   
33 #  print "MyIV: ".$myiv." LastCBC: $cbc\n";
34
35   if($myiv eq $cbc) {
36     print $packetHead." IV PREDICTED\n";
37   } else {
38     print $packetHead." IV PROBABLY RANDOM\n";
39   }
40 }
41
42 while(<>) {
43   $lastthree = $lasttwo;
44   $lasttwo   = $last;
45   $last      = $_;
46
47   if(/^\d*\.\d*\.\d*\.\d* \> \d*\.\d*\.\d*\.\d*\: ESP/) {
48     $packetnum++;
49
50     # save the lines of the previous packet
51     $lastPacketLine2 = $lasttwo;
52     $lastPacketLine3 = $lastthree;
53     $packetHead = $_;
54     chop($packetHead);
55
56 #    print "PACKET: $_";
57
58     next;
59   }
60
61   if(/^0x0000/) {
62     $inpacket=1;
63     next;
64   }
65   
66   if(/^0x0010/) {
67 #    print " FIRST: $_";
68     @bytes=split(/\s+/,$_);
69     $first4IV = $bytes[7].$bytes[8];
70     next;
71   }
72
73   if(/^0x0020/) { 
74 #    print "SECOND: $_";
75     @bytes=split(/\s+/,$_);
76     $last4IV = $bytes[1].$bytes[2];
77
78     if($packetnum > 1) {
79       &extractlastcipherblock;
80     }
81     next;
82   }
83 }