2022-08-09 12:45:47 +02:00
|
|
|
/*
|
2022-08-19 13:09:17 +02:00
|
|
|
* Core bignum functions
|
2022-08-09 12:45:47 +02:00
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/platform_util.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define mbedtls_printf printf
|
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "bignum_core.h"
|
2022-08-24 12:08:51 +02:00
|
|
|
#include "bn_mul.h"
|
|
|
|
#include "constant_time_internal.h"
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-22 12:19:10 +02:00
|
|
|
size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
size_t j;
|
|
|
|
mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
|
|
|
|
|
|
|
|
for( j = 0; j < biL; j++ )
|
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
if( a & mask ) break;
|
2022-08-09 12:45:47 +02:00
|
|
|
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
|
2022-08-12 15:36:56 +02:00
|
|
|
return( j );
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
if( A_limbs == 0 )
|
2022-08-09 12:45:47 +02:00
|
|
|
return( 0 );
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
for( i = A_limbs - 1; i > 0; i-- )
|
|
|
|
if( A[i] != 0 )
|
2022-08-09 12:45:47 +02:00
|
|
|
break;
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
j = biL - mbedtls_mpi_core_clz( A[i] );
|
2022-08-09 12:45:47 +02:00
|
|
|
|
|
|
|
return( ( i * biL ) + j );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
|
|
|
|
* into the storage form used by mbedtls_mpi. */
|
2022-08-19 13:24:40 +02:00
|
|
|
static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
uint8_t i;
|
2022-08-19 13:24:40 +02:00
|
|
|
unsigned char *a_ptr;
|
2022-08-09 12:45:47 +02:00
|
|
|
mbedtls_mpi_uint tmp = 0;
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
tmp <<= CHAR_BIT;
|
2022-08-19 13:24:40 +02:00
|
|
|
tmp |= (mbedtls_mpi_uint) *a_ptr;
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( tmp );
|
|
|
|
}
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
#if defined(__BYTE_ORDER__)
|
|
|
|
|
|
|
|
/* Nothing to do on bigendian systems. */
|
|
|
|
#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
|
2022-08-19 13:24:40 +02:00
|
|
|
return( a );
|
2022-08-09 12:45:47 +02:00
|
|
|
#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
|
|
|
|
|
|
|
|
#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
|
|
|
|
|
|
|
|
/* For GCC and Clang, have builtins for byte swapping. */
|
|
|
|
#if defined(__GNUC__) && defined(__GNUC_PREREQ)
|
|
|
|
#if __GNUC_PREREQ(4,3)
|
|
|
|
#define have_bswap
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__clang__) && defined(__has_builtin)
|
|
|
|
#if __has_builtin(__builtin_bswap32) && \
|
|
|
|
__has_builtin(__builtin_bswap64)
|
|
|
|
#define have_bswap
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(have_bswap)
|
|
|
|
/* The compiler is hopefully able to statically evaluate this! */
|
|
|
|
switch( sizeof(mbedtls_mpi_uint) )
|
|
|
|
{
|
|
|
|
case 4:
|
2022-08-19 13:24:40 +02:00
|
|
|
return( __builtin_bswap32(a) );
|
2022-08-09 12:45:47 +02:00
|
|
|
case 8:
|
2022-08-19 13:24:40 +02:00
|
|
|
return( __builtin_bswap64(a) );
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
|
|
|
|
#endif /* __BYTE_ORDER__ */
|
|
|
|
|
|
|
|
/* Fall back to C-based reordering if we don't know the byte order
|
|
|
|
* or we couldn't use a compiler-specific builtin. */
|
2022-08-19 13:24:40 +02:00
|
|
|
return( mpi_bigendian_to_host_c( a ) );
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
|
2022-08-22 11:01:27 +02:00
|
|
|
size_t A_limbs )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *cur_limb_left;
|
|
|
|
mbedtls_mpi_uint *cur_limb_right;
|
2022-08-22 11:01:27 +02:00
|
|
|
if( A_limbs == 0 )
|
2022-08-09 12:45:47 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse limbs and
|
|
|
|
* - adapt byte-order in each limb
|
|
|
|
* - swap the limbs themselves.
|
|
|
|
* For that, simultaneously traverse the limbs from left to right
|
|
|
|
* and from right to left, as long as the left index is not bigger
|
|
|
|
* than the right index (it's not a problem if limbs is odd and the
|
|
|
|
* indices coincide in the last iteration).
|
|
|
|
*/
|
2022-08-22 11:01:27 +02:00
|
|
|
for( cur_limb_left = A, cur_limb_right = A + ( A_limbs - 1 );
|
2022-08-09 12:45:47 +02:00
|
|
|
cur_limb_left <= cur_limb_right;
|
|
|
|
cur_limb_left++, cur_limb_right-- )
|
|
|
|
{
|
|
|
|
mbedtls_mpi_uint tmp;
|
|
|
|
/* Note that if cur_limb_left == cur_limb_right,
|
|
|
|
* this code effectively swaps the bytes only once. */
|
2022-08-30 16:15:02 +02:00
|
|
|
tmp = mpi_bigendian_to_host( *cur_limb_left );
|
2022-08-09 12:45:47 +02:00
|
|
|
*cur_limb_left = mpi_bigendian_to_host( *cur_limb_right );
|
|
|
|
*cur_limb_right = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t X_limbs,
|
|
|
|
const unsigned char *input,
|
|
|
|
size_t input_length )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
const size_t limbs = CHARS_TO_LIMBS( input_length );
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
if( X_limbs < limbs )
|
2022-08-11 17:13:53 +02:00
|
|
|
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
|
|
|
|
|
|
|
if( X != NULL )
|
2022-08-12 14:11:56 +02:00
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
memset( X, 0, X_limbs * ciL );
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
for( size_t i = 0; i < input_length; i++ )
|
2022-08-19 13:05:28 +02:00
|
|
|
{
|
|
|
|
size_t offset = ( ( i % ciL ) << 3 );
|
|
|
|
X[i / ciL] |= ( (mbedtls_mpi_uint) input[i] ) << offset;
|
|
|
|
}
|
2022-08-12 14:11:56 +02:00
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-11 17:13:53 +02:00
|
|
|
return( 0 );
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t X_limbs,
|
|
|
|
const unsigned char *input,
|
|
|
|
size_t input_length )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
const size_t limbs = CHARS_TO_LIMBS( input_length );
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
if( X_limbs < limbs )
|
2022-08-11 17:13:53 +02:00
|
|
|
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
/* If X_limbs is 0, input_length must also be 0 (from previous test).
|
|
|
|
* Nothing to do. */
|
|
|
|
if( X_limbs == 0 )
|
2022-08-12 17:47:39 +02:00
|
|
|
return( 0 );
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
memset( X, 0, X_limbs * ciL );
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-12 17:47:39 +02:00
|
|
|
/* memcpy() with (NULL, 0) is undefined behaviour */
|
2022-08-19 13:24:40 +02:00
|
|
|
if( input_length != 0 )
|
2022-08-12 17:47:39 +02:00
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t overhead = ( X_limbs * ciL ) - input_length;
|
2022-08-12 17:47:39 +02:00
|
|
|
unsigned char *Xp = (unsigned char *) X;
|
2022-08-19 13:24:40 +02:00
|
|
|
memcpy( Xp + overhead, input, input_length );
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
|
2022-08-12 17:47:39 +02:00
|
|
|
|
2022-08-11 17:13:53 +02:00
|
|
|
return( 0 );
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
|
|
|
|
size_t A_limbs,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_length )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t stored_bytes = A_limbs * ciL;
|
2022-08-09 12:45:47 +02:00
|
|
|
size_t bytes_to_copy;
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
if( stored_bytes < output_length )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
bytes_to_copy = stored_bytes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
bytes_to_copy = output_length;
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-22 10:06:32 +02:00
|
|
|
/* The output buffer is smaller than the allocated size of A.
|
2022-08-19 13:24:40 +02:00
|
|
|
* However A may fit if its leading bytes are zero. */
|
2022-08-15 13:08:49 +02:00
|
|
|
for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
if( GET_BYTE( A, i ) != 0 )
|
2022-08-09 12:45:47 +02:00
|
|
|
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 13:08:49 +02:00
|
|
|
for( size_t i = 0; i < bytes_to_copy; i++ )
|
2022-08-19 13:24:40 +02:00
|
|
|
output[i] = GET_BYTE( A, i );
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
if( stored_bytes < output_length )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
/* Write trailing 0 bytes */
|
2022-08-19 13:24:40 +02:00
|
|
|
memset( output + stored_bytes, 0, output_length - stored_bytes );
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t X_limbs,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_length )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
size_t stored_bytes;
|
|
|
|
size_t bytes_to_copy;
|
|
|
|
unsigned char *p;
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
stored_bytes = X_limbs * ciL;
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
if( stored_bytes < output_length )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2022-08-22 10:06:32 +02:00
|
|
|
/* There is enough space in the output buffer. Write initial
|
2022-08-09 12:45:47 +02:00
|
|
|
* null bytes and record the position at which to start
|
|
|
|
* writing the significant bytes. In this case, the execution
|
|
|
|
* trace of this function does not depend on the value of the
|
|
|
|
* number. */
|
|
|
|
bytes_to_copy = stored_bytes;
|
2022-08-19 13:24:40 +02:00
|
|
|
p = output + output_length - stored_bytes;
|
|
|
|
memset( output, 0, output_length - stored_bytes );
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-22 10:06:32 +02:00
|
|
|
/* The output buffer is smaller than the allocated size of X.
|
2022-08-09 12:45:47 +02:00
|
|
|
* However X may fit if its leading bytes are zero. */
|
2022-08-19 13:24:40 +02:00
|
|
|
bytes_to_copy = output_length;
|
|
|
|
p = output;
|
2022-08-15 13:08:49 +02:00
|
|
|
for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
if( GET_BYTE( X, i ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 13:08:49 +02:00
|
|
|
for( size_t i = 0; i < bytes_to_copy; i++ )
|
2022-08-09 12:45:47 +02:00
|
|
|
p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-09-15 16:55:07 +02:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
2022-08-30 12:57:22 +02:00
|
|
|
size_t limbs,
|
|
|
|
unsigned cond )
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-08-30 16:15:02 +02:00
|
|
|
mbedtls_mpi_uint c = 0;
|
|
|
|
|
2022-09-15 16:00:07 +02:00
|
|
|
/* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
|
|
|
|
const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask( cond );
|
2022-08-30 18:41:23 +02:00
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
for( size_t i = 0; i < limbs; i++ )
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-09-15 16:55:07 +02:00
|
|
|
mbedtls_mpi_uint add = mask & A[i];
|
|
|
|
mbedtls_mpi_uint t = c + X[i];
|
|
|
|
c = ( t < X[i] );
|
2022-08-30 16:15:02 +02:00
|
|
|
t += add;
|
|
|
|
c += ( t < add );
|
2022-09-15 16:55:07 +02:00
|
|
|
X[i] = t;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
2022-08-30 16:15:02 +02:00
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
return( c );
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
size_t limbs )
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-08-30 12:57:22 +02:00
|
|
|
mbedtls_mpi_uint c = 0;
|
2022-08-24 12:08:51 +02:00
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
for( size_t i = 0; i < limbs; i++ )
|
|
|
|
{
|
|
|
|
mbedtls_mpi_uint z = ( A[i] < c );
|
|
|
|
mbedtls_mpi_uint t = A[i] - c;
|
|
|
|
c = ( t < B[i] ) + z;
|
|
|
|
X[i] = t - B[i];
|
|
|
|
}
|
2022-08-24 12:08:51 +02:00
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
return( c );
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
|
|
|
|
const mbedtls_mpi_uint *s, size_t s_len,
|
|
|
|
mbedtls_mpi_uint b )
|
|
|
|
{
|
|
|
|
mbedtls_mpi_uint c = 0; /* carry */
|
2022-08-30 15:31:49 +02:00
|
|
|
/*
|
|
|
|
* It is a documented precondition of this function that d_len >= s_len.
|
|
|
|
* If that's not the case, we swap these round: this turns what would be
|
|
|
|
* a buffer overflow into an incorrect result.
|
|
|
|
*/
|
2022-08-24 12:08:51 +02:00
|
|
|
if( d_len < s_len )
|
|
|
|
s_len = d_len;
|
|
|
|
size_t excess_len = d_len - s_len;
|
|
|
|
size_t steps_x8 = s_len / 8;
|
|
|
|
size_t steps_x1 = s_len & 7;
|
|
|
|
|
|
|
|
while( steps_x8-- )
|
|
|
|
{
|
|
|
|
MULADDC_X8_INIT
|
|
|
|
MULADDC_X8_CORE
|
|
|
|
MULADDC_X8_STOP
|
|
|
|
}
|
|
|
|
|
|
|
|
while( steps_x1-- )
|
|
|
|
{
|
|
|
|
MULADDC_X1_INIT
|
|
|
|
MULADDC_X1_CORE
|
|
|
|
MULADDC_X1_STOP
|
|
|
|
}
|
|
|
|
|
|
|
|
while( excess_len-- )
|
|
|
|
{
|
2022-08-30 16:15:02 +02:00
|
|
|
*d += c;
|
|
|
|
c = ( *d < c );
|
|
|
|
d++;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( c );
|
|
|
|
}
|
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
/*
|
|
|
|
* Fast Montgomery initialization (thanks to Tom St Denis).
|
|
|
|
*/
|
2022-09-15 16:05:59 +02:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N )
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-08-30 12:57:22 +02:00
|
|
|
mbedtls_mpi_uint x = N[0];
|
2022-08-24 12:08:51 +02:00
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
x += ( ( N[0] + 2 ) & 4 ) << 1;
|
2022-08-24 12:08:51 +02:00
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
for( unsigned int i = biL; i >= 8; i /= 2 )
|
|
|
|
x *= ( 2 - ( N[0] * x ) );
|
|
|
|
|
|
|
|
return( ~x + 1 );
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
size_t B_limbs,
|
|
|
|
const mbedtls_mpi_uint *N,
|
|
|
|
size_t AN_limbs,
|
|
|
|
mbedtls_mpi_uint mm,
|
|
|
|
mbedtls_mpi_uint *T )
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-08-30 12:57:22 +02:00
|
|
|
memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
|
|
|
|
|
2022-09-02 14:28:59 +02:00
|
|
|
for( size_t i = 0; i < AN_limbs; i++ )
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-08-30 12:57:22 +02:00
|
|
|
/* T = (T + u0*B + u1*N) / 2^biL */
|
2022-08-31 18:57:34 +02:00
|
|
|
mbedtls_mpi_uint u0 = A[i];
|
|
|
|
mbedtls_mpi_uint u1 = ( T[0] + u0 * B[0] ) * mm;
|
2022-08-30 12:57:22 +02:00
|
|
|
|
|
|
|
(void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
|
|
|
|
(void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
|
2022-09-02 14:28:59 +02:00
|
|
|
|
|
|
|
T++;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
2022-08-30 12:57:22 +02:00
|
|
|
|
2022-08-31 18:57:34 +02:00
|
|
|
/*
|
|
|
|
* The result we want is (T >= N) ? T - N : T.
|
|
|
|
*
|
|
|
|
* For better constant-time properties in this function, we always do the
|
|
|
|
* subtraction, with the result in X.
|
|
|
|
*
|
|
|
|
* We also look to see if there was any carry in the final additions in the
|
|
|
|
* loop above.
|
|
|
|
*/
|
2022-08-30 12:57:22 +02:00
|
|
|
|
|
|
|
mbedtls_mpi_uint carry = T[AN_limbs];
|
|
|
|
mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
|
|
|
|
|
|
|
|
/*
|
2022-08-31 18:57:34 +02:00
|
|
|
* Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
2022-08-31 18:57:34 +02:00
|
|
|
* T can be in one of 3 ranges:
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
2022-08-31 18:57:34 +02:00
|
|
|
* 1) T < N : (carry, borrow) = (0, 1): we want T
|
|
|
|
* 2) N <= T < R : (carry, borrow) = (0, 0): we want X
|
|
|
|
* 3) T >= R : (carry, borrow) = (1, 1): we want X
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
2022-08-31 18:57:34 +02:00
|
|
|
* and (carry, borrow) = (1, 0) can't happen.
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
|
|
|
* So the correct return value is already in X if (carry ^ borrow) = 0,
|
|
|
|
* but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
|
|
|
|
*/
|
2022-09-29 15:40:21 +02:00
|
|
|
mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-09 12:45:47 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|