Skip to content

Commit 061ebce

Browse files
committed
CR fixes
1 parent aada875 commit 061ebce

File tree

3 files changed

+70
-60
lines changed

3 files changed

+70
-60
lines changed

crypto/dilithium/p_pqdsa_test.cc

+69-58
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ CMP_VEC_AND_PTR(vec, pkey->pkey.pqdsa_key->public_key, len)
354354
#define CMP_VEC_AND_PKEY_SECRET(vec, pkey, len) \
355355
CMP_VEC_AND_PTR(vec, pkey->pkey.pqdsa_key->private_key, len)
356356

357+
// The PQDSAParameterTest performs testing on each of the named algorithms
358+
// in the list PQDSA_types. This name must exactly match the name provided in
359+
// PQDSATestVector.name for the test vector to run.
360+
std::vector<std::string> PQDSA_types{"MLDSA65"};
361+
357362
static const struct PQDSATestVector parameterSet[] = {
358363
{"MLDSA65", NID_MLDSA65, 1952, 4032, 3309, "dilithium/kat/MLDSA_65_hedged_pure.txt", mldsa65kPublicKey, mldsa65kPublicKeySPKI, 1974},
359364
};
@@ -369,64 +374,70 @@ TEST_P(PQDSAParameterTest, KAT) {
369374
std::string kat_filepath = "crypto/";
370375
kat_filepath += GetParam().kat_filename;
371376

372-
FileTestGTest(kat_filepath.c_str(), [&](FileTest *t) {
373-
std::string count, mlen, smlen;
374-
std::vector<uint8_t> xi, rng, seed, msg, pk, sk, sm, ctxstr;
375-
376-
ASSERT_TRUE(t->GetAttribute(&count, "count"));
377-
ASSERT_TRUE(t->GetBytes(&xi, "xi"));
378-
ASSERT_TRUE(t->GetBytes(&rng, "rng"));
379-
ASSERT_TRUE(t->GetBytes(&seed, "seed"));
380-
ASSERT_TRUE(t->GetBytes(&pk, "pk"));
381-
ASSERT_TRUE(t->GetBytes(&sk, "sk"));
382-
ASSERT_TRUE(t->GetBytes(&msg, "msg"));
383-
ASSERT_TRUE(t->GetAttribute(&mlen, "mlen"));
384-
ASSERT_TRUE(t->GetBytes(&sm, "sm"));
385-
ASSERT_TRUE(t->GetAttribute(&smlen, "smlen"));
386-
ASSERT_TRUE(t->GetBytes(&ctxstr, "ctx"));
387-
388-
size_t pk_len = GetParam().public_key_len;
389-
size_t sk_len = GetParam().private_key_len;
390-
size_t sig_len = GetParam().signature_len;
391-
std::string name = GetParam().name;
392-
393-
std::vector<uint8_t> pub(pk_len);
394-
std::vector<uint8_t> priv(sk_len);
395-
std::vector<uint8_t> signature(sig_len);
396-
397-
size_t mlen_int = std::stoi(mlen);
398-
sm.resize(sig_len);
399-
400-
// Generate key pair from seed xi and assert that public and private keys
401-
// are equal to expected values from KAT
402-
if (name == "MLDSA65") {
403-
ASSERT_TRUE(ml_dsa_65_keypair_internal(pub.data(),priv.data(),xi.data()));
404-
}
405-
EXPECT_EQ(Bytes(pub), Bytes(pk));
406-
EXPECT_EQ(Bytes(priv), Bytes(sk));
407-
408-
// Prepare m_prime = (0 || ctxlen || ctx) as in FIPS 204: Algorithm 2 line 10
409-
uint8_t m_prime[257];
410-
size_t m_prime_len = ctxstr.size() + 2;
411-
m_prime[0] = 0;
412-
m_prime[1] = ctxstr.size();
413-
OPENSSL_memcpy(m_prime + 2 , ctxstr.data(), ctxstr.size());
414-
415-
// Generate signature by signing |msg|, assert that signature is equal
416-
// to expected value from KAT, then verify signature.
417-
if (name == "MLDSA65") {
418-
ASSERT_TRUE(ml_dsa_65_sign_internal(priv.data(),
419-
signature.data(), &sig_len,
420-
msg.data(), mlen_int,
421-
m_prime,m_prime_len,
422-
rng.data()));
423-
ASSERT_EQ(Bytes(signature), Bytes(sm));
424-
ASSERT_TRUE(ml_dsa_65_verify_internal(pub.data(),
425-
signature.data(), sig_len,
426-
msg.data(), mlen_int,
427-
m_prime, m_prime_len));
428-
}
429-
});
377+
// Only peform the KAT if the parameter name matches one in PQDSA_types
378+
std::string name = GetParam().name;
379+
if (std::find(std::begin(PQDSA_types), std::end(PQDSA_types), name)
380+
!= std::end(PQDSA_types)) {
381+
382+
FileTestGTest(kat_filepath.c_str(), [&](FileTest *t) {
383+
std::string count, mlen, smlen;
384+
std::vector<uint8_t> xi, rng, seed, msg, pk, sk, sm, ctxstr;
385+
386+
ASSERT_TRUE(t->GetAttribute(&count, "count"));
387+
ASSERT_TRUE(t->GetBytes(&xi, "xi"));
388+
ASSERT_TRUE(t->GetBytes(&rng, "rng"));
389+
ASSERT_TRUE(t->GetBytes(&seed, "seed"));
390+
ASSERT_TRUE(t->GetBytes(&pk, "pk"));
391+
ASSERT_TRUE(t->GetBytes(&sk, "sk"));
392+
ASSERT_TRUE(t->GetBytes(&msg, "msg"));
393+
ASSERT_TRUE(t->GetAttribute(&mlen, "mlen"));
394+
ASSERT_TRUE(t->GetBytes(&sm, "sm"));
395+
ASSERT_TRUE(t->GetAttribute(&smlen, "smlen"));
396+
ASSERT_TRUE(t->GetBytes(&ctxstr, "ctx"));
397+
398+
size_t pk_len = GetParam().public_key_len;
399+
size_t sk_len = GetParam().private_key_len;
400+
size_t sig_len = GetParam().signature_len;
401+
402+
std::vector<uint8_t> pub(pk_len);
403+
std::vector<uint8_t> priv(sk_len);
404+
std::vector<uint8_t> signature(sig_len);
405+
406+
size_t mlen_int = std::stoi(mlen);
407+
sm.resize(sig_len);
408+
409+
// Generate key pair from seed xi and assert that public and private keys
410+
// are equal to expected values from KAT
411+
if (name == "MLDSA65") {
412+
ASSERT_TRUE(ml_dsa_65_keypair_internal(pub.data(), priv.data(), xi.data()));
413+
}
414+
EXPECT_EQ(Bytes(pub), Bytes(pk));
415+
EXPECT_EQ(Bytes(priv), Bytes(sk));
416+
417+
// Prepare m_prime = (0 || ctxlen || ctx) as in FIPS 204: Algorithm 2 line 10
418+
uint8_t m_prime[257];
419+
size_t m_prime_len = ctxstr.size() + 2;
420+
m_prime[0] = 0;
421+
m_prime[1] = ctxstr.size();
422+
ASSERT_TRUE(ctxstr.size() <= 255);
423+
OPENSSL_memcpy(m_prime + 2 , ctxstr.data(), ctxstr.size());
424+
425+
// Generate signature by signing |msg|, assert that signature is equal
426+
// to expected value from KAT, then verify signature.
427+
if (name == "MLDSA65") {
428+
ASSERT_TRUE(ml_dsa_65_sign_internal(priv.data(),
429+
signature.data(), &sig_len,
430+
msg.data(), mlen_int,
431+
m_prime, m_prime_len,
432+
rng.data()));
433+
ASSERT_EQ(Bytes(signature), Bytes(sm));
434+
ASSERT_TRUE(ml_dsa_65_verify_internal(pub.data(),
435+
signature.data(), sig_len,
436+
msg.data(), mlen_int,
437+
m_prime, m_prime_len));
438+
}
439+
});
440+
}
430441
}
431442

