mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
synced 2026-04-17 22:23:45 -04:00
perf util: Add BLAKE2s support
Add BLAKE2s support to the perf utility library. The code is borrowed from the kernel. This will replace the use of SHA-1 in genelf.c. Signed-off-by: Eric Biggers <ebiggers@kernel.org> Tested-by: Ian Rogers <irogers@google.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Fangrui Song <maskray@sourceware.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@linaro.org> Cc: Jason A. Donenfeld <Jason@zx2c4.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Pablo Galindo <pablogsal@gmail.com> Cc: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
committed by
Arnaldo Carvalho de Melo
parent
b6ee9b6e20
commit
8d3b664949
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include "tests.h"
|
||||
#include "util/blake2s.h"
|
||||
#include "util/debug.h"
|
||||
#include "util/sha1.h"
|
||||
|
||||
@@ -59,8 +60,79 @@ static int test_sha1(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Maximum data length tested by test_blake2s() */
|
||||
#define MAX_DATA_LEN 512
|
||||
|
||||
/*
|
||||
* Hash length tested by test_blake2s(). BLAKE2s supports variable-length
|
||||
* hashes. However, the only user of BLAKE2s in 'perf' uses 20-byte hashes,
|
||||
* matching the length of the ELF build ID field. So that's the length we test.
|
||||
*/
|
||||
#define HASH_LEN 20
|
||||
|
||||
/* Test the implementation of the BLAKE2s hash algorithm. */
|
||||
static int test_blake2s(void)
|
||||
{
|
||||
u8 data[MAX_DATA_LEN];
|
||||
u8 hash[HASH_LEN];
|
||||
u8 hash2[HASH_LEN];
|
||||
struct blake2s_ctx main_ctx;
|
||||
/*
|
||||
* This value was generated by the following Python code:
|
||||
*
|
||||
* import hashlib
|
||||
*
|
||||
* data = bytes(i % 256 for i in range(513))
|
||||
* h = hashlib.blake2s(digest_size=20)
|
||||
* for i in range(513):
|
||||
* h.update(hashlib.blake2s(data=data[:i], digest_size=20).digest())
|
||||
* print(h.hexdigest())
|
||||
*/
|
||||
static const u8 expected_hash_of_hashes[20] = {
|
||||
0xef, 0x9b, 0x13, 0x98, 0x78, 0x8e, 0x74, 0x59, 0x9c, 0xd5,
|
||||
0x0c, 0xf0, 0x33, 0x97, 0x79, 0x3d, 0x3e, 0xd0, 0x95, 0xa6
|
||||
};
|
||||
size_t i;
|
||||
|
||||
/* Generate MAX_DATA_LEN bytes of data. */
|
||||
for (i = 0; i < MAX_DATA_LEN; i++)
|
||||
data[i] = i;
|
||||
|
||||
blake2s_init(&main_ctx, sizeof(hash));
|
||||
for (i = 0; i <= MAX_DATA_LEN; i++) {
|
||||
struct blake2s_ctx ctx;
|
||||
|
||||
/* Compute the BLAKE2s hash of 'i' data bytes. */
|
||||
blake2s_init(&ctx, HASH_LEN);
|
||||
blake2s_update(&ctx, data, i);
|
||||
blake2s_final(&ctx, hash);
|
||||
|
||||
/* Verify that multiple updates produce the same result. */
|
||||
blake2s_init(&ctx, HASH_LEN);
|
||||
blake2s_update(&ctx, data, i / 2);
|
||||
blake2s_update(&ctx, &data[i / 2], i - (i / 2));
|
||||
blake2s_final(&ctx, hash2);
|
||||
TEST_ASSERT_VAL("inconsistent BLAKE2s hashes",
|
||||
memcmp(hash, hash2, HASH_LEN) == 0);
|
||||
|
||||
/*
|
||||
* Pass the hash to another BLAKE2s context, so that we
|
||||
* incrementally compute the hash of all the hashes.
|
||||
*/
|
||||
blake2s_update(&main_ctx, hash, HASH_LEN);
|
||||
}
|
||||
|
||||
/* Verify the hash of all the hashes. */
|
||||
blake2s_final(&main_ctx, hash);
|
||||
TEST_ASSERT_VAL("wrong BLAKE2s hashes",
|
||||
memcmp(hash, expected_hash_of_hashes, HASH_LEN) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test__util(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
|
||||
{
|
||||
int ret;
|
||||
|
||||
TEST_ASSERT_VAL("empty string", test_strreplace(' ', "", "123", ""));
|
||||
TEST_ASSERT_VAL("no match", test_strreplace('5', "123", "4", "123"));
|
||||
TEST_ASSERT_VAL("replace 1", test_strreplace('3', "123", "4", "124"));
|
||||
@@ -68,7 +140,11 @@ static int test__util(struct test_suite *t __maybe_unused, int subtest __maybe_u
|
||||
TEST_ASSERT_VAL("replace long", test_strreplace('a', "abcabc", "longlong",
|
||||
"longlongbclonglongbc"));
|
||||
|
||||
return test_sha1();
|
||||
ret = test_sha1();
|
||||
if (ret != TEST_OK)
|
||||
return ret;
|
||||
|
||||
return test_blake2s();
|
||||
}
|
||||
|
||||
DEFINE_SUITE("util", util);
|
||||
|
||||
Reference in New Issue
Block a user