1 #include <test.h>
2
3 #include <cf3.defs.h>
4 #include <crypto.h>
5
6 #define PLAINTEXT "123456789012345678901234567890123"
7 #define KEY "1234567890123456789012345678901234567890123456789012345678901234" /* at least 512 bits long (to be sure) */
8
9 // use Blowfish (64-bit block size) for now
10 #define CIPHER_TYPE_CFENGINE 'c'
11 #define CIPHER_BLOCK_SIZE_BYTES 8
12 static const char CIPHERTEXT_PRECOMPUTED[] =
13 {
14 0x99, 0xfd, 0x86, 0x9c, 0x17, 0xb9, 0xe4, 0x98,
15 0xab, 0x01, 0x17, 0x5a, 0x4a, 0xcf, 0xfc, 0x1f,
16 0xd4, 0xc5, 0xa3, 0xab, 0xf0, 0x1c, 0xa7, 0x39,
17 0xf1, 0xf4, 0x09, 0xe4, 0xac, 0xb6, 0x44, 0xbb,
18 0x47, 0xdd, 0xe6, 0xc4, 0x0e, 0x4a, 0x16, 0xf0
19 };
20
ComputeCiphertextLen(int plaintext_len,int cipher_block_size_bytes)21 static int ComputeCiphertextLen(int plaintext_len, int cipher_block_size_bytes)
22 {
23 int last_block_offset = plaintext_len % cipher_block_size_bytes;
24 int padding = cipher_block_size_bytes - last_block_offset;
25
26 return (plaintext_len + padding);
27 }
28
test_cipher_init(void)29 static void test_cipher_init(void)
30 {
31 unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
32 unsigned char iv[] = {1,2,3,4,5,6,7,8};
33 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
34
35 EVP_CIPHER_CTX_init(ctx);
36 EVP_EncryptInit_ex(ctx, EVP_bf_cbc(), NULL, key, iv);
37 EVP_CIPHER_CTX_free(ctx);
38 }
39
test_symmetric_encrypt(void)40 static void test_symmetric_encrypt(void)
41 {
42 char ciphertext[CF_BUFSIZE];
43 int plaintext_len = strlen(PLAINTEXT) + 1;
44
45 int ciphertext_len = EncryptString(ciphertext, sizeof(ciphertext),
46 PLAINTEXT, plaintext_len,
47 CIPHER_TYPE_CFENGINE, KEY);
48
49 assert_int_equal(ciphertext_len, ComputeCiphertextLen(plaintext_len, CIPHER_BLOCK_SIZE_BYTES));
50
51 assert_memory_equal(ciphertext, CIPHERTEXT_PRECOMPUTED, ciphertext_len);
52 }
53
test_symmetric_decrypt(void)54 static void test_symmetric_decrypt(void)
55 {
56 char *ciphertext = (char *)CIPHERTEXT_PRECOMPUTED;
57 int ciphertext_len = sizeof(CIPHERTEXT_PRECOMPUTED);
58
59 char plaintext_out[CF_BUFSIZE];
60
61 int plaintext_len = DecryptString(plaintext_out, sizeof(plaintext_out),
62 ciphertext, ciphertext_len, CIPHER_TYPE_CFENGINE, KEY);
63
64 assert_int_equal(plaintext_len, strlen(PLAINTEXT) + 1);
65
66 assert_string_equal(plaintext_out, PLAINTEXT);
67 }
68
test_cipher_block_size(void)69 static void test_cipher_block_size(void)
70 {
71 assert_int_equal(CipherBlockSizeBytes(EVP_bf_cbc()), 8);
72
73 assert_int_equal(CipherBlockSizeBytes(EVP_aes_256_cbc()), 16);
74 }
75
test_cipher_text_size_max(void)76 static void test_cipher_text_size_max(void)
77 {
78 assert_int_equal(CipherTextSizeMax(EVP_aes_256_cbc(), 1), 32);
79
80 assert_int_equal(CipherTextSizeMax(EVP_aes_256_cbc(), CF_BUFSIZE), 4127);
81
82 assert_int_equal(CipherTextSizeMax(EVP_bf_cbc(), 1), 16);
83
84 assert_int_equal(CipherTextSizeMax(EVP_bf_cbc(), CF_BUFSIZE), 4111);
85 }
86
test_plain_text_size_max(void)87 static void test_plain_text_size_max(void)
88 {
89 assert_int_equal(PlainTextSizeMax(EVP_aes_256_cbc(), 1), 33);
90
91 assert_int_equal(PlainTextSizeMax(EVP_aes_256_cbc(), CF_BUFSIZE), 4128);
92
93 assert_int_equal(PlainTextSizeMax(EVP_bf_cbc(), 1), 17);
94
95 assert_int_equal(PlainTextSizeMax(EVP_bf_cbc(), CF_BUFSIZE), 4112);
96 }
97
main()98 int main()
99 {
100 PRINT_TEST_BANNER();
101 CryptoInitialize();
102
103 const UnitTest tests[] =
104 {
105 unit_test(test_cipher_init),
106 unit_test(test_symmetric_encrypt),
107 unit_test(test_symmetric_decrypt),
108 unit_test(test_cipher_block_size),
109 unit_test(test_cipher_text_size_max),
110 unit_test(test_plain_text_size_max),
111 };
112
113 return run_tests(tests);
114 }
115