Skip to content

Commit

Permalink
Dilithium: Final and draft available in one build
Browse files Browse the repository at this point in the history
Make draft version of ML-DSA compiled in with final.
Use WC_ML_DSA_44_DRAFT, WC_ML_DSA_65_DRAFT and WC_ML_DSA_87_DRAFT for
the level to get the draft implementation.
  • Loading branch information
SparkiDev committed Sep 25, 2024
1 parent 72711b4 commit a8c5041
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 128 deletions.
91 changes: 66 additions & 25 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -32875,7 +32875,6 @@ static int test_wc_dilithium_sign_vfy(void)
{
EXPECT_DECLS;
#if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
!defined(WOLFSSL_DILITHIUM_FIPS204_DRAFT) && \
!defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) && \
!defined(WOLFSSL_DILITHIUM_NO_SIGN) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
dilithium_key* key;
Expand Down Expand Up @@ -34089,7 +34088,6 @@ static int test_wc_dilithium_make_key_from_seed(void)
#if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
!defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
dilithium_key* key;
#ifndef WOLFSSL_DILITHIUM_FIPS204_DRAFT
#ifndef WOLFSSL_NO_ML_DSA_44
static const byte seed_44[] = {
0x93, 0xEF, 0x2E, 0x6E, 0xF1, 0xFB, 0x08, 0x99,
Expand Down Expand Up @@ -36294,15 +36292,15 @@ static int test_wc_dilithium_make_key_from_seed(void)
0xDA, 0xC1, 0x7F, 0x93, 0x6F, 0x54, 0xC4, 0xC7
};
#endif /* WOLFSSL_NO_ML_DSA_87 */
#else
#ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT
#ifndef WOLFSSL_NO_ML_DSA_44
static const byte seed_44[] = {
static const byte seed_44_draft[] = {
0xBA, 0xC0, 0x59, 0x52, 0x75, 0x5B, 0x26, 0x47,
0x01, 0xCA, 0x7D, 0x80, 0x6D, 0xFA, 0x08, 0x35,
0x10, 0x28, 0xF6, 0x7B, 0x0E, 0x83, 0xC4, 0x24,
0x01, 0x6F, 0x66, 0xCC, 0x83, 0x87, 0xD4, 0x69
};
static const byte pk_44[] = {
static const byte pk_44_draft[] = {
0x86, 0xF0, 0x0C, 0x20, 0xE0, 0xDA, 0xEE, 0x5E,
0x1E, 0xDE, 0x71, 0x39, 0x49, 0x0C, 0xC8, 0xCF,
0xEF, 0xC9, 0xAB, 0x62, 0x3B, 0x8D, 0xEF, 0x0B,
Expand Down Expand Up @@ -36468,7 +36466,7 @@ static int test_wc_dilithium_make_key_from_seed(void)
0xFC, 0xDD, 0x2D, 0x4C, 0xE2, 0x99, 0x33, 0x04,
0xE4, 0x26, 0x15, 0x37, 0x6C, 0x32, 0xB9, 0x17
};
static const byte sk_44[] = {
static const byte sk_44_draft[] = {
0x86, 0xF0, 0x0C, 0x20, 0xE0, 0xDA, 0xEE, 0x5E,
0x1E, 0xDE, 0x71, 0x39, 0x49, 0x0C, 0xC8, 0xCF,
0xEF, 0xC9, 0xAB, 0x62, 0x3B, 0x8D, 0xEF, 0x0B,
Expand Down Expand Up @@ -36792,13 +36790,13 @@ static int test_wc_dilithium_make_key_from_seed(void)
};
#endif /* !WOLFSSL_NO_ML_DSA_44 */
#ifndef WOLFSSL_NO_ML_DSA_65
static const byte seed_65[] = {
static const byte seed_65_draft[] = {
0x41, 0xAF, 0x98, 0x7B, 0x02, 0x6E, 0x47, 0x5F,
0x37, 0x91, 0x7F, 0x2A, 0x6A, 0x9A, 0x87, 0xE7,
0x51, 0xAD, 0xF9, 0x5B, 0x92, 0x7F, 0x2D, 0xCE,
0xF0, 0xD4, 0xF3, 0xDA, 0x8F, 0x8C, 0x86, 0x6B
};
static const byte pk_65[] = {
static const byte pk_65_draft[] = {
0xDC, 0x38, 0xE5, 0x5F, 0xDF, 0x2E, 0x9D, 0xD4,
0x34, 0x5C, 0xAE, 0x1A, 0x7D, 0xF4, 0x2E, 0x2E,
0xBC, 0x58, 0x57, 0x80, 0x55, 0x02, 0xE4, 0x3F,
Expand Down Expand Up @@ -37044,7 +37042,7 @@ static int test_wc_dilithium_make_key_from_seed(void)
0x36, 0xE3, 0x3C, 0x70, 0xE3, 0xEA, 0xAC, 0x34,
0x32, 0xB7, 0x0D, 0xBA, 0x7C, 0xAB, 0xE6, 0x18
};
static const byte sk_65[] = {
static const byte sk_65_draft[] = {
0xDC, 0x38, 0xE5, 0x5F, 0xDF, 0x2E, 0x9D, 0xD4,
0x34, 0x5C, 0xAE, 0x1A, 0x7D, 0xF4, 0x2E, 0x2E,
0xBC, 0x58, 0x57, 0x80, 0x55, 0x02, 0xE4, 0x3F,
Expand Down Expand Up @@ -37552,13 +37550,13 @@ static int test_wc_dilithium_make_key_from_seed(void)
};
#endif /* WOLFSSL_NO_ML_DSA_65 */
#ifndef WOLFSSL_NO_ML_DSA_87
static const byte seed_87[] = {
static const byte seed_87_draft[] = {
0x22, 0x5F, 0x77, 0x07, 0x5E, 0x66, 0xCE, 0x1C,
0x99, 0xBA, 0x95, 0xB4, 0xFC, 0xDF, 0x25, 0x8B,
0xBB, 0x6F, 0xA5, 0xFE, 0x9C, 0x34, 0x9F, 0x0F,
0xDE, 0x3F, 0x71, 0xD5, 0x33, 0x9F, 0x6F, 0xD8
};
static const byte pk_87[] = {
static const byte pk_87_draft[] = {
0x8C, 0x52, 0x4B, 0xD9, 0xAC, 0x48, 0x5C, 0xC6,
0x9A, 0xA0, 0x75, 0x64, 0xE1, 0x4F, 0x0F, 0x60,
0x13, 0x0E, 0xDE, 0x34, 0x08, 0xA5, 0xD4, 0x81,
Expand Down Expand Up @@ -37884,7 +37882,7 @@ static int test_wc_dilithium_make_key_from_seed(void)
0x01, 0x33, 0x82, 0x84, 0x37, 0x03, 0xEB, 0x0E,
0xB1, 0x5F, 0x1B, 0x60, 0x8A, 0x2C, 0x9F, 0x39
};
static const byte sk_87[] = {
static const byte sk_87_draft[] = {
0x8C, 0x52, 0x4B, 0xD9, 0xAC, 0x48, 0x5C, 0xC6,
0x9A, 0xA0, 0x75, 0x64, 0xE1, 0x4F, 0x0F, 0x60,
0x13, 0x0E, 0xDE, 0x34, 0x08, 0xA5, 0xD4, 0x81,
Expand Down Expand Up @@ -38514,18 +38512,36 @@ static int test_wc_dilithium_make_key_from_seed(void)
ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_44), 0);
ExpectIntEQ(XMEMCMP(key->p, pk_44, sizeof(pk_44)), 0);
ExpectIntEQ(XMEMCMP(key->k, sk_44, sizeof(sk_44)), 0);
#ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44_DRAFT), 0);
ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_44_draft), 0);
ExpectIntEQ(XMEMCMP(key->p, pk_44_draft, sizeof(pk_44_draft)), 0);
ExpectIntEQ(XMEMCMP(key->k, sk_44_draft, sizeof(sk_44_draft)), 0);
#endif
#endif
#ifndef WOLFSSL_NO_ML_DSA_65
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_65), 0);
ExpectIntEQ(XMEMCMP(key->p, pk_65, sizeof(pk_65)), 0);
ExpectIntEQ(XMEMCMP(key->k, sk_65, sizeof(sk_65)), 0);
#ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65_DRAFT), 0);
ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_65_draft), 0);
ExpectIntEQ(XMEMCMP(key->p, pk_65_draft, sizeof(pk_65_draft)), 0);
ExpectIntEQ(XMEMCMP(key->k, sk_65_draft, sizeof(sk_65_draft)), 0);
#endif
#endif
#ifndef WOLFSSL_NO_ML_DSA_87
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_87), 0);
ExpectIntEQ(XMEMCMP(key->p, pk_87, sizeof(pk_87)), 0);
ExpectIntEQ(XMEMCMP(key->k, sk_87, sizeof(sk_87)), 0);
#ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87_DRAFT), 0);
ExpectIntEQ(wc_dilithium_make_key_from_seed(key, seed_87_draft), 0);
ExpectIntEQ(XMEMCMP(key->p, pk_87_draft, sizeof(pk_87_draft)), 0);
ExpectIntEQ(XMEMCMP(key->k, sk_87_draft, sizeof(sk_87_draft)), 0);
#endif
#endif

