Skip to content

Commit 9c0a9a6

Browse files
authored
Merge pull request #10084 from holtrop-wolfssl/zd21439
Add buffer size and callback checks to wc_LmsKey_Sign
2 parents 348d75b + 048a03e commit 9c0a9a6

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

wolfcrypt/src/wc_lms.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,11 @@ int wc_LmsKey_GetPrivLen(const LmsKey* key, word32* len)
963963
* @return 0 on success.
964964
* @return BAD_FUNC_ARG when key, sig, sigSz or msg is NULL.
965965
* @return BAD_FUNC_ARG when msgSz is not greater than 0.
966+
* @return BAD_FUNC_ARG when a write private key is not set.
967+
* @return BAD_FUNC_ARG when a read/write private key context is not set.
968+
* @return BUFFER_E when sigSz is too small.
969+
* @return BAD_STATE_E when wrong state for operation.
970+
* @return IO_FAILED_E when reading or writing private key failed.
966971
*/
967972
int wc_LmsKey_Sign(LmsKey* key, byte* sig, word32* sigSz, const byte* msg,
968973
int msgSz)
@@ -987,6 +992,22 @@ int wc_LmsKey_Sign(LmsKey* key, byte* sig, word32* sigSz, const byte* msg,
987992
WOLFSSL_MSG("error: can't sign, LMS key not in good state");
988993
ret = BAD_STATE_E;
989994
}
995+
/* Check signature buffer size. */
996+
if ((ret == 0) && (*sigSz < key->params->sig_len)) {
997+
/* Signature buffer too small. */
998+
WOLFSSL_MSG("error: LMS sig buffer too small");
999+
ret = BUFFER_E;
1000+
}
1001+
/* Check read and write callbacks available. */
1002+
if ((ret == 0) && (key->write_private_key == NULL)) {
1003+
WOLFSSL_MSG("error: LmsKey write/read callbacks are not set");
1004+
ret = BAD_FUNC_ARG;
1005+
}
1006+
/* Check read/write callback context available. */
1007+
if ((ret == 0) && (key->context == NULL)) {
1008+
WOLFSSL_MSG("error: LmsKey context is not set");
1009+
ret = BAD_FUNC_ARG;
1010+
}
9901011

9911012
if (ret == 0) {
9921013
WC_DECLARE_VAR(state, LmsState, 1, 0);

wolfcrypt/test/test.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51595,6 +51595,39 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t lms_test(void)
5159551595
ERROR_OUT(WC_TEST_RET_ENC_I(sigSz), out);
5159651596
}
5159751597

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

0 commit comments

Comments
 (0)