Skip to content

Commit

Permalink
Separate out low-level mpi_safe_cond_assign
Browse files Browse the repository at this point in the history
Separate out a version of mpi_safe_cond_assign that works on
equal-sized limb arrays, without worrying about allocation sizes or
signs.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
  • Loading branch information
gilles-peskine-arm committed Jun 4, 2020
1 parent 2a82f72 commit f04d11e
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions library/bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,22 @@ void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
memcpy( Y, &T, sizeof( mbedtls_mpi ) );
}

/*
* Conditionally assign dest = src, without leaking information
* about whether the assignment was made or not.
* dest and src must be arrays of limbs of size n.
* assign must be 0 or 1.
*/
static void mpi_safe_cond_assign( size_t n,
mbedtls_mpi_uint *dest,
const mbedtls_mpi_uint *src,
unsigned char assign )
{
size_t i;
for( i = 0; i < n; i++ )
dest[i] = dest[i] * ( 1 - assign ) + src[i] * assign;
}

/*
* Conditionally assign X = Y, without leaking information
* about whether the assignment was made or not.
Expand All @@ -262,10 +278,9 @@ int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned

X->s = X->s * ( 1 - assign ) + Y->s * assign;

for( i = 0; i < Y->n; i++ )
X->p[i] = X->p[i] * ( 1 - assign ) + Y->p[i] * assign;
mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );

for( ; i < X->n; i++ )
for( i = Y->n; i < X->n; i++ )
X->p[i] *= ( 1 - assign );

cleanup:
Expand Down

0 comments on commit f04d11e

Please sign in to comment.