c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/* $KAME: sha1.c,v 1.5 2000/11/08 06:13:08 itojun Exp $ */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * All rights reserved.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * Redistribution and use in source and binary forms, with or without
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * modification, are permitted provided that the following conditions
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 1. Redistributions of source code must retain the above copyright
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * notice, this list of conditions and the following disclaimer.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 2. Redistributions in binary form must reproduce the above copyright
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * notice, this list of conditions and the following disclaimer in the
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * documentation and/or other materials provided with the distribution.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * 3. Neither the name of the project nor the names of its contributors
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * may be used to endorse or promote products derived from this software
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * without specific prior written permission.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * SUCH DAMAGE.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * FIPS pub 180-1: Secure Hash Algorithm (SHA-1)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * based on: http://csrc.nist.gov/fips/fip180-1.txt
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * implemented by Jun-ichiro itojun Itoh <itojun@itojun.org>
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/* constant table */
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic uint32_t SHA1_K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d)))
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define F2(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define S(n, x) (((x) << (n)) | ((x) >> (32 - n)))
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen uint32_t a, b, c, d, e;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[0] = tctxt.m.b8[3]; ctxt->m.b8[1] = tctxt.m.b8[2];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[2] = tctxt.m.b8[1]; ctxt->m.b8[3] = tctxt.m.b8[0];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[4] = tctxt.m.b8[7]; ctxt->m.b8[5] = tctxt.m.b8[6];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[6] = tctxt.m.b8[5]; ctxt->m.b8[7] = tctxt.m.b8[4];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[8] = tctxt.m.b8[11]; ctxt->m.b8[9] = tctxt.m.b8[10];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[10] = tctxt.m.b8[9]; ctxt->m.b8[11] = tctxt.m.b8[8];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[12] = tctxt.m.b8[15]; ctxt->m.b8[13] = tctxt.m.b8[14];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[14] = tctxt.m.b8[13]; ctxt->m.b8[15] = tctxt.m.b8[12];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[16] = tctxt.m.b8[19]; ctxt->m.b8[17] = tctxt.m.b8[18];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[18] = tctxt.m.b8[17]; ctxt->m.b8[19] = tctxt.m.b8[16];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[20] = tctxt.m.b8[23]; ctxt->m.b8[21] = tctxt.m.b8[22];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[22] = tctxt.m.b8[21]; ctxt->m.b8[23] = tctxt.m.b8[20];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[24] = tctxt.m.b8[27]; ctxt->m.b8[25] = tctxt.m.b8[26];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[26] = tctxt.m.b8[25]; ctxt->m.b8[27] = tctxt.m.b8[24];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[28] = tctxt.m.b8[31]; ctxt->m.b8[29] = tctxt.m.b8[30];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[30] = tctxt.m.b8[29]; ctxt->m.b8[31] = tctxt.m.b8[28];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[32] = tctxt.m.b8[35]; ctxt->m.b8[33] = tctxt.m.b8[34];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[34] = tctxt.m.b8[33]; ctxt->m.b8[35] = tctxt.m.b8[32];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[36] = tctxt.m.b8[39]; ctxt->m.b8[37] = tctxt.m.b8[38];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[38] = tctxt.m.b8[37]; ctxt->m.b8[39] = tctxt.m.b8[36];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[40] = tctxt.m.b8[43]; ctxt->m.b8[41] = tctxt.m.b8[42];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[42] = tctxt.m.b8[41]; ctxt->m.b8[43] = tctxt.m.b8[40];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[44] = tctxt.m.b8[47]; ctxt->m.b8[45] = tctxt.m.b8[46];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[46] = tctxt.m.b8[45]; ctxt->m.b8[47] = tctxt.m.b8[44];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[48] = tctxt.m.b8[51]; ctxt->m.b8[49] = tctxt.m.b8[50];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[50] = tctxt.m.b8[49]; ctxt->m.b8[51] = tctxt.m.b8[48];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[52] = tctxt.m.b8[55]; ctxt->m.b8[53] = tctxt.m.b8[54];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[54] = tctxt.m.b8[53]; ctxt->m.b8[55] = tctxt.m.b8[52];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[56] = tctxt.m.b8[59]; ctxt->m.b8[57] = tctxt.m.b8[58];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[58] = tctxt.m.b8[57]; ctxt->m.b8[59] = tctxt.m.b8[56];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[60] = tctxt.m.b8[63]; ctxt->m.b8[61] = tctxt.m.b8[62];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[62] = tctxt.m.b8[61]; ctxt->m.b8[63] = tctxt.m.b8[60];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen a = H(0); b = H(1); c = H(2); d = H(3); e = H(4);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen for (t = 0; t < 20; t++) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen s = t & 0x0f;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (t >= 16) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen s = t & 0x0f;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen tmp = S(5, a) + F1(b, c, d) + e + W(s) + K(t);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen s = t & 0x0f;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen tmp = S(5, a) + F2(b, c, d) + e + W(s) + K(t);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen s = t & 0x0f;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s));
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen tmp = S(5, a) + F3(b, c, d) + e + W(s) + K(t);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(0) = H(0) + a;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/*------------------------------------------------------------*/
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(0) = 0x67452301;
77a8c99da71844aaf0fa3036960473024d19f471Timo Sirainensha1_loop(struct sha1_ctxt *ctxt, const void *input, size_t len)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen copysiz = (gaplen < len - off) ? gaplen : len - off;
77a8c99da71844aaf0fa3036960473024d19f471Timo Sirainen memmove(&ctxt->m.b8[gapstart], &input_c[off], copysiz);
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainensha1_result(struct sha1_ctxt *ctxt, void *digest0)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[0] = ctxt->h.b8[3]; digest[1] = ctxt->h.b8[2];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[2] = ctxt->h.b8[1]; digest[3] = ctxt->h.b8[0];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[4] = ctxt->h.b8[7]; digest[5] = ctxt->h.b8[6];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[6] = ctxt->h.b8[5]; digest[7] = ctxt->h.b8[4];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[8] = ctxt->h.b8[11]; digest[9] = ctxt->h.b8[10];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[10] = ctxt->h.b8[9]; digest[11] = ctxt->h.b8[8];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[12] = ctxt->h.b8[15]; digest[13] = ctxt->h.b8[14];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[14] = ctxt->h.b8[13]; digest[15] = ctxt->h.b8[12];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[16] = ctxt->h.b8[19]; digest[17] = ctxt->h.b8[18];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen digest[18] = ctxt->h.b8[17]; digest[19] = ctxt->h.b8[16];
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen safe_memset(ctxt, 0, sizeof(struct sha1_ctxt));
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainenvoid sha1_get_digest(const void *data, size_t size,
9625595c47c665f5aee57ebfcb1fcbe9ad1bf3a0Martti Rannanjärvi unsigned char result[STATIC_ARRAY SHA1_RESULTLEN])
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenstatic void hash_method_init_sha1(void *context)
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenstatic void hash_method_loop_sha1(void *context, const void *data, size_t size)
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenstatic void hash_method_result_sha1(void *context, unsigned char *result_r)
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen sizeof(struct sha1_ctxt),