]> wimlib.net Git - wimlib/blob - src/sha1.c
bitops: rename bit scan functions
[wimlib] / src / sha1.c
1 /*
2  * sha1.c - implementation of the Secure Hash Algorithm version 1 (FIPS 180-1)
3  *
4  * The following copying information applies to this specific source code file:
5  *
6  * Written in 2014-2015 by Eric Biggers <ebiggers3@gmail.com>
7  *
8  * To the extent possible under law, the author(s) have dedicated all copyright
9  * and related and neighboring rights to this software to the public domain
10  * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
11  * Dedication (the "CC0").
12  *
13  * This software is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
16  *
17  * You should have received a copy of the CC0 along with this software; if not
18  * see <http://creativecommons.org/publicdomain/zero/1.0/>.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include "wimlib/endianness.h"
26 #include "wimlib/sha1.h"
27 #include "wimlib/unaligned.h"
28
29 /* Dummy SHA-1 message digest of all 0's.  This is used in the WIM format to
30  * mean "SHA-1 not specified".  */
31 const u8 zero_hash[20];
32
33 /*
34  * Builds a hexadecimal string representation of a SHA-1 message digest.
35  *
36  * The output buffer must be at least 41 characters.
37  */
38 void
39 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_SIZE * 2 + 1])
40 {
41         int i;
42         u8 high, low;
43
44         for (i = 0; i < SHA1_HASH_SIZE; i++) {
45                 high = hash[i] >> 4;
46                 low = hash[i] & 0xF;
47                 strbuf[i * 2 + 0] = (high < 10 ? high + '0' : high - 10 + 'a');
48                 strbuf[i * 2 + 1] = (low  < 10 ? low  + '0' : low  - 10 + 'a');
49         }
50         strbuf[i * 2] = 0;
51 }
52
53 /* If we use libcrypto (e.g. OpenSSL) then we get all the SHA-1 functions for
54  * free.  Otherwise we need to implement them ourselves.  */
55
56 #ifndef WITH_LIBCRYPTO
57
58 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
59
60 #define blk0(i) (tmp[i] = be32_to_cpu(load_be32_unaligned(&(block)[(i) * 4])))
61
62 #define blk(i) (tmp[i & 15] = rol(tmp[(i + 13) & 15] ^ \
63                                   tmp[(i +  8) & 15] ^ \
64                                   tmp[(i +  2) & 15] ^ \
65                                   tmp[(i +  0) & 15], 1))
66
67 #define R0(v, w, x, y, z, i) \
68         z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
69         w = rol(w, 30);
70
71 #define R1(v, w, x, y, z, i) \
72         z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
73         w = rol(w, 30);
74
75 #define R2(v, w, x, y, z, i) \
76         z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
77         w = rol(w, 30);
78
79 #define R3(v, w, x, y, z, i) \
80         z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
81         w = rol(w, 30);
82
83 #define R4(v, w, x, y, z, i) \
84         z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
85         w = rol(w, 30);
86
87 /* Hash a single 512-bit block. This is the core of the algorithm.  */
88 static void
89 sha1_transform_default(u32 state[5], const u8 block[64])
90 {
91         u32 a, b, c, d, e;
92         u32 tmp[16];
93
94         /* Copy ctx->state[] to working vars */
95         a = state[0];
96         b = state[1];
97         c = state[2];
98         d = state[3];
99         e = state[4];
100
101         /* 4 rounds of 20 operations each. Loop unrolled. */
102         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);
103         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);
104         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);
105         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);
106         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);
107         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);
108         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);
109         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);
110         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);
111         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);
112         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);
113         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);
114         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);
115         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);
116         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);
117         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);
118         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);
119         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);
120         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);
121         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);
122
123         /* Add the working vars back into context.state[] */
124         state[0] += a;
125         state[1] += b;
126         state[2] += c;
127         state[3] += d;
128         state[4] += e;
129 }
130
131 #ifdef ENABLE_SSSE3_SHA1
132 extern void
133 sha1_transform_blocks_ssse3(u32 state[5], const void *data, size_t num_blocks);
134 extern void
135 sha1_transform_blocks_default(u32 state[5], const void *data, size_t num_blocks);
136 #  define sha1_transform_blocks sha1_transform_blocks_ssse3
137 #else
138 #  define sha1_transform_blocks sha1_transform_blocks_default
139 #endif
140
141 #ifndef ENABLE_SSSE3_SHA1
142 static
143 #endif
144 void
145 sha1_transform_blocks_default(u32 state[5], const void *data, size_t num_blocks)
146 {
147         do {
148                 sha1_transform_default(state, data);
149                 data += 64;
150         } while (--num_blocks);
151 }
152
153 /* Initializes the specified SHA-1 context.
154  *
155  * After sha1_init(), call sha1_update() zero or more times to provide the data
156  * to be hashed.  Then call sha1_final() to get the final hash.  */
157 void
158 sha1_init(SHA_CTX *ctx)
159 {
160         ctx->bytecount = 0;
161
162         ctx->state[0] = 0x67452301;
163         ctx->state[1] = 0xEFCDAB89;
164         ctx->state[2] = 0x98BADCFE;
165         ctx->state[3] = 0x10325476;
166         ctx->state[4] = 0xC3D2E1F0;
167 }
168
169 /* Updates the SHA-1 context with @len bytes of data.  */
170 void
171 sha1_update(SHA_CTX *ctx, const void *data, size_t len)
172 {
173         unsigned buffered = ctx->bytecount & 63;
174
175         ctx->bytecount += len;
176
177         if (buffered) {
178                 /* Previous block is unfinished.  */
179                 if (len < 64 - buffered) {
180                         memcpy(&ctx->buffer[buffered], data, len);
181                         /* Previous block still unfinished.  */
182                         return;
183                 } else {
184                         memcpy(&ctx->buffer[buffered], data, 64 - buffered);
185                         /* Finished the previous block.  */
186                         sha1_transform_blocks(ctx->state, ctx->buffer, 1);
187                         data += 64 - buffered;
188                         len -= 64 - buffered;
189                 }
190         }
191
192         /* Process blocks directly from the input data.  */
193         if (len / 64) {
194                 sha1_transform_blocks(ctx->state, data, len / 64);
195                 data += len & ~63;
196                 len &= 63;
197         }
198
199         /* Copy any remaining bytes to the buffer.  */
200         if (len)
201                 memcpy(ctx->buffer, data, len);
202 }
203
204 /* Pad the message and generate the final SHA-1 message digest.  */
205 void
206 sha1_final(u8 md[20], SHA_CTX *ctx)
207 {
208         /* Logically, we must append 1 bit, then a variable number of 0 bits,
209          * then the message length in bits as a big-endian integer, so that the
210          * final length is a multiple of the block size.  */
211         static const u8 padding[64] = {0x80, };
212         be64 finalcount = cpu_to_be64(ctx->bytecount << 3);
213
214         sha1_update(ctx, padding, 64 - ((ctx->bytecount + 8) & 63));
215         sha1_update(ctx, &finalcount, 8);
216
217         for (int i = 0; i < 5; i++)
218                 store_be32_unaligned(cpu_to_be32(ctx->state[i]), &md[i * 4]);
219 }
220
221 /* Calculate the SHA-1 message digest of the specified buffer.
222  * @len is the buffer length in bytes.  */
223 void
224 sha1_buffer(const void *buffer, size_t len, u8 md[20])
225 {
226         SHA_CTX ctx;
227
228         sha1_init(&ctx);
229         sha1_update(&ctx, buffer, len);
230         sha1_final(md, &ctx);
231 }
232
233 #endif /* !WITH_LIBCRYPTO */