Portable C99 cryptographic primitives for embedded and constrained systems.
microcrypt is a low-level primitive library. It provides SHA-256, HMAC-SHA256, AES-128 block transforms, AES-128-CBC helpers, and AES-128-GCM AEAD. It does not provide padding or a message encryption protocol.
This repository does not claim audit status, formal proof, FIPS validation, or side-channel resistance. AES uses lookup tables and is not suitable against cache/timing/power/EM attackers on observable platforms. CBC provides confidentiality only and must be used with authenticated designs.
- SHA-256 with checked init/update/final lifecycle
- HMAC-SHA256 with checked verification
- AES-128 block encrypt/decrypt
- AES-128-CBC with explicit chaining output
- AES-128-GCM authenticated encryption with a 12-byte nonce, 16-byte tag, and AAD
- Secure clearing helpers for secret material
cmake -S . -B build
cmake --build build --config Debug
ctest --test-dir build -C Debug --output-on-failureThe CMake package exports microcrypt::microcrypt.
find_package(microcrypt CONFIG REQUIRED) target_link_libraries(app PRIVATE microcrypt::microcrypt)
#include "mcrypt.h" uint8_t digest[MCRYPT_SHA256_DIGEST_SIZE]; mcrypt_status_t status = mcrypt_sha256("abc", 3, digest); if (status != MCRYPT_OK) { /* handle error */ }
mcrypt_hmac_sha256_t ctx; uint8_t mac[MCRYPT_HMAC_SHA256_SIZE]; if (mcrypt_hmac_sha256_init(&ctx, key, key_len) == MCRYPT_OK && mcrypt_hmac_sha256_update(&ctx, data, data_len) == MCRYPT_OK && mcrypt_hmac_sha256_final(&ctx, mac) == MCRYPT_OK) { /* mac is ready */ }
AES-GCM nonces must be unique for every encryption under a given key. The library neither generates nor manages nonces.
Current version: 3.0.1
mcrypt_hmac_sha256_t changed layout in 3.0.0. Recompile consumers against
3.0.0: source uses such as mcrypt_hmac_sha256_t ctx; remain compatible after
recompilation, but binary compatibility with the 2.x public layout is not
guaranteed. The context is reduced from 320 B to 192 B, and SHA-256 uses a
16-word circular message schedule without changing algorithm outputs.