Skip to content

Commit 37ec4a2

Browse files
committed
Minor changes of SHA224/256 integration.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 6dc9519 commit 37ec4a2

6 files changed

Lines changed: 23 additions & 85 deletions

File tree

src/hashes/sha2/sha224.c

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#if defined(LTC_SHA224) && defined(LTC_SHA256)
1111

12-
const struct ltc_hash_descriptor sha224_c_desc =
12+
const struct ltc_hash_descriptor sha224_portable_desc =
1313
{
1414
"sha224",
1515
10,
@@ -21,7 +21,7 @@ const struct ltc_hash_descriptor sha224_c_desc =
2121
9,
2222

2323
&sha224_c_init,
24-
&sha256_process,
24+
&sha256_c_process,
2525
&sha224_c_done,
2626
&sha224_test,
2727
NULL
@@ -78,41 +78,7 @@ int sha224_c_done(hash_state * md, unsigned char *out)
7878
*/
7979
int sha224_c_test(void)
8080
{
81-
#ifndef LTC_TEST
82-
return CRYPT_NOP;
83-
#else
84-
static const struct {
85-
const char *msg;
86-
unsigned char hash[28];
87-
} tests[] = {
88-
{ "abc",
89-
{ 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8,
90-
0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2,
91-
0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd,
92-
0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 }
93-
},
94-
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
95-
{ 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76,
96-
0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89,
97-
0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4,
98-
0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 }
99-
},
100-
};
101-
102-
int i;
103-
unsigned char tmp[28];
104-
hash_state md;
105-
106-
for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
107-
sha224_c_init(&md);
108-
sha224_c_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
109-
sha224_c_done(&md, tmp);
110-
if (ltc_compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA224", i)) {
111-
return CRYPT_FAIL_TESTVECTOR;
112-
}
113-
}
114-
return CRYPT_OK;
115-
#endif
81+
return sha224_test_desc(&sha224_portable_desc, "SHA224 portable");
11682
}
11783

11884
#endif /* defined(LTC_SHA224) && defined(LTC_SHA256) */

src/hashes/sha2/sha256.c

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#ifdef LTC_SHA256
1111

12-
const struct ltc_hash_descriptor sha256_c_desc =
12+
const struct ltc_hash_descriptor sha256_portable_desc =
1313
{
1414
"sha256",
1515
0,
@@ -318,41 +318,7 @@ int sha256_c_done(hash_state * md, unsigned char *out)
318318
*/
319319
int sha256_c_test(void)
320320
{
321-
#ifndef LTC_TEST
322-
return CRYPT_NOP;
323-
#else
324-
static const struct {
325-
const char *msg;
326-
unsigned char hash[32];
327-
} tests[] = {
328-
{ "abc",
329-
{ 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
330-
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
331-
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
332-
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }
333-
},
334-
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
335-
{ 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
336-
0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
337-
0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
338-
0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }
339-
},
340-
};
341-
342-
int i;
343-
unsigned char tmp[32];
344-
hash_state md;
345-
346-
for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
347-
sha256_c_init(&md);
348-
sha256_c_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
349-
sha256_c_done(&md, tmp);
350-
if (ltc_compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) {
351-
return CRYPT_FAIL_TESTVECTOR;
352-
}
353-
}
354-
return CRYPT_OK;
355-
#endif
321+
return sha256_test_desc(&sha256_portable_desc, "SHA256 portable");
356322
}
357323

358324
#undef Ch

src/hashes/sha2/sha256_x86.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const struct ltc_hash_descriptor sha256_x86_desc =
4343
};
4444

4545
/* the K array */
46+
#define K sha256_x86_K
4647
#pragma pack(push)
4748
#pragma pack(16) /* todo #pragma pack might not work */
4849
static const ulong32 K[64] = {
@@ -253,6 +254,7 @@ static int ltc_attribute_sha256 s_sha256_x86_compress(hash_state * md, const uns
253254
_mm_store_si128(((__m128i*)(&md->sha256_x86.state[4])), state_1);
254255
return CRYPT_OK;
255256
}
257+
#undef K
256258

257259
#ifdef LTC_CLEAN_STACK
258260
static int s_sha256_compress(hash_state * md, const unsigned char *buf)

src/hashes/sha2/sha512.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const struct ltc_hash_descriptor sha512_desc =
2828
};
2929

3030
/* the K array */
31+
#define K sha512_K
3132
static const ulong64 K[80] = {
3233
CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd),
3334
CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc),
@@ -186,6 +187,7 @@ static int s_sha512_compress(hash_state * md, const unsigned char *buf)
186187

187188
return CRYPT_OK;
188189
}
190+
#undef K
189191

190192
/* compress 1024-bits */
191193
#ifdef LTC_CLEAN_STACK

src/headers/tomcrypt_hash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ int sha256_c_init(hash_state * md);
413413
int sha256_c_process(hash_state * md, const unsigned char *in, unsigned long inlen);
414414
int sha256_c_done(hash_state * md, unsigned char *out);
415415
int sha256_c_test(void);
416-
extern const struct ltc_hash_descriptor sha256_c_desc;
416+
extern const struct ltc_hash_descriptor sha256_portable_desc;
417417

418418
#ifdef LTC_SHA256_X86
419419
int sha256_x86_init(hash_state * md);
@@ -437,7 +437,7 @@ int sha224_c_init(hash_state * md);
437437
#define sha224_c_process sha256_c_process
438438
int sha224_c_done(hash_state * md, unsigned char *out);
439439
int sha224_c_test(void);
440-
extern const struct ltc_hash_descriptor sha224_c_desc;
440+
extern const struct ltc_hash_descriptor sha224_portable_desc;
441441

442442
#ifdef LTC_SHA224_X86
443443
int sha224_x86_init(hash_state * md);

tests/test.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,24 +204,26 @@ static void s_unregister_all(void)
204204
unregister_hash(&md5_desc);
205205
#endif
206206
#ifdef LTC_SHA1
207+
/* `register_all_hashes()` does not register
208+
* - `sha1_portable_desc`
209+
* - `sha1_x86_desc`
210+
* so we don't have to unregister them */
207211
unregister_hash(&sha1_desc);
208-
unregister_hash(&sha1_portable_desc);
209-
#endif
210-
#ifdef LTC_SHA1_X86
211-
unregister_hash(&sha1_x86_desc);
212212
#endif
213213
#ifdef LTC_SHA224
214+
/* `register_all_hashes()` does not register
215+
* - `sha224_portable_desc`
216+
* - `sha224_x86_desc`
217+
* so we don't have to unregister them */
214218
unregister_hash(&sha224_desc);
215219
#endif
216-
#ifdef LTC_SHA224_X86
217-
unregister_hash(&sha224_x86_desc);
218-
#endif
219220
#ifdef LTC_SHA256
221+
/* `register_all_hashes()` does not register
222+
* - `sha256_portable_desc`
223+
* - `sha256_x86_desc`
224+
* so we don't have to unregister them */
220225
unregister_hash(&sha256_desc);
221226
#endif
222-
#ifdef LTC_SHA256_X86
223-
unregister_hash(&sha256_x86_desc);
224-
#endif
225227
#ifdef LTC_SHA384
226228
unregister_hash(&sha384_desc);
227229
#endif

0 commit comments

Comments
 (0)