Skip to content

Commit 1c21758

Browse files
ifranzkip-steuer
authored andcommitted
Add testcases for SHA-512/224 and SHA-512/256
Signed-off-by: Ingo Franzki <[email protected]>
1 parent 90446f2 commit 1c21758

File tree

3 files changed

+1223
-3
lines changed

3 files changed

+1223
-3
lines changed

test/sha_test.c

Lines changed: 181 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ static int sha224_new_api_test(test_t * test);
7070
static int sha256_new_api_test(test_t * test);
7171
static int sha384_new_api_test(test_t * test);
7272
static int sha512_new_api_test(test_t * test);
73+
static int sha512_224_new_api_test(test_t * test);
74+
static int sha512_256_new_api_test(test_t * test);
7375

7476
static int sha3_224_api_test(test_t * test);
7577
static int sha3_256_api_test(test_t * test);
@@ -159,6 +161,14 @@ int main(int argc, char *argv[])
159161
V_(printf("SHA512 ...\n"));
160162
rc = sha512_new_api_test(curr_test);
161163
break;
164+
case SHA512_224:
165+
V_(printf("SHA512/224 ...\n"));
166+
rc = sha512_224_new_api_test(curr_test);
167+
break;
168+
case SHA512_256:
169+
V_(printf("SHA512/256 ...\n"));
170+
rc = sha512_256_new_api_test(curr_test);
171+
break;
162172
case SHA3_224:
163173
V_(printf("SHA3-224 ...\n"));
164174
rc = sha3_224_api_test(curr_test);
@@ -266,19 +276,25 @@ static int read_test_data(FILE * test_data, int sha3_flag)
266276
test_t tmp_test = new_test_t();
267277
unsigned int current_type = NO_TYPE_SET;
268278
unsigned int current_msg_digest_length = NO_LENGTH_SET;
279+
char parsed_type[20];
269280

270281
unsigned int line_number = 0;
271282

272283
char *tmp = NULL;
284+
char *tmp2 = NULL;
273285
search_term = MSG_LENGTH;
286+
memset(parsed_type, 0, sizeof(parsed_type));
274287

