Skip to content

Commit d224869

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

4 files changed

Lines changed: 69 additions & 76 deletions

File tree

src/hashes/sha1.c

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ static int s_sha1_c_compress(hash_state *md, const unsigned char *buf)
6363
}
6464

6565
/* copy state */
66-
a = md->sha1_c.state[0];
67-
b = md->sha1_c.state[1];
68-
c = md->sha1_c.state[2];
69-
d = md->sha1_c.state[3];
70-
e = md->sha1_c.state[4];
66+
a = md->sha1.state[0];
67+
b = md->sha1.state[1];
68+
c = md->sha1.state[2];
69+
d = md->sha1.state[3];
70+
e = md->sha1.state[4];
7171

7272
#ifdef LTC_SMALL_STACK_SHA1
7373
#define Wi(i) do { W[(i) % 16] = ROL(W[((i) - 3) % 16] ^ W[((i) - 8) % 16] ^ W[((i) - 14) % 16] ^ W[((i) - 16) % 16], 1); } while(0)
@@ -160,11 +160,11 @@ static int s_sha1_c_compress(hash_state *md, const unsigned char *buf)
160160
#undef Windex
161161

162162
/* store */
163-
md->sha1_c.state[0] = md->sha1_c.state[0] + a;
164-
md->sha1_c.state[1] = md->sha1_c.state[1] + b;
165-
md->sha1_c.state[2] = md->sha1_c.state[2] + c;
166-
md->sha1_c.state[3] = md->sha1_c.state[3] + d;
167-
md->sha1_c.state[4] = md->sha1_c.state[4] + e;
163+
md->sha1.state[0] = md->sha1.state[0] + a;
164+
md->sha1.state[1] = md->sha1.state[1] + b;
165+
md->sha1.state[2] = md->sha1.state[2] + c;
166+
md->sha1.state[3] = md->sha1.state[3] + d;
167+
md->sha1.state[4] = md->sha1.state[4] + e;
168168

169169
return CRYPT_OK;
170170
}
@@ -187,13 +187,16 @@ static int s_sha1_c_compress(hash_state *md, const unsigned char *buf)
187187
int sha1_c_init(hash_state * md)
188188
{
189189
LTC_ARGCHK(md != NULL);
190-
md->sha1_c.state[0] = 0x67452301UL;
191-
md->sha1_c.state[1] = 0xefcdab89UL;
192-
md->sha1_c.state[2] = 0x98badcfeUL;
193-
md->sha1_c.state[3] = 0x10325476UL;
194-
md->sha1_c.state[4] = 0xc3d2e1f0UL;
195-
md->sha1_c.curlen = 0;
196-
md->sha1_c.length = 0;
190+
191+
md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);
192+
193+
md->sha1.state[0] = 0x67452301UL;
194+
md->sha1.state[1] = 0xefcdab89UL;
195+
md->sha1.state[2] = 0x98badcfeUL;
196+
md->sha1.state[3] = 0x10325476UL;
197+
md->sha1.state[4] = 0xc3d2e1f0UL;
198+
md->sha1.curlen = 0;
199+
md->sha1.length = 0;
197200
return CRYPT_OK;
198201
}
199202

@@ -204,7 +207,7 @@ int sha1_c_init(hash_state * md)
204207
@param inlen The length of the data (octets)
205208
@return CRYPT_OK if successful
206209
*/
207-
HASH_PROCESS(sha1_c_process, s_sha1_c_compress, sha1_c, 64)
210+
HASH_PROCESS(sha1_c_process, s_sha1_c_compress, sha1, 64)
208211

209212
/**
210213
Terminate the hash to get the digest
@@ -219,40 +222,40 @@ int sha1_c_done(hash_state * md, unsigned char *out)
219222
LTC_ARGCHK(md != NULL);
220223
LTC_ARGCHK(out != NULL);
221224

222-
if (md->sha1_c.curlen >= sizeof(md->sha1_c.buf)) {
225+
if (md->sha1.curlen >= sizeof(md->sha1.buf)) {
223226
return CRYPT_INVALID_ARG;
224227
}
225228

226229
/* increase the length of the message */
227-
md->sha1_c.length += md->sha1_c.curlen * 8;
230+
md->sha1.length += md->sha1.curlen * 8;
228231

229232
/* append the '1' bit */
230-
md->sha1_c.buf[md->sha1_c.curlen++] = (unsigned char)0x80;
233+
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80;
231234

