test_suite_rsa: add test for key write with incremental output size
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/tests/suites/test_suite_rsa.data b/tests/suites/test_suite_rsa.data
index 545e7ff..8a224d5 100644
--- a/tests/suites/test_suite_rsa.data
+++ b/tests/suites/test_suite_rsa.data
@@ -720,3 +720,9 @@
RSA parse public key - correct values, trailing garbage
rsa_parse_pkcs1_key:1:"30818a028181009f091e6968b474f76f0e9c237c1d895996ae704b4f6d706acec8d2daac6209bf524aa3f658d0283adba1077f6cbe92e425dcde52290b239cade91be86c88425434986806e85734e159768f3dfea932baaa9409d25bace8ee9dce0cdde0903207299de575ae60feccf0daf82334ab83638539b0da74072f253acea8afc8e66bb7020301000100":MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
+
+RSA priv key write - incremental output buffer size
+rsa_key_write_incremental:0:"3063020100021100cc8ab070369ede72920e5a51523c857102030100010211009a6318982a7231de1894c54aa4909201020900f3058fd8dc484d61020900d7770dbd8b78a2110209009471f14c26428401020813425f060c4b72210208052b93d01747a87c"
+
+RSA priv public key write - incremental output buffer size
+rsa_key_write_incremental:1:"308189028181009f091e6968b474f76f0e9c237c1d895996ae704b4f6d706acec8d2daac6209bf524aa3f658d0283adba1077f6cbe92e425dcde52290b239cade91be86c88425434986806e85734e159768f3dfea932baaa9409d25bace8ee9dce0cdde0903207299de575ae60feccf0daf82334ab83638539b0da74072f253acea8afc8e66bb70203010001"
diff --git a/tests/suites/test_suite_rsa.function b/tests/suites/test_suite_rsa.function
index 71ca2b9..44caacd 100644
--- a/tests/suites/test_suite_rsa.function
+++ b/tests/suites/test_suite_rsa.function
@@ -1430,6 +1430,56 @@
}
/* END_CASE */
+/* BEGIN_CASE */
+void rsa_key_write_incremental(int is_public, data_t *input)
+{
+ mbedtls_rsa_context rsa_ctx;
+ unsigned char *buf = NULL, *start, *end;
+ size_t i;
+
+ mbedtls_rsa_init(&rsa_ctx);
+
+ /* This is supposed to succeed as the real target of this test are the
+ * write attempt below. */
+ if (is_public) {
+ start = input->x;
+ end = input->x + input->len;
+ TEST_EQUAL(mbedtls_rsa_pubkey_parse(&rsa_ctx, &start, end), 0);
+ } else {
+ TEST_EQUAL(mbedtls_rsa_key_parse(&rsa_ctx, input->x, input->len), 0);
+ }
+
+ for (i = 1; i < input->len; i++) {
+ TEST_CALLOC(buf, i);
+ end = buf + i;
+ /* We don't care much about the return value as long as it fails. */
+ if (is_public) {
+ TEST_ASSERT(mbedtls_rsa_pubkey_write(&rsa_ctx, buf, &end) != 0);
+ } else {
+ TEST_ASSERT(mbedtls_rsa_key_write(&rsa_ctx, buf, &end) != 0);
+ }
+ mbedtls_free(buf);
+ buf = NULL;
+ }
+
+ /* Ensure with the correct output buffer size everything works as expected. */
+ TEST_CALLOC(buf, i);
+ end = buf + i;
+
+ if (is_public) {
+ TEST_ASSERT(mbedtls_rsa_pubkey_write(&rsa_ctx, buf, &end) != 0);
+ } else {
+ TEST_ASSERT(mbedtls_rsa_key_write(&rsa_ctx, buf, &end) > 0);
+ }
+
+exit:
+ if (buf != NULL) {
+ mbedtls_free(buf);
+ }
+ mbedtls_rsa_free(&rsa_ctx);
+}
+/* END_CASE */
+
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
void rsa_selftest()
{