forked from canokeys/canokey-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_des.c
More file actions
73 lines (62 loc) · 2.2 KB
/
test_des.c
File metadata and controls
73 lines (62 loc) · 2.2 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
// SPDX-License-Identifier: Apache-2.0
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <block-cipher.h>
#include <cmocka.h>
#include <des.h>
static void test_des_ecb(void **state) {
(void)state;
uint8_t data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
uint8_t expected[] = {0xE1, 0xB2, 0x46, 0xE5, 0xA7, 0xC7, 0x4C, 0xBC};
block_cipher_config cfg = {.mode = ECB,
.in = data,
.in_size = sizeof(data),
.out = data,
.key = key,
.iv = NULL,
.block_size = 8,
.encrypt = des_enc,
.decrypt = des_dec};
block_cipher_enc(&cfg);
for (int i = 0; i != 8; ++i) {
assert_int_equal(data[i], expected[i]);
}
block_cipher_dec(&cfg);
for (int i = 0; i != 8; ++i) {
assert_int_equal(data[i], i);
}
}
static void test_tdes_ecb(void **state) {
(void)state;
uint8_t data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
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};
uint8_t expected[] = {0x58, 0xED, 0x24, 0x8F, 0x77, 0xF6, 0xB1, 0x9E};
block_cipher_config cfg = {.mode = ECB,
.in = data,
.in_size = sizeof(data),
.out = data,
.key = key,
.iv = NULL,
.block_size = 8,
.encrypt = tdes_enc,
.decrypt = tdes_dec};
block_cipher_enc(&cfg);
for (int i = 0; i != 8; ++i) {
assert_int_equal(data[i], expected[i]);
}
block_cipher_dec(&cfg);
for (int i = 0; i != 8; ++i) {
assert_int_equal(data[i], i);
}
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_des_ecb),
cmocka_unit_test(test_tdes_ecb),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}