mbed TLS v2.14.1

Data Structures | Macros | Typedefs | Enumerations | Functions
bignum.h File Reference

Multi-precision integer library. More...

`#include "config.h"`
`#include <stddef.h>`
`#include <stdint.h>`
`#include <stdio.h>`
Include dependency graph for bignum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures
struct | mbedtls_mpi
| MPI structure. More...

## Macros
#define | MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
#define | MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
#define | MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
#define | MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
#define | MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
#define | MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
#define | MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
#define | MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
#define | MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
#define | MBEDTLS_MPI_MAX_LIMBS 10000
#define | MBEDTLS_MPI_WINDOW_SIZE 6
#define | MBEDTLS_MPI_MAX_SIZE 1024
#define | MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
#define | MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
#define | MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
#define | MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
#define | MBEDTLS_HAVE_INT32
#define | MBEDTLS_HAVE_UDBL
#define | MBEDTLS_DEPRECATED

## Typedefs
typedef int32_t | mbedtls_mpi_sint
typedef uint32_t | mbedtls_mpi_uint
typedef uint64_t | mbedtls_t_udbl
typedef struct mbedtls_mpi | mbedtls_mpi
| MPI structure. More...

## Enumerations
enum | mbedtls_mpi_gen_prime_flag_t { MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002 }
| Flags for mbedtls_mpi_gen_prime() More...

