tests: Integrate Argon2 tests into functional_test

Refactor the Argon2 tests to enable the module build and integrate the
tests into function_test.

Signed-off-by: Gary Lin <glin@suse.com>
Tested-By: Waldemar Brodkorb <wbx@openadk.org>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Gary Lin 2025-10-15 11:00:25 +08:00 committed by Daniel Kiper
parent 6a525ee64f
commit c1bd9fc829
3 changed files with 57 additions and 53 deletions

View File

@ -2265,6 +2265,11 @@ module = {
common = tests/pbkdf2_test.c;
};
module = {
name = argon2_test;
common = tests/argon2_test.c;
};
module = {
name = legacy_password_test;
common = tests/legacy_password_test.c;

View File

@ -1,23 +1,50 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2025 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/test.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/crypto.h>
GRUB_MOD_LICENSE ("GPLv3+");
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
static void
check_argon2 (void)
argon2_test (void)
{
gcry_error_t err;
static struct {
int subalgo;
unsigned long param[4];
size_t passlen;
grub_size_t passlen;
const char *pass;
size_t saltlen;
grub_size_t saltlen;
const char *salt;
size_t keylen;
grub_size_t keylen;
const char *key;
size_t adlen;
grub_size_t adlen;
const char *ad;
size_t dklen;
grub_size_t dklen;
const char *dk;
} tv[] = {
{
GCRY_KDF_ARGON2D,
GRUB_GCRY_KDF_ARGON2D,
{ 32, 3, 32, 4 },
32,
"\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
@ -33,7 +60,7 @@ check_argon2 (void)
"\xf8\x68\xe3\xbe\x39\x84\xf3\xc1\xa1\x3a\x4d\xb9\xfa\xbe\x4a\xcb"
},
{
GCRY_KDF_ARGON2I,
GRUB_GCRY_KDF_ARGON2I,
{ 32, 3, 32, 4 },
32,
"\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
@ -49,7 +76,7 @@ check_argon2 (void)
"\xc8\xde\x6b\x01\x6d\xd3\x88\xd2\x99\x52\xa4\xc4\x67\x2b\x6c\xe8"
},
{
GCRY_KDF_ARGON2ID,
GRUB_GCRY_KDF_ARGON2ID,
{ 32, 3, 32, 4 },
32,
"\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
@ -66,7 +93,7 @@ check_argon2 (void)
},
{
/* empty password */
GCRY_KDF_ARGON2I,
GRUB_GCRY_KDF_ARGON2I,
{ 32, 3, 128, 1 },
0, NULL,
16,
@ -79,7 +106,7 @@ check_argon2 (void)
},
{
/* empty password */
GCRY_KDF_ARGON2ID,
GRUB_GCRY_KDF_ARGON2ID,
{ 32, 3, 128, 1 },
0, NULL,
16,
@ -92,50 +119,21 @@ check_argon2 (void)
},
};
unsigned char out[32];
int i;
int count;
unsigned int count;
for (count = 0; count < DIM(tv); count++)
{
if (verbose)
fprintf (stderr, "checking ARGON2 test vector %d\n", count);
err = my_kdf_derive (0, GCRY_KDF_ARGON2,
tv[count].subalgo, tv[count].param, 4,
tv[count].pass, tv[count].passlen,
tv[count].salt, tv[count].saltlen,
tv[count].key, tv[count].keylen,
tv[count].ad, tv[count].adlen,
tv[count].dklen, out);
if (err)
fail ("argon2 test %d failed: %s\n", count*2+0, gpg_strerror (err));
else if (memcmp (out, tv[count].dk, tv[count].dklen))
{
fail ("argon2 test %d failed: mismatch\n", count*2+0);
fputs ("got:", stderr);
for (i=0; i < tv[count].dklen; i++)
fprintf (stderr, " %02x", out[i]);
putc ('\n', stderr);
}
#ifdef HAVE_PTHREAD
err = my_kdf_derive (1, GCRY_KDF_ARGON2,
tv[count].subalgo, tv[count].param, 4,
tv[count].pass, tv[count].passlen,
tv[count].salt, tv[count].saltlen,
tv[count].key, tv[count].keylen,
tv[count].ad, tv[count].adlen,
tv[count].dklen, out);
if (err)
fail ("argon2 test %d failed: %s\n", count*2+1, gpg_strerror (err));
else if (memcmp (out, tv[count].dk, tv[count].dklen))
{
fail ("argon2 test %d failed: mismatch\n", count*2+1);
fputs ("got:", stderr);
for (i=0; i < tv[count].dklen; i++)
fprintf (stderr, " %02x", out[i]);
putc ('\n', stderr);
}
#endif
err = grub_crypto_argon2 (tv[count].subalgo,
tv[count].param, 4,
tv[count].pass, tv[count].passlen,
tv[count].salt, tv[count].saltlen,
tv[count].key, tv[count].keylen,
tv[count].ad, tv[count].adlen,
tv[count].dklen, out);
grub_test_assert (err == 0, "argon2 test %d failed: %d", count, err);
grub_test_assert (grub_memcmp (out, tv[count].dk, tv[count].dklen) == 0,
"argon2 test %d failed: mismatch", count);
}
}
GRUB_FUNCTIONAL_TEST (argon2_test, argon2_test);

View File

@ -81,6 +81,7 @@ grub_functional_all_tests (grub_extcmd_context_t ctxt __attribute__ ((unused)),
grub_dl_load ("mul_test");
grub_dl_load ("shift_test");
grub_dl_load ("asn1_test");
grub_dl_load ("argon2_test");
FOR_LIST_ELEMENTS (test, grub_test_list)
ok = !grub_test_run (test) && ok;