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