mpi.h
Go to the documentation of this file.
1 /**
2  * @file mpi.h
3  * @brief MPI (Multiple Precision Integer Arithmetic)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _MPI_H
32 #define _MPI_H
33 
34 //Dependencies
35 #include <stdio.h>
36 #include "core/crypto.h"
37 
38 //Maximum size, in bits, of a multiple precision integer (static memory allocation)
39 #ifndef MPI_MAX_BIT_SIZE
40  #define MPI_MAX_BIT_SIZE 4096
41 #elif (MPI_MAX_BIT_SIZE < 0)
42  #error MPI_MAX_BIT_SIZE parameter is not valid
43 #endif
44 
45 //Size of the sub data type
46 #define MPI_INT_SIZE sizeof(uint_t)
47 
48 //Maximum size, in words, of a multiple precision integer
49 #define MPI_MAX_INT_SIZE ((MPI_MAX_BIT_SIZE + (MPI_INT_SIZE * 8) - 1) / (MPI_INT_SIZE * 8))
50 
51 //Error code checking
52 #define MPI_CHECK(f) if((error = f) != NO_ERROR) goto end
53 
54 //Miscellaneous macros
55 #define mpiIsEven(a) !mpiGetBitValue(a, 0)
56 #define mpiIsOdd(a) mpiGetBitValue(a, 0)
57 
58 //C++ guard
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 
64 /**
65  * @brief MPI import/export format
66  **/
67 
68 typedef enum
69 {
73 
74 
75 /**
76  * @brief Arbitrary precision integer
77  **/
78 
79 typedef struct
80 {
83 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
85 #else
87 #endif
88 } Mpi;
89 
90 
91 //MPI related functions
92 void mpiInit(Mpi *r);
93 void mpiFree(Mpi *r);
94 
95 error_t mpiGrow(Mpi *r, uint_t size);
96 
97 uint_t mpiGetLength(const Mpi *a);
99 uint_t mpiGetBitLength(const Mpi *a);
100 
102 uint_t mpiGetBitValue(const Mpi *a, uint_t index);
103 
104 int_t mpiComp(const Mpi *a, const Mpi *b);
105 int_t mpiCompInt(const Mpi *a, int_t b);
106 int_t mpiCompAbs(const Mpi *a, const Mpi *b);
107 
108 error_t mpiCopy(Mpi *r, const Mpi *a);
110 
111 error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo,
112  void *prngContext);
113 
114 error_t mpiRandRange(Mpi *r, const Mpi *p, const PrngAlgo *prngAlgo,
115  void *prngContext);
116 
118 
119 error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format);
120 error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format);
121 
122 error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b);
123 error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b);
124 
125 error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b);
126 error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b);
127 
128 error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b);
129 error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b);
130 
133 
134 error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b);
135 error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b);
136 
137 error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b);
138 error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b);
139 
140 error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p);
141 error_t mpiAddMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
142 error_t mpiSubMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
143 error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
144 error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p);
145 
146 error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
147 error_t mpiExpModFast(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
148 error_t mpiExpModRegular(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
149 
150 error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k,
151  const Mpi *p, Mpi *t);
152 
153 error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t);
154 
155 void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b);
156 
157 void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a);
158 
159 //C++ guard
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif
signed int int_t
Definition: compiler_port.h:49
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
General definitions for cryptographic algorithms.
#define PrngAlgo
Definition: crypto.h:917
uint8_t n
error_t
Error codes.
Definition: error.h:43
uint8_t data[]
Definition: ethernet.h:222
uint8_t t
Definition: lldp_ext_med.h:212
error_t mpiShiftLeft(Mpi *r, uint_t n)
Left shift operation.
Definition: mpi.c:1111
error_t mpiRandRange(Mpi *r, const Mpi *p, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value in the range 1 to p-1.
Definition: mpi.c:564
uint_t mpiGetLength(const Mpi *a)
Get the actual length in words.
Definition: mpi.c:168
error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p)
Modular inverse.
error_t mpiSubMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular subtraction.
Definition: mpi.c:1532
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Definition: mpi.c:234
error_t mpiCopy(Mpi *r, const Mpi *a)
Copy a multiple precision integer.
Definition: mpi.c:447
error_t mpiAddMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular addition.
Definition: mpi.c:1509
error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision subtraction.
Definition: mpi.c:864
error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k, const Mpi *p, Mpi *t)
Montgomery multiplication.
Definition: mpi.c:1877
int_t mpiCompInt(const Mpi *a, int_t b)
Compare a multiple precision integer with an integer.
Definition: mpi.c:382
error_t mpiExpModRegular(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (regular calculation)
int_t mpiComp(const Mpi *a, const Mpi *b)
Compare two multiple precision integers.
Definition: mpi.c:338
error_t mpiShiftRight(Mpi *r, uint_t n)
Right shift operation.
Definition: mpi.c:1178
error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision division.
Definition: mpi.c:1343
error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision multiplication.
MpiFormat
MPI import/export format.
Definition: mpi.h:69
@ MPI_FORMAT_BIG_ENDIAN
Definition: mpi.h:71
@ MPI_FORMAT_LITTLE_ENDIAN
Definition: mpi.h:70
error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b)
Add an integer to a multiple precision integer.
Definition: mpi.c:836
void mpiInit(Mpi *r)
Initialize a multiple precision integer.
Definition: mpi.c:48
error_t mpiSetBitValue(Mpi *r, uint_t index, uint_t value)
Set the bit value at the specified index.
Definition: mpi.c:275
error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b)
Subtract an integer from a multiple precision integer.
Definition: mpi.c:913
error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b)
Multiply a multiple precision integer by an integer.
Definition: mpi.c:1314
error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b)
Helper routine for multiple precision addition.
Definition: mpi.c:941
#define MPI_MAX_INT_SIZE
Definition: mpi.h:49
error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format)
Octet string to integer conversion.
Definition: mpi.c:624
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
Definition: mpi.c:195
error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b)
Helper routine for multiple precision subtraction.
Definition: mpi.c:1021
error_t mpiSetValue(Mpi *a, int_t b)
Set the value of a multiple precision integer.
Definition: mpi.c:484
error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b)
Divide a multiple precision integer by an integer.
Definition: mpi.c:1416
error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value.
Definition: mpi.c:515
void mpiFree(Mpi *r)
Release a multiple precision integer.
Definition: mpi.c:64
error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format)
Integer to octet string conversion.
Definition: mpi.c:709
void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a)
Display the contents of a multiple precision integer.
Definition: mpi.c:2028
void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b)
Multiply-accumulate operation.
Definition: mpi.c:1980
error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular multiplication.
error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision addition.
Definition: mpi.c:787
error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p)
Modulo operation.
Definition: mpi.c:1444
error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t)
Montgomery reduction.
Definition: mpi.c:1950
uint_t mpiGetBitValue(const Mpi *a, uint_t index)
Get the bit value at the specified index.
Definition: mpi.c:313
error_t mpiGrow(Mpi *r, uint_t size)
Adjust the size of multiple precision integer.
Definition: mpi.c:94
error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
error_t mpiExpModFast(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (fast calculation)
int_t mpiCompAbs(const Mpi *a, const Mpi *b)
Compare the absolute value of two multiple precision integers.
Definition: mpi.c:409
error_t mpiCheckProbablePrime(const Mpi *a)
Test whether a number is probable prime.
uint8_t b
Definition: nbns_common.h:104
uint8_t r
Definition: ndp.h:346
uint8_t p
Definition: ndp.h:300
uint8_t m
Definition: ndp.h:304
uint8_t a
Definition: ndp.h:411
Arbitrary precision integer.
Definition: mpi.h:80
uint_t * data
Definition: mpi.h:84
uint_t size
Definition: mpi.h:82
int_t sign
Definition: mpi.h:81
uint8_t length
Definition: tcp.h:368
uint8_t value[]
Definition: tcp.h:369