Commit 5dddf7c8 by Andreas Ericsson

Add block-sha1 in favour of the mozilla routines

Since block-sha1 from git.git has such excellent performance, we
can also get rid of the openssl dependency. It's rather simple
to add it back later as an optional extra, but we really needn't
bother to pull in the entire ssl library and have to deal with
linking issues now that we have the portable and, performance-wise,
truly excellent block-sha1 code to fall back on.

Since this requires a slight revamp of the build rules anyway, we
take the opportunity to fix including EXTRA_OBJS in the final build
as well.

The block-sha1 code was originally implemented for git.git by
Linus Torvalds <torvalds@linux-foundation.org> and was later
polished by Nicolas Pitre <nico@cam.org>.

Signed-off-by: Andreas Ericsson <ae@op5.se>
parent d15e94c0
...@@ -3,10 +3,6 @@ all:: ...@@ -3,10 +3,6 @@ all::
# Define NO_VISIBILITY if your compiler does not support symbol # Define NO_VISIBILITY if your compiler does not support symbol
# visibility in general (and the -fvisibility switch in particular). # visibility in general (and the -fvisibility switch in particular).
# #
# Define NO_OPENSSL if you do not have OpenSSL, or if you simply want
# to use the bundled (Mozilla) SHA1 routines. (The bundled SHA1
# routines are reported to be faster than OpenSSL on some platforms)
#
DOXYGEN = doxygen DOXYGEN = doxygen
INSTALL = install INSTALL = install
...@@ -28,6 +24,7 @@ OS = unix ...@@ -28,6 +24,7 @@ OS = unix
EXTRA_SRC = EXTRA_SRC =
EXTRA_OBJ = EXTRA_OBJ =
EXTRA_CFLAGS =
AR_OUT= AR_OUT=
CC_OUT=-o # add a space CC_OUT=-o # add a space
...@@ -58,7 +55,7 @@ endif ...@@ -58,7 +55,7 @@ endif
SRC_C = $(wildcard src/*.c) SRC_C = $(wildcard src/*.c)
OS_SRC = $(wildcard src/$(OS)/*.c) OS_SRC = $(wildcard src/$(OS)/*.c)
SRC_C += $(OS_SRC) SRC_C += $(OS_SRC)
OBJS = $(patsubst %.c,%.o,$(SRC_C)) OBJS = $(patsubst %.c,%.o,$(SRC_C)) $(EXTRA_OBJ)
HDRS = $(wildcard src/*.h) HDRS = $(wildcard src/*.h)
PUBLIC_HEADERS = $(wildcard src/git/*.h) PUBLIC_HEADERS = $(wildcard src/git/*.h)
HDRS += $(PUBLIC_HEADERS) HDRS += $(PUBLIC_HEADERS)
...@@ -71,22 +68,21 @@ TEST_EXE = $(patsubst %.o,%.exe,$(TEST_OBJ)) ...@@ -71,22 +68,21 @@ TEST_EXE = $(patsubst %.o,%.exe,$(TEST_OBJ))
TEST_RUN = $(patsubst %.exe,%.run,$(TEST_EXE)) TEST_RUN = $(patsubst %.exe,%.run,$(TEST_EXE))
TEST_VAL = $(patsubst %.exe,%.val,$(TEST_EXE)) TEST_VAL = $(patsubst %.exe,%.val,$(TEST_EXE))
ifndef NO_OPENSSL ifdef PPC_SHA1
SHA1_HEADER = <openssl/sha.h> EXTRA_SRC += src/ppc/sha1.c src/ppc/sha1ppc.S
EXTRA_OBJ += src/ppc/sha1.o src/ppc/sha1ppc.o
EXTRA_CFLAGS += -DPPC_SHA1
else else
SHA1_HEADER = "sha1/sha1.h" EXTRA_SRC += src/block-sha1/sha1.c
EXTRA_SRC += src/sha1/sha1.c EXTRA_OBJ += src/block-sha1/sha1.o
EXTRA_OBJ += src/sha1/sha1.o
endif endif
BASIC_CFLAGS := -Isrc -DSHA1_HEADER='$(SHA1_HEADER)' BASIC_CFLAGS := -Isrc
ifndef NO_VISIBILITY ifndef NO_VISIBILITY
BASIC_CFLAGS += -fvisibility=hidden BASIC_CFLAGS += -fvisibility=hidden
endif endif
ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS) ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS) $(EXTRA_CFLAGS)
SRC_C += $(EXTRA_SRC)
OBJ += $(EXTRA_OBJ)
all:: $(GIT_LIB) all:: $(GIT_LIB)
......
/*
* SHA1 routine optimized to do word accesses rather than byte accesses,
* and to avoid unnecessary copies into the context array.
*
* This was initially based on the Mozilla SHA1 implementation, although
* none of the original Mozilla code remains.
*/
#include "common.h"
#include "sha1.h"
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
/*
* Force usage of rol or ror by selecting the one with the smaller constant.
* It _can_ generate slightly smaller code (a constant of 1 is special), but
* perhaps more importantly it's possibly faster on any uarch that does a
* rotate with a loop.
*/
#define SHA_ASM(op, x, n) ({ unsigned int __res; __asm__(op " %1,%0":"=r" (__res):"i" (n), "0" (x)); __res; })
#define SHA_ROL(x,n) SHA_ASM("rol", x, n)
#define SHA_ROR(x,n) SHA_ASM("ror", x, n)
#else
#define SHA_ROT(X,l,r) (((X) << (l)) | ((X) >> (r)))
#define SHA_ROL(X,n) SHA_ROT(X,n,32-(n))
#define SHA_ROR(X,n) SHA_ROT(X,32-(n),n)
#endif
/*
* If you have 32 registers or more, the compiler can (and should)
* try to change the array[] accesses into registers. However, on
* machines with less than ~25 registers, that won't really work,
* and at least gcc will make an unholy mess of it.
*
* So to avoid that mess which just slows things down, we force
* the stores to memory to actually happen (we might be better off
* with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
* suggested by Artur Skawina - that will also make gcc unable to
* try to do the silly "optimize away loads" part because it won't
* see what the value will be).
*
* Ben Herrenschmidt reports that on PPC, the C version comes close
* to the optimized asm with this (ie on PPC you don't want that
* 'volatile', since there are lots of registers).
*
* On ARM we get the best code generation by forcing a full memory barrier
* between each SHA_ROUND, otherwise gcc happily get wild with spilling and
* the stack frame size simply explode and performance goes down the drain.
*/
#if defined(__i386__) || defined(__x86_64__)
#define setW(x, val) (*(volatile unsigned int *)&W(x) = (val))
#elif defined(__GNUC__) && defined(__arm__)
#define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
#else
#define setW(x, val) (W(x) = (val))
#endif
/*
* Performance might be improved if the CPU architecture is OK with
* unaligned 32-bit loads and a fast ntohl() is available.
* Otherwise fall back to byte loads and shifts which is portable,
* and is faster on architectures with memory alignment issues.
*/
#if defined(__i386__) || defined(__x86_64__) || \
defined(__ppc__) || defined(__ppc64__) || \
defined(__powerpc__) || defined(__powerpc64__) || \
defined(__s390__) || defined(__s390x__)
#define get_be32(p) ntohl(*(unsigned int *)(p))
#define put_be32(p, v) do { *(unsigned int *)(p) = htonl(v); } while (0)
#else
#define get_be32(p) ( \
(*((unsigned char *)(p) + 0) << 24) | \
(*((unsigned char *)(p) + 1) << 16) | \
(*((unsigned char *)(p) + 2) << 8) | \
(*((unsigned char *)(p) + 3) << 0) )
#define put_be32(p, v) do { \
unsigned int __v = (v); \
*((unsigned char *)(p) + 0) = __v >> 24; \
*((unsigned char *)(p) + 1) = __v >> 16; \
*((unsigned char *)(p) + 2) = __v >> 8; \
*((unsigned char *)(p) + 3) = __v >> 0; } while (0)
#endif
/* This "rolls" over the 512-bit array */
#define W(x) (array[(x)&15])
/*
* Where do we get the source from? The first 16 iterations get it from
* the input data, the next mix it from the 512-bit array.
*/
#define SHA_SRC(t) get_be32(data + t)
#define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
#define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
unsigned int TEMP = input(t); setW(t, TEMP); \
E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \
B = SHA_ROR(B, 2); } while (0)
#define T_0_15(t, A, B, C, D, E) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
#define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
#define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
#define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
static void blk_SHA1_Block(blk_SHA_CTX *ctx, const unsigned int *data)
{
unsigned int A,B,C,D,E;
unsigned int array[16];
A = ctx->H[0];
B = ctx->H[1];
C = ctx->H[2];
D = ctx->H[3];
E = ctx->H[4];
/* Round 1 - iterations 0-16 take their input from 'data' */
T_0_15( 0, A, B, C, D, E);
T_0_15( 1, E, A, B, C, D);
T_0_15( 2, D, E, A, B, C);
T_0_15( 3, C, D, E, A, B);
T_0_15( 4, B, C, D, E, A);
T_0_15( 5, A, B, C, D, E);
T_0_15( 6, E, A, B, C, D);
T_0_15( 7, D, E, A, B, C);
T_0_15( 8, C, D, E, A, B);
T_0_15( 9, B, C, D, E, A);
T_0_15(10, A, B, C, D, E);
T_0_15(11, E, A, B, C, D);
T_0_15(12, D, E, A, B, C);
T_0_15(13, C, D, E, A, B);
T_0_15(14, B, C, D, E, A);
T_0_15(15, A, B, C, D, E);
/* Round 1 - tail. Input from 512-bit mixing array */
T_16_19(16, E, A, B, C, D);
T_16_19(17, D, E, A, B, C);
T_16_19(18, C, D, E, A, B);
T_16_19(19, B, C, D, E, A);
/* Round 2 */
T_20_39(20, A, B, C, D, E);
T_20_39(21, E, A, B, C, D);
T_20_39(22, D, E, A, B, C);
T_20_39(23, C, D, E, A, B);
T_20_39(24, B, C, D, E, A);
T_20_39(25, A, B, C, D, E);
T_20_39(26, E, A, B, C, D);
T_20_39(27, D, E, A, B, C);
T_20_39(28, C, D, E, A, B);
T_20_39(29, B, C, D, E, A);
T_20_39(30, A, B, C, D, E);
T_20_39(31, E, A, B, C, D);
T_20_39(32, D, E, A, B, C);
T_20_39(33, C, D, E, A, B);
T_20_39(34, B, C, D, E, A);
T_20_39(35, A, B, C, D, E);
T_20_39(36, E, A, B, C, D);
T_20_39(37, D, E, A, B, C);
T_20_39(38, C, D, E, A, B);
T_20_39(39, B, C, D, E, A);
/* Round 3 */
T_40_59(40, A, B, C, D, E);
T_40_59(41, E, A, B, C, D);
T_40_59(42, D, E, A, B, C);
T_40_59(43, C, D, E, A, B);
T_40_59(44, B, C, D, E, A);
T_40_59(45, A, B, C, D, E);
T_40_59(46, E, A, B, C, D);
T_40_59(47, D, E, A, B, C);
T_40_59(48, C, D, E, A, B);
T_40_59(49, B, C, D, E, A);
T_40_59(50, A, B, C, D, E);
T_40_59(51, E, A, B, C, D);
T_40_59(52, D, E, A, B, C);
T_40_59(53, C, D, E, A, B);
T_40_59(54, B, C, D, E, A);
T_40_59(55, A, B, C, D, E);
T_40_59(56, E, A, B, C, D);
T_40_59(57, D, E, A, B, C);
T_40_59(58, C, D, E, A, B);
T_40_59(59, B, C, D, E, A);
/* Round 4 */
T_60_79(60, A, B, C, D, E);
T_60_79(61, E, A, B, C, D);
T_60_79(62, D, E, A, B, C);
T_60_79(63, C, D, E, A, B);
T_60_79(64, B, C, D, E, A);
T_60_79(65, A, B, C, D, E);
T_60_79(66, E, A, B, C, D);
T_60_79(67, D, E, A, B, C);
T_60_79(68, C, D, E, A, B);
T_60_79(69, B, C, D, E, A);
T_60_79(70, A, B, C, D, E);
T_60_79(71, E, A, B, C, D);
T_60_79(72, D, E, A, B, C);
T_60_79(73, C, D, E, A, B);
T_60_79(74, B, C, D, E, A);
T_60_79(75, A, B, C, D, E);
T_60_79(76, E, A, B, C, D);
T_60_79(77, D, E, A, B, C);
T_60_79(78, C, D, E, A, B);
T_60_79(79, B, C, D, E, A);
ctx->H[0] += A;
ctx->H[1] += B;
ctx->H[2] += C;
ctx->H[3] += D;
ctx->H[4] += E;
}
void blk_SHA1_Init(blk_SHA_CTX *ctx)
{
ctx->size = 0;
/* Initialize H with the magic constants (see FIPS180 for constants) */
ctx->H[0] = 0x67452301;
ctx->H[1] = 0xefcdab89;
ctx->H[2] = 0x98badcfe;
ctx->H[3] = 0x10325476;
ctx->H[4] = 0xc3d2e1f0;
}
void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
{
int lenW = ctx->size & 63;
ctx->size += len;
/* Read the data into W and process blocks as they get full */
if (lenW) {
int left = 64 - lenW;
if (len < left)
left = len;
memcpy(lenW + (char *)ctx->W, data, left);
lenW = (lenW + left) & 63;
len -= left;
data = ((const char *)data + left);
if (lenW)
return;
blk_SHA1_Block(ctx, ctx->W);
}
while (len >= 64) {
blk_SHA1_Block(ctx, data);
data = ((const char *)data + 64);
len -= 64;
}
if (len)
memcpy(ctx->W, data, len);
}
void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
{
static const unsigned char pad[64] = { 0x80 };
unsigned int padlen[2];
int i;
/* Pad with a binary 1 (ie 0x80), then zeroes, then length */
padlen[0] = htonl(ctx->size >> 29);
padlen[1] = htonl(ctx->size << 3);
i = ctx->size & 63;
blk_SHA1_Update(ctx, pad, 1+ (63 & (55 - i)));
blk_SHA1_Update(ctx, padlen, 8);
/* Output hash */
for (i = 0; i < 5; i++)
put_be32(hashout + i*4, ctx->H[i]);
}
/*
* SHA1 routine optimized to do word accesses rather than byte accesses,
* and to avoid unnecessary copies into the context array.
*
* This was initially based on the Mozilla SHA1 implementation, although
* none of the original Mozilla code remains.
*/
typedef struct {
unsigned long long size;
unsigned int H[5];
unsigned int W[16];
} blk_SHA_CTX;
void blk_SHA1_Init(blk_SHA_CTX *ctx);
void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *dataIn, unsigned long len);
void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx);
#define git_SHA_CTX blk_SHA_CTX
#define git_SHA1_Init blk_SHA1_Init
#define git_SHA1_Update blk_SHA1_Update
#define git_SHA1_Final blk_SHA1_Final
/*
* Let's make sure we always have a sane definition for ntohl()/htonl().
* Some libraries define those as a function call, just to perform byte
* shifting, bringing significant overhead to what should be a simple
* operation.
*/
#include "common.h"
/*
* Default version that the compiler ought to optimize properly with
* constant values.
*/
static inline uint32_t default_swab32(uint32_t val)
{
return (((val & 0xff000000) >> 24) |
((val & 0x00ff0000) >> 8) |
((val & 0x0000ff00) << 8) |
((val & 0x000000ff) << 24));
}
#undef bswap32
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
#define bswap32(x) ({ \
uint32_t __res; \
if (__builtin_constant_p(x)) { \
__res = default_swab32(x); \
} else { \
__asm__("bswap %0" : "=r" (__res) : "0" ((uint32_t)(x))); \
} \
__res; })
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
#include <stdlib.h>
#define bswap32(x) _byteswap_ulong(x)
#endif
#ifdef bswap32
#undef ntohl
#undef htonl
#define ntohl(x) bswap32(x)
#define htonl(x) bswap32(x)
#endif
...@@ -80,6 +80,7 @@ typedef __int64 off64_t; ...@@ -80,6 +80,7 @@ typedef __int64 off64_t;
#include "util.h" #include "util.h"
#include "thread-utils.h" #include "thread-utils.h"
#include "errors.h" #include "errors.h"
#include "bswap.h"
#define GIT_PATH_MAX 4096 #define GIT_PATH_MAX 4096
......
...@@ -25,10 +25,10 @@ ...@@ -25,10 +25,10 @@
#include "common.h" #include "common.h"
#include "hash.h" #include "hash.h"
#include SHA1_HEADER #include "sha1.h"
struct git_hash_ctx { struct git_hash_ctx {
SHA_CTX c; git_SHA_CTX c;
}; };
git_hash_ctx *git_hash_new_ctx(void) git_hash_ctx *git_hash_new_ctx(void)
...@@ -38,7 +38,7 @@ git_hash_ctx *git_hash_new_ctx(void) ...@@ -38,7 +38,7 @@ git_hash_ctx *git_hash_new_ctx(void)
if (!ctx) if (!ctx)
return NULL; return NULL;
SHA1_Init(&ctx->c); git_SHA1_Init(&ctx->c);
return ctx; return ctx;
} }
...@@ -51,37 +51,37 @@ void git_hash_free_ctx(git_hash_ctx *ctx) ...@@ -51,37 +51,37 @@ void git_hash_free_ctx(git_hash_ctx *ctx)
void git_hash_init(git_hash_ctx *ctx) void git_hash_init(git_hash_ctx *ctx)
{ {
assert(ctx); assert(ctx);
SHA1_Init(&ctx->c); git_SHA1_Init(&ctx->c);
} }
void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
{ {
assert(ctx); assert(ctx);
SHA1_Update(&ctx->c, data, len); git_SHA1_Update(&ctx->c, data, len);
} }
void git_hash_final(git_oid *out, git_hash_ctx *ctx) void git_hash_final(git_oid *out, git_hash_ctx *ctx)
{ {
assert(ctx); assert(ctx);
SHA1_Final(out->id, &ctx->c); git_SHA1_Final(out->id, &ctx->c);
} }
void git_hash_buf(git_oid *out, const void *data, size_t len) void git_hash_buf(git_oid *out, const void *data, size_t len)
{ {
SHA_CTX c; git_SHA_CTX c;
SHA1_Init(&c); git_SHA1_Init(&c);
SHA1_Update(&c, data, len); git_SHA1_Update(&c, data, len);
SHA1_Final(out->id, &c); git_SHA1_Final(out->id, &c);
} }
void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n) void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
{ {
SHA_CTX c; git_SHA_CTX c;
size_t i; size_t i;
SHA1_Init(&c); git_SHA1_Init(&c);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
SHA1_Update(&c, vec[i].data, vec[i].len); git_SHA1_Update(&c, vec[i].data, vec[i].len);
SHA1_Final(out->id, &c); git_SHA1_Final(out->id, &c);
} }
#ifndef INCLUDE_sha1_h__
#define INCLUDE_sha1_h__
#ifdef PPC_SHA1
# include "ppc/sha1.h"
#else
# include "block-sha1/sha1.h"
#endif
#endif /* INCLUDE_sha1_h__ */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is SHA 180-1 Reference Implementation (Compact version)
*
* The Initial Developer of the Original Code is Paul Kocher of
* Cryptography Research. Portions created by Paul Kocher are
* Copyright (C) 1995-9 by Cryptography Research, Inc. All
* Rights Reserved.
*
* Contributor(s):
*
* Paul Kocher
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License, version 2, (the "GPL")
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* If you wish to allow use of your version of this file only under
* the terms of the GPL and not to allow others to use your version
* of this file under the MPL, indicate your decision by deleting
* the provisions above and replace them with the notice and other
* provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file
* under either the MPL or the GPL.
*/
#include "sha1.h"
static void shaHashBlock(SHA_CTX *ctx);
void SHA1_Init(SHA_CTX *ctx) {
int i;
ctx->lenW = 0;
ctx->sizeHi = ctx->sizeLo = 0;
/* Initialize H with the magic constants (see FIPS180 for constants)
*/
ctx->H[0] = 0x67452301;
ctx->H[1] = 0xefcdab89;
ctx->H[2] = 0x98badcfe;
ctx->H[3] = 0x10325476;
ctx->H[4] = 0xc3d2e1f0;
for (i = 0; i < 80; i++)
ctx->W[i] = 0;
}
void SHA1_Update(SHA_CTX *ctx, const void *_dataIn, int len) {
const unsigned char *dataIn = _dataIn;
int i;
/* Read the data into W and process blocks as they get full
*/
for (i = 0; i < len; i++) {
ctx->W[ctx->lenW / 4] <<= 8;
ctx->W[ctx->lenW / 4] |= (unsigned int)dataIn[i];
if ((++ctx->lenW) % 64 == 0) {
shaHashBlock(ctx);
ctx->lenW = 0;
}
ctx->sizeLo += 8;
ctx->sizeHi += (ctx->sizeLo < 8);
}
}
void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx) {
unsigned char pad0x80 = 0x80;
unsigned char pad0x00 = 0x00;
unsigned char padlen[8];
int i;
/* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
*/
padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
SHA1_Update(ctx, &pad0x80, 1);
while (ctx->lenW != 56)
SHA1_Update(ctx, &pad0x00, 1);
SHA1_Update(ctx, padlen, 8);
/* Output hash
*/
for (i = 0; i < 20; i++) {
hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
ctx->H[i / 4] <<= 8;
}
/*
* Re-initialize the context (also zeroizes contents)
*/
SHA1_Init(ctx);
}
#define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32-(n))))
static void shaHashBlock(SHA_CTX *ctx) {
int t;
unsigned int A,B,C,D,E,TEMP;
for (t = 16; t <= 79; t++)
ctx->W[t] =
SHA_ROT(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
A = ctx->H[0];
B = ctx->H[1];
C = ctx->H[2];
D = ctx->H[3];
E = ctx->H[4];
for (t = 0; t <= 19; t++) {
TEMP = SHA_ROT(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999;
E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
}
for (t = 20; t <= 39; t++) {
TEMP = SHA_ROT(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1;
E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
}
for (t = 40; t <= 59; t++) {
TEMP = SHA_ROT(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdc;
E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
}
for (t = 60; t <= 79; t++) {
TEMP = SHA_ROT(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6;
E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
}
ctx->H[0] += A;
ctx->H[1] += B;
ctx->H[2] += C;
ctx->H[3] += D;
ctx->H[4] += E;
}
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is SHA 180-1 Header File
*
* The Initial Developer of the Original Code is Paul Kocher of
* Cryptography Research. Portions created by Paul Kocher are
* Copyright (C) 1995-9 by Cryptography Research, Inc. All
* Rights Reserved.
*
* Contributor(s):
*
* Paul Kocher
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License, version 2, (the "GPL")
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* If you wish to allow use of your version of this file only under
* the terms of the GPL and not to allow others to use your version
* of this file under the MPL, indicate your decision by deleting
* the provisions above and replace them with the notice and other
* provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file
* under either the MPL or the GPL.
*/
typedef struct {
unsigned int H[5];
unsigned int W[80];
int lenW;
unsigned int sizeHi,sizeLo;
} SHA_CTX;
void SHA1_Init(SHA_CTX *ctx);
void SHA1_Update(SHA_CTX *ctx, const void *dataIn, int len);
void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment