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-2022 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.1.4
29  **/
30 
31 #ifndef _MPI_H
32 #define _MPI_H
33 
34 //Dependencies
35 #include <stdio.h>
36 #include "core/crypto.h"
37 
38 //Size of the sub data type
39 #define MPI_INT_SIZE sizeof(uint_t)
40 
41 //Error code checking
42 #define MPI_CHECK(f) if((error = f) != NO_ERROR) goto end
43 
44 //Miscellaneous macros
45 #define mpiIsEven(a) !mpiGetBitValue(a, 0)
46 #define mpiIsOdd(a) mpiGetBitValue(a, 0)
47 
48 //C++ guard
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 
54 /**
55  * @brief MPI import/export format
56  **/
57 
58 typedef enum
59 {
63 
64 
65 /**
66  * @brief Arbitrary precision integer
67  **/
68 
69 typedef struct
70 {
74 } Mpi;
75 
76 
77 //MPI related functions
78 void mpiInit(Mpi *r);
79 void mpiFree(Mpi *r);
80 
81 error_t mpiGrow(Mpi *r, uint_t size);
82 
83 uint_t mpiGetLength(const Mpi *a);
85 uint_t mpiGetBitLength(const Mpi *a);
86 
88 uint_t mpiGetBitValue(const Mpi *a, uint_t index);
89 
90 int_t mpiComp(const Mpi *a, const Mpi *b);
91 int_t mpiCompInt(const Mpi *a, int_t b);
92 int_t mpiCompAbs(const Mpi *a, const Mpi *b);
93 
94 error_t mpiCopy(Mpi *r, const Mpi *a);
96 
97 error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo,
98  void *prngContext);
99 
100 error_t mpiRandRange(Mpi *r, const Mpi *p, const PrngAlgo *prngAlgo,
101  void *prngContext);
102 
104 
105 error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format);
106 error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format);
107 
108 error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b);
109 error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b);
110 
111 error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b);
112 error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b);
113 
114 error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b);
115 error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b);
116 
119 
120 error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b);
121 error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b);
122 
123 error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b);
124 error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b);
125 
126 error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p);
127 error_t mpiAddMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
128 error_t mpiSubMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
129 error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
130 error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p);
131 
132 error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
133 error_t mpiExpModFast(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
134 error_t mpiExpModRegular(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
135 
136 error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k,
137  const Mpi *p, Mpi *t);
138 
139 error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t);
140 
141 void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b);
142 
143 void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a);
144 
145 //C++ guard
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif
uint8_t length
Definition: coap_common.h:191
signed int int_t
Definition: compiler_port.h:44
unsigned int uint_t
Definition: compiler_port.h:45
char char_t
Definition: compiler_port.h:43
General definitions for cryptographic algorithms.
uint8_t n
error_t
Error codes.
Definition: error.h:43
uint8_t data[]
Definition: ethernet.h:220
uint8_t b[6]
Definition: ethernet.h:190
uint8_t t
Definition: lldp_ext_med.h:210
error_t mpiShiftLeft(Mpi *r, uint_t n)
Left shift operation.
Definition: mpi.c:1056
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:517
uint_t mpiGetLength(const Mpi *a)
Get the actual length in words.
Definition: mpi.c:129
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:1457
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Definition: mpi.c:195
error_t mpiCopy(Mpi *r, const Mpi *a)
Copy a multiple precision integer.
Definition: mpi.c:400
error_t mpiAddMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular addition.
Definition: mpi.c:1434
error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision subtraction.
Definition: mpi.c:813
error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k, const Mpi *p, Mpi *t)
Montgomery multiplication.
Definition: mpi.c:1802
int_t mpiCompInt(const Mpi *a, int_t b)
Compare a multiple precision integer with an integer.
Definition: mpi.c:339
error_t mpiExpModRegular(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (regular calculation)
Definition: mpi.c:1784
int_t mpiComp(const Mpi *a, const Mpi *b)
Compare two multiple precision integers.
Definition: mpi.c:295
error_t mpiShiftRight(Mpi *r, uint_t n)
Right shift operation.
Definition: mpi.c:1117
error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision division.
Definition: mpi.c:1278
error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision multiplication.
MpiFormat
MPI import/export format.
Definition: mpi.h:59
@ MPI_FORMAT_BIG_ENDIAN
Definition: mpi.h:61
@ MPI_FORMAT_LITTLE_ENDIAN
Definition: mpi.h:60
error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b)
Add an integer to a multiple precision integer.
Definition: mpi.c:789
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:236
error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b)
Subtract an integer from a multiple precision integer.
Definition: mpi.c:862
error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b)
Multiply a multiple precision integer by an integer.
Definition: mpi.c:1253
error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b)
Helper routine for multiple precision addition.
Definition: mpi.c:886
error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format)
Octet string to integer conversion.
Definition: mpi.c:577
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
Definition: mpi.c:156
error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b)
Helper routine for multiple precision subtraction.
Definition: mpi.c:966
error_t mpiSetValue(Mpi *a, int_t b)
Set the value of a multiple precision integer.
Definition: mpi.c:437
error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b)
Divide a multiple precision integer by an integer.
Definition: mpi.c:1345
error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value.
Definition: mpi.c:468
void mpiFree(Mpi *r)
Release a multiple precision integer.
Definition: mpi.c:62
error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format)
Integer to octet string conversion.
Definition: mpi.c:662
void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a)
Display the contents of a multiple precision integer.
Definition: mpi.c:1949
void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b)
Multiply-accumulate operation.
Definition: mpi.c:1901
error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular multiplication.
Definition: mpi.c:1480
error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision addition.
Definition: mpi.c:740
error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p)
Modulo operation.
Definition: mpi.c:1369
error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t)
Montgomery reduction.
Definition: mpi.c:1875
uint_t mpiGetBitValue(const Mpi *a, uint_t index)
Get the bit value at the specified index.
Definition: mpi.c:270
error_t mpiGrow(Mpi *r, uint_t size)
Adjust the size of multiple precision integer.
Definition: mpi.c:85
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)
Definition: mpi.c:1768
int_t mpiCompAbs(const Mpi *a, const Mpi *b)
Compare the absolute value of two multiple precision integers.
Definition: mpi.c:362
error_t mpiCheckProbablePrime(const Mpi *a)
Test whether a number is probable prime.
uint8_t r
Definition: ndp.h:344
uint8_t p
Definition: ndp.h:298
uint8_t m
Definition: ndp.h:302
uint8_t a
Definition: ndp.h:409
Arbitrary precision integer.
Definition: mpi.h:70
uint_t * data
Definition: mpi.h:73
uint_t size
Definition: mpi.h:72
int_t sign
Definition: mpi.h:71
Common interface for pseudo-random number generators.
Definition: crypto.h:892
uint8_t value[]
Definition: tcp.h:367