diff options
Diffstat (limited to 'src/lib/math/bigint')
-rw-r--r-- | src/lib/math/bigint/bigint.cpp | 15 | ||||
-rw-r--r-- | src/lib/math/bigint/bigint.h | 6 |
2 files changed, 21 insertions, 0 deletions
diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp index 495907d1a..5283c893c 100644 --- a/src/lib/math/bigint/bigint.cpp +++ b/src/lib/math/bigint/bigint.cpp @@ -341,6 +341,21 @@ void BigInt::binary_decode(const uint8_t buf[], size_t length) m_reg[length / WORD_BYTES] = (m_reg[length / WORD_BYTES] << 8) | buf[i]; } +void BigInt::ct_cond_assign(bool predicate, BigInt& other) + { + const size_t t_words = size(); + const size_t o_words = other.size(); + + const size_t r_words = std::max(t_words, o_words); + + const word mask = CT::expand_mask<word>(predicate); + + for(size_t i = 0; i != r_words; ++i) + { + this->set_word_at(i, CT::select<word>(mask, other.word_at(i), this->word_at(i))); + } + } + #if defined(BOTAN_HAS_VALGRIND) void BigInt::const_time_poison() const { diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h index 4a07723b7..8e09e4283 100644 --- a/src/lib/math/bigint/bigint.h +++ b/src/lib/math/bigint/bigint.h @@ -616,6 +616,12 @@ class BOTAN_PUBLIC_API(2,0) BigInt final */ void encode_words(word out[], size_t size) const; + /** + * If predicate is true assign other to *this + * Uses a masked operation to avoid side channels + */ + void ct_cond_assign(bool predicate, BigInt& other); + #if defined(BOTAN_HAS_VALGRIND) void const_time_poison() const; void const_time_unpoison() const; |