Skip to content

Commit 8d651a3

Browse files
Add buffer size and callback checks to wc_LmsKey_Sign
Fixes ZD#21439
1 parent 7efc962 commit 8d651a3

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

wolfcrypt/src/wc_lms.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,23 @@ int wc_LmsKey_Sign(LmsKey* key, byte* sig, word32* sigSz, const byte* msg,
987987
WOLFSSL_MSG("error: can't sign, LMS key not in good state");
988988
ret = BAD_STATE_E;
989989
}
990+
/* Check signature buffer size. */
991+
if ((ret == 0) && (*sigSz < key->params->sig_len)) {
992+
/* Signature buffer too small. */
993+
WOLFSSL_MSG("error: LMS sig buffer too small");
994+
ret = BUFFER_E;
995+
}
996+
/* Check read and write callbacks available. */
997+
if ((ret == 0) && ((key->write_private_key == NULL) ||
998+
(key->read_private_key == NULL))) {
999+
WOLFSSL_MSG("error: LmsKey write/read callbacks are not set");
1000+
ret = BAD_FUNC_ARG;
1001+
}
1002+
/* Check read/write callback context available. */
1003+
if ((ret == 0) && (key->context == NULL)) {
1004+
WOLFSSL_MSG("error: LmsKey context is not set");
1005+
ret = BAD_FUNC_ARG;
1006+
}
9901007

9911008
if (ret == 0) {
9921009
WC_DECLARE_VAR(state, LmsState, 1, 0);

wolfcrypt/test/test.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51593,6 +51593,49 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void)
5159351593
ERROR_OUT(WC_TEST_RET_ENC_I(sigSz), out);
5159451594
}
5159551595

51596+
/* Test wc_LmsKey_Sign input validation. */
51597+
{
51598+
word32 smallSz = 1;
51599+
wc_lms_write_private_key_cb saved_write_cb;
51600+
wc_lms_read_private_key_cb saved_read_cb;
51601+
void* saved_ctx;
51602+
51603+
/* Undersized sig buffer should return BUFFER_E. */
51604+
ret = wc_LmsKey_Sign(&signingKey, sig, &smallSz, (byte *) msg, msgSz);
51605+
if (ret != WC_NO_ERR_TRACE(BUFFER_E)) {
51606+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
51607+
}
51608+
51609+
/* NULL write callback should return BAD_FUNC_ARG. */
51610+
saved_write_cb = signingKey.write_private_key;
51611+
signingKey.write_private_key = NULL;
51612+
ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
51613+
signingKey.write_private_key = saved_write_cb;
51614+
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
51615+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
51616+
}
51617+
51618+
/* NULL read callback should return BAD_FUNC_ARG. */
51619+
saved_read_cb = signingKey.read_private_key;
51620+
signingKey.read_private_key = NULL;
51621+
ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
51622+
signingKey.read_private_key = saved_read_cb;
51623+
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
51624+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
51625+
}
51626+
51627+
/* NULL context should return BAD_FUNC_ARG. */
51628+
saved_ctx = signingKey.context;
51629+
signingKey.context = NULL;
51630+
ret = wc_LmsKey_Sign(&signingKey, sig, &sigSz, (byte *) msg, msgSz);
51631+
signingKey.context = saved_ctx;
51632+
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
51633+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
51634+
}
51635+
51636+
ret = 0;
51637+
}
51638+
5159651639
/* 2 ** 5 should be the max number of signatures */
5159751640
for (i = 0; i < 32; ++i) {
5159851641
/* We should have remaining signstures. */

0 commit comments

Comments
 (0)