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 "blake2_locl.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[BLAKE2B_KEYBYTES];
24 static EVP_MAC_IMPL *blake2b_mac_new(void)
26 EVP_MAC_IMPL *macctx = OPENSSL_zalloc(sizeof(*macctx));
28 blake2b_param_init(&macctx->params);
29 /* ctx initialization is deferred to BLAKE2b_Init() */
34 static void blake2b_mac_free(EVP_MAC_IMPL *macctx)
37 OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
42 static int blake2b_mac_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src)
48 static int blake2b_mac_init(EVP_MAC_IMPL *macctx)
50 /* Check key has been set */
51 if (macctx->params.key_length == 0) {
52 EVPerr(EVP_F_BLAKE2B_MAC_INIT, EVP_R_NO_KEY_SET);
56 return BLAKE2b_Init_key(&macctx->ctx, &macctx->params, macctx->key);
59 static int blake2b_mac_update(EVP_MAC_IMPL *macctx, const unsigned char *data,
62 return BLAKE2b_Update(&macctx->ctx, data, datalen);
65 static int blake2b_mac_final(EVP_MAC_IMPL *macctx, unsigned char *out)
67 return BLAKE2b_Final(out, &macctx->ctx);
71 * ALL Ctrl functions should be set before init().
73 static int blake2b_mac_ctrl(EVP_MAC_IMPL *macctx, int cmd, va_list args)
75 const unsigned char *p;
80 case EVP_MAC_CTRL_SET_SIZE:
81 size = va_arg(args, size_t);
82 if (size < 1 || size > BLAKE2B_OUTBYTES) {
83 EVPerr(EVP_F_BLAKE2B_MAC_CTRL, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
86 blake2b_param_set_digest_length(&macctx->params, (uint8_t)size);
89 case EVP_MAC_CTRL_SET_KEY:
90 p = va_arg(args, const unsigned char *);
91 len = va_arg(args, size_t);
92 if (len < 1 || len > BLAKE2B_KEYBYTES) {
93 EVPerr(EVP_F_BLAKE2B_MAC_CTRL, EVP_R_INVALID_KEY_LENGTH);
96 blake2b_param_set_key_length(&macctx->params, (uint8_t)len);
97 memcpy(macctx->key, p, len);
98 memset(macctx->key + len, 0, BLAKE2B_KEYBYTES - len);
101 case EVP_MAC_CTRL_SET_CUSTOM:
102 p = va_arg(args, const unsigned char *);
103 len = va_arg(args, size_t);
104 if (len > BLAKE2B_PERSONALBYTES) {
105 EVPerr(EVP_F_BLAKE2B_MAC_CTRL, EVP_R_INVALID_CUSTOM_LENGTH);
108 blake2b_param_set_personal(&macctx->params, p, len);
111 case EVP_MAC_CTRL_SET_SALT:
112 p = va_arg(args, const unsigned char *);
113 len = va_arg(args, size_t);
114 if (len > BLAKE2B_SALTBYTES) {
115 EVPerr(EVP_F_BLAKE2B_MAC_CTRL, EVP_R_INVALID_SALT_LENGTH);
118 blake2b_param_set_salt(&macctx->params, p, len);
126 static int blake2b_mac_ctrl_int(EVP_MAC_IMPL *macctx, int cmd, ...)
132 rv = blake2b_mac_ctrl(macctx, cmd, args);
138 static int blake2b_mac_ctrl_str_cb(void *macctx, int cmd, void *buf, size_t buflen)
140 return blake2b_mac_ctrl_int(macctx, cmd, buf, buflen);
143 static int blake2b_mac_ctrl_str(EVP_MAC_IMPL *macctx, const char *type,
149 if (strcmp(type, "outlen") == 0)
150 return blake2b_mac_ctrl_int(macctx, EVP_MAC_CTRL_SET_SIZE, (size_t)atoi(value));
151 if (strcmp(type, "key") == 0)
152 return EVP_str2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
154 if (strcmp(type, "hexkey") == 0)
155 return EVP_hex2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
157 if (strcmp(type, "custom") == 0)
158 return EVP_str2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
160 if (strcmp(type, "hexcustom") == 0)
161 return EVP_hex2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
163 if (strcmp(type, "salt") == 0)
164 return EVP_str2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
166 if (strcmp(type, "hexsalt") == 0)
167 return EVP_hex2ctrl(blake2b_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
172 static size_t blake2b_mac_size(EVP_MAC_IMPL *macctx)
174 return macctx->params.digest_length;
177 const EVP_MAC blake2b_mac_meth = {