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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _MPI_H
30 #define _MPI_H
31 
32 //Dependencies
33 #include <stdio.h>
34 #include "core/crypto.h"
35 
36 //Size of the sub data type
37 #define MPI_INT_SIZE sizeof(uint_t)
38 
39 //Error code checking
40 #define MPI_CHECK(f) if((error = f) != NO_ERROR) goto end
41 
42 //Miscellaneous macros
43 #define mpiIsEven(a) !mpiGetBitValue(a, 0)
44 #define mpiIsOdd(a) mpiGetBitValue(a, 0)
45 
46 //C++ guard
47 #ifdef __cplusplus
48  extern "C" {
49 #endif
50 
51 
52 /**
53  * @brief MPI import/export format
54  **/
55 
56 typedef enum
57 {
60 } MpiFormat;
61 
62 
63 /**
64  * @brief Arbitrary precision integer
65  **/
66 
67 typedef struct
68 {
72 } Mpi;
73 
74 
75 //MPI related functions
76 void mpiInit(Mpi *r);
77 void mpiFree(Mpi *r);
78 
79 error_t mpiGrow(Mpi *r, uint_t size);
80 
81 uint_t mpiGetLength(const Mpi *a);
83 uint_t mpiGetBitLength(const Mpi *a);
84 
86 uint_t mpiGetBitValue(const Mpi *a, uint_t index);
87 
88 int_t mpiComp(const Mpi *a, const Mpi *b);
89 int_t mpiCompInt(const Mpi *a, int_t b);
90 int_t mpiCompAbs(const Mpi *a, const Mpi *b);
91 
92 error_t mpiCopy(Mpi *r, const Mpi *a);
94 
95 error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo, void *prngContext);
96 
97 error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format);
98 error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format);
99 
100 error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b);
101 error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b);
102 
103 error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b);
104 error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b);
105 
106 error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b);
107 error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b);
108 
111 
112 error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b);
113 error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b);
114 
115 error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b);
116 error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b);
117 
118 error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p);
119 error_t mpiAddMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
120 error_t mpiSubMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
121 error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
122 error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p);
123 error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
124 
125 error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k,
126  const Mpi *p, Mpi *t);
127 
128 error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t);
129 
130 void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b);
131 
132 void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a);
133 
134 //C++ guard
135 #ifdef __cplusplus
136  }
137 #endif
138 
139 #endif
int_t mpiComp(const Mpi *a, const Mpi *b)
Compare two multiple precision integers.
Definition: mpi.c:287
uint_t mpiGetLength(const Mpi *a)
Get the actual length in words.
Definition: mpi.c:127
Arbitrary precision integer.
Definition: mpi.h:67
error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value.
Definition: mpi.c:460
int_t sign
Definition: mpi.h:69
char char_t
Definition: compiler_port.h:41
error_t mpiAddMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular addition.
Definition: mpi.c:1348
MpiFormat
MPI import/export format.
Definition: mpi.h:56
error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular multiplication.
Definition: mpi.c:1394
uint8_t p
Definition: ndp.h:295
void mpiFree(Mpi *r)
Release a multiple precision integer.
Definition: mpi.c:60
General definitions for cryptographic algorithms.
uint8_t m
Definition: ndp.h:299
error_t mpiSubMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular subtraction.
Definition: mpi.c:1371
uint8_t a
Definition: ndp.h:407
error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision division.
Definition: mpi.c:1192
error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision subtraction.
Definition: mpi.c:747
signed int int_t
Definition: compiler_port.h:42
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
Definition: mpi.c:154
error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k, const Mpi *p, Mpi *t)
Montgomery multiplication.
Definition: mpi.c:1680
uint_t mpiGetBitValue(const Mpi *a, uint_t index)
Get the bit value at the specified index.
Definition: mpi.c:262
error_t mpiGrow(Mpi *r, uint_t size)
Adjust the size of multiple precision integer.
Definition: mpi.c:83
error_t mpiImport(Mpi *r, const uint8_t *data, uint_t length, MpiFormat format)
Octet string to integer conversion.
Definition: mpi.c:511
error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision multiplication.
Definition: mpi.c:1094
error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p)
Modular inverse.
Definition: mpi.c:1416
error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b)
Helper routine for multiple precision addition.
Definition: mpi.c:820
error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision addition.
Definition: mpi.c:674
error_t mpiSetValue(Mpi *a, int_t b)
Set the value of a multiple precision integer.
Definition: mpi.c:429
error_t mpiCopy(Mpi *r, const Mpi *a)
Copy a multiple precision integer.
Definition: mpi.c:392
int_t mpiCompAbs(const Mpi *a, const Mpi *b)
Compare the absolute value of two multiple precision integers.
Definition: mpi.c:354
error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t)
Montgomery reduction.
Definition: mpi.c:1751
void mpiInit(Mpi *r)
Initialize a multiple precision integer.
Definition: mpi.c:46
uint_t * data
Definition: mpi.h:71
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
uint8_t data[]
Definition: dtls_misc.h:167
Common interface for pseudo-random number generators.
Definition: crypto.h:1091
uint8_t value[]
Definition: dtls_misc.h:141
error_t mpiExport(const Mpi *a, uint8_t *data, uint_t length, MpiFormat format)
Integer to octet string conversion.
Definition: mpi.c:596
uint32_t r
Definition: ndp.h:342
void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b)
Multiply-accumulate operation.
Definition: mpi.c:1777
void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a)
Display the contents of a multiple precision integer.
Definition: mpi.c:1825
error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b)
Subtract an integer from a multiple precision integer.
Definition: mpi.c:796
error_t mpiShiftRight(Mpi *r, uint_t n)
Right shift operation.
Definition: mpi.c:1041
error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b)
Add an integer to a multiple precision integer.
Definition: mpi.c:723
uint_t size
Definition: mpi.h:70
error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p)
Modulo operation.
Definition: mpi.c:1283
error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b)
Multiply a multiple precision integer by an integer.
Definition: mpi.c:1167
uint8_t length
Definition: dtls_misc.h:140
uint8_t n
int_t mpiCompInt(const Mpi *a, int_t b)
Compare a multiple precision integer with an integer.
Definition: mpi.c:331
error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b)
Helper routine for multiple precision subtraction.
Definition: mpi.c:900
error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b)
Divide a multiple precision integer by an integer.
Definition: mpi.c:1259
error_t mpiSetBitValue(Mpi *r, uint_t index, uint_t value)
Set the bit value at the specified index.
Definition: mpi.c:228
error_t mpiShiftLeft(Mpi *r, uint_t n)
Left shift operation.
Definition: mpi.c:986
uint8_t b[6]
Definition: dtls_misc.h:130
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Definition: mpi.c:190
error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
Definition: mpi.c:1492