summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/src/flb_hash.c
blob: eef18d868783a1987b7c3bbcd6718c47f7a630e5 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2019-2020 The Fluent Bit Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <fluent-bit/flb_hash.h>
#include <openssl/bio.h>

static const EVP_MD *flb_crypto_get_digest_algorithm_instance_by_id(int algorithm_id)
{
    const EVP_MD *algorithm;

    if (algorithm_id == FLB_HASH_SHA256) {
        algorithm = EVP_sha256();
    }
    else if (algorithm_id == FLB_HASH_SHA512) {
        algorithm = EVP_sha512();
    }
    else if (algorithm_id == FLB_HASH_MD5) {
        algorithm = EVP_md5();
    }
    else {
        algorithm = NULL;
    }

    return algorithm;
}

int flb_hash_init(struct flb_hash *context, int hash_type)
{
    const EVP_MD *digest_algorithm;
    int           result;

    if (context == NULL) {
        return FLB_CRYPTO_INVALID_ARGUMENT;
    }

    digest_algorithm = flb_crypto_get_digest_algorithm_instance_by_id(hash_type);

    if (digest_algorithm == NULL) {
        return FLB_CRYPTO_INVALID_ARGUMENT;
    }

    context->backend_context = EVP_MD_CTX_create();

    if (context->backend_context == NULL) {
        context->last_error = ERR_get_error();

        return FLB_CRYPTO_BACKEND_ERROR;
    }

    result = EVP_DigestInit_ex(context->backend_context, digest_algorithm, 0);

    if (result == 0) {
        context->last_error = ERR_get_error();

        return FLB_CRYPTO_BACKEND_ERROR;
    }

    context->digest_size = EVP_MD_CTX_size(context->backend_context);

    return FLB_CRYPTO_SUCCESS;
}

int flb_hash_finalize(struct flb_hash *context,
                      unsigned char *digest_buffer,
                      size_t digest_buffer_size)
{
    unsigned int digest_length;
    int          result;

    if (context->backend_context == NULL) {
        return FLB_CRYPTO_INVALID_ARGUMENT;
    }

    if (digest_buffer == NULL) {
        return FLB_CRYPTO_INVALID_ARGUMENT;
    }

    if (digest_buffer_size < context->digest_size) {
        return FLB_CRYPTO_INVALID_ARGUMENT;
    }

    result = EVP_DigestFinal_ex(context->backend_context,
                                digest_buffer, &digest_length);

    if (result == 0) {
        context->last_error = ERR_get_error();

        return FLB_CRYPTO_BACKEND_ERROR;
    }

    (void) digest_length;

    return FLB_CRYPTO_SUCCESS;
}

int flb_hash_update(struct flb_hash *context,
                    unsigned char *data,
                    size_t data_length)
{
    int result;

    if (context->backend_context == NULL) {
        return FLB_CRYPTO_INVALID_ARGUMENT;
    }

    if (data == NULL) {
        return FLB_CRYPTO_INVALID_ARGUMENT;
    }

    result = EVP_DigestUpdate(context->backend_context,
                              data,
                              data_length);

    if (result == 0) {
        context->last_error = ERR_get_error();

        return FLB_CRYPTO_BACKEND_ERROR;
    }

    return FLB_CRYPTO_SUCCESS;
}

int flb_hash_cleanup(struct flb_hash *context)
{
    if (context->backend_context == NULL) {
        return FLB_CRYPTO_INVALID_ARGUMENT;
    }

    EVP_MD_CTX_destroy(context->backend_context);

    context->backend_context = NULL;

    return FLB_CRYPTO_SUCCESS;
}

int  flb_hash_simple_batch(int hash_type,
                           size_t entry_count,
                           unsigned char **data_entries,
                           size_t *length_entries,
                           unsigned char *digest_buffer,
                           size_t digest_buffer_size)
{
    struct flb_hash digest_context;
    size_t          entry_index;
    int             result;

    result = flb_hash_init(&digest_context, hash_type);

    if (result == FLB_CRYPTO_SUCCESS) {
        for (entry_index = 0 ;
             entry_index < entry_count && result == FLB_CRYPTO_SUCCESS;
             entry_index++) {
            if (data_entries[entry_index] != NULL &&
                length_entries[entry_index] > 0) {
                result = flb_hash_update(&digest_context,
                                         data_entries[entry_index],
                                         length_entries[entry_index]);
            }
        }

        if (result == FLB_CRYPTO_SUCCESS) {
            result = flb_hash_finalize(&digest_context,
                                       digest_buffer,
                                       digest_buffer_size);
        }

        flb_hash_cleanup(&digest_context);
    }

    return result;
}

int flb_hash_simple(int hash_type,
                    unsigned char *data,
                    size_t data_length,
                    unsigned char *digest_buffer,
                    size_t digest_buffer_size)
{
    size_t         length_entries[1];
    unsigned char *data_entries[1];

    data_entries[0] = data;
    length_entries[0] = data_length;

    return flb_hash_simple_batch(hash_type,
                                 1,
                                 data_entries,
                                 length_entries,
                                 digest_buffer,
                                 digest_buffer_size);
}