Skip to content

Commit e0acff2

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

6 files changed

Lines changed: 86 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: 26 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,16 @@ 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.curlen = 0;
239+
md->sha256.length = 0;
240+
md->sha256.state[0] = 0x6A09E667UL;
241+
md->sha256.state[1] = 0xBB67AE85UL;
242+
md->sha256.state[2] = 0x3C6EF372UL;
243+
md->sha256.state[3] = 0xA54FF53AUL;
244+
md->sha256.state[4] = 0x510E527FUL;
245+
md->sha256.state[5] = 0x9B05688CUL;
246+
md->sha256.state[6] = 0x1F83D9ABUL;
247+
md->sha256.state[7] = 0x5BE0CD19UL;
248248
return CRYPT_OK;
249249
}
250250

@@ -255,7 +255,7 @@ int sha256_c_init(hash_state * md)
255255
@param inlen The length of the data (octets)
256256
@return CRYPT_OK if successful
257257
*/
258-
HASH_PROCESS(sha256_c_process,s_sha256_compress, sha256_c, 64)
258+
HASH_PROCESS(sha256_c_process,s_sha256_compress, sha256, 64)
259259

260260
/**
261261
Terminate the hash to get the digest
@@ -270,41 +270,41 @@ int sha256_c_done(hash_state * md, unsigned char *out)
270270
LTC_ARGCHK(md != NULL);
271271
LTC_ARGCHK(out != NULL);
272272

273-
if (md->sha256_c.curlen >= sizeof(md->sha256_c.buf)) {
273+
if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
274274
return CRYPT_INVALID_ARG;
275275
}
276276

277277

278278
/* increase the length of the message */
279-
md->sha256_c.length += md->sha256_c.curlen * 8;
279+
md->sha256.length += md->sha256.curlen * 8;
280280

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

284284
/* if the length is currently above 56 bytes we append zeros
285285
* then compress. Then we can fall back to padding zeros and length
286286
* encoding like normal.
287287
*/
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;
288+
if (md->sha256.curlen > 56) {
289+
while (md->sha256.curlen < 64) {
290+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
291291
}
292-
s_sha256_compress(md, md->sha256_c.buf);
293-
md->sha256_c.curlen = 0;
292+
s_sha256_compress(md, md->sha256.buf);
293+
md->sha256.curlen = 0;
294294
}
295295

296296
/* 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;
297+
while (md->sha256.curlen < 56) {
298+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
299299
}
300300

301301
/* store length */
302-
STORE64H(md->sha256_c.length, md->sha256_c.buf+56);
303-
s_sha256_compress(md, md->sha256_c.buf);
302+
STORE64H(md->sha256.length, md->sha256.buf+56);
303+
s_sha256_compress(md, md->sha256.buf);
304304

305305
/* copy output */
306306
for (i = 0; i < 8; i++) {
307-
STORE32H(md->sha256_c.state[i], out+(4*i));
307+
STORE32H(md->sha256.state[i], out+(4*i));
308308
}
309309
#ifdef LTC_CLEAN_STACK
310310
zeromem(md, sizeof(hash_state));

