Add macro for error code addition
Adds a macro (`MBEDTLS_ERR_ADD`) to add error codes together and check that the
result will not be corrupted. This additional check is only enabled during
testing when `MBEDTLS_TEST_HOOKS` is defined.
Also includes a reference usage example in `rsa.c` where two high-level error
codes could be incorrectly added together under the right conditions. This now
ensures that when this error occurs during testing it will be correctly
reported.
Signed-off-by: Chris Jones <christopher.jones@arm.com>
diff --git a/include/mbedtls/error.h b/include/mbedtls/error.h
index cd7731e..d164e9f 100644
--- a/include/mbedtls/error.h
+++ b/include/mbedtls/error.h
@@ -114,6 +114,16 @@
#define MBEDTLS_ERR_ERROR_GENERIC_ERROR -0x0001 /**< Generic error */
#define MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED -0x006E /**< This is a bug in the library */
+#if defined(MBEDTLS_TEST_HOOKS)
+void (*mbedtls_test_err_add_hook)( int, int, const char *, int );
+int mbedtls_err_add( int high, int low, const char *file, int line );
+#define MBEDTLS_ERR_ADD( high, low ) \
+ ( mbedtls_err_add( high, low, __FILE__, __LINE__ ) )
+#else
+#define MBEDTLS_ERR_ADD( high, low ) \
+ ( high + low )
+#endif
+
/**
* \brief Translate a mbed TLS error code into a string representation,
* Result is truncated if necessary and always includes a terminating