Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf util: add a basic SHA-1 implementation

SHA-1 can be written in fewer than 100 lines of code. Just add a basic
SHA-1 implementation so that there's no need to use an external library
or try to pull in the kernel's SHA-1 implementation. The kernel's SHA-1
implementation is not really intended to be pulled into userspace
programs in the way that it was proposed to do so for perf
(https://lore.kernel.org/r/20250521225307.743726-3-yuzhuo@google.com/),
and it's also likely to undergo some refactoring in the future. There's
no need to tie userspace tools to it.

Include a test for sha1() in the util test suite.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Reviewed-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250625202311.23244-3-ebiggers@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Eric Biggers and committed by
Namhyung Kim
43830468 55a18d2f

+148 -1
+44 -1
tools/perf/tests/util.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include "tests.h" 3 3 #include "util/debug.h" 4 + #include "util/sha1.h" 4 5 5 6 #include <linux/compiler.h> 6 7 #include <stdlib.h> ··· 17 16 return ret == 0; 18 17 } 19 18 19 + #define MAX_LEN 512 20 + 21 + /* Test sha1() for all lengths from 0 to MAX_LEN inclusively. */ 22 + static int test_sha1(void) 23 + { 24 + u8 data[MAX_LEN]; 25 + size_t digests_size = (MAX_LEN + 1) * SHA1_DIGEST_SIZE; 26 + u8 *digests; 27 + u8 digest_of_digests[SHA1_DIGEST_SIZE]; 28 + /* 29 + * The correctness of this value was verified by running this test with 30 + * sha1() replaced by OpenSSL's SHA1(). 31 + */ 32 + static const u8 expected_digest_of_digests[SHA1_DIGEST_SIZE] = { 33 + 0x74, 0xcd, 0x4c, 0xb9, 0xd8, 0xa6, 0xd5, 0x95, 0x22, 0x8b, 34 + 0x7e, 0xd6, 0x8b, 0x7e, 0x46, 0x95, 0x31, 0x9b, 0xa2, 0x43, 35 + }; 36 + size_t i; 37 + 38 + digests = malloc(digests_size); 39 + TEST_ASSERT_VAL("failed to allocate digests", digests != NULL); 40 + 41 + /* Generate MAX_LEN bytes of data. */ 42 + for (i = 0; i < MAX_LEN; i++) 43 + data[i] = i; 44 + 45 + /* Calculate a SHA-1 for each length 0 through MAX_LEN inclusively. */ 46 + for (i = 0; i <= MAX_LEN; i++) 47 + sha1(data, i, &digests[i * SHA1_DIGEST_SIZE]); 48 + 49 + /* Calculate digest of all digests calculated above. */ 50 + sha1(digests, digests_size, digest_of_digests); 51 + 52 + free(digests); 53 + 54 + /* Check for the expected result. */ 55 + TEST_ASSERT_VAL("wrong output from sha1()", 56 + memcmp(digest_of_digests, expected_digest_of_digests, 57 + SHA1_DIGEST_SIZE) == 0); 58 + return 0; 59 + } 60 + 20 61 static int test__util(struct test_suite *t __maybe_unused, int subtest __maybe_unused) 21 62 { 22 63 TEST_ASSERT_VAL("empty string", test_strreplace(' ', "", "123", "")); ··· 68 25 TEST_ASSERT_VAL("replace long", test_strreplace('a', "abcabc", "longlong", 69 26 "longlongbclonglongbc")); 70 27 71 - return 0; 28 + return test_sha1(); 72 29 } 73 30 74 31 DEFINE_SUITE("util", util);
+1
tools/perf/util/Build
··· 41 41 perf-util-y += libstring.o 42 42 perf-util-y += bitmap.o 43 43 perf-util-y += hweight.o 44 + perf-util-y += sha1.o 44 45 perf-util-y += smt.o 45 46 perf-util-y += strbuf.o 46 47 perf-util-y += string.o
+97
tools/perf/util/sha1.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * SHA-1 message digest algorithm 4 + * 5 + * Copyright 2025 Google LLC 6 + */ 7 + #include <linux/bitops.h> 8 + #include <linux/kernel.h> 9 + #include <linux/unaligned.h> 10 + #include <string.h> 11 + 12 + #include "sha1.h" 13 + 14 + #define SHA1_BLOCK_SIZE 64 15 + 16 + static const u32 sha1_K[4] = { 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 }; 17 + 18 + #define SHA1_ROUND(i, a, b, c, d, e) \ 19 + do { \ 20 + if ((i) >= 16) \ 21 + w[i] = rol32(w[(i) - 16] ^ w[(i) - 14] ^ w[(i) - 8] ^ \ 22 + w[(i) - 3], \ 23 + 1); \ 24 + e += w[i] + rol32(a, 5) + sha1_K[(i) / 20]; \ 25 + if ((i) < 20) \ 26 + e += (b & (c ^ d)) ^ d; \ 27 + else if ((i) < 40 || (i) >= 60) \ 28 + e += b ^ c ^ d; \ 29 + else \ 30 + e += (c & d) ^ (b & (c ^ d)); \ 31 + b = rol32(b, 30); \ 32 + /* The new (a, b, c, d, e) is the old (e, a, b, c, d). */ \ 33 + } while (0) 34 + 35 + #define SHA1_5ROUNDS(i) \ 36 + do { \ 37 + SHA1_ROUND((i) + 0, a, b, c, d, e); \ 38 + SHA1_ROUND((i) + 1, e, a, b, c, d); \ 39 + SHA1_ROUND((i) + 2, d, e, a, b, c); \ 40 + SHA1_ROUND((i) + 3, c, d, e, a, b); \ 41 + SHA1_ROUND((i) + 4, b, c, d, e, a); \ 42 + } while (0) 43 + 44 + #define SHA1_20ROUNDS(i) \ 45 + do { \ 46 + SHA1_5ROUNDS((i) + 0); \ 47 + SHA1_5ROUNDS((i) + 5); \ 48 + SHA1_5ROUNDS((i) + 10); \ 49 + SHA1_5ROUNDS((i) + 15); \ 50 + } while (0) 51 + 52 + static void sha1_blocks(u32 h[5], const u8 *data, size_t nblocks) 53 + { 54 + while (nblocks--) { 55 + u32 a = h[0]; 56 + u32 b = h[1]; 57 + u32 c = h[2]; 58 + u32 d = h[3]; 59 + u32 e = h[4]; 60 + u32 w[80]; 61 + 62 + for (int i = 0; i < 16; i++) 63 + w[i] = get_unaligned_be32(&data[i * 4]); 64 + SHA1_20ROUNDS(0); 65 + SHA1_20ROUNDS(20); 66 + SHA1_20ROUNDS(40); 67 + SHA1_20ROUNDS(60); 68 + 69 + h[0] += a; 70 + h[1] += b; 71 + h[2] += c; 72 + h[3] += d; 73 + h[4] += e; 74 + data += SHA1_BLOCK_SIZE; 75 + } 76 + } 77 + 78 + /* Calculate the SHA-1 message digest of the given data. */ 79 + void sha1(const void *data, size_t len, u8 out[SHA1_DIGEST_SIZE]) 80 + { 81 + u32 h[5] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 82 + 0xC3D2E1F0 }; 83 + u8 final_data[2 * SHA1_BLOCK_SIZE] = { 0 }; 84 + size_t final_len = len % SHA1_BLOCK_SIZE; 85 + 86 + sha1_blocks(h, data, len / SHA1_BLOCK_SIZE); 87 + 88 + memcpy(final_data, data + len - final_len, final_len); 89 + final_data[final_len] = 0x80; 90 + final_len = round_up(final_len + 9, SHA1_BLOCK_SIZE); 91 + put_unaligned_be64((u64)len * 8, &final_data[final_len - 8]); 92 + 93 + sha1_blocks(h, final_data, final_len / SHA1_BLOCK_SIZE); 94 + 95 + for (int i = 0; i < 5; i++) 96 + put_unaligned_be32(h[i], &out[i * 4]); 97 + }
+6
tools/perf/util/sha1.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + #include <linux/types.h> 3 + 4 + #define SHA1_DIGEST_SIZE 20 5 + 6 + void sha1(const void *data, size_t len, u8 out[SHA1_DIGEST_SIZE]);