src/hashes/sha2/sha256_x86.c

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ static int ltc_attribute_sha256 s_sha256_x86_compress(hash_state * md, const uns
9393

9494
LTC_ARGCHK(md != NULL);
9595
LTC_ARGCHK(buf != NULL);
96-
LTC_ARGCHK(((uintptr_t)(&md->sha256_x86.state[0])) % 16 == 0);
96+
LTC_ARGCHK(((uintptr_t)(&md->sha256.state[0])) % 16 == 0);
9797
LTC_ARGCHK(((uintptr_t)(&K[0])) % 16 == 0);
9898
LTC_ARGCHK(sizeof(int) == 4);
9999

100100
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])));
101+
state_0 = _mm_load_si128(((__m128i const*)(&md->sha256.state[0])));
102+
state_1 = _mm_load_si128(((__m128i const*)(&md->sha256.state[4])));
103103
tmp = _mm_shuffle_epi32(state_0, k_shuffle_epi32(0x2, 0x3, 0x0, 0x1));
104104
state_1 = _mm_shuffle_epi32(state_1, k_shuffle_epi32(0x0, 0x1, 0x2, 0x3));
105105
state_0 = _mm_alignr_epi8(tmp, state_1, k_alignr_epi8(2));
@@ -250,8 +250,8 @@ static int ltc_attribute_sha256 s_sha256_x86_compress(hash_state * md, const uns
250250
state_1 = _mm_shuffle_epi32(state_1, k_shuffle_epi32(0x2, 0x3, 0x0, 0x1));
251251
state_0 = ltc_mm_blend_epi32(tmp, state_1, k_blend_epi32(0x1, 0x1, 0x0, 0x0));
252252
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);
253+
_mm_store_si128(((__m128i*)(&md->sha256.state[0])), state_0);
254+
_mm_store_si128(((__m128i*)(&md->sha256.state[4])), state_1);
255255
return CRYPT_OK;
256256
}
257257
#undef K
@@ -275,16 +275,18 @@ int sha256_x86_init(hash_state * md)
275275
{
276276
LTC_ARGCHK(md != NULL);
277277

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;
278+
md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
279+
280+
md->sha256.curlen = 0;
281+
md->sha256.length = 0;
282+
md->sha256.state[0] = 0x6A09E667UL;
283+
md->sha256.state[1] = 0xBB67AE85UL;
284+
md->sha256.state[2] = 0x3C6EF372UL;
285+
md->sha256.state[3] = 0xA54FF53AUL;
286+
md->sha256.state[4] = 0x510E527FUL;
287+
md->sha256.state[5] = 0x9B05688CUL;
288+
md->sha256.state[6] = 0x1F83D9ABUL;
289+
md->sha256.state[7] = 0x5BE0CD19UL;
288290
return CRYPT_OK;
289291
}
290292

@@ -295,7 +297,7 @@ int sha256_x86_init(hash_state * md)
295297
@param inlen The length of the data (octets)
296298
@return CRYPT_OK if successful
297299
*/
298-
HASH_PROCESS(sha256_x86_process,s_sha256_x86_compress, sha256_x86, 64)
300+
HASH_PROCESS(sha256_x86_process,s_sha256_x86_compress, sha256, 64)
299301

300302
/**
301303
Terminate the hash to get the digest
@@ -310,41 +312,41 @@ int sha256_x86_done(hash_state * md, unsigned char *out)
310312
LTC_ARGCHK(md != NULL);
311313
LTC_ARGCHK(out != NULL);
312314

313-
if (md->sha256_x86.curlen >= sizeof(md->sha256_x86.buf)) {
315+
if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
314316
return CRYPT_INVALID_ARG;
315317
}
316318

317319

318320
/* increase the length of the message */
319-
md->sha256_x86.length += md->sha256_x86.curlen * 8;
321+
md->sha256.length += md->sha256.curlen * 8;
320322

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

324326
/* if the length is currently above 56 bytes we append zeros
325327
* then compress. Then we can fall back to padding zeros and length
326328
* encoding like normal.
327329
*/
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;
330+
if (md->sha256.curlen > 56) {
331+
while (md->sha256.curlen < 64) {
332+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
331333
}
332-
s_sha256_x86_compress(md, md->sha256_x86.buf);
333-
md->sha256_x86.curlen = 0;
334+
s_sha256_x86_compress(md, md->sha256.buf);
335+
md->sha256.curlen = 0;
334336
}
335337

336338
/* 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;
339+
while (md->sha256.curlen < 56) {
340+
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
339341
}
340342

341343
/* store length */
342-
STORE64H(md->sha256_x86.length, md->sha256_x86.buf+56);
343-
s_sha256_x86_compress(md, md->sha256_x86.buf);
344+
STORE64H(md->sha256.length, md->sha256.buf+56);
345+
s_sha256_x86_compress(md, md->sha256.buf);
344346

345347
/* copy output */
346348
for (i = 0; i < 8; i++) {
347-
STORE32H(md->sha256_x86.state[i], out+(4*i));
349+
STORE32H(md->sha256.state[i], out+(4*i));
348350
}
349351
#ifdef LTC_CLEAN_STACK
350352
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)