432443
TEST_P(PQDSAParameterTest, KeyGen) {

crypto/dilithium/pqcrystals_dilithium_ref_common/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ that initialize a given structure with values corresponding to a parameter set.
1818

1919
**Testing**
2020

21-
The KATs were obtained from https://github.com/post-quantum-cryptography/KAT. They have been modified to insert linebreaks between each test vector set.
21+
The KATs were obtained from https://github.com/post-quantum-cryptography/KAT. We select the KATs for the signing mode `hedged`, which derives the signing private random seed (rho) pseudorandomly from the signer's private key, the message to be signed, and a 256-bit string `rnd` which is generated at random. The `pure` variant of these KATs were used, as they provide test vector inputs for "pure" i.e., non-pre-hashed messages. The KAT files have been modified to insert linebreaks between each test vector set.

crypto/dilithium/pqcrystals_dilithium_ref_common/sign.c

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ int crypto_sign_keypair_internal(ml_dsa_params *params,
8787
**************************************************/
8888
int crypto_sign_keypair(ml_dsa_params *params, uint8_t *pk, uint8_t *sk) {
8989
uint8_t seed[SEEDBYTES];
90-
/* Get randomness for rho, rhoprime and key */
9190
if (!RAND_bytes(seed, SEEDBYTES)) {
9291
return -1;
9392
}

0 commit comments

Comments
 (0)