mirror of
				https://github.com/cuberite/polarssl.git
				synced 2025-11-04 04:32:24 -05:00 
			
		
		
		
	Add little endian import to Bignum
The function `mbedtls_mpi_read_binary()` expects big endian byte order, but we need to be able to read from little endian in some caseses. (For example when handling keys corresponding to Montgomery curves.) Used `echo xx | tac -rs .. | tr [a-z] [A-Z]` to transform the test data to little endian and `echo "ibase=16;xx" | bc` to convert to decimal.
This commit is contained in:
		
							parent
							
								
									3081629de4
								
							
						
					
					
						commit
						a778a94b7d
					
				@ -490,8 +490,24 @@ int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf,
 | 
				
			|||||||
                             size_t buflen );
 | 
					                             size_t buflen );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * \brief          Export an MPI into unsigned big endian binary data
 | 
					 * \brief          Import X from unsigned binary data, little endian
 | 
				
			||||||
 *                 of fixed size.
 | 
					 *
 | 
				
			||||||
 | 
					 * \param X        The destination MPI. This must point to an initialized MPI.
 | 
				
			||||||
 | 
					 * \param buf      The input buffer. This must be a readable buffer of length
 | 
				
			||||||
 | 
					 *                 \p buflen Bytes.
 | 
				
			||||||
 | 
					 * \param buflen   The length of the input buffer \p p in Bytes.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * \return         \c 0 if successful.
 | 
				
			||||||
 | 
					 * \return         #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
 | 
				
			||||||
 | 
					 * \return         Another negative error code on different kinds of failure.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
 | 
				
			||||||
 | 
					                                const unsigned char *buf, size_t buflen );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * \brief          Export X into unsigned binary data, big endian.
 | 
				
			||||||
 | 
					 *                 Always fills the whole buffer, which will start with zeros
 | 
				
			||||||
 | 
					 *                 if the number is smaller.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * \param X        The source MPI. This must point to an initialized MPI.
 | 
					 * \param X        The source MPI. This must point to an initialized MPI.
 | 
				
			||||||
 * \param buf      The output buffer. This must be a writable buffer of length
 | 
					 * \param buf      The output buffer. This must be a writable buffer of length
 | 
				
			||||||
 | 
				
			|||||||
@ -813,6 +813,34 @@ static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Import X from unsigned binary data, little endian
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
 | 
				
			||||||
 | 
					                                const unsigned char *buf, size_t buflen )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int ret;
 | 
				
			||||||
 | 
					    size_t i;
 | 
				
			||||||
 | 
					    size_t const limbs = CHARS_TO_LIMBS( buflen );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Ensure that target MPI has exactly the necessary number of limbs */
 | 
				
			||||||
 | 
					    if( X->n != limbs )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        mbedtls_mpi_free( X );
 | 
				
			||||||
 | 
					        mbedtls_mpi_init( X );
 | 
				
			||||||
 | 
					        MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for( i = 0; i < buflen; i++ )
 | 
				
			||||||
 | 
					        X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					cleanup:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return( ret );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Import X from unsigned binary data, big endian
 | 
					 * Import X from unsigned binary data, big endian
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
				
			|||||||
@ -58,6 +58,9 @@ mpi_read_write_string:16:"-1":16:"":3:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
 | 
				
			|||||||
Base test mbedtls_mpi_read_binary #1
 | 
					Base test mbedtls_mpi_read_binary #1
 | 
				
			||||||
mbedtls_mpi_read_binary:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924"
 | 
					mbedtls_mpi_read_binary:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Base test mbedtls_mpi_read_binary_le #1
 | 
				
			||||||
 | 
					mbedtls_mpi_read_binary_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":10:"219946662473865722255717126709915431768051735954189829340600976826409773245337023925691629251672268961177825243440202069039100741562168093042339401187848509859789949044607421190014088260008793380554914226244485299326152319899746569"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Base test mbedtls_mpi_write_binary #1
 | 
					Base test mbedtls_mpi_write_binary #1
 | 
				
			||||||
mbedtls_mpi_write_binary:10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924":"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":200:0
 | 
					mbedtls_mpi_write_binary:10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924":"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":200:0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -328,6 +328,25 @@ exit:
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
/* END_CASE */
 | 
					/* END_CASE */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* BEGIN_CASE */
 | 
				
			||||||
 | 
					void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    mbedtls_mpi X;
 | 
				
			||||||
 | 
					    unsigned char str[1000];
 | 
				
			||||||
 | 
					    size_t len;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    mbedtls_mpi_init( &X );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
 | 
				
			||||||
 | 
					    TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
 | 
				
			||||||
 | 
					    TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exit:
 | 
				
			||||||
 | 
					    mbedtls_mpi_free( &X );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/* END_CASE */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* BEGIN_CASE */
 | 
					/* BEGIN_CASE */
 | 
				
			||||||
void mbedtls_mpi_write_binary( int radix_X, char * input_X,
 | 
					void mbedtls_mpi_write_binary( int radix_X, char * input_X,
 | 
				
			||||||
                               data_t * input_A, int output_size,
 | 
					                               data_t * input_A, int output_size,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user