Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 0 additions & 92 deletions .tmp_tt.py

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ built once; the modules only declare its interface.
## Getting started

```sh
mcpp new hash --template mcpplibs.mwrap.mbedtls@3.6.8
mcpp new hash --template mcpplibs.mwrap.mbedtls@3.6.8.1
cd hash
mcpp run
```
Expand All @@ -42,7 +42,7 @@ that streaming gives the same SHA-256.

```toml
[dependencies]
mcpplibs.mwrap.mbedtls = "3.6.8"
mcpplibs.mwrap.mbedtls = "3.6.8.1"
```

```cpp
Expand Down
4 changes: 2 additions & 2 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
## 快速开始

```sh
mcpp new hash --template mcpplibs.mwrap.mbedtls@3.6.8
mcpp new hash --template mcpplibs.mwrap.mbedtls@3.6.8.1
cd hash
mcpp run
```
Expand All @@ -40,7 +40,7 @@ mcpp run

```toml
[dependencies]
mcpplibs.mwrap.mbedtls = "3.6.8"
mcpplibs.mwrap.mbedtls = "3.6.8.1"
```

```cpp
Expand Down
4 changes: 2 additions & 2 deletions mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
[package]
namespace = "mcpplibs.mwrap"
name = "mbedtls"
version = "3.6.8"
description = "Mbed TLS 3.6.8 hashes as C++ modules for mcpp (import mcpplibs.mwrap.mbedtls;): SHA-1, SHA-224/256, SHA-384/512, SHA-3, MD5, the generic message-digest and HMAC API, over compat.mbedtls"
version = "3.6.8.1"
description = "Mbed TLS 3.6.8 hashes as C++ modules for mcpp (import mcpplibs.mwrap.mbedtls; or one sub-module, mcpplibs.mwrap.mbedtls.sha256): SHA-1, SHA-224/256, SHA-384/512, SHA-3, MD5, the generic message-digest and HMAC API, over compat.mbedtls"
license = "Apache-2.0"
authors = ["mcpp-community"]
repo = "https://github.com/mcpplibs/mbedtls-m"
Expand Down
16 changes: 9 additions & 7 deletions src/mbedtls.cppm
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// mcpplibs.mwrap.mbedtls --- the root: every public module below, re-exported.
// mcpplibs.mwrap.mbedtls --- the root: every public sub-module below, re-exported.
// An importer takes the root for all of them, or one sub-module on its own
// (`mcpplibs.mwrap.mbedtls.sha256`, ...), as the mwrap spec names them.
// The names are Mbed TLS's own (mbedtls_sha256, mbedtls_md_hmac, ...), in the
// global namespace where upstream declares them. The implementation is
// compat.mbedtls. Written by hand: a root has nothing to read from a header.
export module mcpplibs.mwrap.mbedtls;

export import :sha256;
export import :sha512;
export import :sha1;
export import :md5;
export import :sha3;
export import :md;
export import mcpplibs.mwrap.mbedtls.sha256;
export import mcpplibs.mwrap.mbedtls.sha512;
export import mcpplibs.mwrap.mbedtls.sha1;
export import mcpplibs.mwrap.mbedtls.md5;
export import mcpplibs.mwrap.mbedtls.sha3;
export import mcpplibs.mwrap.mbedtls.md;
2 changes: 1 addition & 1 deletion src/md.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module;
#include <mbedtls/md.h>

export module mcpplibs.mwrap.mbedtls:md;
export module mcpplibs.mwrap.mbedtls.md;

export using ::mbedtls_md_info_t;

Expand Down
2 changes: 1 addition & 1 deletion src/md5.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module;
#include <mbedtls/md5.h>

export module mcpplibs.mwrap.mbedtls:md5;
export module mcpplibs.mwrap.mbedtls.md5;

export {
using ::mbedtls_md5_context;
Expand Down
2 changes: 1 addition & 1 deletion src/sha1.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module;
#include <mbedtls/sha1.h>

export module mcpplibs.mwrap.mbedtls:sha1;
export module mcpplibs.mwrap.mbedtls.sha1;

export {
using ::mbedtls_sha1_context;
Expand Down
2 changes: 1 addition & 1 deletion src/sha256.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module;
#include <mbedtls/sha256.h>

export module mcpplibs.mwrap.mbedtls:sha256;
export module mcpplibs.mwrap.mbedtls.sha256;

export {
using ::mbedtls_sha256_context;
Expand Down
2 changes: 1 addition & 1 deletion src/sha3.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module;
#include <mbedtls/sha3.h>

export module mcpplibs.mwrap.mbedtls:sha3;
export module mcpplibs.mwrap.mbedtls.sha3;

export {
using enum mbedtls_sha3_id;
Expand Down
2 changes: 1 addition & 1 deletion src/sha512.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module;
#include <mbedtls/sha512.h>

export module mcpplibs.mwrap.mbedtls:sha512;
export module mcpplibs.mwrap.mbedtls.sha512;

export {
using ::mbedtls_sha512_context;
Expand Down
46 changes: 46 additions & 0 deletions tests/submodule_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Each sub-module stands on its own, as the README and the mwrap spec name
// them: an importer that takes only `mcpplibs.mwrap.mbedtls.sha256` gets the
// SHA-256 API without the root. (mwrap-3.6.8.1: until then they were partitions
// of the root, and this import did not compile.)
import std;
import mcpplibs.mwrap.mbedtls.sha256;

namespace {

int failures = 0;
void check(bool ok, std::string_view what)
{
if (!ok) ++failures;
std::println("{} {}", ok ? "ok " : "FAIL", what);
}

std::string hex(const unsigned char* bytes, std::size_t n)
{
std::string out;
for (std::size_t i = 0; i < n; ++i) out += std::format("{:02x}", bytes[i]);
return out;
}

} // namespace

int main()
{
// FIPS 180-2's vector for "abc", by the one-shot call.
unsigned char out[32] = {};
const int rc = mbedtls_sha256(reinterpret_cast<const unsigned char*>("abc"), 3, out, 0);
check(rc == 0 && hex(out, 32) == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"the sha256 sub-module alone hashes \"abc\" to the published digest");

// The context type and the streaming calls come with it.
mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
int rc2 = mbedtls_sha256_starts(&ctx, 0);
rc2 |= mbedtls_sha256_update(&ctx, reinterpret_cast<const unsigned char*>("ab"), 2);
rc2 |= mbedtls_sha256_update(&ctx, reinterpret_cast<const unsigned char*>("c"), 1);
rc2 |= mbedtls_sha256_finish(&ctx, out);
mbedtls_sha256_free(&ctx);
check(rc2 == 0 && hex(out, 32) == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"the sha256 sub-module alone streams the same bytes to the same digest");

return failures == 0 ? 0 : 1;
}
2 changes: 1 addition & 1 deletion tools/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def render(module, header, what, types, functions, opaque, enums):
out.append("module;")
out.append(f"#include <{header}>")
out.append("")
out.append(f"export module mcpplibs.mwrap.mbedtls:{module};")
out.append(f"export module mcpplibs.mwrap.mbedtls.{module};")
out.append("")
for name in opaque:
out.append(f"export using ::{name};")
Expand Down
Loading