Skip to content

Commit f689fbb

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

4 files changed

Lines changed: 68 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: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ static int ltc_attribute_sha1 s_sha1_x86_compress(hash_state *md, const unsigned
6464

6565
LTC_ARGCHK(md != NULL);
6666
LTC_ARGCHK(buf != NULL);
67-
LTC_ARGCHK(((uintptr_t)(&md->sha1_x86.state[0])) % 16 == 0);
67+
LTC_ARGCHK(((uintptr_t)(&md->sha1.state[0])) % 16 == 0);
6868
LTC_ARGCHK(sizeof(int) == 4);
6969

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

7575
old_abcd = abcdx;
7676
old_e = e;
@@ -176,8 +176,8 @@ static int ltc_attribute_sha1 s_sha1_x86_compress(hash_state *md, const unsigned
176176
e = _mm_add_epi32(e, old_e);
177177

178178
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);
179+
_mm_store_si128(((__m128i*)(&md->sha1.state[0])), abcdx);
180+
*((int*)(&md->sha1.state[4])) = _mm_extract_epi32(e, 3);
181181

182182
return CRYPT_OK;
183183

@@ -202,13 +202,16 @@ static int s_sha1_x86_compress(hash_state *md, const unsigned char *buf)
202202
int sha1_x86_init(hash_state * md)
203203
{
204204
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;
205+
206+
md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);
207+
208+
md->sha1.state[0] = 0x67452301UL;
209+
md->sha1.state[1] = 0xefcdab89UL;
210+
md->sha1.state[2] = 0x98badcfeUL;
211+
md->sha1.state[3] = 0x10325476UL;
212+
md->sha1.state[4] = 0xc3d2e1f0UL;
213+
md->sha1.curlen = 0;
214+
md->sha1.length = 0;
212215
return CRYPT_OK;
213216
}
214217

@@ -219,7 +222,7 @@ int sha1_x86_init(hash_state * md)
219222
@param inlen The length of the data (octets)
220223
@return CRYPT_OK if successful
221224
*/
222-
HASH_PROCESS(sha1_x86_process, s_sha1_x86_compress, sha1_x86, 64)
225+
HASH_PROCESS(sha1_x86_process, s_sha1_x86_compress, sha1, 64)
223226

224227
/**
225228
Terminate the hash to get the digest
@@ -234,40 +237,40 @@ int sha1_x86_done(hash_state * md, unsigned char *out)
234237
LTC_ARGCHK(md != NULL);
235238
LTC_ARGCHK(out != NULL);
236239

237-
if (md->sha1_x86.curlen >= ((int)(sizeof(md->sha1_x86.buf)))) {
240+
if (md->sha1.curlen >= ((int)(sizeof(md->sha1.buf)))) {
238241
return CRYPT_INVALID_ARG;
239242
}
240243

241244
/* increase the length of the message */
242-
md->sha1_x86.length += md->sha1_x86.curlen * 8;
245+
md->sha1.length += md->sha1.curlen * 8;
243246

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

247250
/* if the length is currently above 56 bytes we append zeros
248251
* then compress. Then we can fall back to padding zeros and length
249252
* encoding like normal.
250253
*/
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;
254+
if (md->sha1.curlen > 56) {
255+
while (md->sha1.curlen < 64) {
256+
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
254257
}
255-
s_sha1_x86_compress(md, md->sha1_x86.buf);
256-
md->sha1_x86.curlen = 0;
258+
s_sha1_x86_compress(md, md->sha1.buf);
259+
md->sha1.curlen = 0;
257260
}
258261

259262
/* 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;
263+
while (md->sha1.curlen < 56) {
264+
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
262265
}
263266

264267
/* store length */
265-
STORE64H(md->sha1_x86.length, md->sha1_x86.buf+56);
266-
s_sha1_x86_compress(md, md->sha1_x86.buf);
268+
STORE64H(md->sha1.length, md->sha1.buf+56);
269+
s_sha1_x86_compress(md, md->sha1.buf);
267270

268271
/* copy output */
269272
for (i = 0; i < 5; i++) {
270-
STORE32H(md->sha1_x86.state[i], out+(4*i));
273+
STORE32H(md->sha1.state[i], out+(4*i));
271274
}
272275
#ifdef LTC_CLEAN_STACK
273276
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)