-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tester.c
54 lines (49 loc) · 1.36 KB
/
Tester.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
#include "DES.h"
#include "stdio.h"
#include "string.h"
#include "assert.h"
#include "ctype.h"
#define block_length 8
uint8_t to_hex(char chr)
{
if (isdigit(chr)) {
return chr - '0';
}
else {
return chr - 'A' + 10;
}
}
void read_block(uint8_t buffer[block_length])
{
char raw_string[2 * block_length] = {0};
scanf("%16s", raw_string);
for (int i = 0; i < block_length; ++i) {
buffer[i] = (to_hex(raw_string[2 * i]) << 4) | to_hex(raw_string[2 * i + 1]);
}
}
int main()
{
freopen("./tests.txt", "r", stdin);
int tests_count = 0;
scanf("%d\n", &tests_count);
for (int test = 0; test < tests_count; ++test) {
uint8_t key[8] = {0};
read_block(key);
uint8_t data[block_length] = {0};
uint8_t encrypted[block_length] = {0};
read_block(data);
uint8_t expected_result[block_length] = {0};
read_block(expected_result);
DES_encrypt(data, encrypted, key, block_length);
for (size_t i = 0; i < block_length; ++i) {
assert(encrypted[i] == expected_result[i]);
}
uint8_t decrypted[block_length] = {0};
DES_decrypt(encrypted, decrypted, key, block_length);
for (size_t i = 0; i < block_length; ++i) {
assert(decrypted[i] == data[i]);
}
}
printf("All tests passed\n");
return 0;
}