Skip to content

Commit 5e38c20

Browse files
authored
mwrap: mbedtls 3.6.8 (Mbed TLS hashes as C++23 modules over compat.mbedtls) (#935)
* mwrap: mbedtls 3.6.8 (Mbed TLS hashes as C++23 modules over compat.mbedtls), consumer mbedtls-hashes * docs: add mbedtls-m 3.6.8 plan * docs: the plan names its PR --------- Co-authored-by: Sunrisepeak <Sunrisepeak@users.noreply.github.com>
1 parent 94b871e commit 5e38c20

5 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 收录 mcpplibs.mwrap.mbedtls 3.6.8(mbedtls-m)
2+
3+
> PR: https://github.com/mcpplibs/mcpp-index/pull/935
4+
> 日期:2026-12-01
5+
> 背景:gitxxx(Sunrisepeak/gitxxx)需要 sha256/HMAC,索引里没有模块化的哈希 API(wuffs-m 只包了 crc32/adler32,tinyhttps 未导出 sha256),而 `compat.mbedtls` 已在索引中。按 mwrap 规范为 Mbed TLS 的哈希接口做模块包装,再登记进索引。
6+
7+
## 1. 来源与形态判定
8+
9+
- 来源:**(b) 基于 mcpp 开发的库**。[mcpplibs/mbedtls-m](https://github.com/mcpplibs/mbedtls-m) 自带 `mcpp.toml`,依赖 `compat.mbedtls = "3.6.8"`。
10+
- 版本:首发 `3.6.8`(等于上游 Mbed TLS 版本;tag `mwrap-3.6.8`)。
11+
- License:包装层 Apache-2.0;Mbed TLS 为 Apache-2.0 OR GPL-2.0-or-later,静态链接,包通过 `runtime.deploy` 部署其 README 与 LICENSE。
12+
- 形态:**Form A** 模块库,描述符只声明元数据与下载地址。
13+
14+
## 2. 包内容
15+
16+
`src/<module>.cppm` 由 `tools/generate.py` 从 `compat.mbedtls` 的头文件生成(`--check` 在 CI 审计):模块 `sha256`、`sha512`、`sha1`、`md5`、`sha3`、`md`(通用摘要与 HMAC),根模块重导出全部。上游名字原样保留(`mbedtls_sha256`、`mbedtls_md_hmac`、`mbedtls_md_type_t` …),位于全局命名空间。
17+
18+
三条导出规则(clang 23 实测)写在生成文件里:非作用域枚举的枚举量经 `using enum` 导出;不透明句柄(`typedef struct mbedtls_md_info_t mbedtls_md_info_t;`)只能由命名空间作用域的 `using` 导出,`export { … }` 块内无效;`mbedtls_internal_*_process` 不导出。
19+
20+
## 3. 镜像
21+
22+
无 `mcpp-res` 写权限,描述符使用纯字符串 `url = "<GLOBAL release>"`(lint 允许),CN 镜像由维护者后续补充。
23+
24+
## 4. 自动更新
25+
26+
`extensions.autoupdate = { tag = "mwrap-${version}", url = ".../releases/download/${tag}/mbedtls-m-${version}.tar.gz" }`:GitHub release、独立 tag 序列 `mwrap-*`,符合自动更新条件。
27+
28+
## 5. 消费者与验证
29+
30+
- `tests/mwrap/mbedtls-hashes`:`tests/digests.cpp` 以 `return n` 形式断言——SHA-224/256、SHA-384/512、SHA-1、MD5、SHA3-256 对 `abc` 的公开摘要值,流式与一次调用一致,RFC 4231 的 HMAC-SHA-256。
31+
- 已加入仓根 `[workspace].members`。
32+
- 本地:`bash tests/run_members.sh mbedtls-hashes`(结果见 PR)。
33+
- 上游仓库 CI(linux gcc/llvm、linux-musl、macos、windows)与默认模板的实装运行均已通过:https://github.com/mcpplibs/mbedtls-m/actions。
34+
35+
## 6. 注意事项
36+
37+
- `*_self_test` 函数由头文件声明并导出,但 `compat.mbedtls` 未以 `MBEDTLS_SELF_TEST_C` 构建,链接它们会失败;`tests/linked.cpp` 说明了这一点,文档要求在 `#if defined(MBEDTLS_SELF_TEST_C)` 下调用。
38+
- 版本尾号:首个 mwrap 身份直接用上游版本 `3.6.8`,后续包装修订才加 `.1`。

‎mcpp.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ members = [
6262
"tests/mwrap/godot-math",
6363
"tests/mwrap/godot-sdk",
6464
"tests/mwrap/onnxruntime-sdk",
65+
"tests/mwrap/mbedtls-hashes",
6566
"tests/examples/abseil",
6667
"tests/examples/archive",
6768
"tests/examples/argparse",

‎pkgs/mwrap/mbedtls.lua‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- Immutable upstream-aligned mwrap source release (mbedtls-m mwrap-3.6.8).
2+
-- GLOBAL only: the CN mirror under mcpp-res is a maintainer's step; the
3+
-- plain string form is what the lint allows until then (docs/cn-mirror.md).
4+
--
5+
-- The wrapper is Apache-2.0; the library it links, compat.mbedtls (Mbed TLS
6+
-- 3.6.8), is Apache-2.0 OR GPL-2.0-or-later, linked statically: the package
7+
-- deploys Mbed TLS's README and LICENSE beside programs (runtime.deploy).
8+
package = {
9+
spec = "1",
10+
namespace = "mcpplibs.mwrap",
11+
name = "mbedtls",
12+
description = "mbedtls-m: Mbed TLS 3.6.8 hashes (SHA-1, SHA-224/256, SHA-384/512, SHA-3, MD5, the generic digest and HMAC) as C++23 modules over compat.mbedtls",
13+
licenses = { "Apache-2.0" },
14+
repo = "https://github.com/mcpplibs/mbedtls-m",
15+
type = "package",
16+
extensions = {
17+
autoupdate = {
18+
tag = "mwrap-${version}",
19+
url = "https://github.com/mcpplibs/mbedtls-m/releases/download/${tag}/mbedtls-m-${version}.tar.gz",
20+
},
21+
},
22+
xpm = {
23+
linux = {
24+
["3.6.8"] = {
25+
url = "https://github.com/mcpplibs/mbedtls-m/releases/download/mwrap-3.6.8/mbedtls-m-3.6.8.tar.gz",
26+
sha256 = "91a2ecb55857499426bc764a3b7c1a1f2f94d3f8051e962cb4ff1db99f2c1584",
27+
},
28+
},
29+
macosx = {
30+
["3.6.8"] = {
31+
url = "https://github.com/mcpplibs/mbedtls-m/releases/download/mwrap-3.6.8/mbedtls-m-3.6.8.tar.gz",
32+
sha256 = "91a2ecb55857499426bc764a3b7c1a1f2f94d3f8051e962cb4ff1db99f2c1584",
33+
},
34+
},
35+
windows = {
36+
["3.6.8"] = {
37+
url = "https://github.com/mcpplibs/mbedtls-m/releases/download/mwrap-3.6.8/mbedtls-m-3.6.8.tar.gz",
38+
sha256 = "91a2ecb55857499426bc764a3b7c1a1f2f94d3f8051e962cb4ff1db99f2c1584",
39+
},
40+
},
41+
},
42+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "mbedtls-hashes"
3+
version = "3.6.8"
4+
description = "Consume the immutable Mbed TLS wrapper: the published digests of abc for every hash, streaming equal to one call, and HMAC-SHA-256 from RFC 4231"
5+
6+
[dependencies]
7+
mcpplibs.mwrap.mbedtls = "3.6.8"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// The installed Mbed TLS wrapper: the published digests of "abc" for every
2+
// hash, streaming equal to one call, and HMAC-SHA-256 from RFC 4231.
3+
import std;
4+
import mcpplibs.mwrap.mbedtls;
5+
6+
namespace {
7+
std::string hex(const unsigned char* bytes, std::size_t n) {
8+
std::string out;
9+
for (std::size_t i = 0; i < n; ++i) out += std::format("{:02x}", bytes[i]);
10+
return out;
11+
}
12+
bool sha256_of(std::string_view text, std::string_view want) {
13+
unsigned char out[32] = {};
14+
return mbedtls_sha256(reinterpret_cast<const unsigned char*>(text.data()), text.size(), out, 0) == 0
15+
&& hex(out, 32) == want;
16+
}
17+
} // namespace
18+
19+
int main() {
20+
if (!sha256_of("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")) return 1;
21+
unsigned char out[64] = {};
22+
if (mbedtls_sha512(reinterpret_cast<const unsigned char*>("abc"), 3, out, 0) != 0
23+
|| hex(out, 64) != "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
24+
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")
25+
return 2;
26+
if (mbedtls_sha1(reinterpret_cast<const unsigned char*>("abc"), 3, out) != 0
27+
|| hex(out, 20) != "a9993e364706816aba3e25717850c26c9cd0d89d") return 3;
28+
if (mbedtls_md5(reinterpret_cast<const unsigned char*>("abc"), 3, out) != 0
29+
|| hex(out, 16) != "900150983cd24fb0d6963f7d28e17f72") return 4;
30+
if (mbedtls_sha3(MBEDTLS_SHA3_256, reinterpret_cast<const std::uint8_t*>("abc"), 3, out, 32) != 0
31+
|| hex(out, 32) != "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532") return 5;
32+
33+
// streaming in uneven pieces equals one call
34+
std::string text;
35+
for (int i = 0; i < 400; ++i) text += std::format("{},", i * 31);
36+
mbedtls_sha256_context ctx;
37+
mbedtls_sha256_init(&ctx);
38+
bool ok = mbedtls_sha256_starts(&ctx, 0) == 0;
39+
for (std::size_t at = 0; ok && at < text.size(); at += 37)
40+
ok = mbedtls_sha256_update(&ctx, reinterpret_cast<const unsigned char*>(text.data() + at),
41+
std::min<std::size_t>(37, text.size() - at)) == 0;
42+
unsigned char streamed[32] = {}, direct[32] = {};
43+
ok = ok && mbedtls_sha256_finish(&ctx, streamed) == 0;
44+
mbedtls_sha256_free(&ctx);
45+
ok = ok && mbedtls_sha256(reinterpret_cast<const unsigned char*>(text.data()), text.size(), direct, 0) == 0
46+
&& std::equal(streamed, streamed + 32, direct);
47+
if (!ok) return 6;
48+
49+
// HMAC-SHA-256, RFC 4231 test case 2
50+
const unsigned char key[] = "key";
51+
constexpr std::string_view msg = "The quick brown fox jumps over the lazy dog";
52+
unsigned char mac[32] = {};
53+
if (mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), key, 3,
54+
reinterpret_cast<const unsigned char*>(msg.data()), msg.size(), mac) != 0
55+
|| hex(mac, 32) != "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8")
56+
return 7;
57+
std::println("Mbed TLS SHA-256 via mcpplibs.mwrap.mbedtls");
58+
return 0;
59+
}

0 commit comments

Comments
 (0)