diff options
author | Michael Brown <mcb30@ipxe.org> | 2024-01-19 12:34:02 +0000 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2024-01-19 12:34:02 +0000 |
commit | bac13ba1f658a1e742b9ceb958e670086affebe7 (patch) | |
tree | 912fd8cd64b62fc7b6eb2242b97a0f68dec0a0d5 /src/crypto/bigint.c | |
parent | 13e390d54edde17c8e22b0f6d8897c273a91c5d0 (diff) | |
download | ipxe-bac13ba1f658a1e742b9ceb958e670086affebe7.tar.gz |
[crypto] Add bigint_swap() to conditionally swap big integers
Add a helper function bigint_swap() that can be used to conditionally
swap a pair of big integers in constant time.
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/crypto/bigint.c')
-rw-r--r-- | src/crypto/bigint.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/crypto/bigint.c b/src/crypto/bigint.c index ac9670ef6..656f979e5 100644 --- a/src/crypto/bigint.c +++ b/src/crypto/bigint.c @@ -51,6 +51,31 @@ static struct profiler bigint_mod_multiply_subtract_profiler __profiler = { .name = "bigint_mod_multiply.subtract" }; /** + * Conditionally swap big integers (in constant time) + * + * @v first0 Element 0 of big integer to be conditionally swapped + * @v second0 Element 0 of big integer to be conditionally swapped + * @v size Number of elements in big integers + * @v swap Swap first and second big integers + */ +void bigint_swap_raw ( bigint_element_t *first0, bigint_element_t *second0, + unsigned int size, int swap ) { + bigint_element_t mask; + bigint_element_t xor; + unsigned int i; + + /* Construct mask */ + mask = ( ( bigint_element_t ) ( ! swap ) - 1 ); + + /* Conditionally swap elements */ + for ( i = 0 ; i < size ; i++ ) { + xor = ( mask & ( first0[i] ^ second0[i] ) ); + first0[i] ^= xor; + second0[i] ^= xor; + } +} + +/** * Perform modular multiplication of big integers * * @v multiplicand0 Element 0 of big integer to be multiplied |