35 lines
923 B
C++
35 lines
923 B
C++
#include "x25519_handshake.hpp"
|
||
#include "config.hpp"
|
||
|
||
extern "C" {
|
||
#include "libs/monocypher.h"
|
||
}
|
||
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <iostream>
|
||
|
||
void x25519GenerateEphemeral(AppConfig &config) {
|
||
FILE* f = fopen("/dev/urandom", "rb");
|
||
if (!f) {
|
||
std::cerr << "[x25519] Не удалось открыть /dev/urandom\n";
|
||
return;
|
||
}
|
||
fread(config.ephemeralSec, 1, 32, f);
|
||
fclose(f);
|
||
|
||
crypto_x25519_public_key(config.ephemeralPub, config.ephemeralSec);
|
||
|
||
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);
|
||
memcpy(config.sharedSecret, shared, 32);
|
||
|
||
config.haveSharedSecret = true;
|
||
std::cout << "[x25519] Получен общий сеансовый ключ (32 байта).\n";
|
||
}
|