Skip to content

Commit 1ee37a5

Browse files
committed
Some minor SHA1/256 improvements&fixes.
* Use a shared init function. * Fix some functions used in descriptors and implementation. Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 22e04f8 commit 1ee37a5

9 files changed

Lines changed: 56 additions & 110 deletions

File tree

src/hashes/sha1.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const struct ltc_hash_descriptor sha1_portable_desc =
2929
{ 1, 3, 14, 3, 2, 26, },
3030
6,
3131

32-
&sha1_c_init,
32+
&sha1_init,
3333
&sha1_c_process,
3434
&sha1_c_done,
3535
&sha1_c_test,
@@ -186,18 +186,7 @@ static int s_sha1_c_compress(hash_state *md, const unsigned char *buf)
186186
*/
187187
int sha1_c_init(hash_state * md)
188188
{
189-
LTC_ARGCHK(md != NULL);
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;
200-
return CRYPT_OK;
189+
return sha1_init(md);
201190
}
202191

203192
/**

src/hashes/sha1_desc.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,18 @@ static LTC_INLINE int s_sha1_x86_is_supported(void)
7979
*/
8080
int sha1_init(hash_state * md)
8181
{
82-
#if defined LTC_SHA1_X86
83-
if(s_sha1_x86_is_supported()) {
84-
return sha1_x86_init(md);
85-
}
86-
#endif
87-
return sha1_c_init(md);
82+
LTC_ARGCHK(md != NULL);
83+
84+
md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);
85+
86+
md->sha1.state[0] = 0x67452301UL;
87+
md->sha1.state[1] = 0xefcdab89UL;
88+
md->sha1.state[2] = 0x98badcfeUL;
89+
md->sha1.state[3] = 0x10325476UL;
90+
md->sha1.state[4] = 0xc3d2e1f0UL;
91+
md->sha1.curlen = 0;
92+
md->sha1.length = 0;
93+
return CRYPT_OK;
8894
}
8995

9096
/**

src/hashes/sha1_x86.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const struct ltc_hash_descriptor sha1_x86_desc =
3838
{ 1, 3, 14, 3, 2, 26, },
3939
6,
4040

41-
&sha1_x86_init,
41+
&sha1_init,
4242
&sha1_x86_process,
4343
&sha1_x86_done,
4444
&sha1_x86_test,
@@ -198,18 +198,7 @@ static int s_sha1_x86_compress(hash_state *md, const unsigned char *buf)
198198
*/
199199
int sha1_x86_init(hash_state * md)
200200
{
201-
LTC_ARGCHK(md != NULL);
202-
203-
md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);
204-
205-
md->sha1.state[0] = 0x67452301UL;
206-
md->sha1.state[1] = 0xefcdab89UL;
207-
md->sha1.state[2] = 0x98badcfeUL;
208-
md->sha1.state[3] = 0x10325476UL;
209-
md->sha1.state[4] = 0xc3d2e1f0UL;
210-
md->sha1.curlen = 0;
211-
md->sha1.length = 0;
212-
return CRYPT_OK;
201+
return sha1_init(md);
213202
}
214203

215204
/**

src/hashes/sha2/sha224.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const struct ltc_hash_descriptor sha224_portable_desc =
2020
{ 2, 16, 840, 1, 101, 3, 4, 2, 4, },
2121
9,
2222

23-
&sha224_c_init,
23+
&sha224_init,
2424
&sha256_c_process,
2525
&sha224_c_done,
26-
&sha224_test,
26+
&sha224_c_test,
2727
NULL
2828
};
2929

@@ -35,21 +35,7 @@ const struct ltc_hash_descriptor sha224_portable_desc =
3535
*/
3636
int sha224_c_init(hash_state * md)
3737
{
38-
LTC_ARGCHK(md != NULL);
39-
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;
52-
return CRYPT_OK;
38+
return sha224_init(md);
5339
}
5440

5541
/**
@@ -66,7 +52,7 @@ int sha224_c_done(hash_state * md, unsigned char *out)
6652
LTC_ARGCHK(md != NULL);
6753
LTC_ARGCHK(out != NULL);
6854

69-
err = sha256_done(md, buf);
55+
err = sha256_c_done(md, buf);
7056
XMEMCPY(out, buf, 28);
7157
#ifdef LTC_CLEAN_STACK
7258
zeromem(buf, sizeof(buf));

src/hashes/sha2/sha224_desc.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,21 @@ static LTC_INLINE int s_sha224_x86_is_supported(void)
8585
*/
8686
int sha224_init(hash_state * md)
8787
{
88-
#if defined LTC_SHA224_X86
89-
if(s_sha224_x86_is_supported()) {
90-
return sha224_x86_init(md);
91-
}
92-
#endif
93-
return sha224_c_init(md);
88+
LTC_ARGCHK(md != NULL);
89+
90+
md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
91+
92+
md->sha256.curlen = 0;
93+
md->sha256.length = 0;
94+
md->sha256.state[0] = 0xc1059ed8UL;
95+
md->sha256.state[1] = 0x367cd507UL;
96+
md->sha256.state[2] = 0x3070dd17UL;
97+
md->sha256.state[3] = 0xf70e5939UL;
98+
md->sha256.state[4] = 0xffc00b31UL;
99+
md->sha256.state[5] = 0x68581511UL;
100+
md->sha256.state[6] = 0x64f98fa7UL;
101+
md->sha256.state[7] = 0xbefa4fa4UL;
102+
return CRYPT_OK;
94103
}
95104

