summaryrefslogtreecommitdiffstats
path: root/fluent-bit/tests/internal/hmac.c
blob: f7de588f515030044d092af5941835741c6b04fc (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_hmac.h>

#include "flb_tests_internal.h"

static size_t get_sample_data(unsigned char ***buffer_list,
                              size_t **length_list,
                              char **precomputed_key,
                              char **precomputed_mac,
                              int *precomputed_mac_type)
{
    static size_t       lengths[] = {0, 0, 0, 0};
    static const char  *buffers[] = {
                                        "This is one ",
                                        "of the four ",
                                        "buffers that will be passed ",
                                        "to the simple batched digest processor function"
                                    };
    static const char  *signature = "a68b8b3e480e00e0e61b4c097a29e64a261dc2548c608677c2827ea1e5b65d77";
    static const int    mac_type = FLB_HASH_SHA256;
    size_t              index;
    static const char  *key = "Long live the bird thing!";

    if (lengths[0] == 0) {
        for (index = 0 ; index < 4 ; index++) {
            lengths[index] = strlen(buffers[index]);
        }
    }

    *buffer_list = (unsigned char **) buffers;
    *length_list = lengths;
    *precomputed_key = (char *) key;
    *precomputed_mac = (char *) signature;
    *precomputed_mac_type = mac_type;

    return 4;
}

static void hexlify(uint8_t *hash, char *out)
{
    int i;
    static const char hex[] = "0123456789abcdef";
    char *buf = out;

    for (i = 0; i < 32; i++) {
        *buf++ = hex[hash[i] >> 4];
        *buf++ = hex[hash[i] & 0xf];
    }
}

static void test_hmac_standard()
{
    int               ref_signature_type;
    char              hex_signature[64];
    char             *ref_hex_signature;
    uint8_t           raw_signature[32];
    size_t            buffer_count;
    unsigned char   **buffer_list;
    size_t           *length_list;
    char             *ref_key;
    int               result;
    size_t            index;
    struct flb_hmac   hmac;

    buffer_count = get_sample_data(&buffer_list, &length_list,
                                   &ref_key,
                                   &ref_hex_signature,
                                   &ref_signature_type);

    TEST_CHECK(buffer_count > 0);
    TEST_CHECK(ref_signature_type == FLB_HASH_SHA256);

    result = flb_hmac_init(&hmac,
                           ref_signature_type,
                           (unsigned char *) ref_key,
                           strlen(ref_key));
    TEST_CHECK(result == FLB_CRYPTO_SUCCESS);

    for (index = 0 ; index < buffer_count ; index++) {
        result = flb_hmac_update(&hmac, buffer_list[index], length_list[index]);
        TEST_CHECK(result == FLB_CRYPTO_SUCCESS);
    }

    result = flb_hmac_finalize(&hmac, raw_signature, sizeof(raw_signature));
    TEST_CHECK(result == FLB_CRYPTO_SUCCESS);

    hexlify(raw_signature, hex_signature);

    TEST_CHECK(memcmp(hex_signature, ref_hex_signature, 64) == 0);

    flb_hmac_cleanup(&hmac);
}


static void test_hmac_simple_batch()
{
    int               ref_signature_type;
    char              hex_signature[64];
    char             *ref_hex_signature;
    uint8_t           raw_signature[32];
    size_t            buffer_count;
    unsigned char   **buffer_list;
    size_t           *length_list;
    char             *ref_key;
    int               result;

    buffer_count = get_sample_data(&buffer_list, &length_list,
                                   &ref_key,
                                   &ref_hex_signature,
                                   &ref_signature_type);

    TEST_CHECK(buffer_count > 0);
    TEST_CHECK(ref_signature_type == FLB_HASH_SHA256);

    result = flb_hmac_simple_batch(ref_signature_type,
                                   (unsigned char *) ref_key,
                                   strlen(ref_key),
                                   buffer_count,
                                   buffer_list,
                                   length_list,
                                   raw_signature,
                                   sizeof(raw_signature));
    TEST_CHECK(result == FLB_CRYPTO_SUCCESS);

    hexlify(raw_signature, hex_signature);

    TEST_CHECK(memcmp(hex_signature, ref_hex_signature, 64) == 0);
}

TEST_LIST = {
    { "test_hmac_simple_batch", test_hmac_simple_batch },
    { "test_hmac_standard",     test_hmac_standard },
    { 0 }
};