Skip to content

Commit 902be86

Browse files
committed
Unify the different SHA256 structs again.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent d224869 commit 902be86

6 files changed

Lines changed: 89 additions & 99 deletions

File tree

src/hashes/sha2/sha224.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ int sha224_c_init(hash_state * md)
3737
{
3838
LTC_ARGCHK(md != NULL);
3939

40-
md->sha256_c.curlen = 0;
41-
md->sha256_c.length = 0;
42-
md->sha256_c.state[0] = 0xc1059ed8UL;
43-
md->sha256_c.state[1] = 0x367cd507UL;
44-
md->sha256_c.state[2] = 0x3070dd17UL;
45-
md->sha256_c.state[3] = 0xf70e5939UL;
46-
md->sha256_c.state[4] = 0xffc00b31UL;
47-
md->sha256_c.state[5] = 0x68581511UL;
48-
md->sha256_c.state[6] = 0x64f98fa7UL;
49-
md->sha256_c.state[7] = 0xbefa4fa4UL;
40+
md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
41+
42+
md->sha256.curlen = 0;
43+
md->sha256.length = 0;
44+
md->sha256.state[0] = 0xc1059ed8UL;
45+
md->sha256.state[1] = 0x367cd507UL;
46+
md->sha256.state[2] = 0x3070dd17UL;
47+
md->sha256.state[3] = 0xf70e5939UL;
48+
md->sha256.state[4] = 0xffc00b31UL;
49+
md->sha256.state[5] = 0x68581511UL;
50+
md->sha256.state[6] = 0x64f98fa7UL;
51+
md->sha256.state[7] = 0xbefa4fa4UL;
5052
return CRYPT_OK;
5153
}
5254

src/hashes/sha2/sha224_x86.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ int sha224_x86_init(hash_state * md)
3737
{
3838
LTC_ARGCHK(md != NULL);
3939

40-
md->sha256_x86.curlen = 0;
41-
md->sha256_x86.length = 0;
42-
md->sha256_x86.state[0] = 0xc1059ed8UL;
43-
md->sha256_x86.state[1] = 0x367cd507UL;
44-
md->sha256_x86.state[2] = 0x3070dd17UL;
45-
md->sha256_x86.state[3] = 0xf70e5939UL;
46-
md->sha256_x86.state[4] = 0xffc00b31UL;
47-
md->sha256_x86.state[5] = 0x68581511UL;
48-
md->sha256_x86.state[6] = 0x64f98fa7UL;
49-
md->sha256_x86.state[7] = 0xbefa4fa4UL;
40+
md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
41+
42+
md->sha256.curlen = 0;
43+
md->sha256.length = 0;
44+
md->sha256.state[0] = 0xc1059ed8UL;
45+
md->sha256.state[1] = 0x367cd507UL;
46+
md->sha256.state[2] = 0x3070dd17UL;
47+
md->sha256.state[3] = 0xf70e5939UL;
48+
md->sha256.state[4] = 0xffc00b31UL;
49+
md->sha256.state[5] = 0x68581511UL;
50+
md->sha256.state[6] = 0x64f98fa7UL;
51+
md->sha256.state[7] = 0xbefa4fa4UL;
5052
return CRYPT_OK;
5153
}
5254

src/hashes/sha2/sha256.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
8585

8686
/* copy state into S */
8787
for (i = 0; i < 8; i++) {
88-
S[i] = md->sha256_c.state[i];
88+
S[i] = md->sha256.state[i];
8989
}
9090

9191
/* copy the state into 512-bits into W[0..15] */
@@ -211,7 +211,7 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
211211

212212
/* feedback */
213213
for (i = 0; i < 8; i++) {
214-
md->sha256_c.state[i] = md->sha256_c.state[i] + S[i];
214+
md->sha256.state[i] = md->sha256.state[i] + S[i];
215215
}
216216
return CRYPT_OK;
217217
}
@@ -235,16 +235,18 @@ int sha256_c_init(hash_state * md)
235235
{
236236
LTC_ARGCHK(md != NULL);
237237

238-
md->sha256_c.curlen = 0;
239-
md->sha256_c.length = 0;
240-
md->sha256_c.state[0] = 0x6A09E667UL;
241-
md->sha256_c.state[1] = 0xBB67AE85UL;
242-
md->sha256_c.state[2] = 0x3C6EF372UL;
243-
md->sha256_c.state[3] = 0xA54FF53AUL;
244-
md->sha256_c.state[4] = 0x510E527FUL;
245-
md->sha256_c.state[5] = 0x9B05688CUL;
246-
md->sha256_c.state[6] = 0x1F83D9ABUL;
247-
md->sha256_c.state[7] = 0x5BE0CD19UL;
238+
md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
239+
240+
md->sha256.curlen = 0;
241+
md->sha256.length = 0;
242+
md->sha256.state[0] = 0x6A09E667UL;
243+
md->sha256.state[1] = 0xBB67AE85UL;
244+
md->sha256.state[2] = 0x3C6EF372UL;
245+
md->sha256.state[3] = 0xA54FF53AUL;
246+
md->sha256.state[4] = 0x510E527FUL;
247+
md->sha256.state[5] = 0x9B05688CUL;
248+
md->sha256.state[6] = 0x1F83D9ABUL;
249+
md->sha256.state[7] = 0x5BE0CD19UL;
248250
return CRYPT_OK;
249251
}
250252

