aboutsummaryrefslogtreecommitdiff
path: root/epid/common/math/bignum.h
blob: e9e9df4df8faef7ab554e8c1f830232a02afddb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*############################################################################
  # Copyright 2016 Intel Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ############################################################################*/

/*!
 * \file
 * \brief Big number interface.
 */

#ifndef EPID_COMMON_MATH_BIGNUM_H_
#define EPID_COMMON_MATH_BIGNUM_H_

#include <stddef.h>
#include <stdint.h>
#include "epid/common/stdtypes.h"
#include "epid/common/errors.h"
#include "epid/common/types.h"

/// Big number operations
/*!
  \defgroup BigNumPrimitives bignum
  This module provides an API for working with large numbers. BigNums
  represent non-negative integers.

  Each BigNum variable represents a number of a byte-size set when the variable
  was created. BigNum variables cannot be re-sized after they are created.


  \ingroup EpidMath
  @{
*/

/// Internal representation of large numbers
typedef struct BigNum BigNum;

/// Constructs a new BigNum.
/*!
  Allocates memory and creates a new BigNum.

  Use DeleteBigNum() to free memory.

  \param[in] data_size_bytes
  The size in bytes of the new number.
  \param[out] bignum
  The BigNum.

  \returns ::EpidStatus

  \see DeleteBigNum
*/
EpidStatus NewBigNum(size_t data_size_bytes, BigNum** bignum);

/// Deletes a previously allocated BigNum.
/*!
  Frees memory pointed to by bignum. Nulls the pointer.

  \param[in] bignum
  The BigNum. Can be NULL.

  \see NewBigNum
*/
void DeleteBigNum(BigNum** bignum);

/// Deserializes a BigNum from a string.
/*!
  \param[in] bn_str
  The serialized value.
  \param[in] strlen
  The size of bn_str in bytes.
  \param[out] bn
  The target BigNum.

  \returns ::EpidStatus
*/
EpidStatus ReadBigNum(void const* bn_str, size_t strlen, BigNum* bn);

/// Serializes a BigNum to a string.
/*!
  \param[in] bn
  The BigNum to be serialized.
  \param[in] strlen
  The size of bn_str in bytes.
  \param[out] bn_str
  The target string.

  \returns ::EpidStatus
*/
EpidStatus WriteBigNum(BigNum const* bn, size_t strlen, void* bn_str);

/// Adds two BigNum values.
/*!
  \param[in] a
  The first operand to be added.
  \param[in] b
  The second operand to be added.
  \param[out] r
  The result of adding a and b.

  \returns ::EpidStatus
*/
EpidStatus BigNumAdd(BigNum const* a, BigNum const* b, BigNum* r);

/// Subtracts two BigNum values.
/*!
  \param[in] a
  The first operand to use in subtraction.
  \param[in] b
  The second operand to use in subtraction.
  \param[out] r
  The result of subtracting a and b.

  \returns ::EpidStatus
*/
EpidStatus BigNumSub(BigNum const* a, BigNum const* b, BigNum* r);

/// Multiplies two BigNum values.
/*!
  \param[in] a
  The first operand to be multiplied.
  \param[in] b
  The second operand to be multiplied.
  \param[out] r
  The result of multiplying a and b.

  \returns ::EpidStatus
*/
EpidStatus BigNumMul(BigNum const* a, BigNum const* b, BigNum* r);

/// Divides two BigNum values.
/*!
\note Only needed for Intel(R) EPID 1.1 verification.

\param[in] a
Dividend parameter.
\param[in] b
Divisor parameter.
\param[out] q
Quotient of result.
\param[out] r
Remainder of result.

\returns ::EpidStatus
*/
EpidStatus BigNumDiv(BigNum const* a, BigNum const* b, BigNum* q, BigNum* r);

/// Computes modular reduction for BigNum value by specified modulus.
/*!
\param[in] a
The BigNum value.
\param[in] b
The modulus.
\param[out] r
Modular reduction result.

\returns ::EpidStatus
*/
EpidStatus BigNumMod(BigNum const* a, BigNum const* b, BigNum* r);

/// Checks if a BigNum is even.
/*!
 \param[in] a
 The BigNum to check.
 \param[out] is_even
 The result of the check.

 \returns ::EpidStatus
 */
EpidStatus BigNumIsEven(BigNum const* a, bool* is_even);

/// Checks if a BigNum is zero.
/*!
 \param[in] a
 The BigNum to check.
 \param[out] is_zero
 The result of the check.

 \returns ::EpidStatus
 */
EpidStatus BigNumIsZero(BigNum const* a, bool* is_zero);

/// Raises 2 to the given power
/*!
 \param[in] n
 The power.
 \param[out] r
 The result of 2^n.

 \returns ::EpidStatus
 */
EpidStatus BigNumPow2N(unsigned int n, BigNum* r);

/*!
  @}
*/
#endif  // EPID_COMMON_MATH_BIGNUM_H_