Cryptographic primitives for fastC — SHA-256, HMAC, constant-time compare, secure random.
Part of the fastc-core launch set. The implementation currently
ships inside the fastC compiler's built-in prelude — every fastC
v1.0 program already gets use crypto_primitives::* for free. This
repository is the public home for the module's API and will become
installable via fastc add github.com/Skelf-Research/fastc-core-crypto-primitives
once stage 1.7's vendor-consumption flow completes the loop.
use crypto_primitives::sha256;
// (data: slice(u8)) -> arr(u8, 32)
// FIPS 180-4 SHA-256 hash of `data`.
use crypto_primitives::hmac_sha256;
// (key: slice(u8), data: slice(u8)) -> arr(u8, 32)
// RFC 2104 HMAC built on SHA-256.
use crypto_primitives::constant_time_compare;
// (a: slice(u8), b: slice(u8)) -> bool
// Timing-attack-resistant byte equality. Runs in time
// proportional to max(len(a), len(b)) regardless of where
// the first differing byte sits.
use crypto_primitives::random_bytes;
// (cap: ref(CapRand), n: usize) -> Vec[u8]
// `n` bytes drawn from the OS CSPRNG. Requires `CapRand`.
use crypto_primitives::sha256;
use crypto_primitives::constant_time_compare;
use io::println;
fn main() -> i32 {
let msg: slice(u8) = b"hello, fastc";
let digest_a: arr(u8, 32) = sha256(msg);
let digest_b: arr(u8, 32) = sha256(msg);
if (constant_time_compare(digest_a[..], digest_b[..])) {
println(cstr("digests match"));
} else {
println(cstr("digests differ"));
}
return 0;
}
sha256,hmac_sha256,constant_time_compare— pure. No capability token required; these touch only their arguments.random_bytes— requiresref(CapRand). The token is minted inmod capsand threaded down to whichever scope needs entropy. Pulling randomness withoutCapRandis a compile-time error, not a runtime one.
These primitives are intended for general-purpose use inside fastC programs. The SHA-256 core is implemented per FIPS 180-4 and HMAC per RFC 2104, but this package has not been independently audited or certified. Users with FIPS 140-3, NIST validation, or any other compliance obligation should consult their auditor before relying on it; the right move there is usually to wrap a vetted native library through fastC's FFI rather than ship this code into a regulated boundary.
constant_time_compare is written to avoid data-dependent branches
and memory accesses on the supported targets, but timing-channel
freedom is a property of the whole stack (compiler, CPU, OS).
Treat it as a strong default, not a formal proof.
v0.1.0 — preview. API is final; the package becomes a true
installable via fastc add once the consumption flow ships. Until
then, the same API is available in every fastC v1.0 program via
the built-in prelude.
MIT