SPRTL: 'memory compare' function
The 'memory compare' function with security enhancement - return the
result after all elements are enumerated to avoid attack by time
measurement.
Change-Id: I979a32b56e38832cbfd28581749671cb26634ca4
Signed-off-by: Shawn Shan <shawn.shan@arm.com>
diff --git a/secure_fw/lib/sprt/CMakeLists.inc b/secure_fw/lib/sprt/CMakeLists.inc
index 05412d4..dca3a87 100644
--- a/secure_fw/lib/sprt/CMakeLists.inc
+++ b/secure_fw/lib/sprt/CMakeLists.inc
@@ -27,6 +27,7 @@
set (LIBSPRT_C_SRC
"${LIBSPRT_DIR}/tfm_libsprt_c_memcpy.c"
"${LIBSPRT_DIR}/tfm_libsprt_c_memmove.c"
+ "${LIBSPRT_DIR}/tfm_libsprt_c_memcmp.c"
"${TFM_ROOT_DIR}/interface/src/log/tfm_log_raw.c")
#Append all our source files to global lists.
diff --git a/secure_fw/lib/sprt/tfm_libsprt_c.h b/secure_fw/lib/sprt/tfm_libsprt_c.h
index 7dd2259..f303dc4 100644
--- a/secure_fw/lib/sprt/tfm_libsprt_c.h
+++ b/secure_fw/lib/sprt/tfm_libsprt_c.h
@@ -35,4 +35,20 @@
*/
void *tfm_sprt_c_memcpy(void *dest, const void *src, size_t n);
+/**
+ * \brief Compare the first 'n' bytes of the memory areas 's1' and 's2'.
+ *
+ * \param[in] s1 The address of the first memory area
+ * \param[in] s2 The address of the second memory area
+ * \param[in] n The size(Byte) to compare
+ *
+ * \retval > 0 The first n bytes of s1 great than the first n
+ * bytes of s2
+ * \retval < 0 The first n bytes of s1 less than the first n
+ * bytes of s2
+ * \retval = 0 The first n bytes of s1 equal to the first n
+ * bytes of s2
+ */
+int tfm_sprt_c_memcmp(const void *s1, const void *s2, size_t n);
+
#endif /* __TFM_LIBSPRT_C_H__ */
diff --git a/secure_fw/lib/sprt/tfm_libsprt_c_memcmp.c b/secure_fw/lib/sprt/tfm_libsprt_c_memcmp.c
new file mode 100644
index 0000000..149e18c
--- /dev/null
+++ b/secure_fw/lib/sprt/tfm_libsprt_c_memcmp.c
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+
+int tfm_sprt_c_memcmp(const void *s1, const void *s2, size_t n)
+{
+ int result = 0;
+ const uint8_t *p1 = (const uint8_t *)s1;
+ const uint8_t *p2 = (const uint8_t *)s2;
+ while (n--) {
+ if ((*p1 != *p2) && (result == 0)) {
+ result = *p1 - *p2;
+ } else {
+ p1++;
+ p2++;
+ }
+ }
+ return result;
+}