2
0

ecp_nistp256.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. /*
  2. * Copyright 2011-2021 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-256 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/opensslconf.h>
  37. #include <stdint.h>
  38. #include <string.h>
  39. #include <openssl/err.h>
  40. #include "ec_local.h"
  41. #include "internal/numbers.h"
  42. #ifndef INT128_MAX
  43. # error "Your compiler doesn't appear to support 128-bit integer types"
  44. #endif
  45. typedef uint8_t u8;
  46. typedef uint32_t u32;
  47. typedef uint64_t u64;
  48. /*
  49. * The underlying field. P256 operates over GF(2^256-2^224+2^192+2^96-1). We
  50. * can serialize an element of this field into 32 bytes. We call this an
  51. * felem_bytearray.
  52. */
  53. typedef u8 felem_bytearray[32];
  54. /*
  55. * These are the parameters of P256, taken from FIPS 186-3, page 86. These
  56. * values are big-endian.
  57. */
  58. static const felem_bytearray nistp256_curve_params[5] = {
  59. {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, /* p */
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  62. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  63. {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, /* a = -3 */
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  66. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc},
  67. {0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, /* b */
  68. 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc,
  69. 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6,
  70. 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b},
  71. {0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, /* x */
  72. 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2,
  73. 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
  74. 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96},
  75. {0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, /* y */
  76. 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
  77. 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
  78. 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5}
  79. };
  80. /*-
  81. * The representation of field elements.
  82. * ------------------------------------
  83. *
  84. * We represent field elements with either four 128-bit values, eight 128-bit
  85. * values, or four 64-bit values. The field element represented is:
  86. * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + v[3]*2^192 (mod p)
  87. * or:
  88. * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + ... + v[8]*2^512 (mod p)
  89. *
  90. * 128-bit values are called 'limbs'. Since the limbs are spaced only 64 bits
  91. * apart, but are 128-bits wide, the most significant bits of each limb overlap
  92. * with the least significant bits of the next.
  93. *
  94. * A field element with four limbs is an 'felem'. One with eight limbs is a
  95. * 'longfelem'
  96. *
  97. * A field element with four, 64-bit values is called a 'smallfelem'. Small
  98. * values are used as intermediate values before multiplication.
  99. */
  100. #define NLIMBS 4
  101. typedef uint128_t limb;
  102. typedef limb felem[NLIMBS];
  103. typedef limb longfelem[NLIMBS * 2];
  104. typedef u64 smallfelem[NLIMBS];
  105. /* This is the value of the prime as four 64-bit words, little-endian. */
  106. static const u64 kPrime[4] =
  107. { 0xfffffffffffffffful, 0xffffffff, 0, 0xffffffff00000001ul };
  108. static const u64 bottom63bits = 0x7ffffffffffffffful;
  109. /*
  110. * bin32_to_felem takes a little-endian byte array and converts it into felem
  111. * form. This assumes that the CPU is little-endian.
  112. */
  113. static void bin32_to_felem(felem out, const u8 in[32])
  114. {
  115. out[0] = *((u64 *)&in[0]);
  116. out[1] = *((u64 *)&in[8]);
  117. out[2] = *((u64 *)&in[16]);
  118. out[3] = *((u64 *)&in[24]);
  119. }
  120. /*
  121. * smallfelem_to_bin32 takes a smallfelem and serializes into a little
  122. * endian, 32 byte array. This assumes that the CPU is little-endian.
  123. */
  124. static void smallfelem_to_bin32(u8 out[32], const smallfelem in)
  125. {
  126. *((u64 *)&out[0]) = in[0];
  127. *((u64 *)&out[8]) = in[1];
  128. *((u64 *)&out[16]) = in[2];
  129. *((u64 *)&out[24]) = in[3];
  130. }
  131. /* BN_to_felem converts an OpenSSL BIGNUM into an felem */
  132. static int BN_to_felem(felem out, const BIGNUM *bn)
  133. {
  134. felem_bytearray b_out;
  135. int num_bytes;
  136. if (BN_is_negative(bn)) {
  137. ERR_raise(ERR_LIB_EC, EC_R_BIGNUM_OUT_OF_RANGE);
  138. return 0;
  139. }
  140. num_bytes = BN_bn2lebinpad(bn, b_out, sizeof(b_out));
  141. if (num_bytes < 0) {
  142. ERR_raise(ERR_LIB_EC, EC_R_BIGNUM_OUT_OF_RANGE);
  143. return 0;
  144. }
  145. bin32_to_felem(out, b_out);
  146. return 1;
  147. }
  148. /* felem_to_BN converts an felem into an OpenSSL BIGNUM */
  149. static BIGNUM *smallfelem_to_BN(BIGNUM *out, const smallfelem in)
  150. {
  151. felem_bytearray b_out;
  152. smallfelem_to_bin32(b_out, in);
  153. return BN_lebin2bn(b_out, sizeof(b_out), out);
  154. }
  155. /*-
  156. * Field operations
  157. * ----------------
  158. */
  159. static void smallfelem_one(smallfelem out)
  160. {
  161. out[0] = 1;
  162. out[1] = 0;
  163. out[2] = 0;
  164. out[3] = 0;
  165. }
  166. static void smallfelem_assign(smallfelem out, const smallfelem in)
  167. {
  168. out[0] = in[0];
  169. out[1] = in[1];
  170. out[2] = in[2];
  171. out[3] = in[3];
  172. }
  173. static void felem_assign(felem out, const felem in)
  174. {
  175. out[0] = in[0];
  176. out[1] = in[1];
  177. out[2] = in[2];
  178. out[3] = in[3];
  179. }
  180. /* felem_sum sets out = out + in. */
  181. static void felem_sum(felem out, const felem in)
  182. {
  183. out[0] += in[0];
  184. out[1] += in[1];
  185. out[2] += in[2];
  186. out[3] += in[3];
  187. }
  188. /* felem_small_sum sets out = out + in. */
  189. static void felem_small_sum(felem out, const smallfelem in)
  190. {
  191. out[0] += in[0];
  192. out[1] += in[1];
  193. out[2] += in[2];
  194. out[3] += in[3];
  195. }
  196. /* felem_scalar sets out = out * scalar */
  197. static void felem_scalar(felem out, const u64 scalar)
  198. {
  199. out[0] *= scalar;
  200. out[1] *= scalar;
  201. out[2] *= scalar;
  202. out[3] *= scalar;
  203. }
  204. /* longfelem_scalar sets out = out * scalar */
  205. static void longfelem_scalar(longfelem out, const u64 scalar)
  206. {
  207. out[0] *= scalar;
  208. out[1] *= scalar;
  209. out[2] *= scalar;
  210. out[3] *= scalar;
  211. out[4] *= scalar;
  212. out[5] *= scalar;
  213. out[6] *= scalar;
  214. out[7] *= scalar;
  215. }
  216. #define two105m41m9 (((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9)
  217. #define two105 (((limb)1) << 105)
  218. #define two105m41p9 (((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9)
  219. /* zero105 is 0 mod p */
  220. static const felem zero105 =
  221. { two105m41m9, two105, two105m41p9, two105m41p9 };
  222. /*-
  223. * smallfelem_neg sets |out| to |-small|
  224. * On exit:
  225. * out[i] < out[i] + 2^105
  226. */
  227. static void smallfelem_neg(felem out, const smallfelem small)
  228. {
  229. /* In order to prevent underflow, we subtract from 0 mod p. */
  230. out[0] = zero105[0] - small[0];
  231. out[1] = zero105[1] - small[1];
  232. out[2] = zero105[2] - small[2];
  233. out[3] = zero105[3] - small[3];
  234. }
  235. /*-
  236. * felem_diff subtracts |in| from |out|
  237. * On entry:
  238. * in[i] < 2^104
  239. * On exit:
  240. * out[i] < out[i] + 2^105
  241. */
  242. static void felem_diff(felem out, const felem in)
  243. {
  244. /*
  245. * In order to prevent underflow, we add 0 mod p before subtracting.
  246. */
  247. out[0] += zero105[0];
  248. out[1] += zero105[1];
  249. out[2] += zero105[2];
  250. out[3] += zero105[3];
  251. out[0] -= in[0];
  252. out[1] -= in[1];
  253. out[2] -= in[2];
  254. out[3] -= in[3];
  255. }
  256. #define two107m43m11 (((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11)
  257. #define two107 (((limb)1) << 107)
  258. #define two107m43p11 (((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11)
  259. /* zero107 is 0 mod p */
  260. static const felem zero107 =
  261. { two107m43m11, two107, two107m43p11, two107m43p11 };
  262. /*-
  263. * An alternative felem_diff for larger inputs |in|
  264. * felem_diff_zero107 subtracts |in| from |out|
  265. * On entry:
  266. * in[i] < 2^106
  267. * On exit:
  268. * out[i] < out[i] + 2^107
  269. */
  270. static void felem_diff_zero107(felem out, const felem in)
  271. {
  272. /*
  273. * In order to prevent underflow, we add 0 mod p before subtracting.
  274. */
  275. out[0] += zero107[0];
  276. out[1] += zero107[1];
  277. out[2] += zero107[2];
  278. out[3] += zero107[3];
  279. out[0] -= in[0];
  280. out[1] -= in[1];
  281. out[2] -= in[2];
  282. out[3] -= in[3];
  283. }
  284. /*-
  285. * longfelem_diff subtracts |in| from |out|
  286. * On entry:
  287. * in[i] < 7*2^67
  288. * On exit:
  289. * out[i] < out[i] + 2^70 + 2^40
  290. */
  291. static void longfelem_diff(longfelem out, const longfelem in)
  292. {
  293. static const limb two70m8p6 =
  294. (((limb) 1) << 70) - (((limb) 1) << 8) + (((limb) 1) << 6);
  295. static const limb two70p40 = (((limb) 1) << 70) + (((limb) 1) << 40);
  296. static const limb two70 = (((limb) 1) << 70);
  297. static const limb two70m40m38p6 =
  298. (((limb) 1) << 70) - (((limb) 1) << 40) - (((limb) 1) << 38) +
  299. (((limb) 1) << 6);
  300. static const limb two70m6 = (((limb) 1) << 70) - (((limb) 1) << 6);
  301. /* add 0 mod p to avoid underflow */
  302. out[0] += two70m8p6;
  303. out[1] += two70p40;
  304. out[2] += two70;
  305. out[3] += two70m40m38p6;
  306. out[4] += two70m6;
  307. out[5] += two70m6;
  308. out[6] += two70m6;
  309. out[7] += two70m6;
  310. /* in[i] < 7*2^67 < 2^70 - 2^40 - 2^38 + 2^6 */
  311. out[0] -= in[0];
  312. out[1] -= in[1];
  313. out[2] -= in[2];
  314. out[3] -= in[3];
  315. out[4] -= in[4];
  316. out[5] -= in[5];
  317. out[6] -= in[6];
  318. out[7] -= in[7];
  319. }
  320. #define two64m0 (((limb)1) << 64) - 1
  321. #define two110p32m0 (((limb)1) << 110) + (((limb)1) << 32) - 1
  322. #define two64m46 (((limb)1) << 64) - (((limb)1) << 46)
  323. #define two64m32 (((limb)1) << 64) - (((limb)1) << 32)
  324. /* zero110 is 0 mod p */
  325. static const felem zero110 = { two64m0, two110p32m0, two64m46, two64m32 };
  326. /*-
  327. * felem_shrink converts an felem into a smallfelem. The result isn't quite
  328. * minimal as the value may be greater than p.
  329. *
  330. * On entry:
  331. * in[i] < 2^109
  332. * On exit:
  333. * out[i] < 2^64
  334. */
  335. static void felem_shrink(smallfelem out, const felem in)
  336. {
  337. felem tmp;
  338. u64 a, b, mask;
  339. u64 high, low;
  340. static const u64 kPrime3Test = 0x7fffffff00000001ul; /* 2^63 - 2^32 + 1 */
  341. /* Carry 2->3 */
  342. tmp[3] = zero110[3] + in[3] + ((u64)(in[2] >> 64));
  343. /* tmp[3] < 2^110 */
  344. tmp[2] = zero110[2] + (u64)in[2];
  345. tmp[0] = zero110[0] + in[0];
  346. tmp[1] = zero110[1] + in[1];
  347. /* tmp[0] < 2**110, tmp[1] < 2^111, tmp[2] < 2**65 */
  348. /*
  349. * We perform two partial reductions where we eliminate the high-word of
  350. * tmp[3]. We don't update the other words till the end.
  351. */
  352. a = tmp[3] >> 64; /* a < 2^46 */
  353. tmp[3] = (u64)tmp[3];
  354. tmp[3] -= a;
  355. tmp[3] += ((limb) a) << 32;
  356. /* tmp[3] < 2^79 */
  357. b = a;
  358. a = tmp[3] >> 64; /* a < 2^15 */
  359. b += a; /* b < 2^46 + 2^15 < 2^47 */
  360. tmp[3] = (u64)tmp[3];
  361. tmp[3] -= a;
  362. tmp[3] += ((limb) a) << 32;
  363. /* tmp[3] < 2^64 + 2^47 */
  364. /*
  365. * This adjusts the other two words to complete the two partial
  366. * reductions.
  367. */
  368. tmp[0] += b;
  369. tmp[1] -= (((limb) b) << 32);
  370. /*
  371. * In order to make space in tmp[3] for the carry from 2 -> 3, we
  372. * conditionally subtract kPrime if tmp[3] is large enough.
  373. */
  374. high = (u64)(tmp[3] >> 64);
  375. /* As tmp[3] < 2^65, high is either 1 or 0 */
  376. high = 0 - high;
  377. /*-
  378. * high is:
  379. * all ones if the high word of tmp[3] is 1
  380. * all zeros if the high word of tmp[3] if 0
  381. */
  382. low = (u64)tmp[3];
  383. mask = 0 - (low >> 63);
  384. /*-
  385. * mask is:
  386. * all ones if the MSB of low is 1
  387. * all zeros if the MSB of low if 0
  388. */
  389. low &= bottom63bits;
  390. low -= kPrime3Test;
  391. /* if low was greater than kPrime3Test then the MSB is zero */
  392. low = ~low;
  393. low = 0 - (low >> 63);
  394. /*-
  395. * low is:
  396. * all ones if low was > kPrime3Test
  397. * all zeros if low was <= kPrime3Test
  398. */
  399. mask = (mask & low) | high;
  400. tmp[0] -= mask & kPrime[0];
  401. tmp[1] -= mask & kPrime[1];
  402. /* kPrime[2] is zero, so omitted */
  403. tmp[3] -= mask & kPrime[3];
  404. /* tmp[3] < 2**64 - 2**32 + 1 */
  405. tmp[1] += ((u64)(tmp[0] >> 64));
  406. tmp[0] = (u64)tmp[0];
  407. tmp[2] += ((u64)(tmp[1] >> 64));
  408. tmp[1] = (u64)tmp[1];
  409. tmp[3] += ((u64)(tmp[2] >> 64));
  410. tmp[2] = (u64)tmp[2];
  411. /* tmp[i] < 2^64 */
  412. out[0] = tmp[0];
  413. out[1] = tmp[1];
  414. out[2] = tmp[2];
  415. out[3] = tmp[3];
  416. }
  417. /* smallfelem_expand converts a smallfelem to an felem */
  418. static void smallfelem_expand(felem out, const smallfelem in)
  419. {
  420. out[0] = in[0];
  421. out[1] = in[1];
  422. out[2] = in[2];
  423. out[3] = in[3];
  424. }
  425. /*-
  426. * smallfelem_square sets |out| = |small|^2
  427. * On entry:
  428. * small[i] < 2^64
  429. * On exit:
  430. * out[i] < 7 * 2^64 < 2^67
  431. */
  432. static void smallfelem_square(longfelem out, const smallfelem small)
  433. {
  434. limb a;
  435. u64 high, low;
  436. a = ((uint128_t) small[0]) * small[0];
  437. low = a;
  438. high = a >> 64;
  439. out[0] = low;
  440. out[1] = high;
  441. a = ((uint128_t) small[0]) * small[1];
  442. low = a;
  443. high = a >> 64;
  444. out[1] += low;
  445. out[1] += low;
  446. out[2] = high;
  447. a = ((uint128_t) small[0]) * small[2];
  448. low = a;
  449. high = a >> 64;
  450. out[2] += low;
  451. out[2] *= 2;
  452. out[3] = high;
  453. a = ((uint128_t) small[0]) * small[3];
  454. low = a;
  455. high = a >> 64;
  456. out[3] += low;
  457. out[4] = high;
  458. a = ((uint128_t) small[1]) * small[2];
  459. low = a;
  460. high = a >> 64;
  461. out[3] += low;
  462. out[3] *= 2;
  463. out[4] += high;
  464. a = ((uint128_t) small[1]) * small[1];
  465. low = a;
  466. high = a >> 64;
  467. out[2] += low;
  468. out[3] += high;
  469. a = ((uint128_t) small[1]) * small[3];
  470. low = a;
  471. high = a >> 64;
  472. out[4] += low;
  473. out[4] *= 2;
  474. out[5] = high;
  475. a = ((uint128_t) small[2]) * small[3];
  476. low = a;
  477. high = a >> 64;
  478. out[5] += low;
  479. out[5] *= 2;
  480. out[6] = high;
  481. out[6] += high;
  482. a = ((uint128_t) small[2]) * small[2];
  483. low = a;
  484. high = a >> 64;
  485. out[4] += low;
  486. out[5] += high;
  487. a = ((uint128_t) small[3]) * small[3];
  488. low = a;
  489. high = a >> 64;
  490. out[6] += low;
  491. out[7] = high;
  492. }
  493. /*-
  494. * felem_square sets |out| = |in|^2
  495. * On entry:
  496. * in[i] < 2^109
  497. * On exit:
  498. * out[i] < 7 * 2^64 < 2^67
  499. */
  500. static void felem_square(longfelem out, const felem in)
  501. {
  502. u64 small[4];
  503. felem_shrink(small, in);
  504. smallfelem_square(out, small);
  505. }
  506. /*-
  507. * smallfelem_mul sets |out| = |small1| * |small2|
  508. * On entry:
  509. * small1[i] < 2^64
  510. * small2[i] < 2^64
  511. * On exit:
  512. * out[i] < 7 * 2^64 < 2^67
  513. */
  514. static void smallfelem_mul(longfelem out, const smallfelem small1,
  515. const smallfelem small2)
  516. {
  517. limb a;
  518. u64 high, low;
  519. a = ((uint128_t) small1[0]) * small2[0];
  520. low = a;
  521. high = a >> 64;
  522. out[0] = low;
  523. out[1] = high;
  524. a = ((uint128_t) small1[0]) * small2[1];
  525. low = a;
  526. high = a >> 64;
  527. out[1] += low;
  528. out[2] = high;
  529. a = ((uint128_t) small1[1]) * small2[0];
  530. low = a;
  531. high = a >> 64;
  532. out[1] += low;
  533. out[2] += high;
  534. a = ((uint128_t) small1[0]) * small2[2];
  535. low = a;
  536. high = a >> 64;
  537. out[2] += low;
  538. out[3] = high;
  539. a = ((uint128_t) small1[1]) * small2[1];
  540. low = a;
  541. high = a >> 64;
  542. out[2] += low;
  543. out[3] += high;
  544. a = ((uint128_t) small1[2]) * small2[0];
  545. low = a;
  546. high = a >> 64;
  547. out[2] += low;
  548. out[3] += high;
  549. a = ((uint128_t) small1[0]) * small2[3];
  550. low = a;
  551. high = a >> 64;
  552. out[3] += low;
  553. out[4] = high;
  554. a = ((uint128_t) small1[1]) * small2[2];
  555. low = a;
  556. high = a >> 64;
  557. out[3] += low;
  558. out[4] += high;
  559. a = ((uint128_t) small1[2]) * small2[1];
  560. low = a;
  561. high = a >> 64;
  562. out[3] += low;
  563. out[4] += high;
  564. a = ((uint128_t) small1[3]) * small2[0];
  565. low = a;
  566. high = a >> 64;
  567. out[3] += low;
  568. out[4] += high;
  569. a = ((uint128_t) small1[1]) * small2[3];
  570. low = a;
  571. high = a >> 64;
  572. out[4] += low;
  573. out[5] = high;
  574. a = ((uint128_t) small1[2]) * small2[2];
  575. low = a;
  576. high = a >> 64;
  577. out[4] += low;
  578. out[5] += high;
  579. a = ((uint128_t) small1[3]) * small2[1];
  580. low = a;
  581. high = a >> 64;
  582. out[4] += low;
  583. out[5] += high;
  584. a = ((uint128_t) small1[2]) * small2[3];
  585. low = a;
  586. high = a >> 64;
  587. out[5] += low;
  588. out[6] = high;
  589. a = ((uint128_t) small1[3]) * small2[2];
  590. low = a;
  591. high = a >> 64;
  592. out[5] += low;
  593. out[6] += high;
  594. a = ((uint128_t) small1[3]) * small2[3];
  595. low = a;
  596. high = a >> 64;
  597. out[6] += low;
  598. out[7] = high;
  599. }
  600. /*-
  601. * felem_mul sets |out| = |in1| * |in2|
  602. * On entry:
  603. * in1[i] < 2^109
  604. * in2[i] < 2^109
  605. * On exit:
  606. * out[i] < 7 * 2^64 < 2^67
  607. */
  608. static void felem_mul(longfelem out, const felem in1, const felem in2)
  609. {
  610. smallfelem small1, small2;
  611. felem_shrink(small1, in1);
  612. felem_shrink(small2, in2);
  613. smallfelem_mul(out, small1, small2);
  614. }
  615. /*-
  616. * felem_small_mul sets |out| = |small1| * |in2|
  617. * On entry:
  618. * small1[i] < 2^64
  619. * in2[i] < 2^109
  620. * On exit:
  621. * out[i] < 7 * 2^64 < 2^67
  622. */
  623. static void felem_small_mul(longfelem out, const smallfelem small1,
  624. const felem in2)
  625. {
  626. smallfelem small2;
  627. felem_shrink(small2, in2);
  628. smallfelem_mul(out, small1, small2);
  629. }
  630. #define two100m36m4 (((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4)
  631. #define two100 (((limb)1) << 100)
  632. #define two100m36p4 (((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4)
  633. /* zero100 is 0 mod p */
  634. static const felem zero100 =
  635. { two100m36m4, two100, two100m36p4, two100m36p4 };
  636. /*-
  637. * Internal function for the different flavours of felem_reduce.
  638. * felem_reduce_ reduces the higher coefficients in[4]-in[7].
  639. * On entry:
  640. * out[0] >= in[6] + 2^32*in[6] + in[7] + 2^32*in[7]
  641. * out[1] >= in[7] + 2^32*in[4]
  642. * out[2] >= in[5] + 2^32*in[5]
  643. * out[3] >= in[4] + 2^32*in[5] + 2^32*in[6]
  644. * On exit:
  645. * out[0] <= out[0] + in[4] + 2^32*in[5]
  646. * out[1] <= out[1] + in[5] + 2^33*in[6]
  647. * out[2] <= out[2] + in[7] + 2*in[6] + 2^33*in[7]
  648. * out[3] <= out[3] + 2^32*in[4] + 3*in[7]
  649. */
  650. static void felem_reduce_(felem out, const longfelem in)
  651. {
  652. int128_t c;
  653. /* combine common terms from below */
  654. c = in[4] + (in[5] << 32);
  655. out[0] += c;
  656. out[3] -= c;
  657. c = in[5] - in[7];
  658. out[1] += c;
  659. out[2] -= c;
  660. /* the remaining terms */
  661. /* 256: [(0,1),(96,-1),(192,-1),(224,1)] */
  662. out[1] -= (in[4] << 32);
  663. out[3] += (in[4] << 32);
  664. /* 320: [(32,1),(64,1),(128,-1),(160,-1),(224,-1)] */
  665. out[2] -= (in[5] << 32);
  666. /* 384: [(0,-1),(32,-1),(96,2),(128,2),(224,-1)] */
  667. out[0] -= in[6];
  668. out[0] -= (in[6] << 32);
  669. out[1] += (in[6] << 33);
  670. out[2] += (in[6] * 2);
  671. out[3] -= (in[6] << 32);
  672. /* 448: [(0,-1),(32,-1),(64,-1),(128,1),(160,2),(192,3)] */
  673. out[0] -= in[7];
  674. out[0] -= (in[7] << 32);
  675. out[2] += (in[7] << 33);
  676. out[3] += (in[7] * 3);
  677. }
  678. /*-
  679. * felem_reduce converts a longfelem into an felem.
  680. * To be called directly after felem_square or felem_mul.
  681. * On entry:
  682. * in[0] < 2^64, in[1] < 3*2^64, in[2] < 5*2^64, in[3] < 7*2^64
  683. * in[4] < 7*2^64, in[5] < 5*2^64, in[6] < 3*2^64, in[7] < 2*64
  684. * On exit:
  685. * out[i] < 2^101
  686. */
  687. static void felem_reduce(felem out, const longfelem in)
  688. {
  689. out[0] = zero100[0] + in[0];
  690. out[1] = zero100[1] + in[1];
  691. out[2] = zero100[2] + in[2];
  692. out[3] = zero100[3] + in[3];
  693. felem_reduce_(out, in);
  694. /*-
  695. * out[0] > 2^100 - 2^36 - 2^4 - 3*2^64 - 3*2^96 - 2^64 - 2^96 > 0
  696. * out[1] > 2^100 - 2^64 - 7*2^96 > 0
  697. * out[2] > 2^100 - 2^36 + 2^4 - 5*2^64 - 5*2^96 > 0
  698. * out[3] > 2^100 - 2^36 + 2^4 - 7*2^64 - 5*2^96 - 3*2^96 > 0
  699. *
  700. * out[0] < 2^100 + 2^64 + 7*2^64 + 5*2^96 < 2^101
  701. * out[1] < 2^100 + 3*2^64 + 5*2^64 + 3*2^97 < 2^101
  702. * out[2] < 2^100 + 5*2^64 + 2^64 + 3*2^65 + 2^97 < 2^101
  703. * out[3] < 2^100 + 7*2^64 + 7*2^96 + 3*2^64 < 2^101
  704. */
  705. }
  706. /*-
  707. * felem_reduce_zero105 converts a larger longfelem into an felem.
  708. * On entry:
  709. * in[0] < 2^71
  710. * On exit:
  711. * out[i] < 2^106
  712. */
  713. static void felem_reduce_zero105(felem out, const longfelem in)
  714. {
  715. out[0] = zero105[0] + in[0];
  716. out[1] = zero105[1] + in[1];
  717. out[2] = zero105[2] + in[2];
  718. out[3] = zero105[3] + in[3];
  719. felem_reduce_(out, in);
  720. /*-
  721. * out[0] > 2^105 - 2^41 - 2^9 - 2^71 - 2^103 - 2^71 - 2^103 > 0
  722. * out[1] > 2^105 - 2^71 - 2^103 > 0
  723. * out[2] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 > 0
  724. * out[3] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 - 2^103 > 0
  725. *
  726. * out[0] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
  727. * out[1] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
  728. * out[2] < 2^105 + 2^71 + 2^71 + 2^71 + 2^103 < 2^106
  729. * out[3] < 2^105 + 2^71 + 2^103 + 2^71 < 2^106
  730. */
  731. }
  732. /*
  733. * subtract_u64 sets *result = *result - v and *carry to one if the
  734. * subtraction underflowed.
  735. */
  736. static void subtract_u64(u64 *result, u64 *carry, u64 v)
  737. {
  738. uint128_t r = *result;
  739. r -= v;
  740. *carry = (r >> 64) & 1;
  741. *result = (u64)r;
  742. }
  743. /*
  744. * felem_contract converts |in| to its unique, minimal representation. On
  745. * entry: in[i] < 2^109
  746. */
  747. static void felem_contract(smallfelem out, const felem in)
  748. {
  749. unsigned i;
  750. u64 all_equal_so_far = 0, result = 0, carry;
  751. felem_shrink(out, in);
  752. /* small is minimal except that the value might be > p */
  753. all_equal_so_far--;
  754. /*
  755. * We are doing a constant time test if out >= kPrime. We need to compare
  756. * each u64, from most-significant to least significant. For each one, if
  757. * all words so far have been equal (m is all ones) then a non-equal
  758. * result is the answer. Otherwise we continue.
  759. */
  760. for (i = 3; i < 4; i--) {
  761. u64 equal;
  762. uint128_t a = ((uint128_t) kPrime[i]) - out[i];
  763. /*
  764. * if out[i] > kPrime[i] then a will underflow and the high 64-bits
  765. * will all be set.
  766. */
  767. result |= all_equal_so_far & ((u64)(a >> 64));
  768. /*
  769. * if kPrime[i] == out[i] then |equal| will be all zeros and the
  770. * decrement will make it all ones.
  771. */
  772. equal = kPrime[i] ^ out[i];
  773. equal--;
  774. equal &= equal << 32;
  775. equal &= equal << 16;
  776. equal &= equal << 8;
  777. equal &= equal << 4;
  778. equal &= equal << 2;
  779. equal &= equal << 1;
  780. equal = 0 - (equal >> 63);
  781. all_equal_so_far &= equal;
  782. }
  783. /*
  784. * if all_equal_so_far is still all ones then the two values are equal
  785. * and so out >= kPrime is true.
  786. */
  787. result |= all_equal_so_far;
  788. /* if out >= kPrime then we subtract kPrime. */
  789. subtract_u64(&out[0], &carry, result & kPrime[0]);
  790. subtract_u64(&out[1], &carry, carry);
  791. subtract_u64(&out[2], &carry, carry);
  792. subtract_u64(&out[3], &carry, carry);
  793. subtract_u64(&out[1], &carry, result & kPrime[1]);
  794. subtract_u64(&out[2], &carry, carry);
  795. subtract_u64(&out[3], &carry, carry);
  796. subtract_u64(&out[2], &carry, result & kPrime[2]);
  797. subtract_u64(&out[3], &carry, carry);
  798. subtract_u64(&out[3], &carry, result & kPrime[3]);
  799. }
  800. static void smallfelem_square_contract(smallfelem out, const smallfelem in)
  801. {
  802. longfelem longtmp;
  803. felem tmp;
  804. smallfelem_square(longtmp, in);
  805. felem_reduce(tmp, longtmp);
  806. felem_contract(out, tmp);
  807. }
  808. static void smallfelem_mul_contract(smallfelem out, const smallfelem in1,
  809. const smallfelem in2)
  810. {
  811. longfelem longtmp;
  812. felem tmp;
  813. smallfelem_mul(longtmp, in1, in2);
  814. felem_reduce(tmp, longtmp);
  815. felem_contract(out, tmp);
  816. }
  817. /*-
  818. * felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
  819. * otherwise.
  820. * On entry:
  821. * small[i] < 2^64
  822. */
  823. static limb smallfelem_is_zero(const smallfelem small)
  824. {
  825. limb result;
  826. u64 is_p;
  827. u64 is_zero = small[0] | small[1] | small[2] | small[3];
  828. is_zero--;
  829. is_zero &= is_zero << 32;
  830. is_zero &= is_zero << 16;
  831. is_zero &= is_zero << 8;
  832. is_zero &= is_zero << 4;
  833. is_zero &= is_zero << 2;
  834. is_zero &= is_zero << 1;
  835. is_zero = 0 - (is_zero >> 63);
  836. is_p = (small[0] ^ kPrime[0]) |
  837. (small[1] ^ kPrime[1]) |
  838. (small[2] ^ kPrime[2]) | (small[3] ^ kPrime[3]);
  839. is_p--;
  840. is_p &= is_p << 32;
  841. is_p &= is_p << 16;
  842. is_p &= is_p << 8;
  843. is_p &= is_p << 4;
  844. is_p &= is_p << 2;
  845. is_p &= is_p << 1;
  846. is_p = 0 - (is_p >> 63);
  847. is_zero |= is_p;
  848. result = is_zero;
  849. result |= ((limb) is_zero) << 64;
  850. return result;
  851. }
  852. static int smallfelem_is_zero_int(const void *small)
  853. {
  854. return (int)(smallfelem_is_zero(small) & ((limb) 1));
  855. }
  856. /*-
  857. * felem_inv calculates |out| = |in|^{-1}
  858. *
  859. * Based on Fermat's Little Theorem:
  860. * a^p = a (mod p)
  861. * a^{p-1} = 1 (mod p)
  862. * a^{p-2} = a^{-1} (mod p)
  863. */
  864. static void felem_inv(felem out, const felem in)
  865. {
  866. felem ftmp, ftmp2;
  867. /* each e_I will hold |in|^{2^I - 1} */
  868. felem e2, e4, e8, e16, e32, e64;
  869. longfelem tmp;
  870. unsigned i;
  871. felem_square(tmp, in);
  872. felem_reduce(ftmp, tmp); /* 2^1 */
  873. felem_mul(tmp, in, ftmp);
  874. felem_reduce(ftmp, tmp); /* 2^2 - 2^0 */
  875. felem_assign(e2, ftmp);
  876. felem_square(tmp, ftmp);
  877. felem_reduce(ftmp, tmp); /* 2^3 - 2^1 */
  878. felem_square(tmp, ftmp);
  879. felem_reduce(ftmp, tmp); /* 2^4 - 2^2 */
  880. felem_mul(tmp, ftmp, e2);
  881. felem_reduce(ftmp, tmp); /* 2^4 - 2^0 */
  882. felem_assign(e4, ftmp);
  883. felem_square(tmp, ftmp);
  884. felem_reduce(ftmp, tmp); /* 2^5 - 2^1 */
  885. felem_square(tmp, ftmp);
  886. felem_reduce(ftmp, tmp); /* 2^6 - 2^2 */
  887. felem_square(tmp, ftmp);
  888. felem_reduce(ftmp, tmp); /* 2^7 - 2^3 */
  889. felem_square(tmp, ftmp);
  890. felem_reduce(ftmp, tmp); /* 2^8 - 2^4 */
  891. felem_mul(tmp, ftmp, e4);
  892. felem_reduce(ftmp, tmp); /* 2^8 - 2^0 */
  893. felem_assign(e8, ftmp);
  894. for (i = 0; i < 8; i++) {
  895. felem_square(tmp, ftmp);
  896. felem_reduce(ftmp, tmp);
  897. } /* 2^16 - 2^8 */
  898. felem_mul(tmp, ftmp, e8);
  899. felem_reduce(ftmp, tmp); /* 2^16 - 2^0 */
  900. felem_assign(e16, ftmp);
  901. for (i = 0; i < 16; i++) {
  902. felem_square(tmp, ftmp);
  903. felem_reduce(ftmp, tmp);
  904. } /* 2^32 - 2^16 */
  905. felem_mul(tmp, ftmp, e16);
  906. felem_reduce(ftmp, tmp); /* 2^32 - 2^0 */
  907. felem_assign(e32, ftmp);
  908. for (i = 0; i < 32; i++) {
  909. felem_square(tmp, ftmp);
  910. felem_reduce(ftmp, tmp);
  911. } /* 2^64 - 2^32 */
  912. felem_assign(e64, ftmp);
  913. felem_mul(tmp, ftmp, in);
  914. felem_reduce(ftmp, tmp); /* 2^64 - 2^32 + 2^0 */
  915. for (i = 0; i < 192; i++) {
  916. felem_square(tmp, ftmp);
  917. felem_reduce(ftmp, tmp);
  918. } /* 2^256 - 2^224 + 2^192 */
  919. felem_mul(tmp, e64, e32);
  920. felem_reduce(ftmp2, tmp); /* 2^64 - 2^0 */
  921. for (i = 0; i < 16; i++) {
  922. felem_square(tmp, ftmp2);
  923. felem_reduce(ftmp2, tmp);
  924. } /* 2^80 - 2^16 */
  925. felem_mul(tmp, ftmp2, e16);
  926. felem_reduce(ftmp2, tmp); /* 2^80 - 2^0 */
  927. for (i = 0; i < 8; i++) {
  928. felem_square(tmp, ftmp2);
  929. felem_reduce(ftmp2, tmp);
  930. } /* 2^88 - 2^8 */
  931. felem_mul(tmp, ftmp2, e8);
  932. felem_reduce(ftmp2, tmp); /* 2^88 - 2^0 */
  933. for (i = 0; i < 4; i++) {
  934. felem_square(tmp, ftmp2);
  935. felem_reduce(ftmp2, tmp);
  936. } /* 2^92 - 2^4 */
  937. felem_mul(tmp, ftmp2, e4);
  938. felem_reduce(ftmp2, tmp); /* 2^92 - 2^0 */
  939. felem_square(tmp, ftmp2);
  940. felem_reduce(ftmp2, tmp); /* 2^93 - 2^1 */
  941. felem_square(tmp, ftmp2);
  942. felem_reduce(ftmp2, tmp); /* 2^94 - 2^2 */
  943. felem_mul(tmp, ftmp2, e2);
  944. felem_reduce(ftmp2, tmp); /* 2^94 - 2^0 */
  945. felem_square(tmp, ftmp2);
  946. felem_reduce(ftmp2, tmp); /* 2^95 - 2^1 */
  947. felem_square(tmp, ftmp2);
  948. felem_reduce(ftmp2, tmp); /* 2^96 - 2^2 */
  949. felem_mul(tmp, ftmp2, in);
  950. felem_reduce(ftmp2, tmp); /* 2^96 - 3 */
  951. felem_mul(tmp, ftmp2, ftmp);
  952. felem_reduce(out, tmp); /* 2^256 - 2^224 + 2^192 + 2^96 - 3 */
  953. }
  954. static void smallfelem_inv_contract(smallfelem out, const smallfelem in)
  955. {
  956. felem tmp;
  957. smallfelem_expand(tmp, in);
  958. felem_inv(tmp, tmp);
  959. felem_contract(out, tmp);
  960. }
  961. /*-
  962. * Group operations
  963. * ----------------
  964. *
  965. * Building on top of the field operations we have the operations on the
  966. * elliptic curve group itself. Points on the curve are represented in Jacobian
  967. * coordinates
  968. */
  969. /*-
  970. * point_double calculates 2*(x_in, y_in, z_in)
  971. *
  972. * The method is taken from:
  973. * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  974. *
  975. * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
  976. * while x_out == y_in is not (maybe this works, but it's not tested).
  977. */
  978. static void
  979. point_double(felem x_out, felem y_out, felem z_out,
  980. const felem x_in, const felem y_in, const felem z_in)
  981. {
  982. longfelem tmp, tmp2;
  983. felem delta, gamma, beta, alpha, ftmp, ftmp2;
  984. smallfelem small1, small2;
  985. felem_assign(ftmp, x_in);
  986. /* ftmp[i] < 2^106 */
  987. felem_assign(ftmp2, x_in);
  988. /* ftmp2[i] < 2^106 */
  989. /* delta = z^2 */
  990. felem_square(tmp, z_in);
  991. felem_reduce(delta, tmp);
  992. /* delta[i] < 2^101 */
  993. /* gamma = y^2 */
  994. felem_square(tmp, y_in);
  995. felem_reduce(gamma, tmp);
  996. /* gamma[i] < 2^101 */
  997. felem_shrink(small1, gamma);
  998. /* beta = x*gamma */
  999. felem_small_mul(tmp, small1, x_in);
  1000. felem_reduce(beta, tmp);
  1001. /* beta[i] < 2^101 */
  1002. /* alpha = 3*(x-delta)*(x+delta) */
  1003. felem_diff(ftmp, delta);
  1004. /* ftmp[i] < 2^105 + 2^106 < 2^107 */
  1005. felem_sum(ftmp2, delta);
  1006. /* ftmp2[i] < 2^105 + 2^106 < 2^107 */
  1007. felem_scalar(ftmp2, 3);
  1008. /* ftmp2[i] < 3 * 2^107 < 2^109 */
  1009. felem_mul(tmp, ftmp, ftmp2);
  1010. felem_reduce(alpha, tmp);
  1011. /* alpha[i] < 2^101 */
  1012. felem_shrink(small2, alpha);
  1013. /* x' = alpha^2 - 8*beta */
  1014. smallfelem_square(tmp, small2);
  1015. felem_reduce(x_out, tmp);
  1016. felem_assign(ftmp, beta);
  1017. felem_scalar(ftmp, 8);
  1018. /* ftmp[i] < 8 * 2^101 = 2^104 */
  1019. felem_diff(x_out, ftmp);
  1020. /* x_out[i] < 2^105 + 2^101 < 2^106 */
  1021. /* z' = (y + z)^2 - gamma - delta */
  1022. felem_sum(delta, gamma);
  1023. /* delta[i] < 2^101 + 2^101 = 2^102 */
  1024. felem_assign(ftmp, y_in);
  1025. felem_sum(ftmp, z_in);
  1026. /* ftmp[i] < 2^106 + 2^106 = 2^107 */
  1027. felem_square(tmp, ftmp);
  1028. felem_reduce(z_out, tmp);
  1029. felem_diff(z_out, delta);
  1030. /* z_out[i] < 2^105 + 2^101 < 2^106 */
  1031. /* y' = alpha*(4*beta - x') - 8*gamma^2 */
  1032. felem_scalar(beta, 4);
  1033. /* beta[i] < 4 * 2^101 = 2^103 */
  1034. felem_diff_zero107(beta, x_out);
  1035. /* beta[i] < 2^107 + 2^103 < 2^108 */
  1036. felem_small_mul(tmp, small2, beta);
  1037. /* tmp[i] < 7 * 2^64 < 2^67 */
  1038. smallfelem_square(tmp2, small1);
  1039. /* tmp2[i] < 7 * 2^64 */
  1040. longfelem_scalar(tmp2, 8);
  1041. /* tmp2[i] < 8 * 7 * 2^64 = 7 * 2^67 */
  1042. longfelem_diff(tmp, tmp2);
  1043. /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
  1044. felem_reduce_zero105(y_out, tmp);
  1045. /* y_out[i] < 2^106 */
  1046. }
  1047. /*
  1048. * point_double_small is the same as point_double, except that it operates on
  1049. * smallfelems
  1050. */
  1051. static void
  1052. point_double_small(smallfelem x_out, smallfelem y_out, smallfelem z_out,
  1053. const smallfelem x_in, const smallfelem y_in,
  1054. const smallfelem z_in)
  1055. {
  1056. felem felem_x_out, felem_y_out, felem_z_out;
  1057. felem felem_x_in, felem_y_in, felem_z_in;
  1058. smallfelem_expand(felem_x_in, x_in);
  1059. smallfelem_expand(felem_y_in, y_in);
  1060. smallfelem_expand(felem_z_in, z_in);
  1061. point_double(felem_x_out, felem_y_out, felem_z_out,
  1062. felem_x_in, felem_y_in, felem_z_in);
  1063. felem_shrink(x_out, felem_x_out);
  1064. felem_shrink(y_out, felem_y_out);
  1065. felem_shrink(z_out, felem_z_out);
  1066. }
  1067. /* copy_conditional copies in to out iff mask is all ones. */
  1068. static void copy_conditional(felem out, const felem in, limb mask)
  1069. {
  1070. unsigned i;
  1071. for (i = 0; i < NLIMBS; ++i) {
  1072. const limb tmp = mask & (in[i] ^ out[i]);
  1073. out[i] ^= tmp;
  1074. }
  1075. }
  1076. /* copy_small_conditional copies in to out iff mask is all ones. */
  1077. static void copy_small_conditional(felem out, const smallfelem in, limb mask)
  1078. {
  1079. unsigned i;
  1080. const u64 mask64 = mask;
  1081. for (i = 0; i < NLIMBS; ++i) {
  1082. out[i] = ((limb) (in[i] & mask64)) | (out[i] & ~mask);
  1083. }
  1084. }
  1085. /*-
  1086. * point_add calculates (x1, y1, z1) + (x2, y2, z2)
  1087. *
  1088. * The method is taken from:
  1089. * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
  1090. * adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
  1091. *
  1092. * This function includes a branch for checking whether the two input points
  1093. * are equal, (while not equal to the point at infinity). This case never
  1094. * happens during single point multiplication, so there is no timing leak for
  1095. * ECDH or ECDSA signing.
  1096. */
  1097. static void point_add(felem x3, felem y3, felem z3,
  1098. const felem x1, const felem y1, const felem z1,
  1099. const int mixed, const smallfelem x2,
  1100. const smallfelem y2, const smallfelem z2)
  1101. {
  1102. felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
  1103. longfelem tmp, tmp2;
  1104. smallfelem small1, small2, small3, small4, small5;
  1105. limb x_equal, y_equal, z1_is_zero, z2_is_zero;
  1106. limb points_equal;
  1107. felem_shrink(small3, z1);
  1108. z1_is_zero = smallfelem_is_zero(small3);
  1109. z2_is_zero = smallfelem_is_zero(z2);
  1110. /* ftmp = z1z1 = z1**2 */
  1111. smallfelem_square(tmp, small3);
  1112. felem_reduce(ftmp, tmp);
  1113. /* ftmp[i] < 2^101 */
  1114. felem_shrink(small1, ftmp);
  1115. if (!mixed) {
  1116. /* ftmp2 = z2z2 = z2**2 */
  1117. smallfelem_square(tmp, z2);
  1118. felem_reduce(ftmp2, tmp);
  1119. /* ftmp2[i] < 2^101 */
  1120. felem_shrink(small2, ftmp2);
  1121. felem_shrink(small5, x1);
  1122. /* u1 = ftmp3 = x1*z2z2 */
  1123. smallfelem_mul(tmp, small5, small2);
  1124. felem_reduce(ftmp3, tmp);
  1125. /* ftmp3[i] < 2^101 */
  1126. /* ftmp5 = z1 + z2 */
  1127. felem_assign(ftmp5, z1);
  1128. felem_small_sum(ftmp5, z2);
  1129. /* ftmp5[i] < 2^107 */
  1130. /* ftmp5 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2 */
  1131. felem_square(tmp, ftmp5);
  1132. felem_reduce(ftmp5, tmp);
  1133. /* ftmp2 = z2z2 + z1z1 */
  1134. felem_sum(ftmp2, ftmp);
  1135. /* ftmp2[i] < 2^101 + 2^101 = 2^102 */
  1136. felem_diff(ftmp5, ftmp2);
  1137. /* ftmp5[i] < 2^105 + 2^101 < 2^106 */
  1138. /* ftmp2 = z2 * z2z2 */
  1139. smallfelem_mul(tmp, small2, z2);
  1140. felem_reduce(ftmp2, tmp);
  1141. /* s1 = ftmp2 = y1 * z2**3 */
  1142. felem_mul(tmp, y1, ftmp2);
  1143. felem_reduce(ftmp6, tmp);
  1144. /* ftmp6[i] < 2^101 */
  1145. } else {
  1146. /*
  1147. * We'll assume z2 = 1 (special case z2 = 0 is handled later)
  1148. */
  1149. /* u1 = ftmp3 = x1*z2z2 */
  1150. felem_assign(ftmp3, x1);
  1151. /* ftmp3[i] < 2^106 */
  1152. /* ftmp5 = 2z1z2 */
  1153. felem_assign(ftmp5, z1);
  1154. felem_scalar(ftmp5, 2);
  1155. /* ftmp5[i] < 2*2^106 = 2^107 */
  1156. /* s1 = ftmp2 = y1 * z2**3 */
  1157. felem_assign(ftmp6, y1);
  1158. /* ftmp6[i] < 2^106 */
  1159. }
  1160. /* u2 = x2*z1z1 */
  1161. smallfelem_mul(tmp, x2, small1);
  1162. felem_reduce(ftmp4, tmp);
  1163. /* h = ftmp4 = u2 - u1 */
  1164. felem_diff_zero107(ftmp4, ftmp3);
  1165. /* ftmp4[i] < 2^107 + 2^101 < 2^108 */
  1166. felem_shrink(small4, ftmp4);
  1167. x_equal = smallfelem_is_zero(small4);
  1168. /* z_out = ftmp5 * h */
  1169. felem_small_mul(tmp, small4, ftmp5);
  1170. felem_reduce(z_out, tmp);
  1171. /* z_out[i] < 2^101 */
  1172. /* ftmp = z1 * z1z1 */
  1173. smallfelem_mul(tmp, small1, small3);
  1174. felem_reduce(ftmp, tmp);
  1175. /* s2 = tmp = y2 * z1**3 */
  1176. felem_small_mul(tmp, y2, ftmp);
  1177. felem_reduce(ftmp5, tmp);
  1178. /* r = ftmp5 = (s2 - s1)*2 */
  1179. felem_diff_zero107(ftmp5, ftmp6);
  1180. /* ftmp5[i] < 2^107 + 2^107 = 2^108 */
  1181. felem_scalar(ftmp5, 2);
  1182. /* ftmp5[i] < 2^109 */
  1183. felem_shrink(small1, ftmp5);
  1184. y_equal = smallfelem_is_zero(small1);
  1185. /*
  1186. * The formulae are incorrect if the points are equal, in affine coordinates
  1187. * (X_1, Y_1) == (X_2, Y_2), so we check for this and do doubling if this
  1188. * happens.
  1189. *
  1190. * We use bitwise operations to avoid potential side-channels introduced by
  1191. * the short-circuiting behaviour of boolean operators.
  1192. *
  1193. * The special case of either point being the point at infinity (z1 and/or
  1194. * z2 are zero), is handled separately later on in this function, so we
  1195. * avoid jumping to point_double here in those special cases.
  1196. */
  1197. points_equal = (x_equal & y_equal & (~z1_is_zero) & (~z2_is_zero));
  1198. if (points_equal) {
  1199. /*
  1200. * This is obviously not constant-time but, as mentioned before, this
  1201. * case never happens during single point multiplication, so there is no
  1202. * timing leak for ECDH or ECDSA signing.
  1203. */
  1204. point_double(x3, y3, z3, x1, y1, z1);
  1205. return;
  1206. }
  1207. /* I = ftmp = (2h)**2 */
  1208. felem_assign(ftmp, ftmp4);
  1209. felem_scalar(ftmp, 2);
  1210. /* ftmp[i] < 2*2^108 = 2^109 */
  1211. felem_square(tmp, ftmp);
  1212. felem_reduce(ftmp, tmp);
  1213. /* J = ftmp2 = h * I */
  1214. felem_mul(tmp, ftmp4, ftmp);
  1215. felem_reduce(ftmp2, tmp);
  1216. /* V = ftmp4 = U1 * I */
  1217. felem_mul(tmp, ftmp3, ftmp);
  1218. felem_reduce(ftmp4, tmp);
  1219. /* x_out = r**2 - J - 2V */
  1220. smallfelem_square(tmp, small1);
  1221. felem_reduce(x_out, tmp);
  1222. felem_assign(ftmp3, ftmp4);
  1223. felem_scalar(ftmp4, 2);
  1224. felem_sum(ftmp4, ftmp2);
  1225. /* ftmp4[i] < 2*2^101 + 2^101 < 2^103 */
  1226. felem_diff(x_out, ftmp4);
  1227. /* x_out[i] < 2^105 + 2^101 */
  1228. /* y_out = r(V-x_out) - 2 * s1 * J */
  1229. felem_diff_zero107(ftmp3, x_out);
  1230. /* ftmp3[i] < 2^107 + 2^101 < 2^108 */
  1231. felem_small_mul(tmp, small1, ftmp3);
  1232. felem_mul(tmp2, ftmp6, ftmp2);
  1233. longfelem_scalar(tmp2, 2);
  1234. /* tmp2[i] < 2*2^67 = 2^68 */
  1235. longfelem_diff(tmp, tmp2);
  1236. /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
  1237. felem_reduce_zero105(y_out, tmp);
  1238. /* y_out[i] < 2^106 */
  1239. copy_small_conditional(x_out, x2, z1_is_zero);
  1240. copy_conditional(x_out, x1, z2_is_zero);
  1241. copy_small_conditional(y_out, y2, z1_is_zero);
  1242. copy_conditional(y_out, y1, z2_is_zero);
  1243. copy_small_conditional(z_out, z2, z1_is_zero);
  1244. copy_conditional(z_out, z1, z2_is_zero);
  1245. felem_assign(x3, x_out);
  1246. felem_assign(y3, y_out);
  1247. felem_assign(z3, z_out);
  1248. }
  1249. /*
  1250. * point_add_small is the same as point_add, except that it operates on
  1251. * smallfelems
  1252. */
  1253. static void point_add_small(smallfelem x3, smallfelem y3, smallfelem z3,
  1254. smallfelem x1, smallfelem y1, smallfelem z1,
  1255. smallfelem x2, smallfelem y2, smallfelem z2)
  1256. {
  1257. felem felem_x3, felem_y3, felem_z3;
  1258. felem felem_x1, felem_y1, felem_z1;
  1259. smallfelem_expand(felem_x1, x1);
  1260. smallfelem_expand(felem_y1, y1);
  1261. smallfelem_expand(felem_z1, z1);
  1262. point_add(felem_x3, felem_y3, felem_z3, felem_x1, felem_y1, felem_z1, 0,
  1263. x2, y2, z2);
  1264. felem_shrink(x3, felem_x3);
  1265. felem_shrink(y3, felem_y3);
  1266. felem_shrink(z3, felem_z3);
  1267. }
  1268. /*-
  1269. * Base point pre computation
  1270. * --------------------------
  1271. *
  1272. * Two different sorts of precomputed tables are used in the following code.
  1273. * Each contain various points on the curve, where each point is three field
  1274. * elements (x, y, z).
  1275. *
  1276. * For the base point table, z is usually 1 (0 for the point at infinity).
  1277. * This table has 2 * 16 elements, starting with the following:
  1278. * index | bits | point
  1279. * ------+---------+------------------------------
  1280. * 0 | 0 0 0 0 | 0G
  1281. * 1 | 0 0 0 1 | 1G
  1282. * 2 | 0 0 1 0 | 2^64G
  1283. * 3 | 0 0 1 1 | (2^64 + 1)G
  1284. * 4 | 0 1 0 0 | 2^128G
  1285. * 5 | 0 1 0 1 | (2^128 + 1)G
  1286. * 6 | 0 1 1 0 | (2^128 + 2^64)G
  1287. * 7 | 0 1 1 1 | (2^128 + 2^64 + 1)G
  1288. * 8 | 1 0 0 0 | 2^192G
  1289. * 9 | 1 0 0 1 | (2^192 + 1)G
  1290. * 10 | 1 0 1 0 | (2^192 + 2^64)G
  1291. * 11 | 1 0 1 1 | (2^192 + 2^64 + 1)G
  1292. * 12 | 1 1 0 0 | (2^192 + 2^128)G
  1293. * 13 | 1 1 0 1 | (2^192 + 2^128 + 1)G
  1294. * 14 | 1 1 1 0 | (2^192 + 2^128 + 2^64)G
  1295. * 15 | 1 1 1 1 | (2^192 + 2^128 + 2^64 + 1)G
  1296. * followed by a copy of this with each element multiplied by 2^32.
  1297. *
  1298. * The reason for this is so that we can clock bits into four different
  1299. * locations when doing simple scalar multiplies against the base point,
  1300. * and then another four locations using the second 16 elements.
  1301. *
  1302. * Tables for other points have table[i] = iG for i in 0 .. 16. */
  1303. /* gmul is the table of precomputed base points */
  1304. static const smallfelem gmul[2][16][3] = {
  1305. {{{0, 0, 0, 0},
  1306. {0, 0, 0, 0},
  1307. {0, 0, 0, 0}},
  1308. {{0xf4a13945d898c296, 0x77037d812deb33a0, 0xf8bce6e563a440f2,
  1309. 0x6b17d1f2e12c4247},
  1310. {0xcbb6406837bf51f5, 0x2bce33576b315ece, 0x8ee7eb4a7c0f9e16,
  1311. 0x4fe342e2fe1a7f9b},
  1312. {1, 0, 0, 0}},
  1313. {{0x90e75cb48e14db63, 0x29493baaad651f7e, 0x8492592e326e25de,
  1314. 0x0fa822bc2811aaa5},
  1315. {0xe41124545f462ee7, 0x34b1a65050fe82f5, 0x6f4ad4bcb3df188b,
  1316. 0xbff44ae8f5dba80d},
  1317. {1, 0, 0, 0}},
  1318. {{0x93391ce2097992af, 0xe96c98fd0d35f1fa, 0xb257c0de95e02789,
  1319. 0x300a4bbc89d6726f},
  1320. {0xaa54a291c08127a0, 0x5bb1eeada9d806a5, 0x7f1ddb25ff1e3c6f,
  1321. 0x72aac7e0d09b4644},
  1322. {1, 0, 0, 0}},
  1323. {{0x57c84fc9d789bd85, 0xfc35ff7dc297eac3, 0xfb982fd588c6766e,
  1324. 0x447d739beedb5e67},
  1325. {0x0c7e33c972e25b32, 0x3d349b95a7fae500, 0xe12e9d953a4aaff7,
  1326. 0x2d4825ab834131ee},
  1327. {1, 0, 0, 0}},
  1328. {{0x13949c932a1d367f, 0xef7fbd2b1a0a11b7, 0xddc6068bb91dfc60,
  1329. 0xef9519328a9c72ff},
  1330. {0x196035a77376d8a8, 0x23183b0895ca1740, 0xc1ee9807022c219c,
  1331. 0x611e9fc37dbb2c9b},
  1332. {1, 0, 0, 0}},
  1333. {{0xcae2b1920b57f4bc, 0x2936df5ec6c9bc36, 0x7dea6482e11238bf,
  1334. 0x550663797b51f5d8},
  1335. {0x44ffe216348a964c, 0x9fb3d576dbdefbe1, 0x0afa40018d9d50e5,
  1336. 0x157164848aecb851},
  1337. {1, 0, 0, 0}},
  1338. {{0xe48ecafffc5cde01, 0x7ccd84e70d715f26, 0xa2e8f483f43e4391,
  1339. 0xeb5d7745b21141ea},
  1340. {0xcac917e2731a3479, 0x85f22cfe2844b645, 0x0990e6a158006cee,
  1341. 0xeafd72ebdbecc17b},
  1342. {1, 0, 0, 0}},
  1343. {{0x6cf20ffb313728be, 0x96439591a3c6b94a, 0x2736ff8344315fc5,
  1344. 0xa6d39677a7849276},
  1345. {0xf2bab833c357f5f4, 0x824a920c2284059b, 0x66b8babd2d27ecdf,
  1346. 0x674f84749b0b8816},
  1347. {1, 0, 0, 0}},
  1348. {{0x2df48c04677c8a3e, 0x74e02f080203a56b, 0x31855f7db8c7fedb,
  1349. 0x4e769e7672c9ddad},
  1350. {0xa4c36165b824bbb0, 0xfb9ae16f3b9122a5, 0x1ec0057206947281,
  1351. 0x42b99082de830663},
  1352. {1, 0, 0, 0}},
  1353. {{0x6ef95150dda868b9, 0xd1f89e799c0ce131, 0x7fdc1ca008a1c478,
  1354. 0x78878ef61c6ce04d},
  1355. {0x9c62b9121fe0d976, 0x6ace570ebde08d4f, 0xde53142c12309def,
  1356. 0xb6cb3f5d7b72c321},
  1357. {1, 0, 0, 0}},
  1358. {{0x7f991ed2c31a3573, 0x5b82dd5bd54fb496, 0x595c5220812ffcae,
  1359. 0x0c88bc4d716b1287},
  1360. {0x3a57bf635f48aca8, 0x7c8181f4df2564f3, 0x18d1b5b39c04e6aa,
  1361. 0xdd5ddea3f3901dc6},
  1362. {1, 0, 0, 0}},
  1363. {{0xe96a79fb3e72ad0c, 0x43a0a28c42ba792f, 0xefe0a423083e49f3,
  1364. 0x68f344af6b317466},
  1365. {0xcdfe17db3fb24d4a, 0x668bfc2271f5c626, 0x604ed93c24d67ff3,
  1366. 0x31b9c405f8540a20},
  1367. {1, 0, 0, 0}},
  1368. {{0xd36b4789a2582e7f, 0x0d1a10144ec39c28, 0x663c62c3edbad7a0,
  1369. 0x4052bf4b6f461db9},
  1370. {0x235a27c3188d25eb, 0xe724f33999bfcc5b, 0x862be6bd71d70cc8,
  1371. 0xfecf4d5190b0fc61},
  1372. {1, 0, 0, 0}},
  1373. {{0x74346c10a1d4cfac, 0xafdf5cc08526a7a4, 0x123202a8f62bff7a,
  1374. 0x1eddbae2c802e41a},
  1375. {0x8fa0af2dd603f844, 0x36e06b7e4c701917, 0x0c45f45273db33a0,
  1376. 0x43104d86560ebcfc},
  1377. {1, 0, 0, 0}},
  1378. {{0x9615b5110d1d78e5, 0x66b0de3225c4744b, 0x0a4a46fb6aaf363a,
  1379. 0xb48e26b484f7a21c},
  1380. {0x06ebb0f621a01b2d, 0xc004e4048b7b0f98, 0x64131bcdfed6f668,
  1381. 0xfac015404d4d3dab},
  1382. {1, 0, 0, 0}}},
  1383. {{{0, 0, 0, 0},
  1384. {0, 0, 0, 0},
  1385. {0, 0, 0, 0}},
  1386. {{0x3a5a9e22185a5943, 0x1ab919365c65dfb6, 0x21656b32262c71da,
  1387. 0x7fe36b40af22af89},
  1388. {0xd50d152c699ca101, 0x74b3d5867b8af212, 0x9f09f40407dca6f1,
  1389. 0xe697d45825b63624},
  1390. {1, 0, 0, 0}},
  1391. {{0xa84aa9397512218e, 0xe9a521b074ca0141, 0x57880b3a18a2e902,
  1392. 0x4a5b506612a677a6},
  1393. {0x0beada7a4c4f3840, 0x626db15419e26d9d, 0xc42604fbe1627d40,
  1394. 0xeb13461ceac089f1},
  1395. {1, 0, 0, 0}},
  1396. {{0xf9faed0927a43281, 0x5e52c4144103ecbc, 0xc342967aa815c857,
  1397. 0x0781b8291c6a220a},
  1398. {0x5a8343ceeac55f80, 0x88f80eeee54a05e3, 0x97b2a14f12916434,
  1399. 0x690cde8df0151593},
  1400. {1, 0, 0, 0}},
  1401. {{0xaee9c75df7f82f2a, 0x9e4c35874afdf43a, 0xf5622df437371326,
  1402. 0x8a535f566ec73617},
  1403. {0xc5f9a0ac223094b7, 0xcde533864c8c7669, 0x37e02819085a92bf,
  1404. 0x0455c08468b08bd7},
  1405. {1, 0, 0, 0}},
  1406. {{0x0c0a6e2c9477b5d9, 0xf9a4bf62876dc444, 0x5050a949b6cdc279,
  1407. 0x06bada7ab77f8276},
  1408. {0xc8b4aed1ea48dac9, 0xdebd8a4b7ea1070f, 0x427d49101366eb70,
  1409. 0x5b476dfd0e6cb18a},
  1410. {1, 0, 0, 0}},
  1411. {{0x7c5c3e44278c340a, 0x4d54606812d66f3b, 0x29a751b1ae23c5d8,
  1412. 0x3e29864e8a2ec908},
  1413. {0x142d2a6626dbb850, 0xad1744c4765bd780, 0x1f150e68e322d1ed,
  1414. 0x239b90ea3dc31e7e},
  1415. {1, 0, 0, 0}},
  1416. {{0x78c416527a53322a, 0x305dde6709776f8e, 0xdbcab759f8862ed4,
  1417. 0x820f4dd949f72ff7},
  1418. {0x6cc544a62b5debd4, 0x75be5d937b4e8cc4, 0x1b481b1b215c14d3,
  1419. 0x140406ec783a05ec},
  1420. {1, 0, 0, 0}},
  1421. {{0x6a703f10e895df07, 0xfd75f3fa01876bd8, 0xeb5b06e70ce08ffe,
  1422. 0x68f6b8542783dfee},
  1423. {0x90c76f8a78712655, 0xcf5293d2f310bf7f, 0xfbc8044dfda45028,
  1424. 0xcbe1feba92e40ce6},
  1425. {1, 0, 0, 0}},
  1426. {{0xe998ceea4396e4c1, 0xfc82ef0b6acea274, 0x230f729f2250e927,
  1427. 0xd0b2f94d2f420109},
  1428. {0x4305adddb38d4966, 0x10b838f8624c3b45, 0x7db2636658954e7a,
  1429. 0x971459828b0719e5},
  1430. {1, 0, 0, 0}},
  1431. {{0x4bd6b72623369fc9, 0x57f2929e53d0b876, 0xc2d5cba4f2340687,
  1432. 0x961610004a866aba},
  1433. {0x49997bcd2e407a5e, 0x69ab197d92ddcb24, 0x2cf1f2438fe5131c,
  1434. 0x7acb9fadcee75e44},
  1435. {1, 0, 0, 0}},
  1436. {{0x254e839423d2d4c0, 0xf57f0c917aea685b, 0xa60d880f6f75aaea,
  1437. 0x24eb9acca333bf5b},
  1438. {0xe3de4ccb1cda5dea, 0xfeef9341c51a6b4f, 0x743125f88bac4c4d,
  1439. 0x69f891c5acd079cc},
  1440. {1, 0, 0, 0}},
  1441. {{0xeee44b35702476b5, 0x7ed031a0e45c2258, 0xb422d1e7bd6f8514,
  1442. 0xe51f547c5972a107},
  1443. {0xa25bcd6fc9cf343d, 0x8ca922ee097c184e, 0xa62f98b3a9fe9a06,
  1444. 0x1c309a2b25bb1387},
  1445. {1, 0, 0, 0}},
  1446. {{0x9295dbeb1967c459, 0xb00148833472c98e, 0xc504977708011828,
  1447. 0x20b87b8aa2c4e503},
  1448. {0x3063175de057c277, 0x1bd539338fe582dd, 0x0d11adef5f69a044,
  1449. 0xf5c6fa49919776be},
  1450. {1, 0, 0, 0}},
  1451. {{0x8c944e760fd59e11, 0x3876cba1102fad5f, 0xa454c3fad83faa56,
  1452. 0x1ed7d1b9332010b9},
  1453. {0xa1011a270024b889, 0x05e4d0dcac0cd344, 0x52b520f0eb6a2a24,
  1454. 0x3a2b03f03217257a},
  1455. {1, 0, 0, 0}},
  1456. {{0xf20fc2afdf1d043d, 0xf330240db58d5a62, 0xfc7d229ca0058c3b,
  1457. 0x15fee545c78dd9f6},
  1458. {0x501e82885bc98cda, 0x41ef80e5d046ac04, 0x557d9f49461210fb,
  1459. 0x4ab5b6b2b8753f81},
  1460. {1, 0, 0, 0}}}
  1461. };
  1462. /*
  1463. * select_point selects the |idx|th point from a precomputation table and
  1464. * copies it to out.
  1465. */
  1466. static void select_point(const u64 idx, unsigned int size,
  1467. const smallfelem pre_comp[16][3], smallfelem out[3])
  1468. {
  1469. unsigned i, j;
  1470. u64 *outlimbs = &out[0][0];
  1471. memset(out, 0, sizeof(*out) * 3);
  1472. for (i = 0; i < size; i++) {
  1473. const u64 *inlimbs = (u64 *)&pre_comp[i][0][0];
  1474. u64 mask = i ^ idx;
  1475. mask |= mask >> 4;
  1476. mask |= mask >> 2;
  1477. mask |= mask >> 1;
  1478. mask &= 1;
  1479. mask--;
  1480. for (j = 0; j < NLIMBS * 3; j++)
  1481. outlimbs[j] |= inlimbs[j] & mask;
  1482. }
  1483. }
  1484. /* get_bit returns the |i|th bit in |in| */
  1485. static char get_bit(const felem_bytearray in, int i)
  1486. {
  1487. if ((i < 0) || (i >= 256))
  1488. return 0;
  1489. return (in[i >> 3] >> (i & 7)) & 1;
  1490. }
  1491. /*
  1492. * Interleaved point multiplication using precomputed point multiples: The
  1493. * small point multiples 0*P, 1*P, ..., 17*P are in pre_comp[], the scalars
  1494. * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
  1495. * generator, using certain (large) precomputed multiples in g_pre_comp.
  1496. * Output point (X, Y, Z) is stored in x_out, y_out, z_out
  1497. */
  1498. static void batch_mul(felem x_out, felem y_out, felem z_out,
  1499. const felem_bytearray scalars[],
  1500. const unsigned num_points, const u8 *g_scalar,
  1501. const int mixed, const smallfelem pre_comp[][17][3],
  1502. const smallfelem g_pre_comp[2][16][3])
  1503. {
  1504. int i, skip;
  1505. unsigned num, gen_mul = (g_scalar != NULL);
  1506. felem nq[3], ftmp;
  1507. smallfelem tmp[3];
  1508. u64 bits;
  1509. u8 sign, digit;
  1510. /* set nq to the point at infinity */
  1511. memset(nq, 0, sizeof(nq));
  1512. /*
  1513. * Loop over all scalars msb-to-lsb, interleaving additions of multiples
  1514. * of the generator (two in each of the last 32 rounds) and additions of
  1515. * other points multiples (every 5th round).
  1516. */
  1517. skip = 1; /* save two point operations in the first
  1518. * round */
  1519. for (i = (num_points ? 255 : 31); i >= 0; --i) {
  1520. /* double */
  1521. if (!skip)
  1522. point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
  1523. /* add multiples of the generator */
  1524. if (gen_mul && (i <= 31)) {
  1525. /* first, look 32 bits upwards */
  1526. bits = get_bit(g_scalar, i + 224) << 3;
  1527. bits |= get_bit(g_scalar, i + 160) << 2;
  1528. bits |= get_bit(g_scalar, i + 96) << 1;
  1529. bits |= get_bit(g_scalar, i + 32);
  1530. /* select the point to add, in constant time */
  1531. select_point(bits, 16, g_pre_comp[1], tmp);
  1532. if (!skip) {
  1533. /* Arg 1 below is for "mixed" */
  1534. point_add(nq[0], nq[1], nq[2],
  1535. nq[0], nq[1], nq[2], 1, tmp[0], tmp[1], tmp[2]);
  1536. } else {
  1537. smallfelem_expand(nq[0], tmp[0]);
  1538. smallfelem_expand(nq[1], tmp[1]);
  1539. smallfelem_expand(nq[2], tmp[2]);
  1540. skip = 0;
  1541. }
  1542. /* second, look at the current position */
  1543. bits = get_bit(g_scalar, i + 192) << 3;
  1544. bits |= get_bit(g_scalar, i + 128) << 2;
  1545. bits |= get_bit(g_scalar, i + 64) << 1;
  1546. bits |= get_bit(g_scalar, i);
  1547. /* select the point to add, in constant time */
  1548. select_point(bits, 16, g_pre_comp[0], tmp);
  1549. /* Arg 1 below is for "mixed" */
  1550. point_add(nq[0], nq[1], nq[2],
  1551. nq[0], nq[1], nq[2], 1, tmp[0], tmp[1], tmp[2]);
  1552. }
  1553. /* do other additions every 5 doublings */
  1554. if (num_points && (i % 5 == 0)) {
  1555. /* loop over all scalars */
  1556. for (num = 0; num < num_points; ++num) {
  1557. bits = get_bit(scalars[num], i + 4) << 5;
  1558. bits |= get_bit(scalars[num], i + 3) << 4;
  1559. bits |= get_bit(scalars[num], i + 2) << 3;
  1560. bits |= get_bit(scalars[num], i + 1) << 2;
  1561. bits |= get_bit(scalars[num], i) << 1;
  1562. bits |= get_bit(scalars[num], i - 1);
  1563. ossl_ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
  1564. /*
  1565. * select the point to add or subtract, in constant time
  1566. */
  1567. select_point(digit, 17, pre_comp[num], tmp);
  1568. smallfelem_neg(ftmp, tmp[1]); /* (X, -Y, Z) is the negative
  1569. * point */
  1570. copy_small_conditional(ftmp, tmp[1], (((limb) sign) - 1));
  1571. felem_contract(tmp[1], ftmp);
  1572. if (!skip) {
  1573. point_add(nq[0], nq[1], nq[2],
  1574. nq[0], nq[1], nq[2],
  1575. mixed, tmp[0], tmp[1], tmp[2]);
  1576. } else {
  1577. smallfelem_expand(nq[0], tmp[0]);
  1578. smallfelem_expand(nq[1], tmp[1]);
  1579. smallfelem_expand(nq[2], tmp[2]);
  1580. skip = 0;
  1581. }
  1582. }
  1583. }
  1584. }
  1585. felem_assign(x_out, nq[0]);
  1586. felem_assign(y_out, nq[1]);
  1587. felem_assign(z_out, nq[2]);
  1588. }
  1589. /* Precomputation for the group generator. */
  1590. struct nistp256_pre_comp_st {
  1591. smallfelem g_pre_comp[2][16][3];
  1592. CRYPTO_REF_COUNT references;
  1593. CRYPTO_RWLOCK *lock;
  1594. };
  1595. const EC_METHOD *EC_GFp_nistp256_method(void)
  1596. {
  1597. static const EC_METHOD ret = {
  1598. EC_FLAGS_DEFAULT_OCT,
  1599. NID_X9_62_prime_field,
  1600. ossl_ec_GFp_nistp256_group_init,
  1601. ossl_ec_GFp_simple_group_finish,
  1602. ossl_ec_GFp_simple_group_clear_finish,
  1603. ossl_ec_GFp_nist_group_copy,
  1604. ossl_ec_GFp_nistp256_group_set_curve,
  1605. ossl_ec_GFp_simple_group_get_curve,
  1606. ossl_ec_GFp_simple_group_get_degree,
  1607. ossl_ec_group_simple_order_bits,
  1608. ossl_ec_GFp_simple_group_check_discriminant,
  1609. ossl_ec_GFp_simple_point_init,
  1610. ossl_ec_GFp_simple_point_finish,
  1611. ossl_ec_GFp_simple_point_clear_finish,
  1612. ossl_ec_GFp_simple_point_copy,
  1613. ossl_ec_GFp_simple_point_set_to_infinity,
  1614. ossl_ec_GFp_simple_point_set_affine_coordinates,
  1615. ossl_ec_GFp_nistp256_point_get_affine_coordinates,
  1616. 0 /* point_set_compressed_coordinates */ ,
  1617. 0 /* point2oct */ ,
  1618. 0 /* oct2point */ ,
  1619. ossl_ec_GFp_simple_add,
  1620. ossl_ec_GFp_simple_dbl,
  1621. ossl_ec_GFp_simple_invert,
  1622. ossl_ec_GFp_simple_is_at_infinity,
  1623. ossl_ec_GFp_simple_is_on_curve,
  1624. ossl_ec_GFp_simple_cmp,
  1625. ossl_ec_GFp_simple_make_affine,
  1626. ossl_ec_GFp_simple_points_make_affine,
  1627. ossl_ec_GFp_nistp256_points_mul,
  1628. ossl_ec_GFp_nistp256_precompute_mult,
  1629. ossl_ec_GFp_nistp256_have_precompute_mult,
  1630. ossl_ec_GFp_nist_field_mul,
  1631. ossl_ec_GFp_nist_field_sqr,
  1632. 0 /* field_div */ ,
  1633. ossl_ec_GFp_simple_field_inv,
  1634. 0 /* field_encode */ ,
  1635. 0 /* field_decode */ ,
  1636. 0, /* field_set_to_one */
  1637. ossl_ec_key_simple_priv2oct,
  1638. ossl_ec_key_simple_oct2priv,
  1639. 0, /* set private */
  1640. ossl_ec_key_simple_generate_key,
  1641. ossl_ec_key_simple_check_key,
  1642. ossl_ec_key_simple_generate_public_key,
  1643. 0, /* keycopy */
  1644. 0, /* keyfinish */
  1645. ossl_ecdh_simple_compute_key,
  1646. ossl_ecdsa_simple_sign_setup,
  1647. ossl_ecdsa_simple_sign_sig,
  1648. ossl_ecdsa_simple_verify_sig,
  1649. 0, /* field_inverse_mod_ord */
  1650. 0, /* blind_coordinates */
  1651. 0, /* ladder_pre */
  1652. 0, /* ladder_step */
  1653. 0 /* ladder_post */
  1654. };
  1655. return &ret;
  1656. }
  1657. /******************************************************************************/
  1658. /*
  1659. * FUNCTIONS TO MANAGE PRECOMPUTATION
  1660. */
  1661. static NISTP256_PRE_COMP *nistp256_pre_comp_new(void)
  1662. {
  1663. NISTP256_PRE_COMP *ret = OPENSSL_zalloc(sizeof(*ret));
  1664. if (ret == NULL) {
  1665. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  1666. return ret;
  1667. }
  1668. ret->references = 1;
  1669. ret->lock = CRYPTO_THREAD_lock_new();
  1670. if (ret->lock == NULL) {
  1671. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  1672. OPENSSL_free(ret);
  1673. return NULL;
  1674. }
  1675. return ret;
  1676. }
  1677. NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *p)
  1678. {
  1679. int i;
  1680. if (p != NULL)
  1681. CRYPTO_UP_REF(&p->references, &i, p->lock);
  1682. return p;
  1683. }
  1684. void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *pre)
  1685. {
  1686. int i;
  1687. if (pre == NULL)
  1688. return;
  1689. CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
  1690. REF_PRINT_COUNT("EC_nistp256", pre);
  1691. if (i > 0)
  1692. return;
  1693. REF_ASSERT_ISNT(i < 0);
  1694. CRYPTO_THREAD_lock_free(pre->lock);
  1695. OPENSSL_free(pre);
  1696. }
  1697. /******************************************************************************/
  1698. /*
  1699. * OPENSSL EC_METHOD FUNCTIONS
  1700. */
  1701. int ossl_ec_GFp_nistp256_group_init(EC_GROUP *group)
  1702. {
  1703. int ret;
  1704. ret = ossl_ec_GFp_simple_group_init(group);
  1705. group->a_is_minus3 = 1;
  1706. return ret;
  1707. }
  1708. int ossl_ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  1709. const BIGNUM *a, const BIGNUM *b,
  1710. BN_CTX *ctx)
  1711. {
  1712. int ret = 0;
  1713. BIGNUM *curve_p, *curve_a, *curve_b;
  1714. #ifndef FIPS_MODULE
  1715. BN_CTX *new_ctx = NULL;
  1716. if (ctx == NULL)
  1717. ctx = new_ctx = BN_CTX_new();
  1718. #endif
  1719. if (ctx == NULL)
  1720. return 0;
  1721. BN_CTX_start(ctx);
  1722. curve_p = BN_CTX_get(ctx);
  1723. curve_a = BN_CTX_get(ctx);
  1724. curve_b = BN_CTX_get(ctx);
  1725. if (curve_b == NULL)
  1726. goto err;
  1727. BN_bin2bn(nistp256_curve_params[0], sizeof(felem_bytearray), curve_p);
  1728. BN_bin2bn(nistp256_curve_params[1], sizeof(felem_bytearray), curve_a);
  1729. BN_bin2bn(nistp256_curve_params[2], sizeof(felem_bytearray), curve_b);
  1730. if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
  1731. ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
  1732. goto err;
  1733. }
  1734. group->field_mod_func = BN_nist_mod_256;
  1735. ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  1736. err:
  1737. BN_CTX_end(ctx);
  1738. #ifndef FIPS_MODULE
  1739. BN_CTX_free(new_ctx);
  1740. #endif
  1741. return ret;
  1742. }
  1743. /*
  1744. * Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
  1745. * (X/Z^2, Y/Z^3)
  1746. */
  1747. int ossl_ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
  1748. const EC_POINT *point,
  1749. BIGNUM *x, BIGNUM *y,
  1750. BN_CTX *ctx)
  1751. {
  1752. felem z1, z2, x_in, y_in;
  1753. smallfelem x_out, y_out;
  1754. longfelem tmp;
  1755. if (EC_POINT_is_at_infinity(group, point)) {
  1756. ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
  1757. return 0;
  1758. }
  1759. if ((!BN_to_felem(x_in, point->X)) || (!BN_to_felem(y_in, point->Y)) ||
  1760. (!BN_to_felem(z1, point->Z)))
  1761. return 0;
  1762. felem_inv(z2, z1);
  1763. felem_square(tmp, z2);
  1764. felem_reduce(z1, tmp);
  1765. felem_mul(tmp, x_in, z1);
  1766. felem_reduce(x_in, tmp);
  1767. felem_contract(x_out, x_in);
  1768. if (x != NULL) {
  1769. if (!smallfelem_to_BN(x, x_out)) {
  1770. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1771. return 0;
  1772. }
  1773. }
  1774. felem_mul(tmp, z1, z2);
  1775. felem_reduce(z1, tmp);
  1776. felem_mul(tmp, y_in, z1);
  1777. felem_reduce(y_in, tmp);
  1778. felem_contract(y_out, y_in);
  1779. if (y != NULL) {
  1780. if (!smallfelem_to_BN(y, y_out)) {
  1781. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1782. return 0;
  1783. }
  1784. }
  1785. return 1;
  1786. }
  1787. /* points below is of size |num|, and tmp_smallfelems is of size |num+1| */
  1788. static void make_points_affine(size_t num, smallfelem points[][3],
  1789. smallfelem tmp_smallfelems[])
  1790. {
  1791. /*
  1792. * Runs in constant time, unless an input is the point at infinity (which
  1793. * normally shouldn't happen).
  1794. */
  1795. ossl_ec_GFp_nistp_points_make_affine_internal(num,
  1796. points,
  1797. sizeof(smallfelem),
  1798. tmp_smallfelems,
  1799. (void (*)(void *))smallfelem_one,
  1800. smallfelem_is_zero_int,
  1801. (void (*)(void *, const void *))
  1802. smallfelem_assign,
  1803. (void (*)(void *, const void *))
  1804. smallfelem_square_contract,
  1805. (void (*)
  1806. (void *, const void *,
  1807. const void *))
  1808. smallfelem_mul_contract,
  1809. (void (*)(void *, const void *))
  1810. smallfelem_inv_contract,
  1811. /* nothing to contract */
  1812. (void (*)(void *, const void *))
  1813. smallfelem_assign);
  1814. }
  1815. /*
  1816. * Computes scalar*generator + \sum scalars[i]*points[i], ignoring NULL
  1817. * values Result is stored in r (r can equal one of the inputs).
  1818. */
  1819. int ossl_ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
  1820. const BIGNUM *scalar, size_t num,
  1821. const EC_POINT *points[],
  1822. const BIGNUM *scalars[], BN_CTX *ctx)
  1823. {
  1824. int ret = 0;
  1825. int j;
  1826. int mixed = 0;
  1827. BIGNUM *x, *y, *z, *tmp_scalar;
  1828. felem_bytearray g_secret;
  1829. felem_bytearray *secrets = NULL;
  1830. smallfelem (*pre_comp)[17][3] = NULL;
  1831. smallfelem *tmp_smallfelems = NULL;
  1832. unsigned i;
  1833. int num_bytes;
  1834. int have_pre_comp = 0;
  1835. size_t num_points = num;
  1836. smallfelem x_in, y_in, z_in;
  1837. felem x_out, y_out, z_out;
  1838. NISTP256_PRE_COMP *pre = NULL;
  1839. const smallfelem(*g_pre_comp)[16][3] = NULL;
  1840. EC_POINT *generator = NULL;
  1841. const EC_POINT *p = NULL;
  1842. const BIGNUM *p_scalar = NULL;
  1843. BN_CTX_start(ctx);
  1844. x = BN_CTX_get(ctx);
  1845. y = BN_CTX_get(ctx);
  1846. z = BN_CTX_get(ctx);
  1847. tmp_scalar = BN_CTX_get(ctx);
  1848. if (tmp_scalar == NULL)
  1849. goto err;
  1850. if (scalar != NULL) {
  1851. pre = group->pre_comp.nistp256;
  1852. if (pre)
  1853. /* we have precomputation, try to use it */
  1854. g_pre_comp = (const smallfelem(*)[16][3])pre->g_pre_comp;
  1855. else
  1856. /* try to use the standard precomputation */
  1857. g_pre_comp = &gmul[0];
  1858. generator = EC_POINT_new(group);
  1859. if (generator == NULL)
  1860. goto err;
  1861. /* get the generator from precomputation */
  1862. if (!smallfelem_to_BN(x, g_pre_comp[0][1][0]) ||
  1863. !smallfelem_to_BN(y, g_pre_comp[0][1][1]) ||
  1864. !smallfelem_to_BN(z, g_pre_comp[0][1][2])) {
  1865. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1866. goto err;
  1867. }
  1868. if (!ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group,
  1869. generator,
  1870. x, y, z, ctx))
  1871. goto err;
  1872. if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
  1873. /* precomputation matches generator */
  1874. have_pre_comp = 1;
  1875. else
  1876. /*
  1877. * we don't have valid precomputation: treat the generator as a
  1878. * random point
  1879. */
  1880. num_points++;
  1881. }
  1882. if (num_points > 0) {
  1883. if (num_points >= 3) {
  1884. /*
  1885. * unless we precompute multiples for just one or two points,
  1886. * converting those into affine form is time well spent
  1887. */
  1888. mixed = 1;
  1889. }
  1890. secrets = OPENSSL_malloc(sizeof(*secrets) * num_points);
  1891. pre_comp = OPENSSL_malloc(sizeof(*pre_comp) * num_points);
  1892. if (mixed)
  1893. tmp_smallfelems =
  1894. OPENSSL_malloc(sizeof(*tmp_smallfelems) * (num_points * 17 + 1));
  1895. if ((secrets == NULL) || (pre_comp == NULL)
  1896. || (mixed && (tmp_smallfelems == NULL))) {
  1897. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  1898. goto err;
  1899. }
  1900. /*
  1901. * we treat NULL scalars as 0, and NULL points as points at infinity,
  1902. * i.e., they contribute nothing to the linear combination
  1903. */
  1904. memset(secrets, 0, sizeof(*secrets) * num_points);
  1905. memset(pre_comp, 0, sizeof(*pre_comp) * num_points);
  1906. for (i = 0; i < num_points; ++i) {
  1907. if (i == num) {
  1908. /*
  1909. * we didn't have a valid precomputation, so we pick the
  1910. * generator
  1911. */
  1912. p = EC_GROUP_get0_generator(group);
  1913. p_scalar = scalar;
  1914. } else {
  1915. /* the i^th point */
  1916. p = points[i];
  1917. p_scalar = scalars[i];
  1918. }
  1919. if ((p_scalar != NULL) && (p != NULL)) {
  1920. /* reduce scalar to 0 <= scalar < 2^256 */
  1921. if ((BN_num_bits(p_scalar) > 256)
  1922. || (BN_is_negative(p_scalar))) {
  1923. /*
  1924. * this is an unusual input, and we don't guarantee
  1925. * constant-timeness
  1926. */
  1927. if (!BN_nnmod(tmp_scalar, p_scalar, group->order, ctx)) {
  1928. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1929. goto err;
  1930. }
  1931. num_bytes = BN_bn2lebinpad(tmp_scalar,
  1932. secrets[i], sizeof(secrets[i]));
  1933. } else {
  1934. num_bytes = BN_bn2lebinpad(p_scalar,
  1935. secrets[i], sizeof(secrets[i]));
  1936. }
  1937. if (num_bytes < 0) {
  1938. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1939. goto err;
  1940. }
  1941. /* precompute multiples */
  1942. if ((!BN_to_felem(x_out, p->X)) ||
  1943. (!BN_to_felem(y_out, p->Y)) ||
  1944. (!BN_to_felem(z_out, p->Z)))
  1945. goto err;
  1946. felem_shrink(pre_comp[i][1][0], x_out);
  1947. felem_shrink(pre_comp[i][1][1], y_out);
  1948. felem_shrink(pre_comp[i][1][2], z_out);
  1949. for (j = 2; j <= 16; ++j) {
  1950. if (j & 1) {
  1951. point_add_small(pre_comp[i][j][0], pre_comp[i][j][1],
  1952. pre_comp[i][j][2], pre_comp[i][1][0],
  1953. pre_comp[i][1][1], pre_comp[i][1][2],
  1954. pre_comp[i][j - 1][0],
  1955. pre_comp[i][j - 1][1],
  1956. pre_comp[i][j - 1][2]);
  1957. } else {
  1958. point_double_small(pre_comp[i][j][0],
  1959. pre_comp[i][j][1],
  1960. pre_comp[i][j][2],
  1961. pre_comp[i][j / 2][0],
  1962. pre_comp[i][j / 2][1],
  1963. pre_comp[i][j / 2][2]);
  1964. }
  1965. }
  1966. }
  1967. }
  1968. if (mixed)
  1969. make_points_affine(num_points * 17, pre_comp[0], tmp_smallfelems);
  1970. }
  1971. /* the scalar for the generator */
  1972. if ((scalar != NULL) && (have_pre_comp)) {
  1973. memset(g_secret, 0, sizeof(g_secret));
  1974. /* reduce scalar to 0 <= scalar < 2^256 */
  1975. if ((BN_num_bits(scalar) > 256) || (BN_is_negative(scalar))) {
  1976. /*
  1977. * this is an unusual input, and we don't guarantee
  1978. * constant-timeness
  1979. */
  1980. if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
  1981. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1982. goto err;
  1983. }
  1984. num_bytes = BN_bn2lebinpad(tmp_scalar, g_secret, sizeof(g_secret));
  1985. } else {
  1986. num_bytes = BN_bn2lebinpad(scalar, g_secret, sizeof(g_secret));
  1987. }
  1988. /* do the multiplication with generator precomputation */
  1989. batch_mul(x_out, y_out, z_out,
  1990. (const felem_bytearray(*))secrets, num_points,
  1991. g_secret,
  1992. mixed, (const smallfelem(*)[17][3])pre_comp, g_pre_comp);
  1993. } else {
  1994. /* do the multiplication without generator precomputation */
  1995. batch_mul(x_out, y_out, z_out,
  1996. (const felem_bytearray(*))secrets, num_points,
  1997. NULL, mixed, (const smallfelem(*)[17][3])pre_comp, NULL);
  1998. }
  1999. /* reduce the output to its unique minimal representation */
  2000. felem_contract(x_in, x_out);
  2001. felem_contract(y_in, y_out);
  2002. felem_contract(z_in, z_out);
  2003. if ((!smallfelem_to_BN(x, x_in)) || (!smallfelem_to_BN(y, y_in)) ||
  2004. (!smallfelem_to_BN(z, z_in))) {
  2005. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  2006. goto err;
  2007. }
  2008. ret = ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, r, x, y, z,
  2009. ctx);
  2010. err:
  2011. BN_CTX_end(ctx);
  2012. EC_POINT_free(generator);
  2013. OPENSSL_free(secrets);
  2014. OPENSSL_free(pre_comp);
  2015. OPENSSL_free(tmp_smallfelems);
  2016. return ret;
  2017. }
  2018. int ossl_ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  2019. {
  2020. int ret = 0;
  2021. NISTP256_PRE_COMP *pre = NULL;
  2022. int i, j;
  2023. BIGNUM *x, *y;
  2024. EC_POINT *generator = NULL;
  2025. smallfelem tmp_smallfelems[32];
  2026. felem x_tmp, y_tmp, z_tmp;
  2027. #ifndef FIPS_MODULE
  2028. BN_CTX *new_ctx = NULL;
  2029. #endif
  2030. /* throw away old precomputation */
  2031. EC_pre_comp_free(group);
  2032. #ifndef FIPS_MODULE
  2033. if (ctx == NULL)
  2034. ctx = new_ctx = BN_CTX_new();
  2035. #endif
  2036. if (ctx == NULL)
  2037. return 0;
  2038. BN_CTX_start(ctx);
  2039. x = BN_CTX_get(ctx);
  2040. y = BN_CTX_get(ctx);
  2041. if (y == NULL)
  2042. goto err;
  2043. /* get the generator */
  2044. if (group->generator == NULL)
  2045. goto err;
  2046. generator = EC_POINT_new(group);
  2047. if (generator == NULL)
  2048. goto err;
  2049. BN_bin2bn(nistp256_curve_params[3], sizeof(felem_bytearray), x);
  2050. BN_bin2bn(nistp256_curve_params[4], sizeof(felem_bytearray), y);
  2051. if (!EC_POINT_set_affine_coordinates(group, generator, x, y, ctx))
  2052. goto err;
  2053. if ((pre = nistp256_pre_comp_new()) == NULL)
  2054. goto err;
  2055. /*
  2056. * if the generator is the standard one, use built-in precomputation
  2057. */
  2058. if (0 == EC_POINT_cmp(group, generator, group->generator, ctx)) {
  2059. memcpy(pre->g_pre_comp, gmul, sizeof(pre->g_pre_comp));
  2060. goto done;
  2061. }
  2062. if ((!BN_to_felem(x_tmp, group->generator->X)) ||
  2063. (!BN_to_felem(y_tmp, group->generator->Y)) ||
  2064. (!BN_to_felem(z_tmp, group->generator->Z)))
  2065. goto err;
  2066. felem_shrink(pre->g_pre_comp[0][1][0], x_tmp);
  2067. felem_shrink(pre->g_pre_comp[0][1][1], y_tmp);
  2068. felem_shrink(pre->g_pre_comp[0][1][2], z_tmp);
  2069. /*
  2070. * compute 2^64*G, 2^128*G, 2^192*G for the first table, 2^32*G, 2^96*G,
  2071. * 2^160*G, 2^224*G for the second one
  2072. */
  2073. for (i = 1; i <= 8; i <<= 1) {
  2074. point_double_small(pre->g_pre_comp[1][i][0], pre->g_pre_comp[1][i][1],
  2075. pre->g_pre_comp[1][i][2], pre->g_pre_comp[0][i][0],
  2076. pre->g_pre_comp[0][i][1],
  2077. pre->g_pre_comp[0][i][2]);
  2078. for (j = 0; j < 31; ++j) {
  2079. point_double_small(pre->g_pre_comp[1][i][0],
  2080. pre->g_pre_comp[1][i][1],
  2081. pre->g_pre_comp[1][i][2],
  2082. pre->g_pre_comp[1][i][0],
  2083. pre->g_pre_comp[1][i][1],
  2084. pre->g_pre_comp[1][i][2]);
  2085. }
  2086. if (i == 8)
  2087. break;
  2088. point_double_small(pre->g_pre_comp[0][2 * i][0],
  2089. pre->g_pre_comp[0][2 * i][1],
  2090. pre->g_pre_comp[0][2 * i][2],
  2091. pre->g_pre_comp[1][i][0], pre->g_pre_comp[1][i][1],
  2092. pre->g_pre_comp[1][i][2]);
  2093. for (j = 0; j < 31; ++j) {
  2094. point_double_small(pre->g_pre_comp[0][2 * i][0],
  2095. pre->g_pre_comp[0][2 * i][1],
  2096. pre->g_pre_comp[0][2 * i][2],
  2097. pre->g_pre_comp[0][2 * i][0],
  2098. pre->g_pre_comp[0][2 * i][1],
  2099. pre->g_pre_comp[0][2 * i][2]);
  2100. }
  2101. }
  2102. for (i = 0; i < 2; i++) {
  2103. /* g_pre_comp[i][0] is the point at infinity */
  2104. memset(pre->g_pre_comp[i][0], 0, sizeof(pre->g_pre_comp[i][0]));
  2105. /* the remaining multiples */
  2106. /* 2^64*G + 2^128*G resp. 2^96*G + 2^160*G */
  2107. point_add_small(pre->g_pre_comp[i][6][0], pre->g_pre_comp[i][6][1],
  2108. pre->g_pre_comp[i][6][2], pre->g_pre_comp[i][4][0],
  2109. pre->g_pre_comp[i][4][1], pre->g_pre_comp[i][4][2],
  2110. pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
  2111. pre->g_pre_comp[i][2][2]);
  2112. /* 2^64*G + 2^192*G resp. 2^96*G + 2^224*G */
  2113. point_add_small(pre->g_pre_comp[i][10][0], pre->g_pre_comp[i][10][1],
  2114. pre->g_pre_comp[i][10][2], pre->g_pre_comp[i][8][0],
  2115. pre->g_pre_comp[i][8][1], pre->g_pre_comp[i][8][2],
  2116. pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
  2117. pre->g_pre_comp[i][2][2]);
  2118. /* 2^128*G + 2^192*G resp. 2^160*G + 2^224*G */
  2119. point_add_small(pre->g_pre_comp[i][12][0], pre->g_pre_comp[i][12][1],
  2120. pre->g_pre_comp[i][12][2], pre->g_pre_comp[i][8][0],
  2121. pre->g_pre_comp[i][8][1], pre->g_pre_comp[i][8][2],
  2122. pre->g_pre_comp[i][4][0], pre->g_pre_comp[i][4][1],
  2123. pre->g_pre_comp[i][4][2]);
  2124. /*
  2125. * 2^64*G + 2^128*G + 2^192*G resp. 2^96*G + 2^160*G + 2^224*G
  2126. */
  2127. point_add_small(pre->g_pre_comp[i][14][0], pre->g_pre_comp[i][14][1],
  2128. pre->g_pre_comp[i][14][2], pre->g_pre_comp[i][12][0],
  2129. pre->g_pre_comp[i][12][1], pre->g_pre_comp[i][12][2],
  2130. pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
  2131. pre->g_pre_comp[i][2][2]);
  2132. for (j = 1; j < 8; ++j) {
  2133. /* odd multiples: add G resp. 2^32*G */
  2134. point_add_small(pre->g_pre_comp[i][2 * j + 1][0],
  2135. pre->g_pre_comp[i][2 * j + 1][1],
  2136. pre->g_pre_comp[i][2 * j + 1][2],
  2137. pre->g_pre_comp[i][2 * j][0],
  2138. pre->g_pre_comp[i][2 * j][1],
  2139. pre->g_pre_comp[i][2 * j][2],
  2140. pre->g_pre_comp[i][1][0],
  2141. pre->g_pre_comp[i][1][1],
  2142. pre->g_pre_comp[i][1][2]);
  2143. }
  2144. }
  2145. make_points_affine(31, &(pre->g_pre_comp[0][1]), tmp_smallfelems);
  2146. done:
  2147. SETPRECOMP(group, nistp256, pre);
  2148. pre = NULL;
  2149. ret = 1;
  2150. err:
  2151. BN_CTX_end(ctx);
  2152. EC_POINT_free(generator);
  2153. #ifndef FIPS_MODULE
  2154. BN_CTX_free(new_ctx);
  2155. #endif
  2156. EC_nistp256_pre_comp_free(pre);
  2157. return ret;
  2158. }
  2159. int ossl_ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group)
  2160. {
  2161. return HAVEPRECOMP(group, nistp256);
  2162. }