sha1.c revision c1252a5812eb11fcb81508b9ed37597a5bc84100
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* $KAME: sha1.c,v 1.5 2000/11/08 06:13:08 itojun Exp $ */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * All rights reserved.
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * Redistribution and use in source and binary forms, with or without
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes * modification, are permitted provided that the following conditions
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 * 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 * 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/* sanity check */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/* constant table */
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesstatic u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
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#define S(n, x) (((x) << (n)) | ((x) >> (32 - n)))
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define PUTBYTE(x) { \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes#define PUTPAD(x) { \
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes u_int32_t a, b, c, d, e;
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 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 s = t & 0x0f;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes s = t & 0x0f;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes s = t & 0x0f;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(0) = H(0) + a;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes/*------------------------------------------------------------*/
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes H(0) = 0x67452301;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes copysiz = (gaplen < len - off) ? gaplen : len - off;
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes memmove(&ctxt->m.b8[gapstart], &input[off], copysiz);
void *digest0;
const void *data;