]> wimlib.net Git - wimlib/blob - src/sha1.c
Rename sha1_buffer() to simply sha1()
[wimlib] / src / sha1.c
1 /*
2  * sha1.c - implementation of the Secure Hash Algorithm version 1 (FIPS 180-1)
3  *
4  * Copyright 2022 Eric Biggers
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31
32 #include "wimlib/endianness.h"
33 #include "wimlib/sha1.h"
34 #include "wimlib/unaligned.h"
35
36 /* Dummy SHA-1 message digest of all 0's.  This is used in the WIM format to
37  * mean "SHA-1 not specified".  */
38 const u8 zero_hash[20];
39
40 /*
41  * Builds a hexadecimal string representation of a SHA-1 message digest.
42  *
43  * The output buffer must be at least 41 characters.
44  */
45 void
46 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_SIZE * 2 + 1])
47 {
48         int i;
49         u8 high, low;
50
51         for (i = 0; i < SHA1_HASH_SIZE; i++) {
52                 high = hash[i] >> 4;
53                 low = hash[i] & 0xF;
54                 strbuf[i * 2 + 0] = (high < 10 ? high + '0' : high - 10 + 'a');
55                 strbuf[i * 2 + 1] = (low  < 10 ? low  + '0' : low  - 10 + 'a');
56         }
57         strbuf[i * 2] = 0;
58 }
59
60 /* If we use libcrypto (e.g. OpenSSL) then we get all the SHA-1 functions for
61  * free.  Otherwise we need to implement them ourselves.  */
62
63 #ifndef WITH_LIBCRYPTO
64
65 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
66
67 #define blk0(i) (tmp[i] = be32_to_cpu(load_be32_unaligned(&(block)[(i) * 4])))
68
69 #define blk(i) (tmp[i & 15] = rol(tmp[(i + 13) & 15] ^ \
70                                   tmp[(i +  8) & 15] ^ \
71                                   tmp[(i +  2) & 15] ^ \
72                                   tmp[(i +  0) & 15], 1))
73
74 #define R0(v, w, x, y, z, i) \
75         z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
76         w = rol(w, 30);
77
78 #define R1(v, w, x, y, z, i) \
79         z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
80         w = rol(w, 30);
81
82 #define R2(v, w, x, y, z, i) \
83         z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
84         w = rol(w, 30);
85
86 #define R3(v, w, x, y, z, i) \
87         z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
88         w = rol(w, 30);
89
90 #define R4(v, w, x, y, z, i) \
91         z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
92         w = rol(w, 30);
93
94 /* Hash a single 512-bit block. This is the core of the algorithm.  */
95 static void
96 sha1_transform_default(u32 state[5], const u8 block[64])
97 {
98         u32 a, b, c, d, e;
99         u32 tmp[16];
100
101         /* Copy ctx->state[] to working vars */
102         a = state[0];
103         b = state[1];
104         c = state[2];
105         d = state[3];
106         e = state[4];
107
108         /* 4 rounds of 20 operations each. Loop unrolled. */
109         R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
110         R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
111         R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
112         R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
113         R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
114         R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
115         R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
116         R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
117         R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
118         R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
119         R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
120         R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
121         R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
122         R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
123         R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
124         R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
125         R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
126         R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
127         R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
128         R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
129
130         /* Add the working vars back into context.state[] */
131         state[0] += a;
132         state[1] += b;
133         state[2] += c;
134         state[3] += d;
135         state[4] += e;
136 }
137
138 #ifdef ENABLE_SSSE3_SHA1
139 extern void
140 sha1_transform_blocks_ssse3(u32 state[5], const void *data, size_t num_blocks);
141 extern void
142 sha1_transform_blocks_default(u32 state[5], const void *data, size_t num_blocks);
143 #  define sha1_transform_blocks sha1_transform_blocks_ssse3
144 #else
145 #  define sha1_transform_blocks sha1_transform_blocks_default
146 #endif
147
148 #ifndef ENABLE_SSSE3_SHA1
149 static
150 #endif
151 void
152 sha1_transform_blocks_default(u32 state[5], const void *data, size_t num_blocks)
153 {
154         do {
155                 sha1_transform_default(state, data);
156                 data += 64;
157         } while (--num_blocks);
158 }
159
160 /* Initializes the specified SHA-1 context.
161  *
162  * After sha1_init(), call sha1_update() zero or more times to provide the data
163  * to be hashed.  Then call sha1_final() to get the final hash.  */
164 void
165 sha1_init(SHA_CTX *ctx)
166 {
167         ctx->bytecount = 0;
168
169         ctx->state[0] = 0x67452301;
170         ctx->state[1] = 0xEFCDAB89;
171         ctx->state[2] = 0x98BADCFE;
172         ctx->state[3] = 0x10325476;
173         ctx->state[4] = 0xC3D2E1F0;
174 }
175
176 /* Updates the SHA-1 context with @len bytes of data.  */
177 void
178 sha1_update(SHA_CTX *ctx, const void *data, size_t len)
179 {
180         unsigned buffered = ctx->bytecount & 63;
181
182         ctx->bytecount += len;
183
184         if (buffered) {
185                 /* Previous block is unfinished.  */
186                 if (len < 64 - buffered) {
187                         memcpy(&ctx->buffer[buffered], data, len);
188                         /* Previous block still unfinished.  */
189                         return;
190                 } else {
191                         memcpy(&ctx->buffer[buffered], data, 64 - buffered);
192                         /* Finished the previous block.  */
193                         sha1_transform_blocks(ctx->state, ctx->buffer, 1);
194                         data += 64 - buffered;
195                         len -= 64 - buffered;
196                 }
197         }
198
199         /* Process blocks directly from the input data.  */
200         if (len / 64) {
201                 sha1_transform_blocks(ctx->state, data, len / 64);
202                 data += len & ~63;
203                 len &= 63;
204         }
205
206         /* Copy any remaining bytes to the buffer.  */
207         if (len)
208                 memcpy(ctx->buffer, data, len);
209 }
210
211 /* Pad the message and generate the final SHA-1 message digest.  */
212 void
213 sha1_final(u8 md[20], SHA_CTX *ctx)
214 {
215         /* Logically, we must append 1 bit, then a variable number of 0 bits,
216          * then the message length in bits as a big-endian integer, so that the
217          * final length is a multiple of the block size.  */
218         static const u8 padding[64] = {0x80, };
219         be64 finalcount = cpu_to_be64(ctx->bytecount << 3);
220
221         sha1_update(ctx, padding, 64 - ((ctx->bytecount + 8) & 63));
222         sha1_update(ctx, &finalcount, 8);
223
224         for (int i = 0; i < 5; i++)
225                 store_be32_unaligned(cpu_to_be32(ctx->state[i]), &md[i * 4]);
226 }
227
228 /* Calculate the SHA-1 message digest of the given data. */
229 void
230 sha1(const void *data, size_t len, u8 hash[SHA1_HASH_SIZE])
231 {
232         SHA_CTX ctx;
233
234         sha1_init(&ctx);
235         sha1_update(&ctx, data, len);
236         sha1_final(hash, &ctx);
237 }
238
239 #endif /* !WITH_LIBCRYPTO */