96105
/**

src/hashes/sha2/sha224_x86.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const struct ltc_hash_descriptor sha224_x86_desc =
2020
{ 2, 16, 840, 1, 101, 3, 4, 2, 4, },
2121
9,
2222

23-
&sha224_x86_init,
23+
&sha224_init,
2424
&sha256_x86_process,
2525
&sha224_x86_done,
2626
&sha224_x86_test,
@@ -35,21 +35,7 @@ const struct ltc_hash_descriptor sha224_x86_desc =
3535
*/
3636
int sha224_x86_init(hash_state * md)
3737
{
38-
LTC_ARGCHK(md != NULL);
39-
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;
52-
return CRYPT_OK;
38+
return sha224_init(md);
5339
}
5440

5541
/**

src/hashes/sha2/sha256.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const struct ltc_hash_descriptor sha256_portable_desc =
2020
{ 2, 16, 840, 1, 101, 3, 4, 2, 1, },
2121
9,
2222

23-
&sha256_c_init,
23+
&sha256_init,
2424
&sha256_c_process,
2525
&sha256_c_done,
2626
&sha256_c_test,
@@ -233,21 +233,7 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
233233
*/
234234
int sha256_c_init(hash_state * md)
235235
{
236-
LTC_ARGCHK(md != NULL);
237-
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;
250-
return CRYPT_OK;
236+
return sha256_init(md);
251237
}
252238

253239
/**

src/hashes/sha2/sha256_desc.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,21 @@ const struct ltc_hash_descriptor sha256_desc =
8888
*/
8989
int sha256_init(hash_state * md)
9090
{
91-
#if defined LTC_SHA256_X86
92-
if(s_sha256_x86_is_supported()) {
93-
return sha256_x86_init(md);
94-
}
95-
#endif
96-
return sha256_c_init(md);
91+
LTC_ARGCHK(md != NULL);
92+
93+
md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
94+
95+
md->sha256.curlen = 0;
96+
md->sha256.length = 0;
97+
md->sha256.state[0] = 0x6A09E667UL;
98+
md->sha256.state[1] = 0xBB67AE85UL;
99+
md->sha256.state[2] = 0x3C6EF372UL;
100+
md->sha256.state[3] = 0xA54FF53AUL;
101+
md->sha256.state[4] = 0x510E527FUL;
102+
md->sha256.state[5] = 0x9B05688CUL;
103+
md->sha256.state[6] = 0x1F83D9ABUL;
104+
md->sha256.state[7] = 0x5BE0CD19UL;
105+
return CRYPT_OK;
97106
}
98107

99108
/**

src/hashes/sha2/sha256_x86.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const struct ltc_hash_descriptor sha256_x86_desc =
3737
{ 2, 16, 840, 1, 101, 3, 4, 2, 1, },
3838
9,
3939

40-
&sha256_x86_init,
40+
&sha256_init,
4141
&sha256_x86_process,
4242
&sha256_x86_done,
4343
&sha256_x86_test,
@@ -265,21 +265,7 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
265265
*/
266266
int sha256_x86_init(hash_state * md)
267267
{
268-
LTC_ARGCHK(md != NULL);
269-
270-
md->sha256.state = LTC_ALIGN_BUF(md->sha256.state_buf, 16);
271-
272-
md->sha256.curlen = 0;
273-
md->sha256.length = 0;
274-
md->sha256.state[0] = 0x6A09E667UL;
275-
md->sha256.state[1] = 0xBB67AE85UL;
276-
md->sha256.state[2] = 0x3C6EF372UL;
277-
md->sha256.state[3] = 0xA54FF53AUL;
278-
md->sha256.state[4] = 0x510E527FUL;
279-
md->sha256.state[5] = 0x9B05688CUL;
280-
md->sha256.state[6] = 0x1F83D9ABUL;
281-
md->sha256.state[7] = 0x5BE0CD19UL;
282-
return CRYPT_OK;
268+
return sha256_init(md);
283269
}
284270

285271
/**

0 commit comments

Comments
 (0)