Lines Matching refs:X

89 static void mpi_init( mbedtls_mpi *X, short use_mempool )  in mpi_init()  argument
91 MPI_VALIDATE( X != NULL ); in mpi_init()
93 X->s = 1; in mpi_init()
94 X->use_mempool = use_mempool; in mpi_init()
95 X->n = 0; in mpi_init()
96 X->p = NULL; in mpi_init()
99 void mbedtls_mpi_init( mbedtls_mpi *X ) in mbedtls_mpi_init() argument
101 mpi_init( X, 0 /*use_mempool*/ ); in mbedtls_mpi_init()
104 void mbedtls_mpi_init_mempool( mbedtls_mpi *X ) in mbedtls_mpi_init_mempool() argument
106 mpi_init( X, !!mbedtls_mpi_mempool /*use_mempool*/ ); in mbedtls_mpi_init_mempool()
112 void mbedtls_mpi_free( mbedtls_mpi *X ) in mbedtls_mpi_free() argument
114 if( X == NULL ) in mbedtls_mpi_free()
117 if( X->p != NULL ) in mbedtls_mpi_free()
119 mbedtls_mpi_zeroize( X->p, X->n ); in mbedtls_mpi_free()
120 if( X->use_mempool ) in mbedtls_mpi_free()
121 mempool_free( mbedtls_mpi_mempool, X->p ); in mbedtls_mpi_free()
123 mbedtls_free( X->p ); in mbedtls_mpi_free()
126 X->s = 1; in mbedtls_mpi_free()
127 X->n = 0; in mbedtls_mpi_free()
128 X->p = NULL; in mbedtls_mpi_free()
134 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ) in mbedtls_mpi_grow() argument
137 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_grow()
142 if( X->n < nblimbs ) in mbedtls_mpi_grow()
144 if( X->use_mempool ) in mbedtls_mpi_grow()
158 if( X->p != NULL ) in mbedtls_mpi_grow()
160 memcpy( p, X->p, X->n * ciL ); in mbedtls_mpi_grow()
161 mbedtls_mpi_zeroize( X->p, X->n ); in mbedtls_mpi_grow()
162 if( X->use_mempool ) in mbedtls_mpi_grow()
163 mempool_free( mbedtls_mpi_mempool, X->p); in mbedtls_mpi_grow()
165 mbedtls_free( X->p ); in mbedtls_mpi_grow()
168 X->n = nblimbs; in mbedtls_mpi_grow()
169 X->p = p; in mbedtls_mpi_grow()
179 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ) in mbedtls_mpi_shrink() argument
183 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_shrink()
189 if( X->n <= nblimbs ) in mbedtls_mpi_shrink()
190 return( mbedtls_mpi_grow( X, nblimbs ) ); in mbedtls_mpi_shrink()
193 for( i = X->n - 1; i > 0; i-- ) in mbedtls_mpi_shrink()
194 if( X->p[i] != 0 ) in mbedtls_mpi_shrink()
201 if( X->use_mempool ) in mbedtls_mpi_shrink()
215 if( X->p != NULL ) in mbedtls_mpi_shrink()
217 memcpy( p, X->p, i * ciL ); in mbedtls_mpi_shrink()
218 mbedtls_mpi_zeroize( X->p, X->n ); in mbedtls_mpi_shrink()
219 if( X->use_mempool ) in mbedtls_mpi_shrink()
220 mempool_free( mbedtls_mpi_mempool, X->p ); in mbedtls_mpi_shrink()
222 mbedtls_free( X->p ); in mbedtls_mpi_shrink()
225 X->n = i; in mbedtls_mpi_shrink()
226 X->p = p; in mbedtls_mpi_shrink()
232 static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs ) in mbedtls_mpi_resize_clear() argument
236 mbedtls_mpi_free( X ); in mbedtls_mpi_resize_clear()
239 else if( X->n == limbs ) in mbedtls_mpi_resize_clear()
241 memset( X->p, 0, limbs * ciL ); in mbedtls_mpi_resize_clear()
242 X->s = 1; in mbedtls_mpi_resize_clear()
247 mbedtls_mpi_free( X ); in mbedtls_mpi_resize_clear()
248 return( mbedtls_mpi_grow( X, limbs ) ); in mbedtls_mpi_resize_clear()
261 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y ) in mbedtls_mpi_copy() argument
265 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_copy()
268 if( X == Y ) in mbedtls_mpi_copy()
273 if( X->n != 0 ) in mbedtls_mpi_copy()
275 X->s = 1; in mbedtls_mpi_copy()
276 memset( X->p, 0, X->n * ciL ); in mbedtls_mpi_copy()
286 X->s = Y->s; in mbedtls_mpi_copy()
288 if( X->n < i ) in mbedtls_mpi_copy()
290 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) ); in mbedtls_mpi_copy()
294 memset( X->p + i, 0, ( X->n - i ) * ciL ); in mbedtls_mpi_copy()
297 memcpy( X->p, Y->p, i * ciL ); in mbedtls_mpi_copy()
307 void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y ) in mbedtls_mpi_swap() argument
310 MPI_VALIDATE( X != NULL ); in mbedtls_mpi_swap()
313 memcpy( &T, X, sizeof( mbedtls_mpi ) ); in mbedtls_mpi_swap()
314 memcpy( X, Y, sizeof( mbedtls_mpi ) ); in mbedtls_mpi_swap()
384 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign ) in mbedtls_mpi_safe_cond_assign() argument
389 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_safe_cond_assign()
408 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) ); in mbedtls_mpi_safe_cond_assign()
410 X->s = mpi_safe_cond_select_sign( X->s, Y->s, assign ); in mbedtls_mpi_safe_cond_assign()
412 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign ); in mbedtls_mpi_safe_cond_assign()
414 for( i = Y->n; i < X->n; i++ ) in mbedtls_mpi_safe_cond_assign()
415 X->p[i] &= ~limb_mask; in mbedtls_mpi_safe_cond_assign()
427 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap ) in mbedtls_mpi_safe_cond_swap() argument
433 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_safe_cond_swap()
436 if( X == Y ) in mbedtls_mpi_safe_cond_swap()
455 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) ); in mbedtls_mpi_safe_cond_swap()
456 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) ); in mbedtls_mpi_safe_cond_swap()
458 s = X->s; in mbedtls_mpi_safe_cond_swap()
459 X->s = mpi_safe_cond_select_sign( X->s, Y->s, swap ); in mbedtls_mpi_safe_cond_swap()
463 for( i = 0; i < X->n; i++ ) in mbedtls_mpi_safe_cond_swap()
465 tmp = X->p[i]; in mbedtls_mpi_safe_cond_swap()
466 X->p[i] = ( X->p[i] & ~limb_mask ) | ( Y->p[i] & limb_mask ); in mbedtls_mpi_safe_cond_swap()
477 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z ) in mbedtls_mpi_lset() argument
480 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_lset()
482 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) ); in mbedtls_mpi_lset()
483 memset( X->p, 0, X->n * ciL ); in mbedtls_mpi_lset()
485 X->p[0] = ( z < 0 ) ? -z : z; in mbedtls_mpi_lset()
486 X->s = ( z < 0 ) ? -1 : 1; in mbedtls_mpi_lset()
496 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos ) in mbedtls_mpi_get_bit() argument
498 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_get_bit()
500 if( X->n * biL <= pos ) in mbedtls_mpi_get_bit()
503 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 ); in mbedtls_mpi_get_bit()
507 #define GET_BYTE( X, i ) \ argument
508 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
513 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val ) in mbedtls_mpi_set_bit() argument
518 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_set_bit()
523 if( X->n * biL <= pos ) in mbedtls_mpi_set_bit()
528 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) ); in mbedtls_mpi_set_bit()
531 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx ); in mbedtls_mpi_set_bit()
532 X->p[off] |= (mbedtls_mpi_uint) val << idx; in mbedtls_mpi_set_bit()
542 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ) in mbedtls_mpi_lsb() argument
545 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 ); in mbedtls_mpi_lsb()
547 for( i = 0; i < X->n; i++ ) in mbedtls_mpi_lsb()
549 if( ( ( X->p[i] >> j ) & 1 ) != 0 ) in mbedtls_mpi_lsb()
576 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X ) in mbedtls_mpi_bitlen() argument
580 if( X->n == 0 ) in mbedtls_mpi_bitlen()
583 for( i = X->n - 1; i > 0; i-- ) in mbedtls_mpi_bitlen()
584 if( X->p[i] != 0 ) in mbedtls_mpi_bitlen()
587 j = biL - mbedtls_clz( X->p[i] ); in mbedtls_mpi_bitlen()
595 size_t mbedtls_mpi_size( const mbedtls_mpi *X ) in mbedtls_mpi_size() argument
597 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 ); in mbedtls_mpi_size()
620 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ) in mbedtls_mpi_read_string() argument
627 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_read_string()
637 mbedtls_mpi_free( X ); in mbedtls_mpi_read_string()
656 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) ); in mbedtls_mpi_read_string()
657 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); in mbedtls_mpi_read_string()
662 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 ); in mbedtls_mpi_read_string()
667 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); in mbedtls_mpi_read_string()
672 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) ); in mbedtls_mpi_read_string()
673 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) ); in mbedtls_mpi_read_string()
677 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 ) in mbedtls_mpi_read_string()
678 X->s = -1; in mbedtls_mpi_read_string()
690 static int mpi_write_hlp( mbedtls_mpi *X, int radix, in mpi_write_hlp() argument
705 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) ); in mpi_write_hlp()
706 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) ); in mpi_write_hlp()
716 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 ); in mpi_write_hlp()
729 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, in mbedtls_mpi_write_string() argument
736 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_write_string()
743 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */ in mbedtls_mpi_write_string()
767 if( X->s == -1 ) in mbedtls_mpi_write_string()
778 for( i = X->n, k = 0; i > 0; i-- ) in mbedtls_mpi_write_string()
782 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF; in mbedtls_mpi_write_string()
795 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) ); in mbedtls_mpi_write_string()
817 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin ) in mbedtls_mpi_read_file() argument
828 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_read_file()
850 return( mbedtls_mpi_read_string( X, radix, p + 1 ) ); in mbedtls_mpi_read_file()
856 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout ) in mbedtls_mpi_write_file() argument
865 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_write_file()
872 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) ); in mbedtls_mpi_write_file()
990 int mbedtls_mpi_read_binary_le( mbedtls_mpi *X, in mbedtls_mpi_read_binary_le() argument
998 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) ); in mbedtls_mpi_read_binary_le()
1001 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3); in mbedtls_mpi_read_binary_le()
1016 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen ) in mbedtls_mpi_read_binary() argument
1023 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_read_binary()
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) ); in mbedtls_mpi_read_binary()
1033 Xp = (unsigned char*) X->p; in mbedtls_mpi_read_binary()
1036 mpi_bigendian_to_host( X->p, limbs ); in mbedtls_mpi_read_binary()
1052 int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X, in mbedtls_mpi_write_binary_le() argument
1055 size_t stored_bytes = X->n * ciL; in mbedtls_mpi_write_binary_le()
1071 if( GET_BYTE( X, i ) != 0 ) in mbedtls_mpi_write_binary_le()
1077 buf[i] = GET_BYTE( X, i ); in mbedtls_mpi_write_binary_le()
1091 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, in mbedtls_mpi_write_binary() argument
1099 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_write_binary()
1102 stored_bytes = X->n * ciL; in mbedtls_mpi_write_binary()
1123 if( GET_BYTE( X, i ) != 0 ) in mbedtls_mpi_write_binary()
1129 p[bytes_to_copy - i - 1] = GET_BYTE( X, i ); in mbedtls_mpi_write_binary()
1137 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ) in mbedtls_mpi_shift_l() argument
1142 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_shift_l()
1147 i = mbedtls_mpi_bitlen( X ) + count; in mbedtls_mpi_shift_l()
1149 if( X->n * biL < i ) in mbedtls_mpi_shift_l()
1150 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) ); in mbedtls_mpi_shift_l()
1159 for( i = X->n; i > v0; i-- ) in mbedtls_mpi_shift_l()
1160 X->p[i - 1] = X->p[i - v0 - 1]; in mbedtls_mpi_shift_l()
1163 X->p[i - 1] = 0; in mbedtls_mpi_shift_l()
1171 for( i = v0; i < X->n; i++ ) in mbedtls_mpi_shift_l()
1173 r1 = X->p[i] >> (biL - t1); in mbedtls_mpi_shift_l()
1174 X->p[i] <<= t1; in mbedtls_mpi_shift_l()
1175 X->p[i] |= r0; in mbedtls_mpi_shift_l()
1188 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ) in mbedtls_mpi_shift_r() argument
1192 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_shift_r()
1197 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) ) in mbedtls_mpi_shift_r()
1198 return mbedtls_mpi_lset( X, 0 ); in mbedtls_mpi_shift_r()
1205 for( i = 0; i < X->n - v0; i++ ) in mbedtls_mpi_shift_r()
1206 X->p[i] = X->p[i + v0]; in mbedtls_mpi_shift_r()
1208 for( ; i < X->n; i++ ) in mbedtls_mpi_shift_r()
1209 X->p[i] = 0; in mbedtls_mpi_shift_r()
1217 for( i = X->n; i > 0; i-- ) in mbedtls_mpi_shift_r()
1219 r1 = X->p[i - 1] << (biL - v1); in mbedtls_mpi_shift_r()
1220 X->p[i - 1] >>= v1; in mbedtls_mpi_shift_r()
1221 X->p[i - 1] |= r0; in mbedtls_mpi_shift_r()
1232 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ) in mbedtls_mpi_cmp_abs() argument
1235 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_cmp_abs()
1238 for( i = X->n; i > 0; i-- ) in mbedtls_mpi_cmp_abs()
1239 if( X->p[i - 1] != 0 ) in mbedtls_mpi_cmp_abs()
1254 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 ); in mbedtls_mpi_cmp_abs()
1255 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 ); in mbedtls_mpi_cmp_abs()
1264 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y ) in mbedtls_mpi_cmp_mpi() argument
1267 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_cmp_mpi()
1270 for( i = X->n; i > 0; i-- ) in mbedtls_mpi_cmp_mpi()
1271 if( X->p[i - 1] != 0 ) in mbedtls_mpi_cmp_mpi()
1281 if( i > j ) return( X->s ); in mbedtls_mpi_cmp_mpi()
1284 if( X->s > 0 && Y->s < 0 ) return( 1 ); in mbedtls_mpi_cmp_mpi()
1285 if( Y->s > 0 && X->s < 0 ) return( -1 ); in mbedtls_mpi_cmp_mpi()
1289 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s ); in mbedtls_mpi_cmp_mpi()
1290 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s ); in mbedtls_mpi_cmp_mpi()
1334 int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y, in mbedtls_mpi_lt_mpi_ct() argument
1341 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_lt_mpi_ct()
1345 if( X->n != Y->n ) in mbedtls_mpi_lt_mpi_ct()
1352 X_is_negative = ( X->s & 2 ) >> 1; in mbedtls_mpi_lt_mpi_ct()
1369 for( i = X->n; i > 0; i-- ) in mbedtls_mpi_lt_mpi_ct()
1378 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] ); in mbedtls_mpi_lt_mpi_ct()
1389 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] ); in mbedtls_mpi_lt_mpi_ct()
1400 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z ) in mbedtls_mpi_cmp_int() argument
1404 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_cmp_int()
1411 return( mbedtls_mpi_cmp_mpi( X, &Y ) ); in mbedtls_mpi_cmp_int()
1417 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_add_abs() argument
1422 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_add_abs()
1426 if( X == B ) in mbedtls_mpi_add_abs()
1428 const mbedtls_mpi *T = A; A = X; B = T; in mbedtls_mpi_add_abs()
1431 if( X != A ) in mbedtls_mpi_add_abs()
1432 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) ); in mbedtls_mpi_add_abs()
1437 X->s = 1; in mbedtls_mpi_add_abs()
1443 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) ); in mbedtls_mpi_add_abs()
1445 o = B->p; p = X->p; c = 0; in mbedtls_mpi_add_abs()
1459 if( i >= X->n ) in mbedtls_mpi_add_abs()
1461 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) ); in mbedtls_mpi_add_abs()
1462 p = X->p + i; in mbedtls_mpi_add_abs()
1510 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_sub_abs() argument
1515 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_sub_abs()
1529 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) ); in mbedtls_mpi_sub_abs()
1535 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL ); in mbedtls_mpi_sub_abs()
1536 if( X->n > A->n ) in mbedtls_mpi_sub_abs()
1537 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL ); in mbedtls_mpi_sub_abs()
1539 carry = mpi_sub_hlp( n, X->p, A->p, B->p ); in mbedtls_mpi_sub_abs()
1543 for( ; n < X->n && X->p[n] == 0; n++ ) in mbedtls_mpi_sub_abs()
1544 --X->p[n]; in mbedtls_mpi_sub_abs()
1547 if( n == X->n ) in mbedtls_mpi_sub_abs()
1552 --X->p[n]; in mbedtls_mpi_sub_abs()
1556 X->s = 1; in mbedtls_mpi_sub_abs()
1565 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_add_mpi() argument
1568 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_add_mpi()
1577 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) ); in mbedtls_mpi_add_mpi()
1578 X->s = s; in mbedtls_mpi_add_mpi()
1582 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) ); in mbedtls_mpi_add_mpi()
1583 X->s = -s; in mbedtls_mpi_add_mpi()
1588 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) ); in mbedtls_mpi_add_mpi()
1589 X->s = s; in mbedtls_mpi_add_mpi()
1600 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_sub_mpi() argument
1603 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_sub_mpi()
1612 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) ); in mbedtls_mpi_sub_mpi()
1613 X->s = s; in mbedtls_mpi_sub_mpi()
1617 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) ); in mbedtls_mpi_sub_mpi()
1618 X->s = -s; in mbedtls_mpi_sub_mpi()
1623 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) ); in mbedtls_mpi_sub_mpi()
1624 X->s = s; in mbedtls_mpi_sub_mpi()
1635 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ) in mbedtls_mpi_add_int() argument
1639 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_add_int()
1647 return( mbedtls_mpi_add_mpi( X, A, &_B ) ); in mbedtls_mpi_add_int()
1653 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ) in mbedtls_mpi_sub_int() argument
1657 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_sub_int()
1665 return( mbedtls_mpi_sub_mpi( X, A, &_B ) ); in mbedtls_mpi_sub_int()
1759 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) in mbedtls_mpi_mul_mpi() argument
1765 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_mul_mpi()
1771 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; } in mbedtls_mpi_mul_mpi()
1772 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; } in mbedtls_mpi_mul_mpi()
1786 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) ); in mbedtls_mpi_mul_mpi()
1787 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); in mbedtls_mpi_mul_mpi()
1790 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] ); in mbedtls_mpi_mul_mpi()
1797 X->s = 1; in mbedtls_mpi_mul_mpi()
1799 X->s = A->s * B->s; in mbedtls_mpi_mul_mpi()
1811 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b ) in mbedtls_mpi_mul_int() argument
1813 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_mul_int()
1825 return( mbedtls_mpi_lset( X, 0 ) ); in mbedtls_mpi_mul_int()
1838 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) ); in mbedtls_mpi_mul_int()
1839 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) ); in mbedtls_mpi_mul_int()
1840 mpi_mul_hlp( n, A->p, X->p, b - 1 ); in mbedtls_mpi_mul_int()
1950 mbedtls_mpi X, Y, Z, T1, T2; in mbedtls_mpi_div_mpi() local
1958 mbedtls_mpi_init_mempool( &X ); mbedtls_mpi_init_mempool( &Y ); in mbedtls_mpi_div_mpi()
1978 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) ); in mbedtls_mpi_div_mpi()
1980 X.s = Y.s = 1; in mbedtls_mpi_div_mpi()
1990 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) ); in mbedtls_mpi_div_mpi()
1995 n = X.n - 1; in mbedtls_mpi_div_mpi()
1999 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 ) in mbedtls_mpi_div_mpi()
2002 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) ); in mbedtls_mpi_div_mpi()
2008 if( X.p[i] >= Y.p[t] ) in mbedtls_mpi_div_mpi()
2012 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1], in mbedtls_mpi_div_mpi()
2016 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2]; in mbedtls_mpi_div_mpi()
2017 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1]; in mbedtls_mpi_div_mpi()
2018 T2.p[2] = X.p[i]; in mbedtls_mpi_div_mpi()
2034 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); in mbedtls_mpi_div_mpi()
2036 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 ) in mbedtls_mpi_div_mpi()
2040 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) ); in mbedtls_mpi_div_mpi()
2053 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) ); in mbedtls_mpi_div_mpi()
2054 X.s = A->s; in mbedtls_mpi_div_mpi()
2055 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) ); in mbedtls_mpi_div_mpi()
2063 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); in mbedtls_mpi_div_mpi()
2362 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, in mbedtls_mpi_exp_mod() argument
2376 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_exp_mod()
2415 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) ); in mbedtls_mpi_exp_mod()
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) ); in mbedtls_mpi_exp_mod()
2477 mpi_montred( X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2537 mpi_montmul( X, X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2555 mpi_montmul( X, X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2561 mpi_montmul( X, &WW, N, mm, &T ); in mbedtls_mpi_exp_mod()
2574 mpi_montmul( X, X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2579 mpi_montmul( X, &W[1], N, mm, &T ); in mbedtls_mpi_exp_mod()
2585 mpi_montred( X, N, mm, &T ); in mbedtls_mpi_exp_mod()
2589 X->s = -1; in mbedtls_mpi_exp_mod()
2590 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) ); in mbedtls_mpi_exp_mod()
2731 mbedtls_mpi *X, size_t n_bytes, in mpi_fill_random_internal() argument
2738 if( X->n < limbs ) in mpi_fill_random_internal()
2741 memset( X->p, 0, overhead ); in mpi_fill_random_internal()
2742 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL ); in mpi_fill_random_internal()
2743 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) ); in mpi_fill_random_internal()
2744 mpi_bigendian_to_host( X->p, limbs ); in mpi_fill_random_internal()
2757 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size, in mbedtls_mpi_fill_random() argument
2764 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_fill_random()
2768 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) ); in mbedtls_mpi_fill_random()
2772 ret = mpi_fill_random_internal( X, size, f_rng, p_rng ); in mbedtls_mpi_fill_random()
2778 int mbedtls_mpi_random( mbedtls_mpi *X, in mbedtls_mpi_random() argument
2820 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) ); in mbedtls_mpi_random()
2834 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) ); in mbedtls_mpi_random()
2835 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) ); in mbedtls_mpi_random()
2843 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, &lower_bound, &lt_lower ) ); in mbedtls_mpi_random()
2844 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &lt_upper ) ); in mbedtls_mpi_random()
2856 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N ) in mbedtls_mpi_inv_mod() argument
2860 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_inv_mod()
2942 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) ); in mbedtls_mpi_inv_mod()
2989 static int mpi_check_small_factors( const mbedtls_mpi *X ) in mpi_check_small_factors() argument
2995 if( ( X->p[0] & 1 ) == 0 ) in mpi_check_small_factors()
3000 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 ) in mpi_check_small_factors()
3003 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) ); in mpi_check_small_factors()
3016 static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds, in mpi_miller_rabin() argument
3024 MPI_VALIDATE_RET( X != NULL ); in mpi_miller_rabin()
3035 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) ); in mpi_miller_rabin()
3047 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) ); in mpi_miller_rabin()
3066 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) ); in mpi_miller_rabin()
3079 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) ); in mpi_miller_rabin()
3109 int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds, in mbedtls_mpi_is_prime_ext() argument
3115 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_is_prime_ext()
3119 XX.n = X->n; in mbedtls_mpi_is_prime_ext()
3120 XX.p = X->p; in mbedtls_mpi_is_prime_ext()
3144 int mbedtls_mpi_is_prime( const mbedtls_mpi *X, in mbedtls_mpi_is_prime() argument
3148 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_is_prime()
3156 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) ); in mbedtls_mpi_is_prime()
3167 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags, in mbedtls_mpi_gen_prime() argument
3184 MPI_VALIDATE_RET( X != NULL ); in mbedtls_mpi_gen_prime()
3217 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) ); in mbedtls_mpi_gen_prime()
3219 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue; in mbedtls_mpi_gen_prime()
3222 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) ); in mbedtls_mpi_gen_prime()
3223 X->p[0] |= 1; in mbedtls_mpi_gen_prime()
3227 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng ); in mbedtls_mpi_gen_prime()
3240 X->p[0] |= 2; in mbedtls_mpi_gen_prime()
3242 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) ); in mbedtls_mpi_gen_prime()
3244 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) ); in mbedtls_mpi_gen_prime()
3246 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) ); in mbedtls_mpi_gen_prime()
3249 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) ); in mbedtls_mpi_gen_prime()
3258 if( ( ret = mpi_check_small_factors( X ) ) == 0 && in mbedtls_mpi_gen_prime()
3260 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) ) in mbedtls_mpi_gen_prime()
3274 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) ); in mbedtls_mpi_gen_prime()
3306 mbedtls_mpi A, E, N, X, Y, U, V; in mbedtls_mpi_self_test() local
3309 mbedtls_mpi_init_mempool( &N ); mbedtls_mpi_init_mempool( &X ); in mbedtls_mpi_self_test()
3330 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) ); in mbedtls_mpi_self_test()
3344 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) in mbedtls_mpi_self_test()
3356 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) ); in mbedtls_mpi_self_test()
3369 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 || in mbedtls_mpi_self_test()
3382 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) ); in mbedtls_mpi_self_test()
3392 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) in mbedtls_mpi_self_test()
3404 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) ); in mbedtls_mpi_self_test()
3414 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) in mbedtls_mpi_self_test()
3431 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) ); in mbedtls_mpi_self_test()
3434 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) ); in mbedtls_mpi_self_test()
3454 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X ); in mbedtls_mpi_self_test()