1 /*
\r * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska H
\9agskolan
\r * (Royal Institute of Technology, Stockholm, Sweden).
\r * All rights reserved.
\r *
\r * Redistribution and use in source and binary forms, with or without
\r * modification, are permitted provided that the following conditions
\r * are met:
\r *
\r * 1. Redistributions of source code must retain the above copyright
\r * notice, this list of conditions and the following disclaimer.
\r *
\r * 2. Redistributions in binary form must reproduce the above copyright
\r * notice, this list of conditions and the following disclaimer in the
\r * documentation and/or other materials provided with the distribution.
\r *
\r * 3. All advertising materials mentioning features or use of this software
\r * must display the following acknowledgement:
\r * This product includes software developed by the Kungliga Tekniska
\r * H
\9agskolan and its contributors.
\r *
\r * 4. Neither the name of the Institute nor the names of its contributors
\r * may be used to endorse or promote products derived from this software
\r * without specific prior written permission.
\r *
\r * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
\r * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
\r * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\r * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
\r * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
\r * SUCH DAMAGE.
\r */
\r\r#ifdef HAVE_CONFIG_H
\r#include "config.h"
\r\rRCSID("$Id: md4.c,v 1.2 2001/12/04 02:06:29 rjs3 Exp $");
\r#endif
\r\r#include <stdlib.h>
\r#include <string.h>
\r\r#include "md4.h"
\r\r#ifndef min
\r#define min(a,b) (((a)>(b))?(b):(a))
\r#endif
\r\r#define A m->counter[0]
\r#define B m->counter[1]
\r#define C m->counter[2]
\r#define D m->counter[3]
\r#define X data
\r\rvoid
\rmd4_init (struct md4 *m)
\r{
\r m->offset = 0;
\r m->sz = 0;
\r D = 0x10325476;
\r C = 0x98badcfe;
\r B = 0xefcdab89;
\r A = 0x67452301;
\r}
\r\rstatic inline u_int32_t
\rcshift (u_int32_t x, unsigned int n)
\r{
\r return (x << n) | (x >> (32 - n));
\r}
\r\r#define F(x,y,z) ((x & y) | (~x & z))
\r#define G(x,y,z) ((x & y) | (x & z) | (y & z))
\r#define H(x,y,z) (x ^ y ^ z)
\r\r#define DOIT(a,b,c,d,k,s,i,OP) \
\ra = cshift(a + OP(b,c,d) + X[k] + i, s)
\r\r#define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
\r#define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
\r#define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
\r\rstatic inline void
\rcalc (struct md4 *m, u_int32_t *data)
\r{
\r u_int32_t AA, BB, CC, DD;
\r\r AA = A;
\r BB = B;
\r CC = C;
\r DD = D;
\r\r /* Round 1 */
\r\r DO1(A,B,C,D,0,3,0);
\r DO1(D,A,B,C,1,7,0);
\r DO1(C,D,A,B,2,11,0);
\r DO1(B,C,D,A,3,19,0);
\r\r DO1(A,B,C,D,4,3,0);
\r DO1(D,A,B,C,5,7,0);
\r DO1(C,D,A,B,6,11,0);
\r DO1(B,C,D,A,7,19,0);
\r\r DO1(A,B,C,D,8,3,0);
\r DO1(D,A,B,C,9,7,0);
\r DO1(C,D,A,B,10,11,0);
\r DO1(B,C,D,A,11,19,0);
\r\r DO1(A,B,C,D,12,3,0);
\r DO1(D,A,B,C,13,7,0);
\r DO1(C,D,A,B,14,11,0);
\r DO1(B,C,D,A,15,19,0);
\r\r /* Round 2 */
\r\r DO2(A,B,C,D,0,3,0x5A827999);
\r DO2(D,A,B,C,4,5,0x5A827999);
\r DO2(C,D,A,B,8,9,0x5A827999);
\r DO2(B,C,D,A,12,13,0x5A827999);
\r\r DO2(A,B,C,D,1,3,0x5A827999);
\r DO2(D,A,B,C,5,5,0x5A827999);
\r DO2(C,D,A,B,9,9,0x5A827999);
\r DO2(B,C,D,A,13,13,0x5A827999);
\r\r DO2(A,B,C,D,2,3,0x5A827999);
\r DO2(D,A,B,C,6,5,0x5A827999);
\r DO2(C,D,A,B,10,9,0x5A827999);
\r DO2(B,C,D,A,14,13,0x5A827999);
\r\r DO2(A,B,C,D,3,3,0x5A827999);
\r DO2(D,A,B,C,7,5,0x5A827999);
\r DO2(C,D,A,B,11,9,0x5A827999);
\r DO2(B,C,D,A,15,13,0x5A827999);
\r\r /* Round 3 */
\r\r DO3(A,B,C,D,0,3,0x6ED9EBA1);
\r DO3(D,A,B,C,8,9,0x6ED9EBA1);
\r DO3(C,D,A,B,4,11,0x6ED9EBA1);
\r DO3(B,C,D,A,12,15,0x6ED9EBA1);
\r\r DO3(A,B,C,D,2,3,0x6ED9EBA1);
\r DO3(D,A,B,C,10,9,0x6ED9EBA1);
\r DO3(C,D,A,B,6,11,0x6ED9EBA1);
\r DO3(B,C,D,A,14,15,0x6ED9EBA1);
\r\r DO3(A,B,C,D,1,3,0x6ED9EBA1);
\r DO3(D,A,B,C,9,9,0x6ED9EBA1);
\r DO3(C,D,A,B,5,11,0x6ED9EBA1);
\r DO3(B,C,D,A,13,15,0x6ED9EBA1);
\r\r DO3(A,B,C,D,3,3,0x6ED9EBA1);
\r DO3(D,A,B,C,11,9,0x6ED9EBA1);
\r DO3(C,D,A,B,7,11,0x6ED9EBA1);
\r DO3(B,C,D,A,15,15,0x6ED9EBA1);
\r\r A += AA;
\r B += BB;
\r C += CC;
\r D += DD;
\r}
\r\r/*
\r * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
\r */
\r\rstatic inline u_int32_t
\rswap_u_int32_t (u_int32_t t)
\r{
\r#if defined(WORDS_BIGENDIAN)
\r#define ROL(x,n) ((x)<<(n))|((x)>>(32-(n)))
\r u_int32_t temp1, temp2;
\r\r temp1 = ROL(t,16);
\r temp2 = temp1 >> 8;
\r temp1 &= 0x00ff00ff;
\r temp2 &= 0x00ff00ff;
\r temp1 <<= 8;
\r return temp1 | temp2;
\r#else
\r return t;
\r#endif
\r}
\r\rstruct x32{
\r unsigned int a:32;
\r unsigned int b:32;
\r};
\r\rvoid
\rmd4_update (struct md4 *m, const void *v, size_t len)
\r{
\r const unsigned char *p = v;
\r m->sz += len;
\r while(len > 0){
\r size_t l = min(len, 64 - m->offset);
\r memcpy(m->save + m->offset, p, l);
\r m->offset += l;
\r p += l;
\r len -= l;
\r if(m->offset == 64){
\r#if defined(WORDS_BIGENDIAN)
\r int i;
\r u_int32_t current[16];
\r struct x32 *u = (struct x32*)m->save;
\r for(i = 0; i < 8; i++){
\r current[2*i+0] = swap_u_int32_t(u[i].a);
\r current[2*i+1] = swap_u_int32_t(u[i].b);
\r }
\r calc(m, current);
\r#else
\r calc(m, (u_int32_t*)m->save);
\r#endif
\r m->offset = 0;
\r }
\r }
\r}
\r\rvoid
\rmd4_finito (struct md4 *m, void *res)
\r{
\r static unsigned char zeros[72];
\r u_int32_t len;
\r unsigned int dstart = (120 - m->offset - 1) % 64 + 1;
\r\r *zeros = 0x80;
\r memset (zeros + 1, 0, sizeof(zeros) - 1);
\r len = 8 * m->sz;
\r zeros[dstart+0] = (len >> 0) & 0xff;
\r zeros[dstart+1] = (len >> 8) & 0xff;
\r zeros[dstart+2] = (len >> 16) & 0xff;
\r zeros[dstart+3] = (len >> 24) & 0xff;
\r md4_update (m, zeros, dstart + 8);
\r {
\r int i;
\r unsigned char *r = (unsigned char *)res;
\r\r for (i = 0; i < 4; ++i) {
\r r[4*i] = m->counter[i] & 0xFF;
\r r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
\r r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
\r r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
\r }
\r }
\r#if 0
\r {
\r int i;
\r u_int32_t *r = (u_int32_t *)res;
\r\r for (i = 0; i < 4; ++i)
\r r[i] = swap_u_int32_t (m->counter[i]);
\r }
\r#endif
\r}
\r