1<?php
2/*
3Copyright (c) 2003, Michael Bretterklieber <michael@bretterklieber.com>
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions
8are met:
9
101. Redistributions of source code must retain the above copyright
11   notice, this list of conditions and the following disclaimer.
122. Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in the
14   documentation and/or other materials provided with the distribution.
153. The names of the authors may not be used to endorse or promote products
16   derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29This code cannot simply be copied and put under the GNU Public License or
30any other GPL-like (LGPL, GPL2) License.
31
32    $Id$
33*/
34
35include_once 'des.php';
36
37function NtPasswordHash($plain)
38{
39    return pack('H*', hash('md4', str2unicode($plain)));
40}
41
42function str2unicode($str)
43{
44
45    for ($i=0;$i<strlen($str);$i++) {
46        $a = ord($str{$i}) << 8;
47        $uni .= sprintf("%X",$a);
48    }
49    return pack('H*', $uni);
50}
51
52function GenerateChallenge($size = 8)
53{
54    mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
55    for($i = 0; $i < $size; $i++) {
56        $chall .= pack('C', 1 + mt_rand() % 255);
57    }
58    return $chall;
59}
60
61function ChallengeResponse($challenge, $nthash)
62{
63    while (strlen($nthash) < 21)
64        $nthash .= "\0";
65
66    $resp1 = des_encrypt_ecb(substr($nthash, 0, 7), $challenge);
67    $resp2 = des_encrypt_ecb(substr($nthash, 7, 7), $challenge);
68    $resp3 = des_encrypt_ecb(substr($nthash, 14, 7), $challenge);
69
70    return $resp1 . $resp2 . $resp3;
71}
72
73// MS-CHAPv2
74
75function GeneratePeerChallenge()
76{
77    return GenerateChallenge(16);
78}
79
80function NtPasswordHashHash($hash)
81{
82    return pack('H*', hash('md4', $hash));
83}
84
85function ChallengeHash($challenge, $peerChallenge, $username)
86{
87    return substr(pack('H*', hash('sha1', $peerChallenge . $challenge . $username)), 0, 8);
88}
89
90function GenerateNTResponse($challenge, $peerChallenge, $username, $password)
91{
92    $challengeHash = ChallengeHash($challenge, $peerChallenge, $username);
93    $pwhash = NtPasswordHash($password);
94    return ChallengeResponse($challengeHash, $pwhash);
95}
96
97?>
98