ecgroup.h 12 KB

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