summaryrefslogtreecommitdiffstats
path: root/crypto/blake2/blake2b_mac.c
blob: 71b8517885d8d202c898e0b113833d93e09f12a9 (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
/*
 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#ifndef OPENSSL_NO_BLAKE2

# include <openssl/evp.h>
# include "blake2_locl.h"
# include "internal/cryptlib.h"
# include "internal/evp_int.h"

/* typedef EVP_MAC_IMPL */
struct evp_mac_impl_st {
    BLAKE2B_CTX ctx;
    BLAKE2B_PARAM params;
    unsigned char key[BLAKE2B_KEYBYTES];
};

static EVP_MAC_IMPL *blake2b_mac_new(void)
{
    EVP_MAC_IMPL *macctx = OPENSSL_zalloc(sizeof(*macctx));
    if (macctx != NULL) {
        blake2b_param_init(&macctx->params);
        /* ctx initialization is deferred to BLAKE2b_Init() */
    }
    return macctx;
}

static void blake2b_mac_free(EVP_MAC_IMPL *macctx)
{
    if (macctx != NULL) {
        OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
        OPENSSL_free(macctx);
    }
}

static int blake2b_mac_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src)
{
    *dst = *src;
    return 1;
}

static int blake2b_mac_init(EVP_MAC_IMPL *macctx)
{
    /* Check key has been set */
    if (macctx->params.key_length == 0) {
        EVPerr(EVP_F_BLAKE2B_MAC_INIT, EVP_R_NO_KEY_SET);
        return 0;
    }

    return BLAKE2b_Init_key(&macctx->ctx, &macctx->params, macctx->key);
}

static int blake2b_mac_update(EVP_MAC_IMPL *macctx, const unsigned char *data,
                              size_t datalen)
{
    return BLAKE2b_Update(&macctx->ctx, data, datalen);
}

static int blake2b_mac_final(EVP_MAC_IMPL *macctx, unsigned char *out)
{
    return BLAKE2b_Final(out, &macctx->ctx);
}

/*
 * ALL Ctrl functions should be set before init().
 */
static int blake2b_mac_ctrl(EVP_MAC_IMPL *macctx, int cmd, va_list args)
{
    const unsigned char *p;
    size_t len;
    size_t size;

    switch (cmd) {
        case EVP_MAC_CTRL_SET_SIZE:
            size = va_arg(args, size_t);
            if (size < 1 || size > BLAKE2B_OUTBYTES) {
                EVPerr(EVP_F_BLAKE2B_MAC_CTRL, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
                return 0;
            }
            blake2b_param_set_digest_length(&macctx->params, (uint8_t)size);
            return 1;

        case EVP_MAC_CTRL_SET_KEY:
            p = va_arg(args, const unsigned char *);
            len = va_arg(args, size_t);
            if (len < 1 || len > BLAKE2B_KEYBYTES) {
                EVPerr(EVP_F_BLAKE2B_MAC_CTRL, EVP_R_INVALID_KEY_LENGTH);
                return 0;
            }
            blake2b_param_set_key_length(&macctx->params, (uint8_t)len);
            memcpy(macctx->key, p, len);
            memset(macctx->key + len, 0, BLAKE2B_KEYBYTES - len);
            return 1;

        case EVP_MAC_CTRL_SET_CUSTOM:
            p = va_arg(args, const unsigned char *);
            len = va_arg(args, size_t);
            if (len > BLAKE2B_PERSONALBYTES) {
                EVPerr(EVP_F_BLAKE2B_MAC_CTRL, EVP_R_INVALID_CUSTOM_LENGTH);
                return 0;
            }
            blake2b_param_set_personal(&macctx->params, p, len);
            return 1;

        case EVP_MAC_CTRL_SET_SALT:
            p = va_arg(args, const unsigned char *);
            len = va_arg(args, size_t);
            if (len > BLAKE2B_SALTBYTES) {
                EVPerr(EVP_F_BLAKE2B_MAC_CTRL, EVP_R_INVALID_SALT_LENGTH);
                return 0;
            }
            blake2b_param_set_salt(&macctx->params, p, len);
            return 1;

        default:
            return -2;
    }
}

static int blake2b_mac_ctrl_int(EVP_MAC_IMPL *macctx, int cmd, ...)
{
    int rv;
    va_list args;

    va_start(args, cmd);
    rv = blake2b_mac_ctrl(macctx, cmd, args);
    va_end(args);

    return rv;
}

static int blake2b_mac_ctrl_str_cb(void *macctx, int cmd, void *buf, size_t buflen)
{
    return blake2b_mac_ctrl_int(macctx, cmd, buf, buflen);
}

static int blake2b_mac_ctrl_str(EVP_MAC_IMPL *macctx, const char *type,
                                const char *value)
{
    if (value == NULL)
        return 0;

    if (strcmp(type, "outlen") == 0)
        return blake2b_mac_ctrl_int(macctx, EVP_MAC_CTRL_SET_SIZE, (size_t)atoi(value));
    if (strcmp(type, "key") == 0)
        return EVP_str2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
                            value);
    if (strcmp(type, "hexkey") == 0)
        return EVP_hex2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
                            value);
    if (strcmp(type, "custom") == 0)
        return EVP_str2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
                            value);
    if (strcmp(type, "hexcustom") == 0)
        return EVP_hex2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
                            value);
    if (strcmp(type, "salt") == 0)
        return EVP_str2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
                            value);
    if (strcmp(type, "hexsalt") == 0)
        return EVP_hex2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
                            value);
    return -2;
}

static size_t blake2b_mac_size(EVP_MAC_IMPL *macctx)
{
    return macctx->params.digest_length;
}

const EVP_MAC blake2b_mac_meth = {
    EVP_MAC_BLAKE2B,
    blake2b_mac_new,
    blake2b_mac_copy,
    blake2b_mac_free,
    blake2b_mac_size,
    blake2b_mac_init,
    blake2b_mac_update,
    blake2b_mac_final,
    blake2b_mac_ctrl,
    blake2b_mac_ctrl_str
};

#endif