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 */
62 #include "modules/hash.h"
67 typedef uint32_t dword;
69 /* collect four bytes into one word: */
70 #define BYTES_TO_DWORD(strptr) \
71 (((dword) *((strptr)+3) << 24) | \
72 ((dword) *((strptr)+2) << 16) | \
73 ((dword) *((strptr)+1) << 8) | \
76 /* ROL(x, n) cyclically rotates x over n bits to the left */
77 /* x must be of an unsigned 32 bits type and 0 <= n < 32. */
78 #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
80 /* the five basic functions F(), G() and H() */
81 #define F(x, y, z) ((x) ^ (y) ^ (z))
82 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
83 #define H(x, y, z) (((x) | ~(y)) ^ (z))
84 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
85 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
87 /* the ten basic operations FF() through III() */
89 #define FF(a, b, c, d, e, x, s) {\
90 (a) += F((b), (c), (d)) + (x);\
91 (a) = ROL((a), (s)) + (e);\
95 #define GG(a, b, c, d, e, x, s) {\
96 (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
97 (a) = ROL((a), (s)) + (e);\
101 #define HH(a, b, c, d, e, x, s) {\
102 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
103 (a) = ROL((a), (s)) + (e);\
107 #define II(a, b, c, d, e, x, s) {\
108 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
109 (a) = ROL((a), (s)) + (e);\
113 #define JJ(a, b, c, d, e, x, s) {\
114 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
115 (a) = ROL((a), (s)) + (e);\
119 #define FFF(a, b, c, d, e, x, s) {\
120 (a) += F((b), (c), (d)) + (x);\
121 (a) = ROL((a), (s)) + (e);\
125 #define GGG(a, b, c, d, e, x, s) {\
126 (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
127 (a) = ROL((a), (s)) + (e);\
131 #define HHH(a, b, c, d, e, x, s) {\
132 (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
133 (a) = ROL((a), (s)) + (e);\
137 #define III(a, b, c, d, e, x, s) {\
138 (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
139 (a) = ROL((a), (s)) + (e);\
143 #define JJJ(a, b, c, d, e, x, s) {\
144 (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
145 (a) = ROL((a), (s)) + (e);\
150 class RIProv : public HashProvider
153 void MDinit(dword *MDbuf, unsigned int* key)
157 ServerInstance->Logs->Log("m_ripemd160.so", LOG_DEBUG, "initialize with custom mdbuf");
166 ServerInstance->Logs->Log("m_ripemd160.so", LOG_DEBUG, "initialize with default mdbuf");
167 MDbuf[0] = 0x67452301UL;
168 MDbuf[1] = 0xefcdab89UL;
169 MDbuf[2] = 0x98badcfeUL;
170 MDbuf[3] = 0x10325476UL;
171 MDbuf[4] = 0xc3d2e1f0UL;
177 void compress(dword *MDbuf, dword *X)
179 dword aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2],
180 dd = MDbuf[3], ee = MDbuf[4];
181 dword aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2],
182 ddd = MDbuf[3], eee = MDbuf[4];
185 FF(aa, bb, cc, dd, ee, X[ 0], 11);
186 FF(ee, aa, bb, cc, dd, X[ 1], 14);
187 FF(dd, ee, aa, bb, cc, X[ 2], 15);
188 FF(cc, dd, ee, aa, bb, X[ 3], 12);
189 FF(bb, cc, dd, ee, aa, X[ 4], 5);
190 FF(aa, bb, cc, dd, ee, X[ 5], 8);
191 FF(ee, aa, bb, cc, dd, X[ 6], 7);
192 FF(dd, ee, aa, bb, cc, X[ 7], 9);
193 FF(cc, dd, ee, aa, bb, X[ 8], 11);
194 FF(bb, cc, dd, ee, aa, X[ 9], 13);
195 FF(aa, bb, cc, dd, ee, X[10], 14);
196 FF(ee, aa, bb, cc, dd, X[11], 15);
197 FF(dd, ee, aa, bb, cc, X[12], 6);
198 FF(cc, dd, ee, aa, bb, X[13], 7);
199 FF(bb, cc, dd, ee, aa, X[14], 9);
200 FF(aa, bb, cc, dd, ee, X[15], 8);
203 GG(ee, aa, bb, cc, dd, X[ 7], 7);
204 GG(dd, ee, aa, bb, cc, X[ 4], 6);
205 GG(cc, dd, ee, aa, bb, X[13], 8);
206 GG(bb, cc, dd, ee, aa, X[ 1], 13);
207 GG(aa, bb, cc, dd, ee, X[10], 11);
208 GG(ee, aa, bb, cc, dd, X[ 6], 9);
209 GG(dd, ee, aa, bb, cc, X[15], 7);
210 GG(cc, dd, ee, aa, bb, X[ 3], 15);
211 GG(bb, cc, dd, ee, aa, X[12], 7);
212 GG(aa, bb, cc, dd, ee, X[ 0], 12);
213 GG(ee, aa, bb, cc, dd, X[ 9], 15);
214 GG(dd, ee, aa, bb, cc, X[ 5], 9);
215 GG(cc, dd, ee, aa, bb, X[ 2], 11);
216 GG(bb, cc, dd, ee, aa, X[14], 7);
217 GG(aa, bb, cc, dd, ee, X[11], 13);
218 GG(ee, aa, bb, cc, dd, X[ 8], 12);
221 HH(dd, ee, aa, bb, cc, X[ 3], 11);
222 HH(cc, dd, ee, aa, bb, X[10], 13);
223 HH(bb, cc, dd, ee, aa, X[14], 6);
224 HH(aa, bb, cc, dd, ee, X[ 4], 7);
225 HH(ee, aa, bb, cc, dd, X[ 9], 14);
226 HH(dd, ee, aa, bb, cc, X[15], 9);
227 HH(cc, dd, ee, aa, bb, X[ 8], 13);
228 HH(bb, cc, dd, ee, aa, X[ 1], 15);
229 HH(aa, bb, cc, dd, ee, X[ 2], 14);
230 HH(ee, aa, bb, cc, dd, X[ 7], 8);
231 HH(dd, ee, aa, bb, cc, X[ 0], 13);
232 HH(cc, dd, ee, aa, bb, X[ 6], 6);
233 HH(bb, cc, dd, ee, aa, X[13], 5);
234 HH(aa, bb, cc, dd, ee, X[11], 12);
235 HH(ee, aa, bb, cc, dd, X[ 5], 7);
236 HH(dd, ee, aa, bb, cc, X[12], 5);
239 II(cc, dd, ee, aa, bb, X[ 1], 11);
240 II(bb, cc, dd, ee, aa, X[ 9], 12);
241 II(aa, bb, cc, dd, ee, X[11], 14);
242 II(ee, aa, bb, cc, dd, X[10], 15);
243 II(dd, ee, aa, bb, cc, X[ 0], 14);
244 II(cc, dd, ee, aa, bb, X[ 8], 15);
245 II(bb, cc, dd, ee, aa, X[12], 9);
246 II(aa, bb, cc, dd, ee, X[ 4], 8);
247 II(ee, aa, bb, cc, dd, X[13], 9);
248 II(dd, ee, aa, bb, cc, X[ 3], 14);
249 II(cc, dd, ee, aa, bb, X[ 7], 5);
250 II(bb, cc, dd, ee, aa, X[15], 6);
251 II(aa, bb, cc, dd, ee, X[14], 8);
252 II(ee, aa, bb, cc, dd, X[ 5], 6);
253 II(dd, ee, aa, bb, cc, X[ 6], 5);
254 II(cc, dd, ee, aa, bb, X[ 2], 12);
257 JJ(bb, cc, dd, ee, aa, X[ 4], 9);
258 JJ(aa, bb, cc, dd, ee, X[ 0], 15);
259 JJ(ee, aa, bb, cc, dd, X[ 5], 5);
260 JJ(dd, ee, aa, bb, cc, X[ 9], 11);
261 JJ(cc, dd, ee, aa, bb, X[ 7], 6);
262 JJ(bb, cc, dd, ee, aa, X[12], 8);
263 JJ(aa, bb, cc, dd, ee, X[ 2], 13);
264 JJ(ee, aa, bb, cc, dd, X[10], 12);
265 JJ(dd, ee, aa, bb, cc, X[14], 5);
266 JJ(cc, dd, ee, aa, bb, X[ 1], 12);
267 JJ(bb, cc, dd, ee, aa, X[ 3], 13);
268 JJ(aa, bb, cc, dd, ee, X[ 8], 14);
269 JJ(ee, aa, bb, cc, dd, X[11], 11);
270 JJ(dd, ee, aa, bb, cc, X[ 6], 8);
271 JJ(cc, dd, ee, aa, bb, X[15], 5);
272 JJ(bb, cc, dd, ee, aa, X[13], 6);
274 /* parallel round 1 */
275 JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
276 JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
277 JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
278 JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
279 JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
280 JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
281 JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
282 JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
283 JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
284 JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
285 JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
286 JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
287 JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
288 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
289 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
290 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
292 /* parallel round 2 */
293 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
294 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
295 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
296 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
297 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
298 III(eee, aaa, bbb, ccc, ddd, X[13], 8);
299 III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
300 III(ccc, ddd, eee, aaa, bbb, X[10], 11);
301 III(bbb, ccc, ddd, eee, aaa, X[14], 7);
302 III(aaa, bbb, ccc, ddd, eee, X[15], 7);
303 III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
304 III(ddd, eee, aaa, bbb, ccc, X[12], 7);
305 III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
306 III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
307 III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
308 III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
310 /* parallel round 3 */
311 HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
312 HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
313 HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
314 HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
315 HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
316 HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
317 HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
318 HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
319 HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
320 HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
321 HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
322 HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
323 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
324 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
325 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
326 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
328 /* parallel round 4 */
329 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
330 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
331 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
332 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
333 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
334 GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
335 GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
336 GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
337 GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
338 GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
339 GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
340 GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
341 GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
342 GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
343 GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
344 GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
346 /* parallel round 5 */
347 FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
348 FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
349 FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
350 FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
351 FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
352 FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
353 FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
354 FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
355 FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
356 FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
357 FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
358 FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
359 FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
360 FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
361 FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
362 FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
364 /* combine results */
365 ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */
366 MDbuf[1] = MDbuf[2] + dd + eee;
367 MDbuf[2] = MDbuf[3] + ee + aaa;
368 MDbuf[3] = MDbuf[4] + aa + bbb;
369 MDbuf[4] = MDbuf[0] + bb + ccc;
375 void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen)
377 unsigned int i; /* counter */
378 dword X[16]; /* message words */
380 memset(X, 0, sizeof(X));
382 /* put bytes from strptr into X */
383 for (i=0; i<(lswlen&63); i++) {
384 /* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */
385 X[i>>2] ^= (dword) *strptr++ << (8 * (i&3));
388 /* append the bit m_n == 1 */
389 X[(lswlen>>2)&15] ^= (dword)1 << (8*(lswlen&3) + 7);
391 if ((lswlen & 63) > 55) {
392 /* length goes to next block */
394 memset(X, 0, sizeof(X));
397 /* append length in bits*/
399 X[15] = (lswlen >> 29) | (mswlen << 3);
405 byte *RMD(byte *message, dword length, unsigned int* key)
407 ServerInstance->Logs->Log("m_ripemd160", LOG_DEBUG, "RMD: '%s' length=%u", (const char*)message, length);
408 dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(E)) */
409 static byte hashcode[RMDsize/8]; /* for final hash-value */
410 dword X[16]; /* current 16-word chunk */
411 unsigned int i; /* counter */
412 dword nbytes; /* # of bytes not yet processed */
417 /* process message in 16-word chunks */
418 for (nbytes=length; nbytes > 63; nbytes-=64) {
419 for (i=0; i<16; i++) {
420 X[i] = BYTES_TO_DWORD(message);
424 } /* length mod 64 bytes left */
426 MDfinish(MDbuf, message, length, 0);
428 for (i=0; i<RMDsize/8; i+=4) {
429 hashcode[i] = MDbuf[i>>2]; /* implicit cast to byte */
430 hashcode[i+1] = (MDbuf[i>>2] >> 8); /* extracts the 8 least */
431 hashcode[i+2] = (MDbuf[i>>2] >> 16); /* significant bits. */
432 hashcode[i+3] = (MDbuf[i>>2] >> 24);
435 return (byte *)hashcode;
438 std::string sum(const std::string& data)
440 char* rv = (char*)RMD((byte*)data.data(), data.length(), NULL);
441 return std::string(rv, RMDsize / 8);
444 RIProv(Module* m) : HashProvider(m, "hash/ripemd160", 20, 64) {}
447 class ModuleRIPEMD160 : public Module
451 ModuleRIPEMD160() : mr(this)
453 ServerInstance->Modules->AddService(mr);
456 Version GetVersion() CXX11_OVERRIDE
458 return Version("Provides RIPEMD-160 hashing", VF_VENDOR);
462 MODULE_INIT(ModuleRIPEMD160)