bignum.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*############################################################################
  2. # Copyright 2016 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief Big number interface.
  19. */
  20. #ifndef EPID_COMMON_MATH_BIGNUM_H_
  21. #define EPID_COMMON_MATH_BIGNUM_H_
  22. #include <stddef.h>
  23. #include <stdint.h>
  24. #include "epid/common/stdtypes.h"
  25. #include "epid/common/errors.h"
  26. #include "epid/common/types.h"
  27. /// Big number operations
  28. /*!
  29. \defgroup BigNumPrimitives bignum
  30. This module provides an API for working with large numbers. BigNums
  31. represent non-negative integers.
  32. Each BigNum variable represents a number of a byte-size set when the variable
  33. was created. BigNum variables cannot be re-sized after they are created.
  34. \ingroup EpidMath
  35. @{
  36. */
  37. /// Internal representation of large numbers
  38. typedef struct BigNum BigNum;
  39. /// Constructs a new BigNum.
  40. /*!
  41. Allocates memory and creates a new BigNum.
  42. Use DeleteBigNum() to free memory.
  43. \param[in] data_size_bytes
  44. The size in bytes of the new number.
  45. \param[out] bignum
  46. The BigNum.
  47. \returns ::EpidStatus
  48. \see DeleteBigNum
  49. */
  50. EpidStatus NewBigNum(size_t data_size_bytes, BigNum** bignum);
  51. /// Deletes a previously allocated BigNum.
  52. /*!
  53. Frees memory pointed to by bignum. Nulls the pointer.
  54. \param[in] bignum
  55. The BigNum. Can be NULL.
  56. \see NewBigNum
  57. */
  58. void DeleteBigNum(BigNum** bignum);
  59. /// Deserializes a BigNum from a string.
  60. /*!
  61. \param[in] bn_str
  62. The serialized value.
  63. \param[in] strlen
  64. The size of bn_str in bytes.
  65. \param[out] bn
  66. The target BigNum.
  67. \returns ::EpidStatus
  68. */
  69. EpidStatus ReadBigNum(void const* bn_str, size_t strlen, BigNum* bn);
  70. /// Serializes a BigNum to a string.
  71. /*!
  72. \param[in] bn
  73. The BigNum to be serialized.
  74. \param[in] strlen
  75. The size of bn_str in bytes.
  76. \param[out] bn_str
  77. The target string.
  78. \returns ::EpidStatus
  79. */
  80. EpidStatus WriteBigNum(BigNum const* bn, size_t strlen, void* bn_str);
  81. /// Adds two BigNum values.
  82. /*!
  83. \param[in] a
  84. The first operand to be added.
  85. \param[in] b
  86. The second operand to be added.
  87. \param[out] r
  88. The result of adding a and b.
  89. \returns ::EpidStatus
  90. */
  91. EpidStatus BigNumAdd(BigNum const* a, BigNum const* b, BigNum* r);
  92. /// Subtracts two BigNum values.
  93. /*!
  94. \param[in] a
  95. The first operand to use in subtraction.
  96. \param[in] b
  97. The second operand to use in subtraction.
  98. \param[out] r
  99. The result of subtracting a and b.
  100. \returns ::EpidStatus
  101. */
  102. EpidStatus BigNumSub(BigNum const* a, BigNum const* b, BigNum* r);
  103. /// Multiplies two BigNum values.
  104. /*!
  105. \param[in] a
  106. The first operand to be multiplied.
  107. \param[in] b
  108. The second operand to be multiplied.
  109. \param[out] r
  110. The result of multiplying a and b.
  111. \returns ::EpidStatus
  112. */
  113. EpidStatus BigNumMul(BigNum const* a, BigNum const* b, BigNum* r);
  114. /// Divides two BigNum values.
  115. /*!
  116. \note Only needed for Intel(R) EPID 1.1 verification.
  117. \param[in] a
  118. Dividend parameter.
  119. \param[in] b
  120. Divisor parameter.
  121. \param[out] q
  122. Quotient of result.
  123. \param[out] r
  124. Remainder of result.
  125. \returns ::EpidStatus
  126. */
  127. EpidStatus BigNumDiv(BigNum const* a, BigNum const* b, BigNum* q, BigNum* r);
  128. /// Computes modular reduction for BigNum value by specified modulus.
  129. /*!
  130. \param[in] a
  131. The BigNum value.
  132. \param[in] b
  133. The modulus.
  134. \param[out] r
  135. Modular reduction result.
  136. \returns ::EpidStatus
  137. */
  138. EpidStatus BigNumMod(BigNum const* a, BigNum const* b, BigNum* r);
  139. /// Checks if a BigNum is even.
  140. /*!
  141. \param[in] a
  142. The BigNum to check.
  143. \param[out] is_even
  144. The result of the check.
  145. \returns ::EpidStatus
  146. */
  147. EpidStatus BigNumIsEven(BigNum const* a, bool* is_even);
  148. /// Checks if a BigNum is zero.
  149. /*!
  150. \param[in] a
  151. The BigNum to check.
  152. \param[out] is_zero
  153. The result of the check.
  154. \returns ::EpidStatus
  155. */
  156. EpidStatus BigNumIsZero(BigNum const* a, bool* is_zero);
  157. /// Raises 2 to the given power
  158. /*!
  159. \param[in] n
  160. The power.
  161. \param[out] r
  162. The result of 2^n.
  163. \returns ::EpidStatus
  164. */
  165. EpidStatus BigNumPow2N(unsigned int n, BigNum* r);
  166. /*!
  167. @}
  168. */
  169. #endif // EPID_COMMON_MATH_BIGNUM_H_