|
CycloneBOOT 2.6.6
Secure Bootloader for ARM Cortex-M MCU
|
MPI (Multiple Precision Integer Arithmetic) More...
#include "core/crypto.h"#include "mpi/mpi.h"#include "mpi/mpi_misc.h"#include "debug.h"Macros | |
| #define | TRACE_LEVEL CRYPTO_TRACE_LEVEL |
Functions | |
| void | mpiInit (Mpi *r) |
| Initialize a multiple precision integer. | |
| void | mpiFree (Mpi *r) |
| Release a multiple precision integer. | |
| error_t | mpiGrow (Mpi *r, uint_t size) |
| Adjust the size of multiple precision integer. | |
| uint_t | mpiGetLength (const Mpi *a) |
| Get the actual length in words. | |
| uint_t | mpiGetByteLength (const Mpi *a) |
| Get the actual length in bytes. | |
| uint_t | mpiGetBitLength (const Mpi *a) |
| Get the actual length in bits. | |
| error_t | mpiSetBitValue (Mpi *r, uint_t index, uint_t value) |
| Set the bit value at the specified index. | |
| uint_t | mpiGetBitValue (const Mpi *a, uint_t index) |
| Get the bit value at the specified index. | |
| int_t | mpiComp (const Mpi *a, const Mpi *b) |
| Compare two multiple precision integers. | |
| int_t | mpiCompInt (const Mpi *a, mpi_sword_t b) |
| Compare a multiple precision integer with an integer. | |
| int_t | mpiCompAbs (const Mpi *a, const Mpi *b) |
| Compare the absolute value of two multiple precision integers. | |
| error_t | mpiCopy (Mpi *r, const Mpi *a) |
| Copy a multiple precision integer. | |
| error_t | mpiSetValue (Mpi *r, mpi_sword_t a) |
| Set the value of a multiple precision integer. | |
| error_t | mpiRand (Mpi *r, uint_t length, const PrngAlgo *prngAlgo, void *prngContext) |
| Generate a random value. | |
| error_t | mpiRandRange (Mpi *r, const Mpi *p, const PrngAlgo *prngAlgo, void *prngContext) |
| Generate a random value in the range 1 to p-1. | |
| __weak_func error_t | mpiCheckProbablePrime (const Mpi *a) |
| Test whether a number is probable prime. | |
| error_t | mpiImport (Mpi *r, const uint8_t *input, size_t length, MpiFormat format) |
| Octet string to integer conversion. | |
| error_t | mpiExport (const Mpi *a, uint8_t *output, size_t length, MpiFormat format) |
| Integer to octet string conversion. | |
| error_t | mpiAdd (Mpi *r, const Mpi *a, const Mpi *b) |
| Multiple precision addition. | |
| error_t | mpiAddInt (Mpi *r, const Mpi *a, mpi_sword_t b) |
| Add an integer to a multiple precision integer. | |
| error_t | mpiSub (Mpi *r, const Mpi *a, const Mpi *b) |
| Multiple precision subtraction. | |
| error_t | mpiSubInt (Mpi *r, const Mpi *a, mpi_sword_t b) |
| Subtract an integer from a multiple precision integer. | |
| error_t | mpiAddAbs (Mpi *r, const Mpi *a, const Mpi *b) |
| Helper routine for multiple precision addition. | |
| error_t | mpiSubAbs (Mpi *r, const Mpi *a, const Mpi *b) |
| Helper routine for multiple precision subtraction. | |
| error_t | mpiShiftLeft (Mpi *r, uint_t n) |
| Left shift operation. | |
| error_t | mpiShiftRight (Mpi *r, uint_t n) |
| Right shift operation. | |
| __weak_func error_t | mpiMul (Mpi *r, const Mpi *a, const Mpi *b) |
| Multiple precision multiplication. | |
| error_t | mpiMulInt (Mpi *r, const Mpi *a, mpi_sword_t b) |
| Multiply a multiple precision integer by an integer. | |
| error_t | mpiDiv (Mpi *q, Mpi *r, const Mpi *a, const Mpi *b) |
| Multiple precision division. | |
| error_t | mpiDivInt (Mpi *q, Mpi *r, const Mpi *a, mpi_sword_t b) |
| Divide a multiple precision integer by an integer. | |
| error_t | mpiMod (Mpi *r, const Mpi *a, const Mpi *p) |
| Modulo operation. | |
| error_t | mpiAddMod (Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p) |
| Modular addition. | |
| error_t | mpiSubMod (Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p) |
| Modular subtraction. | |
| __weak_func error_t | mpiMulMod (Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p) |
| Modular multiplication. | |
| __weak_func error_t | mpiInvMod (Mpi *r, const Mpi *a, const Mpi *p) |
| Modular inverse. | |
| __weak_func error_t | mpiExpMod (Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p) |
| Modular exponentiation. | |
| __weak_func error_t | mpiExpModFast (Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p) |
| Modular exponentiation (fast calculation) | |
| __weak_func error_t | mpiExpModRegular (Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p) |
| Modular exponentiation (regular calculation) | |
| void | mpiDump (FILE *stream, const char_t *prepend, const Mpi *a) |
| Display the contents of a multiple precision integer. | |
MPI (Multiple Precision Integer Arithmetic)
SPDX-License-Identifier: GPL-2.0-or-later
Copyright (C) 2021-2026 Oryx Embedded SARL. All rights reserved.
This file is part of CycloneCRYPTO Open.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
| error_t mpiAdd | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | b | ||
| ) |
Multiple precision addition.
| [out] | r | Resulting integer R = A + B |
| [in] | a | First operand A |
| [in] | b | Second operand B |
| error_t mpiAddAbs | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | b | ||
| ) |
Helper routine for multiple precision addition.
| [out] | r | Resulting integer R = |A + B| |
| [in] | a | First operand A |
| [in] | b | Second operand B |
| error_t mpiAddInt | ( | Mpi * | r, |
| const Mpi * | a, | ||
| mpi_sword_t | b | ||
| ) |
Add an integer to a multiple precision integer.
| [out] | r | Resulting integer R = A + B |
| [in] | a | First operand A |
| [in] | b | Second operand B |
| error_t mpiAddMod | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | b, | ||
| const Mpi * | p | ||
| ) |
Modular addition.
| [out] | r | Resulting integer R = A + B mod P |
| [in] | a | The first operand A |
| [in] | b | The second operand B |
| [in] | p | The modulus P |
| __weak_func error_t mpiCheckProbablePrime | ( | const Mpi * | a | ) |
Test whether a number is probable prime.
| [in] | a | Pointer to a multiple precision integer |
| int_t mpiComp | ( | const Mpi * | a, |
| const Mpi * | b | ||
| ) |
Compare two multiple precision integers.
| [in] | a | The first multiple precision integer to be compared |
| [in] | b | The second multiple precision integer to be compared |
| int_t mpiCompAbs | ( | const Mpi * | a, |
| const Mpi * | b | ||
| ) |
Compare the absolute value of two multiple precision integers.
| [in] | a | The first multiple precision integer to be compared |
| [in] | b | The second multiple precision integer to be compared |
| int_t mpiCompInt | ( | const Mpi * | a, |
| mpi_sword_t | b | ||
| ) |
Compare a multiple precision integer with an integer.
| [in] | a | Multiple precision integer to be compared |
| [in] | b | Integer to be compared |
| error_t mpiCopy | ( | Mpi * | r, |
| const Mpi * | a | ||
| ) |
Copy a multiple precision integer.
| [out] | r | Pointer to a multiple precision integer (destination) |
| [in] | a | Pointer to a multiple precision integer (source) |
| error_t mpiDiv | ( | Mpi * | q, |
| Mpi * | r, | ||
| const Mpi * | a, | ||
| const Mpi * | b | ||
| ) |
Multiple precision division.
| [out] | q | The quotient Q = A / B |
| [out] | r | The remainder R = A mod B |
| [in] | a | The dividend A |
| [in] | b | The divisor B |
| error_t mpiDivInt | ( | Mpi * | q, |
| Mpi * | r, | ||
| const Mpi * | a, | ||
| mpi_sword_t | b | ||
| ) |
Divide a multiple precision integer by an integer.
| [out] | q | The quotient Q = A / B |
| [out] | r | The remainder R = A mod B |
| [in] | a | The dividend A |
| [in] | b | The divisor B |
| void mpiDump | ( | FILE * | stream, |
| const char_t * | prepend, | ||
| const Mpi * | a | ||
| ) |
Display the contents of a multiple precision integer.
| [in] | stream | Pointer to a FILE object that identifies an output stream |
| [in] | prepend | String to prepend to the left of each line |
| [in] | a | Pointer to a multiple precision integer |
| __weak_func error_t mpiExpMod | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | e, | ||
| const Mpi * | p | ||
| ) |
Modular exponentiation.
| [out] | r | Resulting integer R = A ^ E mod P |
| [in] | a | Pointer to a multiple precision integer |
| [in] | e | Exponent |
| [in] | p | Modulus |
| __weak_func error_t mpiExpModFast | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | e, | ||
| const Mpi * | p | ||
| ) |
Modular exponentiation (fast calculation)
| [out] | r | Resulting integer R = A ^ E mod P |
| [in] | a | Pointer to a multiple precision integer |
| [in] | e | Exponent |
| [in] | p | Modulus |
| __weak_func error_t mpiExpModRegular | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | e, | ||
| const Mpi * | p | ||
| ) |
Modular exponentiation (regular calculation)
| [out] | r | Resulting integer R = A ^ E mod P |
| [in] | a | Pointer to a multiple precision integer |
| [in] | e | Exponent |
| [in] | p | Modulus |
| error_t mpiExport | ( | const Mpi * | a, |
| uint8_t * | output, | ||
| size_t | length, | ||
| MpiFormat | format | ||
| ) |
Integer to octet string conversion.
Converts an integer to an octet string of a specified length
| [in] | a | Non-negative integer to be converted |
| [out] | output | Octet string resulting from the conversion |
| [in] | length | Intended length of the resulting octet string |
| [in] | format | Output format |
| void mpiFree | ( | Mpi * | r | ) |
Release a multiple precision integer.
| [in,out] | r | Pointer to the multiple precision integer to be freed |
| uint_t mpiGetBitLength | ( | const Mpi * | a | ) |
Get the actual length in bits.
| [in] | a | Pointer to a multiple precision integer |
| uint_t mpiGetBitValue | ( | const Mpi * | a, |
| uint_t | index | ||
| ) |
Get the bit value at the specified index.
| [in] | a | Pointer to a multiple precision integer |
| [in] | index | Position where to read the bit |
| uint_t mpiGetByteLength | ( | const Mpi * | a | ) |
Get the actual length in bytes.
| [in] | a | Pointer to a multiple precision integer |
| uint_t mpiGetLength | ( | const Mpi * | a | ) |
Get the actual length in words.
| [in] | a | Pointer to a multiple precision integer |
| error_t mpiGrow | ( | Mpi * | r, |
| uint_t | size | ||
| ) |
Adjust the size of multiple precision integer.
| [in,out] | r | A multiple precision integer whose size is to be increased |
| [in] | size | Desired size in words |
| error_t mpiImport | ( | Mpi * | r, |
| const uint8_t * | input, | ||
| size_t | length, | ||
| MpiFormat | format | ||
| ) |
Octet string to integer conversion.
Converts an octet string to a non-negative integer
| [out] | r | Non-negative integer resulting from the conversion |
| [in] | input | Octet string to be converted |
| [in] | length | Length of the octet string |
| [in] | format | Input format |
| void mpiInit | ( | Mpi * | r | ) |
Initialize a multiple precision integer.
| [in,out] | r | Pointer to the multiple precision integer to be initialized |
| __weak_func error_t mpiInvMod | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | p | ||
| ) |
Modular inverse.
| [out] | r | Resulting integer R = A^-1 mod P |
| [in] | a | The multiple precision integer A |
| [in] | p | The modulus P |
| error_t mpiMod | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | p | ||
| ) |
Modulo operation.
| [out] | r | Resulting integer R = A mod P |
| [in] | a | The multiple precision integer to be reduced |
| [in] | p | The modulus P |
| __weak_func error_t mpiMul | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | b | ||
| ) |
Multiple precision multiplication.
| [out] | r | Resulting integer R = A * B |
| [in] | a | First operand A |
| [in] | b | Second operand B |
| error_t mpiMulInt | ( | Mpi * | r, |
| const Mpi * | a, | ||
| mpi_sword_t | b | ||
| ) |
Multiply a multiple precision integer by an integer.
| [out] | r | Resulting integer R = A * B |
| [in] | a | First operand A |
| [in] | b | Second operand B |
| __weak_func error_t mpiMulMod | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | b, | ||
| const Mpi * | p | ||
| ) |
Modular multiplication.
| [out] | r | Resulting integer R = A * B mod P |
| [in] | a | The first operand A |
| [in] | b | The second operand B |
| [in] | p | The modulus P |
| error_t mpiRand | ( | Mpi * | r, |
| uint_t | length, | ||
| const PrngAlgo * | prngAlgo, | ||
| void * | prngContext | ||
| ) |
Generate a random value.
| [out] | r | Pointer to a multiple precision integer |
| [in] | length | Desired length in bits |
| [in] | prngAlgo | PRNG algorithm |
| [in] | prngContext | Pointer to the PRNG context |
| error_t mpiRandRange | ( | Mpi * | r, |
| const Mpi * | p, | ||
| const PrngAlgo * | prngAlgo, | ||
| void * | prngContext | ||
| ) |
Generate a random value in the range 1 to p-1.
| [out] | r | Pointer to a multiple precision integer |
| [in] | p | The upper bound of the range |
| [in] | prngAlgo | PRNG algorithm |
| [in] | prngContext | Pointer to the PRNG context |
| error_t mpiSetBitValue | ( | Mpi * | r, |
| uint_t | index, | ||
| uint_t | value | ||
| ) |
Set the bit value at the specified index.
| [in] | r | Pointer to a multiple precision integer |
| [in] | index | Position of the bit to be written |
| [in] | value | Bit value |
| error_t mpiSetValue | ( | Mpi * | r, |
| mpi_sword_t | a | ||
| ) |
Set the value of a multiple precision integer.
| [out] | r | Pointer to a multiple precision integer |
| [in] | a | Value to be assigned to the multiple precision integer |
| error_t mpiShiftLeft | ( | Mpi * | r, |
| uint_t | n | ||
| ) |
Left shift operation.
| [in,out] | r | The multiple precision integer to be shifted to the left |
| [in] | n | The number of bits to shift |
| error_t mpiShiftRight | ( | Mpi * | r, |
| uint_t | n | ||
| ) |
Right shift operation.
| [in,out] | r | The multiple precision integer to be shifted to the right |
| [in] | n | The number of bits to shift |
| error_t mpiSub | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | b | ||
| ) |
Multiple precision subtraction.
| [out] | r | Resulting integer R = A - B |
| [in] | a | First operand A |
| [in] | b | Second operand B |
| error_t mpiSubAbs | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | b | ||
| ) |
Helper routine for multiple precision subtraction.
| [out] | r | Resulting integer R = |A - B| |
| [in] | a | First operand A |
| [in] | b | Second operand B |
| error_t mpiSubInt | ( | Mpi * | r, |
| const Mpi * | a, | ||
| mpi_sword_t | b | ||
| ) |
Subtract an integer from a multiple precision integer.
| [out] | r | Resulting integer R = A - B |
| [in] | a | First operand A |
| [in] | b | Second operand B |
| error_t mpiSubMod | ( | Mpi * | r, |
| const Mpi * | a, | ||
| const Mpi * | b, | ||
| const Mpi * | p | ||
| ) |
Modular subtraction.
| [out] | r | Resulting integer R = A - B mod P |
| [in] | a | The first operand A |
| [in] | b | The second operand B |
| [in] | p | The modulus P |