@@ -255,7 +257,7 @@ int sha256_c_init(hash_state * md)
255257
@param inlen The length of the data (octets)
256258
@return CRYPT_OK if successful
257259
*/
258-
HASH_PROCESS(sha256_c_process,s_sha256_compress, sha256_c, 64)
260+
HASH_PROCESS(sha256_c_process,s_sha256_compress, sha256, 64)
259261

260262
/**
261263
Terminate the hash to get the digest
@@ -270,41 +272,41 @@ int sha256_c_done(hash_state * md, unsigned char *out)
270272
LTC_ARGCHK(md != NULL);
271273
LTC_ARGCHK(out != NULL);
272274

273-
if (md->sha256_c.curlen >= sizeof(md->sha256_c.buf)) {
275+
if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
274276
return CRYPT_INVALID_ARG;
275277
}
276278

277279

278280
/* increase the length of the message */
279-
md->sha256_c.length += md->sha256_c.curlen * 8;
281+
md->sha256.length += md->sha256.curlen * 8;
280282

281283
/* append the '1' bit */
282-
md->sha256_c.buf[md->sha256_c.curlen++] = (unsigned char)0x80;
284+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;
283285

284286
/* if the length is currently above 56 bytes we append zeros
285287
* then compress. Then we can fall back to padding zeros and length
286288
* encoding like normal.
287289
*/
288-
if (md->sha256_c.curlen > 56) {
289-
while (md->sha256_c.curlen < 64) {
290-
md->sha256_c.buf[md->sha256_c.curlen++] = (unsigned char)0;
290+
if (md->sha256.curlen > 56) {
291+
while (md->sha256.curlen < 64) {
292+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
291293
}
292-
s_sha256_compress(md, md->sha256_c.buf);
293-
md->sha256_c.curlen = 0;
294+
s_sha256_compress(md, md->sha256.buf);
295+
md->sha256.curlen = 0;
294296
}
295297

296298
/* pad upto 56 bytes of zeroes */
297-
while (md->sha256_c.curlen < 56) {
298-
md->sha256_c.buf[md->sha256_c.curlen++] = (unsigned char)0;
299+
while (md->sha256.curlen < 56) {
300+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
299301
}
300302

301303
/* store length */
302-
STORE64H(md->sha256_c.length, md->sha256_c.buf+56);
303-
s_sha256_compress(md, md->sha256_c.buf);
304+
STORE64H(md->sha256.length, md->sha256.buf+56);
305+
s_sha256_compress(md, md->sha256.buf);
304306

305307
/* copy output */
306308
for (i = 0; i < 8; i++) {
307-
STORE32H(md->sha256_c.state[i], out+(4*i));
309+
STORE32H(md->sha256.state[i], out+(4*i));
308310
}
309311
#ifdef LTC_CLEAN_STACK
310312
zeromem(md, sizeof(hash_state));

src/hashes/sha2/sha256_x86.c

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#if defined __GNUC__
1313
#pragma GCC diagnostic push
1414
#pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
15+
#pragma GCC diagnostic ignored "-Wuninitialized"
1516
#pragma GCC diagnostic ignored "-Wunused-function"
1617
#include <emmintrin.h> /* SSE2 _mm_load_si128 _mm_loadu_si128 _mm_store_si128 _mm_set_epi64x _mm_add_epi32 _mm_shuffle_epi32 */
1718
#include <tmmintrin.h> /* SSSE3 _mm_alignr_epi8 _mm_shuffle_epi8 */
@@ -93,13 +94,13 @@ static int ltc_attribute_sha256 s_sha256_x86_compress(hash_state * md, const uns
9394

9495
LTC_ARGCHK(md != NULL);
9596
LTC_ARGCHK(buf != NULL);
96-
LTC_ARGCHK(((uintptr_t)(&md->sha256_x86.state[0])) % 16 == 0);
97+
LTC_ARGCHK(((uintptr_t)(&md->sha256.state[0])) % 16 == 0);
9798
LTC_ARGCHK(((uintptr_t)(&K[0])) % 16 == 0);
9899
LTC_ARGCHK(sizeof(int) == 4);
99100

100101
reverse = _mm_set_epi64x(0x0c0d0e0f08090a0bull, 0x0405060700010203ull);
101-
state_0 = _mm_load_si128(((__m128i const*)(&md->sha256_x86.state[0])));
102-
state_1 = _mm_load_si128(((__m128i const*)(&md->sha256_x86.state[4])));
102+
state_0 = _mm_load_si128(((__m128i const*)(&md->sha256.state[0])));
103+
state_1 = _mm_load_si128(((__m128i const*)(&md->sha256.state[4])));
103104
tmp = _mm_shuffle_epi32(state_0, k_shuffle_epi32(0x2, 0x3, 0x0, 0x1));
104105
state_1 = _mm_shuffle_epi32(state_1, k_shuffle_epi32(0x0, 0x1, 0x2, 0x3));
105106
state_0 = _mm_alignr_epi8(tmp, state_1, k_alignr_epi8(2));
@@ -250,8 +251,8 @@ static int ltc_attribute_sha256 s_sha256_x86_compress(hash_state * md, const uns
250251
state_1 = _mm_shuffle_epi32(state_1, k_shuffle_epi32(0x2, 0x3, 0x0, 0x1));
251252
state_0 = ltc_mm_blend_epi32(tmp, state_1, k_blend_epi32(0x1, 0x1, 0x0, 0x0));
252253
state_1 = _mm_alignr_epi8(state_1, tmp, k_alignr_epi8(2));
253-
_mm_store_si128(((__m128i*)(&md->sha256_x86.state[0])), state_0);
254-
_mm_store_si128(((__m128i*)(&md->sha256_x86.state[4])), state_1);
254+
_mm_store_si128(((__m128i*)(&md->sha256.state[0])), state_0);
255+
_mm_store_si128(((__m128i*)(&md->sha256.state[4])), state_1);
255256
return CRYPT_OK;
256257
}
257258
#undef K
@@ -275,16 +276,18 @@ int sha256_x86_init(hash_state * md)
275276
{
276277
LTC_ARGCHK(md != NULL);
277278

278-
md->sha256_x86.curlen = 0;
279-
md->sha256_x86.length = 0;
280-
md->sha256_x86.state[0] = 0x6A09E667UL;
281-
md->sha256_x86.state[1] = 0xBB67AE85UL;
282-
md->sha256_x86.state[2] = 0x3C6EF372UL;
283-
md->sha256_x86.state[3] = 0xA54FF53AUL;
284-
md->sha256_x86.state[4] = 0x510E527FUL;
285-
md->sha256_x86.state[5] = 0x9B05688CUL;
286-
md->sha256_x86.state[6] = 0x1F83D9ABUL;
287-
md->sha256_x86.state[7] = 0x5BE0CD19UL;
279+
md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
280+
281+
md->sha256.curlen = 0;
282+
md->sha256.length = 0;
283+
md->sha256.state[0] = 0x6A09E667UL;
284+
md->sha256.state[1] = 0xBB67AE85UL;
285+
md->sha256.state[2] = 0x3C6EF372UL;
286+
md->sha256.state[3] = 0xA54FF53AUL;
287+
md->sha256.state[4] = 0x510E527FUL;
288+
md->sha256.state[5] = 0x9B05688CUL;
289+
md->sha256.state[6] = 0x1F83D9ABUL;
290+
md->sha256.state[7] = 0x5BE0CD19UL;
288291
return CRYPT_OK;
289292
}
290293

@@ -295,7 +298,7 @@ int sha256_x86_init(hash_state * md)
295298
@param inlen The length of the data (octets)
296299
@return CRYPT_OK if successful
297300
*/
298-
HASH_PROCESS(sha256_x86_process,s_sha256_x86_compress, sha256_x86, 64)
301+
HASH_PROCESS(sha256_x86_process,s_sha256_x86_compress, sha256, 64)
299302

300303
/**
301304
Terminate the hash to get the digest
@@ -310,41 +313,41 @@ int sha256_x86_done(hash_state * md, unsigned char *out)
310313
LTC_ARGCHK(md != NULL);
311314
LTC_ARGCHK(out != NULL);
312315

313-
if (md->sha256_x86.curlen >= sizeof(md->sha256_x86.buf)) {
316+
if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
314317
return CRYPT_INVALID_ARG;
315318
}
316319

317320

318321
/* increase the length of the message */
319-
md->sha256_x86.length += md->sha256_x86.curlen * 8;
322+
md->sha256.length += md->sha256.curlen * 8;
320323

321324
/* append the '1' bit */
322-
md->sha256_x86.buf[md->sha256_x86.curlen++] = (unsigned char)0x80;
325+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;
323326

324327
/* if the length is currently above 56 bytes we append zeros
325328
* then compress. Then we can fall back to padding zeros and length
326329
* encoding like normal.
327330
*/
328-
if (md->sha256_x86.curlen > 56) {
329-
while (md->sha256_x86.curlen < 64) {
330-
md->sha256_x86.buf[md->sha256_x86.curlen++] = (unsigned char)0;
331+
if (md->sha256.curlen > 56) {
332+
while (md->sha256.curlen < 64) {
333+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
331334
}
332-
s_sha256_x86_compress(md, md->sha256_x86.buf);
333-
md->sha256_x86.curlen = 0;
335+
s_sha256_x86_compress(md, md->sha256.buf);
336+
md->sha256.curlen = 0;
334337
}
335338

336339
/* pad upto 56 bytes of zeroes */
337-
while (md->sha256_x86.curlen < 56) {
338-
md->sha256_x86.buf[md->sha256_x86.curlen++] = (unsigned char)0;
340+
while (md->sha256.curlen < 56) {
341+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
339342
}
340343

341344
/* store length */
342-
STORE64H(md->sha256_x86.length, md->sha256_x86.buf+56);
343-
s_sha256_x86_compress(md, md->sha256_x86.buf);
345+
STORE64H(md->sha256.length, md->sha256.buf+56);
346+
s_sha256_x86_compress(md, md->sha256.buf);
344347

345348
/* copy output */
346349
for (i = 0; i < 8; i++) {
347-
STORE32H(md->sha256_x86.state[i], out+(4*i));
350+
STORE32H(md->sha256.state[i], out+(4*i));
348351
}
349352
#ifdef LTC_CLEAN_STACK
350353
zeromem(md, sizeof(hash_state));

src/headers/tomcrypt_hash.h

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,12 @@ struct sha512_state {
3535
#endif
3636

3737
#ifdef LTC_SHA256
38-
struct sha256_c_state {
38+
struct sha256_state {
3939
ulong64 length;
40-
ulong32 state[8], curlen;
41-
unsigned char buf[64];
42-
};
43-
#endif
44-
45-
#ifdef LTC_SHA256_X86
46-
#if defined _MSC_VER
47-
#define LTC_ALIGN_AS(x) __declspec(align(x))
48-
#else
49-
#define LTC_ALIGN_AS(x) __attribute__((aligned(x)))
50-
#endif
51-
#pragma pack(push)
52-
#pragma pack(16) /* todo #pragma pack seems to not work */
53-
LTC_ALIGN_AS(16) struct sha256_x86_state {
54-
ulong32 state[8];
40+
ulong32 *state, curlen;
5541
unsigned char buf[64];
56-
ulong32 curlen;
57-
ulong64 length;
42+
unsigned char state_buf[LTC_ALIGNED_BUF_SIZE(ulong32, 8, 16)];
5843
};
59-
#pragma pack(pop)
6044
#endif
6145

6246
#ifdef LTC_SHA1
@@ -189,10 +173,7 @@ typedef union Hash_state {
189173
struct sha512_state sha512;
190174
#endif
191175
#ifdef LTC_SHA256
192-
struct sha256_c_state sha256_c;
193-
#endif
194-
#ifdef LTC_SHA256_X86
195-
struct sha256_x86_state sha256_x86;
176+
struct sha256_state sha256;
196177
#endif
197178
#ifdef LTC_SHA1
198179
struct sha1_state sha1;

src/misc/crypt/crypt_sizes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static const crypt_size s_crypt_sizes[] = {
3737
SZ_STRINGIFY_S(sha512_state),
3838
#endif
3939
#ifdef LTC_SHA256
40-
SZ_STRINGIFY_S(sha256_c_state),
40+
SZ_STRINGIFY_S(sha256_state),
4141
#endif
4242
#ifdef LTC_SHA1
4343
SZ_STRINGIFY_S(sha1_state),

0 commit comments

Comments
 (0)