Mercurial > hg > rc1
comparison plugins/password/helpers/dovecot_hmacmd5.php @ 0:1e000243b222
vanilla 1.3.3 distro, I hope
author | Charlie Root |
---|---|
date | Thu, 04 Jan 2018 15:50:29 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1e000243b222 |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * | |
5 * dovecot_hmacmd5.php V1.01 | |
6 * | |
7 * Generates HMAC-MD5 'contexts' for Dovecot's password files. | |
8 * | |
9 * (C) 2008 Hajo Noerenberg | |
10 * | |
11 * http://www.noerenberg.de/hajo/pub/dovecot_hmacmd5.php.txt | |
12 * | |
13 * Most of the code has been shamelessly stolen from various sources: | |
14 * | |
15 * (C) Paul Johnston 1999 - 2000 / http://pajhome.org.uk/crypt/md5/ | |
16 * (C) William K. Cole 2008 / http://www.scconsult.com/bill/crampass.pl | |
17 * (C) Borfast 2002 / http://www.zend.com/code/codex.php?ozid=962&single=1 | |
18 * (C) Thomas Weber / http://pajhome.org.uk/crypt/md5/contrib/md5.java.txt | |
19 * | |
20 * | |
21 * This program is free software: you can redistribute it and/or modify | |
22 * it under the terms of the GNU General Public License version 3.0 as | |
23 * published by the Free Software Foundation. | |
24 * | |
25 * This program is distributed in the hope that it will be useful, | |
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 * GNU General Public License for more details. | |
29 * | |
30 * You should have received a copy of the GNU General Public License along | |
31 * with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. | |
32 * | |
33 */ | |
34 | |
35 /* Convert a 32-bit number to a hex string with ls-byte first | |
36 */ | |
37 | |
38 function rhex($n) { | |
39 $hex_chr = "0123456789abcdef"; $r = ''; | |
40 for($j = 0; $j <= 3; $j++) | |
41 $r .= $hex_chr[($n >> ($j * 8 + 4)) & 0x0F] . $hex_chr[($n >> ($j * 8)) & 0x0F]; | |
42 return $r; | |
43 } | |
44 | |
45 /* zeroFill() is needed because PHP doesn't have a zero-fill | |
46 * right shift operator like JavaScript's >>> | |
47 */ | |
48 | |
49 function zeroFill($a, $b) { | |
50 $z = hexdec(80000000); | |
51 if ($z & $a) { | |
52 $a >>= 1; | |
53 $a &= (~$z); | |
54 $a |= 0x40000000; | |
55 $a >>= ($b-1); | |
56 } else { | |
57 $a >>= $b; | |
58 } | |
59 return $a; | |
60 } | |
61 | |
62 /* Bitwise rotate a 32-bit number to the left | |
63 */ | |
64 | |
65 function bit_rol($num, $cnt) { | |
66 return ($num << $cnt) | (zeroFill($num, (32 - $cnt))); | |
67 } | |
68 | |
69 /* Add integers, wrapping at 2^32 | |
70 */ | |
71 | |
72 function safe_add($x, $y) { | |
73 return (($x&0x7FFFFFFF) + ($y&0x7FFFFFFF)) ^ ($x&0x80000000) ^ ($y&0x80000000); | |
74 } | |
75 | |
76 /* These functions implement the four basic operations the algorithm uses. | |
77 */ | |
78 | |
79 function md5_cmn($q, $a, $b, $x, $s, $t) { | |
80 return safe_add(bit_rol(safe_add(safe_add($a, $q), safe_add($x, $t)), $s), $b); | |
81 } | |
82 function md5_ff($a, $b, $c, $d, $x, $s, $t) { | |
83 return md5_cmn(($b & $c) | ((~$b) & $d), $a, $b, $x, $s, $t); | |
84 } | |
85 function md5_gg($a, $b, $c, $d, $x, $s, $t) { | |
86 return md5_cmn(($b & $d) | ($c & (~$d)), $a, $b, $x, $s, $t); | |
87 } | |
88 function md5_hh($a, $b, $c, $d, $x, $s, $t) { | |
89 return md5_cmn($b ^ $c ^ $d, $a, $b, $x, $s, $t); | |
90 } | |
91 function md5_ii($a, $b, $c, $d, $x, $s, $t) { | |
92 return md5_cmn($c ^ ($b | (~$d)), $a, $b, $x, $s, $t); | |
93 } | |
94 | |
95 /* Calculate the first round of the MD5 algorithm | |
96 */ | |
97 | |
98 function md5_oneround($s, $io) { | |
99 | |
100 $s = str_pad($s, 64, chr(0x00)); | |
101 | |
102 $x = array_fill(0, 16, 0); | |
103 | |
104 for($i = 0; $i < 64; $i++) | |
105 $x[$i >> 2] |= (($io ? 0x36 : 0x5c) ^ ord($s[$i])) << (($i % 4) * 8); | |
106 | |
107 $a = $olda = 1732584193; | |
108 $b = $oldb = -271733879; | |
109 $c = $oldc = -1732584194; | |
110 $d = $oldd = 271733878; | |
111 | |
112 $a = md5_ff($a, $b, $c, $d, $x[ 0], 7 , -680876936); | |
113 $d = md5_ff($d, $a, $b, $c, $x[ 1], 12, -389564586); | |
114 $c = md5_ff($c, $d, $a, $b, $x[ 2], 17, 606105819); | |
115 $b = md5_ff($b, $c, $d, $a, $x[ 3], 22, -1044525330); | |
116 $a = md5_ff($a, $b, $c, $d, $x[ 4], 7 , -176418897); | |
117 $d = md5_ff($d, $a, $b, $c, $x[ 5], 12, 1200080426); | |
118 $c = md5_ff($c, $d, $a, $b, $x[ 6], 17, -1473231341); | |
119 $b = md5_ff($b, $c, $d, $a, $x[ 7], 22, -45705983); | |
120 $a = md5_ff($a, $b, $c, $d, $x[ 8], 7 , 1770035416); | |
121 $d = md5_ff($d, $a, $b, $c, $x[ 9], 12, -1958414417); | |
122 $c = md5_ff($c, $d, $a, $b, $x[10], 17, -42063); | |
123 $b = md5_ff($b, $c, $d, $a, $x[11], 22, -1990404162); | |
124 $a = md5_ff($a, $b, $c, $d, $x[12], 7 , 1804603682); | |
125 $d = md5_ff($d, $a, $b, $c, $x[13], 12, -40341101); | |
126 $c = md5_ff($c, $d, $a, $b, $x[14], 17, -1502002290); | |
127 $b = md5_ff($b, $c, $d, $a, $x[15], 22, 1236535329); | |
128 | |
129 $a = md5_gg($a, $b, $c, $d, $x[ 1], 5 , -165796510); | |
130 $d = md5_gg($d, $a, $b, $c, $x[ 6], 9 , -1069501632); | |
131 $c = md5_gg($c, $d, $a, $b, $x[11], 14, 643717713); | |
132 $b = md5_gg($b, $c, $d, $a, $x[ 0], 20, -373897302); | |
133 $a = md5_gg($a, $b, $c, $d, $x[ 5], 5 , -701558691); | |
134 $d = md5_gg($d, $a, $b, $c, $x[10], 9 , 38016083); | |
135 $c = md5_gg($c, $d, $a, $b, $x[15], 14, -660478335); | |
136 $b = md5_gg($b, $c, $d, $a, $x[ 4], 20, -405537848); | |
137 $a = md5_gg($a, $b, $c, $d, $x[ 9], 5 , 568446438); | |
138 $d = md5_gg($d, $a, $b, $c, $x[14], 9 , -1019803690); | |
139 $c = md5_gg($c, $d, $a, $b, $x[ 3], 14, -187363961); | |
140 $b = md5_gg($b, $c, $d, $a, $x[ 8], 20, 1163531501); | |
141 $a = md5_gg($a, $b, $c, $d, $x[13], 5 , -1444681467); | |
142 $d = md5_gg($d, $a, $b, $c, $x[ 2], 9 , -51403784); | |
143 $c = md5_gg($c, $d, $a, $b, $x[ 7], 14, 1735328473); | |
144 $b = md5_gg($b, $c, $d, $a, $x[12], 20, -1926607734); | |
145 | |
146 $a = md5_hh($a, $b, $c, $d, $x[ 5], 4 , -378558); | |
147 $d = md5_hh($d, $a, $b, $c, $x[ 8], 11, -2022574463); | |
148 $c = md5_hh($c, $d, $a, $b, $x[11], 16, 1839030562); | |
149 $b = md5_hh($b, $c, $d, $a, $x[14], 23, -35309556); | |
150 $a = md5_hh($a, $b, $c, $d, $x[ 1], 4 , -1530992060); | |
151 $d = md5_hh($d, $a, $b, $c, $x[ 4], 11, 1272893353); | |
152 $c = md5_hh($c, $d, $a, $b, $x[ 7], 16, -155497632); | |
153 $b = md5_hh($b, $c, $d, $a, $x[10], 23, -1094730640); | |
154 $a = md5_hh($a, $b, $c, $d, $x[13], 4 , 681279174); | |
155 $d = md5_hh($d, $a, $b, $c, $x[ 0], 11, -358537222); | |
156 $c = md5_hh($c, $d, $a, $b, $x[ 3], 16, -722521979); | |
157 $b = md5_hh($b, $c, $d, $a, $x[ 6], 23, 76029189); | |
158 $a = md5_hh($a, $b, $c, $d, $x[ 9], 4 , -640364487); | |
159 $d = md5_hh($d, $a, $b, $c, $x[12], 11, -421815835); | |
160 $c = md5_hh($c, $d, $a, $b, $x[15], 16, 530742520); | |
161 $b = md5_hh($b, $c, $d, $a, $x[ 2], 23, -995338651); | |
162 | |
163 $a = md5_ii($a, $b, $c, $d, $x[ 0], 6 , -198630844); | |
164 $d = md5_ii($d, $a, $b, $c, $x[ 7], 10, 1126891415); | |
165 $c = md5_ii($c, $d, $a, $b, $x[14], 15, -1416354905); | |
166 $b = md5_ii($b, $c, $d, $a, $x[ 5], 21, -57434055); | |
167 $a = md5_ii($a, $b, $c, $d, $x[12], 6 , 1700485571); | |
168 $d = md5_ii($d, $a, $b, $c, $x[ 3], 10, -1894986606); | |
169 $c = md5_ii($c, $d, $a, $b, $x[10], 15, -1051523); | |
170 $b = md5_ii($b, $c, $d, $a, $x[ 1], 21, -2054922799); | |
171 $a = md5_ii($a, $b, $c, $d, $x[ 8], 6 , 1873313359); | |
172 $d = md5_ii($d, $a, $b, $c, $x[15], 10, -30611744); | |
173 $c = md5_ii($c, $d, $a, $b, $x[ 6], 15, -1560198380); | |
174 $b = md5_ii($b, $c, $d, $a, $x[13], 21, 1309151649); | |
175 $a = md5_ii($a, $b, $c, $d, $x[ 4], 6 , -145523070); | |
176 $d = md5_ii($d, $a, $b, $c, $x[11], 10, -1120210379); | |
177 $c = md5_ii($c, $d, $a, $b, $x[ 2], 15, 718787259); | |
178 $b = md5_ii($b, $c, $d, $a, $x[ 9], 21, -343485551); | |
179 | |
180 $a = safe_add($a, $olda); | |
181 $b = safe_add($b, $oldb); | |
182 $c = safe_add($c, $oldc); | |
183 $d = safe_add($d, $oldd); | |
184 | |
185 return rhex($a) . rhex($b) . rhex($c) . rhex($d); | |
186 } | |
187 | |
188 function dovecot_hmacmd5 ($s) { | |
189 if (strlen($s) > 64) $s=pack("H*", md5($s)); | |
190 return md5_oneround($s, 0) . md5_oneround($s, 1); | |
191 } |