Mbed TLS hashes through C++23 modules: SHA-1, SHA-2, SHA-3, MD5, HMAC — import, don't include.
English · 简体中文 · Upstream · Releases · mcpp · Index
The upstream API is unchanged. mbedtls_sha256, mbedtls_sha256_context, mbedtls_md_hmac,
mbedtls_md_info_from_type and the rest are the names of <mbedtls/sha256.h> and <mbedtls/md.h>,
in the global namespace where upstream declares them. The library is compat.mbedtls 3.6.8,
built once; the modules only declare its interface.
import mcpplibs.mwrap.mbedtls;: every module belowimport mcpplibs.mwrap.mbedtls.sha256;:mbedtls_sha256_context,mbedtls_sha256_init/_free/_clone/_starts/_update/_finish,mbedtls_sha256(SHA-224 withis224 = 1)import mcpplibs.mwrap.mbedtls.sha512;: the same for SHA-512 / SHA-384 (is384 = 1)import mcpplibs.mwrap.mbedtls.sha1;,.md5: SHA-1 and MD5import mcpplibs.mwrap.mbedtls.sha3;:mbedtls_sha3_id(MBEDTLS_SHA3_224…MBEDTLS_SHA3_512),mbedtls_sha3_context,mbedtls_sha3import mcpplibs.mwrap.mbedtls.md;: the generic API —mbedtls_md_type_t(MBEDTLS_MD_SHA256, …),mbedtls_md_info_from_type/_string/_ctx,mbedtls_md_get_name/_size/_type,mbedtls_md_context_t,mbedtls_md,mbedtls_md_hmacand the streamingmbedtls_md_hmac_*
More Modules API -> Usage and API
mcpp new hash --template mcpplibs.mwrap.mbedtls@3.6.8.1
cd hash
mcpp runThe default template prints the SHA-256, SHA-512 and HMAC-SHA-256 of a text or a file, and checks that streaming gives the same SHA-256.
[dependencies]
mcpplibs.mwrap.mbedtls = "3.6.8.1"import std;
import mcpplibs.mwrap.mbedtls;
int main() {
unsigned char out[32];
mbedtls_sha256(reinterpret_cast<const unsigned char*>("abc"), 3, out, 0);
for (auto b : out) std::print("{:02x}", b); // ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
}tools/generate.py reads the six headers of the compat.mbedtls package mcpp resolved and writes
src/<module>.cppm from them: every public function and type, under its upstream name, nothing
else. tools/generate.py --check rewrites nothing and fails when a committed module differs, so
the export list is audited in CI against the library that is linked.
Three facts of the headers are recorded in the generated files, each measured with clang 23:
- an unscoped enum's enumerators reach an importer through
using enum, not throughusing; - an opaque handle (
typedef struct mbedtls_md_info_t mbedtls_md_info_t;, its body in the library) is exported only by ausingat namespace scope, not from inside anexport { … }block; - the
mbedtls_internal_*_processsteps are upstream's own and are not exported.
The *_self_test functions are declared by the headers and exported with the rest; the library
links them only where it was built with MBEDTLS_SELF_TEST_C, and tests/linked.cpp says which.
A translation unit that both includes the headers and imports the wrapper puts every #include
before the first import. tests/header_mix_test.cpp checks that the module's names are the
header's, through the types of the function pointers.
Linux, macOS and Windows CI build the modules with GCC and LLVM and run the tests: the published
digests of the empty input and of abc for every hash, streaming equal to one call, a clone,
the generic API by type and by name, HMAC-SHA-256 from RFC 4231 (one call and streamed), the
header-mixing check and every exported function linked. CI also runs the export audit
(tools/generate.py --check) and, from a real install, the default template.
Apache-2.0 for the wrapper, by mcpp-community (NOTICE). Mbed TLS remains under its dual Apache-2.0 OR GPL-2.0-or-later license (upstream/mbedtls/LICENSE, README).