232235
/* if the length is currently above 56 bytes we append zeros
233236
* then compress. Then we can fall back to padding zeros and length
234237
* encoding like normal.
235238
*/
236-
if (md->sha1_c.curlen > 56) {
237-
while (md->sha1_c.curlen < 64) {
238-
md->sha1_c.buf[md->sha1_c.curlen++] = (unsigned char)0;
239+
if (md->sha1.curlen > 56) {
240+
while (md->sha1.curlen < 64) {
241+
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
239242
}
240-
s_sha1_c_compress(md, md->sha1_c.buf);
241-
md->sha1_c.curlen = 0;
243+
s_sha1_c_compress(md, md->sha1.buf);
244+
md->sha1.curlen = 0;
242245
}
243246

244247
/* pad upto 56 bytes of zeroes */
245-
while (md->sha1_c.curlen < 56) {
246-
md->sha1_c.buf[md->sha1_c.curlen++] = (unsigned char)0;
248+
while (md->sha1.curlen < 56) {
249+
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
247250
}
248251

249252
/* store length */
250-
STORE64H(md->sha1_c.length, md->sha1_c.buf+56);
251-
s_sha1_c_compress(md, md->sha1_c.buf);
253+
STORE64H(md->sha1.length, md->sha1.buf+56);
254+
s_sha1_c_compress(md, md->sha1.buf);
252255

253256
/* copy output */
254257
for (i = 0; i < 5; i++) {
255-
STORE32H(md->sha1_c.state[i], out+(4*i));
258+
STORE32H(md->sha1.state[i], out+(4*i));
256259
}
257260
#ifdef LTC_CLEAN_STACK
258261
zeromem(md, sizeof(hash_state));

src/hashes/sha1_x86.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#if defined __GNUC__
1414
#pragma GCC diagnostic push
1515
#pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
16+
#pragma GCC diagnostic ignored "-Wuninitialized"
1617
#pragma GCC diagnostic ignored "-Wunused-function"
1718
#include <emmintrin.h> /* SSE2 _mm_load_si128 _mm_loadu_si128 _mm_store_si128 _mm_set_epi32 _mm_set_epi64x _mm_setzero_si128 _mm_xor_si128 _mm_add_epi32 _mm_shuffle_epi32 */
1819
#include <tmmintrin.h> /* SSSE3 _mm_shuffle_epi8 */
@@ -64,13 +65,13 @@ static int ltc_attribute_sha1 s_sha1_x86_compress(hash_state *md, const unsigned
6465

6566
LTC_ARGCHK(md != NULL);
6667
LTC_ARGCHK(buf != NULL);
67-
LTC_ARGCHK(((uintptr_t)(&md->sha1_x86.state[0])) % 16 == 0);
68+
LTC_ARGCHK(((uintptr_t)(&md->sha1.state[0])) % 16 == 0);
6869
LTC_ARGCHK(sizeof(int) == 4);
6970

7071
reverse_8 = _mm_set_epi64x(0x0001020304050607ull, 0x08090a0b0c0d0e0full);
71-
abcdx = _mm_load_si128(((__m128i const*)(&md->sha1_x86.state[0])));
72+
abcdx = _mm_load_si128(((__m128i const*)(&md->sha1.state[0])));
7273
abcdx = _mm_shuffle_epi32(abcdx, k_reverse_32);
73-
e = _mm_set_epi32(*((int const*)(&md->sha1_x86.state[4])), 0, 0, 0);
74+
e = _mm_set_epi32(*((int const*)(&md->sha1.state[4])), 0, 0, 0);
7475

7576
old_abcd = abcdx;
7677
old_e = e;
@@ -176,8 +177,8 @@ static int ltc_attribute_sha1 s_sha1_x86_compress(hash_state *md, const unsigned
176177
e = _mm_add_epi32(e, old_e);
177178

178179
abcdx = _mm_shuffle_epi32(abcdx, k_reverse_32);
179-
_mm_store_si128(((__m128i*)(&md->sha1_x86.state[0])), abcdx);
180-
*((int*)(&md->sha1_x86.state[4])) = _mm_extract_epi32(e, 3);
180+
_mm_store_si128(((__m128i*)(&md->sha1.state[0])), abcdx);
181+
*((int*)(&md->sha1.state[4])) = _mm_extract_epi32(e, 3);
181182

182183
return CRYPT_OK;
183184

@@ -202,13 +203,16 @@ static int s_sha1_x86_compress(hash_state *md, const unsigned char *buf)
202203
int sha1_x86_init(hash_state * md)
203204
{
204205
LTC_ARGCHK(md != NULL);
205-
md->sha1_x86.state[0] = 0x67452301UL;
206-
md->sha1_x86.state[1] = 0xefcdab89UL;
207-
md->sha1_x86.state[2] = 0x98badcfeUL;
208-
md->sha1_x86.state[3] = 0x10325476UL;
209-
md->sha1_x86.state[4] = 0xc3d2e1f0UL;
210-
md->sha1_x86.curlen = 0;
211-
md->sha1_x86.length = 0;
206+
207+
md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);
208+
209+
md->sha1.state[0] = 0x67452301UL;
210+
md->sha1.state[1] = 0xefcdab89UL;
211+
md->sha1.state[2] = 0x98badcfeUL;
212+
md->sha1.state[3] = 0x10325476UL;
213+
md->sha1.state[4] = 0xc3d2e1f0UL;
214+
md->sha1.curlen = 0;
215+
md->sha1.length = 0;
212216
return CRYPT_OK;
213217
}
214218

@@ -219,7 +223,7 @@ int sha1_x86_init(hash_state * md)
219223
@param inlen The length of the data (octets)
220224
@return CRYPT_OK if successful
221225
*/
222-
HASH_PROCESS(sha1_x86_process, s_sha1_x86_compress, sha1_x86, 64)
226+
HASH_PROCESS(sha1_x86_process, s_sha1_x86_compress, sha1, 64)
223227

224228
/**
225229
Terminate the hash to get the digest
@@ -234,40 +238,40 @@ int sha1_x86_done(hash_state * md, unsigned char *out)
234238
LTC_ARGCHK(md != NULL);
235239
LTC_ARGCHK(out != NULL);
236240

237-
if (md->sha1_x86.curlen >= ((int)(sizeof(md->sha1_x86.buf)))) {
241+
if (md->sha1.curlen >= ((int)(sizeof(md->sha1.buf)))) {
238242
return CRYPT_INVALID_ARG;
239243
}
240244

241245
/* increase the length of the message */
242-
md->sha1_x86.length += md->sha1_x86.curlen * 8;
246+
md->sha1.length += md->sha1.curlen * 8;
243247

244248
/* append the '1' bit */
245-
md->sha1_x86.buf[md->sha1_x86.curlen++] = (unsigned char)0x80;
249+
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80;
246250

247251
/* if the length is currently above 56 bytes we append zeros
248252
* then compress. Then we can fall back to padding zeros and length
249253
* encoding like normal.
250254
*/
251-
if (md->sha1_x86.curlen > 56) {
252-
while (md->sha1_x86.curlen < 64) {
253-
md->sha1_x86.buf[md->sha1_x86.curlen++] = (unsigned char)0;
255+
if (md->sha1.curlen > 56) {
256+
while (md->sha1.curlen < 64) {
257+
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
254258
}
255-
s_sha1_x86_compress(md, md->sha1_x86.buf);
256-
md->sha1_x86.curlen = 0;
259+
s_sha1_x86_compress(md, md->sha1.buf);
260+
md->sha1.curlen = 0;
257261
}
258262

259263
/* pad upto 56 bytes of zeroes */
260-
while (md->sha1_x86.curlen < 56) {
261-
md->sha1_x86.buf[md->sha1_x86.curlen++] = (unsigned char)0;
264+
while (md->sha1.curlen < 56) {
265+
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
262266
}
263267

264268
/* store length */
265-
STORE64H(md->sha1_x86.length, md->sha1_x86.buf+56);
266-
s_sha1_x86_compress(md, md->sha1_x86.buf);
269+
STORE64H(md->sha1.length, md->sha1.buf+56);
270+
s_sha1_x86_compress(md, md->sha1.buf);
267271

268272
/* copy output */
269273
for (i = 0; i < 5; i++) {
270-
STORE32H(md->sha1_x86.state[i], out+(4*i));
274+
STORE32H(md->sha1.state[i], out+(4*i));
271275
}
272276
#ifdef LTC_CLEAN_STACK
273277
zeromem(md, sizeof(hash_state));

src/headers/tomcrypt_hash.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,14 @@ LTC_ALIGN_AS(16) struct sha256_x86_state {
6060
#endif
6161

6262
#ifdef LTC_SHA1
63-
struct sha1_c_state {
63+
struct sha1_state {
6464
ulong64 length;
65-
ulong32 state[5], curlen;
65+
ulong32 *state, curlen;
6666
unsigned char buf[64];
67+
unsigned char state_buf[LTC_ALIGNED_BUF_SIZE(ulong32, 5, 16)];
6768
};
6869
#endif
6970

70-
#ifdef LTC_SHA1_X86
71-
#pragma pack(push)
72-
#pragma pack(16)
73-
struct sha1_x86_state {
74-
ulong32 state[5];
75-
ulong32 curlen;
76-
ulong64 length;
77-
unsigned char buf[64];
78-
};
79-
#pragma pack(pop)
80-
#endif
81-
8271
#ifdef LTC_MD5
8372
struct md5_state {
8473
ulong64 length;
@@ -206,10 +195,7 @@ typedef union Hash_state {
206195
struct sha256_x86_state sha256_x86;
207196
#endif
208197
#ifdef LTC_SHA1
209-
struct sha1_c_state sha1_c;
210-
#endif
211-
#ifdef LTC_SHA1_X86
212-
struct sha1_x86_state sha1_x86;
198+
struct sha1_state sha1;
213199
#endif
214200
#ifdef LTC_MD5
215201
struct md5_state md5;

src/misc/crypt/crypt_sizes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static const crypt_size s_crypt_sizes[] = {
4040
SZ_STRINGIFY_S(sha256_c_state),
4141
#endif
4242
#ifdef LTC_SHA1
43-
SZ_STRINGIFY_S(sha1_c_state),
43+
SZ_STRINGIFY_S(sha1_state),
4444
#endif
4545
#ifdef LTC_MD5
4646
SZ_STRINGIFY_S(md5_state),

0 commit comments

Comments
 (0)