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 #include <string.h> 30 #include <sys/types.h> 31 #include <sys/endian.h> 32 #include <rmd160.h> 33 34 #define PUT_64BIT_LE(cp, value) do { \ 35 (cp)[7] = (value) >> 56; \ 36 (cp)[6] = (value) >> 48; \ 37 (cp)[5] = (value) >> 40; \ 38 (cp)[4] = (value) >> 32; \ 39 (cp)[3] = (value) >> 24; \ 40 (cp)[2] = (value) >> 16; \ 41 (cp)[1] = (value) >> 8; \ 42 (cp)[0] = (value); } while (0) 43 44 #define PUT_32BIT_LE(cp, value) do { \ 45 (cp)[3] = (value) >> 24; \ 46 (cp)[2] = (value) >> 16; \ 47 (cp)[1] = (value) >> 8; \ 48 (cp)[0] = (value); } while (0) 49 50 #define H0 0x67452301U 51 #define H1 0xEFCDAB89U 52 #define H2 0x98BADCFEU 53 #define H3 0x10325476U 54 #define H4 0xC3D2E1F0U 55 56 #define K0 0x00000000U 57 #define K1 0x5A827999U 58 #define K2 0x6ED9EBA1U 59 #define K3 0x8F1BBCDCU 60 #define K4 0xA953FD4EU 61 62 #define KK0 0x50A28BE6U 63 #define KK1 0x5C4DD124U 64 #define KK2 0x6D703EF3U 65 #define KK3 0x7A6D76E9U 66 #define KK4 0x00000000U 67 68 /* rotate x left n bits. */ 69 #define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n)))) 70 71 #define F0(x, y, z) ((x) ^ (y) ^ (z)) 72 #define F1(x, y, z) (((x) & (y)) | ((~x) & (z))) 73 #define F2(x, y, z) (((x) | (~y)) ^ (z)) 74 #define F3(x, y, z) (((x) & (z)) | ((y) & (~z))) 75 #define F4(x, y, z) ((x) ^ ((y) | (~z))) 76 77 #define R(a, b, c, d, e, Fj, Kj, sj, rj) \ 78 do { \ 79 a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \ 80 c = ROL(10, c); \ 81 } while(0) 82 83 #define X(i) x[i] 84 85 static u_int8_t PADDING[RMD160_BLOCK_LENGTH] = { 86 0x80, 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, 0, 0, 0, 0, 88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 89 }; 90 91 void 92 RMD160Init(RMD160_CTX *ctx) 93 { 94 ctx->count = 0; 95 ctx->state[0] = H0; 96 ctx->state[1] = H1; 97 ctx->state[2] = H2; 98 ctx->state[3] = H3; 99 ctx->state[4] = H4; 100 } 101 102 void 103 RMD160Update(RMD160_CTX *ctx, const u_int8_t *input, size_t len) 104 { 105 size_t have, off, need; 106 107 have = (ctx->count / 8) % RMD160_BLOCK_LENGTH; 108 need = RMD160_BLOCK_LENGTH - have; 109 ctx->count += 8 * len; 110 off = 0; 111 112 if (len >= need) { 113 if (have) { 114 memcpy(ctx->buffer + have, input, need); 115 RMD160Transform(ctx->state, ctx->buffer); 116 off = need; 117 have = 0; 118 } 119 /* now the buffer is empty */ 120 while (off + RMD160_BLOCK_LENGTH <= len) { 121 RMD160Transform(ctx->state, input+off); 122 off += RMD160_BLOCK_LENGTH; 123 } 124 } 125 if (off < len) 126 memcpy(ctx->buffer + have, input+off, len-off); 127 } 128 129 void 130 RMD160Pad(RMD160_CTX *ctx) 131 { 132 u_int8_t size[8]; 133 size_t padlen; 134 135 PUT_64BIT_LE(size, ctx->count); 136 137 /* 138 * pad to RMD160_BLOCK_LENGTH byte blocks, at least one byte from 139 * PADDING plus 8 bytes for the size 140 */ 141 padlen = RMD160_BLOCK_LENGTH - ((ctx->count / 8) % RMD160_BLOCK_LENGTH); 142 if (padlen < 1 + 8) 143 padlen += RMD160_BLOCK_LENGTH; 144 RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ 145 RMD160Update(ctx, size, 8); 146 } 147 148 void 149 RMD160Final(u_int8_t digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx) 150 { 151 int i; 152 153 RMD160Pad(ctx); 154 if (digest != NULL) { 155 for (i = 0; i < 5; i++) 156 PUT_32BIT_LE(digest + i*4, ctx->state[i]); 157 memset(ctx, 0, sizeof (*ctx)); 158 } 159 } 160 161 void 162 RMD160Transform(u_int32_t state[5], const u_int8_t block[RMD160_BLOCK_LENGTH]) 163 { 164 u_int32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16]; 165 166 #if BYTE_ORDER == LITTLE_ENDIAN 167 memcpy(x, block, RMD160_BLOCK_LENGTH); 168 #else 169 int i; 170 171 for (i = 0; i < 16; i++) 172 x[i] = (u_int32_t)( 173 (u_int32_t)(block[i*4 + 0]) | 174 (u_int32_t)(block[i*4 + 1]) << 8 | 175 (u_int32_t)(block[i*4 + 2]) << 16 | 176 (u_int32_t)(block[i*4 + 3]) << 24); 177 #endif 178 179 a = state[0]; 180 b = state[1]; 181 c = state[2]; 182 d = state[3]; 183 e = state[4]; 184 185 /* Round 1 */ 186 R(a, b, c, d, e, F0, K0, 11, 0); 187 R(e, a, b, c, d, F0, K0, 14, 1); 188 R(d, e, a, b, c, F0, K0, 15, 2); 189 R(c, d, e, a, b, F0, K0, 12, 3); 190 R(b, c, d, e, a, F0, K0, 5, 4); 191 R(a, b, c, d, e, F0, K0, 8, 5); 192 R(e, a, b, c, d, F0, K0, 7, 6); 193 R(d, e, a, b, c, F0, K0, 9, 7); 194 R(c, d, e, a, b, F0, K0, 11, 8); 195 R(b, c, d, e, a, F0, K0, 13, 9); 196 R(a, b, c, d, e, F0, K0, 14, 10); 197 R(e, a, b, c, d, F0, K0, 15, 11); 198 R(d, e, a, b, c, F0, K0, 6, 12); 199 R(c, d, e, a, b, F0, K0, 7, 13); 200 R(b, c, d, e, a, F0, K0, 9, 14); 201 R(a, b, c, d, e, F0, K0, 8, 15); /* #15 */ 202 /* Round 2 */ 203 R(e, a, b, c, d, F1, K1, 7, 7); 204 R(d, e, a, b, c, F1, K1, 6, 4); 205 R(c, d, e, a, b, F1, K1, 8, 13); 206 R(b, c, d, e, a, F1, K1, 13, 1); 207 R(a, b, c, d, e, F1, K1, 11, 10); 208 R(e, a, b, c, d, F1, K1, 9, 6); 209 R(d, e, a, b, c, F1, K1, 7, 15); 210 R(c, d, e, a, b, F1, K1, 15, 3); 211 R(b, c, d, e, a, F1, K1, 7, 12); 212 R(a, b, c, d, e, F1, K1, 12, 0); 213 R(e, a, b, c, d, F1, K1, 15, 9); 214 R(d, e, a, b, c, F1, K1, 9, 5); 215 R(c, d, e, a, b, F1, K1, 11, 2); 216 R(b, c, d, e, a, F1, K1, 7, 14); 217 R(a, b, c, d, e, F1, K1, 13, 11); 218 R(e, a, b, c, d, F1, K1, 12, 8); /* #31 */ 219 /* Round 3 */ 220 R(d, e, a, b, c, F2, K2, 11, 3); 221 R(c, d, e, a, b, F2, K2, 13, 10); 222 R(b, c, d, e, a, F2, K2, 6, 14); 223 R(a, b, c, d, e, F2, K2, 7, 4); 224 R(e, a, b, c, d, F2, K2, 14, 9); 225 R(d, e, a, b, c, F2, K2, 9, 15); 226 R(c, d, e, a, b, F2, K2, 13, 8); 227 R(b, c, d, e, a, F2, K2, 15, 1); 228 R(a, b, c, d, e, F2, K2, 14, 2); 229 R(e, a, b, c, d, F2, K2, 8, 7); 230 R(d, e, a, b, c, F2, K2, 13, 0); 231 R(c, d, e, a, b, F2, K2, 6, 6); 232 R(b, c, d, e, a, F2, K2, 5, 13); 233 R(a, b, c, d, e, F2, K2, 12, 11); 234 R(e, a, b, c, d, F2, K2, 7, 5); 235 R(d, e, a, b, c, F2, K2, 5, 12); /* #47 */ 236 /* Round 4 */ 237 R(c, d, e, a, b, F3, K3, 11, 1); 238 R(b, c, d, e, a, F3, K3, 12, 9); 239 R(a, b, c, d, e, F3, K3, 14, 11); 240 R(e, a, b, c, d, F3, K3, 15, 10); 241 R(d, e, a, b, c, F3, K3, 14, 0); 242 R(c, d, e, a, b, F3, K3, 15, 8); 243 R(b, c, d, e, a, F3, K3, 9, 12); 244 R(a, b, c, d, e, F3, K3, 8, 4); 245 R(e, a, b, c, d, F3, K3, 9, 13); 246 R(d, e, a, b, c, F3, K3, 14, 3); 247 R(c, d, e, a, b, F3, K3, 5, 7); 248 R(b, c, d, e, a, F3, K3, 6, 15); 249 R(a, b, c, d, e, F3, K3, 8, 14); 250 R(e, a, b, c, d, F3, K3, 6, 5); 251 R(d, e, a, b, c, F3, K3, 5, 6); 252 R(c, d, e, a, b, F3, K3, 12, 2); /* #63 */ 253 /* Round 5 */ 254 R(b, c, d, e, a, F4, K4, 9, 4); 255 R(a, b, c, d, e, F4, K4, 15, 0); 256 R(e, a, b, c, d, F4, K4, 5, 5); 257 R(d, e, a, b, c, F4, K4, 11, 9); 258 R(c, d, e, a, b, F4, K4, 6, 7); 259 R(b, c, d, e, a, F4, K4, 8, 12); 260 R(a, b, c, d, e, F4, K4, 13, 2); 261 R(e, a, b, c, d, F4, K4, 12, 10); 262 R(d, e, a, b, c, F4, K4, 5, 14); 263 R(c, d, e, a, b, F4, K4, 12, 1); 264 R(b, c, d, e, a, F4, K4, 13, 3); 265 R(a, b, c, d, e, F4, K4, 14, 8); 266 R(e, a, b, c, d, F4, K4, 11, 11); 267 R(d, e, a, b, c, F4, K4, 8, 6); 268 R(c, d, e, a, b, F4, K4, 5, 15); 269 R(b, c, d, e, a, F4, K4, 6, 13); /* #79 */ 270 271 aa = a ; bb = b; cc = c; dd = d; ee = e; 272 273 a = state[0]; 274 b = state[1]; 275 c = state[2]; 276 d = state[3]; 277 e = state[4]; 278 279 /* Parallel round 1 */ 280 R(a, b, c, d, e, F4, KK0, 8, 5); 281 R(e, a, b, c, d, F4, KK0, 9, 14); 282 R(d, e, a, b, c, F4, KK0, 9, 7); 283 R(c, d, e, a, b, F4, KK0, 11, 0); 284 R(b, c, d, e, a, F4, KK0, 13, 9); 285 R(a, b, c, d, e, F4, KK0, 15, 2); 286 R(e, a, b, c, d, F4, KK0, 15, 11); 287 R(d, e, a, b, c, F4, KK0, 5, 4); 288 R(c, d, e, a, b, F4, KK0, 7, 13); 289 R(b, c, d, e, a, F4, KK0, 7, 6); 290 R(a, b, c, d, e, F4, KK0, 8, 15); 291 R(e, a, b, c, d, F4, KK0, 11, 8); 292 R(d, e, a, b, c, F4, KK0, 14, 1); 293 R(c, d, e, a, b, F4, KK0, 14, 10); 294 R(b, c, d, e, a, F4, KK0, 12, 3); 295 R(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */ 296 /* Parallel round 2 */ 297 R(e, a, b, c, d, F3, KK1, 9, 6); 298 R(d, e, a, b, c, F3, KK1, 13, 11); 299 R(c, d, e, a, b, F3, KK1, 15, 3); 300 R(b, c, d, e, a, F3, KK1, 7, 7); 301 R(a, b, c, d, e, F3, KK1, 12, 0); 302 R(e, a, b, c, d, F3, KK1, 8, 13); 303 R(d, e, a, b, c, F3, KK1, 9, 5); 304 R(c, d, e, a, b, F3, KK1, 11, 10); 305 R(b, c, d, e, a, F3, KK1, 7, 14); 306 R(a, b, c, d, e, F3, KK1, 7, 15); 307 R(e, a, b, c, d, F3, KK1, 12, 8); 308 R(d, e, a, b, c, F3, KK1, 7, 12); 309 R(c, d, e, a, b, F3, KK1, 6, 4); 310 R(b, c, d, e, a, F3, KK1, 15, 9); 311 R(a, b, c, d, e, F3, KK1, 13, 1); 312 R(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */ 313 /* Parallel round 3 */ 314 R(d, e, a, b, c, F2, KK2, 9, 15); 315 R(c, d, e, a, b, F2, KK2, 7, 5); 316 R(b, c, d, e, a, F2, KK2, 15, 1); 317 R(a, b, c, d, e, F2, KK2, 11, 3); 318 R(e, a, b, c, d, F2, KK2, 8, 7); 319 R(d, e, a, b, c, F2, KK2, 6, 14); 320 R(c, d, e, a, b, F2, KK2, 6, 6); 321 R(b, c, d, e, a, F2, KK2, 14, 9); 322 R(a, b, c, d, e, F2, KK2, 12, 11); 323 R(e, a, b, c, d, F2, KK2, 13, 8); 324 R(d, e, a, b, c, F2, KK2, 5, 12); 325 R(c, d, e, a, b, F2, KK2, 14, 2); 326 R(b, c, d, e, a, F2, KK2, 13, 10); 327 R(a, b, c, d, e, F2, KK2, 13, 0); 328 R(e, a, b, c, d, F2, KK2, 7, 4); 329 R(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */ 330 /* Parallel round 4 */ 331 R(c, d, e, a, b, F1, KK3, 15, 8); 332 R(b, c, d, e, a, F1, KK3, 5, 6); 333 R(a, b, c, d, e, F1, KK3, 8, 4); 334 R(e, a, b, c, d, F1, KK3, 11, 1); 335 R(d, e, a, b, c, F1, KK3, 14, 3); 336 R(c, d, e, a, b, F1, KK3, 14, 11); 337 R(b, c, d, e, a, F1, KK3, 6, 15); 338 R(a, b, c, d, e, F1, KK3, 14, 0); 339 R(e, a, b, c, d, F1, KK3, 6, 5); 340 R(d, e, a, b, c, F1, KK3, 9, 12); 341 R(c, d, e, a, b, F1, KK3, 12, 2); 342 R(b, c, d, e, a, F1, KK3, 9, 13); 343 R(a, b, c, d, e, F1, KK3, 12, 9); 344 R(e, a, b, c, d, F1, KK3, 5, 7); 345 R(d, e, a, b, c, F1, KK3, 15, 10); 346 R(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */ 347 /* Parallel round 5 */ 348 R(b, c, d, e, a, F0, KK4, 8, 12); 349 R(a, b, c, d, e, F0, KK4, 5, 15); 350 R(e, a, b, c, d, F0, KK4, 12, 10); 351 R(d, e, a, b, c, F0, KK4, 9, 4); 352 R(c, d, e, a, b, F0, KK4, 12, 1); 353 R(b, c, d, e, a, F0, KK4, 5, 5); 354 R(a, b, c, d, e, F0, KK4, 14, 8); 355 R(e, a, b, c, d, F0, KK4, 6, 7); 356 R(d, e, a, b, c, F0, KK4, 8, 6); 357 R(c, d, e, a, b, F0, KK4, 13, 2); 358 R(b, c, d, e, a, F0, KK4, 6, 13); 359 R(a, b, c, d, e, F0, KK4, 5, 14); 360 R(e, a, b, c, d, F0, KK4, 15, 0); 361 R(d, e, a, b, c, F0, KK4, 13, 3); 362 R(c, d, e, a, b, F0, KK4, 11, 9); 363 R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */ 364 365 t = state[1] + cc + d; 366 state[1] = state[2] + dd + e; 367 state[2] = state[3] + ee + a; 368 state[3] = state[4] + aa + b; 369 state[4] = state[0] + bb + c; 370 state[0] = t; 371 } 372