Add support for const error description strings
Problem
-------
mbedtls_strerror is a utility function which converts an mbedTLS error code
into a human readable string. It requires the caller to allocate a buffer every
time an error code needs to be converted to a string. It is an overkill and a
waste of RAM for resource constrained microcontrollers - where the most common
use case is to use these strings for logging.
Solution
--------
The proposed commit adds two functions:
* const char * mbedtls_high_level_strerr( int error_code );
* const char * mbedtls_low_level_strerr( int error_code );
The above two functions convert the high level and low level parts of an mbedTLS
error code to human readable strings. They return a const pointer to an
unmodifiable string which is not supposed to be modified by the caller and only
to be used for logging purposes. The caller no longer needs to allocate a
buffer.
Backward Compatibility
----------------------
The proposed change is completely backward compatible as it does not change
the existing mbedtls_strerror function and ensures that it continues to behave
the same way.
Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
diff --git a/include/mbedtls/error.h b/include/mbedtls/error.h
index 82b0188..3f0af04 100644
--- a/include/mbedtls/error.h
+++ b/include/mbedtls/error.h
@@ -127,6 +127,32 @@
*/
void mbedtls_strerror( int errnum, char *buffer, size_t buflen );
+/**
+ * \brief Translate high level part of a mbed TLS error code into a string
+ * representation.
+ *
+ * This function returns a const pointer to an un-modifiable string. The caller
+ * must not try to modify the string use it only for logging purposes.
+ *
+ * \param error_code error code
+ *
+ * \return The string representation of the error code.
+ */
+const char * mbedtls_high_level_strerr( int error_code );
+
+/**
+ * \brief Translate low level part of a mbed TLS error code into a string
+ * representation.
+ *
+ * This function returns a const pointer to an un-modifiable string. The caller
+ * must not try to modify the string and use it only for logging purposes.
+ *
+ * \param error_code error code
+ *
+ * \return The string representation of the error code.
+ */
+const char * mbedtls_low_level_strerr( int error_code );
+
#ifdef __cplusplus
}
#endif