diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-27 18:38:35 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-27 18:38:35 +0200 |
| commit | d8c4d84dc75300c6d4d8b0adceafa33741960b92 (patch) | |
| tree | 00e2dfcc5c836d62fccff76c862e6ec3b0a74db8 /libs/openssl-3.6.0-beta1/projects/examples/src/cmac.cpp | |
| parent | fa088bb60692ba02d30d39affa9a31d9e2b688e2 (diff) | |
added http lib, working on AI invoice importing
Diffstat (limited to 'libs/openssl-3.6.0-beta1/projects/examples/src/cmac.cpp')
| -rw-r--r-- | libs/openssl-3.6.0-beta1/projects/examples/src/cmac.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libs/openssl-3.6.0-beta1/projects/examples/src/cmac.cpp b/libs/openssl-3.6.0-beta1/projects/examples/src/cmac.cpp new file mode 100644 index 0000000..9e8397c --- /dev/null +++ b/libs/openssl-3.6.0-beta1/projects/examples/src/cmac.cpp @@ -0,0 +1,57 @@ +/* Simple test program for libcrypto (OpenSSL) CMAC ([found on](https://gist.github.com/enkore/56c756d32197f65ae7769e7f9e0a5d35)) + */ + +import std; +#include <stdlib.h> + +#include <openssl/cmac.h> +#include <openssl/core_names.h> +#include <openssl/evp.h> + +template<auto fn> +using fn_constant = std::integral_constant<decltype(fn), fn>; +using cmac_ptr = std::unique_ptr<EVP_MAC, fn_constant<EVP_MAC_free>>; +using cmac_ctx_ptr = std::unique_ptr<EVP_MAC_CTX, fn_constant<EVP_MAC_CTX_free>>; + +int wmain() +{ + cmac_ptr cmac{EVP_MAC_fetch(nullptr, "CMAC", nullptr)}; + if (!cmac) { + return EXIT_FAILURE; + } + cmac_ctx_ptr ctx{EVP_MAC_CTX_new(cmac.get())}; + if (!cmac) { + return EXIT_FAILURE; + } + + /* The underlying cipher to be used */ + const EVP_CIPHER* cipher = EVP_aes_256_cbc(); + const char* cypherName = EVP_CIPHER_get0_name(cipher); + const int keyLength = EVP_CIPHER_get_key_length(cipher); + const OSSL_PARAM params[2] = {OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, const_cast<char*>(cypherName), 0), + OSSL_PARAM_construct_end()}; + + std::vector<unsigned char> key(keyLength, '\0'); + unsigned int msgLength = 1024 * 1024 * 1024; + std::vector<unsigned char> msg(msgLength, '\0'); + + auto t0 = std::chrono::steady_clock::now(); + if (!EVP_MAC_init(ctx.get(), key.data(), key.size(), params)) { + return EXIT_FAILURE; + } + if (!EVP_MAC_update(ctx.get(), msg.data(), msgLength)) { + return EXIT_FAILURE; + } + size_t resultLength = 0; + EVP_MAC_final(ctx.get(), nullptr, &resultLength, 0); + std::vector<unsigned char> result(resultLength, '\0'); + if (!EVP_MAC_final(ctx.get(), result.data(), &resultLength, resultLength)) { + return EXIT_FAILURE; + } + auto t1 = std::chrono::steady_clock::now(); + std::chrono::duration<double> tdelta = t1 - t0; + + std::wcout << std::format(L"CMAC-AES-256'd {} bytes in {} ({:.1f} MiB/s)", msgLength, tdelta, msgLength / tdelta.count() / 1024 / 1024); + + return 0; +} |
