2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * AUTHOR: Antoon Bosselaers, ESAT-COSIC
29 * Copyright (c) Katholieke Universiteit Leuven
30 * 1996, All Rights Reserved
32 * Conditions for use of the RIPEMD-160 Software
34 * The RIPEMD-160 software is freely available for use under the terms and
35 * conditions described hereunder, which shall be deemed to be accepted by
36 * any user of the software and applicable on any use of the software:
38 * 1. K.U.Leuven Department of Electrical Engineering-ESAT/COSIC shall for
39 * all purposes be considered the owner of the RIPEMD-160 software and of
40 * all copyright, trade secret, patent or other intellectual property
42 * 2. The RIPEMD-160 software is provided on an "as is" basis without
43 * warranty of any sort, express or implied. K.U.Leuven makes no
44 * representation that the use of the software will not infringe any
45 * patent or proprietary right of third parties. User will indemnify
46 * K.U.Leuven and hold K.U.Leuven harmless from any claims or liabilities
47 * which may arise as a result of its use of the software. In no
48 * circumstances K.U.Leuven R&D will be held liable for any deficiency,
49 * fault or other mishappening with regard to the use or performance of
51 * 3. User agrees to give due credit to K.U.Leuven in scientific publications
52 * or communications in relation with the use of the RIPEMD-160 software
53 * as follows: RIPEMD-160 software written by Antoon Bosselaers,
54 * available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.
59 /* $ModDesc: Allows for RIPEMD-160 encrypted oper passwords */
61 /* macro definitions */
72 typedef unsigned char byte;
73 typedef unsigned int dword;
76 typedef uint32_t dword;
79 /* collect four bytes into one word: */
80 #define BYTES_TO_DWORD(strptr) \
81 (((dword) *((strptr)+3) << 24) | \
82 ((dword) *((strptr)+2) << 16) | \
83 ((dword) *((strptr)+1) << 8) | \
86 /* ROL(x, n) cyclically rotates x over n bits to the left */
87 /* x must be of an unsigned 32 bits type and 0 <= n < 32. */
88 #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
90 /* the five basic functions F(), G() and H() */
91 #define F(x, y, z) ((x) ^ (y) ^ (z))
92 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
93 #define H(x, y, z) (((x) | ~(y)) ^ (z))
94 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
95 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
97 /* the ten basic operations FF() through III() */
99 #define FF(a, b, c, d, e, x, s) {\
100 (a) += F((b), (c), (d)) + (x);\
101 (a) = ROL((a), (s)) + (e);\
105 #define GG(a, b, c, d, e, x, s) {\
106 (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
107 (a) = ROL((a), (s)) + (e);\
111 #define HH(a, b, c, d, e, x, s) {\
112 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
113 (a) = ROL((a), (s)) + (e);\
117 #define II(a, b, c, d, e, x, s) {\
118 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
119 (a) = ROL((a), (s)) + (e);\
123 #define JJ(a, b, c, d, e, x, s) {\
124 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
125 (a) = ROL((a), (s)) + (e);\
129 #define FFF(a, b, c, d, e, x, s) {\
130 (a) += F((b), (c), (d)) + (x);\
131 (a) = ROL((a), (s)) + (e);\
135 #define GGG(a, b, c, d, e, x, s) {\
136 (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
137 (a) = ROL((a), (s)) + (e);\
141 #define HHH(a, b, c, d, e, x, s) {\
142 (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
143 (a) = ROL((a), (s)) + (e);\
147 #define III(a, b, c, d, e, x, s) {\
148 (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
149 (a) = ROL((a), (s)) + (e);\
153 #define JJJ(a, b, c, d, e, x, s) {\
154 (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
155 (a) = ROL((a), (s)) + (e);\
160 class RIProv : public HashProvider
163 void MDinit(dword *MDbuf, unsigned int* key)
167 ServerInstance->Logs->Log("m_ripemd160.so", DEBUG, "initialize with custom mdbuf");
176 ServerInstance->Logs->Log("m_ripemd160.so", DEBUG, "initialize with default mdbuf");
177 MDbuf[0] = 0x67452301UL;
178 MDbuf[1] = 0xefcdab89UL;
179 MDbuf[2] = 0x98badcfeUL;
180 MDbuf[3] = 0x10325476UL;
181 MDbuf[4] = 0xc3d2e1f0UL;
187 void compress(dword *MDbuf, dword *X)
189 dword aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2],
190 dd = MDbuf[3], ee = MDbuf[4];
191 dword aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2],
192 ddd = MDbuf[3], eee = MDbuf[4];
195 FF(aa, bb, cc, dd, ee, X[ 0], 11);
196 FF(ee, aa, bb, cc, dd, X[ 1], 14);
197 FF(dd, ee, aa, bb, cc, X[ 2], 15);
198 FF(cc, dd, ee, aa, bb, X[ 3], 12);
199 FF(bb, cc, dd, ee, aa, X[ 4], 5);
200 FF(aa, bb, cc, dd, ee, X[ 5], 8);
201 FF(ee, aa, bb, cc, dd, X[ 6], 7);
202 FF(dd, ee, aa, bb, cc, X[ 7], 9);
203 FF(cc, dd, ee, aa, bb, X[ 8], 11);
204 FF(bb, cc, dd, ee, aa, X[ 9], 13);
205 FF(aa, bb, cc, dd, ee, X[10], 14);
206 FF(ee, aa, bb, cc, dd, X[11], 15);
207 FF(dd, ee, aa, bb, cc, X[12], 6);
208 FF(cc, dd, ee, aa, bb, X[13], 7);
209 FF(bb, cc, dd, ee, aa, X[14], 9);
210 FF(aa, bb, cc, dd, ee, X[15], 8);
213 GG(ee, aa, bb, cc, dd, X[ 7], 7);
214 GG(dd, ee, aa, bb, cc, X[ 4], 6);
215 GG(cc, dd, ee, aa, bb, X[13], 8);
216 GG(bb, cc, dd, ee, aa, X[ 1], 13);
217 GG(aa, bb, cc, dd, ee, X[10], 11);
218 GG(ee, aa, bb, cc, dd, X[ 6], 9);
219 GG(dd, ee, aa, bb, cc, X[15], 7);
220 GG(cc, dd, ee, aa, bb, X[ 3], 15);
221 GG(bb, cc, dd, ee, aa, X[12], 7);
222 GG(aa, bb, cc, dd, ee, X[ 0], 12);
223 GG(ee, aa, bb, cc, dd, X[ 9], 15);
224 GG(dd, ee, aa, bb, cc, X[ 5], 9);
225 GG(cc, dd, ee, aa, bb, X[ 2], 11);
226 GG(bb, cc, dd, ee, aa, X[14], 7);
227 GG(aa, bb, cc, dd, ee, X[11], 13);
228 GG(ee, aa, bb, cc, dd, X[ 8], 12);
231 HH(dd, ee, aa, bb, cc, X[ 3], 11);
232 HH(cc, dd, ee, aa, bb, X[10], 13);
233 HH(bb, cc, dd, ee, aa, X[14], 6);
234 HH(aa, bb, cc, dd, ee, X[ 4], 7);
235 HH(ee, aa, bb, cc, dd, X[ 9], 14);
236 HH(dd, ee, aa, bb, cc, X[15], 9);
237 HH(cc, dd, ee, aa, bb, X[ 8], 13);
238 HH(bb, cc, dd, ee, aa, X[ 1], 15);
239 HH(aa, bb, cc, dd, ee, X[ 2], 14);
240 HH(ee, aa, bb, cc, dd, X[ 7], 8);
241 HH(dd, ee, aa, bb, cc, X[ 0], 13);
242 HH(cc, dd, ee, aa, bb, X[ 6], 6);
243 HH(bb, cc, dd, ee, aa, X[13], 5);
244 HH(aa, bb, cc, dd, ee, X[11], 12);
245 HH(ee, aa, bb, cc, dd, X[ 5], 7);
246 HH(dd, ee, aa, bb, cc, X[12], 5);
249 II(cc, dd, ee, aa, bb, X[ 1], 11);
250 II(bb, cc, dd, ee, aa, X[ 9], 12);
251 II(aa, bb, cc, dd, ee, X[11], 14);
252 II(ee, aa, bb, cc, dd, X[10], 15);
253 II(dd, ee, aa, bb, cc, X[ 0], 14);
254 II(cc, dd, ee, aa, bb, X[ 8], 15);
255 II(bb, cc, dd, ee, aa, X[12], 9);
256 II(aa, bb, cc, dd, ee, X[ 4], 8);
257 II(ee, aa, bb, cc, dd, X[13], 9);
258 II(dd, ee, aa, bb, cc, X[ 3], 14);
259 II(cc, dd, ee, aa, bb, X[ 7], 5);
260 II(bb, cc, dd, ee, aa, X[15], 6);
261 II(aa, bb, cc, dd, ee, X[14], 8);
262 II(ee, aa, bb, cc, dd, X[ 5], 6);
263 II(dd, ee, aa, bb, cc, X[ 6], 5);
264 II(cc, dd, ee, aa, bb, X[ 2], 12);
267 JJ(bb, cc, dd, ee, aa, X[ 4], 9);
268 JJ(aa, bb, cc, dd, ee, X[ 0], 15);
269 JJ(ee, aa, bb, cc, dd, X[ 5], 5);
270 JJ(dd, ee, aa, bb, cc, X[ 9], 11);
271 JJ(cc, dd, ee, aa, bb, X[ 7], 6);
272 JJ(bb, cc, dd, ee, aa, X[12], 8);
273 JJ(aa, bb, cc, dd, ee, X[ 2], 13);
274 JJ(ee, aa, bb, cc, dd, X[10], 12);
275 JJ(dd, ee, aa, bb, cc, X[14], 5);
276 JJ(cc, dd, ee, aa, bb, X[ 1], 12);
277 JJ(bb, cc, dd, ee, aa, X[ 3], 13);
278 JJ(aa, bb, cc, dd, ee, X[ 8], 14);
279 JJ(ee, aa, bb, cc, dd, X[11], 11);
280 JJ(dd, ee, aa, bb, cc, X[ 6], 8);
281 JJ(cc, dd, ee, aa, bb, X[15], 5);
282 JJ(bb, cc, dd, ee, aa, X[13], 6);
284 /* parallel round 1 */
285 JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
286 JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
287 JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
288 JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
289 JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
290 JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
291 JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
292 JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
293 JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
294 JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
295 JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
296 JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
297 JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
298 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
299 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
300 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
302 /* parallel round 2 */
303 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
304 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
305 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
306 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
307 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
308 III(eee, aaa, bbb, ccc, ddd, X[13], 8);
309 III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
310 III(ccc, ddd, eee, aaa, bbb, X[10], 11);
311 III(bbb, ccc, ddd, eee, aaa, X[14], 7);
312 III(aaa, bbb, ccc, ddd, eee, X[15], 7);
313 III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
314 III(ddd, eee, aaa, bbb, ccc, X[12], 7);
315 III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
316 III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
317 III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
318 III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
320 /* parallel round 3 */
321 HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
322 HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
323 HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
324 HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
325 HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
326 HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
327 HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
328 HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
329 HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
330 HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
331 HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
332 HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
333 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
334 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
335 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
336 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
338 /* parallel round 4 */
339 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
340 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
341 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
342 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
343 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
344 GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
345 GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
346 GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
347 GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
348 GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
349 GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
350 GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
351 GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
352 GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
353 GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
354 GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
356 /* parallel round 5 */
357 FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
358 FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
359 FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
360 FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
361 FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
362 FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
363 FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
364 FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
365 FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
366 FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
367 FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
368 FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
369 FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
370 FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
371 FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
372 FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
374 /* combine results */
375 ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */
376 MDbuf[1] = MDbuf[2] + dd + eee;
377 MDbuf[2] = MDbuf[3] + ee + aaa;
378 MDbuf[3] = MDbuf[4] + aa + bbb;
379 MDbuf[4] = MDbuf[0] + bb + ccc;
385 void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen)
387 unsigned int i; /* counter */
388 dword X[16]; /* message words */
390 memset(X, 0, sizeof(X));
392 /* put bytes from strptr into X */
393 for (i=0; i<(lswlen&63); i++) {
394 /* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */
395 X[i>>2] ^= (dword) *strptr++ << (8 * (i&3));
398 /* append the bit m_n == 1 */
399 X[(lswlen>>2)&15] ^= (dword)1 << (8*(lswlen&3) + 7);
401 if ((lswlen & 63) > 55) {
402 /* length goes to next block */
404 memset(X, 0, sizeof(X));
407 /* append length in bits*/
409 X[15] = (lswlen >> 29) | (mswlen << 3);
415 byte *RMD(byte *message, dword length, unsigned int* key)
417 ServerInstance->Logs->Log("m_ripemd160", DEBUG, "RMD: '%s' length=%u", (const char*)message, length);
418 dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(E)) */
419 static byte hashcode[RMDsize/8]; /* for final hash-value */
420 dword X[16]; /* current 16-word chunk */
421 unsigned int i; /* counter */
422 dword nbytes; /* # of bytes not yet processed */
427 /* process message in 16-word chunks */
428 for (nbytes=length; nbytes > 63; nbytes-=64) {
429 for (i=0; i<16; i++) {
430 X[i] = BYTES_TO_DWORD(message);
434 } /* length mod 64 bytes left */
436 MDfinish(MDbuf, message, length, 0);
438 for (i=0; i<RMDsize/8; i+=4) {
439 hashcode[i] = MDbuf[i>>2]; /* implicit cast to byte */
440 hashcode[i+1] = (MDbuf[i>>2] >> 8); /* extracts the 8 least */
441 hashcode[i+2] = (MDbuf[i>>2] >> 16); /* significant bits. */
442 hashcode[i+3] = (MDbuf[i>>2] >> 24);
445 return (byte *)hashcode;
448 std::string sum(const std::string& data)
450 char* rv = (char*)RMD((byte*)data.data(), data.length(), NULL);
451 return std::string(rv, RMDsize / 8);
454 std::string sumIV(unsigned int* IV, const char* HexMap, const std::string &sdata)
459 RIProv(Module* m) : HashProvider(m, "hash/ripemd160", 20, 64) {}
462 class ModuleRIPEMD160 : public Module
466 ModuleRIPEMD160() : mr(this)
468 ServerInstance->Modules->AddService(mr);
473 return Version("Provides RIPEMD-160 hashing", VF_VENDOR);
478 MODULE_INIT(ModuleRIPEMD160)