]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/blake2/blake2s_mac.c
9ce8db13605beb4b5aaaa92c62c7b59391eaeb79
[thirdparty/openssl.git] / crypto / blake2 / blake2s_mac.c
1 /*
2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #ifndef OPENSSL_NO_BLAKE2
11
12 # include <openssl/evp.h>
13 # include "internal/blake2.h"
14 # include "internal/cryptlib.h"
15 # include "internal/evp_int.h"
16
17 /* typedef EVP_MAC_IMPL */
18 struct evp_mac_impl_st {
19 BLAKE2S_CTX ctx;
20 BLAKE2S_PARAM params;
21 unsigned char key[BLAKE2S_KEYBYTES];
22 };
23
24 static EVP_MAC_IMPL *blake2s_mac_new(void)
25 {
26 EVP_MAC_IMPL *macctx = OPENSSL_zalloc(sizeof(*macctx));
27 if (macctx != NULL) {
28 blake2s_param_init(&macctx->params);
29 /* ctx initialization is deferred to BLAKE2s_Init() */
30 }
31 return macctx;
32 }
33
34 static void blake2s_mac_free(EVP_MAC_IMPL *macctx)
35 {
36 if (macctx != NULL) {
37 OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
38 OPENSSL_free(macctx);
39 }
40 }
41
42 static EVP_MAC_IMPL *blake2s_mac_dup(const EVP_MAC_IMPL *src)
43 {
44 EVP_MAC_IMPL *dst;
45
46 dst = OPENSSL_malloc(sizeof(*dst));
47 if (dst == NULL)
48 return NULL;
49
50 *dst = *src;
51 return dst;
52 }
53
54 static int blake2s_mac_init(EVP_MAC_IMPL *macctx)
55 {
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);
59 return 0;
60 }
61
62 return blake2s_init_key(&macctx->ctx, &macctx->params, macctx->key);
63 }
64
65 static int blake2s_mac_update(EVP_MAC_IMPL *macctx, const unsigned char *data,
66 size_t datalen)
67 {
68 return blake2s_update(&macctx->ctx, data, datalen);
69 }
70
71 static int blake2s_mac_final(EVP_MAC_IMPL *macctx, unsigned char *out)
72 {
73 return blake2s_final(out, &macctx->ctx);
74 }
75
76 /*
77 * ALL Ctrl functions should be set before init().
78 */
79 static int blake2s_mac_ctrl(EVP_MAC_IMPL *macctx, int cmd, va_list args)
80 {
81 const unsigned char *p;
82 size_t len;
83 size_t size;
84
85 switch (cmd) {
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);
90 return 0;
91 }
92 blake2s_param_set_digest_length(&macctx->params, (uint8_t)size);
93 return 1;
94
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);
100 return 0;
101 }
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);
105 return 1;
106
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);
112 return 0;
113 }
114 blake2s_param_set_personal(&macctx->params, p, len);
115 return 1;
116
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);
122 return 0;
123 }
124 blake2s_param_set_salt(&macctx->params, p, len);
125 return 1;
126
127 default:
128 return -2;
129 }
130 }
131
132 static int blake2s_mac_ctrl_int(EVP_MAC_IMPL *macctx, int cmd, ...)
133 {
134 int rv;
135 va_list args;
136
137 va_start(args, cmd);
138 rv = blake2s_mac_ctrl(macctx, cmd, args);
139 va_end(args);
140
141 return rv;
142 }
143
144 static int blake2s_mac_ctrl_str_cb(void *macctx, int cmd, void *buf, size_t buflen)
145 {
146 return blake2s_mac_ctrl_int(macctx, cmd, buf, buflen);
147 }
148
149 static int blake2s_mac_ctrl_str(EVP_MAC_IMPL *macctx, const char *type,
150 const char *value)
151 {
152 if (value == NULL)
153 return 0;
154
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,
159 value);
160 if (strcmp(type, "hexkey") == 0)
161 return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
162 value);
163 if (strcmp(type, "custom") == 0)
164 return EVP_str2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
165 value);
166 if (strcmp(type, "hexcustom") == 0)
167 return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
168 value);
169 if (strcmp(type, "salt") == 0)
170 return EVP_str2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
171 value);
172 if (strcmp(type, "hexsalt") == 0)
173 return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
174 value);
175 return -2;
176 }
177
178 static size_t blake2s_mac_size(EVP_MAC_IMPL *macctx)
179 {
180 return macctx->params.digest_length;
181 }
182
183 const EVP_MAC blake2s_mac_meth = {
184 EVP_MAC_BLAKE2S,
185 blake2s_mac_new,
186 blake2s_mac_dup,
187 blake2s_mac_free,
188 blake2s_mac_size,
189 blake2s_mac_init,
190 blake2s_mac_update,
191 blake2s_mac_final,
192 blake2s_mac_ctrl,
193 blake2s_mac_ctrl_str
194 };
195
196 #endif