1 /* vi: set sw=4 ts=4: */
3 * Based on shasum from http://www.netsw.org/crypto/hash/
4 * Majorly hacked up to use Dr Brian Gladman's sha1 code
6 * Copyright (C) 2002 Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
7 * Copyright (C) 2003 Glenn L. McGrath
8 * Copyright (C) 2003 Erik Andersen
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 * ---------------------------------------------------------------------------
13 * Issue Date: 10/11/2002
15 * This is a byte oriented version of SHA1 that operates on arrays of bytes
16 * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
29 # define SHA1_BLOCK_SIZE 64
30 # define SHA1_DIGEST_SIZE 20
31 # define SHA1_HASH_SIZE SHA1_DIGEST_SIZE
35 # define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
37 # define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
39 /* reverse byte order in 32-bit words */
40 #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
41 #define parity(x,y,z) ((x) ^ (y) ^ (z))
42 #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
44 /* A normal version as set out in the FIPS. This version uses */
45 /* partial loop unrolling and is optimised for the Pentium 4 */
47 t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
48 e = d; d = c; c = rotl32(b, 30); b = t
51 static void sha1_compile(sha1_ctx_t *ctx)
53 uint32_t w[80], i, a, b, c, d, e, t;
55 /* note that words are compiled from the buffer into 32-bit */
56 /* words in big-endian order so an order reversal is needed */
57 /* here on little endian machines */
58 for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
59 w[i] = htonl(ctx->wbuf[i]);
61 for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
62 w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
70 for (i = 0; i < 20; ++i) {
74 for (i = 20; i < 40; ++i) {
75 rnd(parity, 0x6ed9eba1);
78 for (i = 40; i < 60; ++i) {
82 for (i = 60; i < 80; ++i) {
83 rnd(parity, 0xca62c1d6);
93 void sha1_begin(sha1_ctx_t *ctx)
95 ctx->count[0] = ctx->count[1] = 0;
96 ctx->hash[0] = 0x67452301;
97 ctx->hash[1] = 0xefcdab89;
98 ctx->hash[2] = 0x98badcfe;
99 ctx->hash[3] = 0x10325476;
100 ctx->hash[4] = 0xc3d2e1f0;
103 /* SHA1 hash data in an array of bytes into hash buffer and call the */
104 /* hash_compile function as required. */
105 void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx)
107 uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK);
108 uint32_t freeb = SHA1_BLOCK_SIZE - pos;
109 const unsigned char *sp = data;
111 if ((ctx->count[0] += length) < length)
114 while (length >= freeb) { /* tranfer whole blocks while possible */
115 memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb);
118 freeb = SHA1_BLOCK_SIZE;
123 memcpy(((unsigned char *) ctx->wbuf) + pos, sp, length);
126 void *sha1_end(void *resbuf, sha1_ctx_t *ctx)
128 /* SHA1 Final padding and digest calculation */
130 static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
131 static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
133 static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
134 static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
137 uint8_t *hval = resbuf;
138 uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK);
140 /* mask out the rest of any partial 32-bit word and then set */
141 /* the next byte to 0x80. On big-endian machines any bytes in */
142 /* the buffer will be at the top end of 32 bit words, on little */
143 /* endian machines they will be at the bottom. Hence the AND */
144 /* and OR masks above are reversed for little endian systems */
145 ctx->wbuf[cnt >> 2] =
146 (ctx->wbuf[cnt >> 2] & mask[cnt & 3]) | bits[cnt & 3];
148 /* we need 9 or more empty positions, one for the padding byte */
149 /* (above) and eight for the length count. If there is not */
150 /* enough space pad and empty the buffer */
151 if (cnt > SHA1_BLOCK_SIZE - 9) {
156 } else /* compute a word index for the empty buffer positions */
157 cnt = (cnt >> 2) + 1;
159 while (cnt < 14) /* and zero pad all but last two positions */
160 ctx->wbuf[cnt++] = 0;
162 /* assemble the eight byte counter in the buffer in big-endian */
165 ctx->wbuf[14] = htonl((ctx->count[1] << 3) | (ctx->count[0] >> 29));
166 ctx->wbuf[15] = htonl(ctx->count[0] << 3);
170 /* extract the hash value as bytes in case the hash buffer is */
171 /* misaligned for 32-bit words */
173 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
174 hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3));