summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sender.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/sender.c b/sender.c
index 3469735..19a3bed 100644
--- a/sender.c
+++ b/sender.c
@@ -27,7 +27,7 @@ static const unsigned char pubkey[] =
27static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH"; 27static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH";
28 28
29enum { SESSION_ID_LENGTH = 8, AES_KEY_LENGTH = 16, GCM_IV_LENGTH = 16, GCM_TAG_LENGTH = 16 }; 29enum { SESSION_ID_LENGTH = 8, AES_KEY_LENGTH = 16, GCM_IV_LENGTH = 16, GCM_TAG_LENGTH = 16 };
30static uint8_t session_id[8] = { 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01 }; 30static uint64_t session_id = 0x0123456789abcdef;
31static uint8_t aes_key[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; 31static uint8_t aes_key[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
32 32
33void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text, size_t len) { 33void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text, size_t len) {
@@ -37,14 +37,14 @@ void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text,
37 const size_t total_length = 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH + len; 37 const size_t total_length = 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH + len;
38 uint8_t *output = alloca(total_length); 38 uint8_t *output = alloca(total_length);
39 output[0] = 1; 39 output[0] = 1;
40 memcpy(output + 1, session_id, SESSION_ID_LENGTH); 40 memcpy(output + 1, (uint8_t*)&session_id, SESSION_ID_LENGTH);
41 memcpy(output + 1 + SESSION_ID_LENGTH, iv, GCM_IV_LENGTH); 41 memcpy(output + 1 + SESSION_ID_LENGTH, iv, GCM_IV_LENGTH);
42 42
43 mbedtls_gcm_context ctx; 43 mbedtls_gcm_context ctx;
44 mbedtls_gcm_init(&ctx); 44 mbedtls_gcm_init(&ctx);
45 mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH); 45 mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH);
46 46
47 if (!mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, len, iv, GCM_IV_LENGTH, session_id, SESSION_ID_LENGTH, 47 if (!mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, len, iv, GCM_IV_LENGTH, (uint8_t*)&session_id, SESSION_ID_LENGTH,
48 text, output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH, GCM_TAG_LENGTH, 48 text, output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH, GCM_TAG_LENGTH,
49 output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH)) { 49 output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH)) {
50 struct sockaddr_in to; 50 struct sockaddr_in to;
@@ -60,12 +60,12 @@ void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text,
60} 60}
61 61
62void new_session(int sock, mbedtls_ctr_drbg_context *ctr_drbg) { 62void new_session(int sock, mbedtls_ctr_drbg_context *ctr_drbg) {
63 mbedtls_ctr_drbg_random(ctr_drbg, session_id, sizeof(session_id)); 63 mbedtls_ctr_drbg_random(ctr_drbg, (uint8_t*)&session_id, sizeof(session_id));
64 mbedtls_ctr_drbg_random(ctr_drbg, aes_key, sizeof(aes_key)); 64 mbedtls_ctr_drbg_random(ctr_drbg, aes_key, sizeof(aes_key));
65 65
66 unsigned char output[512]; 66 unsigned char output[512];
67 output[0] = 0; 67 output[0] = 0;
68 memcpy(output + 1, session_id, SESSION_ID_LENGTH); 68 memcpy(output + 1, (uint8_t*)&session_id, SESSION_ID_LENGTH);
69 69
70 mbedtls_pk_context pk; 70 mbedtls_pk_context pk;
71 mbedtls_pk_init(&pk); 71 mbedtls_pk_init(&pk);
@@ -105,6 +105,8 @@ int main() {
105 105
106 const unsigned char *logline = (const unsigned char*)"Juchuuu, es klappt!\n"; 106 const unsigned char *logline = (const unsigned char*)"Juchuuu, es klappt!\n";
107 send_udp(sock, &ctr_drbg, logline, strlen((char*)logline)); 107 send_udp(sock, &ctr_drbg, logline, strlen((char*)logline));
108 send_udp(sock, &ctr_drbg, logline, strlen((char*)logline));
109 send_udp(sock, &ctr_drbg, logline, strlen((char*)logline));
108 110
109 close(sock); 111 close(sock);
110} 112}