// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module; #include #include #include #include #include module Nostro:Crypto_impl; import :Crypto; import std; using namespace Nostro; namespace { std::string ReadBio(BIO* bio) { // BIO_get_mem_data hands out its buffer through a char*. // lint-disable-next-line no-char-pointer char* data; const std::int64_t len = Crypto::ThrowOnOpenssl(BIO_get_mem_data(bio, &data)); std::string out(data, len); BIO_free(bio); return out; } } std::int64_t Crypto::ThrowOnOpenssl(std::int64_t status) { if(status <= 0) { ERR_print_errors_fp(stderr); throw std::runtime_error("openssl error"); } return status; } Crypto::Key Crypto::OwnKey(EVP_PKEY* key) { return Key(key, EVP_PKEY_free); } std::string Crypto::SignBody(EVP_PKEY* privateKey, std::string_view body) { EVP_MD_CTX* ctx = ThrowOnOpenssl(EVP_MD_CTX_new()); ThrowOnOpenssl(EVP_DigestSignInit(ctx, nullptr, EVP_sha256(), nullptr, privateKey)); ThrowOnOpenssl(EVP_DigestSignUpdate(ctx, body.data(), body.length())); size_t sigLen = 0; ThrowOnOpenssl(EVP_DigestSignFinal(ctx, nullptr, &sigLen)); std::string sig; sig.resize(sigLen); ThrowOnOpenssl(EVP_DigestSignFinal(ctx, reinterpret_cast(sig.data()), &sigLen)); sig.resize(sigLen); std::string sig64; sig64.resize(4 * ((sig.size() + 2) / 3)); const std::int32_t n = EVP_EncodeBlock(reinterpret_cast(sig64.data()), reinterpret_cast(sig.data()), static_cast(sig.size())); sig64.resize(n); EVP_MD_CTX_free(ctx); return sig64; } bool Crypto::VerifyBody(EVP_PKEY* publicKey, std::string_view body, std::string_view signature) { // EVP_DecodeBlock keeps the bytes the '=' padding stands for, as zeros: count the padding and drop them. if(signature.empty() || signature.size() % 4 != 0) { return false; } std::string decoded(3 * (signature.size() / 4), '\0'); const int n = EVP_DecodeBlock(reinterpret_cast(decoded.data()), reinterpret_cast(signature.data()), static_cast(signature.size())); if(n < 0) { return false; } std::size_t padding = 0; if(signature.ends_with("==")) { padding = 2; } else if(signature.ends_with('=')) { padding = 1; } decoded.resize(static_cast(n) - padding); EVP_MD_CTX* ctx = ThrowOnOpenssl(EVP_MD_CTX_new()); const bool valid = EVP_DigestVerifyInit(ctx, nullptr, EVP_sha256(), nullptr, publicKey) == 1 && EVP_DigestVerifyUpdate(ctx, body.data(), body.length()) == 1 && EVP_DigestVerifyFinal(ctx, reinterpret_cast(decoded.data()), decoded.size()) == 1; EVP_MD_CTX_free(ctx); // A failed verification leaves its reason on OpenSSL's error queue; it is not an error of the next call. ERR_clear_error(); return valid; } std::tuple Crypto::CreateKey() { EVP_PKEY* pkey = ThrowOnOpenssl(EVP_RSA_gen(2048)); return {pkey, PublicKeyToPem(pkey)}; } EVP_PKEY* Crypto::LoadKeyFromPem(std::string_view pem) { BIO* bio = ThrowOnOpenssl(BIO_new_mem_buf(pem.data(), static_cast(pem.size()))); EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr); BIO_free(bio); return ThrowOnOpenssl(pkey); } EVP_PKEY* Crypto::LoadPublicKeyFromPem(std::string_view pem) { BIO* bio = ThrowOnOpenssl(BIO_new_mem_buf(pem.data(), static_cast(pem.size()))); EVP_PKEY* pkey = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr); BIO_free(bio); return ThrowOnOpenssl(pkey); } std::string Crypto::PrivateKeyToPem(EVP_PKEY* key) { BIO* bio = ThrowOnOpenssl(BIO_new(BIO_s_mem())); ThrowOnOpenssl(PEM_write_bio_PrivateKey(bio, key, nullptr, nullptr, 0, nullptr, nullptr)); return ReadBio(bio); } std::string Crypto::PublicKeyToPem(EVP_PKEY* key) { BIO* bio = ThrowOnOpenssl(BIO_new(BIO_s_mem())); ThrowOnOpenssl(PEM_write_bio_PUBKEY(bio, key)); return ReadBio(bio); } std::string Crypto::RandomHex(std::size_t bytes) { std::vector random(bytes); ThrowOnOpenssl(RAND_bytes(random.data(), static_cast(random.size()))); std::string out; for(const unsigned char byte : random) { out += std::format("{:02x}", byte); } return out; }