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