-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchall.c
More file actions
255 lines (217 loc) · 6.16 KB
/
Copy pathchall.c
File metadata and controls
255 lines (217 loc) · 6.16 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include "aes-gcm.h"
#define NUM_TEXTS 0x10
#define BUF_SZ 0x178
typedef struct {
uint8_t buf[BUF_SZ];
size_t len;
} Text;
_Static_assert(sizeof(Text) % 0x10 == 0, "Text size");
Text *texts[NUM_TEXTS] = {0};
int setup(){
setvbuf(stdin,NULL,_IONBF,0);
setvbuf(stdout,NULL,_IONBF,0);
}
void menu(){
printf("1. Create Text\n2. Read Text\n3. Encrypt Text\n4. Decrypt Text\n5. Delete Text\n6. Exit\n");
}
int get_num(){
int result;
scanf("%d", &result);
while (getchar() != '\n');
return result;
}
static inline int get_text_idx(){
int result = get_num();
if (result < 0 || result >= NUM_TEXTS)
exit(1);
return result;
}
void print_hex(const uint8_t *buf, size_t len) {
for (size_t i = 0; i < len; i++) {
printf("%02x", buf[i]);
}
}
static uint8_t hex_to_byte(uint8_t high, uint8_t low) {
int hi = isdigit(high) ? high - '0' : toupper(high) - 'A' + 10;
int lo = isdigit(low) ? low - '0' : toupper(low) - 'A' + 10;
return (uint8_t)((hi << 4) | lo);
}
uint8_t *read_hex_as_bytes(size_t *out_len) {
uint8_t *hex = NULL;
size_t bufsize = 0;
if (getline(&hex, &bufsize, stdin) == -1) {
free(hex);
return NULL;
}
// Strip newline
hex[strcspn(hex, "\n")] = '\0';
size_t hex_len = strlen(hex);
// Hex length must be even
if (hex_len % 2 != 0) {
memset(hex, 0, bufsize);
free(hex);
return NULL;
}
size_t bytes_len = hex_len / 2;
uint8_t *bytes = malloc(bytes_len);
if (!bytes) {
memset(hex, 0, bufsize);
free(hex);
return NULL;
}
// Convert hex → bytes
for (size_t i = 0; i < bytes_len; i++) {
uint8_t h = hex[2*i];
uint8_t l = hex[2*i + 1];
if (!isxdigit(h) || !isxdigit(l)) {
memset(hex, 0, bufsize);
memset(bytes, 0, bytes_len);
free(hex);
free(bytes);
return NULL;
}
bytes[i] = hex_to_byte(h, l);
}
memset(hex, 0, bufsize);
free(hex);
*out_len = bytes_len;
return bytes;
}
void encrypt_text(uint8_t *key, Text *data){
uint8_t* dat = data;
for (size_t i = 0; i < sizeof(Text); i+=0x10)
{
aes_encrypt(key, dat + i, dat + i);
}
}
void decrypt_text(uint8_t *key, Text *data){
uint8_t* dat = data;
for (size_t i = 0; i < sizeof(Text); i+=0x10)
{
aes_decrypt(key, dat + i, dat + i);
}
}
int main(){
setup();
uint8_t *key = malloc(AES_BLOCK_SIZE);
int randfd = open("/dev/urandom", O_RDONLY);
if (randfd == -1){
perror("Error getting random: ");
exit(1);
}
int err = read(randfd, key, 16);
if (err == -1){
perror("Error getting random: ");
exit(1);
}
int repeat = 1;
int res;
size_t idx, sz;
uint8_t iv[AES_BLOCK_SIZE], tag[AES_BLOCK_SIZE];
uint8_t *ct;
while (repeat)
{
menu();
printf("> ");
int choice = get_num();
switch (choice)
{
case 1:
printf("Index: ");
idx = get_text_idx();
if (texts[idx] != NULL){
printf("That index has been used\n");
break;
}
texts[idx] = malloc(sizeof(Text));
texts[idx]->len = read(STDIN_FILENO, texts[idx]->buf, BUF_SZ);
encrypt_text(key, texts[idx]);
break;
case 2:
printf("Index: ");
idx = get_text_idx();
if (texts[idx] == NULL){
printf("That index is empty\n");
break;
}
printf("Text: ");
decrypt_text(key, texts[idx]);
write(STDOUT_FILENO, texts[idx]->buf, texts[idx]->len);
encrypt_text(key, texts[idx]);
printf("\n");
break;
case 3:
printf("Index: ");
idx = get_text_idx();
if (texts[idx] == NULL){
printf("That index is empty\n");
break;
}
decrypt_text(key, texts[idx]);
printf("Ciphertext: ");
ct = malloc(texts[idx]->len);
read(randfd, iv, AES_BLOCK_SIZE);
aes_aead_ae(key, AES_BLOCK_SIZE, iv, AES_BLOCK_SIZE, texts[idx]->buf, texts[idx]->len, "", 0, ct, tag);
print_hex(tag, AES_BLOCK_SIZE);
print_hex(iv, AES_BLOCK_SIZE);
print_hex(ct, texts[idx]->len);
printf("\n");
free(ct);
encrypt_text(key, texts[idx]);
break;
case 4:
printf("Index: ");
idx = get_text_idx();
if (texts[idx] != NULL){
printf("That index has been used\n");
break;
}
printf("Ciphertext: ");
ct = read_hex_as_bytes(&sz);
if (ct == NULL || sz <= 32){
printf("Error\n");
if (ct != NULL)
free(ct);
break;
}
texts[idx] = malloc(sizeof(Text));
texts[idx]->len = sz - (AES_BLOCK_SIZE * 2);
res = aes_aead_ad(key, AES_BLOCK_SIZE, ct + AES_BLOCK_SIZE, AES_BLOCK_SIZE, ct + (AES_BLOCK_SIZE * 2), texts[idx]->len, "", 0, ct, texts[idx]->buf);
if (res != 0) {
printf("Invalid ciphertext\n");
memset(ct, 0, sz);
free(texts[idx]);
texts[idx] = NULL;
break;
}
memset(ct, 0, sz);
free(ct);
encrypt_text(key, texts[idx]);
break;
case 5:
printf("Index: ");
idx = get_text_idx();
if (texts[idx] == NULL){
printf("That index is empty\n");
break;
}
free(texts[idx]);
texts[idx] = NULL;
break;
case 6:
repeat = 0;
break;
default:
printf("That is not an option\n");
break;
}
}
}