[Title] Add packaging/nettle.spec to build nettle on OBS system
[external/nettle.git] / sha1.c
1 /* sha1.c
2  *
3  * The sha1 hash function.
4  * Defined by http://www.itl.nist.gov/fipspubs/fip180-1.htm.
5  */
6
7 /* nettle, low-level cryptographics library
8  *
9  * Copyright (C) 2001 Peter Gutmann, Andrew Kuchling, Niels Möller
10  *  
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.
15  * 
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.
20  * 
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,
24  * MA 02111-1307, USA.
25  */
26
27 /* Here's the first paragraph of Peter Gutmann's posting,
28  * <30ajo5$oe8@ccu2.auckland.ac.nz>: 
29  *
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).
37  */
38
39 #if HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 #include <assert.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "sha.h"
48
49 #include "macros.h"
50 #include "nettle-write.h"
51
52 /* A block, treated as a sequence of 32-bit words. */
53 #define SHA1_DATA_LENGTH 16
54
55 /* SHA initial values */
56
57 #define h0init  0x67452301L
58 #define h1init  0xEFCDAB89L
59 #define h2init  0x98BADCFEL
60 #define h3init  0x10325476L
61 #define h4init  0xC3D2E1F0L
62
63 /* Initialize the SHA values */
64
65 void
66 sha1_init(struct sha1_ctx *ctx)
67 {
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;
74
75   /* Initialize bit count */
76   ctx->count_low = ctx->count_high = 0;
77   
78   /* Initialize buffer */
79   ctx->index = 0;
80 }
81
82 #define SHA1_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low)
83
84 void
85 sha1_update(struct sha1_ctx *ctx,
86             unsigned length, const uint8_t *buffer)
87 {
88   if (ctx->index)
89     { /* Try to fill partial block */
90       unsigned left = SHA1_DATA_SIZE - ctx->index;
91       if (length < left)
92         {
93           memcpy(ctx->block + ctx->index, buffer, length);
94           ctx->index += length;
95           return; /* Finished */
96         }
97       else
98         {
99           memcpy(ctx->block + ctx->index, buffer, left);
100
101           _nettle_sha1_compress(ctx->digest, ctx->block);
102           SHA1_INCR(ctx);
103
104           buffer += left;
105           length -= left;
106         }
107     }
108   while (length >= SHA1_DATA_SIZE)
109     {
110       _nettle_sha1_compress(ctx->digest, buffer);
111       SHA1_INCR(ctx);
112
113       buffer += SHA1_DATA_SIZE;
114       length -= SHA1_DATA_SIZE;
115     }
116   if ((ctx->index = length))     /* This assignment is intended */
117     /* Buffer leftovers */
118     memcpy(ctx->block, buffer, length);
119 }
120           
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) */
123
124 static void
125 sha1_final(struct sha1_ctx *ctx)
126 {
127   uint32_t bitcount_high;
128   uint32_t bitcount_low;
129   unsigned i;
130   
131   i = ctx->index;
132   
133   /* Set the first char of padding to 0x80.  This is safe since there is
134      always at least one byte free */
135
136   assert(i < SHA1_DATA_SIZE);
137   ctx->block[i++] = 0x80;
138
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);
143       
144       _nettle_sha1_compress(ctx->digest, ctx->block);
145       i = 0;
146     }
147   if (i < (SHA1_DATA_SIZE - 8))
148     memset(ctx->block + i, 0, (SHA1_DATA_SIZE - 8) - i);
149
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);
153
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);
159
160   _nettle_sha1_compress(ctx->digest, ctx->block);
161 }
162
163 void
164 sha1_digest(struct sha1_ctx *ctx,
165             unsigned length,
166             uint8_t *digest)
167 {
168   assert(length <= SHA1_DIGEST_SIZE);
169
170   sha1_final(ctx);
171   _nettle_write_be32(length, digest, ctx->digest);
172   sha1_init(ctx);
173 }