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