sha1.c revision 1ee3b153b698e888e55dc944560c845ab6e2879a
/*
* Copyright (C) 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* $Id: sha1.c,v 1.5 2000/06/07 02:28:40 bwelling Exp $ */
/* $NetBSD: sha1.c,v 1.5 2000/01/22 22:19:14 mycroft Exp $ */
/* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */
/*
* SHA-1 in C
* By Steve Reid <steve@edmweb.com>
* 100% Public Domain
*
* Test Vectors (from FIPS PUB 180-1)
* "abc"
* A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
* 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
* A million repetitions of "a"
* 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
#include "config.h"
#include <isc/assertions.h>
#if defined(HAVE_ENDIAN_H)
#include <endian.h>
#elif defined(HAVE_SYS_ENDIAN_H)
#endif
#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER)
#define BYTE_ORDER __BYTE_ORDER
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#endif
#if !defined(BYTE_ORDER) || \
/* you must determine what the correct bit order is for
* your compiler - the next line is an intentional error
* which will force your compiles to bomb until you fix
* the above macros.
*/
#error "Undefined or invalid BYTE_ORDER";
#endif
/*
* blk0() and blk() perform the initial expand.
* I got the idea of expanding during the round function from SSLeay
*/
#if BYTE_ORDER == LITTLE_ENDIAN
# define blk0(i) \
#else
#endif
#define blk(i) \
/*
* (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
*/
#define R0(v,w,x,y,z,i) \
w = rol(w, 30);
#define R1(v,w,x,y,z,i) \
w = rol(w, 30);
#define R2(v,w,x,y,z,i) \
w = rol(w, 30);
#define R3(v,w,x,y,z,i) \
w = rol(w, 30);
#define R4(v,w,x,y,z,i) \
w = rol(w, 30);
typedef union {
unsigned char c[64];
unsigned int l[16];
} CHAR64LONG16;
/*
* Hash a single 512-bit block. This is the core of the algorithm.
*/
static void
isc_uint32_t a, b, c, d, e;
unsigned char workspace[64];
/* Copy context->state[] to working vars */
a = state[0];
b = state[1];
c = state[2];
d = state[3];
e = state[4];
/* 4 rounds of 20 operations each. Loop unrolled. */
/* Add the working vars back into context.state[] */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
/* Wipe variables */
a = b = c = d = e = 0;
}
/*
* isc_sha1_init - Initialize new context
*/
void
{
/* SHA1 initialization constants */
}
void
}
/*
* Run your data through this.
*/
void
unsigned int len)
{
unsigned int i, j;
j = (j >> 3) & 63;
if ((j + len) > 63) {
j = 0;
} else {
i = 0;
}
}
/*
* Add padding and return the message digest.
*/
static const unsigned char final_200 = '\200';
static const unsigned char final_0 = '\0';
void
unsigned int i;
unsigned char finalcount[8];
for (i = 0; i < 8; i++) {
/* Endian independent */
finalcount[i] = (unsigned char)
>> ((3 - (i & 3)) * 8)) & 255);
}
/* The next Update should cause a transform() */
if (digest) {
for (i = 0; i < 20; i++)
digest[i] = (unsigned char)
>> ((3 - (i & 3)) * 8)) & 255);
}
}