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