sha1.c revision c1252a5812eb11fcb81508b9ed37597a5bc84100
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* $KAME: sha1.c,v 1.5 2000/11/08 06:13:08 itojun Exp $ */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/*
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * All rights reserved.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes *
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Redistribution and use in source and binary forms, with or without
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * modification, are permitted provided that the following conditions
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * are met:
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * 1. Redistributions of source code must retain the above copyright
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * notice, this list of conditions and the following disclaimer.
0662ed52e814f8f08ef0e09956413a792584eddffuankg * 2. Redistributions in binary form must reproduce the above copyright
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * notice, this list of conditions and the following disclaimer in the
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * documentation and/or other materials provided with the distribution.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * 3. Neither the name of the project nor the names of its contributors
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * may be used to endorse or promote products derived from this software
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * without specific prior written permission.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes *
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16b55a35cff91315d261d1baa776138af465c4e4fuankg * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
16b55a35cff91315d261d1baa776138af465c4e4fuankg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * SUCH DAMAGE.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/*
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * FIPS pub 180-1: Secure Hash Algorithm (SHA-1)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * based on: http://csrc.nist.gov/fips/fip180-1.txt
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * implemented by Jun-ichiro itojun Itoh <itojun@itojun.org>
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#include <sys/types.h>
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#include <sys/time.h>
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#include <string.h>
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#include "sha1.h"
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#include "safe-memset.h"
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* sanity check */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#if BYTE_ORDER != BIG_ENDIAN
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes# if BYTE_ORDER != LITTLE_ENDIAN
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes# define unsupported 1
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes# endif
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#endif
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#ifndef unsupported
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* constant table */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesstatic u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define K(t) _K[(t) / 20]
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d)))
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg#define F1(b, c, d) (((b) ^ (c)) ^ (d))
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define F2(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define F3(b, c, d) (((b) ^ (c)) ^ (d))
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define S(n, x) (((x) << (n)) | ((x) >> (32 - n)))
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define H(n) (ctxt->h.b32[(n)])
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define COUNT (ctxt->count)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define BCOUNT (ctxt->c.b64[0] / 8)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define W(n) (ctxt->m.b32[(n)])
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define PUTBYTE(x) { \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[(COUNT % 64)] = (x); \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT++; \
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg COUNT %= 64; \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->c.b64[0] += 8; \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (COUNT % 64 == 0) \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes sha1_step(ctxt); \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define PUTPAD(x) { \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[(COUNT % 64)] = (x); \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT++; \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT %= 64; \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (COUNT % 64 == 0) \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes sha1_step(ctxt); \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesstatic void sha1_step(struct sha1_ctxt *);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesstatic void
bb2b38cd44b032118359afbc743efbea12f48e61bnicholessha1_step(ctxt)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes struct sha1_ctxt *ctxt;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes{
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes u_int32_t a, b, c, d, e;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes size_t t, s;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg u_int32_t tmp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#if BYTE_ORDER == LITTLE_ENDIAN
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg struct sha1_ctxt tctxt;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes memmove(&tctxt.m.b8[0], &ctxt->m.b8[0], 64);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[0] = tctxt.m.b8[3]; ctxt->m.b8[1] = tctxt.m.b8[2];
0a39e7683f6611d66c55712f50bb240428d832a1bnicholes ctxt->m.b8[2] = tctxt.m.b8[1]; ctxt->m.b8[3] = tctxt.m.b8[0];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[4] = tctxt.m.b8[7]; ctxt->m.b8[5] = tctxt.m.b8[6];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[6] = tctxt.m.b8[5]; ctxt->m.b8[7] = tctxt.m.b8[4];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[8] = tctxt.m.b8[11]; ctxt->m.b8[9] = tctxt.m.b8[10];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[10] = tctxt.m.b8[9]; ctxt->m.b8[11] = tctxt.m.b8[8];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[12] = tctxt.m.b8[15]; ctxt->m.b8[13] = tctxt.m.b8[14];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[14] = tctxt.m.b8[13]; ctxt->m.b8[15] = tctxt.m.b8[12];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[16] = tctxt.m.b8[19]; ctxt->m.b8[17] = tctxt.m.b8[18];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[18] = tctxt.m.b8[17]; ctxt->m.b8[19] = tctxt.m.b8[16];
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg ctxt->m.b8[20] = tctxt.m.b8[23]; ctxt->m.b8[21] = tctxt.m.b8[22];
0662ed52e814f8f08ef0e09956413a792584eddffuankg ctxt->m.b8[22] = tctxt.m.b8[21]; ctxt->m.b8[23] = tctxt.m.b8[20];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[24] = tctxt.m.b8[27]; ctxt->m.b8[25] = tctxt.m.b8[26];
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg ctxt->m.b8[26] = tctxt.m.b8[25]; ctxt->m.b8[27] = tctxt.m.b8[24];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[28] = tctxt.m.b8[31]; ctxt->m.b8[29] = tctxt.m.b8[30];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[30] = tctxt.m.b8[29]; ctxt->m.b8[31] = tctxt.m.b8[28];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[32] = tctxt.m.b8[35]; ctxt->m.b8[33] = tctxt.m.b8[34];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[34] = tctxt.m.b8[33]; ctxt->m.b8[35] = tctxt.m.b8[32];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[36] = tctxt.m.b8[39]; ctxt->m.b8[37] = tctxt.m.b8[38];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[38] = tctxt.m.b8[37]; ctxt->m.b8[39] = tctxt.m.b8[36];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[40] = tctxt.m.b8[43]; ctxt->m.b8[41] = tctxt.m.b8[42];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[42] = tctxt.m.b8[41]; ctxt->m.b8[43] = tctxt.m.b8[40];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[44] = tctxt.m.b8[47]; ctxt->m.b8[45] = tctxt.m.b8[46];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[46] = tctxt.m.b8[45]; ctxt->m.b8[47] = tctxt.m.b8[44];
0662ed52e814f8f08ef0e09956413a792584eddffuankg ctxt->m.b8[48] = tctxt.m.b8[51]; ctxt->m.b8[49] = tctxt.m.b8[50];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[50] = tctxt.m.b8[49]; ctxt->m.b8[51] = tctxt.m.b8[48];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[52] = tctxt.m.b8[55]; ctxt->m.b8[53] = tctxt.m.b8[54];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[54] = tctxt.m.b8[53]; ctxt->m.b8[55] = tctxt.m.b8[52];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[56] = tctxt.m.b8[59]; ctxt->m.b8[57] = tctxt.m.b8[58];
0662ed52e814f8f08ef0e09956413a792584eddffuankg ctxt->m.b8[58] = tctxt.m.b8[57]; ctxt->m.b8[59] = tctxt.m.b8[56];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[60] = tctxt.m.b8[63]; ctxt->m.b8[61] = tctxt.m.b8[62];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->m.b8[62] = tctxt.m.b8[61]; ctxt->m.b8[63] = tctxt.m.b8[60];
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#endif
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes a = H(0); b = H(1); c = H(2); d = H(3); e = H(4);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes for (t = 0; t < 20; t++) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes s = t & 0x0f;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (t >= 16) {
0662ed52e814f8f08ef0e09956413a792584eddffuankg W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg e = d; d = c; c = S(30, b); b = a; a = tmp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes for (t = 20; t < 40; t++) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes s = t & 0x0f;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes tmp = S(5, a) + F1(b, c, d) + e + W(s) + K(t);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes e = d; d = c; c = S(30, b); b = a; a = tmp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes for (t = 40; t < 60; t++) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes s = t & 0x0f;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes tmp = S(5, a) + F2(b, c, d) + e + W(s) + K(t);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes e = d; d = c; c = S(30, b); b = a; a = tmp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes for (t = 60; t < 80; t++) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes s = t & 0x0f;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes tmp = S(5, a) + F3(b, c, d) + e + W(s) + K(t);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes e = d; d = c; c = S(30, b); b = a; a = tmp;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(0) = H(0) + a;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(1) = H(1) + b;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(2) = H(2) + c;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(3) = H(3) + d;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(4) = H(4) + e;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes memset(&ctxt->m.b8[0], 0, 64);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes}
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/*------------------------------------------------------------*/
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
ac7985784d08a3655291f24f711812b4d8b1cbcffuankgvoid
bb2b38cd44b032118359afbc743efbea12f48e61bnicholessha1_init(ctxt)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes struct sha1_ctxt *ctxt;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes{
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes memset(ctxt, 0, sizeof(struct sha1_ctxt));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(0) = 0x67452301;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(1) = 0xefcdab89;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(2) = 0x98badcfe;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(3) = 0x10325476;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(4) = 0xc3d2e1f0;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes}
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesvoid
bb2b38cd44b032118359afbc743efbea12f48e61bnicholessha1_pad(ctxt)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes struct sha1_ctxt *ctxt;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes{
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg size_t padlen; /*pad length in bytes*/
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes size_t padstart;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes PUTPAD(0x80);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes padstart = COUNT % 64;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes padlen = 64 - padstart;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (padlen < 8) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes memset(&ctxt->m.b8[padstart], 0, padlen);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT += padlen;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT %= 64;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes sha1_step(ctxt);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes padstart = COUNT % 64; /* should be 0 */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes padlen = 64 - padstart; /* should be 64 */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes memset(&ctxt->m.b8[padstart], 0, padlen - 8);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT += (padlen - 8);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT %= 64;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#if BYTE_ORDER == BIG_ENDIAN
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg PUTPAD(ctxt->c.b8[0]); PUTPAD(ctxt->c.b8[1]);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[3]);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[5]);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[7]);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#else
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes PUTPAD(ctxt->c.b8[7]); PUTPAD(ctxt->c.b8[6]);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[4]);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[2]);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[0]);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#endif
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes}
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesvoid
ac7985784d08a3655291f24f711812b4d8b1cbcffuankgsha1_loop(ctxt, input, len)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes struct sha1_ctxt *ctxt;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes const u_int8_t *input;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes size_t len;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes{
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes size_t gaplen;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes size_t gapstart;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes size_t off;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes size_t copysiz;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes off = 0;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
0662ed52e814f8f08ef0e09956413a792584eddffuankg while (off < len) {
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes gapstart = COUNT % 64;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes gaplen = 64 - gapstart;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes copysiz = (gaplen < len - off) ? gaplen : len - off;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes memmove(&ctxt->m.b8[gapstart], &input[off], copysiz);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT += copysiz;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes COUNT %= 64;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ctxt->c.b64[0] += copysiz * 8;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes if (COUNT % 64 == 0)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes sha1_step(ctxt);
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes off += copysiz;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes }
0662ed52e814f8f08ef0e09956413a792584eddffuankg}
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
ac7985784d08a3655291f24f711812b4d8b1cbcffuankgvoid
sha1_result(ctxt, digest0)
struct sha1_ctxt *ctxt;
void *digest0;
{
u_int8_t *digest;
digest = (u_int8_t *)digest0;
sha1_pad(ctxt);
#if BYTE_ORDER == BIG_ENDIAN
memmove(digest, &ctxt->h.b8[0], 20);
#else
digest[0] = ctxt->h.b8[3]; digest[1] = ctxt->h.b8[2];
digest[2] = ctxt->h.b8[1]; digest[3] = ctxt->h.b8[0];
digest[4] = ctxt->h.b8[7]; digest[5] = ctxt->h.b8[6];
digest[6] = ctxt->h.b8[5]; digest[7] = ctxt->h.b8[4];
digest[8] = ctxt->h.b8[11]; digest[9] = ctxt->h.b8[10];
digest[10] = ctxt->h.b8[9]; digest[11] = ctxt->h.b8[8];
digest[12] = ctxt->h.b8[15]; digest[13] = ctxt->h.b8[14];
digest[14] = ctxt->h.b8[13]; digest[15] = ctxt->h.b8[12];
digest[16] = ctxt->h.b8[19]; digest[17] = ctxt->h.b8[18];
digest[18] = ctxt->h.b8[17]; digest[19] = ctxt->h.b8[16];
#endif
safe_memset(ctxt, 0, sizeof(struct sha1_ctxt));
}
void sha1_get_digest(data, size, result)
const void *data;
size_t size;
unsigned char result[SHA1_RESULTLEN];
{
struct sha1_ctxt ctx;
sha1_init(&ctx);
sha1_loop(&ctx, data, size);
sha1_result(&ctx, result);
}
#endif /*unsupported*/