Debian Codegraph @

tests/unittests/torture_session_keys.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #include "torture.h"
6 #include "libssh/bignum.h"
7 #include "libssh/crypto.h"
8 #include "libssh/dh.h"
9
10 uint8_t key[32] =
11 "\xf7\xa0\xe6\xdf\x1f\x87\x7d\x22\x68\xd2\xc4\xb0\xc5\x93\xa4"
12 "\x8e\x30\x17\xc6\xab\xca\xf3\x9a\xa4\x9f\x7b\xed\x51\xb1\xe8"
13 "\x8a\x42";
14 uint8_t secret[32] =
15 "\x33\x64\x8e\x7f\xea\xd9\xd7\xee\x89\x4f\xd8\xd0\xe5\x83\x00"
16 "\x3d\x53\x17\xbc\xa8\x8b\x6b\x2a\x31\x50\xcc\x08\xe9\xea\x87"
17 "\xb4\x23";
18
19 uint8_t eIV[32] =
20 "\x9a\x2b\x40\x9d\x29\x8e\x22\x70\x86\xdf\x0e\x72\x9b\x91\x31"
21 "\x90\x5d\x69\xc5\x87\x79\x83\x72\x63\x4e\x67\xf5\x9e\x00\x77"
22 "\x8c\x7f";
23 uint8_t dIV[32] =
24 "\x10\xdd\x7f\x31\x6d\xe3\x49\x28\xbf\x99\x80\x08\x16\xb3\x99"
25 "\xff\x8c\x61\x9b\xb9\xc2\xdd\x40\xfb\x36\xf9\x97\xd8\x8c\x55"
26 "\xbf\xa0";
27 uint8_t eK[24] =
28 "\xe1\x99\x36\xb8\xe6\x1f\x3d\x54\xc3\xa2\xdd\x79\xf0\xfe\x78"
29 "\x9e\x87\xd5\x05\x54\x26\x34\x21\xd0";
30 uint8_t dK[24] =
31 "\xf8\xdd\xc3\xea\x5a\x59\x98\xb9\x86\xaa\x77\x29\x67\x51\x46"
32 "\x21\x73\xc2\x6a\x6b\xed\xf2\x49\x98";
33 uint8_t encrypt_MAC[32] =
34 "\x0f\xbd\x1f\xe9\x2a\xaa\x84\xdc\xb5\xfc\xfb\x68\x2c\xa5\xe0"
35 "\xba\xf2\x6f\xe5\x80\xee\x8f\x5c\x5b\x30\x55\x25\xb3\x7b\x21"
36 "\xdc\xe5";
37 uint8_t decrypt_MAC[32] =
38 "\xa3\x52\x6e\x72\xa8\x8b\xde\xc5\x68\x66\x89\xae\x0a\xd2\x83"
39 "\x23\x21\x4b\x3f\x04\x2e\x7f\x86\x04\x0f\xa8\x04\x3c\x62\xad"
40 "\x74\x91";
41
42 struct ssh_cipher_struct fake_in_cipher = {
43 .keysize = 192
44 };
45
46 struct ssh_cipher_struct fake_out_cipher = {
47 .keysize = 192
48 };
49
50 struct ssh_crypto_struct test_crypto = {
51 .session_id_len = 32,
52 .session_id = secret,
53 .digest_len = 32,
54 .secret_hash = secret,
55 .in_cipher = &fake_in_cipher,
56 .out_cipher = &fake_out_cipher,
57 .in_hmac = SSH_HMAC_SHA256,
58 .out_hmac = SSH_HMAC_SHA256,
59 .digest_type = SSH_KDF_SHA256,
60 };
61
62 struct ssh_session_struct session = {
63 .next_crypto = &test_crypto
64 };
65
66 static void torture_session_keys(UNUSED_PARAM(void **state))
67 {
68 ssh_string k_string;
69 int rc;
70
71 k_string = ssh_string_new(32);
72 rc = ssh_string_fill(k_string, key, 32);
73 assert_int_equal(rc, 0);
74
75 test_crypto.shared_secret = ssh_make_string_bn(k_string);
76 SSH_STRING_FREE(k_string);
77
78 rc = ssh_generate_session_keys(&session);
79 assert_int_equal(rc, 0);
80
81 assert_memory_equal(test_crypto.encryptIV, eIV, 32);
82 assert_memory_equal(test_crypto.decryptIV, dIV, 32);
83 assert_memory_equal(test_crypto.encryptkey, eK, 24);
84 assert_memory_equal(test_crypto.decryptkey, dK, 24);
85 assert_memory_equal(test_crypto.encryptMAC, encrypt_MAC, 32);
86 assert_memory_equal(test_crypto.decryptMAC, decrypt_MAC, 32);
87
88 bignum_safe_free(test_crypto.shared_secret);
89 SAFE_FREE(test_crypto.encryptIV);
90 SAFE_FREE(test_crypto.decryptIV);
91 SAFE_FREE(test_crypto.encryptkey);
92 SAFE_FREE(test_crypto.decryptkey);
93 SAFE_FREE(test_crypto.encryptMAC);
94 SAFE_FREE(test_crypto.decryptMAC);
95 }
96
97 int torture_run_tests(void) {
98 int rc;
99 const struct CMUnitTest tests[] = {
100 cmocka_unit_test(torture_session_keys),
101 };
102
103 ssh_init();
104 rc = cmocka_run_group_tests(tests, NULL, NULL);
105 ssh_finalize();
106 return rc;
107 }