]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/sha256.c
Update the code to mostly match the new simpler file format
[icculus/xz.git] / src / liblzma / check / sha256.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       sha256.c
4 /// \brief      SHA256
5 //
6 //  Based on the public domain code found from Wei Dai's Crypto++ library
7 //  version 5.5.1: http://www.cryptopp.com/
8 //  This code has been put into the public domain.
9 //
10 /// \todo       Crypto++ has x86 ASM optimizations. They use SSE so if they
11 ///             are imported to liblzma, SSE instructions need to be used
12 ///             conditionally to keep the code working on older boxes.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "check.h"
21
22 #ifndef WORDS_BIGENDIAN
23 #       include "../../common/bswap.h"
24 #endif
25
26 // At least on x86, GCC is able to optimize this to a rotate instruction.
27 #define rotr_32(num, amount) ((num) >> (amount) | (num) << (32 - (amount)))
28
29 #define blk0(i) (W[i] = data[i])
30 #define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \
31                 + s0(W[(i - 15) & 15]))
32
33 #define Ch(x, y, z) (z ^ (x & (y ^ z)))
34 #define Maj(x, y, z) ((x & y) | (z & (x | y)))
35
36 #define a(i) T[(0 - i) & 7]
37 #define b(i) T[(1 - i) & 7]
38 #define c(i) T[(2 - i) & 7]
39 #define d(i) T[(3 - i) & 7]
40 #define e(i) T[(4 - i) & 7]
41 #define f(i) T[(5 - i) & 7]
42 #define g(i) T[(6 - i) & 7]
43 #define h(i) T[(7 - i) & 7]
44
45 #define R(i) \
46         h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + SHA256_K[i + j] \
47                 + (j ? blk2(i) : blk0(i)); \
48         d(i) += h(i); \
49         h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
50
51 #define S0(x) (rotr_32(x, 2) ^ rotr_32(x, 13) ^ rotr_32(x, 22))
52 #define S1(x) (rotr_32(x, 6) ^ rotr_32(x, 11) ^ rotr_32(x, 25))
53 #define s0(x) (rotr_32(x, 7) ^ rotr_32(x, 18) ^ (x >> 3))
54 #define s1(x) (rotr_32(x, 17) ^ rotr_32(x, 19) ^ (x >> 10))
55
56
57 static const uint32_t SHA256_K[64] = {
58         0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
59         0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
60         0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
61         0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
62         0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
63         0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
64         0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
65         0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
66         0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
67         0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
68         0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
69         0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
70         0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
71         0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
72         0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
73         0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
74 };
75
76
77 static void
78 transform(uint32_t state[static 8], const uint32_t data[static 16])
79 {
80         uint32_t W[16];
81         uint32_t T[8];
82
83         // Copy state[] to working vars.
84         memcpy(T, state, sizeof(T));
85
86         // 64 operations, partially loop unrolled
87         for (unsigned int j = 0; j < 64; j += 16) {
88                 R( 0); R( 1); R( 2); R( 3);
89                 R( 4); R( 5); R( 6); R( 7);
90                 R( 8); R( 9); R(10); R(11);
91                 R(12); R(13); R(14); R(15);
92         }
93
94         // Add the working vars back into state[].
95         state[0] += a(0);
96         state[1] += b(0);
97         state[2] += c(0);
98         state[3] += d(0);
99         state[4] += e(0);
100         state[5] += f(0);
101         state[6] += g(0);
102         state[7] += h(0);
103 }
104
105
106 static void
107 process(lzma_check *check)
108 {
109 #ifdef WORDS_BIGENDIAN
110         transform(check->state.sha256.state, (uint32_t *)(check->buffer));
111
112 #else
113         uint32_t data[16];
114
115         for (size_t i = 0; i < 16; ++i)
116                 data[i] = bswap_32(*((uint32_t*)(check->buffer) + i));
117
118         transform(check->state.sha256.state, data);
119 #endif
120
121         return;
122 }
123
124
125 extern void
126 lzma_sha256_init(lzma_check *check)
127 {
128         static const uint32_t s[8] = {
129                 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
130                 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
131         };
132
133         memcpy(check->state.sha256.state, s, sizeof(s));
134         check->state.sha256.size = 0;
135
136         return;
137 }
138
139
140 extern void
141 lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check *check)
142 {
143         // Copy the input data into a properly aligned temporary buffer.
144         // This way we can be called with arbitrarily sized buffers
145         // (no need to be multiple of 64 bytes), and the code works also
146         // on architectures that don't allow unaligned memory access.
147         while (size > 0) {
148                 const size_t copy_start = check->state.sha256.size & 0x3F;
149                 size_t copy_size = 64 - copy_start;
150                 if (copy_size > size)
151                         copy_size = size;
152
153                 memcpy(check->buffer + copy_start, buf, copy_size);
154
155                 buf += copy_size;
156                 size -= copy_size;
157                 check->state.sha256.size += copy_size;
158
159                 if ((check->state.sha256.size & 0x3F) == 0)
160                         process(check);
161         }
162
163         return;
164 }
165
166
167 extern void
168 lzma_sha256_finish(lzma_check *check)
169 {
170         // Add padding as described in RFC 3174 (it describes SHA-1 but
171         // the same padding style is used for SHA-256 too).
172         size_t pos = check->state.sha256.size & 0x3F;
173         check->buffer[pos++] = 0x80;
174
175         while (pos != 64 - 8) {
176                 if (pos == 64) {
177                         process(check);
178                         pos = 0;
179                 }
180
181                 check->buffer[pos++] = 0x00;
182         }
183
184         // Convert the message size from bytes to bits.
185         check->state.sha256.size *= 8;
186
187 #ifdef WORDS_BIGENDIAN
188         *(uint64_t *)(check->buffer + 64 - 8) = check->state.sha256.size;
189 #else
190         *(uint64_t *)(check->buffer + 64 - 8)
191                         = bswap_64(check->state.sha256.size);
192 #endif
193
194         process(check);
195
196         for (size_t i = 0; i < 8; ++i)
197 #ifdef WORDS_BIGENDIAN
198                 ((uint32_t *)(check->buffer))[i]
199                                 = check->state.sha256.state[i];
200 #else
201                 ((uint32_t *)(check->buffer))[i]
202                                 = bswap_32(check->state.sha256.state[i]);
203 #endif
204
205         return;
206 }