summaryrefslogtreecommitdiffstats
path: root/test/sha512t.c
diff options
context:
space:
mode:
authorPauli <paul.dale@oracle.com>2017-04-10 01:14:49 +0200
committerMatt Caswell <matt@openssl.org>2017-04-12 10:38:47 +0200
commitf46074c7b9d03235e2968546c28a6057db9f8725 (patch)
tree5c819111e13d7a1f21f342044f66a329359d48b9 /test/sha512t.c
parentConvert exdata tests to new test framework (diff)
downloadopenssl-f46074c7b9d03235e2968546c28a6057db9f8725.tar.xz
openssl-f46074c7b9d03235e2968546c28a6057db9f8725.zip
Update sha256t and sha512t to use the test infrastructure
Reviewed-by: Rich Salz <rsalz@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/3168)
Diffstat (limited to '')
-rw-r--r--test/sha512t.c225
1 files changed, 108 insertions, 117 deletions
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 <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
#include <openssl/sha.h>
#include <openssl/evp.h>
-#include <openssl/crypto.h>
+
+#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);
}