aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/torture_hashes.c
blob: 59e23d28c5470cec4f5eb3d86ea266b4e3cc23d2 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "config.h"

#define LIBSSH_STATIC

#include "torture.h"
#include "torture_key.h"
#include "legacy.c"
#include "dh.c"

static int setup_rsa_key(void **state)
{
    int rc=0;
    enum ssh_keytypes_e type;
    char *b64_key, *p;
    ssh_key key;

    const char *q;

    b64_key = strdup(torture_get_testkey_pub(SSH_KEYTYPE_RSA, 0));
    assert_true(b64_key != NULL);

    q = p = b64_key;
    while (*p != ' ') p++;
    *p = '\0';

    type = ssh_key_type_from_name(q);
    assert_true(type == SSH_KEYTYPE_RSA);

    q = ++p;
    while (*p != ' ') p++;
    *p = '\0';

    rc = ssh_pki_import_pubkey_base64(q, type, &key);
    assert_true(rc == 0);

    free(b64_key);
    *state = key;

    return 0;
}

static int teardown(void **state)
{
    SSH_KEY_FREE(*state);
    return 0;
}

static void torture_md5_hash(void **state)
{
    ssh_key pubkey = *state;
    char *hash = NULL;
    char *hexa = NULL;
    size_t hlen;
    int rc = 0;

    rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_MD5,
                                (unsigned char **)&hash, &hlen);
    assert_true(rc == 0);

    hexa = ssh_get_hexa((unsigned char *)hash, hlen);
    SSH_STRING_FREE_CHAR(hash);
    assert_string_equal(hexa,
                        "50:15:a0:9b:92:bf:33:1c:01:c5:8c:fe:18:fa:ce:78");

    SSH_STRING_FREE_CHAR(hexa);
}

static void torture_sha1_hash(void **state)
{
    ssh_key pubkey = *state;
    char *hash = NULL;
    char *sha1 = NULL;
    int rc = 0;
    size_t hlen;

    rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA1,
                                (unsigned char **)&hash, &hlen);
    assert_true(rc == 0);

    sha1 = ssh_get_b64_unpadded((unsigned char *)hash, hlen);
    SSH_STRING_FREE_CHAR(hash);
    assert_string_equal(sha1, "6wP+houujQmxLBiFugTcoeoODCM");

    SSH_STRING_FREE_CHAR(sha1);
}

static void torture_sha256_hash(void **state)
{
    ssh_key pubkey = *state;
    char *hash = NULL;
    char *sha256 = NULL;
    int rc = 0;
    size_t hlen;

    rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256,
                                (unsigned char **)&hash, &hlen);
    assert_true(rc == 0);

    sha256 = ssh_get_b64_unpadded((unsigned char *)hash, hlen);
    SSH_STRING_FREE_CHAR(hash);
    assert_string_equal(sha256, "jXstVLLe84fSDo1kEYGn6iumnPCSorhaiWxnJz8VTII");

    SSH_STRING_FREE_CHAR(sha256);

}

static void torture_sha256_fingerprint(void **state)
{
    ssh_key pubkey = *state;
    char *hash = NULL;
    char *sha256 = NULL;
    int rc = 0;
    size_t hlen;

    rc = ssh_get_publickey_hash(pubkey,
                                SSH_PUBLICKEY_HASH_SHA256,
                                (unsigned char **)&hash,
                                &hlen);
    assert_true(rc == 0);

    sha256 = ssh_get_fingerprint_hash(SSH_PUBLICKEY_HASH_SHA256,
                                      (unsigned char *)hash,
                                      hlen);
    SSH_STRING_FREE_CHAR(hash);
    assert_string_equal(sha256,
                        "SHA256:jXstVLLe84fSDo1kEYGn6iumnPCSorhaiWxnJz8VTII");

    SSH_STRING_FREE_CHAR(sha256);
}

int torture_run_tests(void) {
    int rc;
    struct CMUnitTest tests[] = {
        cmocka_unit_test_setup_teardown(torture_md5_hash,
                                        setup_rsa_key,
                                        teardown),
        cmocka_unit_test_setup_teardown(torture_sha1_hash,
                                        setup_rsa_key,
                                        teardown),
        cmocka_unit_test_setup_teardown(torture_sha256_hash,
                                        setup_rsa_key,
                                        teardown),
        cmocka_unit_test_setup_teardown(torture_sha256_fingerprint,
                                        setup_rsa_key,
                                        teardown),
    };

    torture_filter_tests(tests);
    rc = cmocka_run_group_tests(tests, NULL, NULL);
    return rc;
}