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-2019 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 1.9.6
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 {
62 } MpiFormat;
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 
101 
102 error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format);
103 error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format);
104 
105 error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b);
106 error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b);
107 
108 error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b);
109 error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b);
110 
111 error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b);
112 error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b);
113 
116 
117 error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b);
118 error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b);
119 
120 error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b);
121 error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b);
122 
123 error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p);
124 error_t mpiAddMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
125 error_t mpiSubMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
126 error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
127 error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p);
128 error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
129 
130 error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k,
131  const Mpi *p, Mpi *t);
132 
133 error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t);
134 
135 void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b);
136 
137 void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a);
138 
139 //C++ guard
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 #endif
error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision multiplication.
uint8_t length
Definition: dtls_misc.h:149
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:1390
uint8_t a
Definition: ndp.h:410
void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a)
Display the contents of a multiple precision integer.
Definition: mpi.c:1873
Arbitrary precision integer.
Definition: mpi.h:69
Common interface for pseudo-random number generators.
Definition: crypto.h:1168
signed int int_t
Definition: compiler_port.h:44
uint8_t b[6]
Definition: dtls_misc.h:139
uint8_t p
Definition: ndp.h:298
int_t mpiCompInt(const Mpi *a, int_t b)
Compare a multiple precision integer with an integer.
Definition: mpi.c:339
error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b)
Helper routine for multiple precision subtraction.
Definition: mpi.c:922
int_t mpiCompAbs(const Mpi *a, const Mpi *b)
Compare the absolute value of two multiple precision integers.
Definition: mpi.c:362
void mpiInit(Mpi *r)
Initialize a multiple precision integer.
Definition: mpi.c:48
void mpiFree(Mpi *r)
Release a multiple precision integer.
Definition: mpi.c:62
error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b)
Helper routine for multiple precision addition.
Definition: mpi.c:842
int_t mpiComp(const Mpi *a, const Mpi *b)
Compare two multiple precision integers.
Definition: mpi.c:295
uint32_t r
Definition: ndp.h:345
uint8_t t
Definition: llmnr_common.h:81
error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular multiplication.
Definition: mpi.c:1436
error_t
Error codes.
Definition: error.h:42
error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k, const Mpi *p, Mpi *t)
Montgomery multiplication.
Definition: mpi.c:1726
General definitions for cryptographic algorithms.
error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p)
Modular inverse.
error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t)
Montgomery reduction.
Definition: mpi.c:1799
uint_t mpiGetLength(const Mpi *a)
Get the actual length in words.
Definition: mpi.c:129
uint_t * data
Definition: mpi.h:73
void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b)
Multiply-accumulate operation.
Definition: mpi.c:1825
error_t mpiSetBitValue(Mpi *r, uint_t index, uint_t value)
Set the bit value at the specified index.
Definition: mpi.c:236
MpiFormat
MPI import/export format.
Definition: mpi.h:58
error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision addition.
Definition: mpi.c:696
char char_t
Definition: compiler_port.h:43
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Definition: mpi.c:195
error_t mpiCheckProbablePrime(const Mpi *a)
Test whether a number is probable prime.
error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b)
Add an integer to a multiple precision integer.
Definition: mpi.c:745
uint8_t m
Definition: ndp.h:302
uint8_t n
error_t mpiGrow(Mpi *r, uint_t size)
Adjust the size of multiple precision integer.
Definition: mpi.c:85
uint_t size
Definition: mpi.h:72
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
Definition: mpi.c:156
uint_t mpiGetBitValue(const Mpi *a, uint_t index)
Get the bit value at the specified index.
Definition: mpi.c:270
error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format)
Integer to octet string conversion.
Definition: mpi.c:618
error_t mpiSubMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular subtraction.
Definition: mpi.c:1413
error_t mpiShiftRight(Mpi *r, uint_t n)
Right shift operation.
Definition: mpi.c:1073
error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value.
Definition: mpi.c:468
error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision division.
Definition: mpi.c:1234
error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format)
Octet string to integer conversion.
Definition: mpi.c:533
error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b)
Multiply a multiple precision integer by an integer.
Definition: mpi.c:1209
uint8_t value[]
Definition: dtls_misc.h:150
unsigned int uint_t
Definition: compiler_port.h:45
uint8_t data[]
Definition: dtls_misc.h:176
error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p)
Modulo operation.
Definition: mpi.c:1325
error_t mpiSetValue(Mpi *a, int_t b)
Set the value of a multiple precision integer.
Definition: mpi.c:437
error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision subtraction.
Definition: mpi.c:769
error_t mpiShiftLeft(Mpi *r, uint_t n)
Left shift operation.
Definition: mpi.c:1012
error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b)
Subtract an integer from a multiple precision integer.
Definition: mpi.c:818
int_t sign
Definition: mpi.h:71
error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b)
Divide a multiple precision integer by an integer.
Definition: mpi.c:1301