From f46074c7b9d03235e2968546c28a6057db9f8725 Mon Sep 17 00:00:00 2001 From: Pauli Date: Mon, 10 Apr 2017 09:14:49 +1000 Subject: [PATCH] Update sha256t and sha512t to use the test infrastructure Reviewed-by: Rich Salz Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/3168) --- test/build.info | 4 +- test/sha256t.c | 217 ++++++++++++++++++++++------------------------ test/sha512t.c | 225 +++++++++++++++++++++++------------------------- 3 files changed, 214 insertions(+), 232 deletions(-) diff --git a/test/build.info b/test/build.info index d712b71805..cc0820d1e4 100644 --- a/test/build.info +++ b/test/build.info @@ -103,11 +103,11 @@ IF[{- !$disabled{tests} -}] INCLUDE[sha1test]=../include DEPEND[sha1test]=../libcrypto - SOURCE[sha256t]=sha256t.c + SOURCE[sha256t]=sha256t.c testutil.c test_main.c INCLUDE[sha256t]=../include DEPEND[sha256t]=../libcrypto - SOURCE[sha512t]=sha512t.c + SOURCE[sha512t]=sha512t.c testutil.c test_main.c INCLUDE[sha512t]=../include DEPEND[sha512t]=../libcrypto diff --git a/test/sha256t.c b/test/sha256t.c index 90262d9624..29e65d5ed0 100644 --- a/test/sha256t.c +++ b/test/sha256t.c @@ -1,5 +1,5 @@ /* - * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2004-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the OpenSSL license (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -7,13 +7,12 @@ * https://www.openssl.org/source/license.html */ -#include -#include -#include - #include #include +#include "test_main.h" +#include "testutil.h" + static const unsigned char app_b1[SHA256_DIGEST_LENGTH] = { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, @@ -56,122 +55,114 @@ static const unsigned char addenum_3[SHA224_DIGEST_LENGTH] = { 0x4e, 0xe7, 0xad, 0x67 }; -int main(int argc, char **argv) +static int test_sha256_short(void) +{ + unsigned char md[SHA256_DIGEST_LENGTH]; + + if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha256(), NULL))) + return 0; + return TEST_mem_eq(md, sizeof(md), app_b1, sizeof(app_b1)); +} + +static int test_sha256_long(void) { unsigned char md[SHA256_DIGEST_LENGTH]; - int i; - EVP_MD_CTX *evp; - fprintf(stdout, "Testing SHA-256 "); - - if (!EVP_Digest("abc", 3, md, NULL, EVP_sha256(), NULL)) - goto err; - if (memcmp(md, app_b1, sizeof(app_b1))) { - fflush(stdout); - fprintf(stderr, "\nTEST 1 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); - - if (!EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk" - "ijkljklm" "klmnlmno" "mnopnopq", 56, md, - NULL, EVP_sha256(), NULL)) - goto err; - if (memcmp(md, app_b2, sizeof(app_b2))) { - fflush(stdout); - fprintf(stderr, "\nTEST 2 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); + if (!TEST_true(EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk" + "ijkljklm" "klmnlmno" "mnopnopq", 56, md, + NULL, EVP_sha256(), NULL))) + return 0; + return TEST_mem_eq(md, sizeof(md), app_b2, sizeof(app_b2)); +} + +static int test_sha256_multi(void) +{ + unsigned char md[SHA256_DIGEST_LENGTH]; + int i, testresult = 0; + EVP_MD_CTX *evp; + static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"; evp = EVP_MD_CTX_new(); - if (evp == NULL) { - fflush(stdout); - fprintf(stderr, "\nTEST 3 of 3 failed. (malloc failure)\n"); - return 1; - } - if (!EVP_DigestInit_ex(evp, EVP_sha256(), NULL)) - goto err; - for (i = 0; i < 1000000; i += 288) { - if (!EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa", - (1000000 - i) < 288 ? 1000000 - i : 288)) - goto err; - } - if (!EVP_DigestFinal_ex(evp, md, NULL)) - goto err; - - if (memcmp(md, app_b3, sizeof(app_b3))) { - fflush(stdout); - fprintf(stderr, "\nTEST 3 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); - - fprintf(stdout, " passed.\n"); - fflush(stdout); - - fprintf(stdout, "Testing SHA-224 "); - - if (!EVP_Digest("abc", 3, md, NULL, EVP_sha224(), NULL)) - goto err; - if (memcmp(md, addenum_1, sizeof(addenum_1))) { - fflush(stdout); - fprintf(stderr, "\nTEST 1 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); - - if (!EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk" - "ijkljklm" "klmnlmno" "mnopnopq", 56, md, - NULL, EVP_sha224(), NULL)) - goto err; - if (memcmp(md, addenum_2, sizeof(addenum_2))) { - fflush(stdout); - fprintf(stderr, "\nTEST 2 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); - - EVP_MD_CTX_reset(evp); - if (!EVP_DigestInit_ex(evp, EVP_sha224(), NULL)) - goto err; - for (i = 0; i < 1000000; i += 64) { - if (!EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa", - (1000000 - i) < 64 ? 1000000 - i : 64)) - goto err; - } - if (!EVP_DigestFinal_ex(evp, md, NULL)) - goto err; + if (!TEST_ptr(evp)) + return 0; + if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha256(), NULL))) + goto end; + for (i = 0; i < 1000000; i += 288) + if (!TEST_true(EVP_DigestUpdate(evp, updstr, + (1000000 - i) < 288 ? 1000000 - i + : 288))) + goto end; + if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL)) + || !TEST_mem_eq(md, sizeof(md), app_b3, sizeof(app_b3))) + goto end; + + testresult = 1; + end: EVP_MD_CTX_free(evp); + return testresult; +} - if (memcmp(md, addenum_3, sizeof(addenum_3))) { - fflush(stdout); - fprintf(stderr, "\nTEST 3 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); +static int test_sha224_short(void) +{ + unsigned char md[SHA224_DIGEST_LENGTH]; - fprintf(stdout, " passed.\n"); - fflush(stdout); + if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha224(), NULL))) + return 0; + return TEST_mem_eq(md, sizeof(md), addenum_1, sizeof(addenum_1)); +} - return 0; +static int test_sha224_long(void) +{ + unsigned char md[SHA224_DIGEST_LENGTH]; - err: - fprintf(stderr, "Fatal EVP error!\n"); - return 1; + if (!TEST_true(EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk" + "ijkljklm" "klmnlmno" "mnopnopq", 56, md, + NULL, EVP_sha224(), NULL))) + return 0; + return TEST_mem_eq(md, sizeof(md), addenum_2, sizeof(addenum_2)); +} + +static int test_sha224_multi(void) +{ + unsigned char md[SHA224_DIGEST_LENGTH]; + int i, testresult = 0; + EVP_MD_CTX *evp; + static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"; + + evp = EVP_MD_CTX_new(); + if (!TEST_ptr(evp)) + return 0; + if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha224(), NULL))) + goto end; + for (i = 0; i < 1000000; i += 64) + if (!TEST_true(EVP_DigestUpdate(evp, updstr, + (1000000 - i) < 64 ? 1000000 - i : 64))) + goto end; + if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL)) + || !TEST_mem_eq(md, sizeof(md), addenum_3, sizeof(addenum_3))) + goto end; + + testresult = 1; + end: + EVP_MD_CTX_free(evp); + return testresult; +} + +void register_tests(void) +{ + ADD_TEST(test_sha256_short); + ADD_TEST(test_sha256_long); + ADD_TEST(test_sha256_multi); + ADD_TEST(test_sha224_short); + ADD_TEST(test_sha224_long); + ADD_TEST(test_sha224_multi); } diff --git a/test/sha512t.c b/test/sha512t.c index 18cdf39736..91efa328d6 100644 --- a/test/sha512t.c +++ b/test/sha512t.c @@ -1,5 +1,5 @@ /* - * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2004-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the OpenSSL license (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -7,13 +7,11 @@ * https://www.openssl.org/source/license.html */ -#include -#include -#include - #include #include -#include + +#include "test_main.h" +#include "testutil.h" static const unsigned char app_c1[SHA512_DIGEST_LENGTH] = { 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, @@ -75,125 +73,118 @@ static const unsigned char app_d3[SHA384_DIGEST_LENGTH] = { 0xae, 0x97, 0xdd, 0xd8, 0x7f, 0x3d, 0x89, 0x85 }; -int main(int argc, char **argv) +static int test_sha512_short(void) { unsigned char md[SHA512_DIGEST_LENGTH]; - int i; - EVP_MD_CTX *evp; - fprintf(stdout, "Testing SHA-512 "); - - if (!EVP_Digest("abc", 3, md, NULL, EVP_sha512(), NULL)) - goto err; - if (memcmp(md, app_c1, sizeof(app_c1))) { - fflush(stdout); - fprintf(stderr, "\nTEST 1 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); - - if (!EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk" - "efghijkl" "fghijklm" "ghijklmn" "hijklmno" - "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs" - "mnopqrst" "nopqrstu", 112, md, NULL, EVP_sha512(), NULL)) - goto err; - if (memcmp(md, app_c2, sizeof(app_c2))) { - fflush(stdout); - fprintf(stderr, "\nTEST 2 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); + if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha512(), NULL))) + return 0; + return TEST_mem_eq(md, sizeof(md), app_c1, sizeof(app_c1)); +} + +static int test_sha512_long(void) +{ + unsigned char md[SHA512_DIGEST_LENGTH]; + + if (!TEST_true(EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk" + "efghijkl" "fghijklm" "ghijklmn" "hijklmno" + "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs" + "mnopqrst" "nopqrstu", 112, md, NULL, + EVP_sha512(), NULL))) + return 0; + return TEST_mem_eq(md, sizeof(md), app_c2, sizeof(app_c2)); +} + +static int test_sha512_multi(void) +{ + unsigned char md[SHA512_DIGEST_LENGTH]; + int i, testresult = 0; + EVP_MD_CTX *evp; + static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"; evp = EVP_MD_CTX_new(); - if (evp == NULL) { - fflush(stdout); - fprintf(stderr, "\nTEST 3 of 3 failed. (malloc failure)\n"); - return 1; - } - if (!EVP_DigestInit_ex(evp, EVP_sha512(), NULL)) - goto err; - for (i = 0; i < 1000000; i += 288) { - if (!EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa", - (1000000 - i) < 288 ? 1000000 - i : 288)) - goto err; - } - if (!EVP_DigestFinal_ex(evp, md, NULL)) - goto err; - EVP_MD_CTX_reset(evp); - - if (memcmp(md, app_c3, sizeof(app_c3))) { - fflush(stdout); - fprintf(stderr, "\nTEST 3 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); - - fprintf(stdout, " passed.\n"); - fflush(stdout); - - fprintf(stdout, "Testing SHA-384 "); - - if (!EVP_Digest("abc", 3, md, NULL, EVP_sha384(), NULL)) - goto err; - if (memcmp(md, app_d1, sizeof(app_d1))) { - fflush(stdout); - fprintf(stderr, "\nTEST 1 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); - - if (!EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk" - "efghijkl" "fghijklm" "ghijklmn" "hijklmno" - "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs" - "mnopqrst" "nopqrstu", 112, md, NULL, EVP_sha384(), NULL)) - goto err; - if (memcmp(md, app_d2, sizeof(app_d2))) { - fflush(stdout); - fprintf(stderr, "\nTEST 2 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); - - if (!EVP_DigestInit_ex(evp, EVP_sha384(), NULL)) - goto err; - for (i = 0; i < 1000000; i += 64) { - if (!EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" - "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa", - (1000000 - i) < 64 ? 1000000 - i : 64)) - goto err; - } - if (!EVP_DigestFinal_ex(evp, md, NULL)) - goto err; + if (!TEST_ptr(evp)) + return 0; + if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha512(), NULL))) + goto end; + for (i = 0; i < 1000000; i += 288) + if (!TEST_true(EVP_DigestUpdate(evp, updstr, + (1000000 - i) < 288 ? 1000000 - i + : 288))) + goto end; + if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL)) + || !TEST_mem_eq(md, sizeof(md), app_c3, sizeof(app_c3))) + goto end; + + testresult = 1; + end: EVP_MD_CTX_free(evp); + return testresult; +} - if (memcmp(md, app_d3, sizeof(app_d3))) { - fflush(stdout); - fprintf(stderr, "\nTEST 3 of 3 failed.\n"); - return 1; - } else - fprintf(stdout, "."); - fflush(stdout); +static int test_sha384_short(void) +{ + unsigned char md[SHA384_DIGEST_LENGTH]; - fprintf(stdout, " passed.\n"); - fflush(stdout); + if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha384(), NULL))) + return 0; + return TEST_mem_eq(md, sizeof(md), app_d1, sizeof(app_d1)); +} - return 0; +static int test_sha384_long(void) +{ + unsigned char md[SHA384_DIGEST_LENGTH]; + + if (!TEST_true(EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk" + "efghijkl" "fghijklm" "ghijklmn" "hijklmno" + "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs" + "mnopqrst" "nopqrstu", 112, md, NULL, + EVP_sha384(), NULL))) + return 0; + return TEST_mem_eq(md, sizeof(md), app_d2, sizeof(app_d2)); +} - err: - fflush(stdout); - fprintf(stderr, "\nFatal EVP error!\n"); - return 1; +static int test_sha384_multi(void) +{ + unsigned char md[SHA384_DIGEST_LENGTH]; + int i, testresult = 0; + EVP_MD_CTX *evp; + static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" + "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"; + + evp = EVP_MD_CTX_new(); + if (!TEST_ptr(evp)) + return 0; + if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha384(), NULL))) + goto end; + for (i = 0; i < 1000000; i += 64) + if (!TEST_true(EVP_DigestUpdate(evp, updstr, + (1000000 - i) < 64 ? 1000000 - i : 64))) + goto end; + if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL)) + || !TEST_mem_eq(md, sizeof(md), app_d3, sizeof(app_d3))) + goto end; + + testresult = 1; + end: + EVP_MD_CTX_free(evp); + return testresult; +} + +void register_tests(void) +{ + ADD_TEST(test_sha512_short); + ADD_TEST(test_sha512_long); + ADD_TEST(test_sha512_multi); + ADD_TEST(test_sha384_short); + ADD_TEST(test_sha384_long); + ADD_TEST(test_sha384_multi); } -- 2.25.1