aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/torture_hashes.c
blob: 2e242563f25a5f2fb7652b3bdc90387ca600ec24 (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
#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;
    unsigned char *hash = NULL;
    char *hexa = NULL;
    size_t hlen;
    int rc = 0;

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

    hexa = ssh_get_hexa(hash, hlen);
    ssh_string_free_char((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;
    unsigned char *hash = NULL;
    char *sha1 = NULL;
    int rc = 0;
    size_t hlen;

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

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

    ssh_string_free_char(sha1);
}

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

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

    sha256 = ssh_get_b64_unpadded(hash, hlen);
    ssh_string_free_char((char *)hash);
    assert_string_equal(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),
    };

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