mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-24 18:25:10 +00:00
And add a vb2_digest_buffer() call which produces the hash of a buffer all in a single function call. That function actually already existed, but was in a unit test file rather than in the library itself. It's a small function, so adding it won't increase the size of the library significantly - or at all, on platforms which compile with -ffunction-sections. This allows coreboot to reuse this SHA library for hashing CBFS entries and file data. All it has to do is #define NEED_VB2_SHA_LIBRARY and then #include "vb2_api.h". BUG=chromium:482652 BRANCH=none TEST=make -j runtests Change-Id: Ice2d0929324b58b2665f3989b5b887225f6ef61e Signed-off-by: Randall Spangler <rspangler@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/269523 Reviewed-by: Julius Werner <jwerner@chromium.org>
149 lines
4.1 KiB
C
149 lines
4.1 KiB
C
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
/* FIPS 180-2 Tests for message digest functions. */
|
|
|
|
#include "2sysincludes.h"
|
|
#include "2rsa.h"
|
|
#include "2sha.h"
|
|
#include "2return_codes.h"
|
|
|
|
#include "sha_test_vectors.h"
|
|
#include "test_common.h"
|
|
|
|
void sha1_tests(void)
|
|
{
|
|
uint8_t digest[VB2_SHA1_DIGEST_SIZE];
|
|
uint8_t *test_inputs[3];
|
|
int i;
|
|
|
|
test_inputs[0] = (uint8_t *) oneblock_msg;
|
|
test_inputs[1] = (uint8_t *) multiblock_msg1;
|
|
test_inputs[2] = (uint8_t *) long_msg;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
TEST_SUCC(vb2_digest_buffer(test_inputs[i],
|
|
strlen((char *)test_inputs[i]),
|
|
VB2_HASH_SHA1,
|
|
digest, sizeof(digest)),
|
|
"vb2_digest_buffer() SHA1");
|
|
TEST_EQ(memcmp(digest, sha1_results[i], sizeof(digest)),
|
|
0, "SHA1 digest");
|
|
}
|
|
|
|
TEST_EQ(vb2_digest_buffer(test_inputs[0],
|
|
strlen((char *)test_inputs[0]),
|
|
VB2_HASH_SHA1, digest, sizeof(digest) - 1),
|
|
VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE,
|
|
"vb2_digest_buffer() too small");
|
|
}
|
|
|
|
void sha256_tests(void)
|
|
{
|
|
uint8_t digest[VB2_SHA256_DIGEST_SIZE];
|
|
uint8_t *test_inputs[3];
|
|
int i;
|
|
|
|
test_inputs[0] = (uint8_t *) oneblock_msg;
|
|
test_inputs[1] = (uint8_t *) multiblock_msg1;
|
|
test_inputs[2] = (uint8_t *) long_msg;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
TEST_SUCC(vb2_digest_buffer(test_inputs[i],
|
|
strlen((char *)test_inputs[i]),
|
|
VB2_HASH_SHA256,
|
|
digest, sizeof(digest)),
|
|
"vb2_digest_buffer() SHA256");
|
|
TEST_EQ(memcmp(digest, sha256_results[i], sizeof(digest)),
|
|
0, "SHA-256 digest");
|
|
}
|
|
|
|
TEST_EQ(vb2_digest_buffer(test_inputs[0],
|
|
strlen((char *)test_inputs[0]),
|
|
VB2_HASH_SHA256, digest, sizeof(digest) - 1),
|
|
VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE,
|
|
"vb2_digest_buffer() too small");
|
|
}
|
|
|
|
void sha512_tests(void)
|
|
{
|
|
uint8_t digest[VB2_SHA512_DIGEST_SIZE];
|
|
uint8_t *test_inputs[3];
|
|
int i;
|
|
|
|
test_inputs[0] = (uint8_t *) oneblock_msg;
|
|
test_inputs[1] = (uint8_t *) multiblock_msg2;
|
|
test_inputs[2] = (uint8_t *) long_msg;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
TEST_SUCC(vb2_digest_buffer(test_inputs[i],
|
|
strlen((char *)test_inputs[i]),
|
|
VB2_HASH_SHA512,
|
|
digest, sizeof(digest)),
|
|
"vb2_digest_buffer() SHA512");
|
|
TEST_EQ(memcmp(digest, sha512_results[i], sizeof(digest)),
|
|
0, "SHA-512 digest");
|
|
}
|
|
|
|
TEST_EQ(vb2_digest_buffer(test_inputs[0],
|
|
strlen((char *)test_inputs[0]),
|
|
VB2_HASH_SHA512, digest, sizeof(digest) - 1),
|
|
VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE,
|
|
"vb2_digest_buffer() too small");
|
|
}
|
|
|
|
void misc_tests(void)
|
|
{
|
|
uint8_t digest[VB2_SHA512_DIGEST_SIZE];
|
|
struct vb2_digest_context dc;
|
|
|
|
/* Crypto algorithm to hash algorithm mapping */
|
|
TEST_EQ(vb2_crypto_to_hash(VB2_ALG_RSA1024_SHA1), VB2_HASH_SHA1,
|
|
"Crypto map to SHA1");
|
|
TEST_EQ(vb2_crypto_to_hash(VB2_ALG_RSA2048_SHA256), VB2_HASH_SHA256,
|
|
"Crypto map to SHA256");
|
|
TEST_EQ(vb2_crypto_to_hash(VB2_ALG_RSA4096_SHA256), VB2_HASH_SHA256,
|
|
"Crypto map to SHA256 2");
|
|
TEST_EQ(vb2_crypto_to_hash(VB2_ALG_RSA8192_SHA512), VB2_HASH_SHA512,
|
|
"Crypto map to SHA512");
|
|
TEST_EQ(vb2_crypto_to_hash(VB2_ALG_COUNT), VB2_HASH_INVALID,
|
|
"Crypto map to invalid");
|
|
|
|
TEST_EQ(vb2_digest_size(VB2_HASH_INVALID), 0,
|
|
"digest size invalid alg");
|
|
|
|
TEST_EQ(vb2_digest_buffer((uint8_t *)oneblock_msg, strlen(oneblock_msg),
|
|
VB2_HASH_INVALID, digest, sizeof(digest)),
|
|
VB2_ERROR_SHA_INIT_ALGORITHM,
|
|
"vb2_digest_buffer() invalid alg");
|
|
|
|
/* Test bad algorithm inside extend and finalize */
|
|
vb2_digest_init(&dc, VB2_HASH_SHA256);
|
|
dc.hash_alg = VB2_HASH_INVALID;
|
|
TEST_EQ(vb2_digest_extend(&dc, digest, sizeof(digest)),
|
|
VB2_ERROR_SHA_EXTEND_ALGORITHM,
|
|
"vb2_digest_extend() invalid alg");
|
|
TEST_EQ(vb2_digest_finalize(&dc, digest, sizeof(digest)),
|
|
VB2_ERROR_SHA_FINALIZE_ALGORITHM,
|
|
"vb2_digest_finalize() invalid alg");
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
/* Initialize long_msg with 'a' x 1,000,000 */
|
|
long_msg = (char *) malloc(1000001);
|
|
memset(long_msg, 'a', 1000000);
|
|
long_msg[1000000]=0;
|
|
|
|
sha1_tests();
|
|
sha256_tests();
|
|
sha512_tests();
|
|
misc_tests();
|
|
|
|
free(long_msg);
|
|
|
|
return gTestSuccess ? 0 : 255;
|
|
}
|