Skip to content

Commit 2e539ef

Browse files
authored
Merge pull request #7983 from philljj/tiny_dilithium_cleanup
dilithium: small cleanup to support wolfboot.
2 parents f1e6dd2 + c3410f2 commit 2e539ef

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

wolfcrypt/src/dilithium.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
* but is slower.
7171
* WOLFSSL_DILITHIUM_SMALL_MEM_POLY64 Default: OFF
7272
* Compiles the small memory implementations to use a 64-bit polynomial.
73-
* Uses 2KB of memory but is slighlty quicker (2.75-7%).
73+
* Uses 2KB of memory but is slightly quicker (2.75-7%).
7474
*
7575
* WOLFSSL_DILITHIUM_ALIGNMENT Default: 8
7676
* Use to indicate whether loading and storing of words needs to be aligned.

wolfcrypt/test/test.c

+27
Original file line numberDiff line numberDiff line change
@@ -42117,16 +42117,25 @@ static wc_test_ret_t dilithium_param_vfy_test(int param, const byte* pubKey,
4211742117
{
4211842118
byte msg[512];
4211942119
dilithium_key* key;
42120+
byte * pubExported = NULL;
4212042121
wc_test_ret_t ret;
4212142122
int i;
4212242123
int res = 0;
42124+
word32 lenExported = pubKeyLen;
42125+
int n_diff = 0;
4212342126

4212442127
key = (dilithium_key*)XMALLOC(sizeof(*key), HEAP_HINT,
4212542128
DYNAMIC_TYPE_TMP_BUFFER);
4212642129
if (key == NULL) {
4212742130
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
4212842131
}
4212942132

42133+
pubExported = (byte*)XMALLOC(pubKeyLen, HEAP_HINT,
42134+
DYNAMIC_TYPE_TMP_BUFFER);
42135+
if (pubExported == NULL) {
42136+
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
42137+
}
42138+
4213042139
/* make dummy msg */
4213142140
for (i = 0; i < (int)sizeof(msg); i++) {
4213242141
msg[i] = (byte)i;
@@ -42160,9 +42169,27 @@ static wc_test_ret_t dilithium_param_vfy_test(int param, const byte* pubKey,
4216042169
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
4216142170
if (res != 1)
4216242171
ERROR_OUT(WC_TEST_RET_ENC_EC(res), out);
42172+
42173+
/* Now test the export pub raw API, verify we recover the original pub. */
42174+
ret = wc_dilithium_export_public(key, pubExported, &lenExported);
42175+
if (ret != 0) {
42176+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
42177+
}
42178+
42179+
if (lenExported <= 0 || lenExported != pubKeyLen) {
42180+
ERROR_OUT(WC_TEST_RET_ENC_EC(lenExported), out);
42181+
}
42182+
42183+
n_diff = XMEMCMP(pubExported, pubKey, pubKeyLen);
42184+
42185+
if (n_diff) {
42186+
ERROR_OUT(WC_TEST_RET_ENC_EC(n_diff), out);
42187+
}
42188+
4216342189
out:
4216442190
wc_dilithium_free(key);
4216542191
XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
42192+
XFREE(pubExported, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
4216642193
return ret;
4216742194
}
4216842195

wolfssl/wolfcrypt/dilithium.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ int wc_dilithium_export_public(dilithium_key* key, byte* out, word32* outLen);
765765
#ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
766766
WOLFSSL_API
767767
int wc_dilithium_export_private(dilithium_key* key, byte* out, word32* outLen);
768+
#define wc_dilithium_export_private_only wc_dilithium_export_private
768769
#endif
769770
#ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
770771
WOLFSSL_API
@@ -841,15 +842,15 @@ WOLFSSL_API int wc_Dilithium_PrivateKeyToDer(dilithium_key* key, byte* output,
841842
#define wc_MlDsaKey_ExportPrivRaw(key, out, outLen) \
842843
wc_dilithium_export_private_only(key, out, outLen)
843844
#define wc_MlDsaKey_ImportPrivRaw(key, in, inLen) \
844-
wc_dilithium_import_private_only(out, outLen, key)
845+
wc_dilithium_import_private_only(in, inLen, key)
845846
#define wc_MlDsaKey_Sign(key, sig, sigSz, msg, msgSz, rng) \
846847
wc_dilithium_sign_msg(msg, msgSz, sig, sigSz, key, rng)
847848
#define wc_MlDsaKey_Free(key) \
848849
wc_dilithium_free(key)
849850
#define wc_MlDsaKey_ExportPubRaw(key, out, outLen) \
850851
wc_dilithium_export_public(key, out, outLen)
851852
#define wc_MlDsaKey_ImportPubRaw(key, in, inLen) \
852-
wc_dilithium_import_public(out, outLen, key)
853+
wc_dilithium_import_public(in, inLen, key)
853854
#define wc_MlDsaKey_Verify(key, sig, sigSz, msg, msgSz, res) \
854855
wc_dilithium_verify_msg(sig, sigSz, msg, msgSz, res, key)
855856

0 commit comments

Comments
 (0)