Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

2.2 KB
Raw Blame
#ifndef CBM_SHA256_H
#define CBM_SHA256_H

/* In-process SHA-256 (FIPS 180-4). Used to verify the integrity of a
 * downloaded release before installing it, without shelling out to a
 * platform hashing tool (shasum / sha256sum / certutil) — those differ per
 * OS, may be absent, and mis-quote paths under cmd.exe. */

#include <stddef.h>
#include <stdint.h>

#define CBM_SHA256_DIGEST_LEN 32 /* raw digest bytes */
#define CBM_SHA256_HEX_LEN 64    /* lowercase hex chars (no NUL) */

typedef struct {
    uint32_t state[8];
    uint64_t bitlen;
    uint8_t buf[64];
    size_t buflen;
    /* Message-schedule scratch for one compression round. It lives in the
     * context rather than in sha256_transform's own frame because a 256-byte
     * local there is large enough for ASan's use-after-return fake stack,
     * which then heap-allocates it on EVERY 64-byte block — millions of
     * __asan_stack_malloc calls to fingerprint one large file, turning
     * sanitized binary fingerprinting into minutes. Here it is allocated once
     * per hash instead of once per block. */
    uint32_t sched[64];
} cbm_sha256_ctx;

void cbm_sha256_init(cbm_sha256_ctx *c);
void cbm_sha256_update(cbm_sha256_ctx *c, const void *data, size_t len);
void cbm_sha256_final(cbm_sha256_ctx *c, uint8_t out[CBM_SHA256_DIGEST_LEN]);

/* One-shot hash of a buffer to lowercase hex. `out` must hold
 * CBM_SHA256_HEX_LEN + 1 bytes (hex chars + NUL). */
void cbm_sha256_hex(const void *data, size_t len, char out[CBM_SHA256_HEX_LEN + 1]);

/* RFC 2104 HMAC-SHA-256. The output is always CBM_SHA256_DIGEST_LEN bytes.
 * A NULL key/data pointer is accepted only when its corresponding length is
 * zero. */
void cbm_hmac_sha256(const void *key, size_t key_len, const void *data, size_t data_len,
                     uint8_t out[CBM_SHA256_DIGEST_LEN]);

#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
#include <stdbool.h>
/* The block backend in use: "arm-sha2", "x86-sha-ni" or "portable". */
const char *cbm_sha256_backend_name_for_testing(void);
/* Force the portable transform (true) or return to the probed backend (false),
 * so a test can hash the same input both ways. */
void cbm_sha256_force_portable_for_testing(bool force);
#endif

#endif /* CBM_SHA256_H */