-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsender.c
More file actions
107 lines (86 loc) · 3.13 KB
/
sender.c
File metadata and controls
107 lines (86 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "ipc.h"
#include "encryption.h"
#include "integrity.h"
#include "keygen.h"
int main() {
int shmid, semid;
struct shared_data *data;
char plaintext[MAX_MESSAGE_SIZE];
uint8_t ciphertext[MAX_MESSAGE_SIZE];
char master_key[MASTER_KEY_LEN + 1];
char enc_key[DERIVED_KEY_LEN + 1];
char hash_key[DERIVED_KEY_LEN + 1];
unsigned char hash[HASH_SIZE];
printf("=== SENDER: Secure Message Transmission ===\n\n");
// Get master key from user
printf("Enter master key: ");
fgets(master_key, sizeof(master_key), stdin);
master_key[strcspn(master_key, "\n")] = 0; // Remove newline
// Pad or truncate to exact length
int key_len = strlen(master_key);
if (key_len < MASTER_KEY_LEN) {
// Pad with zeros if too short
for (int i = key_len; i < MASTER_KEY_LEN; i++) {
master_key[i] = '0';
}
master_key[MASTER_KEY_LEN] = '\0';
} else if (key_len > MASTER_KEY_LEN) {
// Truncate if too long
master_key[MASTER_KEY_LEN] = '\0';
}
// Derive encryption and hashing keys from master key
derive_keys(master_key, enc_key, hash_key);
printf("Key received! Preparing secure channel...\n");
printf("\n--- Backend Key Information ---\n");
printf("Encryption Key: %s\n", enc_key);
printf("Hash Key: %s\n", hash_key);
printf("-------------------------------\n\n");
// Create or get shared memory and semaphore
shmid = create_shared_memory();
semid = create_semaphore();
data = attach_shared_memory(shmid);
// Get message from user
printf("Enter message to send: ");
fgets(plaintext, MAX_MESSAGE_SIZE, stdin);
plaintext[strcspn(plaintext, "\n")] = 0; // Remove newline
int len = strlen(plaintext);
// Encrypt the message
hybrid_encrypt((uint8_t*)plaintext, ciphertext, len, (uint8_t*)enc_key, strlen(enc_key));
// Display encrypted text in hex format
printf("\n--- Encrypted Text (Hex) ---\n");
printf("Plaintext: %s\n", plaintext);
printf("Ciphertext: ");
for (int i = 0; i < len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n----------------------------\n");
// Generate hash for integrity verification
key_mixed_rolling_hash((char*)ciphertext, len, hash_key, hash, HASH_SIZE);
to_hex(hash, HASH_SIZE, data->hash);
printf("\n--- Integrity Hash (Hex) ---\n");
printf("Generated Hash: %s\n", data->hash);
printf("----------------------------\n\n");
// Wait for semaphore (lock)
printf("Encrypting and sending message...\n");
semaphore_wait(semid);
// Write to shared memory
memcpy(data->message, ciphertext, len);
data->message_length = len;
data->ready = 1;
// Signal semaphore (unlock)
semaphore_signal(semid);
// Wait for receiver to consume the message
printf("Waiting for receiver...\n");
while (data->ready == 1) {
sleep(1);
}
printf("\nMessage delivered successfully!\n");
// Cleanup
detach_shared_memory(data);
printf("\n=== SENDER: Transmission Complete ===\n");
return 0;
}