Implement BLAKE2s with the same macro as BLAKE2b
authorнаб <nabijaczleweli@nabijaczleweli.xyz>
Thu, 19 Oct 2023 16:31:33 +0000 (18:31 +0200)
committerTomas Mraz <tomas@openssl.org>
Wed, 8 Nov 2023 08:42:13 +0000 (09:42 +0100)
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 https://github.com/openssl/openssl/pull/22444)

providers/implementations/digests/blake2_prov.c
providers/implementations/include/prov/blake2.h
test/recipes/30-test_evp_data/evpmd_blake.txt

index f80d307321199d7a45f5005b243ee8dd3deeb4eb..4178d0554d3ed554b278f8510b3c8e4dbf7da156 100644 (file)
 #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}, \
@@ -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)
index 445fd89aa21b5f958ac33a8934bebab9739741a8..42229e2d740288d571acd0cfd3f0e3ae5e8c07a0 100644 (file)
@@ -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);
@@ -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 */
index 474e659142981bae07ca455e3cdc03671ed11766..949de2f783a0f419680a642021067291e65aa2ad 100644 (file)
@@ -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