/src/nettle-with-libgmp/sha1.c
Line  | Count  | Source  | 
1  |  | /* sha1.c  | 
2  |  |  | 
3  |  |    The sha1 hash function.  | 
4  |  |    Defined by http://www.itl.nist.gov/fipspubs/fip180-1.htm.  | 
5  |  |  | 
6  |  |    Copyright (C) 2001, 2013 Niels Möller  | 
7  |  |  | 
8  |  |    This file is part of GNU Nettle.  | 
9  |  |  | 
10  |  |    GNU Nettle is free software: you can redistribute it and/or  | 
11  |  |    modify it under the terms of either:  | 
12  |  |  | 
13  |  |      * the GNU Lesser General Public License as published by the Free  | 
14  |  |        Software Foundation; either version 3 of the License, or (at your  | 
15  |  |        option) any later version.  | 
16  |  |  | 
17  |  |    or  | 
18  |  |  | 
19  |  |      * the GNU General Public License as published by the Free  | 
20  |  |        Software Foundation; either version 2 of the License, or (at your  | 
21  |  |        option) any later version.  | 
22  |  |  | 
23  |  |    or both in parallel, as here.  | 
24  |  |  | 
25  |  |    GNU Nettle is distributed in the hope that it will be useful,  | 
26  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
27  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
28  |  |    General Public License for more details.  | 
29  |  |  | 
30  |  |    You should have received copies of the GNU General Public License and  | 
31  |  |    the GNU Lesser General Public License along with this program.  If  | 
32  |  |    not, see http://www.gnu.org/licenses/.  | 
33  |  | */  | 
34  |  |  | 
35  |  | #if HAVE_CONFIG_H  | 
36  |  | # include "config.h"  | 
37  |  | #endif  | 
38  |  |  | 
39  |  | #include <assert.h>  | 
40  |  | #include <stdlib.h>  | 
41  |  | #include <string.h>  | 
42  |  |  | 
43  |  | #include "sha1.h"  | 
44  |  |  | 
45  |  | #include "macros.h"  | 
46  |  | #include "nettle-write.h"  | 
47  |  |  | 
48  |  | /* Initialize the SHA values */  | 
49  |  | void  | 
50  |  | sha1_init(struct sha1_ctx *ctx)  | 
51  | 64.5k  | { | 
52  |  |   /* FIXME: Put the buffer last in the struct, and arrange so that we  | 
53  |  |      can initialize with a single memcpy. */  | 
54  | 64.5k  |   static const uint32_t iv[_SHA1_DIGEST_LENGTH] =   | 
55  | 64.5k  |     { | 
56  |  |       /* SHA initial values, first 4 identical to md5's. */  | 
57  | 64.5k  |       0x67452301L,  | 
58  | 64.5k  |       0xEFCDAB89L,  | 
59  | 64.5k  |       0x98BADCFEL,  | 
60  | 64.5k  |       0x10325476L,  | 
61  | 64.5k  |       0xC3D2E1F0L,  | 
62  | 64.5k  |     };  | 
63  |  |  | 
64  | 64.5k  |   memcpy(ctx->state, iv, sizeof(ctx->state));  | 
65  | 64.5k  |   ctx->count = 0;  | 
66  |  |     | 
67  |  |   /* Initialize buffer */  | 
68  | 64.5k  |   ctx->index = 0;  | 
69  | 64.5k  | }  | 
70  |  |  | 
71  | 14.6k  | #define COMPRESS(ctx, data) (nettle_sha1_compress((ctx)->state, data))  | 
72  |  |  | 
73  |  | void  | 
74  |  | sha1_update(struct sha1_ctx *ctx,  | 
75  |  |       size_t length, const uint8_t *data)  | 
76  | 114k  | { | 
77  | 114k  |   MD_UPDATE (ctx, length, data, COMPRESS, ctx->count++);  | 
78  | 114k  | }  | 
79  |  |       | 
80  |  | void  | 
81  |  | sha1_digest(struct sha1_ctx *ctx,  | 
82  |  |       size_t length,  | 
83  |  |       uint8_t *digest)  | 
84  | 62.8k  | { | 
85  | 62.8k  |   uint64_t bit_count;  | 
86  |  |  | 
87  | 62.8k  |   assert(length <= SHA1_DIGEST_SIZE);  | 
88  |  |  | 
89  | 62.8k  |   MD_PAD(ctx, 8, COMPRESS);  | 
90  |  |  | 
91  |  |   /* There are 512 = 2^9 bits in one block */  | 
92  | 62.8k  |   bit_count = (ctx->count << 9) | (ctx->index << 3);  | 
93  |  |  | 
94  |  |   /* append the 64 bit count */  | 
95  | 62.8k  |   WRITE_UINT64(ctx->block + (SHA1_BLOCK_SIZE - 8), bit_count);  | 
96  | 62.8k  |   nettle_sha1_compress(ctx->state, ctx->block);  | 
97  |  |  | 
98  | 62.8k  |   _nettle_write_be32(length, digest, ctx->state);  | 
99  | 62.8k  |   sha1_init(ctx);  | 
100  | 62.8k  | }  |