Lines Matching refs:X
91 static void mpi_init( mbedtls_mpi *X, short use_mempool ) in mpi_init() argument
93 MPI_VALIDATE( X != NULL ); in mpi_init()
95 X->s = 1; in mpi_init()
96 X->use_mempool = use_mempool; in mpi_init()
97 X->n = 0; in mpi_init()
98 X->p = NULL; in mpi_init()
101 void mbedtls_mpi_init( mbedtls_mpi *X ) in mbedtls_mpi_init() argument
103 mpi_init( X, 0 /*use_mempool*/ ); in mbedtls_mpi_init()
106 void mbedtls_mpi_init_mempool( mbedtls_mpi *X ) in mbedtls_mpi_init_mempool() argument
108 mpi_init( X, !!mbedtls_mpi_mempool /*use_mempool*/ ); in mbedtls_mpi_init_mempool()
114 void mbedtls_mpi_free( mbedtls_mpi *X ) in mbedtls_mpi_free() argument
116 if( X == NULL ) in mbedtls_mpi_free()
119 if( X->p != NULL ) in mbedtls_mpi_free()
121 mbedtls_mpi_zeroize( X->p, X->n ); in mbedtls_mpi_free()
122 if( X->use_mempool ) in mbedtls_mpi_free()
123 mempool_free( mbedtls_mpi_mempool, X->p ); in mbedtls_mpi_free()
125 mbedtls_free( X->p ); in mbedtls_mpi_free()
128 X->s = 1; in mbedtls_mpi_free()
129 X->n = 0; in mbedtls_mpi_free()
130 X->p = NULL; in mbedtls_mpi_free()
136 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ) in mbedtls_mpi_grow() argument
139 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_grow()
144 if( X->n < nblimbs ) in mbedtls_mpi_grow()
146 if( X->use_mempool ) in mbedtls_mpi_grow()
160 if( X->p != NULL ) in mbedtls_mpi_grow()
162 memcpy( p, X->p, X->n * ciL ); in mbedtls_mpi_grow()
163 mbedtls_mpi_zeroize( X->p, X->n ); in mbedtls_mpi_grow()
164 if( X->use_mempool ) in mbedtls_mpi_grow()
165 mempool_free( mbedtls_mpi_mempool, X->p); in mbedtls_mpi_grow()
167 mbedtls_free( X->p ); in mbedtls_mpi_grow()
170 X->n = nblimbs; in mbedtls_mpi_grow()
171 X->p = p; in mbedtls_mpi_grow()
181 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ) in mbedtls_mpi_shrink() argument
185 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_shrink()
191 if( X->n <= nblimbs ) in mbedtls_mpi_shrink()
192 return( mbedtls_mpi_grow( X, nblimbs ) ); in mbedtls_mpi_shrink()
195 for( i = X->n - 1; i > 0; i-- ) in mbedtls_mpi_shrink()
196 if( X->p[i] != 0 ) in mbedtls_mpi_shrink()
203 if( X->use_mempool ) in mbedtls_mpi_shrink()
217 if( X->p != NULL ) in mbedtls_mpi_shrink()
219 memcpy( p, X->p, i * ciL ); in mbedtls_mpi_shrink()
220 mbedtls_mpi_zeroize( X->p, X->n ); in mbedtls_mpi_shrink()
221 if( X->use_mempool ) in mbedtls_mpi_shrink()
222 mempool_free( mbedtls_mpi_mempool, X->p ); in mbedtls_mpi_shrink()
224 mbedtls_free( X->p ); in mbedtls_mpi_shrink()
227 X->n = i; in mbedtls_mpi_shrink()
228 X->p = p; in mbedtls_mpi_shrink()
234 static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs ) in mbedtls_mpi_resize_clear() argument
238 mbedtls_mpi_free( X ); in mbedtls_mpi_resize_clear()
241 else if( X->n == limbs ) in mbedtls_mpi_resize_clear()
243 memset( X->p, 0, limbs * ciL ); in mbedtls_mpi_resize_clear()
244 X->s = 1; in mbedtls_mpi_resize_clear()
249 mbedtls_mpi_free( X ); in mbedtls_mpi_resize_clear()
250 return( mbedtls_mpi_grow( X, limbs ) ); in mbedtls_mpi_resize_clear()
263 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y ) in mbedtls_mpi_copy() argument
267 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_copy()
270 if( X == Y ) in mbedtls_mpi_copy()
275 if( X->n != 0 ) in mbedtls_mpi_copy()
277 X->s = 1; in mbedtls_mpi_copy()
278 memset( X->p, 0, X->n * ciL ); in mbedtls_mpi_copy()
288 X->s = Y->s; in mbedtls_mpi_copy()
290 if( X->n < i ) in mbedtls_mpi_copy()
292 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) ); in mbedtls_mpi_copy()
296 memset( X->p + i, 0, ( X->n - i ) * ciL ); in mbedtls_mpi_copy()
299 memcpy( X->p, Y->p, i * ciL ); in mbedtls_mpi_copy()
309 void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y ) in mbedtls_mpi_swap() argument
312 MPI_VALIDATE( X != NULL ); in mbedtls_mpi_swap()
315 memcpy( &T, X, sizeof( mbedtls_mpi ) ); in mbedtls_mpi_swap()
316 memcpy( X, Y, sizeof( mbedtls_mpi ) ); in mbedtls_mpi_swap()
323 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z ) in mbedtls_mpi_lset() argument
326 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_lset()
328 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) ); in mbedtls_mpi_lset()
329 memset( X->p, 0, X->n * ciL ); in mbedtls_mpi_lset()
331 X->p[0] = ( z < 0 ) ? -z : z; in mbedtls_mpi_lset()
332 X->s = ( z < 0 ) ? -1 : 1; in mbedtls_mpi_lset()
342 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos ) in mbedtls_mpi_get_bit() argument
344 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_get_bit()
346 if( X->n * biL <= pos ) in mbedtls_mpi_get_bit()
349 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 ); in mbedtls_mpi_get_bit()
353 #define GET_BYTE( X, i ) \ argument
354 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
359 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val ) in mbedtls_mpi_set_bit() argument
364 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_set_bit()
369 if( X->n * biL <= pos ) in mbedtls_mpi_set_bit()
374 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) ); in mbedtls_mpi_set_bit()
377 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx ); in mbedtls_mpi_set_bit()
378 X->p[off] |= (mbedtls_mpi_uint) val << idx; in mbedtls_mpi_set_bit()
388 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ) in mbedtls_mpi_lsb() argument
391 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 ); in mbedtls_mpi_lsb()
393 for( i = 0; i < X->n; i++ ) in mbedtls_mpi_lsb()
395 if( ( ( X->p[i] >> j ) & 1 ) != 0 ) in mbedtls_mpi_lsb()
422 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X ) in mbedtls_mpi_bitlen() argument
426 if( X->n == 0 ) in mbedtls_mpi_bitlen()
429 for( i = X->n - 1; i > 0; i-- ) in mbedtls_mpi_bitlen()
430 if( X->p[i] != 0 ) in mbedtls_mpi_bitlen()
433 j = biL - mbedtls_clz( X->p[i] ); in mbedtls_mpi_bitlen()
441 size_t mbedtls_mpi_size( const mbedtls_mpi *X ) in mbedtls_mpi_size() argument
443 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 ); in mbedtls_mpi_size()
466 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ) in mbedtls_mpi_read_string() argument
473 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_read_string()
483 mbedtls_mpi_free( X ); in mbedtls_mpi_read_string()
502 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) ); in mbedtls_mpi_read_string()
503 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); in mbedtls_mpi_read_string()
508 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 ); in mbedtls_mpi_read_string()
513 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); in mbedtls_mpi_read_string()
518 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) ); in mbedtls_mpi_read_string()
519 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) ); in mbedtls_mpi_read_string()
523 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 ) in mbedtls_mpi_read_string()
524 X->s = -1; in mbedtls_mpi_read_string()
536 static int mpi_write_hlp( mbedtls_mpi *X, int radix, in mpi_write_hlp() argument
551 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) ); in mpi_write_hlp()
552 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) ); in mpi_write_hlp()
562 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 ); in mpi_write_hlp()
575 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, in mbedtls_mpi_write_string() argument
582 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_write_string()
589 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */ in mbedtls_mpi_write_string()
613 if( X->s == -1 ) in mbedtls_mpi_write_string()
624 for( i = X->n, k = 0; i > 0; i-- ) in mbedtls_mpi_write_string()
628 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF; in mbedtls_mpi_write_string()
641 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) ); in mbedtls_mpi_write_string()
663 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin ) in mbedtls_mpi_read_file() argument
674 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_read_file()
696 return( mbedtls_mpi_read_string( X, radix, p + 1 ) ); in mbedtls_mpi_read_file()
702 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout ) in mbedtls_mpi_write_file() argument
711 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_write_file()
718 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) ); in mbedtls_mpi_write_file()
836 int mbedtls_mpi_read_binary_le( mbedtls_mpi *X, in mbedtls_mpi_read_binary_le() argument
844 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) ); in mbedtls_mpi_read_binary_le()
847 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3); in mbedtls_mpi_read_binary_le()
862 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen ) in mbedtls_mpi_read_binary() argument
869 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_read_binary()
873 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) ); in mbedtls_mpi_read_binary()
879 Xp = (unsigned char*) X->p; in mbedtls_mpi_read_binary()
882 mpi_bigendian_to_host( X->p, limbs ); in mbedtls_mpi_read_binary()
898 int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X, in mbedtls_mpi_write_binary_le() argument
901 size_t stored_bytes = X->n * ciL; in mbedtls_mpi_write_binary_le()
917 if( GET_BYTE( X, i ) != 0 ) in mbedtls_mpi_write_binary_le()
923 buf[i] = GET_BYTE( X, i ); in mbedtls_mpi_write_binary_le()
937 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, in mbedtls_mpi_write_binary() argument
945 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_write_binary()
948 stored_bytes = X->n * ciL; in mbedtls_mpi_write_binary()
969 if( GET_BYTE( X, i ) != 0 ) in mbedtls_mpi_write_binary()
975 p[bytes_to_copy - i - 1] = GET_BYTE( X, i ); in mbedtls_mpi_write_binary()
983 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ) in mbedtls_mpi_shift_l() argument
988 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_shift_l()
993 i = mbedtls_mpi_bitlen( X ) + count; in mbedtls_mpi_shift_l()
995 if( X->n * biL < i ) in mbedtls_mpi_shift_l()
996 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) ); in mbedtls_mpi_shift_l()
1005 for( i = X->n; i > v0; i-- ) in mbedtls_mpi_shift_l()
1006 X->p[i - 1] = X->p[i - v0 - 1]; in mbedtls_mpi_shift_l()
1009 X->p[i - 1] = 0; in mbedtls_mpi_shift_l()
1017 for( i = v0; i < X->n; i++ ) in mbedtls_mpi_shift_l()
1019 r1 = X->p[i] >> (biL - t1); in mbedtls_mpi_shift_l()
1020 X->p[i] <<= t1; in mbedtls_mpi_shift_l()
1021 X->p[i] |= r0; in mbedtls_mpi_shift_l()
1034 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ) in mbedtls_mpi_shift_r() argument
1038 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_shift_r()
1043 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) ) in mbedtls_mpi_shift_r()
1044 return mbedtls_mpi_lset( X, 0 ); in mbedtls_mpi_shift_r()
1051 for( i = 0; i < X->n - v0; i++ ) in mbedtls_mpi_shift_r()
1052 X->p[i] = X->p[i + v0]; in mbedtls_mpi_shift_r()
1054 for( ; i < X->n; i++ ) in mbedtls_mpi_shift_r()
1055 X->p[i] = 0; in mbedtls_mpi_shift_r()
1063 for( i = X->n; i > 0; i-- ) in mbedtls_mpi_shift_r()
1065 r1 = X->p[i - 1] << (biL - v1); in mbedtls_mpi_shift_r()
1066 X->p[i - 1] >>= v1; in mbedtls_mpi_shift_r()
1067 X->p[i - 1] |= r0; in mbedtls_mpi_shift_r()
1078 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ) in mbedtls_mpi_cmp_abs() argument
1081 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_cmp_abs()
1084 for( i = X->n; i > 0; i-- ) in mbedtls_mpi_cmp_abs()
1085 if( X->p[i - 1] != 0 ) in mbedtls_mpi_cmp_abs()
1100 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 ); in mbedtls_mpi_cmp_abs()
1101 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 ); in mbedtls_mpi_cmp_abs()
1110 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y ) in mbedtls_mpi_cmp_mpi() argument
1113 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_cmp_mpi()
1116 for( i = X->n; i > 0; i-- ) in mbedtls_mpi_cmp_mpi()
1117 if( X->p[i - 1] != 0 ) in mbedtls_mpi_cmp_mpi()
1127 if( i > j ) return( X->s ); in mbedtls_mpi_cmp_mpi()
1130 if( X->s > 0 && Y->s < 0 ) return( 1 ); in mbedtls_mpi_cmp_mpi()
1131 if( Y->s > 0 && X->s < 0 ) return( -1 ); in mbedtls_mpi_cmp_mpi()
1135 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s ); in mbedtls_mpi_cmp_mpi()
1136 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s ); in mbedtls_mpi_cmp_mpi()
1145 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z ) in mbedtls_mpi_cmp_int() argument
1149 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_cmp_int()
1156 return( mbedtls_mpi_cmp_mpi( X, &Y ) ); in mbedtls_mpi_cmp_int()
1162 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_add_abs() argument
1167 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_add_abs()
1171 if( X == B ) in mbedtls_mpi_add_abs()
1173 const mbedtls_mpi *T = A; A = X; B = T; in mbedtls_mpi_add_abs()
1176 if( X != A ) in mbedtls_mpi_add_abs()
1177 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) ); in mbedtls_mpi_add_abs()
1182 X->s = 1; in mbedtls_mpi_add_abs()
1188 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) ); in mbedtls_mpi_add_abs()
1190 o = B->p; p = X->p; c = 0; in mbedtls_mpi_add_abs()
1204 if( i >= X->n ) in mbedtls_mpi_add_abs()
1206 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) ); in mbedtls_mpi_add_abs()
1207 p = X->p + i; in mbedtls_mpi_add_abs()
1255 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_sub_abs() argument
1260 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_sub_abs()
1274 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) ); in mbedtls_mpi_sub_abs()
1280 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL ); in mbedtls_mpi_sub_abs()
1281 if( X->n > A->n ) in mbedtls_mpi_sub_abs()
1282 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL ); in mbedtls_mpi_sub_abs()
1284 carry = mpi_sub_hlp( n, X->p, A->p, B->p ); in mbedtls_mpi_sub_abs()
1288 for( ; n < X->n && X->p[n] == 0; n++ ) in mbedtls_mpi_sub_abs()
1289 --X->p[n]; in mbedtls_mpi_sub_abs()
1292 if( n == X->n ) in mbedtls_mpi_sub_abs()
1297 --X->p[n]; in mbedtls_mpi_sub_abs()
1301 X->s = 1; in mbedtls_mpi_sub_abs()
1310 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_add_mpi() argument
1313 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_add_mpi()
1322 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) ); in mbedtls_mpi_add_mpi()
1323 X->s = s; in mbedtls_mpi_add_mpi()
1327 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) ); in mbedtls_mpi_add_mpi()
1328 X->s = -s; in mbedtls_mpi_add_mpi()
1333 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) ); in mbedtls_mpi_add_mpi()
1334 X->s = s; in mbedtls_mpi_add_mpi()
1345 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_sub_mpi() argument
1348 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_sub_mpi()
1357 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) ); in mbedtls_mpi_sub_mpi()
1358 X->s = s; in mbedtls_mpi_sub_mpi()
1362 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) ); in mbedtls_mpi_sub_mpi()
1363 X->s = -s; in mbedtls_mpi_sub_mpi()
1368 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) ); in mbedtls_mpi_sub_mpi()
1369 X->s = s; in mbedtls_mpi_sub_mpi()
1380 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ) in mbedtls_mpi_add_int() argument
1384 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_add_int()
1392 return( mbedtls_mpi_add_mpi( X, A, &B ) ); in mbedtls_mpi_add_int()
1398 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ) in mbedtls_mpi_sub_int() argument
1402 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_sub_int()
1410 return( mbedtls_mpi_sub_mpi( X, A, &B ) ); in mbedtls_mpi_sub_int()
1504 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_mul_mpi() argument
1510 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_mul_mpi()
1516 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; } in mbedtls_mpi_mul_mpi()
1517 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; } in mbedtls_mpi_mul_mpi()
1531 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) ); in mbedtls_mpi_mul_mpi()
1532 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); in mbedtls_mpi_mul_mpi()
1535 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] ); in mbedtls_mpi_mul_mpi()
1542 X->s = 1; in mbedtls_mpi_mul_mpi()
1544 X->s = A->s * B->s; in mbedtls_mpi_mul_mpi()
1556 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b ) in mbedtls_mpi_mul_int() argument
1558 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_mul_int()
1570 return( mbedtls_mpi_lset( X, 0 ) ); in mbedtls_mpi_mul_int()
1583 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) ); in mbedtls_mpi_mul_int()
1584 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) ); in mbedtls_mpi_mul_int()
1585 mpi_mul_hlp( n, A->p, X->p, b - 1 ); in mbedtls_mpi_mul_int()
1695 mbedtls_mpi X, Y, Z, T1, T2; in mbedtls_mpi_div_mpi() local
1703 mbedtls_mpi_init_mempool( &X ); mbedtls_mpi_init_mempool( &Y ); in mbedtls_mpi_div_mpi()
1723 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) ); in mbedtls_mpi_div_mpi()
1725 X.s = Y.s = 1; in mbedtls_mpi_div_mpi()
1735 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) ); in mbedtls_mpi_div_mpi()
1740 n = X.n - 1; in mbedtls_mpi_div_mpi()
1744 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 ) in mbedtls_mpi_div_mpi()
1747 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) ); in mbedtls_mpi_div_mpi()
1753 if( X.p[i] >= Y.p[t] ) in mbedtls_mpi_div_mpi()
1757 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1], in mbedtls_mpi_div_mpi()
1761 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2]; in mbedtls_mpi_div_mpi()
1762 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1]; in mbedtls_mpi_div_mpi()
1763 T2.p[2] = X.p[i]; in mbedtls_mpi_div_mpi()
1779 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); in mbedtls_mpi_div_mpi()
1781 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 ) in mbedtls_mpi_div_mpi()
1785 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) ); in mbedtls_mpi_div_mpi()
1798 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) ); in mbedtls_mpi_div_mpi()
1799 X.s = A->s; in mbedtls_mpi_div_mpi()
1800 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) ); in mbedtls_mpi_div_mpi()
1808 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); in mbedtls_mpi_div_mpi()
2071 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, in mbedtls_mpi_exp_mod() argument
2085 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_exp_mod()
2124 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) ); in mbedtls_mpi_exp_mod()
2185 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) ); in mbedtls_mpi_exp_mod()
2186 mpi_montred( X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2246 mpi_montmul( X, X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2264 mpi_montmul( X, X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2270 mpi_montmul( X, &WW, N, mm, &T ); in mbedtls_mpi_exp_mod()
2283 mpi_montmul( X, X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2288 mpi_montmul( X, &W[1], N, mm, &T ); in mbedtls_mpi_exp_mod()
2294 mpi_montred( X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2298 X->s = -1; in mbedtls_mpi_exp_mod()
2299 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) ); in mbedtls_mpi_exp_mod()
2440 mbedtls_mpi *X, size_t n_bytes, in mpi_fill_random_internal() argument
2447 if( X->n < limbs ) in mpi_fill_random_internal()
2450 memset( X->p, 0, overhead ); in mpi_fill_random_internal()
2451 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL ); in mpi_fill_random_internal()
2452 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) ); in mpi_fill_random_internal()
2453 mpi_bigendian_to_host( X->p, limbs ); in mpi_fill_random_internal()
2466 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size, in mbedtls_mpi_fill_random() argument
2473 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_fill_random()
2477 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) ); in mbedtls_mpi_fill_random()
2481 ret = mpi_fill_random_internal( X, size, f_rng, p_rng ); in mbedtls_mpi_fill_random()
2487 int mbedtls_mpi_random( mbedtls_mpi *X, in mbedtls_mpi_random() argument
2529 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) ); in mbedtls_mpi_random()
2543 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) ); in mbedtls_mpi_random()
2544 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) ); in mbedtls_mpi_random()
2552 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, &lower_bound, <_lower ) ); in mbedtls_mpi_random()
2553 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, <_upper ) ); in mbedtls_mpi_random()
2565 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N ) in mbedtls_mpi_inv_mod() argument
2569 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_inv_mod()
2651 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) ); in mbedtls_mpi_inv_mod()
2698 static int mpi_check_small_factors( const mbedtls_mpi *X ) in mpi_check_small_factors() argument
2704 if( ( X->p[0] & 1 ) == 0 ) in mpi_check_small_factors()
2709 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 ) in mpi_check_small_factors()
2712 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) ); in mpi_check_small_factors()
2725 static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds, in mpi_miller_rabin() argument
2733 MPI_VALIDATE_RET( X != NULL ); in mpi_miller_rabin()
2744 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) ); in mpi_miller_rabin()
2756 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) ); in mpi_miller_rabin()
2775 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) ); in mpi_miller_rabin()
2788 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) ); in mpi_miller_rabin()
2818 int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds, in mbedtls_mpi_is_prime_ext() argument
2824 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_is_prime_ext()
2828 XX.n = X->n; in mbedtls_mpi_is_prime_ext()
2829 XX.p = X->p; in mbedtls_mpi_is_prime_ext()
2853 int mbedtls_mpi_is_prime( const mbedtls_mpi *X, in mbedtls_mpi_is_prime() argument
2857 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_is_prime()
2865 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) ); in mbedtls_mpi_is_prime()
2876 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags, in mbedtls_mpi_gen_prime() argument
2893 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_gen_prime()
2926 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) ); in mbedtls_mpi_gen_prime()
2928 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue; in mbedtls_mpi_gen_prime()
2931 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) ); in mbedtls_mpi_gen_prime()
2932 X->p[0] |= 1; in mbedtls_mpi_gen_prime()
2936 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng ); in mbedtls_mpi_gen_prime()
2949 X->p[0] |= 2; in mbedtls_mpi_gen_prime()
2951 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) ); in mbedtls_mpi_gen_prime()
2953 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) ); in mbedtls_mpi_gen_prime()
2955 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) ); in mbedtls_mpi_gen_prime()
2958 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) ); in mbedtls_mpi_gen_prime()
2967 if( ( ret = mpi_check_small_factors( X ) ) == 0 && in mbedtls_mpi_gen_prime()
2969 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) ) in mbedtls_mpi_gen_prime()
2983 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) ); in mbedtls_mpi_gen_prime()
3015 mbedtls_mpi A, E, N, X, Y, U, V; in mbedtls_mpi_self_test() local
3018 mbedtls_mpi_init_mempool( &N ); mbedtls_mpi_init_mempool( &X ); in mbedtls_mpi_self_test()
3039 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) ); in mbedtls_mpi_self_test()
3053 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) in mbedtls_mpi_self_test()
3065 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) ); in mbedtls_mpi_self_test()
3078 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 || in mbedtls_mpi_self_test()
3091 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) ); in mbedtls_mpi_self_test()
3101 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) in mbedtls_mpi_self_test()
3113 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) ); in mbedtls_mpi_self_test()
3123 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) in mbedtls_mpi_self_test()
3140 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) ); in mbedtls_mpi_self_test()
3143 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) ); in mbedtls_mpi_self_test()
3163 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X ); in mbedtls_mpi_self_test()