-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
198 lines (174 loc) · 6.48 KB
/
main.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
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
#include <stdio.h>
#include <unistd.h>
#include "aes256.h"
#define MAX_MSG_LGTH 1000000000 //1 GB
void str_to_bytes(char *str, uint8_t *bytes){
int str_len = strlen(str);
int i;
for(i = 0; i < str_len; i++){
bytes[i] = str[i];
}
}
char* bytes_to_hexstr(uint8_t *bytes, uint32_t length){
static char buffer[(uint32_t)MAX_MSG_LGTH * 2];
memset(buffer, 0, strlen(buffer));
int i;
for(i = 0; i < length; i++){
sprintf(buffer + strlen(buffer), "%02x", bytes[i]);
if((i + 1) % 16 == 0){
sprintf(buffer + strlen(buffer),"\n");
}
}
if(length % 16 != 0)
sprintf(buffer + strlen(buffer),"\n");
return buffer;
}
char* bytes_to_str(uint8_t *bytes, uint32_t length){
static char buffer[(uint32_t)MAX_MSG_LGTH];
memset(buffer, 0, strlen(buffer));
int i;
for(i = 0; i < length; i++){
if(bytes[i] == 0 || 32 <= bytes[i] && bytes[i] <= 126)
sprintf(buffer + strlen(buffer), "%c", bytes[i]);
else
sprintf(buffer + strlen(buffer), "%c", '_');
}
return buffer;
}
void parse_02X(uint8_t *key, char *arg, int bytes){
int iter;
for(iter = 0; iter < bytes; iter ++){
uint8_t tempbit;
char temp[2] = "";
temp[0] = (char*)arg[iter*2];
temp[1] = (char*)arg[iter*2+1];
sscanf(temp, "%02hhX", &tempbit);
key[iter] = tempbit;
}
}
int main(int argc, char *argv[]){
uint8_t key[32] = {
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 proofdata[16] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
};
uint8_t iv[16] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};
bool proof = false;
char * message = "";
if(strcmp(argv[1], "-ecb") == 0){
if(argc == 4){
message = argv[2];
char *keystring = argv[3];
if(strlen(keystring) != 64){
printf("key must be 32 bytes\n");
exit(-1);
}
parse_02X(key,keystring,32);
}else{
printf("Usage:\n%s [Message to encrypt] [Key in 02x hex]\n",argv[0],argv[0]);
exit(-1);
}
uint8_t *byteBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
uint8_t *returnBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
if(byteBuffer == NULL || returnBuffer == NULL){
printf("error allocating buffers\n");
return(-1);
}
uint32_t returnlen = 0;
printf("Key is...\n%s", bytes_to_hexstr(key, 32));
str_to_bytes(message, byteBuffer);
printf("Message is... \n%s\n", message);
AES256MainECB(key, byteBuffer, strlen(message), returnBuffer, &returnlen, true);
printf("Encrypted data is...\n%s",bytes_to_hexstr(returnBuffer, returnlen));
uint8_t *newBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
AES256MainECB(key, returnBuffer, returnlen, newBuffer, &returnlen, false);
printf("Unencrypted data is...\n%s",bytes_to_hexstr(newBuffer, returnlen));
printf("Unencrypted message is...\n%s\n",bytes_to_str(newBuffer, returnlen));
}else if(strcmp(argv[1], "-cbc") == 0){
if(argc == 5){
message = argv[2];
char *keystring = argv[3];
if(strlen(keystring) != 64){
printf("key must be 32 bytes (%d)\n",strlen(keystring)/2);
exit(-1);
}
parse_02X(key,keystring,32);
char *ivstring = argv[4];
if(strlen(ivstring) != 32){
printf("iv must be 16 bytes (%d)\n",strlen(ivstring)/2);
exit(-1);
}
parse_02X(iv,ivstring,16);
}else{
printf("Usage:\n%s [Message to encrypt] [Key in 02x hex] [IV in 02x hex]\n",argv[0]);
exit(-1);
}
uint8_t *byteBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
uint8_t *returnBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
if(byteBuffer == NULL || returnBuffer == NULL){
printf("error allocating buffers\n");
return(-1);
}
uint32_t returnlen = 0;
printf("Key is...\n%s", bytes_to_hexstr(key, 32));
printf("IV is...\n%s", bytes_to_hexstr(iv, 16));
str_to_bytes(message, byteBuffer);
printf("Message is... \n%s\n", message);
AES256MainCBC(key, byteBuffer, iv, strlen(message), returnBuffer, &returnlen, true);
printf("Encrypted data is...\n%s",bytes_to_hexstr(returnBuffer, returnlen));
uint8_t *newBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
AES256MainCBC(key, returnBuffer, iv, returnlen, newBuffer, &returnlen, false);
printf("Unencrypted data is...\n%s",bytes_to_hexstr(newBuffer, returnlen));
printf("Unencrypted message is...\n%s\n",bytes_to_str(newBuffer, returnlen));
}else if(strcmp(argv[1], "-ctr") == 0){
if(argc == 5){
message = argv[2];
char *keystring = argv[3];
if(strlen(keystring) != 64){
printf("key must be 32 bytes (%d)\n",strlen(keystring)/2);
exit(-1);
}
parse_02X(key,keystring,32);
char *ivstring = argv[4];
if(strlen(ivstring) != 32){
printf("iv must be 16 bytes (%d)\n",strlen(ivstring)/2);
exit(-1);
}
parse_02X(iv,ivstring,16);
}else{
printf("Usage:\n%s [Message to encrypt] [Key in 02x hex] [IV in 02x hex]\n",argv[0]);
exit(-1);
}
uint8_t *byteBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
uint8_t *returnBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
if(byteBuffer == NULL || returnBuffer == NULL){
printf("error allocating buffers\n");
return(-1);
}
uint32_t returnlen = 0;
printf("Key is...\n%s", bytes_to_hexstr(key, 32));
printf("IV is...\n%s", bytes_to_hexstr(iv, 16));
str_to_bytes(message, byteBuffer);
printf("Message is... \n%s\n", message);
AES256MainCTR(key, byteBuffer, iv, strlen(message), returnBuffer, &returnlen, true);
printf("Encrypted data is...\n%s",bytes_to_hexstr(returnBuffer, returnlen));
uint8_t *newBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
AES256MainCTR(key, returnBuffer, iv, returnlen, newBuffer, &returnlen, false);
printf("Unencrypted data is...\n%s",bytes_to_hexstr(newBuffer, returnlen));
printf("Unencrypted message is...\n%s\n",bytes_to_str(newBuffer, returnlen));
}else{
printf("Supported Modes:\n");
printf("-ecb\n");
printf("-cbc\n");
printf("-ctr\n");
exit(-1);
}
}