From ada363473a9491a3b07e3bb6fa2b5fd9f12aec98 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Sun, 28 Feb 2016 02:43:57 -0500 Subject: 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. --- src/lib/utils/ct_utils.h | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/lib/utils') 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 +inline ValT val_or_zero(PredT pred_val, ValT val) + { + return select(CT::expand_mask(pred_val), val, static_cast(0)); + } + template 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 +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 -- cgit v1.2.3