ecgroup.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 Elliptic curve group interface.
  19. */
  20. #ifndef EPID_COMMON_MATH_ECGROUP_H_
  21. #define EPID_COMMON_MATH_ECGROUP_H_
  22. #include "epid/common/stdtypes.h"
  23. #include "epid/common/errors.h"
  24. #include "epid/common/math/bignum.h"
  25. #include "epid/common/math/finitefield.h"
  26. #include "epid/common/types.h"
  27. /// Elliptic curve group operations
  28. /*!
  29. \defgroup EcGroupPrimitives ecgroup
  30. Provides APIs for working with Elliptic curve groups.
  31. Elliptic curve groups allow simple mathematical operations based on points
  32. that lie on a defined elliptic curve. The results of these operations also
  33. lie on the same curve.
  34. Curves themselves are defined based on elements (::FfElement) of a finite
  35. field (::FiniteField).
  36. \ingroup EpidMath
  37. @{
  38. */
  39. /// Elliptic curve group over finite field.
  40. typedef struct EcGroup EcGroup;
  41. /// Constructs a new EcGroup.
  42. /*!
  43. Allocates memory and creates a new elliptic curve group.
  44. Use DeleteFiniteField() to free memory.
  45. \param[in] ff
  46. The finite field on which the curve is based.
  47. \param[in] a
  48. The A value of the elliptic curve.
  49. \param[in] b
  50. The B value of the elliptic curve.
  51. \param[in] x
  52. The X-coordinate of the base point of the elliptic curve.
  53. \param[in] y
  54. The Y-coordinate of the base point of the elliptic curve.
  55. \param[in] order
  56. The order of the elliptic curve group.
  57. \param[in] cofactor
  58. The co-factor of the elliptic curve.
  59. \param[out] g
  60. The newly constructed elliptic curve group.
  61. \returns ::EpidStatus
  62. \see DeleteEcGroup
  63. */
  64. EpidStatus NewEcGroup(FiniteField const* ff, FfElement const* a,
  65. FfElement const* b, FfElement const* x,
  66. FfElement const* y, BigNum const* order,
  67. BigNum const* cofactor, EcGroup** g);
  68. /// Deletes a previously allocated EcGroup.
  69. /*!
  70. Frees memory pointed to by elliptic curve group. Nulls the pointer.
  71. \param[in] g
  72. The elliptic curve group. Can be NULL.
  73. \see NewEcGroup
  74. */
  75. void DeleteEcGroup(EcGroup** g);
  76. /// Point on elliptic curve over finite field.
  77. typedef struct EcPoint EcPoint;
  78. /// Creates a new EcPoint.
  79. /*!
  80. Allocates memory and creates a new point on elliptic curve group.
  81. Use DeleteEcPoint() to free memory.
  82. \param[in] g
  83. Elliptic curve group.
  84. \param[out] p
  85. Newly constructed point on the elliptic curve group g.
  86. \returns ::EpidStatus
  87. \see NewEcGroup
  88. \see DeleteEcPoint
  89. */
  90. EpidStatus NewEcPoint(EcGroup const* g, EcPoint** p);
  91. /// Deletes a previously allocated EcPoint.
  92. /*!
  93. Frees memory used by a point on elliptic curve group. Nulls the pointer.
  94. \param[in] p
  95. The EcPoint. Can be NULL.
  96. \see NewEcPoint
  97. */
  98. void DeleteEcPoint(EcPoint** p);
  99. /// Deserializes an EcPoint from a string.
  100. /*!
  101. \param[in] g
  102. The elliptic curve group.
  103. \param[in] p_str
  104. The serialized value.
  105. \param[in] strlen
  106. The size of p_str in bytes.
  107. \param[out] p
  108. The target EcPoint.
  109. \returns ::EpidStatus
  110. \see NewEcPoint
  111. */
  112. EpidStatus ReadEcPoint(EcGroup* g, void const* p_str, size_t strlen,
  113. EcPoint* p);
  114. /// Serializes an EcPoint to a string.
  115. /*!
  116. \param[in] g
  117. The elliptic curve group.
  118. \param[in] p
  119. The EcPoint to be serialized.
  120. \param[out] p_str
  121. The target string.
  122. \param[in] strlen
  123. the size of p_str in bytes.
  124. \returns ::EpidStatus
  125. \see NewEcPoint
  126. */
  127. EpidStatus WriteEcPoint(EcGroup* g, EcPoint const* p, void* p_str,
  128. size_t strlen);
  129. /// Multiplies two elements in an elliptic curve group.
  130. /*!
  131. This multiplication operation is also known as element addition for
  132. elliptic curve groups.
  133. \param[in] g
  134. The elliptic curve group.
  135. \param[in] a
  136. The first operand to be multiplied.
  137. \param[in] b
  138. The second operand to be multiplied.
  139. \param[out] r
  140. The result of multiplying a and b.
  141. \returns ::EpidStatus
  142. \see NewEcGroup
  143. \see NewEcPoint
  144. */
  145. EpidStatus EcMul(EcGroup* g, EcPoint const* a, EcPoint const* b, EcPoint* r);
  146. /// Raises a point in an elliptic curve group to a power.
  147. /*!
  148. This exponentiation operation is also known as element multiplication
  149. for elliptic curve groups.
  150. \param[in] g
  151. The elliptic curve group.
  152. \param[in] a
  153. The base.
  154. \param[in] b
  155. The power. Power must be less than the order of the elliptic curve
  156. group.
  157. \param[out] r
  158. The result of raising a to the power b.
  159. \returns ::EpidStatus
  160. \see NewEcGroup
  161. \see NewEcPoint
  162. */
  163. EpidStatus EcExp(EcGroup* g, EcPoint const* a, BigNumStr const* b, EcPoint* r);
  164. /// Software side-channel mitigated implementation of EcExp.
  165. /*!
  166. This exponentiation operation is also known as element multiplication
  167. for elliptic curve groups.
  168. \attention
  169. The reference implementation of EcSscmExp calls EcExp directly because
  170. the implementation of EcExp is already side channel mitigated. Implementers
  171. providing their own versions of this function are responsible for ensuring
  172. that EcSscmExp is side channel mitigated per section 8 of the
  173. Intel(R) EPID 2.0 spec.
  174. \param[in] g
  175. The elliptic curve group.
  176. \param[in] a
  177. The base.
  178. \param[in] b
  179. The power. Power must be less than the order of the elliptic curve
  180. group.
  181. \param[out] r
  182. The result of raising a to the power b.
  183. \returns ::EpidStatus
  184. \see NewEcGroup
  185. \see NewEcPoint
  186. */
  187. EpidStatus EcSscmExp(EcGroup* g, EcPoint const* a, BigNumStr const* b,
  188. EcPoint* r);
  189. /// Multi-exponentiates elements in elliptic curve group.
  190. /*!
  191. Takes a group elements a[0], ... , a[m-1] in G and positive
  192. integers b[0], ..., b[m-1], where m is a small positive integer.
  193. Outputs r (in G) = EcExp(a[0],b[0]) * ... * EcExp(a[m-1],b[m-1]).
  194. \param[in] g
  195. The elliptic curve group.
  196. \param[in] a
  197. The bases.
  198. \param[in] b
  199. The powers. Power must be less than the order of the elliptic curve
  200. group.
  201. \param[in] m
  202. Number of entries in a and b.
  203. \param[out] r
  204. The result of raising each a to the corresponding power b and multiplying
  205. the results.
  206. \returns ::EpidStatus
  207. \see NewEcGroup
  208. \see NewEcPoint
  209. */
  210. EpidStatus EcMultiExp(EcGroup* g, EcPoint const** a, BigNumStr const** b,
  211. size_t m, EcPoint* r);
  212. /// Multi-exponentiates elements in elliptic curve group.
  213. /*!
  214. Takes a group elements a[0], ... , a[m-1] in G and positive
  215. integers b[0], ..., b[m-1], where m is a small positive integer.
  216. Outputs r (in G) = EcExp(a[0],b[0]) * ... * EcExp(a[m-1],b[m-1]).
  217. \param[in] g
  218. The elliptic curve group.
  219. \param[in] a
  220. The bases.
  221. \param[in] b
  222. The powers. Power must be less than the order of the elliptic curve
  223. group.
  224. \param[in] m
  225. Number of entries in a and b.
  226. \param[out] r
  227. The result of raising each a to the corresponding power b and multiplying
  228. the results.
  229. \returns ::EpidStatus
  230. \see NewEcGroup
  231. \see NewEcPoint
  232. */
  233. EpidStatus EcMultiExpBn(EcGroup* g, EcPoint const** a, BigNum const** b,
  234. size_t m, EcPoint* r);
  235. /// Software side-channel mitigated implementation of EcMultiExp.
  236. /*!
  237. Takes a group elements a[0], ... , a[m-1] in G and positive
  238. integers b[0], ..., b[m-1], where m is a small positive integer.
  239. Outputs r (in G) = EcExp(a[0],b[0]) * ... * EcExp(a[m-1],b[m-1]).
  240. \attention
  241. The reference implementation of EcSscmMultiExp calls EcMultiExp
  242. directly because the implementation of EcMultiExp is already side channel
  243. mitigated. Implementers providing their own versions of this function are
  244. responsible for ensuring that EcSscmMultiExp is side channel mitigated per
  245. section 8 of the Intel(R) EPID 2.0 spec.
  246. \param[in] g
  247. The elliptic curve group.
  248. \param[in] a
  249. The bases.
  250. \param[in] b
  251. The powers. Power must be less than the order of the elliptic curve
  252. group.
  253. \param[in] m
  254. Number of entries in a and b.
  255. \param[out] r
  256. The result of raising each a to the corresponding power b and
  257. multiplying the results.
  258. \returns ::EpidStatus
  259. \see NewEcGroup
  260. \see NewEcPoint
  261. */
  262. EpidStatus EcSscmMultiExp(EcGroup* g, EcPoint const** a, BigNumStr const** b,
  263. size_t m, EcPoint* r);
  264. /// Generates a random element from an elliptic curve group.
  265. /*!
  266. This function is only available for G1 and GT.
  267. \param[in] g
  268. The elliptic curve group.
  269. \param[in] rnd_func
  270. Random number generator.
  271. \param[in] rnd_func_param
  272. Pass through context data for rnd_func.
  273. \param[in,out] r
  274. Output random elliptic curve element.
  275. \returns ::EpidStatus
  276. \see NewEcPoint
  277. \see BitSupplier
  278. */
  279. EpidStatus EcGetRandom(EcGroup* g, BitSupplier rnd_func, void* rnd_func_param,
  280. EcPoint* r);
  281. /// Checks if a point is in an elliptic curve group.
  282. /*!
  283. \param[in] g
  284. The elliptic curve group.
  285. \param[in] p_str
  286. A serialized point. Must be a G1ElemStr or G2ElemStr.
  287. \param[in] strlen
  288. The size of p_str in bytes.
  289. \param[out] in_group
  290. The result of the check.
  291. \returns ::EpidStatus
  292. \see NewEcPoint
  293. */
  294. EpidStatus EcInGroup(EcGroup* g, void const* p_str, size_t strlen,
  295. bool* in_group);
  296. /// Hashes an arbitrary message to an Intel(R) EPID 1.1 element in an elliptic
  297. /// curve group.
  298. /*!
  299. \param[in] g
  300. The elliptic curve group.
  301. \param[in] msg
  302. The message.
  303. \param[in] msg_len
  304. The size of msg in bytes.
  305. \param[out] r
  306. The hashed value.
  307. \returns ::EpidStatus
  308. \see NewEcGroup
  309. \see NewEcPoint
  310. */
  311. EpidStatus Epid11EcHash(EcGroup* g, void const* msg, size_t msg_len,
  312. EcPoint* r);
  313. /// Hashes an arbitrary message to an element in an elliptic curve group.
  314. /*!
  315. \param[in] g
  316. The elliptic curve group.
  317. \param[in] msg
  318. The message.
  319. \param[in] msg_len
  320. The size of msg in bytes.
  321. \param[in] hash_alg
  322. The hash algorithm.
  323. \param[out] r
  324. The hashed value.
  325. \returns ::EpidStatus
  326. \see NewEcGroup
  327. \see NewEcPoint
  328. */
  329. EpidStatus EcHash(EcGroup* g, void const* msg, size_t msg_len, HashAlg hash_alg,
  330. EcPoint* r);
  331. /// Sets an EcPoint variable to a point on a curve.
  332. /*!
  333. This function is only available for G1.
  334. \param[in] g
  335. The elliptic curve group.
  336. \param[in] x
  337. The x coordinate.
  338. \param[out] r
  339. The point.
  340. \returns ::EpidStatus
  341. \see NewEcGroup
  342. \see NewEcPoint
  343. \see NewFfElement
  344. */
  345. EpidStatus EcMakePoint(EcGroup* g, FfElement const* x, EcPoint* r);
  346. /// Computes the additive inverse of an EcPoint.
  347. /*!
  348. This inverse operation is also known as element negation
  349. for elliptic curve groups.
  350. \param[in] g
  351. The elliptic curve group.
  352. \param[in] p
  353. The point.
  354. \param[out] r
  355. The inverted point.
  356. \returns ::EpidStatus
  357. \see NewEcGroup
  358. \see NewEcPoint
  359. */
  360. EpidStatus EcInverse(EcGroup* g, EcPoint const* p, EcPoint* r);
  361. /// Checks if two EcPoints are equal.
  362. /*!
  363. \param[in] g
  364. The elliptic curve group.
  365. \param[in] a
  366. A point to check.
  367. \param[in] b
  368. Another point to check.
  369. \param[out] is_equal
  370. The result of the check.
  371. \returns ::EpidStatus
  372. \see NewEcGroup
  373. \see NewEcPoint
  374. */
  375. EpidStatus EcIsEqual(EcGroup* g, EcPoint const* a, EcPoint const* b,
  376. bool* is_equal);
  377. /// Checks if an EcPoint is the identity element.
  378. /*!
  379. Takes a group element P as input. It outputs true if P is the
  380. identity element of G. Otherwise, it outputs false.
  381. \param[in] g
  382. The elliptic curve group.
  383. \param[in] p
  384. The point to check.
  385. \param[out] is_identity
  386. The result of the check.
  387. \returns ::EpidStatus
  388. \see NewEcGroup
  389. \see NewEcPoint
  390. */
  391. EpidStatus EcIsIdentity(EcGroup* g, EcPoint const* p, bool* is_identity);
  392. /*!
  393. @}
  394. */
  395. #endif // EPID_COMMON_MATH_ECGROUP_H_