pre steak commit

This commit is contained in:
malloc 2018-03-27 17:03:59 -05:00
parent a27bb453f9
commit 030e33f483
7 changed files with 98 additions and 4 deletions

View file

@ -1,13 +1,19 @@
#ifndef SOSC_CRYPTO_CIPHER_H
#define SOSC_CRYPTO_CIPHER_H
#include "../utils/bigint.hpp"
namespace sosc {
namespace cgc {
class Cipher {
public:
private:
const std::string sigma = "expand 32-byte k";
const std::string tau = "expand 16-byte k";
std::string key;
std::string state;
};
}}

View file

@ -1,2 +1,52 @@
#include "keyex.hpp"
sosc::cgc::KeyExchange::KeyExchange() {
if(KeyExchange::secret.IsZero())
KeyExchange::secret =
BigUInt::GenerateRandomPrime(this->key_size_bytes);
this->modulus = BigUInt::GenerateRandomPrime(this->key_size_bytes);
}
sosc::Packet sosc::cgc::KeyExchange::GenerateRequest() const {
return Packet(1, {
this->generator.ToString(),
this->modulus.ToString(),
BigUInt::ModPow(this->generator, this->secret, this->modulus);
});
}
bool sosc::cgc::KeyExchange::ParseRequest
(const Packet& request, Packet* response)
{
if(request.GetId() != 1 || request.RegionCount() != 3)
return false;
BigUInt generator, modulus, public_key;
bool check = generator.Parse(request[0]);
check = check || modulus.Parse(request[1]);
check = check || public_key.Parse(request[2]);
if(!check)
return false;
this->private_key = BigUInt::ModPow(public_key, this->secret, modulus);
public_key = BigUInt::ModPow(generator, this->secret, modulus);
*response = Packet(1, { public_key.ToString() });
return true;
}
bool sosc::cgc::KeyExchange::ParseResponse(const Packet& response) {
if(response.GetId() != 1 || response.RegionCount() != 1)
return false;
BigUInt public_key;
if(!public_key.Parse(response[0]))
return false;
this->private_key =
BigUInt::ModPow(public_key, this->secret, this->modulus);
return true;
}

View file

@ -1,15 +1,35 @@
#ifndef SOSC_CRYPTO_KEYEX_H
#define SOSC_CRYPTO_KEYEX_H
#include "../utils/bigint.hpp"
#include "../sock/packet.hpp"
namespace sosc {
namespace cgc {
class KeyExchange {
public:
const int key_size = 512;
const int key_size_bytes = key_size / 8;
const int generator = 2;
private:
KeyExchange();
Packet GenerateRequest() const;
bool ParseRequest(const Packet& request, Packet* response);
bool ParseResponse(const Packet& response);
inline bool Succeeded() {
return !this->private_key.IsZero();
}
inline const BigUInt& GetPrivateKey() {
return this->private_key;
}
private:
const int key_size = 256;
const int key_size_bytes = key_size / 8;
const BigUInt generator = BigUInt(2u);
static BigUInt secret;
BigUInt modulus;
BigUInt private_key;
};
}}

View file

@ -1,5 +1,14 @@
#include "packet.hpp"
sosc::Packet::Packet() {
this->id = 0;
}
sosc::Packet::Packet(uint8_t id, std::vector<std::string> data) {
this->SetId(id);
this->regions = data;
}
bool sosc::Packet::AddRegion(std::string data) {
if(this->regions.size() >= 256)
return false;

View file

@ -18,6 +18,9 @@
namespace sosc {
class Packet {
public:
Packet();
Packet(uint8_t id, std::vector<std::string> data);
bool AddRegion(std::string data);
bool AddRegions(std::vector<std::string> data);

View file

@ -437,6 +437,11 @@ sosc::BigUInt sosc::BigUInt::operator << (const uint64_t& rhs) const {
return shifted;
}
std::string sosc::BigUInt::ToRawString(uint64_t byte_count) const {
std::string raw(byte_count == 0 ? this->UsedByteCount() : byte_count, 0);
}
std::string sosc::BigUInt::ToString() const {
std::stringstream stream;
for(size_t i = this->WordCount() - 1;; --i) {

View file

@ -82,6 +82,7 @@ public:
BigUInt operator >> (const uint64_t& rhs) const;
BigUInt operator << (const uint64_t& rhs) const;
std::string ToRawString(uint64_t byte_count = 0) const;
std::string ToString() const;
inline operator std::string () const {
return this->ToString();