Rework mbedtls_test_unhexify()

Rework mbedtls_test_unhexify to extend its scope of usage.
Return in error when the function detects an error instead
of calling mbedtls_exit().
Improve safety by checking the output buffer is not overrun.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h
index 36ec8e6..69d882c 100644
--- a/tests/include/test/helpers.h
+++ b/tests/include/test/helpers.h
@@ -54,7 +54,29 @@
 int mbedtls_test_platform_setup( void );
 void mbedtls_test_platform_teardown( void );
 
-int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf );
+/**
+ * \brief          This function translates an ASCII string encoding an
+ *                 hexadecimal number into the encoded hexadecimal number. The
+ *                 hexadecimal number is represented as an array of
+ *                 unsigned char.
+ *
+ * \note           The output buffer can be the same as the input buffer. For
+ *                 any other overlapping of the input and output buffers, the
+ *                 behavior is undefined.
+ *
+ * \param obuf     Output buffer.
+ * \param obufmax  Size in number of bytes of \p obuf.
+ * \param ibuf     Input buffer.
+ * \param len      The number of unsigned char written in \p obuf. This must
+ *                 not be \c NULL.
+ *
+ * \return         \c 0 on success.
+ * \return         \c -1 if the output buffer is too small or the input string
+ *                 is not a valid ASCII encoding of an hexadecimal number.
+ */
+int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
+                           const char *ibuf, size_t *len );
+
 void mbedtls_test_hexify( unsigned char *obuf,
                           const unsigned char *ibuf,
                           int len );