aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-02-28 02:43:57 -0500
committerJack Lloyd <[email protected]>2016-03-20 09:38:17 -0400
commitada363473a9491a3b07e3bb6fa2b5fd9f12aec98 (patch)
tree0dc7eefb24c3d9983e45dd6e2e7f0876179c8c11 /src/lib/utils
parentf70a9de37d22282d8cca465632efd0044ab9008c (diff)
Add PK_Decryptor::decrypt_or_random
Performs content checks on the value (expected length, expected bytes) and in constant time returns either the decrypted value or a random value.
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/ct_utils.h26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/lib/utils/ct_utils.h b/src/lib/utils/ct_utils.h
index 401a53e86..ec055374a 100644
--- a/src/lib/utils/ct_utils.h
+++ b/src/lib/utils/ct_utils.h
@@ -100,6 +100,12 @@ inline T select(T mask, T from0, T from1)
return (from0 & mask) | (from1 & ~mask);
}
+template<typename PredT, typename ValT>
+inline ValT val_or_zero(PredT pred_val, ValT val)
+ {
+ return select(CT::expand_mask<ValT>(pred_val), val, static_cast<ValT>(0));
+ }
+
template<typename T>
inline T is_zero(T x)
{
@@ -127,12 +133,28 @@ inline void conditional_copy_mem(T value,
T* to,
const T* from0,
const T* from1,
- size_t bytes)
+ size_t elems)
{
const T mask = CT::expand_mask(value);
- for(size_t i = 0; i != bytes; ++i)
+ for(size_t i = 0; i != elems; ++i)
+ {
to[i] = CT::select(mask, from0[i], from1[i]);
+ }
+ }
+
+template<typename T>
+inline void cond_zero_mem(T cond,
+ T* array,
+ size_t elems)
+ {
+ const T mask = CT::expand_mask(cond);
+ const T zero(0);
+
+ for(size_t i = 0; i != elems; ++i)
+ {
+ array[i] = CT::select(mask, zero, array[i]);
+ }
}
template<typename T>