ecdh.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. Crypto using elliptic curves defined over the finite binary field GF(2^m) where m is prime.
  3. The curves used are the anomalous binary curves (ABC-curves) or also called Koblitz curves.
  4. This class of curves was chosen because it yields efficient implementation of operations.
  5. Curves available - their different NIST/SECG names and eqivalent symmetric security level:
  6. NIST SEC Group strength
  7. ------------------------------------
  8. K-163 sect163k1 80 bit
  9. B-163 sect163r2 80 bit
  10. K-233 sect233k1 112 bit
  11. B-233 sect233r1 112 bit
  12. K-283 sect283k1 128 bit
  13. B-283 sect283r1 128 bit
  14. K-409 sect409k1 192 bit
  15. B-409 sect409r1 192 bit
  16. K-571 sect571k1 256 bit
  17. B-571 sect571r1 256 bit
  18. Curve parameters from:
  19. http://www.secg.org/sec2-v2.pdf
  20. http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
  21. Reference:
  22. https://www.ietf.org/rfc/rfc4492.txt
  23. */
  24. #include <stdint.h>
  25. #include "cipher/ecdh.h"
  26. /* margin for overhead needed in intermediate calculations */
  27. #define BITVEC_MARGIN 3
  28. #define BITVEC_NBITS (CURVE_DEGREE + BITVEC_MARGIN)
  29. #define BITVEC_NWORDS ((BITVEC_NBITS + 31) / 32)
  30. #define BITVEC_NBYTES (sizeof(uint32_t) * BITVEC_NWORDS)
  31. /* Disable assertions? */
  32. #ifndef DISABLE_ASSERT
  33. #define DISABLE_ASSERT 0
  34. #endif
  35. #if defined(DISABLE_ASSERT) && (DISABLE_ASSERT == 1)
  36. #define assert(...)
  37. #else
  38. #include <assert.h>
  39. #endif
  40. /* Default to a (somewhat) constant-time mode?
  41. NOTE: The library is _not_ capable of operating in constant-time and leaks information via timing.
  42. Even if all operations are written const-time-style, it requires the hardware is able to multiply in constant time.
  43. Multiplication on ARM Cortex-M processors takes a variable number of cycles depending on the operands...
  44. */
  45. #ifndef CONST_TIME
  46. #define CONST_TIME 0
  47. #endif
  48. /* Default to using ECC_CDH (cofactor multiplication-variation) ? */
  49. #ifndef ECDH_COFACTOR_VARIANT
  50. #define ECDH_COFACTOR_VARIANT 0
  51. #endif
  52. /******************************************************************************/
  53. /* the following type will represent bit vectors of length (CURVE_DEGREE+MARGIN) */
  54. typedef uint32_t bitvec_t[BITVEC_NWORDS];
  55. typedef bitvec_t gf2elem_t; /* this type will represent field elements */
  56. typedef bitvec_t scalar_t;
  57. /******************************************************************************/
  58. /* Here the curve parameters are defined. */
  59. #if defined (ECC_CURVE) && (ECC_CURVE != 0)
  60. #if (ECC_CURVE == NIST_K163)
  61. #define coeff_a 1
  62. #define cofactor 2
  63. /* NIST K-163 */
  64. const gf2elem_t polynomial = { 0x000000c9, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000008 };
  65. const gf2elem_t coeff_b = { 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
  66. const gf2elem_t base_x = { 0x5c94eee8, 0xde4e6d5e, 0xaa07d793, 0x7bbc11ac, 0xfe13c053, 0x00000002 };
  67. const gf2elem_t base_y = { 0xccdaa3d9, 0x0536d538, 0x321f2e80, 0x5d38ff58, 0x89070fb0, 0x00000002 };
  68. const scalar_t base_order = { 0x99f8a5ef, 0xa2e0cc0d, 0x00020108, 0x00000000, 0x00000000, 0x00000004 };
  69. #endif
  70. #if (ECC_CURVE == NIST_B163)
  71. #define coeff_a 1
  72. #define cofactor 2
  73. /* NIST B-163 */
  74. const gf2elem_t polynomial = { 0x000000c9, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000008 };
  75. const gf2elem_t coeff_b = { 0x4a3205fd, 0x512f7874, 0x1481eb10, 0xb8c953ca, 0x0a601907, 0x00000002 };
  76. const gf2elem_t base_x = { 0xe8343e36, 0xd4994637, 0xa0991168, 0x86a2d57e, 0xf0eba162, 0x00000003 };
  77. const gf2elem_t base_y = { 0x797324f1, 0xb11c5c0c, 0xa2cdd545, 0x71a0094f, 0xd51fbc6c, 0x00000000 };
  78. const scalar_t base_order = { 0xa4234c33, 0x77e70c12, 0x000292fe, 0x00000000, 0x00000000, 0x00000004 };
  79. #endif
  80. #if (ECC_CURVE == NIST_K233)
  81. #define coeff_a 0
  82. #define cofactor 4
  83. /* NIST K-233 */
  84. const gf2elem_t polynomial = { 0x00000001, 0x00000000, 0x00000400, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000200 };
  85. const gf2elem_t coeff_b = { 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
  86. const gf2elem_t base_x = { 0xefad6126, 0x0a4c9d6e, 0x19c26bf5, 0x149563a4, 0x29f22ff4, 0x7e731af1, 0x32ba853a, 0x00000172 };
  87. const gf2elem_t base_y = { 0x56fae6a3, 0x56e0c110, 0xf18aeb9b, 0x27a8cd9b, 0x555a67c4, 0x19b7f70f, 0x537dece8, 0x000001db };
  88. const scalar_t base_order = { 0xf173abdf, 0x6efb1ad5, 0xb915bcd4, 0x00069d5b, 0x00000000, 0x00000000, 0x00000000, 0x00000080 };
  89. #endif
  90. #if (ECC_CURVE == NIST_B233)
  91. #define coeff_a 1
  92. #define cofactor 2
  93. /* NIST B-233 */
  94. const gf2elem_t polynomial = { 0x00000001, 0x00000000, 0x00000400, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000200 };
  95. const gf2elem_t coeff_b = { 0x7d8f90ad, 0x81fe115f, 0x20e9ce42, 0x213b333b, 0x0923bb58, 0x332c7f8c, 0x647ede6c, 0x00000066 };
  96. const gf2elem_t base_x = { 0x71fd558b, 0xf8f8eb73, 0x391f8b36, 0x5fef65bc, 0x39f1bb75, 0x8313bb21, 0xc9dfcbac, 0x000000fa };
  97. const gf2elem_t base_y = { 0x01f81052, 0x36716f7e, 0xf867a7ca, 0xbf8a0bef, 0xe58528be, 0x03350678, 0x6a08a419, 0x00000100 };
  98. const scalar_t base_order = { 0x03cfe0d7, 0x22031d26, 0xe72f8a69, 0x0013e974, 0x00000000, 0x00000000, 0x00000000, 0x00000100 };
  99. #endif
  100. #if (ECC_CURVE == NIST_K283)
  101. #define coeff_a 0
  102. #define cofactor 4
  103. /* NIST K-283 */
  104. const gf2elem_t polynomial = { 0x000010a1, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x08000000 };
  105. const gf2elem_t coeff_b = { 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
  106. const gf2elem_t base_x = { 0x58492836, 0xb0c2ac24, 0x16876913, 0x23c1567a, 0x53cd265f, 0x62f188e5, 0x3f1a3b81, 0x78ca4488, 0x0503213f };
  107. const gf2elem_t base_y = { 0x77dd2259, 0x4e341161, 0xe4596236, 0xe8184698, 0xe87e45c0, 0x07e5426f, 0x8d90f95d, 0x0f1c9e31, 0x01ccda38 };
  108. const scalar_t base_order = { 0x1e163c61, 0x94451e06, 0x265dff7f, 0x2ed07577, 0xffffe9ae, 0xffffffff, 0xffffffff, 0xffffffff, 0x01ffffff };
  109. #endif
  110. #if (ECC_CURVE == NIST_B283)
  111. #define coeff_a 1
  112. #define cofactor 2
  113. /* NIST B-283 */
  114. const gf2elem_t polynomial = { 0x000010a1, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x08000000 };
  115. const gf2elem_t coeff_b = { 0x3b79a2f5, 0xf6263e31, 0xa581485a, 0x45309fa2, 0xca97fd76, 0x19a0303f, 0xa5a4af8a, 0xc8b8596d, 0x027b680a };
  116. const gf2elem_t base_x = { 0x86b12053, 0xf8cdbecd, 0x80e2e198, 0x557eac9c, 0x2eed25b8, 0x70b0dfec, 0xe1934f8c, 0x8db7dd90, 0x05f93925 };
  117. const gf2elem_t base_y = { 0xbe8112f4, 0x13f0df45, 0x826779c8, 0x350eddb0, 0x516ff702, 0xb20d02b4, 0xb98fe6d4, 0xfe24141c, 0x03676854 };
  118. const scalar_t base_order = { 0xefadb307, 0x5b042a7c, 0x938a9016, 0x399660fc, 0xffffef90, 0xffffffff, 0xffffffff, 0xffffffff, 0x03ffffff };
  119. #endif
  120. #if (ECC_CURVE == NIST_K409)
  121. #define coeff_a 0
  122. #define cofactor 4
  123. /* NIST K-409 */
  124. const gf2elem_t polynomial = { 0x00000001, 0x00000000, 0x00800000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x02000000 };
  125. const gf2elem_t coeff_b = { 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
  126. const gf2elem_t base_x = { 0xe9023746, 0xb35540cf, 0xee222eb1, 0xb5aaaa62, 0xc460189e, 0xf9f67cc2, 0x27accfb8, 0xe307c84c, 0x0efd0987, 0x0f718421, 0xad3ab189, 0x658f49c1, 0x0060f05f };
  127. const gf2elem_t base_y = { 0xd8e0286b, 0x5863ec48, 0xaa9ca27a, 0xe9c55215, 0xda5f6c42, 0xe9ea10e3, 0xe6325165, 0x918ea427, 0x3460782f, 0xbf04299c, 0xacba1dac, 0x0b7c4e42, 0x01e36905 };
  128. const scalar_t base_order = { 0xe01e5fcf, 0x4b5c83b8, 0xe3e7ca5b, 0x557d5ed3, 0x20400ec4, 0x83b2d4ea, 0xfffffe5f, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x007fffff };
  129. #endif
  130. #if (ECC_CURVE == NIST_B409)
  131. #define coeff_a 1
  132. #define cofactor 2
  133. /* NIST B-409 */
  134. const gf2elem_t polynomial = { 0x00000001, 0x00000000, 0x00800000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x02000000 };
  135. const gf2elem_t coeff_b = { 0x7b13545f, 0x4f50ae31, 0xd57a55aa, 0x72822f6c, 0xa9a197b2, 0xd6ac27c8, 0x4761fa99, 0xf1f3dd67, 0x7fd6422e, 0x3b7b476b, 0x5c4b9a75, 0xc8ee9feb, 0x0021a5c2 };
  136. const gf2elem_t base_x = { 0xbb7996a7, 0x60794e54, 0x5603aeab, 0x8a118051, 0xdc255a86, 0x34e59703, 0xb01ffe5b, 0xf1771d4d, 0x441cde4a, 0x64756260, 0x496b0c60, 0xd088ddb3, 0x015d4860 };
  137. const gf2elem_t base_y = { 0x0273c706, 0x81c364ba, 0xd2181b36, 0xdf4b4f40, 0x38514f1f, 0x5488d08f, 0x0158aa4f, 0xa7bd198d, 0x7636b9c5, 0x24ed106a, 0x2bbfa783, 0xab6be5f3, 0x0061b1cf };
  138. const scalar_t base_order = { 0xd9a21173, 0x8164cd37, 0x9e052f83, 0x5fa47c3c, 0xf33307be, 0xaad6a612, 0x000001e2, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x01000000 };
  139. #endif
  140. #if (ECC_CURVE == NIST_K571)
  141. #define coeff_a 0
  142. #define cofactor 4
  143. /* NIST K-571 */
  144. const gf2elem_t polynomial = { 0x00000425, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x08000000 };
  145. const gf2elem_t coeff_b = { 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
  146. const gf2elem_t base_x = { 0xa01c8972, 0xe2945283, 0x4dca88c7, 0x988b4717, 0x494776fb, 0xbbd1ba39, 0xb4ceb08c, 0x47da304d, 0x93b205e6, 0x43709584, 0x01841ca4, 0x60248048, 0x0012d5d4, 0xac9ca297, 0xf8103fe4, 0x82189631, 0x59923fbc, 0x026eb7a8 };
  147. const gf2elem_t base_y = { 0x3ef1c7a3, 0x01cd4c14, 0x591984f6, 0x320430c8, 0x7ba7af1b, 0xb620b01a, 0xf772aedc, 0x4fbebbb9, 0xac44aea7, 0x9d4979c0, 0x006d8a2c, 0xffc61efc, 0x9f307a54, 0x4dd58cec, 0x3bca9531, 0x4f4aeade, 0x7f4fbf37, 0x0349dc80 };
  148. const scalar_t base_order = { 0x637c1001, 0x5cfe778f, 0x1e91deb4, 0xe5d63938, 0xb630d84b, 0x917f4138, 0xb391a8db, 0xf19a63e4, 0x131850e1, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x02000000 };
  149. #endif
  150. #if (ECC_CURVE == NIST_B571)
  151. #define coeff_a 1
  152. #define cofactor 2
  153. /* NIST B-571 */
  154. const gf2elem_t polynomial = { 0x00000425, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x08000000 };
  155. const gf2elem_t coeff_b = { 0x2955727a, 0x7ffeff7f, 0x39baca0c, 0x520e4de7, 0x78ff12aa, 0x4afd185a, 0x56a66e29, 0x2be7ad67, 0x8efa5933, 0x84ffabbd, 0x4a9a18ad, 0xcd6ba8ce, 0xcb8ceff1, 0x5c6a97ff, 0xb7f3d62f, 0xde297117, 0x2221f295, 0x02f40e7e };
  156. const gf2elem_t base_x = { 0x8eec2d19, 0xe1e7769c, 0xc850d927, 0x4abfa3b4, 0x8614f139, 0x99ae6003, 0x5b67fb14, 0xcdd711a3, 0xf4c0d293, 0xbde53950, 0xdb7b2abd, 0xa5f40fc8, 0x955fa80a, 0x0a93d1d2, 0x0d3cd775, 0x6c16c0d4, 0x34b85629, 0x0303001d };
  157. const gf2elem_t base_y = { 0x1b8ac15b, 0x1a4827af, 0x6e23dd3c, 0x16e2f151, 0x0485c19b, 0xb3531d2f, 0x461bb2a8, 0x6291af8f, 0xbab08a57, 0x84423e43, 0x3921e8a6, 0x1980f853, 0x009cbbca, 0x8c6c27a6, 0xb73d69d7, 0x6dccfffe, 0x42da639b, 0x037bf273 };
  158. const scalar_t base_order = { 0x2fe84e47, 0x8382e9bb, 0x5174d66e, 0x161de93d, 0xc7dd9ca1, 0x6823851e, 0x08059b18, 0xff559873, 0xe661ce18, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x03ffffff };
  159. #endif
  160. #endif
  161. /*************************************************************************************************/
  162. /* Private / static functions: */
  163. /* some basic bit-manipulation routines that act on bit-vectors follow */
  164. static int bitvec_get_bit(const bitvec_t x, const uint32_t idx)
  165. {
  166. return ((x[idx / 32U] >> (idx & 31U) & 1U));
  167. }
  168. static void bitvec_clr_bit(bitvec_t x, const uint32_t idx)
  169. {
  170. x[idx / 32U] &= ~(1U << (idx & 31U));
  171. }
  172. static void bitvec_copy(bitvec_t x, const bitvec_t y)
  173. {
  174. int i;
  175. for (i = 0; i < BITVEC_NWORDS; ++i)
  176. {
  177. x[i] = y[i];
  178. }
  179. }
  180. static void bitvec_swap(bitvec_t x, bitvec_t y)
  181. {
  182. bitvec_t tmp;
  183. bitvec_copy(tmp, x);
  184. bitvec_copy(x, y);
  185. bitvec_copy(y, tmp);
  186. }
  187. #if defined(CONST_TIME) && (CONST_TIME == 0)
  188. /* fast version of equality test */
  189. static int bitvec_equal(const bitvec_t x, const bitvec_t y)
  190. {
  191. int i;
  192. for (i = 0; i < BITVEC_NWORDS; ++i)
  193. {
  194. if (x[i] != y[i])
  195. {
  196. return 0;
  197. }
  198. }
  199. return 1;
  200. }
  201. #else
  202. /* constant time version of equality test */
  203. static int bitvec_equal(const bitvec_t x, const bitvec_t y)
  204. {
  205. int ret = 1;
  206. int i;
  207. for (i = 0; i < BITVEC_NWORDS; ++i)
  208. {
  209. ret &= (x[i] == y[i]);
  210. }
  211. return ret;
  212. }
  213. #endif
  214. static void bitvec_set_zero(bitvec_t x)
  215. {
  216. int i;
  217. for (i = 0; i < BITVEC_NWORDS; ++i)
  218. {
  219. x[i] = 0;
  220. }
  221. }
  222. #if defined(CONST_TIME) && (CONST_TIME == 0)
  223. /* fast implementation */
  224. static int bitvec_is_zero(const bitvec_t x)
  225. {
  226. uint32_t i = 0;
  227. while (i < BITVEC_NWORDS)
  228. {
  229. if (x[i] != 0)
  230. {
  231. break;
  232. }
  233. i += 1;
  234. }
  235. return (i == BITVEC_NWORDS);
  236. }
  237. #else
  238. /* constant-time implementation */
  239. static int bitvec_is_zero(const bitvec_t x)
  240. {
  241. int ret = 1;
  242. int i = 0;
  243. for (i = 0; i < BITVEC_NWORDS; ++i)
  244. {
  245. ret &= (x[i] == 0);
  246. }
  247. return ret;
  248. }
  249. #endif
  250. /* return the number of the highest one-bit + 1 */
  251. static int bitvec_degree(const bitvec_t x)
  252. {
  253. int i = BITVEC_NWORDS * 32;
  254. /* Start at the back of the vector (MSB) */
  255. x += BITVEC_NWORDS;
  256. /* Skip empty / zero words */
  257. while ( (i > 0)
  258. && (*(--x)) == 0)
  259. {
  260. i -= 32;
  261. }
  262. /* Run through rest if count is not multiple of bitsize of DTYPE */
  263. if (i != 0)
  264. {
  265. uint32_t u32mask = ((uint32_t)1 << 31);
  266. while (((*x) & u32mask) == 0)
  267. {
  268. u32mask >>= 1;
  269. i -= 1;
  270. }
  271. }
  272. return i;
  273. }
  274. /* left-shift by 'count' digits */
  275. static void bitvec_lshift(bitvec_t x, const bitvec_t y, int nbits)
  276. {
  277. int nwords = (nbits / 32);
  278. /* Shift whole words first if nwords > 0 */
  279. int i,j;
  280. for (i = 0; i < nwords; ++i)
  281. {
  282. /* Zero-initialize from least-significant word until offset reached */
  283. x[i] = 0;
  284. }
  285. j = 0;
  286. /* Copy to x output */
  287. while (i < BITVEC_NWORDS)
  288. {
  289. x[i] = y[j];
  290. i += 1;
  291. j += 1;
  292. }
  293. /* Shift the rest if count was not multiple of bitsize of DTYPE */
  294. nbits &= 31;
  295. if (nbits != 0)
  296. {
  297. /* Left shift rest */
  298. int i;
  299. for (i = (BITVEC_NWORDS - 1); i > 0; --i)
  300. {
  301. x[i] = (x[i] << nbits) | (x[i - 1] >> (32 - nbits));
  302. }
  303. x[0] <<= nbits;
  304. }
  305. }
  306. /*************************************************************************************************/
  307. /*
  308. Code that does arithmetic on bit-vectors in the Galois Field GF(2^CURVE_DEGREE).
  309. */
  310. /*************************************************************************************************/
  311. static void gf2field_set_one(gf2elem_t x)
  312. {
  313. /* Set first word to one */
  314. x[0] = 1;
  315. /* .. and the rest to zero */
  316. int i;
  317. for (i = 1; i < BITVEC_NWORDS; ++i)
  318. {
  319. x[i] = 0;
  320. }
  321. }
  322. #if defined(CONST_TIME) && (CONST_TIME == 0)
  323. /* fastest check if x == 1 */
  324. static int gf2field_is_one(const gf2elem_t x)
  325. {
  326. /* Check if first word == 1 */
  327. if (x[0] != 1)
  328. {
  329. return 0;
  330. }
  331. /* ...and if rest of words == 0 */
  332. int i;
  333. for (i = 1; i < BITVEC_NWORDS; ++i)
  334. {
  335. if (x[i] != 0)
  336. {
  337. break;
  338. }
  339. }
  340. return (i == BITVEC_NWORDS);
  341. }
  342. #else
  343. /* constant-time check */
  344. static int gf2field_is_one(const gf2elem_t x)
  345. {
  346. int ret = 0;
  347. /* Check if first word == 1 */
  348. if (x[0] == 1)
  349. {
  350. ret = 1;
  351. }
  352. /* ...and if rest of words == 0 */
  353. int i;
  354. for (i = 1; i < BITVEC_NWORDS; ++i)
  355. {
  356. ret &= (x[i] == 0);
  357. }
  358. return ret; //(i == BITVEC_NWORDS);
  359. }
  360. #endif
  361. /* galois field(2^m) addition is modulo 2, so XOR is used instead - 'z := a + b' */
  362. static void gf2field_add(gf2elem_t z, const gf2elem_t x, const gf2elem_t y)
  363. {
  364. int i;
  365. for (i = 0; i < BITVEC_NWORDS; ++i)
  366. {
  367. z[i] = (x[i] ^ y[i]);
  368. }
  369. }
  370. /* increment element */
  371. static void gf2field_inc(gf2elem_t x)
  372. {
  373. x[0] ^= 1;
  374. }
  375. /* field multiplication 'z := (x * y)' */
  376. static void gf2field_mul(gf2elem_t z, const gf2elem_t x, const gf2elem_t y)
  377. {
  378. int i;
  379. gf2elem_t tmp;
  380. #if defined(CONST_TIME) && (CONST_TIME == 1)
  381. gf2elem_t blind;
  382. bitvec_set_zero(blind);
  383. #endif
  384. assert(z != y);
  385. bitvec_copy(tmp, x);
  386. /* LSB set? Then start with x */
  387. if (bitvec_get_bit(y, 0) != 0)
  388. {
  389. bitvec_copy(z, x);
  390. }
  391. else /* .. or else start with zero */
  392. {
  393. bitvec_set_zero(z);
  394. }
  395. /* Then add 2^i * x for the rest */
  396. for (i = 1; i < CURVE_DEGREE; ++i)
  397. {
  398. /* lshift 1 - doubling the value of tmp */
  399. bitvec_lshift(tmp, tmp, 1);
  400. /* Modulo reduction polynomial if degree(tmp) > CURVE_DEGREE */
  401. if (bitvec_get_bit(tmp, CURVE_DEGREE))
  402. {
  403. gf2field_add(tmp, tmp, polynomial);
  404. }
  405. #if defined(CONST_TIME) && (CONST_TIME == 1)
  406. else /* blinding operation */
  407. {
  408. gf2field_add(tmp, tmp, blind);
  409. }
  410. #endif
  411. /* Add 2^i * tmp if this factor in y is non-zero */
  412. if (bitvec_get_bit(y, i))
  413. {
  414. gf2field_add(z, z, tmp);
  415. }
  416. #if defined(CONST_TIME) && (CONST_TIME == 1)
  417. else /* blinding operation */
  418. {
  419. gf2field_add(z, z, blind);
  420. }
  421. #endif
  422. }
  423. }
  424. /* field inversion 'z := 1/x' */
  425. static void gf2field_inv(gf2elem_t z, const gf2elem_t x)
  426. {
  427. gf2elem_t u, v, g, h;
  428. int i;
  429. bitvec_copy(u, x);
  430. bitvec_copy(v, polynomial);
  431. bitvec_set_zero(g);
  432. gf2field_set_one(z);
  433. while (!gf2field_is_one(u))
  434. {
  435. i = (bitvec_degree(u) - bitvec_degree(v));
  436. if (i < 0)
  437. {
  438. bitvec_swap(u, v);
  439. bitvec_swap(g, z);
  440. i = -i;
  441. }
  442. #if defined(CONST_TIME) && (CONST_TIME == 1)
  443. else
  444. {
  445. bitvec_swap(u, v);
  446. bitvec_swap(v, u);
  447. }
  448. #endif
  449. bitvec_lshift(h, v, i);
  450. gf2field_add(u, u, h);
  451. bitvec_lshift(h, g, i);
  452. gf2field_add(z, z, h);
  453. }
  454. }
  455. /*************************************************************************************************/
  456. /*
  457. The following code takes care of Galois-Field arithmetic.
  458. Elliptic curve points are represented by pairs (x,y) of bitvec_t.
  459. It is assumed that curve coefficient 'a' is {0,1}
  460. This is the case for all NIST binary curves.
  461. Coefficient 'b' is given in 'coeff_b'.
  462. '(base_x, base_y)' is a point that generates a large prime order group.
  463. */
  464. /*************************************************************************************************/
  465. static void gf2point_copy(gf2elem_t x1, gf2elem_t y1, const gf2elem_t x2, const gf2elem_t y2)
  466. {
  467. bitvec_copy(x1, x2);
  468. bitvec_copy(y1, y2);
  469. }
  470. static void gf2point_set_zero(gf2elem_t x, gf2elem_t y)
  471. {
  472. bitvec_set_zero(x);
  473. bitvec_set_zero(y);
  474. }
  475. static int gf2point_is_zero(const gf2elem_t x, const gf2elem_t y)
  476. {
  477. return ( bitvec_is_zero(x)
  478. && bitvec_is_zero(y));
  479. }
  480. /* double the point (x,y) */
  481. static void gf2point_double(gf2elem_t x, gf2elem_t y)
  482. {
  483. /* iff P = O (zero or infinity): 2 * P = P */
  484. if (bitvec_is_zero(x))
  485. {
  486. bitvec_set_zero(y);
  487. }
  488. else
  489. {
  490. gf2elem_t l;
  491. gf2field_inv(l, x);
  492. gf2field_mul(l, l, y);
  493. gf2field_add(l, l, x);
  494. gf2field_mul(y, x, x);
  495. gf2field_mul(x, l, l);
  496. #if (coeff_a == 1)
  497. gf2field_inc(l);
  498. #endif
  499. gf2field_add(x, x, l);
  500. gf2field_mul(l, l, x);
  501. gf2field_add(y, y, l);
  502. }
  503. }
  504. /* add two points together (x1, y1) := (x1, y1) + (x2, y2) */
  505. static void gf2point_add(gf2elem_t x1, gf2elem_t y1, const gf2elem_t x2, const gf2elem_t y2)
  506. {
  507. if (!gf2point_is_zero(x2, y2))
  508. {
  509. if (gf2point_is_zero(x1, y1))
  510. {
  511. gf2point_copy(x1, y1, x2, y2);
  512. }
  513. else
  514. {
  515. if (bitvec_equal(x1, x2))
  516. {
  517. if (bitvec_equal(y1, y2))
  518. {
  519. gf2point_double(x1, y1);
  520. }
  521. else
  522. {
  523. gf2point_set_zero(x1, y1);
  524. }
  525. }
  526. else
  527. {
  528. /* Arithmetic with temporary variables */
  529. gf2elem_t a, b, c, d;
  530. gf2field_add(a, y1, y2);
  531. gf2field_add(b, x1, x2);
  532. gf2field_inv(c, b);
  533. gf2field_mul(c, c, a);
  534. gf2field_mul(d, c, c);
  535. gf2field_add(d, d, c);
  536. gf2field_add(d, d, b);
  537. #if (coeff_a == 1)
  538. gf2field_inc(d);
  539. #endif
  540. gf2field_add(x1, x1, d);
  541. gf2field_mul(a, x1, c);
  542. gf2field_add(a, a, d);
  543. gf2field_add(y1, y1, a);
  544. bitvec_copy(x1, d);
  545. }
  546. }
  547. }
  548. }
  549. #if defined(CONST_TIME) && (CONST_TIME == 0)
  550. /* point multiplication via double-and-add algorithm */
  551. static void gf2point_mul(gf2elem_t x, gf2elem_t y, const scalar_t exp)
  552. {
  553. gf2elem_t tmpx, tmpy;
  554. int i;
  555. int nbits = bitvec_degree(exp);
  556. gf2point_set_zero(tmpx, tmpy);
  557. for (i = (nbits - 1); i >= 0; --i)
  558. {
  559. gf2point_double(tmpx, tmpy);
  560. if (bitvec_get_bit(exp, i))
  561. {
  562. gf2point_add(tmpx, tmpy, x, y);
  563. }
  564. }
  565. gf2point_copy(x, y, tmpx, tmpy);
  566. }
  567. #else
  568. /* point multiplication via double-and-add-always algorithm using scalar blinding */
  569. static void gf2point_mul(gf2elem_t x, gf2elem_t y, const scalar_t exp)
  570. {
  571. gf2elem_t tmpx, tmpy;
  572. gf2elem_t dummyx, dummyy;
  573. int i;
  574. int nbits = bitvec_degree(exp);
  575. gf2point_set_zero(tmpx, tmpy);
  576. gf2point_set_zero(dummyx, dummyy);
  577. for (i = (nbits - 1); i >= 0; --i)
  578. {
  579. gf2point_double(tmpx, tmpy);
  580. /* Add point if bit(i) is set in exp */
  581. if (bitvec_get_bit(exp, i))
  582. {
  583. gf2point_add(tmpx, tmpy, x, y);
  584. }
  585. /* .. or add the neutral element to keep operation constant-time */
  586. else
  587. {
  588. gf2point_add(tmpx, tmpy, dummyx, dummyy);
  589. }
  590. }
  591. gf2point_copy(x, y, tmpx, tmpy);
  592. }
  593. #endif
  594. /* check if y^2 + x*y = x^3 + a*x^2 + coeff_b holds */
  595. static int gf2point_on_curve(const gf2elem_t x, const gf2elem_t y)
  596. {
  597. gf2elem_t a, b;
  598. if (gf2point_is_zero(x, y))
  599. {
  600. return 1;
  601. }
  602. else
  603. {
  604. gf2field_mul(a, x, x);
  605. #if (coeff_a == 0)
  606. gf2field_mul(a, a, x);
  607. #else
  608. gf2field_mul(b, a, x);
  609. gf2field_add(a, a, b);
  610. #endif
  611. gf2field_add(a, a, coeff_b);
  612. gf2field_mul(b, y, y);
  613. gf2field_add(a, a, b);
  614. gf2field_mul(b, x, y);
  615. return bitvec_equal(a, b);
  616. }
  617. }
  618. /*************************************************************************************************/
  619. /*
  620. Elliptic Curve Diffie-Hellman key exchange protocol.
  621. */
  622. /*************************************************************************************************/
  623. /* NOTE: private should contain random data a-priori! */
  624. int ecdh_generate_keys(uint8_t* public_key, uint8_t* private_key)
  625. {
  626. /* Get copy of "base" point 'G' */
  627. gf2point_copy((uint32_t*)public_key, (uint32_t*)(public_key + BITVEC_NBYTES), base_x, base_y);
  628. /* Abort key generation if random number is too small */
  629. if (bitvec_degree((uint32_t*)private_key) < (CURVE_DEGREE / 2))
  630. {
  631. return 0;
  632. }
  633. else
  634. {
  635. /* Clear bits > CURVE_DEGREE in highest word to satisfy constraint 1 <= exp < n. */
  636. int nbits = bitvec_degree(base_order);
  637. int i;
  638. for (i = (nbits - 1); i < (BITVEC_NWORDS * 32); ++i)
  639. {
  640. bitvec_clr_bit((uint32_t*)private_key, i);
  641. }
  642. /* Multiply base-point with scalar (private-key) */
  643. gf2point_mul((uint32_t*)public_key, (uint32_t*)(public_key + BITVEC_NBYTES), (uint32_t*)private_key);
  644. return 1;
  645. }
  646. }
  647. int ecdh_shared_secret(const uint8_t* private_key, const uint8_t* others_pub, uint8_t* output)
  648. {
  649. /* Do some basic validation of other party's public key */
  650. if ( !gf2point_is_zero ((uint32_t*)others_pub, (uint32_t*)(others_pub + BITVEC_NBYTES))
  651. && gf2point_on_curve((uint32_t*)others_pub, (uint32_t*)(others_pub + BITVEC_NBYTES)) )
  652. {
  653. /* Copy other side's public key to output */
  654. unsigned int i;
  655. for (i = 0; i < (BITVEC_NBYTES * 2); ++i)
  656. {
  657. output[i] = others_pub[i];
  658. }
  659. /* Multiply other side's public key with own private key */
  660. gf2point_mul((uint32_t*)output,(uint32_t*)(output + BITVEC_NBYTES), (const uint32_t*)private_key);
  661. /* Multiply outcome by cofactor if using ECC CDH-variant: */
  662. #if defined(ECDH_COFACTOR_VARIANT) && (ECDH_COFACTOR_VARIANT == 1)
  663. #if (cofactor == 2)
  664. gf2point_double((uint32_t*)output, (uint32_t*)(output + BITVEC_NBYTES));
  665. #elif (cofactor == 4)
  666. gf2point_double((uint32_t*)output, (uint32_t*)(output + BITVEC_NBYTES));
  667. gf2point_double((uint32_t*)output, (uint32_t*)(output + BITVEC_NBYTES));
  668. #endif
  669. #endif
  670. return 1;
  671. }
  672. else
  673. {
  674. return 0;
  675. }
  676. }
  677. /* ECDSA is broken :( ... */
  678. int ecdsa_sign(const uint8_t* private_key, uint8_t* hash, uint8_t* random_k, uint8_t* signature)
  679. {
  680. /*
  681. 1) calculate e = HASH(m)
  682. 2) let z be the Ln leftmost bits of e, where Ln is the bit length of the group order n
  683. 3) Select a cryptographically secure random integer k from [1, n-1]
  684. 4) Calculate the curve point (x1, y1) = k * G
  685. 5) Calculate r = x1 mod n - if (r == 0) goto 3
  686. 6) Calculate s = inv(k) * (z + r * d) mod n - if (s == 0) goto 3
  687. 7) The signature is the pair (r, s)
  688. */
  689. assert(private_key != 0);
  690. assert(hash != 0);
  691. assert(random_k != 0);
  692. assert(signature != 0);
  693. int success = 0;
  694. if ( (bitvec_degree((uint32_t*)private_key) >= (CURVE_DEGREE / 2))
  695. && !bitvec_is_zero((uint32_t*)random_k) )
  696. {
  697. gf2elem_t r, s, z, k;
  698. bitvec_set_zero(r);
  699. bitvec_set_zero(s);
  700. bitvec_copy(z, (uint32_t*)hash);
  701. /* 1 + 2 */
  702. int nbits = bitvec_degree(base_order);
  703. int i;
  704. for (i = (nbits - 1); i < BITVEC_NBITS; ++i)
  705. {
  706. bitvec_clr_bit(z, i);
  707. }
  708. /* 3 */
  709. bitvec_copy(k, (uint32_t*)random_k);
  710. /* 4 */
  711. gf2point_copy(r, s, base_x, base_y);
  712. gf2point_mul(r, s, k);
  713. /* 5 */
  714. if (!bitvec_is_zero(r))
  715. {
  716. /* 6) s = inv(k) * (z + (r * d)) mod n ==> if (s == 0) goto 3 **/
  717. gf2field_inv(s, k); /* s = inv(k) */
  718. gf2field_mul(r, r, (uint32_t*)private_key); /* r = (r * d) */
  719. gf2field_add(r, r, z); /* r = z + (r * d) */
  720. nbits = bitvec_degree(r); /* r = r mod n */
  721. for (i = (nbits - 1); i < BITVEC_NBITS; ++i)
  722. {
  723. // printf("reduction r\n");
  724. bitvec_clr_bit(r, i);
  725. }
  726. gf2field_mul(s, s, r); /* s = inv(k) * (z * (r * d)) */
  727. nbits = bitvec_degree(s); /* s = s mod n */
  728. for (i = (nbits - 1); i < BITVEC_NBITS; ++i)
  729. {
  730. // printf("reduction s\n");
  731. bitvec_clr_bit(s, i);
  732. }
  733. if (!bitvec_is_zero(s))
  734. {
  735. bitvec_copy((uint32_t*)signature, r);
  736. bitvec_copy((uint32_t*)(signature + ECC_PRV_KEY_SIZE), s);
  737. success = 1;
  738. }
  739. }
  740. }
  741. return success;
  742. }
  743. int ecdsa_verify(const uint8_t* public_key, uint8_t* hash, const uint8_t* signature)
  744. {
  745. /*
  746. 1) Verify that (r,s) are in [1, n-1]
  747. 2) e = HASH(m)
  748. 3) z = Ln leftmost bits of e
  749. 4) w = inv(s) mod n
  750. 5) u1 = (z * w) mod n
  751. u2 = (r * w) mod n
  752. 6) (x,y) = (u1 * G) + (u2 * public)
  753. 7) Signature is valid if r == x mod n && (x,y) != (0,0)
  754. */
  755. assert(public_key != 0);
  756. assert(hash != 0);
  757. assert(signature != 0);
  758. int success = 0;
  759. gf2elem_t r, s;
  760. bitvec_copy(r, (uint32_t*)(signature));
  761. bitvec_copy(s, (uint32_t*)(signature + ECC_PRV_KEY_SIZE));
  762. if ( !bitvec_is_zero(s)
  763. && !bitvec_is_zero(r))
  764. {
  765. gf2elem_t x1, y1, u1, u2, w, z;
  766. /* 3) z = Ln leftmost bits of e */
  767. bitvec_copy(z, (uint32_t*)hash); /* r,s,z are set */
  768. uint32_t nbits = bitvec_degree(base_order);
  769. uint32_t i;
  770. for (i = (nbits - 1); i < BITVEC_NBITS; ++i)
  771. {
  772. bitvec_clr_bit(z, i);
  773. }
  774. /* 4) w = inv(s) mod n */
  775. gf2field_inv(w, s); /* w = inv(s) */
  776. /* Modulo reduction polynomial if degree(tmp) > CURVE_DEGREE */
  777. if (bitvec_get_bit(w, CURVE_DEGREE))
  778. {
  779. // printf("reduction on w\n");
  780. gf2field_add(w, w, polynomial);
  781. }
  782. /* 5) u1 = zw mod n, u2 = rw mod n*/
  783. gf2field_mul(u1, z, w); /* u1 = z * w */
  784. /* Modulo reduction polynomial if degree(tmp) > CURVE_DEGREE */
  785. if (bitvec_get_bit(u1, CURVE_DEGREE))
  786. {
  787. // printf("reduction on u1\n");
  788. gf2field_add(u1, u1, polynomial);
  789. }
  790. gf2field_mul(u2, r, w); /* u2 = r * w */
  791. /* Modulo reduction polynomial if degree(tmp) > CURVE_DEGREE */
  792. if (bitvec_get_bit(u2, CURVE_DEGREE))
  793. {
  794. // printf("reduction on u2\n");
  795. gf2field_add(u2, u2, polynomial);
  796. }
  797. /* 6) (x,y) = (u1 * G) + (u2 * public) */
  798. bitvec_copy(x1, base_x);
  799. bitvec_copy(y1, base_y);
  800. gf2field_mul(u1, x1, y1); /* u1 * G */
  801. bitvec_copy(w, (uint32_t*)(public_key));
  802. bitvec_copy(z, (uint32_t*)(public_key + ECC_PRV_KEY_SIZE));
  803. gf2field_mul(u2, w, z); /* u2 * Q */
  804. gf2point_add(x1, y1, w, z);
  805. if (bitvec_get_bit(x1, CURVE_DEGREE))
  806. {
  807. // printf("reduction on x1\n");
  808. gf2field_add(x1, x1, polynomial);
  809. }
  810. success = bitvec_equal(r, x1);
  811. if (!success)
  812. {
  813. // printf("x = '");
  814. for (i = 0; i < BITVEC_NWORDS; ++i)
  815. {
  816. // printf("%.08x", x1[i]);
  817. }
  818. // printf("' [%u]\n", i);
  819. // printf("r = '");
  820. for (i = 0; i < BITVEC_NWORDS; ++i)
  821. {
  822. // printf("%.08x", r[i]);
  823. }
  824. // printf("' [%u]\n", i);
  825. }
  826. }
  827. else
  828. {
  829. // printf("(s or r) == zero\n");
  830. }
  831. return success;
  832. }