-
Notifications
You must be signed in to change notification settings - Fork 0
/
testEncryptCBC.c
213 lines (161 loc) · 4.66 KB
/
testEncryptCBC.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
199
200
201
202
203
204
205
206
#include "presentcbc.h"
uint8_t theKey[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uint8_t iv[] = {0x0f,0x79,0xc1,0x38,0x7b,0x22,0x84,0x45};
char *str,*key,*initv;
// char* key;
// char* initv;
unsigned long lenstr, lenmsg;
int blok,baki,pad;
void printMessage( uint8_t *msg, size_t saiz ) {
uint8_t i;
for ( i = 0 ; i < saiz ; i++ ) {
printf( "%02X ", msg[saiz - 1 - i] );
}
printf( "\n" );
}
static void usage()
{
fprintf(stderr, "Usage: ./test [opts] -k <theKey> -m <theMessage>\n");
fprintf(stderr, "\n");
fprintf(stderr, " -k <the Key> \n");
fprintf(stderr, " -m <the Message> \n");
fprintf(stderr, " -v <the Initialization vector>\n" );
exit(-1);
}
void parse_opts(int argc, char** argv)
{
int ch;
// Parse the options/switches
while ((ch = getopt(argc, argv, "v:k:m:?")) != -1)
switch (ch) {
case 'k':
key = optarg;
loadkey( key );
break;
case 'm':
str = optarg;
break;
case 'v':
initv = optarg;
loadMessage(initv);
break;
case '?':
default:
usage();
break;
}
}
bool loadkey( const char * parametr ) {
if (
KEY_SIZE != sscanf( parametr, "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
&theKey[9],
&theKey[8],
&theKey[7],
&theKey[6],
&theKey[5],
&theKey[4],
&theKey[3],
&theKey[2],
&theKey[1],
&theKey[0]
)
) {
printf( "loadkey(): input error!\n" );
return false;
}
printf("Key loaded\n");
return true;
}
bool loadMessage( const char * parametr ) {
if (
BLOCK_SIZE != sscanf( parametr, "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
&iv[7],
&iv[6],
&iv[5],
&iv[4],
&iv[3],
&iv[2],
&iv[1],
&iv[0]
)
) {
printf( "loadMessage(): input error!\n" );
return false;
}
return true;
}
/* Test present.c function - deprecated because I
changed above functions */
// int main(int argc, char** argv)
// {
// printf("PlainText: " );
// printCMessage(theMessage);
// printf("\nEncryption Key: ");
// printKey(theKey);
// encryptP(theCipher,theMessage,theKey);
// printf("\nEncrypted Ciphertext: ");
// printMessage(theCipher);
// decryptP(theDecrypt,theCipher,theKey);
// printf("\nDecrypted text: ");
// printMessage(theDecrypt);
// }
/* test CBC function */
// int main(int argc, char const *argv[])
// {
// printf("PlainText: \n" );
// printMessage(theMessage16,sizeof(theMessage16));
// cbc_encrypt(theKey,iv,theMessage16,sizeof(theMessage16));
// printf("\nEncrypted Ciphertext: \n");
// printMessage(theMessage16,sizeof(theMessage16));
// cbc_decrypt(theKey,iv,theMessage16,sizeof(theMessage16));
// printf("\nDecrypted Plaintext: \n");
// printMessage(theMessage16,sizeof(theMessage16));
// }
int main(int argc, char *argv[])
{
if ( argc <= 1 )
usage();
parse_opts(argc,argv);
if (str == NULL)
usage();
lenstr = strlen(str);
printf("%lu\n", lenstr);
/*pad the message to multiple of 8 bytes*/
blok = lenstr / BLOCK_SIZE;
baki = lenstr % BLOCK_SIZE;
if (baki<=7)
{
blok ++;
pad = (BLOCK_SIZE - baki);
}
else
{
printf("Padding Error\n");
return 1;
}
uint8_t newMessage[blok * BLOCK_SIZE * sizeof(uint8_t)];
memset(newMessage+lenstr,pad,pad);
memcpy(newMessage,str, lenstr);
lenmsg = sizeof(newMessage);
/*padding ends*/
printf("Plaintext: %s\n",str );
printf("Padded Plaintext: \n" );
printMessage(newMessage,lenmsg);
cbc_encrypt((uint8_t*)theKey,iv,newMessage,lenmsg);
printf("\nEncrypted Ciphertext: \n");
printMessage(newMessage,lenmsg);
cbc_decrypt((uint8_t*)theKey,iv,newMessage,lenmsg);
printf("\nDecrypted Plaintext: \n");
printMessage(newMessage,lenmsg);
//copy to new array with size + 1
pad = (int)newMessage[lenmsg-1];
uint8_t finMessage[lenmsg-pad+1];
memcpy (finMessage,newMessage,lenmsg-pad);
//new array without padding
printf("\nRemoved Padding: \n");
printMessage(finMessage,sizeof(finMessage)-1);
//add NULL pointer to turn it into string
memset (finMessage+lenmsg-pad,'\0',1);
printf("Final String: %s\n",(char *)finMessage);
return 0;
}