Debian Codegraph @

tests/unittests/torture_hashes.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #include "torture.h"
6 #include "torture_key.h"
7 #include "legacy.c"
8 #include "dh.c"
9
10 static int setup_rsa_key(void **state)
11 {
12 int rc=0;
13 enum ssh_keytypes_e type;
14 char *b64_key, *p;
15 ssh_key key;
16
17 const char *q;
18
19 b64_key = strdup(torture_get_testkey_pub(SSH_KEYTYPE_RSA));
20 assert_non_null(b64_key);
21
22 q = p = b64_key;
23 while (p != NULL && *p != '\0' && *p != ' ') p++;
24 if (p != NULL) {
25 *p = '\0';
26 }
27
28 type = ssh_key_type_from_name(q);
29 assert_true(type == SSH_KEYTYPE_RSA);
30
31 q = ++p;
32 while (p != NULL && *p != '\0' && *p != ' ') p++;
33 if (p != NULL) {
34 *p = '\0';
35 }
36
37 rc = ssh_pki_import_pubkey_base64(q, type, &key);
38 assert_true(rc == 0);
39
40 free(b64_key);
41 *state = key;
42
43 return 0;
44 }
45
46 static int teardown(void **state)
47 {
48 SSH_KEY_FREE(*state);
49 return 0;
50 }
51
52 static void torture_md5_hash(void **state)
53 {
54 ssh_key pubkey = *state;
55 char *hash = NULL;
56 char *hexa = NULL;
57 size_t hlen;
58 int rc = 0;
59
60 #if defined(HAVE_LIBCRYPTO) && OPENSSL_VERSION_NUMBER < 0x30000000L
61 /* In FIPS mode without OpenSSL providers, we cannot use MD5 */
62 if (ssh_fips_mode()) {
63 skip();
64 }
65 #endif
66
67 rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_MD5,
68 (unsigned char **)&hash, &hlen);
69 assert_int_equal(rc, SSH_OK);
70
71 hexa = ssh_get_hexa((unsigned char *)hash, hlen);
72 SSH_STRING_FREE_CHAR(hash);
73 assert_string_equal(hexa,
74 "50:15:a0:9b:92:bf:33:1c:01:c5:8c:fe:18:fa:ce:78");
75 SSH_STRING_FREE_CHAR(hexa);
76 }
77
78 static void torture_sha1_hash(void **state)
79 {
80 ssh_key pubkey = *state;
81 char *hash = NULL;
82 char *sha1 = NULL;
83 int rc = 0;
84 size_t hlen;
85
86 rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA1,
87 (unsigned char **)&hash, &hlen);
88 assert_true(rc == 0);
89
90 sha1 = ssh_get_b64_unpadded((unsigned char *)hash, hlen);
91 SSH_STRING_FREE_CHAR(hash);
92 assert_string_equal(sha1, "6wP+houujQmxLBiFugTcoeoODCM");
93
94 SSH_STRING_FREE_CHAR(sha1);
95 }
96
97 static void torture_sha256_hash(void **state)
98 {
99 ssh_key pubkey = *state;
100 char *hash = NULL;
101 char *sha256 = NULL;
102 int rc = 0;
103 size_t hlen;
104
105 rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256,
106 (unsigned char **)&hash, &hlen);
107 assert_true(rc == 0);
108
109 sha256 = ssh_get_b64_unpadded((unsigned char *)hash, hlen);
110 SSH_STRING_FREE_CHAR(hash);
111 assert_string_equal(sha256, "jXstVLLe84fSDo1kEYGn6iumnPCSorhaiWxnJz8VTII");
112
113 SSH_STRING_FREE_CHAR(sha256);
114
115 }
116
117 static void torture_sha256_fingerprint(void **state)
118 {
119 ssh_key pubkey = *state;
120 char *hash = NULL;
121 char *sha256 = NULL;
122 int rc = 0;
123 size_t hlen;
124
125 rc = ssh_get_publickey_hash(pubkey,
126 SSH_PUBLICKEY_HASH_SHA256,
127 (unsigned char **)&hash,
128 &hlen);
129 assert_true(rc == 0);
130
131 sha256 = ssh_get_fingerprint_hash(SSH_PUBLICKEY_HASH_SHA256,
132 (unsigned char *)hash,
133 hlen);
134 SSH_STRING_FREE_CHAR(hash);
135 assert_string_equal(sha256,
136 "SHA256:jXstVLLe84fSDo1kEYGn6iumnPCSorhaiWxnJz8VTII");
137
138 SSH_STRING_FREE_CHAR(sha256);
139 }
140
141 int torture_run_tests(void) {
142 int rc;
143 struct CMUnitTest tests[] = {
144 cmocka_unit_test_setup_teardown(torture_md5_hash,
145 setup_rsa_key,
146 teardown),
147 cmocka_unit_test_setup_teardown(torture_sha1_hash,
148 setup_rsa_key,
149 teardown),
150 cmocka_unit_test_setup_teardown(torture_sha256_hash,
151 setup_rsa_key,
152 teardown),
153 cmocka_unit_test_setup_teardown(torture_sha256_fingerprint,
154 setup_rsa_key,
155 teardown),
156 };
157
158 torture_filter_tests(tests);
159 rc = cmocka_run_group_tests(tests, NULL, NULL);
160 return rc;
161 }