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: sha.c,v 1.2 2001/12/04 02:06:31 rjs3 Exp $");
\r#endif
\r\r#include <stdlib.h>
\r#include <string.h>
\r\r#include "sha.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 E m->counter[4]
\r#define X data
\r\rvoid
\rsha_init (struct sha *m)
\r{
\r m->offset = 0;
\r m->sz = 0;
\r A = 0x67452301;
\r B = 0xefcdab89;
\r C = 0x98badcfe;
\r D = 0x10325476;
\r E = 0xc3d2e1f0;
\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 F0(x,y,z) ((x & y) | (~x & z))
\r#define F1(x,y,z) (x ^ y ^ z)
\r#define F2(x,y,z) ((x & y) | (x & z) | (y & z))
\r#define F3(x,y,z) F1(x,y,z)
\r\r#define K0 0x5a827999
\r#define K1 0x6ed9eba1
\r#define K2 0x8f1bbcdc
\r#define K3 0xca62c1d6
\r\r#define DO(t,f,k) \
\rdo { \
\r u_int32_t temp; \
\r \
\r temp = cshift(AA, 5) + f(BB,CC,DD) + EE + data[t] + k; \
\r EE = DD; \
\r DD = CC; \
\r CC = cshift(BB, 30); \
\r BB = AA; \
\r AA = temp; \
\r} while(0)
\r\rstatic inline void
\rcalc (struct sha *m, u_int32_t *in)
\r{
\r u_int32_t AA, BB, CC, DD, EE;
\r u_int32_t data[80];
\r int i;
\r\r AA = A;
\r BB = B;
\r CC = C;
\r DD = D;
\r EE = E;
\r\r for (i = 0; i < 16; ++i)
\r data[i] = in[i];
\r for (i = 16; i < 80; ++i)
\r data[i] = cshift(data[i-3] ^ data[i-8] ^ data[i-14] ^ data[i-16], 1);
\r\r /* t=[0,19] */
\r\r DO(0,F0,K0);
\r DO(1,F0,K0);
\r DO(2,F0,K0);
\r DO(3,F0,K0);
\r DO(4,F0,K0);
\r DO(5,F0,K0);
\r DO(6,F0,K0);
\r DO(7,F0,K0);
\r DO(8,F0,K0);
\r DO(9,F0,K0);
\r DO(10,F0,K0);
\r DO(11,F0,K0);
\r DO(12,F0,K0);
\r DO(13,F0,K0);
\r DO(14,F0,K0);
\r DO(15,F0,K0);
\r DO(16,F0,K0);
\r DO(17,F0,K0);
\r DO(18,F0,K0);
\r DO(19,F0,K0);
\r\r /* t=[20,39] */
\r\r DO(20,F1,K1);
\r DO(21,F1,K1);
\r DO(22,F1,K1);
\r DO(23,F1,K1);
\r DO(24,F1,K1);
\r DO(25,F1,K1);
\r DO(26,F1,K1);
\r DO(27,F1,K1);
\r DO(28,F1,K1);
\r DO(29,F1,K1);
\r DO(30,F1,K1);
\r DO(31,F1,K1);
\r DO(32,F1,K1);
\r DO(33,F1,K1);
\r DO(34,F1,K1);
\r DO(35,F1,K1);
\r DO(36,F1,K1);
\r DO(37,F1,K1);
\r DO(38,F1,K1);
\r DO(39,F1,K1);
\r\r /* t=[40,59] */
\r\r DO(40,F2,K2);
\r DO(41,F2,K2);
\r DO(42,F2,K2);
\r DO(43,F2,K2);
\r DO(44,F2,K2);
\r DO(45,F2,K2);
\r DO(46,F2,K2);
\r DO(47,F2,K2);
\r DO(48,F2,K2);
\r DO(49,F2,K2);
\r DO(50,F2,K2);
\r DO(51,F2,K2);
\r DO(52,F2,K2);
\r DO(53,F2,K2);
\r DO(54,F2,K2);
\r DO(55,F2,K2);
\r DO(56,F2,K2);
\r DO(57,F2,K2);
\r DO(58,F2,K2);
\r DO(59,F2,K2);
\r\r /* t=[60,79] */
\r\r DO(60,F3,K3);
\r DO(61,F3,K3);
\r DO(62,F3,K3);
\r DO(63,F3,K3);
\r DO(64,F3,K3);
\r DO(65,F3,K3);
\r DO(66,F3,K3);
\r DO(67,F3,K3);
\r DO(68,F3,K3);
\r DO(69,F3,K3);
\r DO(70,F3,K3);
\r DO(71,F3,K3);
\r DO(72,F3,K3);
\r DO(73,F3,K3);
\r DO(74,F3,K3);
\r DO(75,F3,K3);
\r DO(76,F3,K3);
\r DO(77,F3,K3);
\r DO(78,F3,K3);
\r DO(79,F3,K3);
\r\r A += AA;
\r B += BB;
\r C += CC;
\r D += DD;
\r E += EE;
\r}
\r\r/*
\r * From `Performance analysis of SHA' 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
\rsha_update (struct sha *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) || defined(_CRAY)
\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
\rsha_finito (struct sha *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+7] = (len >> 0) & 0xff;
\r zeros[dstart+6] = (len >> 8) & 0xff;
\r zeros[dstart+5] = (len >> 16) & 0xff;
\r zeros[dstart+4] = (len >> 24) & 0xff;
\r sha_update (m, zeros, dstart + 8);
\r {
\r int i;
\r unsigned char *r = (unsigned char*)res;
\r\r for (i = 0; i < 5; ++i) {
\r r[4*i+3] = m->counter[i] & 0xFF;
\r r[4*i+2] = (m->counter[i] >> 8) & 0xFF;
\r r[4*i+1] = (m->counter[i] >> 16) & 0xFF;
\r r[4*i] = (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 < 5; ++i)
\r r[i] = swap_u_int32_t (m->counter[i]);
\r }
\r#endif
\r}
\r