3 * The sha1 hash function.
4 * Defined by http://www.itl.nist.gov/fipspubs/fip180-1.htm.
7 /* nettle, low-level cryptographics library
9 * Copyright (C) 2001 Peter Gutmann, Andrew Kuchling, Niels Möller
11 * The nettle library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or (at your
14 * option) any later version.
16 * The nettle library is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 * License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with the nettle library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 /* Here's the first paragraph of Peter Gutmann's posting,
28 * <30ajo5$oe8@ccu2.auckland.ac.nz>:
30 * The following is my SHA (FIPS 180) code updated to allow use of the "fixed"
31 * SHA, thanks to Jim Gillogly and an anonymous contributor for the information on
32 * what's changed in the new version. The fix is a simple change which involves
33 * adding a single rotate in the initial expansion function. It is unknown
34 * whether this is an optimal solution to the problem which was discovered in the
35 * SHA or whether it's simply a bandaid which fixes the problem with a minimum of
36 * effort (for example the reengineering of a great many Capstone chips).
50 #include "nettle-write.h"
52 /* A block, treated as a sequence of 32-bit words. */
53 #define SHA1_DATA_LENGTH 16
55 /* SHA initial values */
57 #define h0init 0x67452301L
58 #define h1init 0xEFCDAB89L
59 #define h2init 0x98BADCFEL
60 #define h3init 0x10325476L
61 #define h4init 0xC3D2E1F0L
63 /* Initialize the SHA values */
66 sha1_init(struct sha1_ctx *ctx)
68 /* Set the h-vars to their initial values */
69 ctx->digest[ 0 ] = h0init;
70 ctx->digest[ 1 ] = h1init;
71 ctx->digest[ 2 ] = h2init;
72 ctx->digest[ 3 ] = h3init;
73 ctx->digest[ 4 ] = h4init;
75 /* Initialize bit count */
76 ctx->count_low = ctx->count_high = 0;
78 /* Initialize buffer */
82 #define SHA1_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low)
85 sha1_update(struct sha1_ctx *ctx,
86 unsigned length, const uint8_t *buffer)
89 { /* Try to fill partial block */
90 unsigned left = SHA1_DATA_SIZE - ctx->index;
93 memcpy(ctx->block + ctx->index, buffer, length);
95 return; /* Finished */
99 memcpy(ctx->block + ctx->index, buffer, left);
101 _nettle_sha1_compress(ctx->digest, ctx->block);
108 while (length >= SHA1_DATA_SIZE)
110 _nettle_sha1_compress(ctx->digest, buffer);
113 buffer += SHA1_DATA_SIZE;
114 length -= SHA1_DATA_SIZE;
116 if ((ctx->index = length)) /* This assignment is intended */
117 /* Buffer leftovers */
118 memcpy(ctx->block, buffer, length);
121 /* Final wrapup - pad to SHA1_DATA_SIZE-byte boundary with the bit pattern
122 1 0* (64-bit count of bits processed, MSB-first) */
125 sha1_final(struct sha1_ctx *ctx)
127 uint32_t bitcount_high;
128 uint32_t bitcount_low;
133 /* Set the first char of padding to 0x80. This is safe since there is
134 always at least one byte free */
136 assert(i < SHA1_DATA_SIZE);
137 ctx->block[i++] = 0x80;
139 if (i > (SHA1_DATA_SIZE - 8))
140 { /* No room for length in this block. Process it and
141 pad with another one */
142 memset(ctx->block + i, 0, SHA1_DATA_SIZE - i);
144 _nettle_sha1_compress(ctx->digest, ctx->block);
147 if (i < (SHA1_DATA_SIZE - 8))
148 memset(ctx->block + i, 0, (SHA1_DATA_SIZE - 8) - i);
150 /* There are 512 = 2^9 bits in one block */
151 bitcount_high = (ctx->count_high << 9) | (ctx->count_low >> 23);
152 bitcount_low = (ctx->count_low << 9) | (ctx->index << 3);
154 /* This is slightly inefficient, as the numbers are converted to
155 big-endian format, and will be converted back by the compression
156 function. It's probably not worth the effort to fix this. */
157 WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 8), bitcount_high);
158 WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 4), bitcount_low);
160 _nettle_sha1_compress(ctx->digest, ctx->block);
164 sha1_digest(struct sha1_ctx *ctx,
168 assert(length <= SHA1_DIGEST_SIZE);
171 _nettle_write_be32(length, digest, ctx->digest);