Latest update and remove TLSv1.3 draft
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2016-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
|
||||
@@ -49,6 +49,7 @@ struct blake2s_ctx_st {
|
||||
uint32_t f[2];
|
||||
uint8_t buf[BLAKE2S_BLOCKBYTES];
|
||||
size_t buflen;
|
||||
size_t outlen;
|
||||
};
|
||||
|
||||
struct blake2b_param_st {
|
||||
@@ -73,6 +74,7 @@ struct blake2b_ctx_st {
|
||||
uint64_t f[2];
|
||||
uint8_t buf[BLAKE2B_BLOCKBYTES];
|
||||
size_t buflen;
|
||||
size_t outlen;
|
||||
};
|
||||
|
||||
#define BLAKE2B_DIGEST_LENGTH 64
|
||||
@@ -81,10 +83,29 @@ struct blake2b_ctx_st {
|
||||
typedef struct blake2s_ctx_st BLAKE2S_CTX;
|
||||
typedef struct blake2b_ctx_st BLAKE2B_CTX;
|
||||
|
||||
int BLAKE2b_Init(BLAKE2B_CTX *c);
|
||||
int BLAKE2b_Init(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P);
|
||||
int BLAKE2b_Init_key(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P, const void *key);
|
||||
int BLAKE2b_Update(BLAKE2B_CTX *c, const void *data, size_t datalen);
|
||||
int BLAKE2b_Final(unsigned char *md, BLAKE2B_CTX *c);
|
||||
|
||||
int BLAKE2s_Init(BLAKE2S_CTX *c);
|
||||
/*
|
||||
* These setters are internal and do not check the validity of their parameters.
|
||||
* See blake2b_mac_ctrl for validation logic.
|
||||
*/
|
||||
|
||||
void blake2b_param_init(BLAKE2B_PARAM *P);
|
||||
void blake2b_param_set_digest_length(BLAKE2B_PARAM *P, uint8_t outlen);
|
||||
void blake2b_param_set_key_length(BLAKE2B_PARAM *P, uint8_t keylen);
|
||||
void blake2b_param_set_personal(BLAKE2B_PARAM *P, const uint8_t *personal, size_t length);
|
||||
void blake2b_param_set_salt(BLAKE2B_PARAM *P, const uint8_t *salt, size_t length);
|
||||
|
||||
int BLAKE2s_Init(BLAKE2S_CTX *c, const BLAKE2S_PARAM *P);
|
||||
int BLAKE2s_Init_key(BLAKE2S_CTX *c, const BLAKE2S_PARAM *P, const void *key);
|
||||
int BLAKE2s_Update(BLAKE2S_CTX *c, const void *data, size_t datalen);
|
||||
int BLAKE2s_Final(unsigned char *md, BLAKE2S_CTX *c);
|
||||
|
||||
void blake2s_param_init(BLAKE2S_PARAM *P);
|
||||
void blake2s_param_set_digest_length(BLAKE2S_PARAM *P, uint8_t outlen);
|
||||
void blake2s_param_set_key_length(BLAKE2S_PARAM *P, uint8_t keylen);
|
||||
void blake2s_param_set_personal(BLAKE2S_PARAM *P, const uint8_t *personal, size_t length);
|
||||
void blake2s_param_set_salt(BLAKE2S_PARAM *P, const uint8_t *salt, size_t length);
|
||||
+69
-9
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2016-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
|
||||
@@ -62,12 +62,14 @@ static ossl_inline void blake2b_init0(BLAKE2B_CTX *S)
|
||||
}
|
||||
}
|
||||
|
||||
/* init xors IV with input parameter block */
|
||||
/* init xors IV with input parameter block and sets the output length */
|
||||
static void blake2b_init_param(BLAKE2B_CTX *S, const BLAKE2B_PARAM *P)
|
||||
{
|
||||
size_t i;
|
||||
const uint8_t *p = (const uint8_t *)(P);
|
||||
|
||||
blake2b_init0(S);
|
||||
S->outlen = P->digest_length;
|
||||
|
||||
/* The param struct is carefully hand packed, and should be 64 bytes on
|
||||
* every platform. */
|
||||
@@ -78,10 +80,9 @@ static void blake2b_init_param(BLAKE2B_CTX *S, const BLAKE2B_PARAM *P)
|
||||
}
|
||||
}
|
||||
|
||||
/* Initialize the hashing context. Always returns 1. */
|
||||
int BLAKE2b_Init(BLAKE2B_CTX *c)
|
||||
/* Initialize the parameter block with default values */
|
||||
void blake2b_param_init(BLAKE2B_PARAM *P)
|
||||
{
|
||||
BLAKE2B_PARAM P[1];
|
||||
P->digest_length = BLAKE2B_DIGEST_LENGTH;
|
||||
P->key_length = 0;
|
||||
P->fanout = 1;
|
||||
@@ -93,10 +94,60 @@ int BLAKE2b_Init(BLAKE2B_CTX *c)
|
||||
memset(P->reserved, 0, sizeof(P->reserved));
|
||||
memset(P->salt, 0, sizeof(P->salt));
|
||||
memset(P->personal, 0, sizeof(P->personal));
|
||||
}
|
||||
|
||||
void blake2b_param_set_digest_length(BLAKE2B_PARAM *P, uint8_t outlen)
|
||||
{
|
||||
P->digest_length = outlen;
|
||||
}
|
||||
|
||||
void blake2b_param_set_key_length(BLAKE2B_PARAM *P, uint8_t keylen)
|
||||
{
|
||||
P->key_length = keylen;
|
||||
}
|
||||
|
||||
void blake2b_param_set_personal(BLAKE2B_PARAM *P, const uint8_t *personal, size_t len)
|
||||
{
|
||||
memcpy(P->personal, personal, len);
|
||||
memset(P->personal + len, 0, BLAKE2B_PERSONALBYTES - len);
|
||||
}
|
||||
|
||||
void blake2b_param_set_salt(BLAKE2B_PARAM *P, const uint8_t *salt, size_t len)
|
||||
{
|
||||
memcpy(P->salt, salt, len);
|
||||
memset(P->salt + len, 0, BLAKE2B_SALTBYTES - len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the hashing context with the given parameter block.
|
||||
* Always returns 1.
|
||||
*/
|
||||
int BLAKE2b_Init(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P)
|
||||
{
|
||||
blake2b_init_param(c, P);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the hashing context with the given parameter block and key.
|
||||
* Always returns 1.
|
||||
*/
|
||||
int BLAKE2b_Init_key(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P, const void *key)
|
||||
{
|
||||
blake2b_init_param(c, P);
|
||||
|
||||
/* Pad the key to form first data block */
|
||||
{
|
||||
uint8_t block[BLAKE2B_BLOCKBYTES] = {0};
|
||||
|
||||
memcpy(block, key, P->key_length);
|
||||
BLAKE2b_Update(c, block, BLAKE2B_BLOCKBYTES);
|
||||
OPENSSL_cleanse(block, BLAKE2B_BLOCKBYTES);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Permute the state while xoring in the block of data. */
|
||||
static void blake2b_compress(BLAKE2B_CTX *S,
|
||||
const uint8_t *blocks,
|
||||
@@ -252,17 +303,26 @@ int BLAKE2b_Update(BLAKE2B_CTX *c, const void *data, size_t datalen)
|
||||
*/
|
||||
int BLAKE2b_Final(unsigned char *md, BLAKE2B_CTX *c)
|
||||
{
|
||||
uint8_t outbuffer[BLAKE2B_OUTBYTES] = {0};
|
||||
uint8_t *target = outbuffer;
|
||||
int iter = (c->outlen + 7) / 8;
|
||||
int i;
|
||||
|
||||
/* Avoid writing to the temporary buffer if possible */
|
||||
if ((c->outlen % sizeof(c->h[0])) == 0)
|
||||
target = md;
|
||||
|
||||
blake2b_set_lastblock(c);
|
||||
/* Padding */
|
||||
memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
|
||||
blake2b_compress(c, c->buf, c->buflen);
|
||||
|
||||
/* Output full hash to message digest */
|
||||
for (i = 0; i < 8; ++i) {
|
||||
store64(md + sizeof(c->h[i]) * i, c->h[i]);
|
||||
}
|
||||
/* Output full hash to buffer */
|
||||
for (i = 0; i < iter; ++i)
|
||||
store64(target + sizeof(c->h[i]) * i, c->h[i]);
|
||||
|
||||
if (target != md)
|
||||
memcpy(md, target, c->outlen);
|
||||
|
||||
OPENSSL_cleanse(c, sizeof(BLAKE2B_CTX));
|
||||
return 1;
|
||||
|
||||
@@ -0,0 +1,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
|
||||
+69
-12
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2016-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
|
||||
@@ -58,27 +58,26 @@ static ossl_inline void blake2s_init0(BLAKE2S_CTX *S)
|
||||
}
|
||||
}
|
||||
|
||||
/* init2 xors IV with input parameter block */
|
||||
/* init xors IV with input parameter block and sets the output length */
|
||||
static void blake2s_init_param(BLAKE2S_CTX *S, const BLAKE2S_PARAM *P)
|
||||
{
|
||||
const uint8_t *p = (const uint8_t *)(P);
|
||||
size_t i;
|
||||
const uint8_t *p = (const uint8_t *)(P);
|
||||
|
||||
blake2s_init0(S);
|
||||
S->outlen = P->digest_length;
|
||||
|
||||
/* The param struct is carefully hand packed, and should be 32 bytes on
|
||||
* every platform. */
|
||||
assert(sizeof(BLAKE2S_PARAM) == 32);
|
||||
blake2s_init0(S);
|
||||
/* IV XOR ParamBlock */
|
||||
for (i = 0; i < 8; ++i) {
|
||||
S->h[i] ^= load32(&p[i*4]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Initialize the hashing context. Always returns 1. */
|
||||
int BLAKE2s_Init(BLAKE2S_CTX *c)
|
||||
void blake2s_param_init(BLAKE2S_PARAM *P)
|
||||
{
|
||||
BLAKE2S_PARAM P[1];
|
||||
|
||||
P->digest_length = BLAKE2S_DIGEST_LENGTH;
|
||||
P->key_length = 0;
|
||||
P->fanout = 1;
|
||||
@@ -89,10 +88,59 @@ int BLAKE2s_Init(BLAKE2S_CTX *c)
|
||||
P->inner_length = 0;
|
||||
memset(P->salt, 0, sizeof(P->salt));
|
||||
memset(P->personal, 0, sizeof(P->personal));
|
||||
}
|
||||
|
||||
void blake2s_param_set_digest_length(BLAKE2S_PARAM *P, uint8_t outlen)
|
||||
{
|
||||
P->digest_length = outlen;
|
||||
}
|
||||
|
||||
void blake2s_param_set_key_length(BLAKE2S_PARAM *P, uint8_t keylen)
|
||||
{
|
||||
P->key_length = keylen;
|
||||
}
|
||||
|
||||
void blake2s_param_set_personal(BLAKE2S_PARAM *P, const uint8_t *personal, size_t len)
|
||||
{
|
||||
memcpy(P->personal, personal, len);
|
||||
memset(P->personal + len, 0, BLAKE2S_PERSONALBYTES - len);
|
||||
}
|
||||
|
||||
void blake2s_param_set_salt(BLAKE2S_PARAM *P, const uint8_t *salt, size_t len)
|
||||
{
|
||||
memcpy(P->salt, salt, len);
|
||||
memset(P->salt + len, 0, BLAKE2S_SALTBYTES - len);}
|
||||
|
||||
/*
|
||||
* Initialize the hashing context with the given parameter block.
|
||||
* Always returns 1.
|
||||
*/
|
||||
int BLAKE2s_Init(BLAKE2S_CTX *c, const BLAKE2S_PARAM *P)
|
||||
{
|
||||
blake2s_init_param(c, P);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the hashing context with the given parameter block and key.
|
||||
* Always returns 1.
|
||||
*/
|
||||
int BLAKE2s_Init_key(BLAKE2S_CTX *c, const BLAKE2S_PARAM *P, const void *key)
|
||||
{
|
||||
blake2s_init_param(c, P);
|
||||
|
||||
/* Pad the key to form first data block */
|
||||
{
|
||||
uint8_t block[BLAKE2S_BLOCKBYTES] = {0};
|
||||
|
||||
memcpy(block, key, P->key_length);
|
||||
BLAKE2s_Update(c, block, BLAKE2S_BLOCKBYTES);
|
||||
OPENSSL_cleanse(block, BLAKE2S_BLOCKBYTES);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Permute the state while xoring in the block of data. */
|
||||
static void blake2s_compress(BLAKE2S_CTX *S,
|
||||
const uint8_t *blocks,
|
||||
@@ -246,17 +294,26 @@ int BLAKE2s_Update(BLAKE2S_CTX *c, const void *data, size_t datalen)
|
||||
*/
|
||||
int BLAKE2s_Final(unsigned char *md, BLAKE2S_CTX *c)
|
||||
{
|
||||
uint8_t outbuffer[BLAKE2S_OUTBYTES] = {0};
|
||||
uint8_t *target = outbuffer;
|
||||
int iter = (c->outlen + 3) / 4;
|
||||
int i;
|
||||
|
||||
/* Avoid writing to the temporary buffer if possible */
|
||||
if ((c->outlen % sizeof(c->h[0])) == 0)
|
||||
target = md;
|
||||
|
||||
blake2s_set_lastblock(c);
|
||||
/* Padding */
|
||||
memset(c->buf + c->buflen, 0, sizeof(c->buf) - c->buflen);
|
||||
blake2s_compress(c, c->buf, c->buflen);
|
||||
|
||||
/* Output full hash to temp buffer */
|
||||
for (i = 0; i < 8; ++i) {
|
||||
store32(md + sizeof(c->h[i]) * i, c->h[i]);
|
||||
}
|
||||
/* Output full hash to buffer */
|
||||
for (i = 0; i < iter; ++i)
|
||||
store32(target + sizeof(c->h[i]) * i, c->h[i]);
|
||||
|
||||
if (target != md)
|
||||
memcpy(md, target, c->outlen);
|
||||
|
||||
OPENSSL_cleanse(c, sizeof(BLAKE2S_CTX));
|
||||
return 1;
|
||||
|
||||
@@ -0,0 +1,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 {
|
||||
BLAKE2S_CTX ctx;
|
||||
BLAKE2S_PARAM params;
|
||||
unsigned char key[BLAKE2S_KEYBYTES];
|
||||
};
|
||||
|
||||
static EVP_MAC_IMPL *blake2s_mac_new(void)
|
||||
{
|
||||
EVP_MAC_IMPL *macctx = OPENSSL_zalloc(sizeof(*macctx));
|
||||
if (macctx != NULL) {
|
||||
blake2s_param_init(&macctx->params);
|
||||
/* ctx initialization is deferred to BLAKE2s_Init() */
|
||||
}
|
||||
return macctx;
|
||||
}
|
||||
|
||||
static void blake2s_mac_free(EVP_MAC_IMPL *macctx)
|
||||
{
|
||||
if (macctx != NULL) {
|
||||
OPENSSL_cleanse(macctx->key, sizeof(macctx->key));
|
||||
OPENSSL_free(macctx);
|
||||
}
|
||||
}
|
||||
|
||||
static int blake2s_mac_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src)
|
||||
{
|
||||
*dst = *src;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int blake2s_mac_init(EVP_MAC_IMPL *macctx)
|
||||
{
|
||||
/* Check key has been set */
|
||||
if (macctx->params.key_length == 0) {
|
||||
EVPerr(EVP_F_BLAKE2S_MAC_INIT, EVP_R_NO_KEY_SET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return BLAKE2s_Init_key(&macctx->ctx, &macctx->params, macctx->key);
|
||||
}
|
||||
|
||||
static int blake2s_mac_update(EVP_MAC_IMPL *macctx, const unsigned char *data,
|
||||
size_t datalen)
|
||||
{
|
||||
return BLAKE2s_Update(&macctx->ctx, data, datalen);
|
||||
}
|
||||
|
||||
static int blake2s_mac_final(EVP_MAC_IMPL *macctx, unsigned char *out)
|
||||
{
|
||||
return BLAKE2s_Final(out, &macctx->ctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* ALL Ctrl functions should be set before init().
|
||||
*/
|
||||
static int blake2s_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 > BLAKE2S_OUTBYTES) {
|
||||
EVPerr(EVP_F_BLAKE2S_MAC_CTRL, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
blake2s_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 > BLAKE2S_KEYBYTES) {
|
||||
EVPerr(EVP_F_BLAKE2S_MAC_CTRL, EVP_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
blake2s_param_set_key_length(&macctx->params, (uint8_t)len);
|
||||
memcpy(macctx->key, p, len);
|
||||
memset(macctx->key + len, 0, BLAKE2S_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 > BLAKE2S_PERSONALBYTES) {
|
||||
EVPerr(EVP_F_BLAKE2S_MAC_CTRL, EVP_R_INVALID_CUSTOM_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
blake2s_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 > BLAKE2S_SALTBYTES) {
|
||||
EVPerr(EVP_F_BLAKE2S_MAC_CTRL, EVP_R_INVALID_SALT_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
blake2s_param_set_salt(&macctx->params, p, len);
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
static int blake2s_mac_ctrl_int(EVP_MAC_IMPL *macctx, int cmd, ...)
|
||||
{
|
||||
int rv;
|
||||
va_list args;
|
||||
|
||||
va_start(args, cmd);
|
||||
rv = blake2s_mac_ctrl(macctx, cmd, args);
|
||||
va_end(args);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static int blake2s_mac_ctrl_str_cb(void *macctx, int cmd, void *buf, size_t buflen)
|
||||
{
|
||||
return blake2s_mac_ctrl_int(macctx, cmd, buf, buflen);
|
||||
}
|
||||
|
||||
static int blake2s_mac_ctrl_str(EVP_MAC_IMPL *macctx, const char *type,
|
||||
const char *value)
|
||||
{
|
||||
if (value == NULL)
|
||||
return 0;
|
||||
|
||||
if (strcmp(type, "outlen") == 0)
|
||||
return blake2s_mac_ctrl_int(macctx, EVP_MAC_CTRL_SET_SIZE, (size_t)atoi(value));
|
||||
if (strcmp(type, "key") == 0)
|
||||
return EVP_str2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
|
||||
value);
|
||||
if (strcmp(type, "hexkey") == 0)
|
||||
return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_KEY,
|
||||
value);
|
||||
if (strcmp(type, "custom") == 0)
|
||||
return EVP_str2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
|
||||
value);
|
||||
if (strcmp(type, "hexcustom") == 0)
|
||||
return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_CUSTOM,
|
||||
value);
|
||||
if (strcmp(type, "salt") == 0)
|
||||
return EVP_str2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
|
||||
value);
|
||||
if (strcmp(type, "hexsalt") == 0)
|
||||
return EVP_hex2ctrl(blake2s_mac_ctrl_str_cb, macctx, EVP_MAC_CTRL_SET_SALT,
|
||||
value);
|
||||
return -2;
|
||||
}
|
||||
|
||||
static size_t blake2s_mac_size(EVP_MAC_IMPL *macctx)
|
||||
{
|
||||
return macctx->params.digest_length;
|
||||
}
|
||||
|
||||
const EVP_MAC blake2s_mac_meth = {
|
||||
EVP_MAC_BLAKE2S,
|
||||
blake2s_mac_new,
|
||||
blake2s_mac_copy,
|
||||
blake2s_mac_free,
|
||||
blake2s_mac_size,
|
||||
blake2s_mac_init,
|
||||
blake2s_mac_update,
|
||||
blake2s_mac_final,
|
||||
blake2s_mac_ctrl,
|
||||
blake2s_mac_ctrl_str
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,3 @@
|
||||
LIBS=../../libcrypto
|
||||
SOURCE[../../libcrypto]=\
|
||||
blake2b.c blake2s.c m_blake2b.c m_blake2s.c
|
||||
blake2b.c blake2s.c blake2b_mac.c blake2s_mac.c m_blake2b.c m_blake2s.c
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2016-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
|
||||
@@ -25,7 +25,9 @@
|
||||
|
||||
static int init(EVP_MD_CTX *ctx)
|
||||
{
|
||||
return BLAKE2b_Init(EVP_MD_CTX_md_data(ctx));
|
||||
BLAKE2B_PARAM P;
|
||||
blake2b_param_init(&P);
|
||||
return BLAKE2b_Init(EVP_MD_CTX_md_data(ctx), &P);
|
||||
}
|
||||
|
||||
static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
|
||||
@@ -49,7 +51,7 @@ static const EVP_MD blake2b_md = {
|
||||
NULL,
|
||||
NULL,
|
||||
BLAKE2B_BLOCKBYTES,
|
||||
sizeof(EVP_MD *) + sizeof(BLAKE2B_CTX),
|
||||
sizeof(BLAKE2B_CTX),
|
||||
};
|
||||
|
||||
const EVP_MD *EVP_blake2b512(void)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2016-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
|
||||
@@ -25,7 +25,9 @@
|
||||
|
||||
static int init(EVP_MD_CTX *ctx)
|
||||
{
|
||||
return BLAKE2s_Init(EVP_MD_CTX_md_data(ctx));
|
||||
BLAKE2S_PARAM P;
|
||||
blake2s_param_init(&P);
|
||||
return BLAKE2s_Init(EVP_MD_CTX_md_data(ctx), &P);
|
||||
}
|
||||
|
||||
static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
|
||||
@@ -49,7 +51,7 @@ static const EVP_MD blake2s_md = {
|
||||
NULL,
|
||||
NULL,
|
||||
BLAKE2S_BLOCKBYTES,
|
||||
sizeof(EVP_MD *) + sizeof(BLAKE2S_CTX),
|
||||
sizeof(BLAKE2S_CTX),
|
||||
};
|
||||
|
||||
const EVP_MD *EVP_blake2s256(void)
|
||||
|
||||
Reference in New Issue
Block a user