wc_dilithium_free(key);
Expand All @@ -38538,8 +38554,7 @@ static int test_wc_dilithium_sig_kats(void)
{
EXPECT_DECLS;
#if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
!defined(WOLFSSL_DILITHIUM_NO_SIGN) && \
!defined(WOLFSSL_DILITHIUM_FIPS204_DRAFT)
!defined(WOLFSSL_DILITHIUM_NO_SIGN)
dilithium_key* key;
#ifndef WOLFSSL_NO_ML_DSA_44
static const byte sk_44[] = {
Expand Down Expand Up @@ -43356,7 +43371,6 @@ static int test_wc_dilithium_verify_kats(void)
!defined(WOLFSSL_DILITHIUM_NO_VERIFY)
dilithium_key* key;
int res;
#ifndef WOLFSSL_DILITHIUM_FIPS204_DRAFT
#ifndef WOLFSSL_NO_ML_DSA_44
static const byte pk_44[] = {
0x09, 0xB4, 0x88, 0x7D, 0x97, 0xBC, 0xF6, 0x37,
Expand Down Expand Up @@ -45457,9 +45471,9 @@ static int test_wc_dilithium_verify_kats(void)
0x29, 0x2E, 0x36
};
#endif
#else
#ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT
#ifndef WOLFSSL_NO_ML_DSA_44
static const byte pk_44[] = {
static const byte pk_44_draft[] = {
0x35, 0x07, 0x31, 0x3A, 0xE3, 0x7A, 0xF6, 0x96,
0x6C, 0x11, 0xA9, 0xE4, 0x0B, 0xEB, 0xEC, 0xE9,
0x2B, 0x67, 0x3F, 0xD2, 0x67, 0x3C, 0x1C, 0x4C,
Expand Down Expand Up @@ -45625,7 +45639,7 @@ static int test_wc_dilithium_verify_kats(void)
0x29, 0x4D, 0xB2, 0xE2, 0xD5, 0x9F, 0xD4, 0xB9,
0x13, 0xB4, 0x33, 0x80, 0x27, 0x84, 0x7E, 0xF4
};
static const byte msg_44[] = {
static const byte msg_44_draft[] = {
0x5C, 0x70, 0x7F, 0xBF, 0xF4, 0xFF, 0xE5, 0x9B,
0x09, 0xAA, 0xF8, 0xDB, 0x21, 0xAD, 0xBE, 0xBA,
0xC6, 0xB2, 0x65, 0x37, 0x9A, 0x9A, 0x43, 0x3A,
Expand All @@ -45643,7 +45657,7 @@ static int test_wc_dilithium_verify_kats(void)
0x9E, 0xC6, 0x26, 0x80, 0x9E, 0xCE, 0x19, 0x8D,
0x6A, 0x6B, 0x09, 0x03, 0x45, 0xDF, 0x22, 0x7D
};
static const byte sig_44[] = {
static const byte sig_44_draft[] = {
0x08, 0xF0, 0x10, 0xFA, 0x63, 0x3F, 0x2B, 0xA1,
0x46, 0x81, 0x34, 0xC4, 0xBC, 0xAB, 0x62, 0x17,
0x0B, 0x64, 0xEA, 0x00, 0x2D, 0xD6, 0x8A, 0xE5,
Expand Down Expand Up @@ -45950,7 +45964,7 @@ static int test_wc_dilithium_verify_kats(void)
};
#endif
#ifndef WOLFSSL_NO_ML_DSA_65
static const byte pk_65[] = {
static const byte pk_65_draft[] = {
0x6C, 0x84, 0x14, 0x38, 0x08, 0x56, 0xCB, 0x52,
0xD7, 0x9C, 0x4B, 0x29, 0x13, 0x9F, 0xB1, 0x83,
0x9B, 0x86, 0x06, 0xF5, 0x94, 0x8B, 0x9D, 0x72,
Expand Down Expand Up @@ -46196,7 +46210,7 @@ static int test_wc_dilithium_verify_kats(void)
0xCF, 0xE4, 0x67, 0x21, 0x03, 0x65, 0x84, 0x34,
0xD0, 0x32, 0x7A, 0xDD, 0xCD, 0x66, 0xBC, 0xB6
};
static const byte msg_65[] = {
static const byte msg_65_draft[] = {
0xDB, 0x84, 0x94, 0xBA, 0x19, 0xC4, 0x11, 0x8F,
0xB1, 0x5D, 0x0A, 0xCF, 0x42, 0x54, 0xFD, 0x37,
0x48, 0x3F, 0xCF, 0x47, 0x48, 0xFD, 0x18, 0x44,
Expand Down Expand Up @@ -46226,7 +46240,7 @@ static int test_wc_dilithium_verify_kats(void)
0x03, 0xEA, 0xFE, 0xF1, 0x70, 0xC1, 0xF1, 0xD2,
0x8E, 0x99, 0xBB
};
static const byte sig_65[] = {
static const byte sig_65_draft[] = {
0xF7, 0x78, 0x9A, 0x45, 0xA3, 0x58, 0x73, 0x30,
0xE7, 0xFC, 0xF7, 0x06, 0x95, 0xF7, 0xF6, 0x96,
0x88, 0xA2, 0xB8, 0xD0, 0xCE, 0x54, 0xF0, 0x90,
Expand Down Expand Up @@ -46644,7 +46658,7 @@ static int test_wc_dilithium_verify_kats(void)
};
#endif
#ifndef WOLFSSL_NO_ML_DSA_87
static const byte pk_87[] = {
static const byte pk_87_draft[] = {
0x2D, 0x1E, 0x6B, 0xED, 0x84, 0x52, 0xEB, 0xF1,
0x26, 0xED, 0xE7, 0x0C, 0xA0, 0xA2, 0xB5, 0x0D,
0x03, 0x34, 0x2D, 0x5B, 0x13, 0xB2, 0xAE, 0x21,
Expand Down Expand Up @@ -46970,12 +46984,12 @@ static int test_wc_dilithium_verify_kats(void)
0x54, 0xAD, 0xB4, 0xB4, 0x17, 0x0A, 0xC7, 0x12,
0x7F, 0x93, 0x17, 0x5C, 0x1E, 0xB2, 0x25, 0x12
};
static const byte msg_87[] = {
static const byte msg_87_draft[] = {
0x14, 0x42, 0x63, 0x34, 0x94, 0x09, 0x60, 0x77,
0x3B, 0xFF, 0x65, 0xF0, 0x8D, 0x1D, 0xE4, 0x89,
0xC4, 0xC3, 0xED, 0x36
};
static const byte sig_87[] = {
static const byte sig_87_draft[] = {
0x13, 0xE8, 0x99, 0xEE, 0xDC, 0xCC, 0x0F, 0xBA,
0x62, 0x91, 0x44, 0xE4, 0xAC, 0x06, 0x79, 0x06,
0xB5, 0x32, 0x6B, 0x8F, 0x9A, 0x6C, 0xCB, 0xAB,
Expand Down Expand Up @@ -47574,6 +47588,15 @@ static int test_wc_dilithium_verify_kats(void)
ExpectIntEQ(wc_dilithium_verify_msg(sig_44, (word32)sizeof(sig_44), msg_44,
(word32)sizeof(msg_44), &res, key), 0);
ExpectIntEQ(res, 1);
#ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44_DRAFT), 0);
ExpectIntEQ(wc_dilithium_import_public(pk_44_draft,
(word32)sizeof(pk_44_draft), key), 0);
ExpectIntEQ(wc_dilithium_verify_msg(sig_44_draft,
(word32)sizeof(sig_44_draft), msg_44_draft,
(word32)sizeof(msg_44_draft), &res, key), 0);
ExpectIntEQ(res, 1);
#endif
#endif
#ifndef WOLFSSL_NO_ML_DSA_65
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
Expand All @@ -47582,6 +47605,15 @@ static int test_wc_dilithium_verify_kats(void)
ExpectIntEQ(wc_dilithium_verify_msg(sig_65, (word32)sizeof(sig_65), msg_65,
(word32)sizeof(msg_65), &res, key), 0);
ExpectIntEQ(res, 1);
#ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65_DRAFT), 0);
ExpectIntEQ(wc_dilithium_import_public(pk_65_draft,
(word32)sizeof(pk_65_draft), key), 0);
ExpectIntEQ(wc_dilithium_verify_msg(sig_65_draft,
(word32)sizeof(sig_65_draft), msg_65_draft,
(word32)sizeof(msg_65_draft), &res, key), 0);
ExpectIntEQ(res, 1);
#endif
#endif
#ifndef WOLFSSL_NO_ML_DSA_87
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
Expand All @@ -47590,6 +47622,15 @@ static int test_wc_dilithium_verify_kats(void)
ExpectIntEQ(wc_dilithium_verify_msg(sig_87, (word32)sizeof(sig_87), msg_87,
(word32)sizeof(msg_87), &res, key), 0);
ExpectIntEQ(res, 1);
#ifdef WOLFSSL_DILITHIUM_FIPS204_DRAFT
ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87_DRAFT), 0);
ExpectIntEQ(wc_dilithium_import_public(pk_87_draft,
(word32)sizeof(pk_87_draft), key), 0);
ExpectIntEQ(wc_dilithium_verify_msg(sig_87_draft,
(word32)sizeof(sig_87_draft), msg_87_draft,
(word32)sizeof(msg_87_draft), &res, key), 0);
ExpectIntEQ(res, 1);
#endif
#endif

wc_dilithium_free(key);
Expand Down
Loading

0 comments on commit a8c5041

Please sign in to comment.