Convert digest_test to C++.
Change-Id: Ibc8c3d8740cbd03dfb0d1ff9834fed66601dd479 Reviewed-on: https://boringssl-review.googlesource.com/4123 Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
parent
c2b45a164a
commit
94e597a51c
@ -12,7 +12,7 @@ add_library(
|
|||||||
add_executable(
|
add_executable(
|
||||||
digest_test
|
digest_test
|
||||||
|
|
||||||
digest_test.c
|
digest_test.cc
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(digest_test crypto)
|
target_link_libraries(digest_test crypto)
|
||||||
|
@ -23,24 +23,26 @@
|
|||||||
#include <openssl/md5.h>
|
#include <openssl/md5.h>
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
|
#include "../test/scoped_types.h"
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
/* md_func is the digest to test. */
|
struct TestVector {
|
||||||
|
// md_func is the digest to test.
|
||||||
const EVP_MD *(*md_func)(void);
|
const EVP_MD *(*md_func)(void);
|
||||||
/* one_shot_func is the convenience one-shot version of the
|
// one_shot_func is the convenience one-shot version of the
|
||||||
* digest. */
|
// digest.
|
||||||
uint8_t *(*one_shot_func)(const uint8_t *, size_t, uint8_t *);
|
uint8_t *(*one_shot_func)(const uint8_t *, size_t, uint8_t *);
|
||||||
/* input is a NUL-terminated string to hash. */
|
// input is a NUL-terminated string to hash.
|
||||||
const char *input;
|
const char *input;
|
||||||
/* repeat is the number of times to repeat input. */
|
// repeat is the number of times to repeat input.
|
||||||
size_t repeat;
|
size_t repeat;
|
||||||
/* expected_hex is the expected digest in hexadecimal. */
|
// expected_hex is the expected digest in hexadecimal.
|
||||||
const char *expected_hex;
|
const char *expected_hex;
|
||||||
} TEST_VECTOR;
|
};
|
||||||
|
|
||||||
static const TEST_VECTOR kTestVectors[] = {
|
static const TestVector kTestVectors[] = {
|
||||||
/* MD4 tests, from RFC 1320. (crypto/md4 does not provide a
|
// MD4 tests, from RFC 1320. (crypto/md4 does not provide a
|
||||||
* one-shot MD4 function.) */
|
// one-shot MD4 function.)
|
||||||
{ &EVP_md4, NULL, "", 1, "31d6cfe0d16ae931b73c59d7e0c089c0" },
|
{ &EVP_md4, NULL, "", 1, "31d6cfe0d16ae931b73c59d7e0c089c0" },
|
||||||
{ &EVP_md4, NULL, "a", 1, "bde52cb31de33e46245e05fbdbd6fb24" },
|
{ &EVP_md4, NULL, "a", 1, "bde52cb31de33e46245e05fbdbd6fb24" },
|
||||||
{ &EVP_md4, NULL, "abc", 1, "a448017aaf21d8525fc10ae87aa6729d" },
|
{ &EVP_md4, NULL, "abc", 1, "a448017aaf21d8525fc10ae87aa6729d" },
|
||||||
@ -53,7 +55,7 @@ static const TEST_VECTOR kTestVectors[] = {
|
|||||||
"043f8582f241db351ce627e153e7f0e4" },
|
"043f8582f241db351ce627e153e7f0e4" },
|
||||||
{ &EVP_md4, NULL, "1234567890", 8, "e33b4ddc9c38f2199c3e7b164fcc0536" },
|
{ &EVP_md4, NULL, "1234567890", 8, "e33b4ddc9c38f2199c3e7b164fcc0536" },
|
||||||
|
|
||||||
/* MD5 tests, from RFC 1321. */
|
// MD5 tests, from RFC 1321.
|
||||||
{ &EVP_md5, &MD5, "", 1, "d41d8cd98f00b204e9800998ecf8427e" },
|
{ &EVP_md5, &MD5, "", 1, "d41d8cd98f00b204e9800998ecf8427e" },
|
||||||
{ &EVP_md5, &MD5, "a", 1, "0cc175b9c0f1b6a831c399e269772661" },
|
{ &EVP_md5, &MD5, "a", 1, "0cc175b9c0f1b6a831c399e269772661" },
|
||||||
{ &EVP_md5, &MD5, "abc", 1, "900150983cd24fb0d6963f7d28e17f72" },
|
{ &EVP_md5, &MD5, "abc", 1, "900150983cd24fb0d6963f7d28e17f72" },
|
||||||
@ -65,7 +67,7 @@ static const TEST_VECTOR kTestVectors[] = {
|
|||||||
"d174ab98d277d9f5a5611c2c9f419d9f" },
|
"d174ab98d277d9f5a5611c2c9f419d9f" },
|
||||||
{ &EVP_md5, &MD5, "1234567890", 8, "57edf4a22be3c955ac49da2e2107b67a" },
|
{ &EVP_md5, &MD5, "1234567890", 8, "57edf4a22be3c955ac49da2e2107b67a" },
|
||||||
|
|
||||||
/* SHA-1 tests, from RFC 3174. */
|
// SHA-1 tests, from RFC 3174.
|
||||||
{ &EVP_sha1, &SHA1, "abc", 1, "a9993e364706816aba3e25717850c26c9cd0d89d" },
|
{ &EVP_sha1, &SHA1, "abc", 1, "a9993e364706816aba3e25717850c26c9cd0d89d" },
|
||||||
{ &EVP_sha1, &SHA1,
|
{ &EVP_sha1, &SHA1,
|
||||||
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
|
||||||
@ -76,7 +78,7 @@ static const TEST_VECTOR kTestVectors[] = {
|
|||||||
"0123456701234567012345670123456701234567012345670123456701234567", 10,
|
"0123456701234567012345670123456701234567012345670123456701234567", 10,
|
||||||
"dea356a2cddd90c7a7ecedc5ebb563934f460452" },
|
"dea356a2cddd90c7a7ecedc5ebb563934f460452" },
|
||||||
|
|
||||||
/* SHA-224 tests, from RFC 3874. */
|
// SHA-224 tests, from RFC 3874.
|
||||||
{ &EVP_sha224, &SHA224, "abc", 1,
|
{ &EVP_sha224, &SHA224, "abc", 1,
|
||||||
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" },
|
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" },
|
||||||
{ &EVP_sha224, &SHA224,
|
{ &EVP_sha224, &SHA224,
|
||||||
@ -86,14 +88,14 @@ static const TEST_VECTOR kTestVectors[] = {
|
|||||||
"a", 1000000,
|
"a", 1000000,
|
||||||
"20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67" },
|
"20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67" },
|
||||||
|
|
||||||
/* SHA-256 tests, from NIST. */
|
// SHA-256 tests, from NIST.
|
||||||
{ &EVP_sha256, &SHA256, "abc", 1,
|
{ &EVP_sha256, &SHA256, "abc", 1,
|
||||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
|
||||||
{ &EVP_sha256, &SHA256,
|
{ &EVP_sha256, &SHA256,
|
||||||
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
|
||||||
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" },
|
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" },
|
||||||
|
|
||||||
/* SHA-384 tests, from NIST. */
|
// SHA-384 tests, from NIST.
|
||||||
{ &EVP_sha384, &SHA384, "abc", 1,
|
{ &EVP_sha384, &SHA384, "abc", 1,
|
||||||
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"
|
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"
|
||||||
"8086072ba1e7cc2358baeca134c825a7" },
|
"8086072ba1e7cc2358baeca134c825a7" },
|
||||||
@ -103,7 +105,7 @@ static const TEST_VECTOR kTestVectors[] = {
|
|||||||
"09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"
|
"09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"
|
||||||
"fcc7c71a557e2db966c3e9fa91746039" },
|
"fcc7c71a557e2db966c3e9fa91746039" },
|
||||||
|
|
||||||
/* SHA-512 tests, from NIST. */
|
// SHA-512 tests, from NIST.
|
||||||
{ &EVP_sha512, &SHA512, "abc", 1,
|
{ &EVP_sha512, &SHA512, "abc", 1,
|
||||||
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
|
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
|
||||||
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" },
|
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" },
|
||||||
@ -113,12 +115,12 @@ static const TEST_VECTOR kTestVectors[] = {
|
|||||||
"8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
|
"8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
|
||||||
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" },
|
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" },
|
||||||
|
|
||||||
/* MD5-SHA1 tests. */
|
// MD5-SHA1 tests.
|
||||||
{ &EVP_md5_sha1, NULL, "abc", 1,
|
{ &EVP_md5_sha1, NULL, "abc", 1,
|
||||||
"900150983cd24fb0d6963f7d28e17f72a9993e364706816aba3e25717850c26c9cd0d89d" },
|
"900150983cd24fb0d6963f7d28e17f72a9993e364706816aba3e25717850c26c9cd0d89d" },
|
||||||
};
|
};
|
||||||
|
|
||||||
static int compare_digest(const TEST_VECTOR *test,
|
static bool CompareDigest(const TestVector *test,
|
||||||
const uint8_t *digest,
|
const uint8_t *digest,
|
||||||
size_t digest_len) {
|
size_t digest_len) {
|
||||||
static const char kHexTable[] = "0123456789abcdef";
|
static const char kHexTable[] = "0123456789abcdef";
|
||||||
@ -135,105 +137,94 @@ static int compare_digest(const TEST_VECTOR *test,
|
|||||||
fprintf(stderr, "%s(\"%s\" * %d) = %s; want %s\n",
|
fprintf(stderr, "%s(\"%s\" * %d) = %s; want %s\n",
|
||||||
EVP_MD_name(test->md_func()), test->input, (int)test->repeat,
|
EVP_MD_name(test->md_func()), test->input, (int)test->repeat,
|
||||||
digest_hex, test->expected_hex);
|
digest_hex, test->expected_hex);
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int test_digest(const TEST_VECTOR *test) {
|
static int TestDigest(const TestVector *test) {
|
||||||
int ret = 0;
|
ScopedEVP_MD_CTX ctx;
|
||||||
EVP_MD_CTX ctx;
|
|
||||||
size_t i;
|
|
||||||
uint8_t digest[EVP_MAX_MD_SIZE];
|
|
||||||
unsigned digest_len;
|
|
||||||
|
|
||||||
EVP_MD_CTX_init(&ctx);
|
// Test the input provided.
|
||||||
|
if (!EVP_DigestInit_ex(ctx.get(), test->md_func(), NULL)) {
|
||||||
/* Test the input provided. */
|
|
||||||
if (!EVP_DigestInit_ex(&ctx, test->md_func(), NULL)) {
|
|
||||||
fprintf(stderr, "EVP_DigestInit_ex failed\n");
|
fprintf(stderr, "EVP_DigestInit_ex failed\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
for (i = 0; i < test->repeat; i++) {
|
for (size_t i = 0; i < test->repeat; i++) {
|
||||||
if (!EVP_DigestUpdate(&ctx, test->input, strlen(test->input))) {
|
if (!EVP_DigestUpdate(ctx.get(), test->input, strlen(test->input))) {
|
||||||
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!EVP_DigestFinal_ex(&ctx, digest, &digest_len)) {
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
||||||
|
unsigned digest_len;
|
||||||
|
if (!EVP_DigestFinal_ex(ctx.get(), digest, &digest_len)) {
|
||||||
fprintf(stderr, "EVP_DigestFinal_ex failed\n");
|
fprintf(stderr, "EVP_DigestFinal_ex failed\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
if (!compare_digest(test, digest, digest_len)) {
|
if (!CompareDigest(test, digest, digest_len)) {
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Test the input one character at a time. */
|
// Test the input one character at a time.
|
||||||
if (!EVP_DigestInit_ex(&ctx, test->md_func(), NULL)) {
|
if (!EVP_DigestInit_ex(ctx.get(), test->md_func(), NULL)) {
|
||||||
fprintf(stderr, "EVP_DigestInit_ex failed\n");
|
fprintf(stderr, "EVP_DigestInit_ex failed\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
if (!EVP_DigestUpdate(&ctx, NULL, 0)) {
|
if (!EVP_DigestUpdate(ctx.get(), NULL, 0)) {
|
||||||
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
for (i = 0; i < test->repeat; i++) {
|
for (size_t i = 0; i < test->repeat; i++) {
|
||||||
const char *p;
|
for (const char *p = test->input; *p; p++) {
|
||||||
for (p = test->input; *p; p++) {
|
if (!EVP_DigestUpdate(ctx.get(), p, 1)) {
|
||||||
if (!EVP_DigestUpdate(&ctx, p, 1)) {
|
|
||||||
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
fprintf(stderr, "EVP_DigestUpdate failed\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!EVP_DigestFinal_ex(&ctx, digest, &digest_len)) {
|
if (!EVP_DigestFinal_ex(ctx.get(), digest, &digest_len)) {
|
||||||
fprintf(stderr, "EVP_DigestFinal_ex failed\n");
|
fprintf(stderr, "EVP_DigestFinal_ex failed\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
if (digest_len != EVP_MD_size(test->md_func())) {
|
if (digest_len != EVP_MD_size(test->md_func())) {
|
||||||
fprintf(stderr, "EVP_MD_size output incorrect\n");
|
fprintf(stderr, "EVP_MD_size output incorrect\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
if (!compare_digest(test, digest, digest_len)) {
|
if (!CompareDigest(test, digest, digest_len)) {
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Test the one-shot function. */
|
// Test the one-shot function.
|
||||||
if (test->one_shot_func && test->repeat == 1) {
|
if (test->one_shot_func && test->repeat == 1) {
|
||||||
uint8_t *out = test->one_shot_func((const uint8_t *)test->input,
|
uint8_t *out = test->one_shot_func((const uint8_t *)test->input,
|
||||||
strlen(test->input), digest);
|
strlen(test->input), digest);
|
||||||
if (out != digest) {
|
if (out != digest) {
|
||||||
fprintf(stderr, "one_shot_func gave incorrect return\n");
|
fprintf(stderr, "one_shot_func gave incorrect return\n");
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
if (!compare_digest(test, digest, EVP_MD_size(test->md_func()))) {
|
if (!CompareDigest(test, digest, EVP_MD_size(test->md_func()))) {
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Test the deprecated static buffer variant, until it's removed. */
|
// Test the deprecated static buffer variant, until it's removed.
|
||||||
out = test->one_shot_func((const uint8_t *)test->input, strlen(test->input),
|
out = test->one_shot_func((const uint8_t *)test->input, strlen(test->input),
|
||||||
NULL);
|
NULL);
|
||||||
if (!compare_digest(test, out, EVP_MD_size(test->md_func()))) {
|
if (!CompareDigest(test, out, EVP_MD_size(test->md_func()))) {
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 1;
|
return true;
|
||||||
|
|
||||||
done:
|
|
||||||
EVP_MD_CTX_cleanup(&ctx);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
size_t i;
|
|
||||||
|
|
||||||
CRYPTO_library_init();
|
CRYPTO_library_init();
|
||||||
ERR_load_crypto_strings();
|
ERR_load_crypto_strings();
|
||||||
|
|
||||||
for (i = 0; i < sizeof(kTestVectors) / sizeof(kTestVectors[0]); i++) {
|
for (size_t i = 0; i < sizeof(kTestVectors) / sizeof(kTestVectors[0]); i++) {
|
||||||
if (!test_digest(&kTestVectors[i])) {
|
if (!TestDigest(&kTestVectors[i])) {
|
||||||
fprintf(stderr, "Test %d failed\n", (int)i);
|
fprintf(stderr, "Test %d failed\n", (int)i);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user