|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 2001 Markus Friedl. All rights reserved. ! 3: * ! 4: * Redistribution and use in source and binary forms, with or without ! 5: * modification, are permitted provided that the following conditions ! 6: * are met: ! 7: * 1. Redistributions of source code must retain the above copyright ! 8: * notice, this list of conditions and the following disclaimer. ! 9: * 2. Redistributions in binary form must reproduce the above copyright ! 10: * notice, this list of conditions and the following disclaimer in the ! 11: * documentation and/or other materials provided with the distribution. ! 12: * ! 13: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR ! 14: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! 15: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ! 16: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, ! 17: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT ! 18: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ! 19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ! 20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ! 21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF ! 22: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ! 23: */ ! 24: /* ! 25: * Preneel, Bosselaers, Dobbertin, "The Cryptographic Hash Function RIPEMD-160", ! 26: * RSA Laboratories, CryptoBytes, Volume 3, Number 2, Autumn 1997, ! 27: * ftp://ftp.rsasecurity.com/pub/cryptobytes/crypto3n2.pdf ! 28: */ ! 29: ! 30: #include "rmd160.h" ! 31: #include <memory.h> ! 32: ! 33: #define PUT_64BIT_LE(cp, value) do { \ ! 34: (cp)[7] = (unsigned char)((value) >> 56); \ ! 35: (cp)[6] = (unsigned char)((value) >> 48); \ ! 36: (cp)[5] = (unsigned char)((value) >> 40); \ ! 37: (cp)[4] = (unsigned char)((value) >> 32); \ ! 38: (cp)[3] = (unsigned char)((value) >> 24); \ ! 39: (cp)[2] = (unsigned char)((value) >> 16); \ ! 40: (cp)[1] = (unsigned char)((value) >> 8); \ ! 41: (cp)[0] = (unsigned char)(value); } while (0) ! 42: ! 43: #define PUT_32BIT_LE(cp, value) do { \ ! 44: (cp)[3] = (unsigned char)((value) >> 24); \ ! 45: (cp)[2] = (unsigned char)((value) >> 16); \ ! 46: (cp)[1] = (unsigned char)((value) >> 8); \ ! 47: (cp)[0] = (unsigned char)(value); } while (0) ! 48: ! 49: #define H0 0x67452301U ! 50: #define H1 0xEFCDAB89U ! 51: #define H2 0x98BADCFEU ! 52: #define H3 0x10325476U ! 53: #define H4 0xC3D2E1F0U ! 54: ! 55: #define K0 0x00000000U ! 56: #define K1 0x5A827999U ! 57: #define K2 0x6ED9EBA1U ! 58: #define K3 0x8F1BBCDCU ! 59: #define K4 0xA953FD4EU ! 60: ! 61: #define KK0 0x50A28BE6U ! 62: #define KK1 0x5C4DD124U ! 63: #define KK2 0x6D703EF3U ! 64: #define KK3 0x7A6D76E9U ! 65: #define KK4 0x00000000U ! 66: ! 67: /* rotate x left n bits. */ ! 68: #define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n)))) ! 69: ! 70: #define F0(x, y, z) ((x) ^ (y) ^ (z)) ! 71: #define F1(x, y, z) (((x) & (y)) | ((~x) & (z))) ! 72: #define F2(x, y, z) (((x) | (~y)) ^ (z)) ! 73: #define F3(x, y, z) (((x) & (z)) | ((y) & (~z))) ! 74: #define F4(x, y, z) ((x) ^ ((y) | (~z))) ! 75: ! 76: #define R(a, b, c, d, e, Fj, Kj, sj, rj) \ ! 77: do { \ ! 78: a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \ ! 79: c = ROL(10, c); \ ! 80: } while(0) ! 81: ! 82: #define X(i) x[i] ! 83: ! 84: static u_char PADDING[64] = { ! 85: 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ! 86: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ! 87: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ! 88: }; ! 89: ! 90: void ! 91: RMD160Init(RMD160_CTX *ctx) ! 92: { ! 93: ctx->count = 0; ! 94: ctx->state[0] = H0; ! 95: ctx->state[1] = H1; ! 96: ctx->state[2] = H2; ! 97: ctx->state[3] = H3; ! 98: ctx->state[4] = H4; ! 99: } ! 100: ! 101: void ! 102: RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len) ! 103: { ! 104: u_int32_t have, off, need; ! 105: ! 106: have = (unsigned long)((ctx->count/8) % 64); ! 107: need = 64 - have; ! 108: ctx->count += 8 * len; ! 109: off = 0; ! 110: ! 111: if (len >= need) { ! 112: if (have) { ! 113: memcpy(ctx->buffer + have, input, need); ! 114: RMD160Transform(ctx->state, ctx->buffer); ! 115: off = need; ! 116: have = 0; ! 117: } ! 118: /* now the buffer is empty */ ! 119: while (off + 64 <= len) { ! 120: RMD160Transform(ctx->state, input+off); ! 121: off += 64; ! 122: } ! 123: } ! 124: if (off < len) ! 125: memcpy(ctx->buffer + have, input+off, len-off); ! 126: } ! 127: ! 128: void ! 129: RMD160Final(u_char digest[20], RMD160_CTX *ctx) ! 130: { ! 131: int i; ! 132: u_char size[8]; ! 133: u_int32_t padlen; ! 134: ! 135: PUT_64BIT_LE(size, ctx->count); ! 136: ! 137: /* ! 138: * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes ! 139: * for the size ! 140: */ ! 141: padlen = (unsigned long)(64 - ((ctx->count/8) % 64)); ! 142: if (padlen < 1 + 8) ! 143: padlen += 64; ! 144: RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ ! 145: RMD160Update(ctx, size, 8); ! 146: ! 147: if (digest != 0) ! 148: for (i = 0; i < 5; i++) ! 149: PUT_32BIT_LE(digest + i*4, ctx->state[i]); ! 150: ! 151: memset(ctx, 0, sizeof (*ctx)); ! 152: } ! 153: ! 154: void ! 155: RMD160Transform(u_int32_t state[5], const u_char block[64]) ! 156: { ! 157: u_int32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16]; ! 158: ! 159: #if BYTE_ORDER == LITTLE_ENDIAN ! 160: memcpy(x, block, 64); ! 161: #else ! 162: int i; ! 163: ! 164: for (i = 0; i < 16; i++) ! 165: x[i] = (u_int32_t)( ! 166: (u_int32_t)(block[i*4 + 0]) | ! 167: (u_int32_t)(block[i*4 + 1]) << 8 | ! 168: (u_int32_t)(block[i*4 + 2]) << 16 | ! 169: (u_int32_t)(block[i*4 + 3]) << 24); ! 170: #endif ! 171: ! 172: a = state[0]; ! 173: b = state[1]; ! 174: c = state[2]; ! 175: d = state[3]; ! 176: e = state[4]; ! 177: ! 178: /* Round 1 */ ! 179: R(a, b, c, d, e, F0, K0, 11, 0); ! 180: R(e, a, b, c, d, F0, K0, 14, 1); ! 181: R(d, e, a, b, c, F0, K0, 15, 2); ! 182: R(c, d, e, a, b, F0, K0, 12, 3); ! 183: R(b, c, d, e, a, F0, K0, 5, 4); ! 184: R(a, b, c, d, e, F0, K0, 8, 5); ! 185: R(e, a, b, c, d, F0, K0, 7, 6); ! 186: R(d, e, a, b, c, F0, K0, 9, 7); ! 187: R(c, d, e, a, b, F0, K0, 11, 8); ! 188: R(b, c, d, e, a, F0, K0, 13, 9); ! 189: R(a, b, c, d, e, F0, K0, 14, 10); ! 190: R(e, a, b, c, d, F0, K0, 15, 11); ! 191: R(d, e, a, b, c, F0, K0, 6, 12); ! 192: R(c, d, e, a, b, F0, K0, 7, 13); ! 193: R(b, c, d, e, a, F0, K0, 9, 14); ! 194: R(a, b, c, d, e, F0, K0, 8, 15); /* #15 */ ! 195: /* Round 2 */ ! 196: R(e, a, b, c, d, F1, K1, 7, 7); ! 197: R(d, e, a, b, c, F1, K1, 6, 4); ! 198: R(c, d, e, a, b, F1, K1, 8, 13); ! 199: R(b, c, d, e, a, F1, K1, 13, 1); ! 200: R(a, b, c, d, e, F1, K1, 11, 10); ! 201: R(e, a, b, c, d, F1, K1, 9, 6); ! 202: R(d, e, a, b, c, F1, K1, 7, 15); ! 203: R(c, d, e, a, b, F1, K1, 15, 3); ! 204: R(b, c, d, e, a, F1, K1, 7, 12); ! 205: R(a, b, c, d, e, F1, K1, 12, 0); ! 206: R(e, a, b, c, d, F1, K1, 15, 9); ! 207: R(d, e, a, b, c, F1, K1, 9, 5); ! 208: R(c, d, e, a, b, F1, K1, 11, 2); ! 209: R(b, c, d, e, a, F1, K1, 7, 14); ! 210: R(a, b, c, d, e, F1, K1, 13, 11); ! 211: R(e, a, b, c, d, F1, K1, 12, 8); /* #31 */ ! 212: /* Round 3 */ ! 213: R(d, e, a, b, c, F2, K2, 11, 3); ! 214: R(c, d, e, a, b, F2, K2, 13, 10); ! 215: R(b, c, d, e, a, F2, K2, 6, 14); ! 216: R(a, b, c, d, e, F2, K2, 7, 4); ! 217: R(e, a, b, c, d, F2, K2, 14, 9); ! 218: R(d, e, a, b, c, F2, K2, 9, 15); ! 219: R(c, d, e, a, b, F2, K2, 13, 8); ! 220: R(b, c, d, e, a, F2, K2, 15, 1); ! 221: R(a, b, c, d, e, F2, K2, 14, 2); ! 222: R(e, a, b, c, d, F2, K2, 8, 7); ! 223: R(d, e, a, b, c, F2, K2, 13, 0); ! 224: R(c, d, e, a, b, F2, K2, 6, 6); ! 225: R(b, c, d, e, a, F2, K2, 5, 13); ! 226: R(a, b, c, d, e, F2, K2, 12, 11); ! 227: R(e, a, b, c, d, F2, K2, 7, 5); ! 228: R(d, e, a, b, c, F2, K2, 5, 12); /* #47 */ ! 229: /* Round 4 */ ! 230: R(c, d, e, a, b, F3, K3, 11, 1); ! 231: R(b, c, d, e, a, F3, K3, 12, 9); ! 232: R(a, b, c, d, e, F3, K3, 14, 11); ! 233: R(e, a, b, c, d, F3, K3, 15, 10); ! 234: R(d, e, a, b, c, F3, K3, 14, 0); ! 235: R(c, d, e, a, b, F3, K3, 15, 8); ! 236: R(b, c, d, e, a, F3, K3, 9, 12); ! 237: R(a, b, c, d, e, F3, K3, 8, 4); ! 238: R(e, a, b, c, d, F3, K3, 9, 13); ! 239: R(d, e, a, b, c, F3, K3, 14, 3); ! 240: R(c, d, e, a, b, F3, K3, 5, 7); ! 241: R(b, c, d, e, a, F3, K3, 6, 15); ! 242: R(a, b, c, d, e, F3, K3, 8, 14); ! 243: R(e, a, b, c, d, F3, K3, 6, 5); ! 244: R(d, e, a, b, c, F3, K3, 5, 6); ! 245: R(c, d, e, a, b, F3, K3, 12, 2); /* #63 */ ! 246: /* Round 5 */ ! 247: R(b, c, d, e, a, F4, K4, 9, 4); ! 248: R(a, b, c, d, e, F4, K4, 15, 0); ! 249: R(e, a, b, c, d, F4, K4, 5, 5); ! 250: R(d, e, a, b, c, F4, K4, 11, 9); ! 251: R(c, d, e, a, b, F4, K4, 6, 7); ! 252: R(b, c, d, e, a, F4, K4, 8, 12); ! 253: R(a, b, c, d, e, F4, K4, 13, 2); ! 254: R(e, a, b, c, d, F4, K4, 12, 10); ! 255: R(d, e, a, b, c, F4, K4, 5, 14); ! 256: R(c, d, e, a, b, F4, K4, 12, 1); ! 257: R(b, c, d, e, a, F4, K4, 13, 3); ! 258: R(a, b, c, d, e, F4, K4, 14, 8); ! 259: R(e, a, b, c, d, F4, K4, 11, 11); ! 260: R(d, e, a, b, c, F4, K4, 8, 6); ! 261: R(c, d, e, a, b, F4, K4, 5, 15); ! 262: R(b, c, d, e, a, F4, K4, 6, 13); /* #79 */ ! 263: ! 264: aa = a ; bb = b; cc = c; dd = d; ee = e; ! 265: ! 266: a = state[0]; ! 267: b = state[1]; ! 268: c = state[2]; ! 269: d = state[3]; ! 270: e = state[4]; ! 271: ! 272: /* Parallel round 1 */ ! 273: R(a, b, c, d, e, F4, KK0, 8, 5); ! 274: R(e, a, b, c, d, F4, KK0, 9, 14); ! 275: R(d, e, a, b, c, F4, KK0, 9, 7); ! 276: R(c, d, e, a, b, F4, KK0, 11, 0); ! 277: R(b, c, d, e, a, F4, KK0, 13, 9); ! 278: R(a, b, c, d, e, F4, KK0, 15, 2); ! 279: R(e, a, b, c, d, F4, KK0, 15, 11); ! 280: R(d, e, a, b, c, F4, KK0, 5, 4); ! 281: R(c, d, e, a, b, F4, KK0, 7, 13); ! 282: R(b, c, d, e, a, F4, KK0, 7, 6); ! 283: R(a, b, c, d, e, F4, KK0, 8, 15); ! 284: R(e, a, b, c, d, F4, KK0, 11, 8); ! 285: R(d, e, a, b, c, F4, KK0, 14, 1); ! 286: R(c, d, e, a, b, F4, KK0, 14, 10); ! 287: R(b, c, d, e, a, F4, KK0, 12, 3); ! 288: R(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */ ! 289: /* Parallel round 2 */ ! 290: R(e, a, b, c, d, F3, KK1, 9, 6); ! 291: R(d, e, a, b, c, F3, KK1, 13, 11); ! 292: R(c, d, e, a, b, F3, KK1, 15, 3); ! 293: R(b, c, d, e, a, F3, KK1, 7, 7); ! 294: R(a, b, c, d, e, F3, KK1, 12, 0); ! 295: R(e, a, b, c, d, F3, KK1, 8, 13); ! 296: R(d, e, a, b, c, F3, KK1, 9, 5); ! 297: R(c, d, e, a, b, F3, KK1, 11, 10); ! 298: R(b, c, d, e, a, F3, KK1, 7, 14); ! 299: R(a, b, c, d, e, F3, KK1, 7, 15); ! 300: R(e, a, b, c, d, F3, KK1, 12, 8); ! 301: R(d, e, a, b, c, F3, KK1, 7, 12); ! 302: R(c, d, e, a, b, F3, KK1, 6, 4); ! 303: R(b, c, d, e, a, F3, KK1, 15, 9); ! 304: R(a, b, c, d, e, F3, KK1, 13, 1); ! 305: R(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */ ! 306: /* Parallel round 3 */ ! 307: R(d, e, a, b, c, F2, KK2, 9, 15); ! 308: R(c, d, e, a, b, F2, KK2, 7, 5); ! 309: R(b, c, d, e, a, F2, KK2, 15, 1); ! 310: R(a, b, c, d, e, F2, KK2, 11, 3); ! 311: R(e, a, b, c, d, F2, KK2, 8, 7); ! 312: R(d, e, a, b, c, F2, KK2, 6, 14); ! 313: R(c, d, e, a, b, F2, KK2, 6, 6); ! 314: R(b, c, d, e, a, F2, KK2, 14, 9); ! 315: R(a, b, c, d, e, F2, KK2, 12, 11); ! 316: R(e, a, b, c, d, F2, KK2, 13, 8); ! 317: R(d, e, a, b, c, F2, KK2, 5, 12); ! 318: R(c, d, e, a, b, F2, KK2, 14, 2); ! 319: R(b, c, d, e, a, F2, KK2, 13, 10); ! 320: R(a, b, c, d, e, F2, KK2, 13, 0); ! 321: R(e, a, b, c, d, F2, KK2, 7, 4); ! 322: R(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */ ! 323: /* Parallel round 4 */ ! 324: R(c, d, e, a, b, F1, KK3, 15, 8); ! 325: R(b, c, d, e, a, F1, KK3, 5, 6); ! 326: R(a, b, c, d, e, F1, KK3, 8, 4); ! 327: R(e, a, b, c, d, F1, KK3, 11, 1); ! 328: R(d, e, a, b, c, F1, KK3, 14, 3); ! 329: R(c, d, e, a, b, F1, KK3, 14, 11); ! 330: R(b, c, d, e, a, F1, KK3, 6, 15); ! 331: R(a, b, c, d, e, F1, KK3, 14, 0); ! 332: R(e, a, b, c, d, F1, KK3, 6, 5); ! 333: R(d, e, a, b, c, F1, KK3, 9, 12); ! 334: R(c, d, e, a, b, F1, KK3, 12, 2); ! 335: R(b, c, d, e, a, F1, KK3, 9, 13); ! 336: R(a, b, c, d, e, F1, KK3, 12, 9); ! 337: R(e, a, b, c, d, F1, KK3, 5, 7); ! 338: R(d, e, a, b, c, F1, KK3, 15, 10); ! 339: R(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */ ! 340: /* Parallel round 5 */ ! 341: R(b, c, d, e, a, F0, KK4, 8, 12); ! 342: R(a, b, c, d, e, F0, KK4, 5, 15); ! 343: R(e, a, b, c, d, F0, KK4, 12, 10); ! 344: R(d, e, a, b, c, F0, KK4, 9, 4); ! 345: R(c, d, e, a, b, F0, KK4, 12, 1); ! 346: R(b, c, d, e, a, F0, KK4, 5, 5); ! 347: R(a, b, c, d, e, F0, KK4, 14, 8); ! 348: R(e, a, b, c, d, F0, KK4, 6, 7); ! 349: R(d, e, a, b, c, F0, KK4, 8, 6); ! 350: R(c, d, e, a, b, F0, KK4, 13, 2); ! 351: R(b, c, d, e, a, F0, KK4, 6, 13); ! 352: R(a, b, c, d, e, F0, KK4, 5, 14); ! 353: R(e, a, b, c, d, F0, KK4, 15, 0); ! 354: R(d, e, a, b, c, F0, KK4, 13, 3); ! 355: R(c, d, e, a, b, F0, KK4, 11, 9); ! 356: R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */ ! 357: ! 358: t = state[1] + cc + d; ! 359: state[1] = state[2] + dd + e; ! 360: state[2] = state[3] + ee + a; ! 361: state[3] = state[4] + aa + b; ! 362: state[4] = state[0] + bb + c; ! 363: state[0] = t; ! 364: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.