ecp_nistp521.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. /*
  2. * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Copyright 2011 Google Inc.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. *
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. /*
  25. * ECDSA low level APIs are deprecated for public use, but still ok for
  26. * internal use.
  27. */
  28. #include "internal/deprecated.h"
  29. /*
  30. * A 64-bit implementation of the NIST P-521 elliptic curve point multiplication
  31. *
  32. * OpenSSL integration was taken from Emilia Kasper's work in ecp_nistp224.c.
  33. * Otherwise based on Emilia's P224 work, which was inspired by my curve25519
  34. * work which got its smarts from Daniel J. Bernstein's work on the same.
  35. */
  36. #include <openssl/e_os2.h>
  37. #include <string.h>
  38. #include <openssl/err.h>
  39. #include "ec_local.h"
  40. #if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16
  41. /* even with gcc, the typedef won't work for 32-bit platforms */
  42. typedef __uint128_t uint128_t; /* nonstandard; implemented by gcc on 64-bit
  43. * platforms */
  44. #else
  45. # error "Your compiler doesn't appear to support 128-bit integer types"
  46. #endif
  47. typedef uint8_t u8;
  48. typedef uint64_t u64;
  49. /*
  50. * The underlying field. P521 operates over GF(2^521-1). We can serialize an
  51. * element of this field into 66 bytes where the most significant byte
  52. * contains only a single bit. We call this an felem_bytearray.
  53. */
  54. typedef u8 felem_bytearray[66];
  55. /*
  56. * These are the parameters of P521, taken from FIPS 186-3, section D.1.2.5.
  57. * These values are big-endian.
  58. */
  59. static const felem_bytearray nistp521_curve_params[5] = {
  60. {0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* p */
  61. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  62. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  63. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  64. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  65. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  66. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  67. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  68. 0xff, 0xff},
  69. {0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* a = -3 */
  70. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  71. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  72. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  73. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  74. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  75. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  76. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  77. 0xff, 0xfc},
  78. {0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, /* b */
  79. 0x9a, 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85,
  80. 0x40, 0xee, 0xa2, 0xda, 0x72, 0x5b, 0x99, 0xb3,
  81. 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1,
  82. 0x09, 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e,
  83. 0x93, 0x7b, 0x16, 0x52, 0xc0, 0xbd, 0x3b, 0xb1,
  84. 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c,
  85. 0x34, 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50,
  86. 0x3f, 0x00},
  87. {0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, /* x */
  88. 0xe9, 0xcd, 0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95,
  89. 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
  90. 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d,
  91. 0x3d, 0xba, 0xa1, 0x4b, 0x5e, 0x77, 0xef, 0xe7,
  92. 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
  93. 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a,
  94. 0x42, 0x9b, 0xf9, 0x7e, 0x7e, 0x31, 0xc2, 0xe5,
  95. 0xbd, 0x66},
  96. {0x01, 0x18, 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b, /* y */
  97. 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d,
  98. 0x1b, 0xd9, 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b,
  99. 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e,
  100. 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4,
  101. 0x26, 0x40, 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad,
  102. 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72,
  103. 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1,
  104. 0x66, 0x50}
  105. };
  106. /*-
  107. * The representation of field elements.
  108. * ------------------------------------
  109. *
  110. * We represent field elements with nine values. These values are either 64 or
  111. * 128 bits and the field element represented is:
  112. * v[0]*2^0 + v[1]*2^58 + v[2]*2^116 + ... + v[8]*2^464 (mod p)
  113. * Each of the nine values is called a 'limb'. Since the limbs are spaced only
  114. * 58 bits apart, but are greater than 58 bits in length, the most significant
  115. * bits of each limb overlap with the least significant bits of the next.
  116. *
  117. * A field element with 64-bit limbs is an 'felem'. One with 128-bit limbs is a
  118. * 'largefelem' */
  119. #define NLIMBS 9
  120. typedef uint64_t limb;
  121. typedef limb limb_aX __attribute((__aligned__(1)));
  122. typedef limb felem[NLIMBS];
  123. typedef uint128_t largefelem[NLIMBS];
  124. static const limb bottom57bits = 0x1ffffffffffffff;
  125. static const limb bottom58bits = 0x3ffffffffffffff;
  126. /*
  127. * bin66_to_felem takes a little-endian byte array and converts it into felem
  128. * form. This assumes that the CPU is little-endian.
  129. */
  130. static void bin66_to_felem(felem out, const u8 in[66])
  131. {
  132. out[0] = (*((limb *) & in[0])) & bottom58bits;
  133. out[1] = (*((limb_aX *) & in[7]) >> 2) & bottom58bits;
  134. out[2] = (*((limb_aX *) & in[14]) >> 4) & bottom58bits;
  135. out[3] = (*((limb_aX *) & in[21]) >> 6) & bottom58bits;
  136. out[4] = (*((limb_aX *) & in[29])) & bottom58bits;
  137. out[5] = (*((limb_aX *) & in[36]) >> 2) & bottom58bits;
  138. out[6] = (*((limb_aX *) & in[43]) >> 4) & bottom58bits;
  139. out[7] = (*((limb_aX *) & in[50]) >> 6) & bottom58bits;
  140. out[8] = (*((limb_aX *) & in[58])) & bottom57bits;
  141. }
  142. /*
  143. * felem_to_bin66 takes an felem and serializes into a little endian, 66 byte
  144. * array. This assumes that the CPU is little-endian.
  145. */
  146. static void felem_to_bin66(u8 out[66], const felem in)
  147. {
  148. memset(out, 0, 66);
  149. (*((limb *) & out[0])) = in[0];
  150. (*((limb_aX *) & out[7])) |= in[1] << 2;
  151. (*((limb_aX *) & out[14])) |= in[2] << 4;
  152. (*((limb_aX *) & out[21])) |= in[3] << 6;
  153. (*((limb_aX *) & out[29])) = in[4];
  154. (*((limb_aX *) & out[36])) |= in[5] << 2;
  155. (*((limb_aX *) & out[43])) |= in[6] << 4;
  156. (*((limb_aX *) & out[50])) |= in[7] << 6;
  157. (*((limb_aX *) & out[58])) = in[8];
  158. }
  159. /* BN_to_felem converts an OpenSSL BIGNUM into an felem */
  160. static int BN_to_felem(felem out, const BIGNUM *bn)
  161. {
  162. felem_bytearray b_out;
  163. int num_bytes;
  164. if (BN_is_negative(bn)) {
  165. ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
  166. return 0;
  167. }
  168. num_bytes = BN_bn2lebinpad(bn, b_out, sizeof(b_out));
  169. if (num_bytes < 0) {
  170. ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
  171. return 0;
  172. }
  173. bin66_to_felem(out, b_out);
  174. return 1;
  175. }
  176. /* felem_to_BN converts an felem into an OpenSSL BIGNUM */
  177. static BIGNUM *felem_to_BN(BIGNUM *out, const felem in)
  178. {
  179. felem_bytearray b_out;
  180. felem_to_bin66(b_out, in);
  181. return BN_lebin2bn(b_out, sizeof(b_out), out);
  182. }
  183. /*-
  184. * Field operations
  185. * ----------------
  186. */
  187. static void felem_one(felem out)
  188. {
  189. out[0] = 1;
  190. out[1] = 0;
  191. out[2] = 0;
  192. out[3] = 0;
  193. out[4] = 0;
  194. out[5] = 0;
  195. out[6] = 0;
  196. out[7] = 0;
  197. out[8] = 0;
  198. }
  199. static void felem_assign(felem out, const felem in)
  200. {
  201. out[0] = in[0];
  202. out[1] = in[1];
  203. out[2] = in[2];
  204. out[3] = in[3];
  205. out[4] = in[4];
  206. out[5] = in[5];
  207. out[6] = in[6];
  208. out[7] = in[7];
  209. out[8] = in[8];
  210. }
  211. /* felem_sum64 sets out = out + in. */
  212. static void felem_sum64(felem out, const felem in)
  213. {
  214. out[0] += in[0];
  215. out[1] += in[1];
  216. out[2] += in[2];
  217. out[3] += in[3];
  218. out[4] += in[4];
  219. out[5] += in[5];
  220. out[6] += in[6];
  221. out[7] += in[7];
  222. out[8] += in[8];
  223. }
  224. /* felem_scalar sets out = in * scalar */
  225. static void felem_scalar(felem out, const felem in, limb scalar)
  226. {
  227. out[0] = in[0] * scalar;
  228. out[1] = in[1] * scalar;
  229. out[2] = in[2] * scalar;
  230. out[3] = in[3] * scalar;
  231. out[4] = in[4] * scalar;
  232. out[5] = in[5] * scalar;
  233. out[6] = in[6] * scalar;
  234. out[7] = in[7] * scalar;
  235. out[8] = in[8] * scalar;
  236. }
  237. /* felem_scalar64 sets out = out * scalar */
  238. static void felem_scalar64(felem out, limb scalar)
  239. {
  240. out[0] *= scalar;
  241. out[1] *= scalar;
  242. out[2] *= scalar;
  243. out[3] *= scalar;
  244. out[4] *= scalar;
  245. out[5] *= scalar;
  246. out[6] *= scalar;
  247. out[7] *= scalar;
  248. out[8] *= scalar;
  249. }
  250. /* felem_scalar128 sets out = out * scalar */
  251. static void felem_scalar128(largefelem out, limb scalar)
  252. {
  253. out[0] *= scalar;
  254. out[1] *= scalar;
  255. out[2] *= scalar;
  256. out[3] *= scalar;
  257. out[4] *= scalar;
  258. out[5] *= scalar;
  259. out[6] *= scalar;
  260. out[7] *= scalar;
  261. out[8] *= scalar;
  262. }
  263. /*-
  264. * felem_neg sets |out| to |-in|
  265. * On entry:
  266. * in[i] < 2^59 + 2^14
  267. * On exit:
  268. * out[i] < 2^62
  269. */
  270. static void felem_neg(felem out, const felem in)
  271. {
  272. /* In order to prevent underflow, we subtract from 0 mod p. */
  273. static const limb two62m3 = (((limb) 1) << 62) - (((limb) 1) << 5);
  274. static const limb two62m2 = (((limb) 1) << 62) - (((limb) 1) << 4);
  275. out[0] = two62m3 - in[0];
  276. out[1] = two62m2 - in[1];
  277. out[2] = two62m2 - in[2];
  278. out[3] = two62m2 - in[3];
  279. out[4] = two62m2 - in[4];
  280. out[5] = two62m2 - in[5];
  281. out[6] = two62m2 - in[6];
  282. out[7] = two62m2 - in[7];
  283. out[8] = two62m2 - in[8];
  284. }
  285. /*-
  286. * felem_diff64 subtracts |in| from |out|
  287. * On entry:
  288. * in[i] < 2^59 + 2^14
  289. * On exit:
  290. * out[i] < out[i] + 2^62
  291. */
  292. static void felem_diff64(felem out, const felem in)
  293. {
  294. /*
  295. * In order to prevent underflow, we add 0 mod p before subtracting.
  296. */
  297. static const limb two62m3 = (((limb) 1) << 62) - (((limb) 1) << 5);
  298. static const limb two62m2 = (((limb) 1) << 62) - (((limb) 1) << 4);
  299. out[0] += two62m3 - in[0];
  300. out[1] += two62m2 - in[1];
  301. out[2] += two62m2 - in[2];
  302. out[3] += two62m2 - in[3];
  303. out[4] += two62m2 - in[4];
  304. out[5] += two62m2 - in[5];
  305. out[6] += two62m2 - in[6];
  306. out[7] += two62m2 - in[7];
  307. out[8] += two62m2 - in[8];
  308. }
  309. /*-
  310. * felem_diff_128_64 subtracts |in| from |out|
  311. * On entry:
  312. * in[i] < 2^62 + 2^17
  313. * On exit:
  314. * out[i] < out[i] + 2^63
  315. */
  316. static void felem_diff_128_64(largefelem out, const felem in)
  317. {
  318. /*
  319. * In order to prevent underflow, we add 64p mod p (which is equivalent
  320. * to 0 mod p) before subtracting. p is 2^521 - 1, i.e. in binary a 521
  321. * digit number with all bits set to 1. See "The representation of field
  322. * elements" comment above for a description of how limbs are used to
  323. * represent a number. 64p is represented with 8 limbs containing a number
  324. * with 58 bits set and one limb with a number with 57 bits set.
  325. */
  326. static const limb two63m6 = (((limb) 1) << 63) - (((limb) 1) << 6);
  327. static const limb two63m5 = (((limb) 1) << 63) - (((limb) 1) << 5);
  328. out[0] += two63m6 - in[0];
  329. out[1] += two63m5 - in[1];
  330. out[2] += two63m5 - in[2];
  331. out[3] += two63m5 - in[3];
  332. out[4] += two63m5 - in[4];
  333. out[5] += two63m5 - in[5];
  334. out[6] += two63m5 - in[6];
  335. out[7] += two63m5 - in[7];
  336. out[8] += two63m5 - in[8];
  337. }
  338. /*-
  339. * felem_diff_128_64 subtracts |in| from |out|
  340. * On entry:
  341. * in[i] < 2^126
  342. * On exit:
  343. * out[i] < out[i] + 2^127 - 2^69
  344. */
  345. static void felem_diff128(largefelem out, const largefelem in)
  346. {
  347. /*
  348. * In order to prevent underflow, we add 0 mod p before subtracting.
  349. */
  350. static const uint128_t two127m70 =
  351. (((uint128_t) 1) << 127) - (((uint128_t) 1) << 70);
  352. static const uint128_t two127m69 =
  353. (((uint128_t) 1) << 127) - (((uint128_t) 1) << 69);
  354. out[0] += (two127m70 - in[0]);
  355. out[1] += (two127m69 - in[1]);
  356. out[2] += (two127m69 - in[2]);
  357. out[3] += (two127m69 - in[3]);
  358. out[4] += (two127m69 - in[4]);
  359. out[5] += (two127m69 - in[5]);
  360. out[6] += (two127m69 - in[6]);
  361. out[7] += (two127m69 - in[7]);
  362. out[8] += (two127m69 - in[8]);
  363. }
  364. /*-
  365. * felem_square sets |out| = |in|^2
  366. * On entry:
  367. * in[i] < 2^62
  368. * On exit:
  369. * out[i] < 17 * max(in[i]) * max(in[i])
  370. */
  371. static void felem_square(largefelem out, const felem in)
  372. {
  373. felem inx2, inx4;
  374. felem_scalar(inx2, in, 2);
  375. felem_scalar(inx4, in, 4);
  376. /*-
  377. * We have many cases were we want to do
  378. * in[x] * in[y] +
  379. * in[y] * in[x]
  380. * This is obviously just
  381. * 2 * in[x] * in[y]
  382. * However, rather than do the doubling on the 128 bit result, we
  383. * double one of the inputs to the multiplication by reading from
  384. * |inx2|
  385. */
  386. out[0] = ((uint128_t) in[0]) * in[0];
  387. out[1] = ((uint128_t) in[0]) * inx2[1];
  388. out[2] = ((uint128_t) in[0]) * inx2[2] + ((uint128_t) in[1]) * in[1];
  389. out[3] = ((uint128_t) in[0]) * inx2[3] + ((uint128_t) in[1]) * inx2[2];
  390. out[4] = ((uint128_t) in[0]) * inx2[4] +
  391. ((uint128_t) in[1]) * inx2[3] + ((uint128_t) in[2]) * in[2];
  392. out[5] = ((uint128_t) in[0]) * inx2[5] +
  393. ((uint128_t) in[1]) * inx2[4] + ((uint128_t) in[2]) * inx2[3];
  394. out[6] = ((uint128_t) in[0]) * inx2[6] +
  395. ((uint128_t) in[1]) * inx2[5] +
  396. ((uint128_t) in[2]) * inx2[4] + ((uint128_t) in[3]) * in[3];
  397. out[7] = ((uint128_t) in[0]) * inx2[7] +
  398. ((uint128_t) in[1]) * inx2[6] +
  399. ((uint128_t) in[2]) * inx2[5] + ((uint128_t) in[3]) * inx2[4];
  400. out[8] = ((uint128_t) in[0]) * inx2[8] +
  401. ((uint128_t) in[1]) * inx2[7] +
  402. ((uint128_t) in[2]) * inx2[6] +
  403. ((uint128_t) in[3]) * inx2[5] + ((uint128_t) in[4]) * in[4];
  404. /*
  405. * The remaining limbs fall above 2^521, with the first falling at 2^522.
  406. * They correspond to locations one bit up from the limbs produced above
  407. * so we would have to multiply by two to align them. Again, rather than
  408. * operate on the 128-bit result, we double one of the inputs to the
  409. * multiplication. If we want to double for both this reason, and the
  410. * reason above, then we end up multiplying by four.
  411. */
  412. /* 9 */
  413. out[0] += ((uint128_t) in[1]) * inx4[8] +
  414. ((uint128_t) in[2]) * inx4[7] +
  415. ((uint128_t) in[3]) * inx4[6] + ((uint128_t) in[4]) * inx4[5];
  416. /* 10 */
  417. out[1] += ((uint128_t) in[2]) * inx4[8] +
  418. ((uint128_t) in[3]) * inx4[7] +
  419. ((uint128_t) in[4]) * inx4[6] + ((uint128_t) in[5]) * inx2[5];
  420. /* 11 */
  421. out[2] += ((uint128_t) in[3]) * inx4[8] +
  422. ((uint128_t) in[4]) * inx4[7] + ((uint128_t) in[5]) * inx4[6];
  423. /* 12 */
  424. out[3] += ((uint128_t) in[4]) * inx4[8] +
  425. ((uint128_t) in[5]) * inx4[7] + ((uint128_t) in[6]) * inx2[6];
  426. /* 13 */
  427. out[4] += ((uint128_t) in[5]) * inx4[8] + ((uint128_t) in[6]) * inx4[7];
  428. /* 14 */
  429. out[5] += ((uint128_t) in[6]) * inx4[8] + ((uint128_t) in[7]) * inx2[7];
  430. /* 15 */
  431. out[6] += ((uint128_t) in[7]) * inx4[8];
  432. /* 16 */
  433. out[7] += ((uint128_t) in[8]) * inx2[8];
  434. }
  435. /*-
  436. * felem_mul sets |out| = |in1| * |in2|
  437. * On entry:
  438. * in1[i] < 2^64
  439. * in2[i] < 2^63
  440. * On exit:
  441. * out[i] < 17 * max(in1[i]) * max(in2[i])
  442. */
  443. static void felem_mul(largefelem out, const felem in1, const felem in2)
  444. {
  445. felem in2x2;
  446. felem_scalar(in2x2, in2, 2);
  447. out[0] = ((uint128_t) in1[0]) * in2[0];
  448. out[1] = ((uint128_t) in1[0]) * in2[1] +
  449. ((uint128_t) in1[1]) * in2[0];
  450. out[2] = ((uint128_t) in1[0]) * in2[2] +
  451. ((uint128_t) in1[1]) * in2[1] +
  452. ((uint128_t) in1[2]) * in2[0];
  453. out[3] = ((uint128_t) in1[0]) * in2[3] +
  454. ((uint128_t) in1[1]) * in2[2] +
  455. ((uint128_t) in1[2]) * in2[1] +
  456. ((uint128_t) in1[3]) * in2[0];
  457. out[4] = ((uint128_t) in1[0]) * in2[4] +
  458. ((uint128_t) in1[1]) * in2[3] +
  459. ((uint128_t) in1[2]) * in2[2] +
  460. ((uint128_t) in1[3]) * in2[1] +
  461. ((uint128_t) in1[4]) * in2[0];
  462. out[5] = ((uint128_t) in1[0]) * in2[5] +
  463. ((uint128_t) in1[1]) * in2[4] +
  464. ((uint128_t) in1[2]) * in2[3] +
  465. ((uint128_t) in1[3]) * in2[2] +
  466. ((uint128_t) in1[4]) * in2[1] +
  467. ((uint128_t) in1[5]) * in2[0];
  468. out[6] = ((uint128_t) in1[0]) * in2[6] +
  469. ((uint128_t) in1[1]) * in2[5] +
  470. ((uint128_t) in1[2]) * in2[4] +
  471. ((uint128_t) in1[3]) * in2[3] +
  472. ((uint128_t) in1[4]) * in2[2] +
  473. ((uint128_t) in1[5]) * in2[1] +
  474. ((uint128_t) in1[6]) * in2[0];
  475. out[7] = ((uint128_t) in1[0]) * in2[7] +
  476. ((uint128_t) in1[1]) * in2[6] +
  477. ((uint128_t) in1[2]) * in2[5] +
  478. ((uint128_t) in1[3]) * in2[4] +
  479. ((uint128_t) in1[4]) * in2[3] +
  480. ((uint128_t) in1[5]) * in2[2] +
  481. ((uint128_t) in1[6]) * in2[1] +
  482. ((uint128_t) in1[7]) * in2[0];
  483. out[8] = ((uint128_t) in1[0]) * in2[8] +
  484. ((uint128_t) in1[1]) * in2[7] +
  485. ((uint128_t) in1[2]) * in2[6] +
  486. ((uint128_t) in1[3]) * in2[5] +
  487. ((uint128_t) in1[4]) * in2[4] +
  488. ((uint128_t) in1[5]) * in2[3] +
  489. ((uint128_t) in1[6]) * in2[2] +
  490. ((uint128_t) in1[7]) * in2[1] +
  491. ((uint128_t) in1[8]) * in2[0];
  492. /* See comment in felem_square about the use of in2x2 here */
  493. out[0] += ((uint128_t) in1[1]) * in2x2[8] +
  494. ((uint128_t) in1[2]) * in2x2[7] +
  495. ((uint128_t) in1[3]) * in2x2[6] +
  496. ((uint128_t) in1[4]) * in2x2[5] +
  497. ((uint128_t) in1[5]) * in2x2[4] +
  498. ((uint128_t) in1[6]) * in2x2[3] +
  499. ((uint128_t) in1[7]) * in2x2[2] +
  500. ((uint128_t) in1[8]) * in2x2[1];
  501. out[1] += ((uint128_t) in1[2]) * in2x2[8] +
  502. ((uint128_t) in1[3]) * in2x2[7] +
  503. ((uint128_t) in1[4]) * in2x2[6] +
  504. ((uint128_t) in1[5]) * in2x2[5] +
  505. ((uint128_t) in1[6]) * in2x2[4] +
  506. ((uint128_t) in1[7]) * in2x2[3] +
  507. ((uint128_t) in1[8]) * in2x2[2];
  508. out[2] += ((uint128_t) in1[3]) * in2x2[8] +
  509. ((uint128_t) in1[4]) * in2x2[7] +
  510. ((uint128_t) in1[5]) * in2x2[6] +
  511. ((uint128_t) in1[6]) * in2x2[5] +
  512. ((uint128_t) in1[7]) * in2x2[4] +
  513. ((uint128_t) in1[8]) * in2x2[3];
  514. out[3] += ((uint128_t) in1[4]) * in2x2[8] +
  515. ((uint128_t) in1[5]) * in2x2[7] +
  516. ((uint128_t) in1[6]) * in2x2[6] +
  517. ((uint128_t) in1[7]) * in2x2[5] +
  518. ((uint128_t) in1[8]) * in2x2[4];
  519. out[4] += ((uint128_t) in1[5]) * in2x2[8] +
  520. ((uint128_t) in1[6]) * in2x2[7] +
  521. ((uint128_t) in1[7]) * in2x2[6] +
  522. ((uint128_t) in1[8]) * in2x2[5];
  523. out[5] += ((uint128_t) in1[6]) * in2x2[8] +
  524. ((uint128_t) in1[7]) * in2x2[7] +
  525. ((uint128_t) in1[8]) * in2x2[6];
  526. out[6] += ((uint128_t) in1[7]) * in2x2[8] +
  527. ((uint128_t) in1[8]) * in2x2[7];
  528. out[7] += ((uint128_t) in1[8]) * in2x2[8];
  529. }
  530. static const limb bottom52bits = 0xfffffffffffff;
  531. /*-
  532. * felem_reduce converts a largefelem to an felem.
  533. * On entry:
  534. * in[i] < 2^128
  535. * On exit:
  536. * out[i] < 2^59 + 2^14
  537. */
  538. static void felem_reduce(felem out, const largefelem in)
  539. {
  540. u64 overflow1, overflow2;
  541. out[0] = ((limb) in[0]) & bottom58bits;
  542. out[1] = ((limb) in[1]) & bottom58bits;
  543. out[2] = ((limb) in[2]) & bottom58bits;
  544. out[3] = ((limb) in[3]) & bottom58bits;
  545. out[4] = ((limb) in[4]) & bottom58bits;
  546. out[5] = ((limb) in[5]) & bottom58bits;
  547. out[6] = ((limb) in[6]) & bottom58bits;
  548. out[7] = ((limb) in[7]) & bottom58bits;
  549. out[8] = ((limb) in[8]) & bottom58bits;
  550. /* out[i] < 2^58 */
  551. out[1] += ((limb) in[0]) >> 58;
  552. out[1] += (((limb) (in[0] >> 64)) & bottom52bits) << 6;
  553. /*-
  554. * out[1] < 2^58 + 2^6 + 2^58
  555. * = 2^59 + 2^6
  556. */
  557. out[2] += ((limb) (in[0] >> 64)) >> 52;
  558. out[2] += ((limb) in[1]) >> 58;
  559. out[2] += (((limb) (in[1] >> 64)) & bottom52bits) << 6;
  560. out[3] += ((limb) (in[1] >> 64)) >> 52;
  561. out[3] += ((limb) in[2]) >> 58;
  562. out[3] += (((limb) (in[2] >> 64)) & bottom52bits) << 6;
  563. out[4] += ((limb) (in[2] >> 64)) >> 52;
  564. out[4] += ((limb) in[3]) >> 58;
  565. out[4] += (((limb) (in[3] >> 64)) & bottom52bits) << 6;
  566. out[5] += ((limb) (in[3] >> 64)) >> 52;
  567. out[5] += ((limb) in[4]) >> 58;
  568. out[5] += (((limb) (in[4] >> 64)) & bottom52bits) << 6;
  569. out[6] += ((limb) (in[4] >> 64)) >> 52;
  570. out[6] += ((limb) in[5]) >> 58;
  571. out[6] += (((limb) (in[5] >> 64)) & bottom52bits) << 6;
  572. out[7] += ((limb) (in[5] >> 64)) >> 52;
  573. out[7] += ((limb) in[6]) >> 58;
  574. out[7] += (((limb) (in[6] >> 64)) & bottom52bits) << 6;
  575. out[8] += ((limb) (in[6] >> 64)) >> 52;
  576. out[8] += ((limb) in[7]) >> 58;
  577. out[8] += (((limb) (in[7] >> 64)) & bottom52bits) << 6;
  578. /*-
  579. * out[x > 1] < 2^58 + 2^6 + 2^58 + 2^12
  580. * < 2^59 + 2^13
  581. */
  582. overflow1 = ((limb) (in[7] >> 64)) >> 52;
  583. overflow1 += ((limb) in[8]) >> 58;
  584. overflow1 += (((limb) (in[8] >> 64)) & bottom52bits) << 6;
  585. overflow2 = ((limb) (in[8] >> 64)) >> 52;
  586. overflow1 <<= 1; /* overflow1 < 2^13 + 2^7 + 2^59 */
  587. overflow2 <<= 1; /* overflow2 < 2^13 */
  588. out[0] += overflow1; /* out[0] < 2^60 */
  589. out[1] += overflow2; /* out[1] < 2^59 + 2^6 + 2^13 */
  590. out[1] += out[0] >> 58;
  591. out[0] &= bottom58bits;
  592. /*-
  593. * out[0] < 2^58
  594. * out[1] < 2^59 + 2^6 + 2^13 + 2^2
  595. * < 2^59 + 2^14
  596. */
  597. }
  598. static void felem_square_reduce(felem out, const felem in)
  599. {
  600. largefelem tmp;
  601. felem_square(tmp, in);
  602. felem_reduce(out, tmp);
  603. }
  604. static void felem_mul_reduce(felem out, const felem in1, const felem in2)
  605. {
  606. largefelem tmp;
  607. felem_mul(tmp, in1, in2);
  608. felem_reduce(out, tmp);
  609. }
  610. /*-
  611. * felem_inv calculates |out| = |in|^{-1}
  612. *
  613. * Based on Fermat's Little Theorem:
  614. * a^p = a (mod p)
  615. * a^{p-1} = 1 (mod p)
  616. * a^{p-2} = a^{-1} (mod p)
  617. */
  618. static void felem_inv(felem out, const felem in)
  619. {
  620. felem ftmp, ftmp2, ftmp3, ftmp4;
  621. largefelem tmp;
  622. unsigned i;
  623. felem_square(tmp, in);
  624. felem_reduce(ftmp, tmp); /* 2^1 */
  625. felem_mul(tmp, in, ftmp);
  626. felem_reduce(ftmp, tmp); /* 2^2 - 2^0 */
  627. felem_assign(ftmp2, ftmp);
  628. felem_square(tmp, ftmp);
  629. felem_reduce(ftmp, tmp); /* 2^3 - 2^1 */
  630. felem_mul(tmp, in, ftmp);
  631. felem_reduce(ftmp, tmp); /* 2^3 - 2^0 */
  632. felem_square(tmp, ftmp);
  633. felem_reduce(ftmp, tmp); /* 2^4 - 2^1 */
  634. felem_square(tmp, ftmp2);
  635. felem_reduce(ftmp3, tmp); /* 2^3 - 2^1 */
  636. felem_square(tmp, ftmp3);
  637. felem_reduce(ftmp3, tmp); /* 2^4 - 2^2 */
  638. felem_mul(tmp, ftmp3, ftmp2);
  639. felem_reduce(ftmp3, tmp); /* 2^4 - 2^0 */
  640. felem_assign(ftmp2, ftmp3);
  641. felem_square(tmp, ftmp3);
  642. felem_reduce(ftmp3, tmp); /* 2^5 - 2^1 */
  643. felem_square(tmp, ftmp3);
  644. felem_reduce(ftmp3, tmp); /* 2^6 - 2^2 */
  645. felem_square(tmp, ftmp3);
  646. felem_reduce(ftmp3, tmp); /* 2^7 - 2^3 */
  647. felem_square(tmp, ftmp3);
  648. felem_reduce(ftmp3, tmp); /* 2^8 - 2^4 */
  649. felem_assign(ftmp4, ftmp3);
  650. felem_mul(tmp, ftmp3, ftmp);
  651. felem_reduce(ftmp4, tmp); /* 2^8 - 2^1 */
  652. felem_square(tmp, ftmp4);
  653. felem_reduce(ftmp4, tmp); /* 2^9 - 2^2 */
  654. felem_mul(tmp, ftmp3, ftmp2);
  655. felem_reduce(ftmp3, tmp); /* 2^8 - 2^0 */
  656. felem_assign(ftmp2, ftmp3);
  657. for (i = 0; i < 8; i++) {
  658. felem_square(tmp, ftmp3);
  659. felem_reduce(ftmp3, tmp); /* 2^16 - 2^8 */
  660. }
  661. felem_mul(tmp, ftmp3, ftmp2);
  662. felem_reduce(ftmp3, tmp); /* 2^16 - 2^0 */
  663. felem_assign(ftmp2, ftmp3);
  664. for (i = 0; i < 16; i++) {
  665. felem_square(tmp, ftmp3);
  666. felem_reduce(ftmp3, tmp); /* 2^32 - 2^16 */
  667. }
  668. felem_mul(tmp, ftmp3, ftmp2);
  669. felem_reduce(ftmp3, tmp); /* 2^32 - 2^0 */
  670. felem_assign(ftmp2, ftmp3);
  671. for (i = 0; i < 32; i++) {
  672. felem_square(tmp, ftmp3);
  673. felem_reduce(ftmp3, tmp); /* 2^64 - 2^32 */
  674. }
  675. felem_mul(tmp, ftmp3, ftmp2);
  676. felem_reduce(ftmp3, tmp); /* 2^64 - 2^0 */
  677. felem_assign(ftmp2, ftmp3);
  678. for (i = 0; i < 64; i++) {
  679. felem_square(tmp, ftmp3);
  680. felem_reduce(ftmp3, tmp); /* 2^128 - 2^64 */
  681. }
  682. felem_mul(tmp, ftmp3, ftmp2);
  683. felem_reduce(ftmp3, tmp); /* 2^128 - 2^0 */
  684. felem_assign(ftmp2, ftmp3);
  685. for (i = 0; i < 128; i++) {
  686. felem_square(tmp, ftmp3);
  687. felem_reduce(ftmp3, tmp); /* 2^256 - 2^128 */
  688. }
  689. felem_mul(tmp, ftmp3, ftmp2);
  690. felem_reduce(ftmp3, tmp); /* 2^256 - 2^0 */
  691. felem_assign(ftmp2, ftmp3);
  692. for (i = 0; i < 256; i++) {
  693. felem_square(tmp, ftmp3);
  694. felem_reduce(ftmp3, tmp); /* 2^512 - 2^256 */
  695. }
  696. felem_mul(tmp, ftmp3, ftmp2);
  697. felem_reduce(ftmp3, tmp); /* 2^512 - 2^0 */
  698. for (i = 0; i < 9; i++) {
  699. felem_square(tmp, ftmp3);
  700. felem_reduce(ftmp3, tmp); /* 2^521 - 2^9 */
  701. }
  702. felem_mul(tmp, ftmp3, ftmp4);
  703. felem_reduce(ftmp3, tmp); /* 2^512 - 2^2 */
  704. felem_mul(tmp, ftmp3, in);
  705. felem_reduce(out, tmp); /* 2^512 - 3 */
  706. }
  707. /* This is 2^521-1, expressed as an felem */
  708. static const felem kPrime = {
  709. 0x03ffffffffffffff, 0x03ffffffffffffff, 0x03ffffffffffffff,
  710. 0x03ffffffffffffff, 0x03ffffffffffffff, 0x03ffffffffffffff,
  711. 0x03ffffffffffffff, 0x03ffffffffffffff, 0x01ffffffffffffff
  712. };
  713. /*-
  714. * felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
  715. * otherwise.
  716. * On entry:
  717. * in[i] < 2^59 + 2^14
  718. */
  719. static limb felem_is_zero(const felem in)
  720. {
  721. felem ftmp;
  722. limb is_zero, is_p;
  723. felem_assign(ftmp, in);
  724. ftmp[0] += ftmp[8] >> 57;
  725. ftmp[8] &= bottom57bits;
  726. /* ftmp[8] < 2^57 */
  727. ftmp[1] += ftmp[0] >> 58;
  728. ftmp[0] &= bottom58bits;
  729. ftmp[2] += ftmp[1] >> 58;
  730. ftmp[1] &= bottom58bits;
  731. ftmp[3] += ftmp[2] >> 58;
  732. ftmp[2] &= bottom58bits;
  733. ftmp[4] += ftmp[3] >> 58;
  734. ftmp[3] &= bottom58bits;
  735. ftmp[5] += ftmp[4] >> 58;
  736. ftmp[4] &= bottom58bits;
  737. ftmp[6] += ftmp[5] >> 58;
  738. ftmp[5] &= bottom58bits;
  739. ftmp[7] += ftmp[6] >> 58;
  740. ftmp[6] &= bottom58bits;
  741. ftmp[8] += ftmp[7] >> 58;
  742. ftmp[7] &= bottom58bits;
  743. /* ftmp[8] < 2^57 + 4 */
  744. /*
  745. * The ninth limb of 2*(2^521-1) is 0x03ffffffffffffff, which is greater
  746. * than our bound for ftmp[8]. Therefore we only have to check if the
  747. * zero is zero or 2^521-1.
  748. */
  749. is_zero = 0;
  750. is_zero |= ftmp[0];
  751. is_zero |= ftmp[1];
  752. is_zero |= ftmp[2];
  753. is_zero |= ftmp[3];
  754. is_zero |= ftmp[4];
  755. is_zero |= ftmp[5];
  756. is_zero |= ftmp[6];
  757. is_zero |= ftmp[7];
  758. is_zero |= ftmp[8];
  759. is_zero--;
  760. /*
  761. * We know that ftmp[i] < 2^63, therefore the only way that the top bit
  762. * can be set is if is_zero was 0 before the decrement.
  763. */
  764. is_zero = 0 - (is_zero >> 63);
  765. is_p = ftmp[0] ^ kPrime[0];
  766. is_p |= ftmp[1] ^ kPrime[1];
  767. is_p |= ftmp[2] ^ kPrime[2];
  768. is_p |= ftmp[3] ^ kPrime[3];
  769. is_p |= ftmp[4] ^ kPrime[4];
  770. is_p |= ftmp[5] ^ kPrime[5];
  771. is_p |= ftmp[6] ^ kPrime[6];
  772. is_p |= ftmp[7] ^ kPrime[7];
  773. is_p |= ftmp[8] ^ kPrime[8];
  774. is_p--;
  775. is_p = 0 - (is_p >> 63);
  776. is_zero |= is_p;
  777. return is_zero;
  778. }
  779. static int felem_is_zero_int(const void *in)
  780. {
  781. return (int)(felem_is_zero(in) & ((limb) 1));
  782. }
  783. /*-
  784. * felem_contract converts |in| to its unique, minimal representation.
  785. * On entry:
  786. * in[i] < 2^59 + 2^14
  787. */
  788. static void felem_contract(felem out, const felem in)
  789. {
  790. limb is_p, is_greater, sign;
  791. static const limb two58 = ((limb) 1) << 58;
  792. felem_assign(out, in);
  793. out[0] += out[8] >> 57;
  794. out[8] &= bottom57bits;
  795. /* out[8] < 2^57 */
  796. out[1] += out[0] >> 58;
  797. out[0] &= bottom58bits;
  798. out[2] += out[1] >> 58;
  799. out[1] &= bottom58bits;
  800. out[3] += out[2] >> 58;
  801. out[2] &= bottom58bits;
  802. out[4] += out[3] >> 58;
  803. out[3] &= bottom58bits;
  804. out[5] += out[4] >> 58;
  805. out[4] &= bottom58bits;
  806. out[6] += out[5] >> 58;
  807. out[5] &= bottom58bits;
  808. out[7] += out[6] >> 58;
  809. out[6] &= bottom58bits;
  810. out[8] += out[7] >> 58;
  811. out[7] &= bottom58bits;
  812. /* out[8] < 2^57 + 4 */
  813. /*
  814. * If the value is greater than 2^521-1 then we have to subtract 2^521-1
  815. * out. See the comments in felem_is_zero regarding why we don't test for
  816. * other multiples of the prime.
  817. */
  818. /*
  819. * First, if |out| is equal to 2^521-1, we subtract it out to get zero.
  820. */
  821. is_p = out[0] ^ kPrime[0];
  822. is_p |= out[1] ^ kPrime[1];
  823. is_p |= out[2] ^ kPrime[2];
  824. is_p |= out[3] ^ kPrime[3];
  825. is_p |= out[4] ^ kPrime[4];
  826. is_p |= out[5] ^ kPrime[5];
  827. is_p |= out[6] ^ kPrime[6];
  828. is_p |= out[7] ^ kPrime[7];
  829. is_p |= out[8] ^ kPrime[8];
  830. is_p--;
  831. is_p &= is_p << 32;
  832. is_p &= is_p << 16;
  833. is_p &= is_p << 8;
  834. is_p &= is_p << 4;
  835. is_p &= is_p << 2;
  836. is_p &= is_p << 1;
  837. is_p = 0 - (is_p >> 63);
  838. is_p = ~is_p;
  839. /* is_p is 0 iff |out| == 2^521-1 and all ones otherwise */
  840. out[0] &= is_p;
  841. out[1] &= is_p;
  842. out[2] &= is_p;
  843. out[3] &= is_p;
  844. out[4] &= is_p;
  845. out[5] &= is_p;
  846. out[6] &= is_p;
  847. out[7] &= is_p;
  848. out[8] &= is_p;
  849. /*
  850. * In order to test that |out| >= 2^521-1 we need only test if out[8] >>
  851. * 57 is greater than zero as (2^521-1) + x >= 2^522
  852. */
  853. is_greater = out[8] >> 57;
  854. is_greater |= is_greater << 32;
  855. is_greater |= is_greater << 16;
  856. is_greater |= is_greater << 8;
  857. is_greater |= is_greater << 4;
  858. is_greater |= is_greater << 2;
  859. is_greater |= is_greater << 1;
  860. is_greater = 0 - (is_greater >> 63);
  861. out[0] -= kPrime[0] & is_greater;
  862. out[1] -= kPrime[1] & is_greater;
  863. out[2] -= kPrime[2] & is_greater;
  864. out[3] -= kPrime[3] & is_greater;
  865. out[4] -= kPrime[4] & is_greater;
  866. out[5] -= kPrime[5] & is_greater;
  867. out[6] -= kPrime[6] & is_greater;
  868. out[7] -= kPrime[7] & is_greater;
  869. out[8] -= kPrime[8] & is_greater;
  870. /* Eliminate negative coefficients */
  871. sign = -(out[0] >> 63);
  872. out[0] += (two58 & sign);
  873. out[1] -= (1 & sign);
  874. sign = -(out[1] >> 63);
  875. out[1] += (two58 & sign);
  876. out[2] -= (1 & sign);
  877. sign = -(out[2] >> 63);
  878. out[2] += (two58 & sign);
  879. out[3] -= (1 & sign);
  880. sign = -(out[3] >> 63);
  881. out[3] += (two58 & sign);
  882. out[4] -= (1 & sign);
  883. sign = -(out[4] >> 63);
  884. out[4] += (two58 & sign);
  885. out[5] -= (1 & sign);
  886. sign = -(out[0] >> 63);
  887. out[5] += (two58 & sign);
  888. out[6] -= (1 & sign);
  889. sign = -(out[6] >> 63);
  890. out[6] += (two58 & sign);
  891. out[7] -= (1 & sign);
  892. sign = -(out[7] >> 63);
  893. out[7] += (two58 & sign);
  894. out[8] -= (1 & sign);
  895. sign = -(out[5] >> 63);
  896. out[5] += (two58 & sign);
  897. out[6] -= (1 & sign);
  898. sign = -(out[6] >> 63);
  899. out[6] += (two58 & sign);
  900. out[7] -= (1 & sign);
  901. sign = -(out[7] >> 63);
  902. out[7] += (two58 & sign);
  903. out[8] -= (1 & sign);
  904. }
  905. /*-
  906. * Group operations
  907. * ----------------
  908. *
  909. * Building on top of the field operations we have the operations on the
  910. * elliptic curve group itself. Points on the curve are represented in Jacobian
  911. * coordinates */
  912. /*-
  913. * point_double calculates 2*(x_in, y_in, z_in)
  914. *
  915. * The method is taken from:
  916. * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  917. *
  918. * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
  919. * while x_out == y_in is not (maybe this works, but it's not tested). */
  920. static void
  921. point_double(felem x_out, felem y_out, felem z_out,
  922. const felem x_in, const felem y_in, const felem z_in)
  923. {
  924. largefelem tmp, tmp2;
  925. felem delta, gamma, beta, alpha, ftmp, ftmp2;
  926. felem_assign(ftmp, x_in);
  927. felem_assign(ftmp2, x_in);
  928. /* delta = z^2 */
  929. felem_square(tmp, z_in);
  930. felem_reduce(delta, tmp); /* delta[i] < 2^59 + 2^14 */
  931. /* gamma = y^2 */
  932. felem_square(tmp, y_in);
  933. felem_reduce(gamma, tmp); /* gamma[i] < 2^59 + 2^14 */
  934. /* beta = x*gamma */
  935. felem_mul(tmp, x_in, gamma);
  936. felem_reduce(beta, tmp); /* beta[i] < 2^59 + 2^14 */
  937. /* alpha = 3*(x-delta)*(x+delta) */
  938. felem_diff64(ftmp, delta);
  939. /* ftmp[i] < 2^61 */
  940. felem_sum64(ftmp2, delta);
  941. /* ftmp2[i] < 2^60 + 2^15 */
  942. felem_scalar64(ftmp2, 3);
  943. /* ftmp2[i] < 3*2^60 + 3*2^15 */
  944. felem_mul(tmp, ftmp, ftmp2);
  945. /*-
  946. * tmp[i] < 17(3*2^121 + 3*2^76)
  947. * = 61*2^121 + 61*2^76
  948. * < 64*2^121 + 64*2^76
  949. * = 2^127 + 2^82
  950. * < 2^128
  951. */
  952. felem_reduce(alpha, tmp);
  953. /* x' = alpha^2 - 8*beta */
  954. felem_square(tmp, alpha);
  955. /*
  956. * tmp[i] < 17*2^120 < 2^125
  957. */
  958. felem_assign(ftmp, beta);
  959. felem_scalar64(ftmp, 8);
  960. /* ftmp[i] < 2^62 + 2^17 */
  961. felem_diff_128_64(tmp, ftmp);
  962. /* tmp[i] < 2^125 + 2^63 + 2^62 + 2^17 */
  963. felem_reduce(x_out, tmp);
  964. /* z' = (y + z)^2 - gamma - delta */
  965. felem_sum64(delta, gamma);
  966. /* delta[i] < 2^60 + 2^15 */
  967. felem_assign(ftmp, y_in);
  968. felem_sum64(ftmp, z_in);
  969. /* ftmp[i] < 2^60 + 2^15 */
  970. felem_square(tmp, ftmp);
  971. /*
  972. * tmp[i] < 17(2^122) < 2^127
  973. */
  974. felem_diff_128_64(tmp, delta);
  975. /* tmp[i] < 2^127 + 2^63 */
  976. felem_reduce(z_out, tmp);
  977. /* y' = alpha*(4*beta - x') - 8*gamma^2 */
  978. felem_scalar64(beta, 4);
  979. /* beta[i] < 2^61 + 2^16 */
  980. felem_diff64(beta, x_out);
  981. /* beta[i] < 2^61 + 2^60 + 2^16 */
  982. felem_mul(tmp, alpha, beta);
  983. /*-
  984. * tmp[i] < 17*((2^59 + 2^14)(2^61 + 2^60 + 2^16))
  985. * = 17*(2^120 + 2^75 + 2^119 + 2^74 + 2^75 + 2^30)
  986. * = 17*(2^120 + 2^119 + 2^76 + 2^74 + 2^30)
  987. * < 2^128
  988. */
  989. felem_square(tmp2, gamma);
  990. /*-
  991. * tmp2[i] < 17*(2^59 + 2^14)^2
  992. * = 17*(2^118 + 2^74 + 2^28)
  993. */
  994. felem_scalar128(tmp2, 8);
  995. /*-
  996. * tmp2[i] < 8*17*(2^118 + 2^74 + 2^28)
  997. * = 2^125 + 2^121 + 2^81 + 2^77 + 2^35 + 2^31
  998. * < 2^126
  999. */
  1000. felem_diff128(tmp, tmp2);
  1001. /*-
  1002. * tmp[i] < 2^127 - 2^69 + 17(2^120 + 2^119 + 2^76 + 2^74 + 2^30)
  1003. * = 2^127 + 2^124 + 2^122 + 2^120 + 2^118 + 2^80 + 2^78 + 2^76 +
  1004. * 2^74 + 2^69 + 2^34 + 2^30
  1005. * < 2^128
  1006. */
  1007. felem_reduce(y_out, tmp);
  1008. }
  1009. /* copy_conditional copies in to out iff mask is all ones. */
  1010. static void copy_conditional(felem out, const felem in, limb mask)
  1011. {
  1012. unsigned i;
  1013. for (i = 0; i < NLIMBS; ++i) {
  1014. const limb tmp = mask & (in[i] ^ out[i]);
  1015. out[i] ^= tmp;
  1016. }
  1017. }
  1018. /*-
  1019. * point_add calculates (x1, y1, z1) + (x2, y2, z2)
  1020. *
  1021. * The method is taken from
  1022. * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
  1023. * adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
  1024. *
  1025. * This function includes a branch for checking whether the two input points
  1026. * are equal (while not equal to the point at infinity). See comment below
  1027. * on constant-time.
  1028. */
  1029. static void point_add(felem x3, felem y3, felem z3,
  1030. const felem x1, const felem y1, const felem z1,
  1031. const int mixed, const felem x2, const felem y2,
  1032. const felem z2)
  1033. {
  1034. felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
  1035. largefelem tmp, tmp2;
  1036. limb x_equal, y_equal, z1_is_zero, z2_is_zero;
  1037. limb points_equal;
  1038. z1_is_zero = felem_is_zero(z1);
  1039. z2_is_zero = felem_is_zero(z2);
  1040. /* ftmp = z1z1 = z1**2 */
  1041. felem_square(tmp, z1);
  1042. felem_reduce(ftmp, tmp);
  1043. if (!mixed) {
  1044. /* ftmp2 = z2z2 = z2**2 */
  1045. felem_square(tmp, z2);
  1046. felem_reduce(ftmp2, tmp);
  1047. /* u1 = ftmp3 = x1*z2z2 */
  1048. felem_mul(tmp, x1, ftmp2);
  1049. felem_reduce(ftmp3, tmp);
  1050. /* ftmp5 = z1 + z2 */
  1051. felem_assign(ftmp5, z1);
  1052. felem_sum64(ftmp5, z2);
  1053. /* ftmp5[i] < 2^61 */
  1054. /* ftmp5 = (z1 + z2)**2 - z1z1 - z2z2 = 2*z1z2 */
  1055. felem_square(tmp, ftmp5);
  1056. /* tmp[i] < 17*2^122 */
  1057. felem_diff_128_64(tmp, ftmp);
  1058. /* tmp[i] < 17*2^122 + 2^63 */
  1059. felem_diff_128_64(tmp, ftmp2);
  1060. /* tmp[i] < 17*2^122 + 2^64 */
  1061. felem_reduce(ftmp5, tmp);
  1062. /* ftmp2 = z2 * z2z2 */
  1063. felem_mul(tmp, ftmp2, z2);
  1064. felem_reduce(ftmp2, tmp);
  1065. /* s1 = ftmp6 = y1 * z2**3 */
  1066. felem_mul(tmp, y1, ftmp2);
  1067. felem_reduce(ftmp6, tmp);
  1068. } else {
  1069. /*
  1070. * We'll assume z2 = 1 (special case z2 = 0 is handled later)
  1071. */
  1072. /* u1 = ftmp3 = x1*z2z2 */
  1073. felem_assign(ftmp3, x1);
  1074. /* ftmp5 = 2*z1z2 */
  1075. felem_scalar(ftmp5, z1, 2);
  1076. /* s1 = ftmp6 = y1 * z2**3 */
  1077. felem_assign(ftmp6, y1);
  1078. }
  1079. /* u2 = x2*z1z1 */
  1080. felem_mul(tmp, x2, ftmp);
  1081. /* tmp[i] < 17*2^120 */
  1082. /* h = ftmp4 = u2 - u1 */
  1083. felem_diff_128_64(tmp, ftmp3);
  1084. /* tmp[i] < 17*2^120 + 2^63 */
  1085. felem_reduce(ftmp4, tmp);
  1086. x_equal = felem_is_zero(ftmp4);
  1087. /* z_out = ftmp5 * h */
  1088. felem_mul(tmp, ftmp5, ftmp4);
  1089. felem_reduce(z_out, tmp);
  1090. /* ftmp = z1 * z1z1 */
  1091. felem_mul(tmp, ftmp, z1);
  1092. felem_reduce(ftmp, tmp);
  1093. /* s2 = tmp = y2 * z1**3 */
  1094. felem_mul(tmp, y2, ftmp);
  1095. /* tmp[i] < 17*2^120 */
  1096. /* r = ftmp5 = (s2 - s1)*2 */
  1097. felem_diff_128_64(tmp, ftmp6);
  1098. /* tmp[i] < 17*2^120 + 2^63 */
  1099. felem_reduce(ftmp5, tmp);
  1100. y_equal = felem_is_zero(ftmp5);
  1101. felem_scalar64(ftmp5, 2);
  1102. /* ftmp5[i] < 2^61 */
  1103. /*
  1104. * The formulae are incorrect if the points are equal, in affine coordinates
  1105. * (X_1, Y_1) == (X_2, Y_2), so we check for this and do doubling if this
  1106. * happens.
  1107. *
  1108. * We use bitwise operations to avoid potential side-channels introduced by
  1109. * the short-circuiting behaviour of boolean operators.
  1110. *
  1111. * The special case of either point being the point at infinity (z1 and/or
  1112. * z2 are zero), is handled separately later on in this function, so we
  1113. * avoid jumping to point_double here in those special cases.
  1114. *
  1115. * Notice the comment below on the implications of this branching for timing
  1116. * leaks and why it is considered practically irrelevant.
  1117. */
  1118. points_equal = (x_equal & y_equal & (~z1_is_zero) & (~z2_is_zero));
  1119. if (points_equal) {
  1120. /*
  1121. * This is obviously not constant-time but it will almost-never happen
  1122. * for ECDH / ECDSA. The case where it can happen is during scalar-mult
  1123. * where the intermediate value gets very close to the group order.
  1124. * Since |ec_GFp_nistp_recode_scalar_bits| produces signed digits for
  1125. * the scalar, it's possible for the intermediate value to be a small
  1126. * negative multiple of the base point, and for the final signed digit
  1127. * to be the same value. We believe that this only occurs for the scalar
  1128. * 1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  1129. * ffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb
  1130. * 71e913863f7, in that case the penultimate intermediate is -9G and
  1131. * the final digit is also -9G. Since this only happens for a single
  1132. * scalar, the timing leak is irrelevant. (Any attacker who wanted to
  1133. * check whether a secret scalar was that exact value, can already do
  1134. * so.)
  1135. */
  1136. point_double(x3, y3, z3, x1, y1, z1);
  1137. return;
  1138. }
  1139. /* I = ftmp = (2h)**2 */
  1140. felem_assign(ftmp, ftmp4);
  1141. felem_scalar64(ftmp, 2);
  1142. /* ftmp[i] < 2^61 */
  1143. felem_square(tmp, ftmp);
  1144. /* tmp[i] < 17*2^122 */
  1145. felem_reduce(ftmp, tmp);
  1146. /* J = ftmp2 = h * I */
  1147. felem_mul(tmp, ftmp4, ftmp);
  1148. felem_reduce(ftmp2, tmp);
  1149. /* V = ftmp4 = U1 * I */
  1150. felem_mul(tmp, ftmp3, ftmp);
  1151. felem_reduce(ftmp4, tmp);
  1152. /* x_out = r**2 - J - 2V */
  1153. felem_square(tmp, ftmp5);
  1154. /* tmp[i] < 17*2^122 */
  1155. felem_diff_128_64(tmp, ftmp2);
  1156. /* tmp[i] < 17*2^122 + 2^63 */
  1157. felem_assign(ftmp3, ftmp4);
  1158. felem_scalar64(ftmp4, 2);
  1159. /* ftmp4[i] < 2^61 */
  1160. felem_diff_128_64(tmp, ftmp4);
  1161. /* tmp[i] < 17*2^122 + 2^64 */
  1162. felem_reduce(x_out, tmp);
  1163. /* y_out = r(V-x_out) - 2 * s1 * J */
  1164. felem_diff64(ftmp3, x_out);
  1165. /*
  1166. * ftmp3[i] < 2^60 + 2^60 = 2^61
  1167. */
  1168. felem_mul(tmp, ftmp5, ftmp3);
  1169. /* tmp[i] < 17*2^122 */
  1170. felem_mul(tmp2, ftmp6, ftmp2);
  1171. /* tmp2[i] < 17*2^120 */
  1172. felem_scalar128(tmp2, 2);
  1173. /* tmp2[i] < 17*2^121 */
  1174. felem_diff128(tmp, tmp2);
  1175. /*-
  1176. * tmp[i] < 2^127 - 2^69 + 17*2^122
  1177. * = 2^126 - 2^122 - 2^6 - 2^2 - 1
  1178. * < 2^127
  1179. */
  1180. felem_reduce(y_out, tmp);
  1181. copy_conditional(x_out, x2, z1_is_zero);
  1182. copy_conditional(x_out, x1, z2_is_zero);
  1183. copy_conditional(y_out, y2, z1_is_zero);
  1184. copy_conditional(y_out, y1, z2_is_zero);
  1185. copy_conditional(z_out, z2, z1_is_zero);
  1186. copy_conditional(z_out, z1, z2_is_zero);
  1187. felem_assign(x3, x_out);
  1188. felem_assign(y3, y_out);
  1189. felem_assign(z3, z_out);
  1190. }
  1191. /*-
  1192. * Base point pre computation
  1193. * --------------------------
  1194. *
  1195. * Two different sorts of precomputed tables are used in the following code.
  1196. * Each contain various points on the curve, where each point is three field
  1197. * elements (x, y, z).
  1198. *
  1199. * For the base point table, z is usually 1 (0 for the point at infinity).
  1200. * This table has 16 elements:
  1201. * index | bits | point
  1202. * ------+---------+------------------------------
  1203. * 0 | 0 0 0 0 | 0G
  1204. * 1 | 0 0 0 1 | 1G
  1205. * 2 | 0 0 1 0 | 2^130G
  1206. * 3 | 0 0 1 1 | (2^130 + 1)G
  1207. * 4 | 0 1 0 0 | 2^260G
  1208. * 5 | 0 1 0 1 | (2^260 + 1)G
  1209. * 6 | 0 1 1 0 | (2^260 + 2^130)G
  1210. * 7 | 0 1 1 1 | (2^260 + 2^130 + 1)G
  1211. * 8 | 1 0 0 0 | 2^390G
  1212. * 9 | 1 0 0 1 | (2^390 + 1)G
  1213. * 10 | 1 0 1 0 | (2^390 + 2^130)G
  1214. * 11 | 1 0 1 1 | (2^390 + 2^130 + 1)G
  1215. * 12 | 1 1 0 0 | (2^390 + 2^260)G
  1216. * 13 | 1 1 0 1 | (2^390 + 2^260 + 1)G
  1217. * 14 | 1 1 1 0 | (2^390 + 2^260 + 2^130)G
  1218. * 15 | 1 1 1 1 | (2^390 + 2^260 + 2^130 + 1)G
  1219. *
  1220. * The reason for this is so that we can clock bits into four different
  1221. * locations when doing simple scalar multiplies against the base point.
  1222. *
  1223. * Tables for other points have table[i] = iG for i in 0 .. 16. */
  1224. /* gmul is the table of precomputed base points */
  1225. static const felem gmul[16][3] = {
  1226. {{0, 0, 0, 0, 0, 0, 0, 0, 0},
  1227. {0, 0, 0, 0, 0, 0, 0, 0, 0},
  1228. {0, 0, 0, 0, 0, 0, 0, 0, 0}},
  1229. {{0x017e7e31c2e5bd66, 0x022cf0615a90a6fe, 0x00127a2ffa8de334,
  1230. 0x01dfbf9d64a3f877, 0x006b4d3dbaa14b5e, 0x014fed487e0a2bd8,
  1231. 0x015b4429c6481390, 0x03a73678fb2d988e, 0x00c6858e06b70404},
  1232. {0x00be94769fd16650, 0x031c21a89cb09022, 0x039013fad0761353,
  1233. 0x02657bd099031542, 0x03273e662c97ee72, 0x01e6d11a05ebef45,
  1234. 0x03d1bd998f544495, 0x03001172297ed0b1, 0x011839296a789a3b},
  1235. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1236. {{0x0373faacbc875bae, 0x00f325023721c671, 0x00f666fd3dbde5ad,
  1237. 0x01a6932363f88ea7, 0x01fc6d9e13f9c47b, 0x03bcbffc2bbf734e,
  1238. 0x013ee3c3647f3a92, 0x029409fefe75d07d, 0x00ef9199963d85e5},
  1239. {0x011173743ad5b178, 0x02499c7c21bf7d46, 0x035beaeabb8b1a58,
  1240. 0x00f989c4752ea0a3, 0x0101e1de48a9c1a3, 0x01a20076be28ba6c,
  1241. 0x02f8052e5eb2de95, 0x01bfe8f82dea117c, 0x0160074d3c36ddb7},
  1242. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1243. {{0x012f3fc373393b3b, 0x03d3d6172f1419fa, 0x02adc943c0b86873,
  1244. 0x00d475584177952b, 0x012a4d1673750ee2, 0x00512517a0f13b0c,
  1245. 0x02b184671a7b1734, 0x0315b84236f1a50a, 0x00a4afc472edbdb9},
  1246. {0x00152a7077f385c4, 0x03044007d8d1c2ee, 0x0065829d61d52b52,
  1247. 0x00494ff6b6631d0d, 0x00a11d94d5f06bcf, 0x02d2f89474d9282e,
  1248. 0x0241c5727c06eeb9, 0x0386928710fbdb9d, 0x01f883f727b0dfbe},
  1249. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1250. {{0x019b0c3c9185544d, 0x006243a37c9d97db, 0x02ee3cbe030a2ad2,
  1251. 0x00cfdd946bb51e0d, 0x0271c00932606b91, 0x03f817d1ec68c561,
  1252. 0x03f37009806a369c, 0x03c1f30baf184fd5, 0x01091022d6d2f065},
  1253. {0x0292c583514c45ed, 0x0316fca51f9a286c, 0x00300af507c1489a,
  1254. 0x0295f69008298cf1, 0x02c0ed8274943d7b, 0x016509b9b47a431e,
  1255. 0x02bc9de9634868ce, 0x005b34929bffcb09, 0x000c1a0121681524},
  1256. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1257. {{0x0286abc0292fb9f2, 0x02665eee9805b3f7, 0x01ed7455f17f26d6,
  1258. 0x0346355b83175d13, 0x006284944cd0a097, 0x0191895bcdec5e51,
  1259. 0x02e288370afda7d9, 0x03b22312bfefa67a, 0x01d104d3fc0613fe},
  1260. {0x0092421a12f7e47f, 0x0077a83fa373c501, 0x03bd25c5f696bd0d,
  1261. 0x035c41e4d5459761, 0x01ca0d1742b24f53, 0x00aaab27863a509c,
  1262. 0x018b6de47df73917, 0x025c0b771705cd01, 0x01fd51d566d760a7},
  1263. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1264. {{0x01dd92ff6b0d1dbd, 0x039c5e2e8f8afa69, 0x0261ed13242c3b27,
  1265. 0x0382c6e67026e6a0, 0x01d60b10be2089f9, 0x03c15f3dce86723f,
  1266. 0x03c764a32d2a062d, 0x017307eac0fad056, 0x018207c0b96c5256},
  1267. {0x0196a16d60e13154, 0x03e6ce74c0267030, 0x00ddbf2b4e52a5aa,
  1268. 0x012738241bbf31c8, 0x00ebe8dc04685a28, 0x024c2ad6d380d4a2,
  1269. 0x035ee062a6e62d0e, 0x0029ed74af7d3a0f, 0x00eef32aec142ebd},
  1270. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1271. {{0x00c31ec398993b39, 0x03a9f45bcda68253, 0x00ac733c24c70890,
  1272. 0x00872b111401ff01, 0x01d178c23195eafb, 0x03bca2c816b87f74,
  1273. 0x0261a9af46fbad7a, 0x0324b2a8dd3d28f9, 0x00918121d8f24e23},
  1274. {0x032bc8c1ca983cd7, 0x00d869dfb08fc8c6, 0x01693cb61fce1516,
  1275. 0x012a5ea68f4e88a8, 0x010869cab88d7ae3, 0x009081ad277ceee1,
  1276. 0x033a77166d064cdc, 0x03955235a1fb3a95, 0x01251a4a9b25b65e},
  1277. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1278. {{0x00148a3a1b27f40b, 0x0123186df1b31fdc, 0x00026e7beaad34ce,
  1279. 0x01db446ac1d3dbba, 0x0299c1a33437eaec, 0x024540610183cbb7,
  1280. 0x0173bb0e9ce92e46, 0x02b937e43921214b, 0x01ab0436a9bf01b5},
  1281. {0x0383381640d46948, 0x008dacbf0e7f330f, 0x03602122bcc3f318,
  1282. 0x01ee596b200620d6, 0x03bd0585fda430b3, 0x014aed77fd123a83,
  1283. 0x005ace749e52f742, 0x0390fe041da2b842, 0x0189a8ceb3299242},
  1284. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1285. {{0x012a19d6b3282473, 0x00c0915918b423ce, 0x023a954eb94405ae,
  1286. 0x00529f692be26158, 0x0289fa1b6fa4b2aa, 0x0198ae4ceea346ef,
  1287. 0x0047d8cdfbdedd49, 0x00cc8c8953f0f6b8, 0x001424abbff49203},
  1288. {0x0256732a1115a03a, 0x0351bc38665c6733, 0x03f7b950fb4a6447,
  1289. 0x000afffa94c22155, 0x025763d0a4dab540, 0x000511e92d4fc283,
  1290. 0x030a7e9eda0ee96c, 0x004c3cd93a28bf0a, 0x017edb3a8719217f},
  1291. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1292. {{0x011de5675a88e673, 0x031d7d0f5e567fbe, 0x0016b2062c970ae5,
  1293. 0x03f4a2be49d90aa7, 0x03cef0bd13822866, 0x03f0923dcf774a6c,
  1294. 0x0284bebc4f322f72, 0x016ab2645302bb2c, 0x01793f95dace0e2a},
  1295. {0x010646e13527a28f, 0x01ca1babd59dc5e7, 0x01afedfd9a5595df,
  1296. 0x01f15785212ea6b1, 0x0324e5d64f6ae3f4, 0x02d680f526d00645,
  1297. 0x0127920fadf627a7, 0x03b383f75df4f684, 0x0089e0057e783b0a},
  1298. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1299. {{0x00f334b9eb3c26c6, 0x0298fdaa98568dce, 0x01c2d24843a82292,
  1300. 0x020bcb24fa1b0711, 0x02cbdb3d2b1875e6, 0x0014907598f89422,
  1301. 0x03abe3aa43b26664, 0x02cbf47f720bc168, 0x0133b5e73014b79b},
  1302. {0x034aab5dab05779d, 0x00cdc5d71fee9abb, 0x0399f16bd4bd9d30,
  1303. 0x03582fa592d82647, 0x02be1cdfb775b0e9, 0x0034f7cea32e94cb,
  1304. 0x0335a7f08f56f286, 0x03b707e9565d1c8b, 0x0015c946ea5b614f},
  1305. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1306. {{0x024676f6cff72255, 0x00d14625cac96378, 0x00532b6008bc3767,
  1307. 0x01fc16721b985322, 0x023355ea1b091668, 0x029de7afdc0317c3,
  1308. 0x02fc8a7ca2da037c, 0x02de1217d74a6f30, 0x013f7173175b73bf},
  1309. {0x0344913f441490b5, 0x0200f9e272b61eca, 0x0258a246b1dd55d2,
  1310. 0x03753db9ea496f36, 0x025e02937a09c5ef, 0x030cbd3d14012692,
  1311. 0x01793a67e70dc72a, 0x03ec1d37048a662e, 0x006550f700c32a8d},
  1312. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1313. {{0x00d3f48a347eba27, 0x008e636649b61bd8, 0x00d3b93716778fb3,
  1314. 0x004d1915757bd209, 0x019d5311a3da44e0, 0x016d1afcbbe6aade,
  1315. 0x0241bf5f73265616, 0x0384672e5d50d39b, 0x005009fee522b684},
  1316. {0x029b4fab064435fe, 0x018868ee095bbb07, 0x01ea3d6936cc92b8,
  1317. 0x000608b00f78a2f3, 0x02db911073d1c20f, 0x018205938470100a,
  1318. 0x01f1e4964cbe6ff2, 0x021a19a29eed4663, 0x01414485f42afa81},
  1319. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1320. {{0x01612b3a17f63e34, 0x03813992885428e6, 0x022b3c215b5a9608,
  1321. 0x029b4057e19f2fcb, 0x0384059a587af7e6, 0x02d6400ace6fe610,
  1322. 0x029354d896e8e331, 0x00c047ee6dfba65e, 0x0037720542e9d49d},
  1323. {0x02ce9eed7c5e9278, 0x0374ed703e79643b, 0x01316c54c4072006,
  1324. 0x005aaa09054b2ee8, 0x002824000c840d57, 0x03d4eba24771ed86,
  1325. 0x0189c50aabc3bdae, 0x0338c01541e15510, 0x00466d56e38eed42},
  1326. {1, 0, 0, 0, 0, 0, 0, 0, 0}},
  1327. {{0x007efd8330ad8bd6, 0x02465ed48047710b, 0x0034c6606b215e0c,
  1328. 0x016ae30c53cbf839, 0x01fa17bd37161216, 0x018ead4e61ce8ab9,
  1329. 0x005482ed5f5dee46, 0x037543755bba1d7f, 0x005e5ac7e70a9d0f},
  1330. {0x0117e1bb2fdcb2a2, 0x03deea36249f40c4, 0x028d09b4a6246cb7,
  1331. 0x03524b8855bcf756, 0x023d7d109d5ceb58, 0x0178e43e3223ef9c,
  1332. 0x0154536a0c6e966a, 0x037964d1286ee9fe, 0x0199bcd90e125055},
  1333. {1, 0, 0, 0, 0, 0, 0, 0, 0}}
  1334. };
  1335. /*
  1336. * select_point selects the |idx|th point from a precomputation table and
  1337. * copies it to out.
  1338. */
  1339. /* pre_comp below is of the size provided in |size| */
  1340. static void select_point(const limb idx, unsigned int size,
  1341. const felem pre_comp[][3], felem out[3])
  1342. {
  1343. unsigned i, j;
  1344. limb *outlimbs = &out[0][0];
  1345. memset(out, 0, sizeof(*out) * 3);
  1346. for (i = 0; i < size; i++) {
  1347. const limb *inlimbs = &pre_comp[i][0][0];
  1348. limb mask = i ^ idx;
  1349. mask |= mask >> 4;
  1350. mask |= mask >> 2;
  1351. mask |= mask >> 1;
  1352. mask &= 1;
  1353. mask--;
  1354. for (j = 0; j < NLIMBS * 3; j++)
  1355. outlimbs[j] |= inlimbs[j] & mask;
  1356. }
  1357. }
  1358. /* get_bit returns the |i|th bit in |in| */
  1359. static char get_bit(const felem_bytearray in, int i)
  1360. {
  1361. if (i < 0)
  1362. return 0;
  1363. return (in[i >> 3] >> (i & 7)) & 1;
  1364. }
  1365. /*
  1366. * Interleaved point multiplication using precomputed point multiples: The
  1367. * small point multiples 0*P, 1*P, ..., 16*P are in pre_comp[], the scalars
  1368. * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
  1369. * generator, using certain (large) precomputed multiples in g_pre_comp.
  1370. * Output point (X, Y, Z) is stored in x_out, y_out, z_out
  1371. */
  1372. static void batch_mul(felem x_out, felem y_out, felem z_out,
  1373. const felem_bytearray scalars[],
  1374. const unsigned num_points, const u8 *g_scalar,
  1375. const int mixed, const felem pre_comp[][17][3],
  1376. const felem g_pre_comp[16][3])
  1377. {
  1378. int i, skip;
  1379. unsigned num, gen_mul = (g_scalar != NULL);
  1380. felem nq[3], tmp[4];
  1381. limb bits;
  1382. u8 sign, digit;
  1383. /* set nq to the point at infinity */
  1384. memset(nq, 0, sizeof(nq));
  1385. /*
  1386. * Loop over all scalars msb-to-lsb, interleaving additions of multiples
  1387. * of the generator (last quarter of rounds) and additions of other
  1388. * points multiples (every 5th round).
  1389. */
  1390. skip = 1; /* save two point operations in the first
  1391. * round */
  1392. for (i = (num_points ? 520 : 130); i >= 0; --i) {
  1393. /* double */
  1394. if (!skip)
  1395. point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
  1396. /* add multiples of the generator */
  1397. if (gen_mul && (i <= 130)) {
  1398. bits = get_bit(g_scalar, i + 390) << 3;
  1399. if (i < 130) {
  1400. bits |= get_bit(g_scalar, i + 260) << 2;
  1401. bits |= get_bit(g_scalar, i + 130) << 1;
  1402. bits |= get_bit(g_scalar, i);
  1403. }
  1404. /* select the point to add, in constant time */
  1405. select_point(bits, 16, g_pre_comp, tmp);
  1406. if (!skip) {
  1407. /* The 1 argument below is for "mixed" */
  1408. point_add(nq[0], nq[1], nq[2],
  1409. nq[0], nq[1], nq[2], 1, tmp[0], tmp[1], tmp[2]);
  1410. } else {
  1411. memcpy(nq, tmp, 3 * sizeof(felem));
  1412. skip = 0;
  1413. }
  1414. }
  1415. /* do other additions every 5 doublings */
  1416. if (num_points && (i % 5 == 0)) {
  1417. /* loop over all scalars */
  1418. for (num = 0; num < num_points; ++num) {
  1419. bits = get_bit(scalars[num], i + 4) << 5;
  1420. bits |= get_bit(scalars[num], i + 3) << 4;
  1421. bits |= get_bit(scalars[num], i + 2) << 3;
  1422. bits |= get_bit(scalars[num], i + 1) << 2;
  1423. bits |= get_bit(scalars[num], i) << 1;
  1424. bits |= get_bit(scalars[num], i - 1);
  1425. ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
  1426. /*
  1427. * select the point to add or subtract, in constant time
  1428. */
  1429. select_point(digit, 17, pre_comp[num], tmp);
  1430. felem_neg(tmp[3], tmp[1]); /* (X, -Y, Z) is the negative
  1431. * point */
  1432. copy_conditional(tmp[1], tmp[3], (-(limb) sign));
  1433. if (!skip) {
  1434. point_add(nq[0], nq[1], nq[2],
  1435. nq[0], nq[1], nq[2],
  1436. mixed, tmp[0], tmp[1], tmp[2]);
  1437. } else {
  1438. memcpy(nq, tmp, 3 * sizeof(felem));
  1439. skip = 0;
  1440. }
  1441. }
  1442. }
  1443. }
  1444. felem_assign(x_out, nq[0]);
  1445. felem_assign(y_out, nq[1]);
  1446. felem_assign(z_out, nq[2]);
  1447. }
  1448. /* Precomputation for the group generator. */
  1449. struct nistp521_pre_comp_st {
  1450. felem g_pre_comp[16][3];
  1451. CRYPTO_REF_COUNT references;
  1452. CRYPTO_RWLOCK *lock;
  1453. };
  1454. const EC_METHOD *EC_GFp_nistp521_method(void)
  1455. {
  1456. static const EC_METHOD ret = {
  1457. EC_FLAGS_DEFAULT_OCT,
  1458. NID_X9_62_prime_field,
  1459. ec_GFp_nistp521_group_init,
  1460. ec_GFp_simple_group_finish,
  1461. ec_GFp_simple_group_clear_finish,
  1462. ec_GFp_nist_group_copy,
  1463. ec_GFp_nistp521_group_set_curve,
  1464. ec_GFp_simple_group_get_curve,
  1465. ec_GFp_simple_group_get_degree,
  1466. ec_group_simple_order_bits,
  1467. ec_GFp_simple_group_check_discriminant,
  1468. ec_GFp_simple_point_init,
  1469. ec_GFp_simple_point_finish,
  1470. ec_GFp_simple_point_clear_finish,
  1471. ec_GFp_simple_point_copy,
  1472. ec_GFp_simple_point_set_to_infinity,
  1473. ec_GFp_simple_point_set_affine_coordinates,
  1474. ec_GFp_nistp521_point_get_affine_coordinates,
  1475. 0 /* point_set_compressed_coordinates */ ,
  1476. 0 /* point2oct */ ,
  1477. 0 /* oct2point */ ,
  1478. ec_GFp_simple_add,
  1479. ec_GFp_simple_dbl,
  1480. ec_GFp_simple_invert,
  1481. ec_GFp_simple_is_at_infinity,
  1482. ec_GFp_simple_is_on_curve,
  1483. ec_GFp_simple_cmp,
  1484. ec_GFp_simple_make_affine,
  1485. ec_GFp_simple_points_make_affine,
  1486. ec_GFp_nistp521_points_mul,
  1487. ec_GFp_nistp521_precompute_mult,
  1488. ec_GFp_nistp521_have_precompute_mult,
  1489. ec_GFp_nist_field_mul,
  1490. ec_GFp_nist_field_sqr,
  1491. 0 /* field_div */ ,
  1492. ec_GFp_simple_field_inv,
  1493. 0 /* field_encode */ ,
  1494. 0 /* field_decode */ ,
  1495. 0, /* field_set_to_one */
  1496. ec_key_simple_priv2oct,
  1497. ec_key_simple_oct2priv,
  1498. 0, /* set private */
  1499. ec_key_simple_generate_key,
  1500. ec_key_simple_check_key,
  1501. ec_key_simple_generate_public_key,
  1502. 0, /* keycopy */
  1503. 0, /* keyfinish */
  1504. ecdh_simple_compute_key,
  1505. ecdsa_simple_sign_setup,
  1506. ecdsa_simple_sign_sig,
  1507. ecdsa_simple_verify_sig,
  1508. 0, /* field_inverse_mod_ord */
  1509. 0, /* blind_coordinates */
  1510. 0, /* ladder_pre */
  1511. 0, /* ladder_step */
  1512. 0 /* ladder_post */
  1513. };
  1514. return &ret;
  1515. }
  1516. /******************************************************************************/
  1517. /*
  1518. * FUNCTIONS TO MANAGE PRECOMPUTATION
  1519. */
  1520. static NISTP521_PRE_COMP *nistp521_pre_comp_new(void)
  1521. {
  1522. NISTP521_PRE_COMP *ret = OPENSSL_zalloc(sizeof(*ret));
  1523. if (ret == NULL) {
  1524. ECerr(EC_F_NISTP521_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  1525. return ret;
  1526. }
  1527. ret->references = 1;
  1528. ret->lock = CRYPTO_THREAD_lock_new();
  1529. if (ret->lock == NULL) {
  1530. ECerr(EC_F_NISTP521_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  1531. OPENSSL_free(ret);
  1532. return NULL;
  1533. }
  1534. return ret;
  1535. }
  1536. NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *p)
  1537. {
  1538. int i;
  1539. if (p != NULL)
  1540. CRYPTO_UP_REF(&p->references, &i, p->lock);
  1541. return p;
  1542. }
  1543. void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *p)
  1544. {
  1545. int i;
  1546. if (p == NULL)
  1547. return;
  1548. CRYPTO_DOWN_REF(&p->references, &i, p->lock);
  1549. REF_PRINT_COUNT("EC_nistp521", x);
  1550. if (i > 0)
  1551. return;
  1552. REF_ASSERT_ISNT(i < 0);
  1553. CRYPTO_THREAD_lock_free(p->lock);
  1554. OPENSSL_free(p);
  1555. }
  1556. /******************************************************************************/
  1557. /*
  1558. * OPENSSL EC_METHOD FUNCTIONS
  1559. */
  1560. int ec_GFp_nistp521_group_init(EC_GROUP *group)
  1561. {
  1562. int ret;
  1563. ret = ec_GFp_simple_group_init(group);
  1564. group->a_is_minus3 = 1;
  1565. return ret;
  1566. }
  1567. int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  1568. const BIGNUM *a, const BIGNUM *b,
  1569. BN_CTX *ctx)
  1570. {
  1571. int ret = 0;
  1572. BIGNUM *curve_p, *curve_a, *curve_b;
  1573. #ifndef FIPS_MODULE
  1574. BN_CTX *new_ctx = NULL;
  1575. if (ctx == NULL)
  1576. ctx = new_ctx = BN_CTX_new();
  1577. #endif
  1578. if (ctx == NULL)
  1579. return 0;
  1580. BN_CTX_start(ctx);
  1581. curve_p = BN_CTX_get(ctx);
  1582. curve_a = BN_CTX_get(ctx);
  1583. curve_b = BN_CTX_get(ctx);
  1584. if (curve_b == NULL)
  1585. goto err;
  1586. BN_bin2bn(nistp521_curve_params[0], sizeof(felem_bytearray), curve_p);
  1587. BN_bin2bn(nistp521_curve_params[1], sizeof(felem_bytearray), curve_a);
  1588. BN_bin2bn(nistp521_curve_params[2], sizeof(felem_bytearray), curve_b);
  1589. if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
  1590. ECerr(EC_F_EC_GFP_NISTP521_GROUP_SET_CURVE,
  1591. EC_R_WRONG_CURVE_PARAMETERS);
  1592. goto err;
  1593. }
  1594. group->field_mod_func = BN_nist_mod_521;
  1595. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  1596. err:
  1597. BN_CTX_end(ctx);
  1598. #ifndef FIPS_MODULE
  1599. BN_CTX_free(new_ctx);
  1600. #endif
  1601. return ret;
  1602. }
  1603. /*
  1604. * Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
  1605. * (X/Z^2, Y/Z^3)
  1606. */
  1607. int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
  1608. const EC_POINT *point,
  1609. BIGNUM *x, BIGNUM *y,
  1610. BN_CTX *ctx)
  1611. {
  1612. felem z1, z2, x_in, y_in, x_out, y_out;
  1613. largefelem tmp;
  1614. if (EC_POINT_is_at_infinity(group, point)) {
  1615. ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
  1616. EC_R_POINT_AT_INFINITY);
  1617. return 0;
  1618. }
  1619. if ((!BN_to_felem(x_in, point->X)) || (!BN_to_felem(y_in, point->Y)) ||
  1620. (!BN_to_felem(z1, point->Z)))
  1621. return 0;
  1622. felem_inv(z2, z1);
  1623. felem_square(tmp, z2);
  1624. felem_reduce(z1, tmp);
  1625. felem_mul(tmp, x_in, z1);
  1626. felem_reduce(x_in, tmp);
  1627. felem_contract(x_out, x_in);
  1628. if (x != NULL) {
  1629. if (!felem_to_BN(x, x_out)) {
  1630. ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
  1631. ERR_R_BN_LIB);
  1632. return 0;
  1633. }
  1634. }
  1635. felem_mul(tmp, z1, z2);
  1636. felem_reduce(z1, tmp);
  1637. felem_mul(tmp, y_in, z1);
  1638. felem_reduce(y_in, tmp);
  1639. felem_contract(y_out, y_in);
  1640. if (y != NULL) {
  1641. if (!felem_to_BN(y, y_out)) {
  1642. ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
  1643. ERR_R_BN_LIB);
  1644. return 0;
  1645. }
  1646. }
  1647. return 1;
  1648. }
  1649. /* points below is of size |num|, and tmp_felems is of size |num+1/ */
  1650. static void make_points_affine(size_t num, felem points[][3],
  1651. felem tmp_felems[])
  1652. {
  1653. /*
  1654. * Runs in constant time, unless an input is the point at infinity (which
  1655. * normally shouldn't happen).
  1656. */
  1657. ec_GFp_nistp_points_make_affine_internal(num,
  1658. points,
  1659. sizeof(felem),
  1660. tmp_felems,
  1661. (void (*)(void *))felem_one,
  1662. felem_is_zero_int,
  1663. (void (*)(void *, const void *))
  1664. felem_assign,
  1665. (void (*)(void *, const void *))
  1666. felem_square_reduce, (void (*)
  1667. (void *,
  1668. const void
  1669. *,
  1670. const void
  1671. *))
  1672. felem_mul_reduce,
  1673. (void (*)(void *, const void *))
  1674. felem_inv,
  1675. (void (*)(void *, const void *))
  1676. felem_contract);
  1677. }
  1678. /*
  1679. * Computes scalar*generator + \sum scalars[i]*points[i], ignoring NULL
  1680. * values Result is stored in r (r can equal one of the inputs).
  1681. */
  1682. int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
  1683. const BIGNUM *scalar, size_t num,
  1684. const EC_POINT *points[],
  1685. const BIGNUM *scalars[], BN_CTX *ctx)
  1686. {
  1687. int ret = 0;
  1688. int j;
  1689. int mixed = 0;
  1690. BIGNUM *x, *y, *z, *tmp_scalar;
  1691. felem_bytearray g_secret;
  1692. felem_bytearray *secrets = NULL;
  1693. felem (*pre_comp)[17][3] = NULL;
  1694. felem *tmp_felems = NULL;
  1695. unsigned i;
  1696. int num_bytes;
  1697. int have_pre_comp = 0;
  1698. size_t num_points = num;
  1699. felem x_in, y_in, z_in, x_out, y_out, z_out;
  1700. NISTP521_PRE_COMP *pre = NULL;
  1701. felem(*g_pre_comp)[3] = NULL;
  1702. EC_POINT *generator = NULL;
  1703. const EC_POINT *p = NULL;
  1704. const BIGNUM *p_scalar = NULL;
  1705. BN_CTX_start(ctx);
  1706. x = BN_CTX_get(ctx);
  1707. y = BN_CTX_get(ctx);
  1708. z = BN_CTX_get(ctx);
  1709. tmp_scalar = BN_CTX_get(ctx);
  1710. if (tmp_scalar == NULL)
  1711. goto err;
  1712. if (scalar != NULL) {
  1713. pre = group->pre_comp.nistp521;
  1714. if (pre)
  1715. /* we have precomputation, try to use it */
  1716. g_pre_comp = &pre->g_pre_comp[0];
  1717. else
  1718. /* try to use the standard precomputation */
  1719. g_pre_comp = (felem(*)[3]) gmul;
  1720. generator = EC_POINT_new(group);
  1721. if (generator == NULL)
  1722. goto err;
  1723. /* get the generator from precomputation */
  1724. if (!felem_to_BN(x, g_pre_comp[1][0]) ||
  1725. !felem_to_BN(y, g_pre_comp[1][1]) ||
  1726. !felem_to_BN(z, g_pre_comp[1][2])) {
  1727. ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
  1728. goto err;
  1729. }
  1730. if (!ec_GFp_simple_set_Jprojective_coordinates_GFp(group, generator, x,
  1731. y, z, ctx))
  1732. goto err;
  1733. if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
  1734. /* precomputation matches generator */
  1735. have_pre_comp = 1;
  1736. else
  1737. /*
  1738. * we don't have valid precomputation: treat the generator as a
  1739. * random point
  1740. */
  1741. num_points++;
  1742. }
  1743. if (num_points > 0) {
  1744. if (num_points >= 2) {
  1745. /*
  1746. * unless we precompute multiples for just one point, converting
  1747. * those into affine form is time well spent
  1748. */
  1749. mixed = 1;
  1750. }
  1751. secrets = OPENSSL_zalloc(sizeof(*secrets) * num_points);
  1752. pre_comp = OPENSSL_zalloc(sizeof(*pre_comp) * num_points);
  1753. if (mixed)
  1754. tmp_felems =
  1755. OPENSSL_malloc(sizeof(*tmp_felems) * (num_points * 17 + 1));
  1756. if ((secrets == NULL) || (pre_comp == NULL)
  1757. || (mixed && (tmp_felems == NULL))) {
  1758. ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  1759. goto err;
  1760. }
  1761. /*
  1762. * we treat NULL scalars as 0, and NULL points as points at infinity,
  1763. * i.e., they contribute nothing to the linear combination
  1764. */
  1765. for (i = 0; i < num_points; ++i) {
  1766. if (i == num) {
  1767. /*
  1768. * we didn't have a valid precomputation, so we pick the
  1769. * generator
  1770. */
  1771. p = EC_GROUP_get0_generator(group);
  1772. p_scalar = scalar;
  1773. } else {
  1774. /* the i^th point */
  1775. p = points[i];
  1776. p_scalar = scalars[i];
  1777. }
  1778. if ((p_scalar != NULL) && (p != NULL)) {
  1779. /* reduce scalar to 0 <= scalar < 2^521 */
  1780. if ((BN_num_bits(p_scalar) > 521)
  1781. || (BN_is_negative(p_scalar))) {
  1782. /*
  1783. * this is an unusual input, and we don't guarantee
  1784. * constant-timeness
  1785. */
  1786. if (!BN_nnmod(tmp_scalar, p_scalar, group->order, ctx)) {
  1787. ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
  1788. goto err;
  1789. }
  1790. num_bytes = BN_bn2lebinpad(tmp_scalar,
  1791. secrets[i], sizeof(secrets[i]));
  1792. } else {
  1793. num_bytes = BN_bn2lebinpad(p_scalar,
  1794. secrets[i], sizeof(secrets[i]));
  1795. }
  1796. if (num_bytes < 0) {
  1797. ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
  1798. goto err;
  1799. }
  1800. /* precompute multiples */
  1801. if ((!BN_to_felem(x_out, p->X)) ||
  1802. (!BN_to_felem(y_out, p->Y)) ||
  1803. (!BN_to_felem(z_out, p->Z)))
  1804. goto err;
  1805. memcpy(pre_comp[i][1][0], x_out, sizeof(felem));
  1806. memcpy(pre_comp[i][1][1], y_out, sizeof(felem));
  1807. memcpy(pre_comp[i][1][2], z_out, sizeof(felem));
  1808. for (j = 2; j <= 16; ++j) {
  1809. if (j & 1) {
  1810. point_add(pre_comp[i][j][0], pre_comp[i][j][1],
  1811. pre_comp[i][j][2], pre_comp[i][1][0],
  1812. pre_comp[i][1][1], pre_comp[i][1][2], 0,
  1813. pre_comp[i][j - 1][0],
  1814. pre_comp[i][j - 1][1],
  1815. pre_comp[i][j - 1][2]);
  1816. } else {
  1817. point_double(pre_comp[i][j][0], pre_comp[i][j][1],
  1818. pre_comp[i][j][2], pre_comp[i][j / 2][0],
  1819. pre_comp[i][j / 2][1],
  1820. pre_comp[i][j / 2][2]);
  1821. }
  1822. }
  1823. }
  1824. }
  1825. if (mixed)
  1826. make_points_affine(num_points * 17, pre_comp[0], tmp_felems);
  1827. }
  1828. /* the scalar for the generator */
  1829. if ((scalar != NULL) && (have_pre_comp)) {
  1830. memset(g_secret, 0, sizeof(g_secret));
  1831. /* reduce scalar to 0 <= scalar < 2^521 */
  1832. if ((BN_num_bits(scalar) > 521) || (BN_is_negative(scalar))) {
  1833. /*
  1834. * this is an unusual input, and we don't guarantee
  1835. * constant-timeness
  1836. */
  1837. if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
  1838. ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
  1839. goto err;
  1840. }
  1841. num_bytes = BN_bn2lebinpad(tmp_scalar, g_secret, sizeof(g_secret));
  1842. } else {
  1843. num_bytes = BN_bn2lebinpad(scalar, g_secret, sizeof(g_secret));
  1844. }
  1845. /* do the multiplication with generator precomputation */
  1846. batch_mul(x_out, y_out, z_out,
  1847. (const felem_bytearray(*))secrets, num_points,
  1848. g_secret,
  1849. mixed, (const felem(*)[17][3])pre_comp,
  1850. (const felem(*)[3])g_pre_comp);
  1851. } else {
  1852. /* do the multiplication without generator precomputation */
  1853. batch_mul(x_out, y_out, z_out,
  1854. (const felem_bytearray(*))secrets, num_points,
  1855. NULL, mixed, (const felem(*)[17][3])pre_comp, NULL);
  1856. }
  1857. /* reduce the output to its unique minimal representation */
  1858. felem_contract(x_in, x_out);
  1859. felem_contract(y_in, y_out);
  1860. felem_contract(z_in, z_out);
  1861. if ((!felem_to_BN(x, x_in)) || (!felem_to_BN(y, y_in)) ||
  1862. (!felem_to_BN(z, z_in))) {
  1863. ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
  1864. goto err;
  1865. }
  1866. ret = ec_GFp_simple_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
  1867. err:
  1868. BN_CTX_end(ctx);
  1869. EC_POINT_free(generator);
  1870. OPENSSL_free(secrets);
  1871. OPENSSL_free(pre_comp);
  1872. OPENSSL_free(tmp_felems);
  1873. return ret;
  1874. }
  1875. int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  1876. {
  1877. int ret = 0;
  1878. NISTP521_PRE_COMP *pre = NULL;
  1879. int i, j;
  1880. BIGNUM *x, *y;
  1881. EC_POINT *generator = NULL;
  1882. felem tmp_felems[16];
  1883. #ifndef FIPS_MODULE
  1884. BN_CTX *new_ctx = NULL;
  1885. #endif
  1886. /* throw away old precomputation */
  1887. EC_pre_comp_free(group);
  1888. #ifndef FIPS_MODULE
  1889. if (ctx == NULL)
  1890. ctx = new_ctx = BN_CTX_new();
  1891. #endif
  1892. if (ctx == NULL)
  1893. return 0;
  1894. BN_CTX_start(ctx);
  1895. x = BN_CTX_get(ctx);
  1896. y = BN_CTX_get(ctx);
  1897. if (y == NULL)
  1898. goto err;
  1899. /* get the generator */
  1900. if (group->generator == NULL)
  1901. goto err;
  1902. generator = EC_POINT_new(group);
  1903. if (generator == NULL)
  1904. goto err;
  1905. BN_bin2bn(nistp521_curve_params[3], sizeof(felem_bytearray), x);
  1906. BN_bin2bn(nistp521_curve_params[4], sizeof(felem_bytearray), y);
  1907. if (!EC_POINT_set_affine_coordinates(group, generator, x, y, ctx))
  1908. goto err;
  1909. if ((pre = nistp521_pre_comp_new()) == NULL)
  1910. goto err;
  1911. /*
  1912. * if the generator is the standard one, use built-in precomputation
  1913. */
  1914. if (0 == EC_POINT_cmp(group, generator, group->generator, ctx)) {
  1915. memcpy(pre->g_pre_comp, gmul, sizeof(pre->g_pre_comp));
  1916. goto done;
  1917. }
  1918. if ((!BN_to_felem(pre->g_pre_comp[1][0], group->generator->X)) ||
  1919. (!BN_to_felem(pre->g_pre_comp[1][1], group->generator->Y)) ||
  1920. (!BN_to_felem(pre->g_pre_comp[1][2], group->generator->Z)))
  1921. goto err;
  1922. /* compute 2^130*G, 2^260*G, 2^390*G */
  1923. for (i = 1; i <= 4; i <<= 1) {
  1924. point_double(pre->g_pre_comp[2 * i][0], pre->g_pre_comp[2 * i][1],
  1925. pre->g_pre_comp[2 * i][2], pre->g_pre_comp[i][0],
  1926. pre->g_pre_comp[i][1], pre->g_pre_comp[i][2]);
  1927. for (j = 0; j < 129; ++j) {
  1928. point_double(pre->g_pre_comp[2 * i][0],
  1929. pre->g_pre_comp[2 * i][1],
  1930. pre->g_pre_comp[2 * i][2],
  1931. pre->g_pre_comp[2 * i][0],
  1932. pre->g_pre_comp[2 * i][1],
  1933. pre->g_pre_comp[2 * i][2]);
  1934. }
  1935. }
  1936. /* g_pre_comp[0] is the point at infinity */
  1937. memset(pre->g_pre_comp[0], 0, sizeof(pre->g_pre_comp[0]));
  1938. /* the remaining multiples */
  1939. /* 2^130*G + 2^260*G */
  1940. point_add(pre->g_pre_comp[6][0], pre->g_pre_comp[6][1],
  1941. pre->g_pre_comp[6][2], pre->g_pre_comp[4][0],
  1942. pre->g_pre_comp[4][1], pre->g_pre_comp[4][2],
  1943. 0, pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
  1944. pre->g_pre_comp[2][2]);
  1945. /* 2^130*G + 2^390*G */
  1946. point_add(pre->g_pre_comp[10][0], pre->g_pre_comp[10][1],
  1947. pre->g_pre_comp[10][2], pre->g_pre_comp[8][0],
  1948. pre->g_pre_comp[8][1], pre->g_pre_comp[8][2],
  1949. 0, pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
  1950. pre->g_pre_comp[2][2]);
  1951. /* 2^260*G + 2^390*G */
  1952. point_add(pre->g_pre_comp[12][0], pre->g_pre_comp[12][1],
  1953. pre->g_pre_comp[12][2], pre->g_pre_comp[8][0],
  1954. pre->g_pre_comp[8][1], pre->g_pre_comp[8][2],
  1955. 0, pre->g_pre_comp[4][0], pre->g_pre_comp[4][1],
  1956. pre->g_pre_comp[4][2]);
  1957. /* 2^130*G + 2^260*G + 2^390*G */
  1958. point_add(pre->g_pre_comp[14][0], pre->g_pre_comp[14][1],
  1959. pre->g_pre_comp[14][2], pre->g_pre_comp[12][0],
  1960. pre->g_pre_comp[12][1], pre->g_pre_comp[12][2],
  1961. 0, pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
  1962. pre->g_pre_comp[2][2]);
  1963. for (i = 1; i < 8; ++i) {
  1964. /* odd multiples: add G */
  1965. point_add(pre->g_pre_comp[2 * i + 1][0],
  1966. pre->g_pre_comp[2 * i + 1][1],
  1967. pre->g_pre_comp[2 * i + 1][2], pre->g_pre_comp[2 * i][0],
  1968. pre->g_pre_comp[2 * i][1], pre->g_pre_comp[2 * i][2], 0,
  1969. pre->g_pre_comp[1][0], pre->g_pre_comp[1][1],
  1970. pre->g_pre_comp[1][2]);
  1971. }
  1972. make_points_affine(15, &(pre->g_pre_comp[1]), tmp_felems);
  1973. done:
  1974. SETPRECOMP(group, nistp521, pre);
  1975. ret = 1;
  1976. pre = NULL;
  1977. err:
  1978. BN_CTX_end(ctx);
  1979. EC_POINT_free(generator);
  1980. #ifndef FIPS_MODULE
  1981. BN_CTX_free(new_ctx);
  1982. #endif
  1983. EC_nistp521_pre_comp_free(pre);
  1984. return ret;
  1985. }
  1986. int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group)
  1987. {
  1988. return HAVEPRECOMP(group, nistp521);
  1989. }