1#!/usr/bin/env perl
2
3##
4## Author......: See docs/credits.txt
5## License.....: MIT
6##
7
8use strict;
9use warnings;
10
11use Crypt::Mode::ECB;
12use Digest::SHA qw (sha1);
13use Encode;
14
15sub module_constraints { [[0, 19], [32, 32], [-1, -1], [-1, -1], [-1, -1]] }
16
17sub module_generate_hash
18{
19  my $word   = shift;
20  my $salt   = shift;
21  my $param  = shift;
22  my $param2 = shift;
23
24  my $iter = 50000;
25
26  my $aes_key_size = 128; # or 256
27
28  if (defined ($param2))
29  {
30    $aes_key_size = $param2;
31  }
32
33  my $salt_bin = pack ("H*", $salt);
34
35  my $tmp = sha1 ($salt_bin . encode ("UTF-16LE", $word));
36
37  for (my $i = 0; $i < $iter; $i++)
38  {
39    my $num32 = pack ("L", $i);
40
41    $tmp = sha1 ($num32 . $tmp);
42  }
43
44  my $zero32 = pack ("L", 0x00);
45
46  my $derivation_array1 = pack ("C", 0x36) x 64;
47  my $derivation_array2 = pack ("C", 0x5C) x 64;
48
49  $tmp = sha1 ($tmp . $zero32);
50
51  my $tmp2 = sha1 ($derivation_array1 ^ $tmp);
52  my $tmp3 = sha1 ($derivation_array2 ^ $tmp);
53
54  my $key = substr ($tmp2 . $tmp3, 0, $aes_key_size / 8);
55
56  my $m = Crypt::Mode::ECB->new ('AES', 0);
57
58  my $encdata;
59
60  if (defined $param)
61  {
62    $encdata = $m->decrypt (pack ("H*", $param), $key);
63  }
64  else
65  {
66    $encdata = "A" x 16; ## can be anything
67  }
68
69  my $data1_buf = $encdata;
70  my $data2_buf = sha1 (substr ($data1_buf, 0, 16));
71
72  $data1_buf = substr ($data1_buf . ("\x00" x 16), 0, 16);
73  $data2_buf = substr ($data2_buf . ("\x00" x 16), 0, 32);
74
75  my $encrypted1 = unpack ("H*", $m->encrypt ($data1_buf, $key));
76  my $encrypted2 = unpack ("H*", $m->encrypt ($data2_buf, $key));
77
78  $encrypted1 = substr ($encrypted1, 0, 32);
79  $encrypted2 = substr ($encrypted2, 0, 40);
80
81  my $hash = sprintf ("\$office\$*%d*%d*%d*%d*%s*%s*%s", 2007, 20, $aes_key_size, 16, $salt, $encrypted1, $encrypted2);
82
83  return $hash;
84}
85
86sub module_verify_hash
87{
88  my $line = shift;
89
90  # Office 2007
91  my ($hash_in, $word) = split ":", $line;
92
93  return unless defined $hash_in;
94  return unless defined $word;
95
96  my @data = split /\*/, $hash_in;
97
98  return unless scalar @data == 8;
99
100  return unless (shift @data eq '$office$');
101  return unless (shift @data eq '2007');
102  return unless (shift @data eq '20');
103
104  my $aes_key_size = shift @data;
105
106  return unless (($aes_key_size eq '128') || ($aes_key_size eq '256'));
107  return unless (shift @data eq '16');
108
109  return unless (length $data[0] == 32);
110  return unless (length $data[1] == 32);
111  return unless (length $data[2] == 40);
112
113  my $salt   = shift @data;
114  my $param  = shift @data;
115  my $param2 = $aes_key_size;
116
117  return unless defined $salt;
118  return unless defined $param;
119  return unless defined $param2;
120
121  $word = pack_if_HEX_notation ($word);
122
123  my $new_hash = module_generate_hash ($word, $salt, $param, $param2);
124
125  return ($new_hash, $word);
126}
127
1281;
129