## Functions
void | mbedtls_mpi_init (mbedtls_mpi *X)
| Initialize one MPI (make internal references valid) This just makes it ready to be set or freed, but does not define a value for the MPI. More...
void | mbedtls_mpi_free (mbedtls_mpi *X)
| Unallocate one MPI. More...
int | mbedtls_mpi_grow (mbedtls_mpi *X, size_t nblimbs)
| Enlarge to the specified number of limbs. More...
int | mbedtls_mpi_shrink (mbedtls_mpi *X, size_t nblimbs)
| Resize down, keeping at least the specified number of limbs. More...
int | mbedtls_mpi_copy (mbedtls_mpi *X, const mbedtls_mpi *Y)
| Copy the contents of Y into X. More...
void | mbedtls_mpi_swap (mbedtls_mpi *X, mbedtls_mpi *Y)
| Swap the contents of X and Y. More...
int | mbedtls_mpi_safe_cond_assign (mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
| Safe conditional assignement X = Y if assign is 1. More...
int | mbedtls_mpi_safe_cond_swap (mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign)
| Safe conditional swap X <-> Y if swap is 1. More...
int | mbedtls_mpi_lset (mbedtls_mpi *X, mbedtls_mpi_sint z)
| Set value from integer. More...
int | mbedtls_mpi_get_bit (const mbedtls_mpi *X, size_t pos)
| Get a specific bit from X. More...
int | mbedtls_mpi_set_bit (mbedtls_mpi *X, size_t pos, unsigned char val)
| Set a bit of X to a specific value of 0 or 1. More...
size_t | mbedtls_mpi_lsb (const mbedtls_mpi *X)
| Return the number of zero-bits before the least significant '1' bit. More...
size_t | mbedtls_mpi_bitlen (const mbedtls_mpi *X)
| Return the number of bits up to and including the most significant '1' bit'. More...
size_t | mbedtls_mpi_size (const mbedtls_mpi *X)
| Return the total size in bytes. More...
int | mbedtls_mpi_read_string (mbedtls_mpi *X, int radix, const char *s)
| Import from an ASCII string. More...
int | mbedtls_mpi_write_string (const mbedtls_mpi *X, int radix, char *buf, size_t buflen, size_t *olen)
| Export into an ASCII string. More...
int | mbedtls_mpi_read_file (mbedtls_mpi *X, int radix, FILE *fin)
| Read MPI from a line in an opened file. More...
int | mbedtls_mpi_write_file (const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
| Write X into an opened file, or stdout if fout is NULL. More...
int | mbedtls_mpi_read_binary (mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
| Import X from unsigned binary data, big endian. More...
int | mbedtls_mpi_write_binary (const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
| Export X into unsigned binary data, big endian. Always fills the whole buffer, which will start with zeros if the number is smaller. More...
int | mbedtls_mpi_shift_l (mbedtls_mpi *X, size_t count)
| Left-shift: X <<= count. More...
int | mbedtls_mpi_shift_r (mbedtls_mpi *X, size_t count)
| Right-shift: X >>= count. More...
int | mbedtls_mpi_cmp_abs (const mbedtls_mpi *X, const mbedtls_mpi *Y)
| Compare unsigned values. More...
int | mbedtls_mpi_cmp_mpi (const mbedtls_mpi *X, const mbedtls_mpi *Y)
| Compare signed values. More...
int | mbedtls_mpi_cmp_int (const mbedtls_mpi *X, mbedtls_mpi_sint z)
| Compare signed values. More...
int | mbedtls_mpi_add_abs (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
| Unsigned addition: X = |A| + |B|. More...
int | mbedtls_mpi_sub_abs (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
| Unsigned subtraction: X = |A| - |B|. More...
int | mbedtls_mpi_add_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
| Signed addition: X = A + B. More...
int | mbedtls_mpi_sub_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
| Signed subtraction: X = A - B. More...
int | mbedtls_mpi_add_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
| Signed addition: X = A + b. More...
int | mbedtls_mpi_sub_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
| Signed subtraction: X = A - b. More...
int | mbedtls_mpi_mul_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
| Baseline multiplication: X = A * B. More...
int | mbedtls_mpi_mul_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
| Baseline multiplication: X = A * b. More...
int | mbedtls_mpi_div_mpi (mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
| Division by mbedtls_mpi: A = Q * B + R. More...
int | mbedtls_mpi_div_int (mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
| Division by int: A = Q * b + R. More...
int | mbedtls_mpi_mod_mpi (mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
| Modulo: R = A mod B. More...
int | mbedtls_mpi_mod_int (mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
| Modulo: r = A mod b. More...
int | mbedtls_mpi_exp_mod (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
| Sliding-window exponentiation: X = A^E mod N. More...
int | mbedtls_mpi_fill_random (mbedtls_mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
| Fill an MPI X with size bytes of random. More...
int | mbedtls_mpi_gcd (mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
| Greatest common divisor: G = gcd(A, B) More...
int | mbedtls_mpi_inv_mod (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
| Modular inverse: X = A^-1 mod N. More...
MBEDTLS_DEPRECATED int | mbedtls_mpi_is_prime (const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
| Miller-Rabin primality test with error probability of 2-80 More...
int | mbedtls_mpi_is_prime_ext (const mbedtls_mpi *X, int rounds, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
| Miller-Rabin primality test. More...
int | mbedtls_mpi_gen_prime (mbedtls_mpi *X, size_t nbits, int flags, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
| Prime number generation. More...
int | mbedtls_mpi_self_test (int verbose)
| Checkup routine. More...

## Detailed Description

Multi-precision integer library.

Definition in file bignum.h.

## Macro Definition Documentation

## ◆ MBEDTLS_DEPRECATED

#define MBEDTLS_DEPRECATED

Definition at line 732 of file bignum.h.

## ◆ MBEDTLS_ERR_MPI_ALLOC_FAILED

#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010

Memory allocation failed.

Definition at line 47 of file bignum.h.

## ◆ MBEDTLS_ERR_MPI_BAD_INPUT_DATA

#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004

Bad input parameters to function.

Definition at line 41 of file bignum.h.

## ◆ MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL

#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008

The buffer is too small to write to.

Definition at line 43 of file bignum.h.

## ◆ MBEDTLS_ERR_MPI_DIVISION_BY_ZERO

#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C

The input argument for division is zero, which is not allowed.

Definition at line 45 of file bignum.h.

## ◆ MBEDTLS_ERR_MPI_FILE_IO_ERROR

#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002

An error occurred while reading from or writing to a file.

Definition at line 40 of file bignum.h.

## ◆ MBEDTLS_ERR_MPI_INVALID_CHARACTER

#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006

There is an invalid character in the digit string.

Definition at line 42 of file bignum.h.

## ◆ MBEDTLS_ERR_MPI_NEGATIVE_VALUE

#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A

The input arguments are negative or result in illegal output.

Definition at line 44 of file bignum.h.

## ◆ MBEDTLS_ERR_MPI_NOT_ACCEPTABLE

#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E

The input arguments are not acceptable.

Definition at line 46 of file bignum.h.

## ◆ MBEDTLS_HAVE_INT32

#define MBEDTLS_HAVE_INT32

Definition at line 163 of file bignum.h.

## ◆ MBEDTLS_HAVE_UDBL

#define MBEDTLS_HAVE_UDBL

Definition at line 169 of file bignum.h.

## ◆ MBEDTLS_LN_2_DIV_LN_10_SCALE100

#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332

Definition at line 101 of file bignum.h.

## ◆ MBEDTLS_MPI_CHK

#define MBEDTLS_MPI_CHK | ( |  | f | ) | do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )

Definition at line 49 of file bignum.h.

## ◆ MBEDTLS_MPI_MAX_BITS

#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )

Maximum number of bits for usable MPIs.

Definition at line 80 of file bignum.h.

## ◆ MBEDTLS_MPI_MAX_BITS_SCALE100

#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )

Definition at line 100 of file bignum.h.

## ◆ MBEDTLS_MPI_MAX_LIMBS

#define MBEDTLS_MPI_MAX_LIMBS 10000

Definition at line 54 of file bignum.h.

## ◆ MBEDTLS_MPI_MAX_SIZE

#define MBEDTLS_MPI_MAX_SIZE 1024

Maximum number of bytes for usable MPIs.

Definition at line 77 of file bignum.h.

## ◆ MBEDTLS_MPI_RW_BUFFER_SIZE

#define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )

Definition at line 102 of file bignum.h.

## ◆ MBEDTLS_MPI_WINDOW_SIZE

#define MBEDTLS_MPI_WINDOW_SIZE 6

Maximum windows size used.

Definition at line 66 of file bignum.h.

## Typedef Documentation

## ◆ mbedtls_mpi

typedef struct mbedtls_mpi mbedtls_mpi

MPI structure.

## ◆ mbedtls_mpi_sint

typedef int32_t mbedtls_mpi_sint

Definition at line 165 of file bignum.h.

## ◆ mbedtls_mpi_uint

typedef uint32_t mbedtls_mpi_uint

Definition at line 166 of file bignum.h.

## ◆ mbedtls_t_udbl

typedef uint64_t mbedtls_t_udbl

Definition at line 168 of file bignum.h.

## Enumeration Type Documentation

## ◆ mbedtls_mpi_gen_prime_flag_t

enum mbedtls_mpi_gen_prime_flag_t

Flags for mbedtls_mpi_gen_prime()

Each of these flags is a constraint on the result X returned by mbedtls_mpi_gen_prime().

Enumerator
MBEDTLS_MPI_GEN_PRIME_FLAG_DH | (X-1)/2 is prime too
MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR | lower error rate from 2-80 to 2-128

Definition at line 787 of file bignum.h.

## Function Documentation

## ◆ mbedtls_mpi_add_abs()

int mbedtls_mpi_add_abs | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | B
| ) |  |

Unsigned addition: X = |A| + |B|.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  B | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_add_int()

int mbedtls_mpi_add_int | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | mbedtls_mpi_sint | b
| ) |  |

Signed addition: X = A + b.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  b | The integer value to add

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_add_mpi()

int mbedtls_mpi_add_mpi | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | B
| ) |  |

Signed addition: X = A + B.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  B | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_bitlen()

size_t mbedtls_mpi_bitlen | ( | const mbedtls_mpi * | X | ) |

Return the number of bits up to and including the most significant '1' bit'.

Note: Thus also the one-based index of the most significant '1' bit

Parameters

  X | MPI to use

## ◆ mbedtls_mpi_cmp_abs()

int mbedtls_mpi_cmp_abs | ( | const mbedtls_mpi * | X,
|  | const mbedtls_mpi * | Y
| ) |  |

Compare unsigned values.

Parameters

  X | Left-hand MPI
  Y | Right-hand MPI

Returns
  1 if |X| is greater than |Y|, -1 if |X| is lesser than |Y| or 0 if |X| is equal to |Y|

## ◆ mbedtls_mpi_cmp_int()

int mbedtls_mpi_cmp_int | ( | const mbedtls_mpi * | X,
|  | mbedtls_mpi_sint | z
| ) |  |

Compare signed values.

Parameters

  X | Left-hand MPI
  z | The integer value to compare to

Returns
  1 if X is greater than z, -1 if X is lesser than z or 0 if X is equal to z

## ◆ mbedtls_mpi_cmp_mpi()

int mbedtls_mpi_cmp_mpi | ( | const mbedtls_mpi * | X,
|  | const mbedtls_mpi * | Y
| ) |  |

Compare signed values.

Parameters

  X | Left-hand MPI
  Y | Right-hand MPI

Returns
  1 if X is greater than Y, -1 if X is lesser than Y or 0 if X is equal to Y

## ◆ mbedtls_mpi_copy()

int mbedtls_mpi_copy | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | Y
| ) |  |

Copy the contents of Y into X.

Parameters

  X | Destination MPI. It is enlarged if necessary.
  Y | Source MPI.

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_div_int()

int mbedtls_mpi_div_int | ( | mbedtls_mpi * | Q,
|  | mbedtls_mpi * | R,
|  | const mbedtls_mpi * | A,
|  | mbedtls_mpi_sint | b
| ) |  |

Division by int: A = Q * b + R.

Parameters

  Q | Destination MPI for the quotient
  R | Destination MPI for the rest value
  A | Left-hand MPI
  b | Integer to divide by

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0

Note
  Either Q or R can be NULL.

## ◆ mbedtls_mpi_div_mpi()

int mbedtls_mpi_div_mpi | ( | mbedtls_mpi * | Q,
|  | mbedtls_mpi * | R,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | B
| ) |  |

Division by mbedtls_mpi: A = Q * B + R.

Parameters

  Q | Destination MPI for the quotient
  R | Destination MPI for the rest value
  A | Left-hand MPI
  B | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0

Note
  Either Q or R can be NULL.

## ◆ mbedtls_mpi_exp_mod()

int mbedtls_mpi_exp_mod | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | E,
|  | const mbedtls_mpi * | N,
|  | mbedtls_mpi * | _RR
| ) |  |

Sliding-window exponentiation: X = A^E mod N.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  E | Exponent MPI
  N | Modular MPI
  _RR | Speed-up MPI used for recalculations

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if E is negative

Note
  _RR is used to avoid re-computing R*R mod N across multiple calls, which speeds up things a bit. It can be set to NULL if the extra performance is unneeded.

## ◆ mbedtls_mpi_fill_random()

int mbedtls_mpi_fill_random | ( | mbedtls_mpi * | X,
|  | size_t | size,
|  | int(*)(void *, unsigned char *, size_t) | f_rng,
|  | void * | p_rng
| ) |  |

Fill an MPI X with size bytes of random.

Parameters

  X | Destination MPI
  size | Size in bytes
  f_rng | RNG function
  p_rng | RNG parameter

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

Note
  The bytes obtained from the PRNG are interpreted as a big-endian representation of an MPI; this can be relevant in applications like deterministic ECDSA.

## ◆ mbedtls_mpi_free()

void mbedtls_mpi_free | ( | mbedtls_mpi * | X | ) |

Unallocate one MPI.

Parameters

  X | One MPI to unallocate.

## ◆ mbedtls_mpi_gcd()

int mbedtls_mpi_gcd | ( | mbedtls_mpi * | G,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | B
| ) |  |

Greatest common divisor: G = gcd(A, B)

Parameters

  G | Destination MPI
  A | Left-hand MPI
  B | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_gen_prime()

int mbedtls_mpi_gen_prime | ( | mbedtls_mpi * | X,
|  | size_t | nbits,
|  | int | flags,
|  | int(*)(void *, unsigned char *, size_t) | f_rng,
|  | void * | p_rng
| ) |  |

Prime number generation.

Parameters

  X | Destination MPI
  nbits | Required size of X in bits ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
  flags | Mask of flags of type mbedtls_mpi_gen_prime_flag_t
  f_rng | RNG function
  p_rng | RNG parameter

Returns
  0 if successful (probably prime), MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3

## ◆ mbedtls_mpi_get_bit()

int mbedtls_mpi_get_bit | ( | const mbedtls_mpi * | X,
|  | size_t | pos
| ) |  |

Get a specific bit from X.

Parameters

  X | MPI to use
  pos | Zero-based index of the bit in X

Returns
  Either a 0 or a 1

## ◆ mbedtls_mpi_grow()

int mbedtls_mpi_grow | ( | mbedtls_mpi * | X,
|  | size_t | nblimbs
| ) |  |

Enlarge to the specified number of limbs.

This function does nothing if the MPI is already large enough.

Parameters

  X | MPI to grow
  nblimbs | The target number of limbs

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_init()

void mbedtls_mpi_init | ( | mbedtls_mpi * | X | ) |

Initialize one MPI (make internal references valid) This just makes it ready to be set or freed, but does not define a value for the MPI.

Parameters

  X | One MPI to initialize.

## ◆ mbedtls_mpi_inv_mod()

int mbedtls_mpi_inv_mod | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | N
| ) |  |

Modular inverse: X = A^-1 mod N.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  N | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1, MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.

## ◆ mbedtls_mpi_is_prime()

MBEDTLS_DEPRECATED int mbedtls_mpi_is_prime | ( | const mbedtls_mpi * | X,
|  | int(*)(void *, unsigned char *, size_t) | f_rng,
|  | void * | p_rng
| ) |  |

Miller-Rabin primality test with error probability of 2-80

Deprecated:
  Superseded by mbedtls_mpi_is_prime_ext() which allows specifying the number of Miller-Rabin rounds.

Parameters

  X | MPI to check
  f_rng | RNG function
  p_rng | RNG parameter

Returns
  0 if successful (probably prime), MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime

## ◆ mbedtls_mpi_is_prime_ext()

int mbedtls_mpi_is_prime_ext | ( | const mbedtls_mpi * | X,
|  | int | rounds,
|  | int(*)(void *, unsigned char *, size_t) | f_rng,
|  | void * | p_rng
| ) |  |

Miller-Rabin primality test.

Warning
  If `X` is potentially generated by an adversary, for example when validating cryptographic parameters that you didn't generate yourself and that are supposed to be prime, then `rounds` should be at least the half of the security strength of the cryptographic algorithm. On the other hand, if `X` is chosen uniformly or non-adversially (as is the case when mbedtls_mpi_gen_prime calls this function), then `rounds` can be much lower.

Parameters

  X | MPI to check
  rounds | Number of bases to perform Miller-Rabin primality test for. The probability of returning 0 on a composite is at most 2-2*`rounds`.
  f_rng | RNG function
  p_rng | RNG parameter

Returns
  0 if successful (probably prime), MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime

## ◆ mbedtls_mpi_lsb()

size_t mbedtls_mpi_lsb | ( | const mbedtls_mpi * | X | ) |

Return the number of zero-bits before the least significant '1' bit.

Note: Thus also the zero-based index of the least significant '1' bit

Parameters

  X | MPI to use

## ◆ mbedtls_mpi_lset()

int mbedtls_mpi_lset | ( | mbedtls_mpi * | X,
|  | mbedtls_mpi_sint | z
| ) |  |

Set value from integer.

Parameters

  X | MPI to set
  z | Value to use

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_mod_int()

int mbedtls_mpi_mod_int | ( | mbedtls_mpi_uint * | r,
|  | const mbedtls_mpi * | A,
|  | mbedtls_mpi_sint | b
| ) |  |

Modulo: r = A mod b.

Parameters

  r | Destination mbedtls_mpi_uint
  A | Left-hand MPI
  b | Integer to divide by

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0, MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0

## ◆ mbedtls_mpi_mod_mpi()

int mbedtls_mpi_mod_mpi | ( | mbedtls_mpi * | R,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | B
| ) |  |

Modulo: R = A mod B.

Parameters

  R | Destination MPI for the rest value
  A | Left-hand MPI
  B | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0, MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0

## ◆ mbedtls_mpi_mul_int()

int mbedtls_mpi_mul_int | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | mbedtls_mpi_uint | b
| ) |  |

Baseline multiplication: X = A * b.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  b | The unsigned integer value to multiply with

Note
  b is unsigned

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_mul_mpi()

int mbedtls_mpi_mul_mpi | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | B
| ) |  |

Baseline multiplication: X = A * B.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  B | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_read_binary()

int mbedtls_mpi_read_binary | ( | mbedtls_mpi * | X,
|  | const unsigned char * | buf,
|  | size_t | buflen
| ) |  |

Import X from unsigned binary data, big endian.

Parameters

  X | Destination MPI
  buf | Input buffer
  buflen | Input buffer size

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_read_file()

int mbedtls_mpi_read_file | ( | mbedtls_mpi * | X,
|  | int | radix,
|  | FILE * | fin
| ) |  |

Read MPI from a line in an opened file.

Parameters

  X | Destination MPI
  radix | Input numeric base
  fin | Input file handle

Returns
  0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer is too small or a MBEDTLS_ERR_MPI_XXX error code

Note
  On success, this function advances the file stream to the end of the current line or to EOF.

The function returns 0 on an empty line.

Leading whitespaces are ignored, as is a '0x' prefix for radix 16.

## ◆ mbedtls_mpi_read_string()

int mbedtls_mpi_read_string | ( | mbedtls_mpi * | X,
|  | int | radix,
|  | const char * | s
| ) |  |

Import from an ASCII string.

Parameters

  X | Destination MPI
  radix | Input numeric base
  s | Null-terminated string buffer

Returns
  0 if successful, or a MBEDTLS_ERR_MPI_XXX error code

## ◆ mbedtls_mpi_safe_cond_assign()

int mbedtls_mpi_safe_cond_assign | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | Y,
|  | unsigned char | assign
| ) |  |

Safe conditional assignement X = Y if assign is 1.

Parameters

  X | MPI to conditionally assign to
  Y | Value to be assigned
  assign | 1: perform the assignment, 0: keep X's original value

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,

Note
  This function is equivalent to if( assign ) mbedtls_mpi_copy( X, Y ); except that it avoids leaking any information about whether the assignment was done or not (the above code may leak information through branch prediction and/or memory access patterns analysis).

## ◆ mbedtls_mpi_safe_cond_swap()

int mbedtls_mpi_safe_cond_swap | ( | mbedtls_mpi * | X,
|  | mbedtls_mpi * | Y,
|  | unsigned char | assign
| ) |  |

Safe conditional swap X <-> Y if swap is 1.

Parameters

  X | First mbedtls_mpi value
  Y | Second mbedtls_mpi value
  assign | 1: perform the swap, 0: keep X and Y's original values

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,

Note
  This function is equivalent to if( assign ) mbedtls_mpi_swap( X, Y ); except that it avoids leaking any information about whether the assignment was done or not (the above code may leak information through branch prediction and/or memory access patterns analysis).

## ◆ mbedtls_mpi_self_test()

int mbedtls_mpi_self_test | ( | int | verbose | ) |

Checkup routine.

Returns
  0 if successful, or 1 if the test failed

## ◆ mbedtls_mpi_set_bit()

int mbedtls_mpi_set_bit | ( | mbedtls_mpi * | X,
|  | size_t | pos,
|  | unsigned char | val
| ) |  |

Set a bit of X to a specific value of 0 or 1.

Note
  Will grow X if necessary to set a bit to 1 in a not yet existing limb. Will not grow if bit should be set to 0

Parameters

  X | MPI to use
  pos | Zero-based index of the bit in X
  val | The value to set the bit to (0 or 1)

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1

## ◆ mbedtls_mpi_shift_l()

int mbedtls_mpi_shift_l | ( | mbedtls_mpi * | X,
|  | size_t | count
| ) |  |

Left-shift: X <<= count.

Parameters

  X | MPI to shift
  count | Amount to shift

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_shift_r()

int mbedtls_mpi_shift_r | ( | mbedtls_mpi * | X,
|  | size_t | count
| ) |  |

Right-shift: X >>= count.

Parameters

  X | MPI to shift
  count | Amount to shift

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_shrink()

int mbedtls_mpi_shrink | ( | mbedtls_mpi * | X,
|  | size_t | nblimbs
| ) |  |

Resize down, keeping at least the specified number of limbs.

If `X` is smaller than `nblimbs`, it is resized up instead.

Parameters

  X | MPI to shrink
  nblimbs | The minimum number of limbs to keep

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed (this can only happen when resizing up).

## ◆ mbedtls_mpi_size()

size_t mbedtls_mpi_size | ( | const mbedtls_mpi * | X | ) |

Return the total size in bytes.

Parameters

  X | MPI to use

## ◆ mbedtls_mpi_sub_abs()

int mbedtls_mpi_sub_abs | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | B
| ) |  |

Unsigned subtraction: X = |A| - |B|.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  B | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A

## ◆ mbedtls_mpi_sub_int()

int mbedtls_mpi_sub_int | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | mbedtls_mpi_sint | b
| ) |  |

Signed subtraction: X = A - b.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  b | The integer value to subtract

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_sub_mpi()

int mbedtls_mpi_sub_mpi | ( | mbedtls_mpi * | X,
|  | const mbedtls_mpi * | A,
|  | const mbedtls_mpi * | B
| ) |  |

Signed subtraction: X = A - B.

Parameters

  X | Destination MPI
  A | Left-hand MPI
  B | Right-hand MPI

Returns
  0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed

## ◆ mbedtls_mpi_swap()

void mbedtls_mpi_swap | ( | mbedtls_mpi * | X,
|  | mbedtls_mpi * | Y
| ) |  |

Swap the contents of X and Y.

Parameters

  X | First MPI value
  Y | Second MPI value

## ◆ mbedtls_mpi_write_binary()

int mbedtls_mpi_write_binary | ( | const mbedtls_mpi * | X,
|  | unsigned char * | buf,
|  | size_t | buflen
| ) |  |

Export X into unsigned binary data, big endian. Always fills the whole buffer, which will start with zeros if the number is smaller.

Parameters

  X | Source MPI
  buf | Output buffer
  buflen | Output buffer size

Returns
  0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough

## ◆ mbedtls_mpi_write_file()

int mbedtls_mpi_write_file | ( | const char * | p,
|  | const mbedtls_mpi * | X,
|  | int | radix,
|  | FILE * | fout
| ) |  |

Write X into an opened file, or stdout if fout is NULL.

Parameters

  p | Prefix, can be NULL
  X | Source MPI
  radix | Output numeric base
  fout | Output file handle (can be NULL)

Returns
  0 if successful, or a MBEDTLS_ERR_MPI_XXX error code

Note
  Set fout == NULL to print X on the console.

## ◆ mbedtls_mpi_write_string()

int mbedtls_mpi_write_string | ( | const mbedtls_mpi * | X,
|  | int | radix,
|  | char * | buf,
|  | size_t | buflen,
|  | size_t * | olen
| ) |  |

Export into an ASCII string.

Parameters

  X | Source MPI
  radix | Output numeric base
  buf | Buffer to write the string to
  buflen | Length of buf
  olen | Length of the string written, including final NUL byte

Returns
  0 if successful, or a MBEDTLS_ERR_MPI_XXX error code. *olen is always updated to reflect the amount of data that has (or would have) been written.

Note
  Call this function with buflen = 0 to obtain the minimum required buffer size in *olen.