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 /* macro definitions */
65 #include "modules/hash.h"
70 typedef unsigned char byte;
71 typedef unsigned int dword;
74 typedef uint32_t dword;
77 /* collect four bytes into one word: */
78 #define BYTES_TO_DWORD(strptr) \
79 (((dword) *((strptr)+3) << 24) | \
80 ((dword) *((strptr)+2) << 16) | \
81 ((dword) *((strptr)+1) << 8) | \
84 /* ROL(x, n) cyclically rotates x over n bits to the left */
85 /* x must be of an unsigned 32 bits type and 0 <= n < 32. */
86 #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
88 /* the five basic functions F(), G() and H() */
89 #define F(x, y, z) ((x) ^ (y) ^ (z))
90 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
91 #define H(x, y, z) (((x) | ~(y)) ^ (z))
92 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
93 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
95 /* the ten basic operations FF() through III() */
97 #define FF(a, b, c, d, e, x, s) {\
98 (a) += F((b), (c), (d)) + (x);\
99 (a) = ROL((a), (s)) + (e);\
103 #define GG(a, b, c, d, e, x, s) {\
104 (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
105 (a) = ROL((a), (s)) + (e);\
109 #define HH(a, b, c, d, e, x, s) {\
110 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
111 (a) = ROL((a), (s)) + (e);\
115 #define II(a, b, c, d, e, x, s) {\
116 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
117 (a) = ROL((a), (s)) + (e);\
121 #define JJ(a, b, c, d, e, x, s) {\
122 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
123 (a) = ROL((a), (s)) + (e);\
127 #define FFF(a, b, c, d, e, x, s) {\
128 (a) += F((b), (c), (d)) + (x);\
129 (a) = ROL((a), (s)) + (e);\
133 #define GGG(a, b, c, d, e, x, s) {\
134 (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
135 (a) = ROL((a), (s)) + (e);\
139 #define HHH(a, b, c, d, e, x, s) {\
140 (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
141 (a) = ROL((a), (s)) + (e);\
145 #define III(a, b, c, d, e, x, s) {\
146 (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
147 (a) = ROL((a), (s)) + (e);\
151 #define JJJ(a, b, c, d, e, x, s) {\
152 (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
153 (a) = ROL((a), (s)) + (e);\
158 class RIProv : public HashProvider
161 void MDinit(dword *MDbuf, unsigned int* key)
165 ServerInstance->Logs->Log("m_ripemd160.so", LOG_DEBUG, "initialize with custom mdbuf");
174 ServerInstance->Logs->Log("m_ripemd160.so", LOG_DEBUG, "initialize with default mdbuf");
175 MDbuf[0] = 0x67452301UL;
176 MDbuf[1] = 0xefcdab89UL;
177 MDbuf[2] = 0x98badcfeUL;
178 MDbuf[3] = 0x10325476UL;
179 MDbuf[4] = 0xc3d2e1f0UL;
185 void compress(dword *MDbuf, dword *X)
187 dword aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2],
188 dd = MDbuf[3], ee = MDbuf[4];
189 dword aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2],
190 ddd = MDbuf[3], eee = MDbuf[4];
193 FF(aa, bb, cc, dd, ee, X[ 0], 11);
194 FF(ee, aa, bb, cc, dd, X[ 1], 14);
195 FF(dd, ee, aa, bb, cc, X[ 2], 15);
196 FF(cc, dd, ee, aa, bb, X[ 3], 12);
197 FF(bb, cc, dd, ee, aa, X[ 4], 5);
198 FF(aa, bb, cc, dd, ee, X[ 5], 8);
199 FF(ee, aa, bb, cc, dd, X[ 6], 7);
200 FF(dd, ee, aa, bb, cc, X[ 7], 9);
201 FF(cc, dd, ee, aa, bb, X[ 8], 11);
202 FF(bb, cc, dd, ee, aa, X[ 9], 13);
203 FF(aa, bb, cc, dd, ee, X[10], 14);
204 FF(ee, aa, bb, cc, dd, X[11], 15);
205 FF(dd, ee, aa, bb, cc, X[12], 6);
206 FF(cc, dd, ee, aa, bb, X[13], 7);
207 FF(bb, cc, dd, ee, aa, X[14], 9);
208 FF(aa, bb, cc, dd, ee, X[15], 8);
211 GG(ee, aa, bb, cc, dd, X[ 7], 7);
212 GG(dd, ee, aa, bb, cc, X[ 4], 6);
213 GG(cc, dd, ee, aa, bb, X[13], 8);
214 GG(bb, cc, dd, ee, aa, X[ 1], 13);
215 GG(aa, bb, cc, dd, ee, X[10], 11);
216 GG(ee, aa, bb, cc, dd, X[ 6], 9);
217 GG(dd, ee, aa, bb, cc, X[15], 7);
218 GG(cc, dd, ee, aa, bb, X[ 3], 15);
219 GG(bb, cc, dd, ee, aa, X[12], 7);
220 GG(aa, bb, cc, dd, ee, X[ 0], 12);
221 GG(ee, aa, bb, cc, dd, X[ 9], 15);
222 GG(dd, ee, aa, bb, cc, X[ 5], 9);
223 GG(cc, dd, ee, aa, bb, X[ 2], 11);
224 GG(bb, cc, dd, ee, aa, X[14], 7);
225 GG(aa, bb, cc, dd, ee, X[11], 13);
226 GG(ee, aa, bb, cc, dd, X[ 8], 12);
229 HH(dd, ee, aa, bb, cc, X[ 3], 11);
230 HH(cc, dd, ee, aa, bb, X[10], 13);
231 HH(bb, cc, dd, ee, aa, X[14], 6);
232 HH(aa, bb, cc, dd, ee, X[ 4], 7);
233 HH(ee, aa, bb, cc, dd, X[ 9], 14);
234 HH(dd, ee, aa, bb, cc, X[15], 9);
235 HH(cc, dd, ee, aa, bb, X[ 8], 13);
236 HH(bb, cc, dd, ee, aa, X[ 1], 15);
237 HH(aa, bb, cc, dd, ee, X[ 2], 14);
238 HH(ee, aa, bb, cc, dd, X[ 7], 8);
239 HH(dd, ee, aa, bb, cc, X[ 0], 13);
240 HH(cc, dd, ee, aa, bb, X[ 6], 6);
241 HH(bb, cc, dd, ee, aa, X[13], 5);
242 HH(aa, bb, cc, dd, ee, X[11], 12);
243 HH(ee, aa, bb, cc, dd, X[ 5], 7);
244 HH(dd, ee, aa, bb, cc, X[12], 5);
247 II(cc, dd, ee, aa, bb, X[ 1], 11);
248 II(bb, cc, dd, ee, aa, X[ 9], 12);
249 II(aa, bb, cc, dd, ee, X[11], 14);
250 II(ee, aa, bb, cc, dd, X[10], 15);
251 II(dd, ee, aa, bb, cc, X[ 0], 14);
252 II(cc, dd, ee, aa, bb, X[ 8], 15);
253 II(bb, cc, dd, ee, aa, X[12], 9);
254 II(aa, bb, cc, dd, ee, X[ 4], 8);
255 II(ee, aa, bb, cc, dd, X[13], 9);
256 II(dd, ee, aa, bb, cc, X[ 3], 14);
257 II(cc, dd, ee, aa, bb, X[ 7], 5);
258 II(bb, cc, dd, ee, aa, X[15], 6);
259 II(aa, bb, cc, dd, ee, X[14], 8);
260 II(ee, aa, bb, cc, dd, X[ 5], 6);
261 II(dd, ee, aa, bb, cc, X[ 6], 5);
262 II(cc, dd, ee, aa, bb, X[ 2], 12);
265 JJ(bb, cc, dd, ee, aa, X[ 4], 9);
266 JJ(aa, bb, cc, dd, ee, X[ 0], 15);
267 JJ(ee, aa, bb, cc, dd, X[ 5], 5);
268 JJ(dd, ee, aa, bb, cc, X[ 9], 11);
269 JJ(cc, dd, ee, aa, bb, X[ 7], 6);
270 JJ(bb, cc, dd, ee, aa, X[12], 8);
271 JJ(aa, bb, cc, dd, ee, X[ 2], 13);
272 JJ(ee, aa, bb, cc, dd, X[10], 12);
273 JJ(dd, ee, aa, bb, cc, X[14], 5);
274 JJ(cc, dd, ee, aa, bb, X[ 1], 12);
275 JJ(bb, cc, dd, ee, aa, X[ 3], 13);
276 JJ(aa, bb, cc, dd, ee, X[ 8], 14);
277 JJ(ee, aa, bb, cc, dd, X[11], 11);
278 JJ(dd, ee, aa, bb, cc, X[ 6], 8);
279 JJ(cc, dd, ee, aa, bb, X[15], 5);
280 JJ(bb, cc, dd, ee, aa, X[13], 6);
282 /* parallel round 1 */
283 JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
284 JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
285 JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
286 JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
287 JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
288 JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
289 JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
290 JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
291 JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
292 JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
293 JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
294 JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
295 JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
296 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
297 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
298 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
300 /* parallel round 2 */
301 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
302 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
303 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
304 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
305 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
306 III(eee, aaa, bbb, ccc, ddd, X[13], 8);
307 III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
308 III(ccc, ddd, eee, aaa, bbb, X[10], 11);
309 III(bbb, ccc, ddd, eee, aaa, X[14], 7);
310 III(aaa, bbb, ccc, ddd, eee, X[15], 7);
311 III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
312 III(ddd, eee, aaa, bbb, ccc, X[12], 7);
313 III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
314 III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
315 III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
316 III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
318 /* parallel round 3 */
319 HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
320 HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
321 HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
322 HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
323 HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
324 HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
325 HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
326 HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
327 HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
328 HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
329 HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
330 HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
331 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
332 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
333 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
334 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
336 /* parallel round 4 */
337 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
338 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
339 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
340 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
341 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
342 GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
343 GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
344 GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
345 GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
346 GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
347 GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
348 GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
349 GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
350 GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
351 GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
352 GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
354 /* parallel round 5 */
355 FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
356 FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
357 FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
358 FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
359 FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
360 FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
361 FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
362 FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
363 FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
364 FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
365 FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
366 FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
367 FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
368 FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
369 FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
370 FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
372 /* combine results */
373 ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */
374 MDbuf[1] = MDbuf[2] + dd + eee;
375 MDbuf[2] = MDbuf[3] + ee + aaa;
376 MDbuf[3] = MDbuf[4] + aa + bbb;
377 MDbuf[4] = MDbuf[0] + bb + ccc;
383 void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen)
385 unsigned int i; /* counter */
386 dword X[16]; /* message words */
388 memset(X, 0, sizeof(X));
390 /* put bytes from strptr into X */
391 for (i=0; i<(lswlen&63); i++) {
392 /* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */
393 X[i>>2] ^= (dword) *strptr++ << (8 * (i&3));
396 /* append the bit m_n == 1 */
397 X[(lswlen>>2)&15] ^= (dword)1 << (8*(lswlen&3) + 7);
399 if ((lswlen & 63) > 55) {
400 /* length goes to next block */
402 memset(X, 0, sizeof(X));
405 /* append length in bits*/
407 X[15] = (lswlen >> 29) | (mswlen << 3);
413 byte *RMD(byte *message, dword length, unsigned int* key)
415 ServerInstance->Logs->Log("m_ripemd160", LOG_DEBUG, "RMD: '%s' length=%u", (const char*)message, length);
416 dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(E)) */
417 static byte hashcode[RMDsize/8]; /* for final hash-value */
418 dword X[16]; /* current 16-word chunk */
419 unsigned int i; /* counter */
420 dword nbytes; /* # of bytes not yet processed */
425 /* process message in 16-word chunks */
426 for (nbytes=length; nbytes > 63; nbytes-=64) {
427 for (i=0; i<16; i++) {
428 X[i] = BYTES_TO_DWORD(message);
432 } /* length mod 64 bytes left */
434 MDfinish(MDbuf, message, length, 0);
436 for (i=0; i<RMDsize/8; i+=4) {
437 hashcode[i] = MDbuf[i>>2]; /* implicit cast to byte */
438 hashcode[i+1] = (MDbuf[i>>2] >> 8); /* extracts the 8 least */
439 hashcode[i+2] = (MDbuf[i>>2] >> 16); /* significant bits. */
440 hashcode[i+3] = (MDbuf[i>>2] >> 24);
443 return (byte *)hashcode;
446 std::string sum(const std::string& data)
448 char* rv = (char*)RMD((byte*)data.data(), data.length(), NULL);
449 return std::string(rv, RMDsize / 8);
452 RIProv(Module* m) : HashProvider(m, "hash/ripemd160", 20, 64) {}
455 class ModuleRIPEMD160 : public Module
459 ModuleRIPEMD160() : mr(this)
461 ServerInstance->Modules->AddService(mr);
464 Version GetVersion() CXX11_OVERRIDE
466 return Version("Provides RIPEMD-160 hashing", VF_VENDOR);
470 MODULE_INIT(ModuleRIPEMD160)