-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiver.c
More file actions
116 lines (94 loc) · 3.48 KB
/
receiver.c
File metadata and controls
116 lines (94 loc) · 3.48 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
108
109
110
111
112
113
114
115
116
#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;
uint8_t ciphertext[MAX_MESSAGE_SIZE];
uint8_t plaintext[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 computed_hash[HASH_SIZE];
char computed_hash_hex[HASH_SIZE * 2 + 1];
printf("=== RECEIVER: Secure Message Reception ===\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! Listening for incoming messages...\n");
printf("\n--- Backend Key Information ---\n");
printf("Encryption Key: %s\n", enc_key);
printf("Hash Key: %s\n", hash_key);
printf("-------------------------------\n\n");
// Attach to shared memory and semaphore
shmid = create_shared_memory();
semid = create_semaphore();
data = attach_shared_memory(shmid);
printf("Waiting for message...\n");
// Wait for data to be ready
while (data->ready == 0) {
sleep(1);
}
// Lock semaphore
semaphore_wait(semid);
// Read encrypted data
int len = data->message_length;
memcpy(ciphertext, data->message, len);
ciphertext[len] = '\0';
// Display received encrypted text
printf("\n--- Received Encrypted Text (Hex) ---\n");
printf("Ciphertext: ");
for (int i = 0; i < len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n-------------------------------------\n");
// Verify integrity
key_mixed_rolling_hash((char*)ciphertext, len, hash_key, computed_hash, HASH_SIZE);
to_hex(computed_hash, HASH_SIZE, computed_hash_hex);
printf("\n--- Hash Verification ---\n");
printf("Received Hash: %s\n", data->hash);
printf("Computed Hash: %s\n", computed_hash_hex);
printf("Match: %s\n", (strcmp(data->hash, computed_hash_hex) == 0) ? "YES" : "NO");
printf("------------------------\n");
if (strcmp(data->hash, computed_hash_hex) == 0) {
// Integrity check passed - decrypt and display message
hybrid_decrypt(ciphertext, plaintext, len, (uint8_t*)enc_key, strlen(enc_key));
plaintext[len] = '\0';
printf("\n--- Decrypted Text ---\n");
printf("Plaintext: %s\n", plaintext);
printf("----------------------\n");
printf("\nWow! You got your text:\n");
printf(">>> %s <<<\n", plaintext);
} else {
// Integrity check failed
printf("\nOops! Sorry, the text was tampered or the key entered is wrong!\n");
}
// Mark as consumed
data->ready = 0;
// Unlock semaphore
semaphore_signal(semid);
// Cleanup
detach_shared_memory(data);
printf("\n=== RECEIVER: Reception Complete ===\n");
return 0;
}