Nostro/implementations/Nostro-Crypto.cpp

131 lines
4.5 KiB
C++
Raw Permalink Normal View History

2026-11-01 18:35:23 +01:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
2026-10-06 15:39:17 +02:00
module;
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
2026-11-01 18:35:23 +01:00
#include <openssl/rand.h>
2026-10-06 15:39:17 +02:00
module Nostro:Crypto_impl;
import :Crypto;
import std;
using namespace Nostro;
2026-11-01 18:35:23 +01:00
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) {
2026-10-06 15:39:17 +02:00
if(status <= 0) {
ERR_print_errors_fp(stderr);
throw std::runtime_error("openssl error");
}
return status;
}
2026-11-01 18:35:23 +01:00
Crypto::Key Crypto::OwnKey(EVP_PKEY* key) {
return Key(key, EVP_PKEY_free);
}
2026-10-06 15:39:17 +02:00
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);
2026-11-01 18:35:23 +01:00
ThrowOnOpenssl(EVP_DigestSignFinal(ctx, reinterpret_cast<std::uint8_t*>(sig.data()), &sigLen));
2026-10-06 15:39:17 +02:00
sig.resize(sigLen);
std::string sig64;
sig64.resize(4 * ((sig.size() + 2) / 3));
2026-11-01 18:35:23 +01:00
const std::int32_t n = EVP_EncodeBlock(reinterpret_cast<unsigned char*>(sig64.data()), reinterpret_cast<const unsigned char*>(sig.data()), static_cast<int>(sig.size()));
2026-10-06 15:39:17 +02:00
sig64.resize(n);
EVP_MD_CTX_free(ctx);
return sig64;
}
2026-11-01 18:35:23 +01:00
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<unsigned char*>(decoded.data()), reinterpret_cast<const unsigned char*>(signature.data()), static_cast<int>(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<std::size_t>(n) - padding);
2026-10-06 15:39:17 +02:00
2026-11-01 18:35:23 +01:00
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<const unsigned char*>(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;
}
2026-10-06 15:39:17 +02:00
2026-11-01 18:35:23 +01:00
std::tuple<EVP_PKEY*, std::string> Crypto::CreateKey() {
EVP_PKEY* pkey = ThrowOnOpenssl(EVP_RSA_gen(2048));
return {pkey, PublicKeyToPem(pkey)};
2026-10-06 15:39:17 +02:00
}
EVP_PKEY* Crypto::LoadKeyFromPem(std::string_view pem) {
BIO* bio = ThrowOnOpenssl(BIO_new_mem_buf(pem.data(), static_cast<int>(pem.size())));
EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
BIO_free(bio);
return ThrowOnOpenssl(pkey);
2026-11-01 18:35:23 +01:00
}
EVP_PKEY* Crypto::LoadPublicKeyFromPem(std::string_view pem) {
BIO* bio = ThrowOnOpenssl(BIO_new_mem_buf(pem.data(), static_cast<int>(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<unsigned char> random(bytes);
ThrowOnOpenssl(RAND_bytes(random.data(), static_cast<std::int32_t>(random.size())));
std::string out;
for(const unsigned char byte : random) {
out += std::format("{:02x}", byte);
}
return out;
}