275288
while (fgets(buffer, (int)sizeof buffer, test_data) != NULL) {
276289

277290
line_number++;
278291

279292
/* remove comments */
280-
if ((tmp = memchr(buffer, (int)'#', strlen(buffer))) != NULL)
293+
if ((tmp = memchr(buffer, (int)'#', strlen(buffer))) != NULL) {
294+
if ((tmp2 = strstr(buffer, "SHA-512/")) != NULL)
295+
strncpy(parsed_type, tmp2, strlen("SHA-512/XXX"));
281296
memset(tmp, 0, strlen(tmp));
297+
}
282298

283299
/* scan for: type/msg_digest_length */
284300
if (((sscanf(buffer, "[L = %u]", &current_msg_digest_length))
@@ -302,10 +318,14 @@ static int read_test_data(FILE * test_data, int sha3_flag)
302318
current_type = SHA1;
303319
break;
304320
case SHA224_HASH_LENGTH:
305-
current_type = sha3_flag ? SHA3_224 : SHA224;
321+
current_type = sha3_flag ? SHA3_224 :
322+
strcmp(parsed_type, "SHA-512/224") == 0
323+
? SHA512_224 : SHA224;
306324
break;
307325
case SHA256_HASH_LENGTH:
308-
current_type = sha3_flag ? SHA3_256 : SHA256;
326+
current_type = sha3_flag ? SHA3_256 :
327+
strcmp(parsed_type, "SHA-512/256") == 0
328+
? SHA512_256 : SHA256;
309329
break;
310330
case SHA384_HASH_LENGTH:
311331
current_type = sha3_flag ? SHA3_384 : SHA384;
@@ -849,6 +869,164 @@ static int sha512_new_api_test(test_t * test)
849869
return TEST_SUCC;
850870
}
851871

872+
static int sha512_224_new_api_test(test_t * test)
873+
{
874+
sha512_context_t sha512_context;
875+
int rc = 0;
876+
size_t off;
877+
unsigned char output[SHA512_224_HASH_LENGTH];
878+
time_t seed;
879+
int i;
880+
881+
srand(time(&seed));
882+
883+
if (test->msg_digest_length != SHA512_224_HASH_LENGTH)
884+
CRITICAL_ERROR("this shouldn't happen.");
885+
886+
rc = (int)ica_sha512_224(SHA_MSG_PART_ONLY, test->msg_length, test->msg,
887+
&sha512_context, output);
888+
889+
if (rc != 0) {
890+
V_(printf("ica_sha512_224 failed with errno %d (0x%x).\n", rc,
891+
(unsigned int)rc));
892+
return TEST_FAIL;
893+
}
894+
895+
VV_(printf("message digest (new api)\n"));
896+
dump_array(output, SHA512_224_HASH_LENGTH);
897+
898+
if (memcmp(output, test->msg_digest, SHA512_224_HASH_LENGTH) != 0) {
899+
V_(printf("output is not what it should be.\n"));
900+
return TEST_FAIL;
901+
}
902+
903+
if (test->msg_length <= SHA512_BLOCK_SIZE)
904+
return TEST_SUCC;
905+
906+
rc = (int)ica_sha512_224(SHA_MSG_PART_FIRST, SHA512_BLOCK_SIZE,
907+
test->msg, &sha512_context, output);
908+
if (rc != 0) {
909+
V_(printf("ica_sha512_224 %s failed with errno %d (0x%x).\n",
910+
"SHA_MSG_PART_FIRST", rc, (unsigned int)rc));
911+
return TEST_FAIL;
912+
}
913+
914+
for (off = SHA512_BLOCK_SIZE;
915+
off < test->msg_length - SHA512_BLOCK_SIZE;) {
916+
i = rand()
917+
% ((test->msg_length - off) / SHA512_BLOCK_SIZE + 1);
918+
rc = (int)ica_sha512_224(SHA_MSG_PART_MIDDLE,
919+
i * SHA512_BLOCK_SIZE,
920+
test->msg + off,
921+
&sha512_context, output);
922+
if (rc != 0) {
923+
V_(printf("ica_sha512_224 %s failed"
924+
" with errno %d (0x%x).\n",
925+
"SHA_MSG_PART_MIDDLE", rc,
926+
(unsigned int)rc));
927+
return TEST_FAIL;
928+
}
929+
off += i * SHA512_BLOCK_SIZE;
930+
}
931+
932+
rc = (int)ica_sha512_224(SHA_MSG_PART_FINAL, test->msg_length - off,
933+
test->msg + off, &sha512_context, output);
934+
if (rc != 0) {
935+
V_(printf("ica_sha512_224 %s failed with errno %d (0x%x).\n",
936+
"SHA_MSG_PART_FINAL", rc, (unsigned int)rc));
937+
return TEST_FAIL;
938+
}
939+
940+
VV_(printf("message digest\n"));
941+
dump_array(output, SHA512_224_HASH_LENGTH);
942+
943+
if (memcmp(output, test->msg_digest, SHA512_224_HASH_LENGTH) != 0) {
944+
V_(printf("output is not what it should be.\n"));
945+
return TEST_FAIL;
946+
}
947+
948+
return TEST_SUCC;
949+
}
950+
951+
static int sha512_256_new_api_test(test_t * test)
952+
{
953+
sha512_context_t sha512_context;
954+
int rc = 0;
955+
size_t off;
956+
unsigned char output[SHA512_256_HASH_LENGTH];
957+
time_t seed;
958+
int i;
959+
960+
srand(time(&seed));
961+
962+
if (test->msg_digest_length != SHA512_256_HASH_LENGTH)
963+
CRITICAL_ERROR("this shouldn't happen.");
964+
965+
rc = (int)ica_sha512_256(SHA_MSG_PART_ONLY, test->msg_length, test->msg,
966+
&sha512_context, output);
967+
968+
if (rc != 0) {
969+
V_(printf("ica_sha512_256 failed with errno %d (0x%x).\n", rc,
970+
(unsigned int)rc));
971+
return TEST_FAIL;
972+
}
973+
974+
VV_(printf("message digest (new api)\n"));
975+
dump_array(output, SHA512_256_HASH_LENGTH);
976+
977+
if (memcmp(output, test->msg_digest, SHA512_256_HASH_LENGTH) != 0) {
978+
V_(printf("output is not what it should be.\n"));
979+
return TEST_FAIL;
980+
}
981+
982+
if (test->msg_length <= SHA512_BLOCK_SIZE)
983+
return TEST_SUCC;
984+
985+
rc = (int)ica_sha512_256(SHA_MSG_PART_FIRST, SHA512_BLOCK_SIZE,
986+
test->msg, &sha512_context, output);
987+
if (rc != 0) {
988+
V_(printf("ica_sha512_256 %s failed with errno %d (0x%x).\n",
989+
"SHA_MSG_PART_FIRST", rc, (unsigned int)rc));
990+
return TEST_FAIL;
991+
}
992+
993+
for (off = SHA512_BLOCK_SIZE;
994+
off < test->msg_length - SHA512_BLOCK_SIZE;) {
995+
i = rand()
996+
% ((test->msg_length - off) / SHA512_BLOCK_SIZE + 1);
997+
rc = (int)ica_sha512_256(SHA_MSG_PART_MIDDLE,
998+
i * SHA512_BLOCK_SIZE,
999+
test->msg + off,
1000+
&sha512_context, output);
1001+
if (rc != 0) {
1002+
V_(printf("ica_sha512_256 %s failed"
1003+
" with errno %d (0x%x).\n",
1004+
"SHA_MSG_PART_MIDDLE", rc,
1005+
(unsigned int)rc));
1006+
return TEST_FAIL;
1007+
}
1008+
off += i * SHA512_BLOCK_SIZE;
1009+
}
1010+
1011+
rc = (int)ica_sha512_256(SHA_MSG_PART_FINAL, test->msg_length - off,
1012+
test->msg + off, &sha512_context, output);
1013+
if (rc != 0) {
1014+
V_(printf("ica_sha512_256 %s failed with errno %d (0x%x).\n",
1015+
"SHA_MSG_PART_FINAL", rc, (unsigned int)rc));
1016+
return TEST_FAIL;
1017+
}
1018+
1019+
VV_(printf("message digest\n"));
1020+
dump_array(output, SHA512_256_HASH_LENGTH);
1021+
1022+
if (memcmp(output, test->msg_digest, SHA512_256_HASH_LENGTH) != 0) {
1023+
V_(printf("output is not what it should be.\n"));
1024+
return TEST_FAIL;
1025+
}
1026+
1027+
return TEST_SUCC;
1028+
}
1029+
8521030
static int sha3_224_api_test(test_t * test)
8531031
{
8541032
sha3_224_context_t sha3_224_context;

0 commit comments

Comments
 (0)