Skip to content

Commit

Permalink
Implement BLAKE2s with the same macro as BLAKE2b
Browse files Browse the repository at this point in the history
This avoids code duplication and provides variable-size support
for BLAKE2s like 786b9a8

Test data obtained with libb2 with the following programs:

	==> b2.c <==
	#include <blake2.h>
	#include <unistd.h>

	int main() {
		char buf[16] = {};
		blake2s(buf, 0, 0, 16, 0, 0);
		write(1, buf, 16);
	}

	==> b3.c <==
	#include <blake2.h>
	#include <unistd.h>

	int main() {
		char buf[10] = {};
		blake2s(buf, "\x61", 0, 10, 1, 0);
		write(1, buf, 10);
	}

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22444)
  • Loading branch information
nabijaczleweli authored and t8m committed Nov 8, 2023
1 parent 8349c02 commit 6d1e730
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
17 changes: 1 addition & 16 deletions providers/implementations/digests/blake2_prov.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,6 @@
#include "prov/digestcommon.h"
#include "prov/implementations.h"

static int ossl_blake2s256_init(void *ctx)
{
BLAKE2S_PARAM P;

ossl_blake2s_param_init(&P);
return ossl_blake2s_init((BLAKE2S_CTX *)ctx, &P);
}

/* ossl_blake2s256_functions */
IMPLEMENT_digest_functions(blake2s256, BLAKE2S_CTX,
BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, 0,
ossl_blake2s256_init, ossl_blake2s_update,
ossl_blake2s_final)

/* ossl_blake2b512_functions */

#define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize) \
static const OSSL_PARAM known_blake##variant##_ctx_params[] = { \
{OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0}, \
Expand Down Expand Up @@ -200,4 +184,5 @@ const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = { \
{0, NULL} \
};

IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)
10 changes: 10 additions & 0 deletions providers/implementations/include/prov/blake2.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ struct blake2b_md_data_st {
BLAKE2B_PARAM params;
};

struct blake2s_md_data_st {
BLAKE2S_CTX ctx;
BLAKE2S_PARAM params;
};

int ossl_blake2b_init(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P);
int ossl_blake2b_init_key(BLAKE2B_CTX *c, const BLAKE2B_PARAM *P,
const void *key);
Expand Down Expand Up @@ -125,4 +130,9 @@ void ossl_blake2s_param_set_personal(BLAKE2S_PARAM *P, const uint8_t *personal,
void ossl_blake2s_param_set_salt(BLAKE2S_PARAM *P, const uint8_t *salt,
size_t length);

OSSL_FUNC_digest_get_ctx_params_fn ossl_blake2s_get_ctx_params;
OSSL_FUNC_digest_set_ctx_params_fn ossl_blake2s_set_ctx_params;
OSSL_FUNC_digest_gettable_ctx_params_fn ossl_blake2s_gettable_ctx_params;
OSSL_FUNC_digest_settable_ctx_params_fn ossl_blake2s_settable_ctx_params;

#endif /* OSSL_PROV_BLAKE2_H */
10 changes: 10 additions & 0 deletions test/recipes/30-test_evp_data/evpmd_blake.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ Digest = BLAKE2s256
Input = 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F8081
Output = C80ABEEBB669AD5DEEB5F5EC8EA6B7A05DDF7D31EC4C0A2EE20B0B98CAEC6746

Digest = BLAKE2s256
Input =
OutputSize = 16
Output = 64550d6ffe2c0a01a14aba1eade0200c

Digest = BLAKE2s256
Input = 61
OutputSize = 10
Output = b60d322755eebca92b5e

Digest = BLAKE2b512
Input =
Output = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce
Expand Down

0 comments on commit 6d1e730

Please sign in to comment.