39 lines
1.2 KiB
C++
39 lines
1.2 KiB
C++
#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;
|
||
}
|