Commit a26a5d5e authored by Grzegorz Adam Hankiewicz's avatar Grzegorz Adam Hankiewicz
Browse files

Implements decryption.

parent e91b34b6
......@@ -47,9 +47,11 @@ Client_message* gen_broadcast_pos(Client_state* state, float lat, float lon)
{
sprintf(g_message.plaintext_json,
"{'a': 'pos', 'lat': %0.6f, 'lon': %0.6f}", lat, lon);
unsigned long long len = strlen(g_message.plaintext_json);
g_message.timestamp = state->current_time;
/* The +1 is to include the string terminating null byte. */
unsigned long long len = strlen(g_message.plaintext_json) + 1;
g_message.ciphertext_length = len + crypto_secretbox_MACBYTES;
g_message.timestamp = state->current_time;
g_message.from_chat_id = state->chat_id;
/* To encrypt the payload we need a nonce from the client's state. */
unsigned char nonce[crypto_secretbox_NONCEBYTES];
......@@ -73,4 +75,29 @@ Client_message* gen_broadcast_pos(Client_state* state, float lat, float lon)
return &g_message;
}
/**
* Using only the ciphertext, length, timestamp, and chat id, tries to decript
* the contents and print them to stdout.
*
* Of course the plaintext is there, but we are trusting ourselves, aren't we?
*/
void decrypt_message(Client_message* message, unsigned char* encryption_key)
{
/* Rebuild the nonce from the sender identity and message time. */
unsigned char nonce[crypto_secretbox_NONCEBYTES];
gen_nonce(nonce, message->timestamp, message->from_chat_id);
unsigned char decrypted[MAX_BUF];
if (0 != crypto_secretbox_open_easy(decrypted, message->ciphertext_json,
message->ciphertext_length, nonce, encryption_key)) {
printf("\n\n\tOoops, couldn't decrypt the message!\n");
return;
}
/* We encrypt including the string null terminator, so just printf it. */
printf("Client decrypted '%s'\n", decrypted);
}
/* vim: set ts=4 sw=4 tw=0 noet :*/
......@@ -12,15 +12,17 @@ typedef struct Client_state {
} Client_state;
typedef struct Client_message {
char plaintext_json[MAX_BUF];
unsigned char ciphertext_json[MAX_BUF];
unsigned long ciphertext_length;
long timestamp;
char plaintext_json[MAX_BUF]; /* The stored plaintext version. */
unsigned char ciphertext_json[MAX_BUF]; /* The encrypted version. */
unsigned long ciphertext_length; /* Length of the binary cipher bytes. */
int from_chat_id; /* The chat id of the message originator. */
long timestamp; /* The time at which the message was constructed. */
} Client_message;
void gen_random_session_id(unsigned char* buf);
void gen_random_encryption_key(unsigned char* buf);
Client_message* gen_broadcast_pos(Client_state* state, float lat, float lon);
void decrypt_message(Client_message* message, unsigned char* encryption_key);
#endif /* _CLIENT_H_ */
......
......@@ -64,6 +64,8 @@ int main(void)
ciphertext_to_base64(m->ciphertext_json, m->ciphertext_length),
m->timestamp, c1.chat_id);
decrypt_message(m, c2.encryption_key);
printf("secretbox bytes %d\n", crypto_secretbox_KEYBYTES);
printf("secretbox nonce bytes %d\n", crypto_secretbox_NONCEBYTES);
printf("secretbox mac bytes %d\n", crypto_secretbox_MACBYTES);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment