-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt_custom_input.c
94 lines (77 loc) · 2.66 KB
/
encrypt_custom_input.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chacha20_functions_v128.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result" // To ignore the fgets warning for not usign its output
void encrypt_custom_input(char *ciphertext)
{
uint32_t state[16];
uint32_t v0[4];
uint32_t v1[4];
uint32_t v2[4];
uint32_t v3[4];
uint8_t key[32];
char key_char[33]; // To accomodate the null terminator
uint8_t nonce[12];
char nonce_char[13];
char input[256];
printf("\nInsert 256-bit Key (32 characters):\n");
// Read key as a string
fgets(key_char, sizeof(key_char), stdin);
int key_char_len = strlen(key_char);
// Convert each character of the string to hex and load it to the key array
for (size_t i = 0; i < key_char_len; i++)
{
key[i] = (uint8_t)key_char[i];
}
/* TEST
for (size_t i = 0; i < key_char_len; i++) {
printf("%02X ", key[i]);
}
printf("\n");*/
// Clear the input buffer to erase the newline character
int c;
while ((c = getchar()) != '\n' && c != EOF);
// Same operation for the nonce
printf("\nInsert 96-bit Nonce (12 characters):\n");
fgets(nonce_char, sizeof(nonce_char), stdin);
int nonce_char_len = strlen(nonce_char);
for (size_t i = 0; i < nonce_char_len; i++)
{
nonce[i] = (uint8_t)nonce_char[i];
}
/* TEST
for (size_t i = 0; i < nonce_char_len; i++) {
printf("%02X ", nonce[i]);
}
printf("\n");*/
/* TEST (hardcoded key and nonce)
uint8_t key[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
uint8_t nonce[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a,
0x00, 0x00, 0x00, 0x00
};*/
int d;
while ((d = getchar()) != '\n' && d != EOF);
printf("\nInsert Plaintext:\n");
fgets(input, sizeof(input), stdin);
int plaintext_len = strlen(input) - 1; // -1 to eliminate the null terminator from the input string
/* TEST
for (size_t i = 0; i < plaintext_len; i++) {
printf("%02X ", input[i]);
}
printf("\n");*/
encrypt_v128(state, "expand 32-byte k", key, 0, nonce, v0, v1, v2, v3, input, ciphertext, plaintext_len);
printf("\nCiphertext (in hex):\n");
for (size_t j = 0; j < plaintext_len; j++) {
printf("%02X", (unsigned char)ciphertext[j]);
}
printf("\n");
}
#pragma GCC diagnostic pop