diff options
Diffstat (limited to 'test/sha256t.c')
-rw-r--r-- | test/sha256t.c | 217 |
1 files changed, 104 insertions, 113 deletions
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 <stdio.h> -#include <string.h> -#include <stdlib.h> - #include <openssl/sha.h> #include <openssl/evp.h> +#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); } |