Cerberus/x25519_handshake.cpp
2025-01-28 21:36:25 +03:00

39 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "x25519_handshake.hpp"
#include "config.hpp"
#include <cstring> // Для std::memcpy и std::memset
#include <iostream>
// Подключаем Monocypher
extern "C" {
#include "monocypher.h"
}
void x25519GenerateEphemeral(AppConfig &config) {
FILE* f = fopen("/dev/urandom", "rb");
if (!f) {
std::cerr << CLR_RED "[x25519] Не удалось открыть /dev/urandom\n" CLR_RESET;
return;
}
size_t read = fread(config.ephemeralSec, 1, 32, f);
fclose(f);
if (read != 32) {
std::cerr << CLR_RED "[x25519] Не удалось прочитать 32 байта из /dev/urandom\n" CLR_RESET;
return;
}
crypto_x25519_public_key(config.ephemeralPub, config.ephemeralSec);
std::memset(config.sharedSecret, 0, 32);
config.haveSharedSecret = false;
}
void x25519ComputeShared(AppConfig &config, const uint8_t otherPub[32]) {
uint8_t shared[32];
crypto_x25519(shared, config.ephemeralSec, otherPub);
std::memcpy(config.sharedSecret, shared, 32);
config.haveSharedSecret = true;
std::cout << CLR_GREEN "[x25519] Общий сеансовый ключ вычислен!\n" CLR_RESET;
}