[PATCH v2 4/5] util: add l_secure_select_byte
James Prestwood <prestwoj at gmail.com>
| Newsgroups | dev.linux.lists.ell |
|---|---|
| Message-ID | <[email protected]> |
This was taken from IWD and modified slightly to act off a boolean
rather than a 0xff/0x00 byte to stay consistent with l_secure_select
---
ell/util.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/ell/util.h b/ell/util.h
index 9d4e36c..d390d27 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -425,6 +425,21 @@ static inline void l_secure_select(bool select_left,
o[i] = r[i] ^ ((l[i] ^ r[i]) & mask);
}
+/*
+ * Returns either true_value or false_value (depending if mask is 0xFF or 0x00
+ * respectively).
+ * This constant time selection method allows to keep an identical memory
+ * access pattern.
+ */
+static inline uint8_t l_secure_select_byte(bool select_true,
+ const uint8_t true_value,
+ const uint8_t false_value)
+{
+ uint8_t mask = -(!!select_true);
+
+ return (mask & true_value) | (~mask & false_value);
+}
+
#ifdef __cplusplus
}
#endif
--
2.31.1