|
| 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