Skip to content

eBPF-based security platform

chain: hash with sha256_buffer() and drop sha256_calc()

Daniel Borkmann711918a3d2e2
diff --git a/builtin-attest.c b/builtin-attest.c
index efb65607..204e0c9d 100644
--- a/builtin-attest.c
+++ b/builtin-attest.c
@@ -193,7 +193,7 @@ static int cert_digest(const char *path, __u8 out[32])
ret = read_file(path, KEYS_DER_MAX, &der, &len);
if (!ret) {
- ret = len ? sha256_calc(der, len, out) : -ENODATA;
+ ret = len ? sha256_buffer(der, len, out) : -ENODATA;
xfree(der, len + 1);
}
if (ret)
@@ -422,7 +422,7 @@ static int cmd_attest_predict(int argc, const char **argv)
goto out;
}
predict_kv(&po, "anchor", anchor);
- if (sha256_calc(anchor, strlen(anchor), anchor_digest)) {
+ if (sha256_buffer(anchor, strlen(anchor), anchor_digest)) {
pr_err("attest predict: cannot hash the anchor");
goto out;
}
diff --git a/builtin-upatch.c b/builtin-upatch.c
index d9595ca8..8fc9fa61 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2110,8 +2110,8 @@ static int upatch_apply(struct upatch *u, const char *label, bool verify_only,
{
__u8 loader_digest[UPATCH_PAYLOAD_HASH_LEN];
- if (sha256_calc(insns, u->hdr.loader_insns_len,
- loader_digest)) {
+ if (sha256_buffer(insns, u->hdr.loader_insns_len,
+ loader_digest)) {
pr_err("__loader.map: cannot hash the loader");
rep_fail(rep, "signed_loader_load", "unknown", EIO,
"loader digest failed");
diff --git a/testing/fuzz/chain.c b/testing/fuzz/chain.c
index a1046d69..7a37e40b 100644
--- a/testing/fuzz/chain.c
+++ b/testing/fuzz/chain.c
@@ -8,17 +8,11 @@
#include "chain.h"
#include "crypto.h"
-int sha256_calc(const void *data, size_t len,
- __u8 out[CHAIN_DIGEST_LEN])
-{
- return sha256_buffer(data, len, out);
-}
-
int chain_fold(__u8 chain[32], const __u8 event[32])
{
__u8 cat[64];
memcpy(cat, chain, 32);
memcpy(cat + 32, event, 32);
- return sha256_calc(cat, sizeof(cat), chain);
+ return sha256_buffer(cat, sizeof(cat), chain);
}
diff --git a/testing/helper/test-register.c b/testing/helper/test-register.c
index 5a0b973b..da0b6954 100644
--- a/testing/helper/test-register.c
+++ b/testing/helper/test-register.c
@@ -20,6 +20,7 @@
#include "attest.h"
#include "chain.h"
#include "config.h"
+#include "crypto.h"
#include "eventlog.h"
#include "hex.h"
#include "identity.h"
@@ -467,8 +468,8 @@ int main(int argc, char **argv)
if (!ret)
ret = chain_anchor(nv.name, anchor, sizeof(anchor));
if (!ret)
- ret = sha256_calc(anchor, strlen(anchor),
- anchor_digest);
+ ret = sha256_buffer(anchor, strlen(anchor),
+ anchor_digest);
if (!ret)
ret = tpm_pcr_extend(tpm, pcr, anchor_digest);
if (!ret)
diff --git a/testing/proofs/cbmc/bpm-tree-state-replay.c b/testing/proofs/cbmc/bpm-tree-state-replay.c
index 16f9ca15..9f5d6ee8 100644
--- a/testing/proofs/cbmc/bpm-tree-state-replay.c
+++ b/testing/proofs/cbmc/bpm-tree-state-replay.c
@@ -159,7 +159,7 @@ static void model_hash(const __u8 *in, __u8 out[32])
}
}
-int sha256_calc(const void *data, size_t len, __u8 out[32])
+int sha256_buffer(const void *data, size_t len, __u8 out[32])
{
__CPROVER_assert(len == 64, "the root before and the digest");
if (hash_error)
diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.c
index 165166ab..a117fce0 100644
--- a/testing/unit/bpm_tree_state.c
+++ b/testing/unit/bpm_tree_state.c
@@ -536,7 +536,7 @@ static void eventlog_matches_only_a_state_read(void)
EXPECT_EQ(tree_eventlog_matches(&s, 0, replay, &matches), -ENOENT);
EXPECT_TRUE(matches);
ASSERT_EQ(sha256_digest_decode(PENDING_DIGEST, next + 32), 0);
- ASSERT_EQ(sha256_calc(next, sizeof(next), root), 0);
+ ASSERT_EQ(sha256_buffer(next, sizeof(next), root), 0);
ASSERT_EQ(tree_event_append(1, PENDING_DIGEST, root), 0);
hex_encode(root, sizeof(root), s.projection_root);
ASSERT_EQ(tree_eventlog_matches(&s, 0, replay, &matches), 0);
diff --git a/testing/unit/chain.c b/testing/unit/chain.c
index bc258519..1ad9b2ea 100644
--- a/testing/unit/chain.c
+++ b/testing/unit/chain.c
@@ -2,26 +2,14 @@
/* Copyright (c) 2026 Authors of shirudo */
#include "chain.h"
-#include "hex.h"
+#include "crypto.h"
#include "unit.h"
-static void sha256_matches_fips_180_vector(void)
-{
- static const char want[] =
- "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
- char hex[2 * 32 + 1];
- __u8 d[32];
-
- ASSERT_EQ(sha256_calc("abc", 3, d), 0);
- hex_encode(d, sizeof(d), hex);
- EXPECT_STREQ(hex, want);
-}
-
static void sha256_2_hashes_the_concatenation(void)
{
__u8 one[32], two[32];
- ASSERT_EQ(sha256_calc("abc", 3, one), 0);
+ ASSERT_EQ(sha256_buffer("abc", 3, one), 0);
ASSERT_EQ(sha256_2("ab", 2, "c", 1, two), 0);
EXPECT_MEMEQ(two, one, sizeof(one));
ASSERT_EQ(sha256_2("abc", 3, NULL, 0, two), 0);
@@ -46,7 +34,7 @@ static void chain_events_hash_their_documented_input(void)
memset(cert, 0xa5, sizeof(cert));
memcpy(buf, tag, sizeof(tag));
memcpy(buf + sizeof(tag), cert, sizeof(cert));
- ASSERT_EQ(sha256_calc(buf, sizeof(buf), want), 0);
+ ASSERT_EQ(sha256_buffer(buf, sizeof(buf), want), 0);
ASSERT_EQ(chain_key_event(tag, cert, event), 0);
EXPECT_MEMEQ(event, want, sizeof(want));
}
@@ -57,13 +45,12 @@ static void chain_fold_extends_like_the_register(void)
memset(event, 0x5a, sizeof(event));
memcpy(cat + 32, event, sizeof(event));
- ASSERT_EQ(sha256_calc(cat, sizeof(cat), want), 0);
+ ASSERT_EQ(sha256_buffer(cat, sizeof(cat), want), 0);
ASSERT_EQ(chain_fold(chain, event), 0);
EXPECT_MEMEQ(chain, want, sizeof(want));
}
static const struct unit_case cases[] = {
- UNIT_CASE(sha256_matches_fips_180_vector),
UNIT_CASE(sha256_2_hashes_the_concatenation),
UNIT_CASE(sha256_2_reports_a_failure),
UNIT_CASE(chain_events_hash_their_documented_input),
diff --git a/testing/unit/crypto_openssl.c b/testing/unit/crypto_openssl.c
index b5bfc812..4172863c 100644
--- a/testing/unit/crypto_openssl.c
+++ b/testing/unit/crypto_openssl.c
@@ -4,8 +4,21 @@
#include <string.h>
#include "crypto.h"
+#include "hex.h"
#include "unit.h"
+static void sha256_matches_fips_180_vector(void)
+{
+ static const char want[] =
+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
+ char hex[2 * CRYPTO_SHA256_LEN + 1];
+ __u8 d[CRYPTO_SHA256_LEN];
+
+ ASSERT_EQ(sha256_buffer("abc", 3, d), 0);
+ hex_encode(d, sizeof(d), hex);
+ EXPECT_STREQ(hex, want);
+}
+
static void sha256_buffer_zeroes_a_failed_digest(void)
{
__u8 out[CRYPTO_SHA256_LEN], zero[CRYPTO_SHA256_LEN] = {};
@@ -16,6 +29,7 @@ static void sha256_buffer_zeroes_a_failed_digest(void)
}
static const struct unit_case cases[] = {
+ UNIT_CASE(sha256_matches_fips_180_vector),
UNIT_CASE(sha256_buffer_zeroes_a_failed_digest),
{}
};
diff --git a/util/attest.c b/util/attest.c
index 74056fe2..e5976e75 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -1196,8 +1196,8 @@ int attest_verify(const char *evidence, size_t len,
char anchor[CHAIN_ANCHOR_MAX];
bool named = !chain_anchor(p->init.name, anchor,
sizeof(anchor)) &&
- !sha256_calc(anchor, strlen(anchor),
- anchor_digest);
+ !sha256_buffer(anchor, strlen(anchor),
+ anchor_digest);
say(&rep, named &&
!memneq_explicit(anchor_digest, p->init.anchor_digest, 32),
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 611f84d1..c351d5f5 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -25,6 +25,7 @@
#include "bpm_tree.h"
#include "chain.h"
+#include "crypto.h"
#include "io.h"
#include "json.h"
#include "kernel_build_id.h"
@@ -728,7 +729,7 @@ int tree_event_replay(__u8 out[32])
}
memcpy(next, chain, 32);
memcpy(next + 32, d, 32);
- ret = sha256_calc(next, sizeof(next), chain);
+ ret = sha256_buffer(next, sizeof(next), chain);
if (ret)
break;
if (memneq_explicit(chain, recorded, 32)) {
diff --git a/util/chain.c b/util/chain.c
index 77fb4aa8..0adc79dc 100644
--- a/util/chain.c
+++ b/util/chain.c
@@ -28,15 +28,6 @@
#include "paths.h"
#include "tpm.h"
-int sha256_calc(const void *data, size_t len, __u8 out[32])
-{
- int ret = sha256_buffer(data, len, out);
-
- if (ret)
- memset(out, 0, 32);
- return ret;
-}
-
int sha256_2(const void *a, size_t a_len,
const void *b, size_t b_len, __u8 out[32])
{
@@ -95,7 +86,7 @@ int chain_init_event(const __u8 cert_sha[32],
}
off += (size_t)w;
}
- ret = sha256_calc(listing, listing_len, set_digest);
+ ret = sha256_buffer(listing, listing_len, set_digest);
xfree(listing, listing_len + 1);
if (ret)
return ret;
@@ -104,7 +95,7 @@ int chain_init_event(const __u8 cert_sha[32],
memcpy(tag + taglen, cert_sha, 32);
memcpy(tag + taglen + 32, set_digest, 32);
memcpy(tag + taglen + 64, tree_root, 32);
- return sha256_calc(tag, taglen + 96, event);
+ return sha256_buffer(tag, taglen + 96, event);
}
int chain_upatch_event(const char *tag, const char *name,
@@ -120,7 +111,7 @@ int chain_upatch_event(const char *tag, const char *name,
off += 32;
memcpy(buf + off, name, namelen);
off += namelen;
- return sha256_calc(buf, off, event);
+ return sha256_buffer(buf, off, event);
}
int chain_key_event(const char *tag, const __u8 cert_sha[32],
@@ -140,7 +131,7 @@ int chain_tree_event(__s64 seq, const __u8 operation_digest[32],
for (i = 0; i < 8; i++)
buf[taglen + i] = (__u8)(s >> (56 - 8 * i));
memcpy(buf + taglen + 8, operation_digest, 32);
- return sha256_calc(buf, taglen + 40, event);
+ return sha256_buffer(buf, taglen + 40, event);
}
int chain_fold(__u8 chain[32], const __u8 event[32])
@@ -149,7 +140,7 @@ int chain_fold(__u8 chain[32], const __u8 event[32])
memcpy(cat, chain, 32);
memcpy(cat + 32, event, 32);
- return sha256_calc(cat, sizeof(cat), chain);
+ return sha256_buffer(cat, sizeof(cat), chain);
}
int chain_anchor(const __u8 name[TPM_NAME_LEN],
@@ -375,8 +366,8 @@ int chain_init(const __u8 event[32],
goto out;
memcpy(info->name, written, sizeof(written));
info->name_len = sizeof(written);
- ret = sha256_calc(info->anchor, strlen(info->anchor),
- info->anchor_digest);
+ ret = sha256_buffer(info->anchor, strlen(info->anchor),
+ info->anchor_digest);
if (ret)
goto out;
ret = tpm_pcr_extend(tpm, CHAIN_PCR,
diff --git a/util/chain.h b/util/chain.h
index abaa93f5..ed8d1c91 100644
--- a/util/chain.h
+++ b/util/chain.h
@@ -34,9 +34,6 @@
(1u << CHAIN_PHASE_PCR))
#define CHAIN_ANCHOR_MAX 128
-int sha256_calc(const void *data, size_t len,
- __u8 out[CHAIN_DIGEST_LEN]) __must_check;
-
int sha256_2(const void *a, size_t a_len,
const void *b, size_t b_len,
__u8 out[CHAIN_DIGEST_LEN]) __must_check;
diff --git a/util/eventlog.c b/util/eventlog.c
index 3a7b89b9..827cc3c6 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -21,6 +21,7 @@
#include <shirudo/upatch.h>
#include "chain.h"
+#include "crypto.h"
#include "eventlog.h"
#include "hex.h"
#include "timefmt.h"
@@ -380,7 +381,7 @@ static int replay_line(const char *line, size_t len, bool is_init, void *ctx)
memset(chain, 0, 32);
memcpy(cat, chain, 32);
memcpy(cat + 32, digest, 32);
- return sha256_calc(cat, sizeof(cat), chain);
+ return sha256_buffer(cat, sizeof(cat), chain);
}
int eventlog_replay(__u8 out[32])