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