2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OPENSSL_NO_BLAKE2
12 # include <openssl/evp.h>
13 # include "internal/blake2.h"
14 # include "internal/cryptlib.h"
15 # include "internal/evp_int.h"
17 /* typedef EVP_MAC_IMPL */
18 struct evp_mac_impl_st {
21 unsigned char key[BLAKE2S_KEYBYTES];
24 static EVP_MAC_IMPL *blake2s_mac_new(void)
26 EVP_MAC_IMPL *macctx = OPENSSL_zalloc(sizeof(*macctx));
28 blake2s_param_init(&macctx->params);
29 /* ctx initialization is deferred to BLAKE2s_Init() */
34 static void blake2s_mac_free(EVP_MAC_IMPL *macctx)
37 OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
42 static EVP_MAC_IMPL *blake2s_mac_dup(const EVP_MAC_IMPL *src)
46 dst = OPENSSL_malloc(sizeof(*dst));
54 static int blake2s_mac_init(EVP_MAC_IMPL *macctx)
56 /* Check key has been set */
57 if (macctx->params.key_length == 0) {
58 EVPerr(EVP_F_BLAKE2S_MAC_INIT, EVP_R_NO_KEY_SET);
62 return blake2s_init_key(&macctx->ctx, &macctx->params, macctx->key);
65 static int blake2s_mac_update(EVP_MAC_IMPL *macctx, const unsigned char *data,
68 return blake2s_update(&macctx->ctx, data, datalen);
71 static int blake2s_mac_final(EVP_MAC_IMPL *macctx, unsigned char *out)
73 return blake2s_final(out, &macctx->ctx);
77 * ALL Ctrl functions should be set before init().
79 static int blake2s_mac_ctrl(EVP_MAC_IMPL *macctx, int cmd, va_list args)
81 const unsigned char *p;
86 case EVP_MAC_CTRL_SET_SIZE:
87 size = va_arg(args, size_t);
88 if (size < 1 || size > BLAKE2S_OUTBYTES) {
89 EVPerr(EVP_F_BLAKE2S_MAC_CTRL, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
92 blake2s_param_set_digest_length(&macctx->params, (uint8_t)size);
95 case EVP_MAC_CTRL_SET_KEY:
96 p = va_arg(args, const unsigned char *);
97 len = va_arg(args, size_t);
98 if (len < 1 || len > BLAKE2S_KEYBYTES) {
99 EVPerr(EVP_F_BLAKE2S_MAC_CTRL, EVP_R_INVALID_KEY_LENGTH);
102 blake2s_param_set_key_length(&macctx->params, (uint8_t)len);
103 memcpy(macctx->key, p, len);
104 memset(macctx->key + len, 0, BLAKE2S_KEYBYTES - len);
107 case EVP_MAC_CTRL_SET_CUSTOM:
108 p = va_arg(args, const unsigned char *);
109 len = va_arg(args, size_t);
110 if (len > BLAKE2S_PERSONALBYTES) {
111 EVPerr(EVP_F_BLAKE2S_MAC_CTRL, EVP_R_INVALID_CUSTOM_LENGTH);
114 blake2s_param_set_personal(&macctx->params, p, len);
117 case EVP_MAC_CTRL_SET_SALT:
118 p = va_arg(args, const unsigned char *);
119 len = va_arg(args, size_t);
120 if (len > BLAKE2S_SALTBYTES) {
121 EVPerr(EVP_F_BLAKE2S_MAC_CTRL, EVP_R_INVALID_SALT_LENGTH);
124 blake2s_param_set_salt(&macctx->params, p, len);
132 static int blake2s_mac_ctrl_int(EVP_MAC_IMPL *macctx, int cmd, ...)
138 rv = blake2s_mac_ctrl(macctx, cmd, args);
144 static int blake2s_mac_ctrl_str_cb(void *macctx, int cmd, void *buf, size_t buflen)
146 return blake2s_mac_ctrl_int(macctx, cmd, buf, buflen);
149 static int blake2s_mac_ctrl_str(EVP_MAC_IMPL *macctx, const char *type,
155 if (strcmp(type, "outlen") == 0)
156 return blake2s_mac_ctrl_int(macctx, EVP_MAC_CTRL_SET_SIZE, (size_t)atoi(value));
157 if (strcmp(type, "key") == 0)
158 return EVP_str2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
160 if (strcmp(type, "hexkey") == 0)
161 return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
163 if (strcmp(type, "custom") == 0)
164 return EVP_str2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
166 if (strcmp(type, "hexcustom") == 0)
167 return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
169 if (strcmp(type, "salt") == 0)
170 return EVP_str2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
172 if (strcmp(type, "hexsalt") == 0)
173 return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
178 static size_t blake2s_mac_size(EVP_MAC_IMPL *macctx)
180 return macctx->params.digest_length;
183 const EVP_MAC blake2s_mac_meth = {