18 #define prompt_return(msg) {util::prompt(msg); return;}
20 #define prompt_break(msg) {util::prompt(msg); break;}
22 #define prompt_continue(msg) {util::prompt(msg); continue;}
29 inline void clear() {std::cout <<
"\033[2J\033[1;1H";}
42 template <
typename T =
bool>
inline T
input(
const std::string& title,
const T& error_ret = T()) {
46 std::cout << title << std::endl;
50 auto f = std::cin.fail();
57 std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
69 inline void prompt(
const std::string& message) {
70 std::cout << message << std::endl;
71 std::cout <<
"Press Enter to Continue" << std::endl;
89 std::cout <<
"Exchanging Keys..." << std::endl;
90 for (
size_t x = 0; x < 4; ++x) {
94 prompt(
"Complete! Ensure that the Shared Key matches!");
104 std::cout <<
"Receiving Key Size..." << std::endl;
107 std::cout <<
"Receiving Ciphertext..." << std::endl;
110 std::cout <<
"Receiving Nonce..." << std::endl;
114 auto str = std::string(&nonce_packet.data[0], PACKET_SIZE);
116 std::istringstream (str) >> nonce;
119 if (nonce_packet.m == network::meta::IV) {
121 std::cout <<
"Message: " <<
aes::gcm::Dec(message, sk, Nr, nonce) << std::endl;
123 catch (std::runtime_error& e) {prompt_return(e.what());}
127 std::cout <<
"Receiving HMAC..." << std::endl;
132 prompt_return(
"HMAC does not match! Message has been altered!");
135 if (nonce_packet.m == network::meta::NONCE) {
136 std::cout <<
"Message: " <<
aes::Ctr(message, sk, Nr, nonce) << std::endl;
139 else if (nonce_packet.m == network::meta::EMPTY) {
140 std::cout <<
"Message: " <<
aes::InvCipher(message, sk, Nr) << std::endl;
144 else prompt_return(
"Peer sent invalid packet!");
146 std::cout <<
"Press Enter to Continue" << std::endl;
158 std::cout <<
"Enter the message:" << std::endl;
159 std::getline(std::cin, message);
162 auto size =
input<int>(
"What size key?\n1. 128\n2. 192\n3. 256\n", -1);
163 if (size < 1 || size > 3) prompt_return(
"Invalid selection");
164 uint64_t Nr = size == 1 ? 10 : size == 2 ? 12 : 14;
167 auto option =
input<int>(
"What mode?\n1. ECB\n2. CTR\n3. GCM", -1);
168 if (option < 1 || option > 3) prompt_return(
"Invalid selection");
185 std::cout <<
"Reaching out to the Peer..." << std::endl;
187 prompt_return(
"Failed to communicate with peer!");
191 switch (response.m) {
192 case network::meta::ACK:
break;
193 case network::meta::REFUSED: prompt_return(
"Peer refused to accept message!");
194 case network::meta::ERROR: prompt_return(
"Could not communicate with peer!");
195 case network::meta::MESSAGE: prompt_return(
"Cannot send two messages at once! One peer must Listen!");
196 default: prompt_return(
"Peer sent invalid response!");
200 const uint64_t nonce = std::rand();
203 auto cipher = option == 1 ?
206 aes::gcm::Enc(message, sk, Nr, nonce);
209 prompt_return(
"Failed to send Key Size!");
213 prompt_return(
"Failed to send ciphertext!");
218 prompt_return(
"Failed to send empty packet!");
222 else if (option == 2) {
224 prompt_return(
"Failed to send nonce!");
230 prompt_return(
"Failed to send IV!");
237 prompt_return(
"Failed to send HMAC!");
252 if (response ==
"y" || response ==
"Y") {
std::string Dec(const std::string &in, const std::array< uint64_t, 4 > &k, const uint64_t Nr, uint64_t nonce)
Decrypt a message with AES-GCM.
Definition aes.h:1049
std::string InvCipher(const std::string &in, const std::array< uint64_t, 4 > &k, const uint64_t &Nr)
Decrypt a message with AES.
Definition aes.h:744
std::string Ctr(const std::string &in, const std::array< uint64_t, 4 > &k, const uint64_t Nr, uint64_t nonce)
An implementation of AES in CTR mode.
Definition aes.h:776
std::string Cipher(const std::string &in, const std::array< uint64_t, 4 > &k, const uint64_t &Nr)
Encrypt a message with AES.
Definition aes.h:716
uint64_t exchange_keys(const bool &server)
Exchange keys on an established connection.
Definition exchange.h:65
This namespace includes the functions needed to generate an HMAC value Using OpenSSL.
Definition hmac.h:14
std::string generate(const std::string &message, const std::array< uint64_t, 4 > &key, const size_t &rounds)
Generate an HMAC for a message.
Definition hmac.h:30
int send_packet(const packet &p, const size_t &timeout=5)
Send a packet.
Definition network.h:68
std::string recv_string(const size_t &timeout=5)
Receive a string.
Definition network.h:188
int send_value(const T &value, const network::meta &type=DATA, const size_t &timeout=5)
Send a value.
Definition network.h:109
int send_string(const std::string &message, const network::meta &type=DATA, const size_t &timeout=5)
Send a string of any size.
Definition network.h:153
const T recv_value(const size_t &timeout=5)
Receive a value.
Definition network.h:131
packet recv_packet(const size_t &timeout=5)
Receive a packet.
Definition network.h:87
The utility namespace.
Definition util.h:16
void construct_shared_key(std::array< uint64_t, 4 > &sk, const bool &server)
Genreate a shared key over a connection.
Definition util.h:88
void receive_message(const std::array< uint64_t, 4 > &sk)
Receive an encrypted message from the peer.
Definition util.h:102
void prompt(const std::string &message)
Prompt the user and wait until they have confirmed it.
Definition util.h:69
bool acknowledge(const std::string &what)
Acknowledge a request from a peer.
Definition util.h:247
void send_message(const std::array< uint64_t, 4 > &sk)
Send an encrypted message to a peer.
Definition util.h:155
T input(const std::string &title, const T &error_ret=T())
std::cin can be a little difficult to use, particularly handling bad input. This sanitized it.
Definition util.h:42
void clear()
Clear the screen.
Definition util.h:29