From 85cc627d509c565a325ed409155ea936e7486377 Mon Sep 17 00:00:00 2001 From: jordan Date: Thu, 19 Sep 2024 15:49:30 -0500 Subject: [PATCH] tiny dilithium cleanup: add test, more cleanup. --- wolfcrypt/test/test.c | 26 ++++++++++++++++++++++++++ wolfssl/wolfcrypt/dilithium.h | 4 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index c618423fc0..8a2e92aaad 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -42071,9 +42071,12 @@ static wc_test_ret_t dilithium_param_vfy_test(int param, const byte* pubKey, { byte msg[512]; dilithium_key* key; + byte * pubExported = NULL; wc_test_ret_t ret; int i; int res = 0; + word32 lenExported = pubKeyLen; + int n_diff = 0; key = (dilithium_key*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); @@ -42081,6 +42084,12 @@ static wc_test_ret_t dilithium_param_vfy_test(int param, const byte* pubKey, ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out); } + pubExported = (byte*)XMALLOC(pubKeyLen, HEAP_HINT, + DYNAMIC_TYPE_TMP_BUFFER); + if (pubExported == NULL) { + ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out); + } + /* make dummy msg */ for (i = 0; i < (int)sizeof(msg); i++) { msg[i] = (byte)i; @@ -42099,6 +42108,22 @@ static wc_test_ret_t dilithium_param_vfy_test(int param, const byte* pubKey, if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out); + /* Now test the ExportPubRaw API, verify we recover the original pub. */ + ret = wc_dilithium_export_public(key, pubExported, &lenExported); + if (ret != 0) { + ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out); + } + + if (lenExported <= 0 || lenExported != pubKeyLen) { + ERROR_OUT(WC_TEST_RET_ENC_EC(lenExported), out); + } + + n_diff = XMEMCMP(pubExported, pubKey, pubKeyLen); + + if (n_diff) { + ERROR_OUT(WC_TEST_RET_ENC_EC(n_diff), out); + } + #ifndef WOLFSSL_DILITHIUM_FIPS204_DRAFT ret = wc_dilithium_verify_ctx_msg(sig, sigLen, NULL, 0, msg, (word32)sizeof(msg), &res, key); @@ -42113,6 +42138,7 @@ static wc_test_ret_t dilithium_param_vfy_test(int param, const byte* pubKey, out: wc_dilithium_free(key); XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(pubExported, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); return ret; } diff --git a/wolfssl/wolfcrypt/dilithium.h b/wolfssl/wolfcrypt/dilithium.h index 7d2b7e6adf..9e309a4049 100644 --- a/wolfssl/wolfcrypt/dilithium.h +++ b/wolfssl/wolfcrypt/dilithium.h @@ -799,7 +799,7 @@ WOLFSSL_API int wc_Dilithium_PrivateKeyToDer(dilithium_key* key, byte* output, #define wc_MlDsaKey_ExportPrivRaw(key, out, outLen) \ wc_dilithium_export_private_only(key, out, outLen) #define wc_MlDsaKey_ImportPrivRaw(key, in, inLen) \ - wc_dilithium_import_private_only(out, outLen, key) + wc_dilithium_import_private_only(in, inLen, key) #define wc_MlDsaKey_Sign(key, sig, sigSz, msg, msgSz, rng) \ wc_dilithium_sign_msg(msg, msgSz, sig, sigSz, key, rng) #define wc_MlDsaKey_Free(key) \ @@ -807,7 +807,7 @@ WOLFSSL_API int wc_Dilithium_PrivateKeyToDer(dilithium_key* key, byte* output, #define wc_MlDsaKey_ExportPubRaw(key, out, outLen) \ wc_dilithium_export_public(key, out, outLen) #define wc_MlDsaKey_ImportPubRaw(key, in, inLen) \ - wc_dilithium_import_public(out, outLen, key) + wc_dilithium_import_public(in, inLen, key) #define wc_MlDsaKey_Verify(key, sig, sigSz, msg, msgSz, res) \ wc_dilithium_verify_msg(sig, sigSz, msg, msgSz, res, key)