Revert "Merge branch 'upstream' into tizen"
[platform/upstream/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., 51 Franklin Street, Fifth Floor, Boston,
24  * MA 02111-1301, 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 "sha1.h"
48
49 #include "macros.h"
50 #include "nettle-write.h"
51
52 /* Initialize the SHA values */
53 void
54 sha1_init(struct sha1_ctx *ctx)
55 {
56   /* FIXME: Put the buffer last in the struct, and arrange so that we
57      can initialize with a single memcpy. */
58   static const uint32_t iv[_SHA1_DIGEST_LENGTH] = 
59     {
60       /* SHA initial values */
61       0x67452301L,
62       0xEFCDAB89L,
63       0x98BADCFEL,
64       0x10325476L,
65       0xC3D2E1F0L,
66     };
67
68   memcpy(ctx->state, iv, sizeof(ctx->state));
69   ctx->count_low = ctx->count_high = 0;
70   
71   /* Initialize buffer */
72   ctx->index = 0;
73 }
74
75 #define COMPRESS(ctx, data) (_nettle_sha1_compress((ctx)->state, data))
76
77 void
78 sha1_update(struct sha1_ctx *ctx,
79             unsigned length, const uint8_t *data)
80 {
81   MD_UPDATE (ctx, length, data, COMPRESS, MD_INCR(ctx));
82 }
83           
84 void
85 sha1_digest(struct sha1_ctx *ctx,
86             unsigned length,
87             uint8_t *digest)
88 {
89   uint32_t high, low;
90
91   assert(length <= SHA1_DIGEST_SIZE);
92
93   MD_PAD(ctx, 8, COMPRESS);
94
95   /* There are 512 = 2^9 bits in one block */  
96   high = (ctx->count_high << 9) | (ctx->count_low >> 23);
97   low = (ctx->count_low << 9) | (ctx->index << 3);
98
99   /* append the 64 bit count */
100   WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 8), high);
101   WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 4), low);
102   _nettle_sha1_compress(ctx->state, ctx->block);
103
104   _nettle_write_be32(length, digest, ctx->state);
105   sha1_init(ctx);
106 }