144301Swollman/* crypto/ripemd/rmd_dgst.c */
244301Swollman/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
344301Swollman * All rights reserved.
444301Swollman *
544301Swollman * This package is an SSL implementation written
644301Swollman * by Eric Young (eay@cryptsoft.com).
744301Swollman * The implementation was written so as to conform with Netscapes SSL.
844301Swollman *
944301Swollman * This library is free for commercial and non-commercial use as long as
1044301Swollman * the following conditions are aheared to.  The following conditions
1144301Swollman * apply to all code found in this distribution, be it the RC4, RSA,
1244301Swollman * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1344301Swollman * included with this distribution is covered by the same copyright terms
1444301Swollman * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1544301Swollman *
1644301Swollman * Copyright remains Eric Young's, and as such any Copyright notices in
1744301Swollman * the code are not to be removed.
1844301Swollman * If this package is used in a product, Eric Young should be given attribution
1944301Swollman * as the author of the parts of the library used.
2044301Swollman * This can be in the form of a textual message at program startup or
2144301Swollman * in documentation (online or textual) provided with the package.
2244301Swollman *
2344301Swollman * Redistribution and use in source and binary forms, with or without
2444301Swollman * modification, are permitted provided that the following conditions
2544301Swollman * are met:
2644301Swollman * 1. Redistributions of source code must retain the copyright
2744301Swollman *    notice, this list of conditions and the following disclaimer.
2844301Swollman * 2. Redistributions in binary form must reproduce the above copyright
2944301Swollman *    notice, this list of conditions and the following disclaimer in the
3044301Swollman *    documentation and/or other materials provided with the distribution.
3144301Swollman * 3. All advertising materials mentioning features or use of this software
3244301Swollman *    must display the following acknowledgement:
3344301Swollman *    "This product includes cryptographic software written by
3444301Swollman *     Eric Young (eay@cryptsoft.com)"
3544301Swollman *    The word 'cryptographic' can be left out if the rouines from the library
3644301Swollman *    being used are not cryptographic related :-).
3744301Swollman * 4. If you include any Windows specific code (or a derivative thereof) from
3844301Swollman *    the apps directory (application code) you must include an acknowledgement:
3944301Swollman *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4044301Swollman *
4144301Swollman * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4244301Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4344301Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4444301Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4544301Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4644301Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4744301Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4844301Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4944301Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5044301Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5144301Swollman * SUCH DAMAGE.
5244301Swollman *
5344301Swollman * The licence and distribution terms for any publically available version or
5444301Swollman * derivative of this code cannot be changed.  i.e. this code cannot simply be
5544301Swollman * copied and put under another distribution licence
5644301Swollman * [including the GNU Public Licence.]
5744301Swollman */
5844301Swollman
5984211Sdillon#include <sys/cdefs.h>
6084211Sdillon__FBSDID("$FreeBSD$");
6184211Sdillon
6244301Swollman#include <sys/types.h>
6344301Swollman
6444301Swollman#include <stdio.h>
6544310Smjacob#include <string.h>
6644301Swollman
6744301Swollman#if 0
6844301Swollman#include <machine/ansi.h>	/* we use the __ variants of bit-sized types */
6944301Swollman#endif
7044301Swollman#include <machine/endian.h>
7144301Swollman
7244301Swollman#include "rmd_locl.h"
7344301Swollman
7444301Swollman/*
7544301Swollman * The assembly-language code is not position-independent, so don't
7644301Swollman * try to use it in a shared library.
7744301Swollman */
7844301Swollman#ifdef PIC
7944301Swollman#undef RMD160_ASM
8044301Swollman#endif
8144301Swollman
8244301Swollmanchar *RMD160_version="RIPEMD160 part of SSLeay 0.9.0b 11-Oct-1998";
8344301Swollman
8444301Swollman#ifdef RMD160_ASM
8544301Swollmanvoid ripemd160_block_x86(RIPEMD160_CTX *c, const u_int32_t *p,int num);
8644301Swollman#define ripemd160_block ripemd160_block_x86
8744301Swollman#else
8844301Swollmanvoid ripemd160_block(RIPEMD160_CTX *c, const u_int32_t *p,int num);
8944301Swollman#endif
9044301Swollman
9144301Swollmanvoid RIPEMD160_Init(c)
9244301SwollmanRIPEMD160_CTX *c;
9344301Swollman	{
9444301Swollman	c->A=RIPEMD160_A;
9544301Swollman	c->B=RIPEMD160_B;
9644301Swollman	c->C=RIPEMD160_C;
9744301Swollman	c->D=RIPEMD160_D;
9844301Swollman	c->E=RIPEMD160_E;
9944301Swollman	c->Nl=0;
10044301Swollman	c->Nh=0;
10144301Swollman	c->num=0;
10244301Swollman	}
10344301Swollman
104154479Sphkvoid RIPEMD160_Update(c, in, len)
10544301SwollmanRIPEMD160_CTX *c;
106154479Sphkconst void *in;
10744301Swollmansize_t len;
10844301Swollman	{
10992913Sobrien	u_int32_t *p;
11044301Swollman	int sw,sc;
11144301Swollman	u_int32_t l;
112154479Sphk	const unsigned char *data = in;
11344301Swollman
11444301Swollman	if (len == 0) return;
11544301Swollman
11644301Swollman	l=(c->Nl+(len<<3))&0xffffffffL;
11744301Swollman	if (l < c->Nl) /* overflow */
11844301Swollman		c->Nh++;
11944301Swollman	c->Nh+=(len>>29);
12044301Swollman	c->Nl=l;
12144301Swollman
12244301Swollman	if (c->num != 0)
12344301Swollman		{
12444301Swollman		p=c->data;
12544301Swollman		sw=c->num>>2;
12644301Swollman		sc=c->num&0x03;
12744301Swollman
12844301Swollman		if ((c->num+len) >= RIPEMD160_CBLOCK)
12944301Swollman			{
13044301Swollman			l= p[sw];
13144301Swollman			p_c2l(data,l,sc);
13244301Swollman			p[sw++]=l;
13344301Swollman			for (; sw<RIPEMD160_LBLOCK; sw++)
13444301Swollman				{
13544301Swollman				c2l(data,l);
13644301Swollman				p[sw]=l;
13744301Swollman				}
13844301Swollman			len-=(RIPEMD160_CBLOCK-c->num);
13944301Swollman
14044301Swollman			ripemd160_block(c,p,64);
14144301Swollman			c->num=0;
14244301Swollman			/* drop through and do the rest */
14344301Swollman			}
14444301Swollman		else
14544301Swollman			{
14644301Swollman			int ew,ec;
14744301Swollman
14844301Swollman			c->num+=(int)len;
14944301Swollman			if ((sc+len) < 4) /* ugly, add char's to a word */
15044301Swollman				{
15144301Swollman				l= p[sw];
15244301Swollman				p_c2l_p(data,l,sc,len);
15344301Swollman				p[sw]=l;
15444301Swollman				}
15544301Swollman			else
15644301Swollman				{
15744301Swollman				ew=(c->num>>2);
15844301Swollman				ec=(c->num&0x03);
15944301Swollman				l= p[sw];
16044301Swollman				p_c2l(data,l,sc);
16144301Swollman				p[sw++]=l;
16244301Swollman				for (; sw < ew; sw++)
16344301Swollman					{ c2l(data,l); p[sw]=l; }
16444301Swollman				if (ec)
16544301Swollman					{
16644301Swollman					c2l_p(data,l,ec);
16744301Swollman					p[sw]=l;
16844301Swollman					}
16944301Swollman				}
17044301Swollman			return;
17144301Swollman			}
17244301Swollman		}
17344301Swollman	/* we now can process the input data in blocks of RIPEMD160_CBLOCK
17444301Swollman	 * chars and save the leftovers to c->data. */
17544301Swollman#if BYTE_ORDER == LITTLE_ENDIAN
17644301Swollman	if ((((unsigned long)data)%sizeof(u_int32_t)) == 0)
17744301Swollman		{
17844301Swollman		sw=(int)len/RIPEMD160_CBLOCK;
17944301Swollman		if (sw > 0)
18044301Swollman			{
18144301Swollman			sw*=RIPEMD160_CBLOCK;
18244301Swollman			ripemd160_block(c,(u_int32_t *)data,sw);
18344301Swollman			data+=sw;
18444301Swollman			len-=sw;
18544301Swollman			}
18644301Swollman		}
18744301Swollman#endif
18844301Swollman	p=c->data;
18944301Swollman	while (len >= RIPEMD160_CBLOCK)
19044301Swollman		{
19144301Swollman#if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == BIG_ENDIAN
19244301Swollman		if (p != (u_int32_t *)data)
19344301Swollman			memcpy(p,data,RIPEMD160_CBLOCK);
19444301Swollman		data+=RIPEMD160_CBLOCK;
19544301Swollman#if BYTE_ORDER == BIG_ENDIAN
19644301Swollman		for (sw=(RIPEMD160_LBLOCK/4); sw; sw--)
19744301Swollman			{
19844301Swollman			Endian_Reverse32(p[0]);
19944301Swollman			Endian_Reverse32(p[1]);
20044301Swollman			Endian_Reverse32(p[2]);
20144301Swollman			Endian_Reverse32(p[3]);
20244301Swollman			p+=4;
20344301Swollman			}
20444301Swollman#endif
20544301Swollman#else
20644301Swollman		for (sw=(RIPEMD160_LBLOCK/4); sw; sw--)
20744301Swollman			{
20844301Swollman			c2l(data,l); *(p++)=l;
20944301Swollman			c2l(data,l); *(p++)=l;
21044301Swollman			c2l(data,l); *(p++)=l;
21144301Swollman			c2l(data,l); *(p++)=l;
21244301Swollman			}
21344301Swollman#endif
21444301Swollman		p=c->data;
21544301Swollman		ripemd160_block(c,p,64);
21644301Swollman		len-=RIPEMD160_CBLOCK;
21744301Swollman		}
21844301Swollman	sc=(int)len;
21944301Swollman	c->num=sc;
22044301Swollman	if (sc)
22144301Swollman		{
22244301Swollman		sw=sc>>2;	/* words to copy */
22344301Swollman#if BYTE_ORDER == LITTLE_ENDIAN
22444301Swollman		p[sw]=0;
22544301Swollman		memcpy(p,data,sc);
22644301Swollman#else
22744301Swollman		sc&=0x03;
22844301Swollman		for ( ; sw; sw--)
22944301Swollman			{ c2l(data,l); *(p++)=l; }
23044301Swollman		c2l_p(data,l,sc);
23144301Swollman		*p=l;
23244301Swollman#endif
23344301Swollman		}
23444301Swollman	}
23544301Swollman
23644301Swollmanvoid RIPEMD160_Transform(c,b)
23744301SwollmanRIPEMD160_CTX *c;
23844301Swollmanunsigned char *b;
23944301Swollman	{
24044301Swollman	u_int32_t p[16];
24144301Swollman#if BYTE_ORDER != LITTLE_ENDIAN
24244301Swollman	u_int32_t *q;
24344301Swollman	int i;
24444301Swollman#endif
24544301Swollman
24644301Swollman#if BYTE_ORDER == BIG_ENDIAN || BYTE_ORDER == LITTLE_ENDIAN
24744301Swollman	memcpy(p,b,64);
24844301Swollman#if BYTE_ORDER == BIG_ENDIAN
24944301Swollman	q=p;
25044301Swollman	for (i=(RIPEMD160_LBLOCK/4); i; i--)
25144301Swollman		{
25244301Swollman		Endian_Reverse32(q[0]);
25344301Swollman		Endian_Reverse32(q[1]);
25444301Swollman		Endian_Reverse32(q[2]);
25544301Swollman		Endian_Reverse32(q[3]);
25644301Swollman		q+=4;
25744301Swollman		}
25844301Swollman#endif
25944301Swollman#else
26044301Swollman	q=p;
26144301Swollman	for (i=(RIPEMD160_LBLOCK/4); i; i--)
26244301Swollman		{
26344301Swollman		u_int32_t l;
26444301Swollman		c2l(b,l); *(q++)=l;
26544301Swollman		c2l(b,l); *(q++)=l;
26644301Swollman		c2l(b,l); *(q++)=l;
26744301Swollman		c2l(b,l); *(q++)=l;
26844301Swollman		}
26944301Swollman#endif
27044301Swollman	ripemd160_block(c,p,64);
27144301Swollman	}
27244301Swollman
27344301Swollman#ifndef RMD160_ASM
27444301Swollman
27544301Swollmanvoid ripemd160_block(ctx, X, num)
27644301SwollmanRIPEMD160_CTX *ctx;
27744301Swollmanconst u_int32_t *X;
27844301Swollmanint num;
27944301Swollman	{
28092913Sobrien	u_int32_t A,B,C,D,E;
28144301Swollman	u_int32_t a,b,c,d,e;
28244301Swollman
28344301Swollman	for (;;)
28444301Swollman		{
28544301Swollman		A=ctx->A; B=ctx->B; C=ctx->C; D=ctx->D; E=ctx->E;
28644301Swollman
28744301Swollman	RIP1(A,B,C,D,E,WL00,SL00);
28844301Swollman	RIP1(E,A,B,C,D,WL01,SL01);
28944301Swollman	RIP1(D,E,A,B,C,WL02,SL02);
29044301Swollman	RIP1(C,D,E,A,B,WL03,SL03);
29144301Swollman	RIP1(B,C,D,E,A,WL04,SL04);
29244301Swollman	RIP1(A,B,C,D,E,WL05,SL05);
29344301Swollman	RIP1(E,A,B,C,D,WL06,SL06);
29444301Swollman	RIP1(D,E,A,B,C,WL07,SL07);
29544301Swollman	RIP1(C,D,E,A,B,WL08,SL08);
29644301Swollman	RIP1(B,C,D,E,A,WL09,SL09);
29744301Swollman	RIP1(A,B,C,D,E,WL10,SL10);
29844301Swollman	RIP1(E,A,B,C,D,WL11,SL11);
29944301Swollman	RIP1(D,E,A,B,C,WL12,SL12);
30044301Swollman	RIP1(C,D,E,A,B,WL13,SL13);
30144301Swollman	RIP1(B,C,D,E,A,WL14,SL14);
30244301Swollman	RIP1(A,B,C,D,E,WL15,SL15);
30344301Swollman
30444301Swollman	RIP2(E,A,B,C,D,WL16,SL16,KL1);
30544301Swollman	RIP2(D,E,A,B,C,WL17,SL17,KL1);
30644301Swollman	RIP2(C,D,E,A,B,WL18,SL18,KL1);
30744301Swollman	RIP2(B,C,D,E,A,WL19,SL19,KL1);
30844301Swollman	RIP2(A,B,C,D,E,WL20,SL20,KL1);
30944301Swollman	RIP2(E,A,B,C,D,WL21,SL21,KL1);
31044301Swollman	RIP2(D,E,A,B,C,WL22,SL22,KL1);
31144301Swollman	RIP2(C,D,E,A,B,WL23,SL23,KL1);
31244301Swollman	RIP2(B,C,D,E,A,WL24,SL24,KL1);
31344301Swollman	RIP2(A,B,C,D,E,WL25,SL25,KL1);
31444301Swollman	RIP2(E,A,B,C,D,WL26,SL26,KL1);
31544301Swollman	RIP2(D,E,A,B,C,WL27,SL27,KL1);
31644301Swollman	RIP2(C,D,E,A,B,WL28,SL28,KL1);
31744301Swollman	RIP2(B,C,D,E,A,WL29,SL29,KL1);
31844301Swollman	RIP2(A,B,C,D,E,WL30,SL30,KL1);
31944301Swollman	RIP2(E,A,B,C,D,WL31,SL31,KL1);
32044301Swollman
32144301Swollman	RIP3(D,E,A,B,C,WL32,SL32,KL2);
32244301Swollman	RIP3(C,D,E,A,B,WL33,SL33,KL2);
32344301Swollman	RIP3(B,C,D,E,A,WL34,SL34,KL2);
32444301Swollman	RIP3(A,B,C,D,E,WL35,SL35,KL2);
32544301Swollman	RIP3(E,A,B,C,D,WL36,SL36,KL2);
32644301Swollman	RIP3(D,E,A,B,C,WL37,SL37,KL2);
32744301Swollman	RIP3(C,D,E,A,B,WL38,SL38,KL2);
32844301Swollman	RIP3(B,C,D,E,A,WL39,SL39,KL2);
32944301Swollman	RIP3(A,B,C,D,E,WL40,SL40,KL2);
33044301Swollman	RIP3(E,A,B,C,D,WL41,SL41,KL2);
33144301Swollman	RIP3(D,E,A,B,C,WL42,SL42,KL2);
33244301Swollman	RIP3(C,D,E,A,B,WL43,SL43,KL2);
33344301Swollman	RIP3(B,C,D,E,A,WL44,SL44,KL2);
33444301Swollman	RIP3(A,B,C,D,E,WL45,SL45,KL2);
33544301Swollman	RIP3(E,A,B,C,D,WL46,SL46,KL2);
33644301Swollman	RIP3(D,E,A,B,C,WL47,SL47,KL2);
33744301Swollman
33844301Swollman	RIP4(C,D,E,A,B,WL48,SL48,KL3);
33944301Swollman	RIP4(B,C,D,E,A,WL49,SL49,KL3);
34044301Swollman	RIP4(A,B,C,D,E,WL50,SL50,KL3);
34144301Swollman	RIP4(E,A,B,C,D,WL51,SL51,KL3);
34244301Swollman	RIP4(D,E,A,B,C,WL52,SL52,KL3);
34344301Swollman	RIP4(C,D,E,A,B,WL53,SL53,KL3);
34444301Swollman	RIP4(B,C,D,E,A,WL54,SL54,KL3);
34544301Swollman	RIP4(A,B,C,D,E,WL55,SL55,KL3);
34644301Swollman	RIP4(E,A,B,C,D,WL56,SL56,KL3);
34744301Swollman	RIP4(D,E,A,B,C,WL57,SL57,KL3);
34844301Swollman	RIP4(C,D,E,A,B,WL58,SL58,KL3);
34944301Swollman	RIP4(B,C,D,E,A,WL59,SL59,KL3);
35044301Swollman	RIP4(A,B,C,D,E,WL60,SL60,KL3);
35144301Swollman	RIP4(E,A,B,C,D,WL61,SL61,KL3);
35244301Swollman	RIP4(D,E,A,B,C,WL62,SL62,KL3);
35344301Swollman	RIP4(C,D,E,A,B,WL63,SL63,KL3);
35444301Swollman
35544301Swollman	RIP5(B,C,D,E,A,WL64,SL64,KL4);
35644301Swollman	RIP5(A,B,C,D,E,WL65,SL65,KL4);
35744301Swollman	RIP5(E,A,B,C,D,WL66,SL66,KL4);
35844301Swollman	RIP5(D,E,A,B,C,WL67,SL67,KL4);
35944301Swollman	RIP5(C,D,E,A,B,WL68,SL68,KL4);
36044301Swollman	RIP5(B,C,D,E,A,WL69,SL69,KL4);
36144301Swollman	RIP5(A,B,C,D,E,WL70,SL70,KL4);
36244301Swollman	RIP5(E,A,B,C,D,WL71,SL71,KL4);
36344301Swollman	RIP5(D,E,A,B,C,WL72,SL72,KL4);
36444301Swollman	RIP5(C,D,E,A,B,WL73,SL73,KL4);
36544301Swollman	RIP5(B,C,D,E,A,WL74,SL74,KL4);
36644301Swollman	RIP5(A,B,C,D,E,WL75,SL75,KL4);
36744301Swollman	RIP5(E,A,B,C,D,WL76,SL76,KL4);
36844301Swollman	RIP5(D,E,A,B,C,WL77,SL77,KL4);
36944301Swollman	RIP5(C,D,E,A,B,WL78,SL78,KL4);
37044301Swollman	RIP5(B,C,D,E,A,WL79,SL79,KL4);
37144301Swollman
37244301Swollman	a=A; b=B; c=C; d=D; e=E;
37344301Swollman	/* Do other half */
37444301Swollman	A=ctx->A; B=ctx->B; C=ctx->C; D=ctx->D; E=ctx->E;
37544301Swollman
37644301Swollman	RIP5(A,B,C,D,E,WR00,SR00,KR0);
37744301Swollman	RIP5(E,A,B,C,D,WR01,SR01,KR0);
37844301Swollman	RIP5(D,E,A,B,C,WR02,SR02,KR0);
37944301Swollman	RIP5(C,D,E,A,B,WR03,SR03,KR0);
38044301Swollman	RIP5(B,C,D,E,A,WR04,SR04,KR0);
38144301Swollman	RIP5(A,B,C,D,E,WR05,SR05,KR0);
38244301Swollman	RIP5(E,A,B,C,D,WR06,SR06,KR0);
38344301Swollman	RIP5(D,E,A,B,C,WR07,SR07,KR0);
38444301Swollman	RIP5(C,D,E,A,B,WR08,SR08,KR0);
38544301Swollman	RIP5(B,C,D,E,A,WR09,SR09,KR0);
38644301Swollman	RIP5(A,B,C,D,E,WR10,SR10,KR0);
38744301Swollman	RIP5(E,A,B,C,D,WR11,SR11,KR0);
38844301Swollman	RIP5(D,E,A,B,C,WR12,SR12,KR0);
38944301Swollman	RIP5(C,D,E,A,B,WR13,SR13,KR0);
39044301Swollman	RIP5(B,C,D,E,A,WR14,SR14,KR0);
39144301Swollman	RIP5(A,B,C,D,E,WR15,SR15,KR0);
39244301Swollman
39344301Swollman	RIP4(E,A,B,C,D,WR16,SR16,KR1);
39444301Swollman	RIP4(D,E,A,B,C,WR17,SR17,KR1);
39544301Swollman	RIP4(C,D,E,A,B,WR18,SR18,KR1);
39644301Swollman	RIP4(B,C,D,E,A,WR19,SR19,KR1);
39744301Swollman	RIP4(A,B,C,D,E,WR20,SR20,KR1);
39844301Swollman	RIP4(E,A,B,C,D,WR21,SR21,KR1);
39944301Swollman	RIP4(D,E,A,B,C,WR22,SR22,KR1);
40044301Swollman	RIP4(C,D,E,A,B,WR23,SR23,KR1);
40144301Swollman	RIP4(B,C,D,E,A,WR24,SR24,KR1);
40244301Swollman	RIP4(A,B,C,D,E,WR25,SR25,KR1);
40344301Swollman	RIP4(E,A,B,C,D,WR26,SR26,KR1);
40444301Swollman	RIP4(D,E,A,B,C,WR27,SR27,KR1);
40544301Swollman	RIP4(C,D,E,A,B,WR28,SR28,KR1);
40644301Swollman	RIP4(B,C,D,E,A,WR29,SR29,KR1);
40744301Swollman	RIP4(A,B,C,D,E,WR30,SR30,KR1);
40844301Swollman	RIP4(E,A,B,C,D,WR31,SR31,KR1);
40944301Swollman
41044301Swollman	RIP3(D,E,A,B,C,WR32,SR32,KR2);
41144301Swollman	RIP3(C,D,E,A,B,WR33,SR33,KR2);
41244301Swollman	RIP3(B,C,D,E,A,WR34,SR34,KR2);
41344301Swollman	RIP3(A,B,C,D,E,WR35,SR35,KR2);
41444301Swollman	RIP3(E,A,B,C,D,WR36,SR36,KR2);
41544301Swollman	RIP3(D,E,A,B,C,WR37,SR37,KR2);
41644301Swollman	RIP3(C,D,E,A,B,WR38,SR38,KR2);
41744301Swollman	RIP3(B,C,D,E,A,WR39,SR39,KR2);
41844301Swollman	RIP3(A,B,C,D,E,WR40,SR40,KR2);
41944301Swollman	RIP3(E,A,B,C,D,WR41,SR41,KR2);
42044301Swollman	RIP3(D,E,A,B,C,WR42,SR42,KR2);
42144301Swollman	RIP3(C,D,E,A,B,WR43,SR43,KR2);
42244301Swollman	RIP3(B,C,D,E,A,WR44,SR44,KR2);
42344301Swollman	RIP3(A,B,C,D,E,WR45,SR45,KR2);
42444301Swollman	RIP3(E,A,B,C,D,WR46,SR46,KR2);
42544301Swollman	RIP3(D,E,A,B,C,WR47,SR47,KR2);
42644301Swollman
42744301Swollman	RIP2(C,D,E,A,B,WR48,SR48,KR3);
42844301Swollman	RIP2(B,C,D,E,A,WR49,SR49,KR3);
42944301Swollman	RIP2(A,B,C,D,E,WR50,SR50,KR3);
43044301Swollman	RIP2(E,A,B,C,D,WR51,SR51,KR3);
43144301Swollman	RIP2(D,E,A,B,C,WR52,SR52,KR3);
43244301Swollman	RIP2(C,D,E,A,B,WR53,SR53,KR3);
43344301Swollman	RIP2(B,C,D,E,A,WR54,SR54,KR3);
43444301Swollman	RIP2(A,B,C,D,E,WR55,SR55,KR3);
43544301Swollman	RIP2(E,A,B,C,D,WR56,SR56,KR3);
43644301Swollman	RIP2(D,E,A,B,C,WR57,SR57,KR3);
43744301Swollman	RIP2(C,D,E,A,B,WR58,SR58,KR3);
43844301Swollman	RIP2(B,C,D,E,A,WR59,SR59,KR3);
43944301Swollman	RIP2(A,B,C,D,E,WR60,SR60,KR3);
44044301Swollman	RIP2(E,A,B,C,D,WR61,SR61,KR3);
44144301Swollman	RIP2(D,E,A,B,C,WR62,SR62,KR3);
44244301Swollman	RIP2(C,D,E,A,B,WR63,SR63,KR3);
44344301Swollman
44444301Swollman	RIP1(B,C,D,E,A,WR64,SR64);
44544301Swollman	RIP1(A,B,C,D,E,WR65,SR65);
44644301Swollman	RIP1(E,A,B,C,D,WR66,SR66);
44744301Swollman	RIP1(D,E,A,B,C,WR67,SR67);
44844301Swollman	RIP1(C,D,E,A,B,WR68,SR68);
44944301Swollman	RIP1(B,C,D,E,A,WR69,SR69);
45044301Swollman	RIP1(A,B,C,D,E,WR70,SR70);
45144301Swollman	RIP1(E,A,B,C,D,WR71,SR71);
45244301Swollman	RIP1(D,E,A,B,C,WR72,SR72);
45344301Swollman	RIP1(C,D,E,A,B,WR73,SR73);
45444301Swollman	RIP1(B,C,D,E,A,WR74,SR74);
45544301Swollman	RIP1(A,B,C,D,E,WR75,SR75);
45644301Swollman	RIP1(E,A,B,C,D,WR76,SR76);
45744301Swollman	RIP1(D,E,A,B,C,WR77,SR77);
45844301Swollman	RIP1(C,D,E,A,B,WR78,SR78);
45944301Swollman	RIP1(B,C,D,E,A,WR79,SR79);
46044301Swollman
46144301Swollman	D     =ctx->B+c+D;
46244301Swollman	ctx->B=ctx->C+d+E;
46344301Swollman	ctx->C=ctx->D+e+A;
46444301Swollman	ctx->D=ctx->E+a+B;
46544301Swollman	ctx->E=ctx->A+b+C;
46644301Swollman	ctx->A=D;
46744301Swollman
46844301Swollman	X+=16;
46944301Swollman	num-=64;
47044301Swollman	if (num <= 0) break;
47144301Swollman		}
47244301Swollman	}
47344301Swollman#endif
47444301Swollman
47544301Swollmanvoid RIPEMD160_Final(md, c)
47644301Swollmanunsigned char *md;
47744301SwollmanRIPEMD160_CTX *c;
47844301Swollman	{
47992913Sobrien	int i,j;
48092913Sobrien	u_int32_t l;
48192913Sobrien	u_int32_t *p;
48244301Swollman	static unsigned char end[4]={0x80,0x00,0x00,0x00};
48344301Swollman	unsigned char *cp=end;
48444301Swollman
48544301Swollman	/* c->num should definitly have room for at least one more byte. */
48644301Swollman	p=c->data;
48744301Swollman	j=c->num;
48844301Swollman	i=j>>2;
48944301Swollman
49044301Swollman	/* purify often complains about the following line as an
49144301Swollman	 * Uninitialized Memory Read.  While this can be true, the
49244301Swollman	 * following p_c2l macro will reset l when that case is true.
49344301Swollman	 * This is because j&0x03 contains the number of 'valid' bytes
49444301Swollman	 * already in p[i].  If and only if j&0x03 == 0, the UMR will
49544301Swollman	 * occur but this is also the only time p_c2l will do
49644301Swollman	 * l= *(cp++) instead of l|= *(cp++)
49744301Swollman	 * Many thanks to Alex Tang <altitude@cic.net> for pickup this
49844301Swollman	 * 'potential bug' */
49944301Swollman#ifdef PURIFY
50044301Swollman	if ((j&0x03) == 0) p[i]=0;
50144301Swollman#endif
50244301Swollman	l=p[i];
50344301Swollman	p_c2l(cp,l,j&0x03);
50444301Swollman	p[i]=l;
50544301Swollman	i++;
50644301Swollman	/* i is the next 'undefined word' */
50744301Swollman	if (c->num >= RIPEMD160_LAST_BLOCK)
50844301Swollman		{
50944301Swollman		for (; i<RIPEMD160_LBLOCK; i++)
51044301Swollman			p[i]=0;
51144301Swollman		ripemd160_block(c,p,64);
51244301Swollman		i=0;
51344301Swollman		}
51444301Swollman	for (; i<(RIPEMD160_LBLOCK-2); i++)
51544301Swollman		p[i]=0;
51644301Swollman	p[RIPEMD160_LBLOCK-2]=c->Nl;
51744301Swollman	p[RIPEMD160_LBLOCK-1]=c->Nh;
51844301Swollman	ripemd160_block(c,p,64);
51944301Swollman	cp=md;
52044301Swollman	l=c->A; l2c(l,cp);
52144301Swollman	l=c->B; l2c(l,cp);
52244301Swollman	l=c->C; l2c(l,cp);
52344301Swollman	l=c->D; l2c(l,cp);
52444301Swollman	l=c->E; l2c(l,cp);
52544301Swollman
52644301Swollman	/* clear stuff, ripemd160_block may be leaving some stuff on the stack
52744301Swollman	 * but I'm not worried :-) */
52844301Swollman	c->num=0;
52944301Swollman/*	memset((char *)&c,0,sizeof(c));*/
53044301Swollman	}
53144301Swollman
53244301Swollman#ifdef undef
53344301Swollmanint printit(l)
53444301Swollmanunsigned long *l;
53544301Swollman	{
53644301Swollman	int i,ii;
53744301Swollman
53844301Swollman	for (i=0; i<2; i++)
53944301Swollman		{
54044301Swollman		for (ii=0; ii<8; ii++)
54144301Swollman			{
54244301Swollman			fprintf(stderr,"%08lx ",l[i*8+ii]);
54344301Swollman			}
54444301Swollman		fprintf(stderr,"\n");
54544301Swollman		}
54644301Swollman	}
54744301Swollman#endif
548