1ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* $NetBSD: sha1.c,v 1.1 2005/12/20 20:29:40 christos Exp $ */ 2ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ 3ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 4ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* 5ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * SHA-1 in C 6ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * By Steve Reid <steve@edmweb.com> 7ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * 100% Public Domain 8ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * 9ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Test Vectors (from FIPS PUB 180-1) 10ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * "abc" 11ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D 12ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 13ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 14ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * A million repetitions of "a" 15ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F 16ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */ 17ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 18ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define SHA1HANDSOFF /* Copies data before messing with it. */ 19ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 204605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#ifndef USE_MINGW 21ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <sys/cdefs.h> 224605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#endif 23ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <sys/types.h> 24ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <assert.h> 25ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <string.h> 26ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 27ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include "sha1.h" 28ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 29ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#if HAVE_NBTOOL_CONFIG_H 30ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include "nbtool_config.h" 31ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif 32ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 33ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#if !HAVE_SHA1_H 34ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 35ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 36ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 37ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* 38ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * blk0() and blk() perform the initial expand. 39ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * I got the idea of expanding during the round function from SSLeay 40ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */ 41ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#if BYTE_ORDER == LITTLE_ENDIAN 42ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ 43ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross |(rol(block->l[i],8)&0x00FF00FF)) 44ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#else 45ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross# define blk0(i) block->l[i] 46ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif 47ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ 48ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross ^block->l[(i+2)&15]^block->l[i&15],1)) 49ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 50ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* 51ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 52ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */ 53ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); 54ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); 55ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); 56ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); 57ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); 58ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 59ec0a2e83dc66d67addeb90e83144187691852a3eColin Crosstypedef union { 60ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_char c[64]; 61ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_int l[16]; 62ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} CHAR64LONG16; 63ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 64ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* old sparc64 gcc could not compile this */ 65ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#undef SPARC64_GCC_WORKAROUND 66ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#if defined(__sparc64__) && defined(__GNUC__) && __GNUC__ < 3 67ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define SPARC64_GCC_WORKAROUND 68ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif 69ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 70ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#ifdef SPARC64_GCC_WORKAROUND 71ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *); 72ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *); 73ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *); 74ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *); 75ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 76ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i) 77ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i) 78ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i) 79ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i) 80ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i) 81ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 82ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid 83ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossdo_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block) 84ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 85ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3); 86ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); 87ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11); 88ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15); 89ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19); 90ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 91ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 92ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid 93ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossdo_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block) 94ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 95ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23); 96ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); 97ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31); 98ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35); 99ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39); 100ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 101ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 102ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid 103ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossdo_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block) 104ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 105ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43); 106ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); 107ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51); 108ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55); 109ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59); 110ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 111ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 112ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid 113ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossdo_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block) 114ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 115ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63); 116ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); 117ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71); 118ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75); 119ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79); 120ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 121ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif 122ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 123ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* 124ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Hash a single 512-bit block. This is the core of the algorithm. 125ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */ 126ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid SHA1Transform(state, buffer) 127ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_int32_t state[5]; 128ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross const u_char buffer[64]; 129ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 130ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_int32_t a, b, c, d, e; 131ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross CHAR64LONG16 *block; 132ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 133ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#ifdef SHA1HANDSOFF 134ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross CHAR64LONG16 workspace; 135ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif 136ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 137ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross assert(buffer != 0); 138ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross assert(state != 0); 139ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 140ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#ifdef SHA1HANDSOFF 141ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross block = &workspace; 142ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross (void)memcpy(block, buffer, 64); 143ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#else 144ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross block = (CHAR64LONG16 *)(void *)buffer; 145ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif 146ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 147ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross /* Copy context->state[] to working vars */ 148ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross a = state[0]; 149ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross b = state[1]; 150ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross c = state[2]; 151ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross d = state[3]; 152ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross e = state[4]; 153ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 154ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#ifdef SPARC64_GCC_WORKAROUND 155ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross do_R01(&a, &b, &c, &d, &e, block); 156ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross do_R2(&a, &b, &c, &d, &e, block); 157ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross do_R3(&a, &b, &c, &d, &e, block); 158ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross do_R4(&a, &b, &c, &d, &e, block); 159ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#else 160ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross /* 4 rounds of 20 operations each. Loop unrolled. */ 161ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); 162ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); 163ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); 164ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); 165ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); 166ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); 167ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); 168ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); 169ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); 170ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); 171ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); 172ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); 173ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); 174ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); 175ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); 176ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); 177ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); 178ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); 179ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); 180ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); 181ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif 182ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 183ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross /* Add the working vars back into context.state[] */ 184ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross state[0] += a; 185ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross state[1] += b; 186ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross state[2] += c; 187ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross state[3] += d; 188ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross state[4] += e; 189ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 190ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross /* Wipe variables */ 191ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross a = b = c = d = e = 0; 192ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 193ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 194ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 195ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* 196ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * SHA1Init - Initialize new context 197ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */ 198ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid SHA1Init(context) 199ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross SHA1_CTX *context; 200ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 201ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 202ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross assert(context != 0); 203ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 204ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross /* SHA1 initialization constants */ 205ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross context->state[0] = 0x67452301; 206ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross context->state[1] = 0xEFCDAB89; 207ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross context->state[2] = 0x98BADCFE; 208ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross context->state[3] = 0x10325476; 209ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross context->state[4] = 0xC3D2E1F0; 210ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross context->count[0] = context->count[1] = 0; 211ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 212ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 213ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 214ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* 215ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Run your data through this. 216ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */ 217ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid SHA1Update(context, data, len) 218ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross SHA1_CTX *context; 219ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross const u_char *data; 220ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_int len; 221ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 222ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_int i, j; 223ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 224ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross assert(context != 0); 225ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross assert(data != 0); 226ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 227ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross j = context->count[0]; 228ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross if ((context->count[0] += len << 3) < j) 229ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross context->count[1] += (len>>29)+1; 230ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross j = (j >> 3) & 63; 231ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross if ((j + len) > 63) { 232ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross (void)memcpy(&context->buffer[j], data, (i = 64-j)); 233ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross SHA1Transform(context->state, context->buffer); 234ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross for ( ; i + 63 < len; i += 64) 235ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross SHA1Transform(context->state, &data[i]); 236ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross j = 0; 237ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross } else { 238ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross i = 0; 239ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross } 240ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross (void)memcpy(&context->buffer[j], &data[i], len - i); 241ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 242ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 243ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 244ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* 245ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Add padding and return the message digest. 246ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */ 247ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossvoid SHA1Final(digest, context) 248ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_char digest[20]; 249ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross SHA1_CTX* context; 250ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{ 251ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_int i; 252ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross u_char finalcount[8]; 253ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 254ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross assert(digest != 0); 255ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross assert(context != 0); 256ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 257ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross for (i = 0; i < 8; i++) { 258ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross finalcount[i] = (u_char)((context->count[(i >= 4 ? 0 : 1)] 259ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ 260ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross } 261ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross SHA1Update(context, (const u_char *)"\200", 1); 262ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross while ((context->count[0] & 504) != 448) 263ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross SHA1Update(context, (const u_char *)"\0", 1); 264ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ 265ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 266ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross if (digest) { 267ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross for (i = 0; i < 20; i++) 268ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross digest[i] = (u_char) 269ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); 270ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross } 271ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross} 272ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross 273ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif /* HAVE_SHA1_H */ 274