Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 230929a

Browse files
Jan-Niklas Burfeindcryptomilk
authored andcommitted
tests: Add torture_hashes for pubkey hashes
Signed-off-by: Jan-Niklas Burfeind <libssh@aiyionpri.me> Reviewed-by: Andreas Schneider <asn@cryptomilk.org> (cherry picked from commit 9510a53)
1 parent c847216 commit 230929a

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

tests/unittests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ target_compile_options(torture_isipaddr PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
3232
add_cmocka_test(torture_knownhosts_parsing torture_knownhosts_parsing.c ${TORTURE_LIBRARY})
3333
target_compile_options(torture_knownhosts_parsing PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
3434

35+
add_cmocka_test(torture_hashes torture_hashes.c ${TORTURE_LIBRARY})
36+
target_compile_options(torture_hashes PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
3537

3638
if (CMAKE_USE_PTHREADS_INIT)
3739
add_cmocka_test(torture_rand torture_rand.c ${TORTURE_LIBRARY})

tests/unittests/torture_hashes.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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, 0));
20+
assert_true(b64_key != NULL);
21+
22+
q = p = b64_key;
23+
while (*p != ' ') p++;
24+
*p = '\0';
25+
26+
type = ssh_key_type_from_name(q);
27+
assert_true(type == SSH_KEYTYPE_RSA);
28+
29+
q = ++p;
30+
while (*p != ' ') p++;
31+
*p = '\0';
32+
33+
rc = ssh_pki_import_pubkey_base64(q, type, &key);
34+
assert_true(rc == 0);
35+
36+
free(b64_key);
37+
*state = key;
38+
39+
return 0;
40+
}
41+
42+
static int teardown(void **state)
43+
{
44+
ssh_key_free(*state);
45+
return 0;
46+
}
47+
48+
static void torture_md5_hash(void **state)
49+
{
50+
ssh_key pubkey = *state;
51+
unsigned char *hash = NULL;
52+
char *hexa = NULL;
53+
size_t hlen;
54+
int rc = 0;
55+
56+
rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_MD5, &hash, &hlen);
57+
assert_true(rc == 0);
58+
59+
hexa = ssh_get_hexa(hash, hlen);
60+
assert_string_equal(hexa,
61+
"50:15:a0:9b:92:bf:33:1c:01:c5:8c:fe:18:fa:ce:78");
62+
63+
ssh_string_free_char(hexa);
64+
}
65+
66+
static void torture_sha1_hash(void **state)
67+
{
68+
ssh_key pubkey = *state;
69+
unsigned char *hash = NULL;
70+
char *sha1 = NULL;
71+
int rc = 0;
72+
size_t hlen;
73+
74+
rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen);
75+
assert_true(rc == 0);
76+
77+
sha1 = ssh_get_b64_unpadded(hash, hlen);
78+
assert_string_equal(sha1, "6wP+houujQmxLBiFugTcoeoODCM");
79+
80+
ssh_string_free_char(sha1);
81+
}
82+
83+
static void torture_sha256_hash(void **state)
84+
{
85+
ssh_key pubkey = *state;
86+
unsigned char *hash = NULL;
87+
char *sha256 = NULL;
88+
int rc = 0;
89+
size_t hlen;
90+
91+
rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256, &hash, &hlen);
92+
assert_true(rc == 0);
93+
94+
sha256 = ssh_get_b64_unpadded(hash, hlen);
95+
assert_string_equal(sha256, "jXstVLLe84fSDo1kEYGn6iumnPCSorhaiWxnJz8VTII");
96+
97+
ssh_string_free_char(sha256);
98+
99+
}
100+
101+
int torture_run_tests(void) {
102+
int rc;
103+
struct CMUnitTest tests[] = {
104+
cmocka_unit_test_setup_teardown(torture_md5_hash,
105+
setup_rsa_key,
106+
teardown),
107+
cmocka_unit_test_setup_teardown(torture_sha1_hash,
108+
setup_rsa_key,
109+
teardown),
110+
cmocka_unit_test_setup_teardown(torture_sha256_hash,
111+
setup_rsa_key,
112+
teardown),
113+
};
114+
115+
torture_filter_tests(tests);
116+
rc = cmocka_run_group_tests(tests, NULL, NULL);
117+
return rc;
118+
}

0 commit comments

Comments
 (0)