c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/* $KAME: sha1.c,v 1.5 2000/11/08 06:13:08 itojun Exp $ */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/*
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen * All rights reserved.
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen *
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 * are met:
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 *
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 */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/*
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 */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen#include "lib.h"
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include "sha1.h"
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#include "safe-memset.h"
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/* constant table */
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic uint32_t SHA1_K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen#define K(t) SHA1_K[(t) / 20]
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d)))
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define F1(b, c, d) (((b) ^ (c)) ^ (d))
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define F2(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define F3(b, c, d) (((b) ^ (c)) ^ (d))
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define S(n, x) (((x) << (n)) | ((x) >> (32 - n)))
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define H(n) (ctxt->h.b32[(n)])
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define COUNT (ctxt->count)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define BCOUNT (ctxt->c.b64[0] / 8)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define W(n) (ctxt->m.b32[(n)])
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define PUTBYTE(x) { \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[(COUNT % 64)] = (x); \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT++; \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT %= 64; \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->c.b64[0] += 8; \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (COUNT % 64 == 0) \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sha1_step(ctxt); \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#define PUTPAD(x) { \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->m.b8[(COUNT % 64)] = (x); \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT++; \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT %= 64; \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (COUNT % 64 == 0) \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sha1_step(ctxt); \
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenstatic void sha1_step(struct sha1_ctxt *);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
21484e16293b2fb12f026d2ad533fa32ebebf641Timo Sirainenstatic void ATTR_UNSIGNED_WRAPS
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainensha1_step(struct sha1_ctxt *ctxt)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen uint32_t a, b, c, d, e;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t t, s;
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen uint32_t tmp;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
d61f31663f3debf9e49d1e0d8ef3c9fb13aad6c4Martti Rannanjärvi#ifndef WORDS_BIGENDIAN
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct sha1_ctxt tctxt;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen memmove(&tctxt.m.b8[0], &ctxt->m.b8[0], 64);
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#endif
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen a = H(0); b = H(1); c = H(2); d = H(3); e = H(4);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
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 }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen e = d; d = c; c = S(30, b); b = a; a = tmp;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen for (t = 20; t < 40; 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 e = d; d = c; c = S(30, b); b = a; a = tmp;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen for (t = 40; t < 60; 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 e = d; d = c; c = S(30, b); b = a; a = tmp;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen for (t = 60; t < 80; 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 e = d; d = c; c = S(30, b); b = a; a = tmp;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(0) = H(0) + a;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(1) = H(1) + b;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(2) = H(2) + c;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(3) = H(3) + d;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(4) = H(4) + e;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen memset(&ctxt->m.b8[0], 0, 64);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen/*------------------------------------------------------------*/
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenvoid
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainensha1_init(struct sha1_ctxt *ctxt)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen memset(ctxt, 0, sizeof(struct sha1_ctxt));
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(0) = 0x67452301;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(1) = 0xefcdab89;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(2) = 0x98badcfe;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(3) = 0x10325476;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen H(4) = 0xc3d2e1f0;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenvoid
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainensha1_pad(struct sha1_ctxt *ctxt)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t padlen; /*pad length in bytes*/
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t padstart;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(0x80);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen padstart = COUNT % 64;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen padlen = 64 - padstart;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (padlen < 8) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen memset(&ctxt->m.b8[padstart], 0, padlen);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT += padlen;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT %= 64;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sha1_step(ctxt);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen padstart = COUNT % 64; /* should be 0 */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen padlen = 64 - padstart; /* should be 64 */
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen memset(&ctxt->m.b8[padstart], 0, padlen - 8);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT += (padlen - 8);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT %= 64;
d61f31663f3debf9e49d1e0d8ef3c9fb13aad6c4Martti Rannanjärvi#ifdef WORDS_BIGENDIAN
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(ctxt->c.b8[0]); PUTPAD(ctxt->c.b8[1]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[3]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[5]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[7]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#else
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(ctxt->c.b8[7]); PUTPAD(ctxt->c.b8[6]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[4]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[2]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[0]);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#endif
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenvoid
77a8c99da71844aaf0fa3036960473024d19f471Timo Sirainensha1_loop(struct sha1_ctxt *ctxt, const void *input, size_t len)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
77a8c99da71844aaf0fa3036960473024d19f471Timo Sirainen const unsigned char *input_c = input;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t gaplen;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t gapstart;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t off;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen size_t copysiz;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen off = 0;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen while (off < len) {
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen gapstart = COUNT % 64;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen gaplen = 64 - gapstart;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen copysiz = (gaplen < len - off) ? gaplen : len - off;
77a8c99da71844aaf0fa3036960473024d19f471Timo Sirainen memmove(&ctxt->m.b8[gapstart], &input_c[off], copysiz);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT += copysiz;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen COUNT %= 64;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen ctxt->c.b64[0] += copysiz * 8;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen if (COUNT % 64 == 0)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sha1_step(ctxt);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen off += copysiz;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen }
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainenvoid
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainensha1_result(struct sha1_ctxt *ctxt, void *digest0)
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen uint8_t *digest;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainen digest = (uint8_t *)digest0;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sha1_pad(ctxt);
debc93e622751c6c09e8105e504c5833f1ca0d6dMartti Rannanjärvi#ifdef WORDS_BIGENDIAN
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen memmove(digest, &ctxt->h.b8[0], 20);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen#else
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#endif
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen safe_memset(ctxt, 0, sizeof(struct sha1_ctxt));
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
01f54478a7c69b88ab13840c99bbab19a0d7d754Timo Sirainenvoid sha1_get_digest(const void *data, size_t size,
9625595c47c665f5aee57ebfcb1fcbe9ad1bf3a0Martti Rannanjärvi unsigned char result[STATIC_ARRAY SHA1_RESULTLEN])
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen{
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen struct sha1_ctxt ctx;
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sha1_init(&ctx);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sha1_loop(&ctx, data, size);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen sha1_result(&ctx, result);
c1252a5812eb11fcb81508b9ed37597a5bc84100Timo Sirainen}
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenstatic void hash_method_init_sha1(void *context)
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen{
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen sha1_init(context);
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen}
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenstatic void hash_method_loop_sha1(void *context, const void *data, size_t size)
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen{
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen sha1_loop(context, data, size);
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen}
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenstatic void hash_method_result_sha1(void *context, unsigned char *result_r)
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen{
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen sha1_result(context, result_r);
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen}
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenconst struct hash_method hash_method_sha1 = {
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen "sha1",
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen sizeof(struct sha1_ctxt),
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen SHA1_RESULTLEN,
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen hash_method_init_sha1,
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen hash_method_loop_sha1,
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen hash_method_result_sha1
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen};