ecp_nistp224.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. /* crypto/ec/ecp_nistp224.c */
  2. /*
  3. * Written by Emilia Kasper (Google) for the OpenSSL project.
  4. */
  5. /* Copyright 2011 Google Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. *
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /*
  21. * A 64-bit implementation of the NIST P-224 elliptic curve point multiplication
  22. *
  23. * Inspired by Daniel J. Bernstein's public domain nistp224 implementation
  24. * and Adam Langley's public domain 64-bit C implementation of curve25519
  25. */
  26. #include <openssl/opensslconf.h>
  27. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  28. # include <stdint.h>
  29. # include <string.h>
  30. # include <openssl/err.h>
  31. # include "ec_lcl.h"
  32. # if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
  33. /* even with gcc, the typedef won't work for 32-bit platforms */
  34. typedef __uint128_t uint128_t; /* nonstandard; implemented by gcc on 64-bit
  35. * platforms */
  36. # else
  37. # error "Need GCC 3.1 or later to define type uint128_t"
  38. # endif
  39. typedef uint8_t u8;
  40. typedef uint64_t u64;
  41. typedef int64_t s64;
  42. /******************************************************************************/
  43. /*-
  44. * INTERNAL REPRESENTATION OF FIELD ELEMENTS
  45. *
  46. * Field elements are represented as a_0 + 2^56*a_1 + 2^112*a_2 + 2^168*a_3
  47. * using 64-bit coefficients called 'limbs',
  48. * and sometimes (for multiplication results) as
  49. * b_0 + 2^56*b_1 + 2^112*b_2 + 2^168*b_3 + 2^224*b_4 + 2^280*b_5 + 2^336*b_6
  50. * using 128-bit coefficients called 'widelimbs'.
  51. * A 4-limb representation is an 'felem';
  52. * a 7-widelimb representation is a 'widefelem'.
  53. * Even within felems, bits of adjacent limbs overlap, and we don't always
  54. * reduce the representations: we ensure that inputs to each felem
  55. * multiplication satisfy a_i < 2^60, so outputs satisfy b_i < 4*2^60*2^60,
  56. * and fit into a 128-bit word without overflow. The coefficients are then
  57. * again partially reduced to obtain an felem satisfying a_i < 2^57.
  58. * We only reduce to the unique minimal representation at the end of the
  59. * computation.
  60. */
  61. typedef uint64_t limb;
  62. typedef uint128_t widelimb;
  63. typedef limb felem[4];
  64. typedef widelimb widefelem[7];
  65. /*
  66. * Field element represented as a byte arrary. 28*8 = 224 bits is also the
  67. * group order size for the elliptic curve, and we also use this type for
  68. * scalars for point multiplication.
  69. */
  70. typedef u8 felem_bytearray[28];
  71. static const felem_bytearray nistp224_curve_params[5] = {
  72. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* p */
  73. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
  75. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* a */
  76. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
  77. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE},
  78. {0xB4, 0x05, 0x0A, 0x85, 0x0C, 0x04, 0xB3, 0xAB, 0xF5, 0x41, /* b */
  79. 0x32, 0x56, 0x50, 0x44, 0xB0, 0xB7, 0xD7, 0xBF, 0xD8, 0xBA,
  80. 0x27, 0x0B, 0x39, 0x43, 0x23, 0x55, 0xFF, 0xB4},
  81. {0xB7, 0x0E, 0x0C, 0xBD, 0x6B, 0xB4, 0xBF, 0x7F, 0x32, 0x13, /* x */
  82. 0x90, 0xB9, 0x4A, 0x03, 0xC1, 0xD3, 0x56, 0xC2, 0x11, 0x22,
  83. 0x34, 0x32, 0x80, 0xD6, 0x11, 0x5C, 0x1D, 0x21},
  84. {0xbd, 0x37, 0x63, 0x88, 0xb5, 0xf7, 0x23, 0xfb, 0x4c, 0x22, /* y */
  85. 0xdf, 0xe6, 0xcd, 0x43, 0x75, 0xa0, 0x5a, 0x07, 0x47, 0x64,
  86. 0x44, 0xd5, 0x81, 0x99, 0x85, 0x00, 0x7e, 0x34}
  87. };
  88. /*-
  89. * Precomputed multiples of the standard generator
  90. * Points are given in coordinates (X, Y, Z) where Z normally is 1
  91. * (0 for the point at infinity).
  92. * For each field element, slice a_0 is word 0, etc.
  93. *
  94. * The table has 2 * 16 elements, starting with the following:
  95. * index | bits | point
  96. * ------+---------+------------------------------
  97. * 0 | 0 0 0 0 | 0G
  98. * 1 | 0 0 0 1 | 1G
  99. * 2 | 0 0 1 0 | 2^56G
  100. * 3 | 0 0 1 1 | (2^56 + 1)G
  101. * 4 | 0 1 0 0 | 2^112G
  102. * 5 | 0 1 0 1 | (2^112 + 1)G
  103. * 6 | 0 1 1 0 | (2^112 + 2^56)G
  104. * 7 | 0 1 1 1 | (2^112 + 2^56 + 1)G
  105. * 8 | 1 0 0 0 | 2^168G
  106. * 9 | 1 0 0 1 | (2^168 + 1)G
  107. * 10 | 1 0 1 0 | (2^168 + 2^56)G
  108. * 11 | 1 0 1 1 | (2^168 + 2^56 + 1)G
  109. * 12 | 1 1 0 0 | (2^168 + 2^112)G
  110. * 13 | 1 1 0 1 | (2^168 + 2^112 + 1)G
  111. * 14 | 1 1 1 0 | (2^168 + 2^112 + 2^56)G
  112. * 15 | 1 1 1 1 | (2^168 + 2^112 + 2^56 + 1)G
  113. * followed by a copy of this with each element multiplied by 2^28.
  114. *
  115. * The reason for this is so that we can clock bits into four different
  116. * locations when doing simple scalar multiplies against the base point,
  117. * and then another four locations using the second 16 elements.
  118. */
  119. static const felem gmul[2][16][3] = {
  120. {{{0, 0, 0, 0},
  121. {0, 0, 0, 0},
  122. {0, 0, 0, 0}},
  123. {{0x3280d6115c1d21, 0xc1d356c2112234, 0x7f321390b94a03, 0xb70e0cbd6bb4bf},
  124. {0xd5819985007e34, 0x75a05a07476444, 0xfb4c22dfe6cd43, 0xbd376388b5f723},
  125. {1, 0, 0, 0}},
  126. {{0xfd9675666ebbe9, 0xbca7664d40ce5e, 0x2242df8d8a2a43, 0x1f49bbb0f99bc5},
  127. {0x29e0b892dc9c43, 0xece8608436e662, 0xdc858f185310d0, 0x9812dd4eb8d321},
  128. {1, 0, 0, 0}},
  129. {{0x6d3e678d5d8eb8, 0x559eed1cb362f1, 0x16e9a3bbce8a3f, 0xeedcccd8c2a748},
  130. {0xf19f90ed50266d, 0xabf2b4bf65f9df, 0x313865468fafec, 0x5cb379ba910a17},
  131. {1, 0, 0, 0}},
  132. {{0x0641966cab26e3, 0x91fb2991fab0a0, 0xefec27a4e13a0b, 0x0499aa8a5f8ebe},
  133. {0x7510407766af5d, 0x84d929610d5450, 0x81d77aae82f706, 0x6916f6d4338c5b},
  134. {1, 0, 0, 0}},
  135. {{0xea95ac3b1f15c6, 0x086000905e82d4, 0xdd323ae4d1c8b1, 0x932b56be7685a3},
  136. {0x9ef93dea25dbbf, 0x41665960f390f0, 0xfdec76dbe2a8a7, 0x523e80f019062a},
  137. {1, 0, 0, 0}},
  138. {{0x822fdd26732c73, 0xa01c83531b5d0f, 0x363f37347c1ba4, 0xc391b45c84725c},
  139. {0xbbd5e1b2d6ad24, 0xddfbcde19dfaec, 0xc393da7e222a7f, 0x1efb7890ede244},
  140. {1, 0, 0, 0}},
  141. {{0x4c9e90ca217da1, 0xd11beca79159bb, 0xff8d33c2c98b7c, 0x2610b39409f849},
  142. {0x44d1352ac64da0, 0xcdbb7b2c46b4fb, 0x966c079b753c89, 0xfe67e4e820b112},
  143. {1, 0, 0, 0}},
  144. {{0xe28cae2df5312d, 0xc71b61d16f5c6e, 0x79b7619a3e7c4c, 0x05c73240899b47},
  145. {0x9f7f6382c73e3a, 0x18615165c56bda, 0x641fab2116fd56, 0x72855882b08394},
  146. {1, 0, 0, 0}},
  147. {{0x0469182f161c09, 0x74a98ca8d00fb5, 0xb89da93489a3e0, 0x41c98768fb0c1d},
  148. {0xe5ea05fb32da81, 0x3dce9ffbca6855, 0x1cfe2d3fbf59e6, 0x0e5e03408738a7},
  149. {1, 0, 0, 0}},
  150. {{0xdab22b2333e87f, 0x4430137a5dd2f6, 0xe03ab9f738beb8, 0xcb0c5d0dc34f24},
  151. {0x764a7df0c8fda5, 0x185ba5c3fa2044, 0x9281d688bcbe50, 0xc40331df893881},
  152. {1, 0, 0, 0}},
  153. {{0xb89530796f0f60, 0xade92bd26909a3, 0x1a0c83fb4884da, 0x1765bf22a5a984},
  154. {0x772a9ee75db09e, 0x23bc6c67cec16f, 0x4c1edba8b14e2f, 0xe2a215d9611369},
  155. {1, 0, 0, 0}},
  156. {{0x571e509fb5efb3, 0xade88696410552, 0xc8ae85fada74fe, 0x6c7e4be83bbde3},
  157. {0xff9f51160f4652, 0xb47ce2495a6539, 0xa2946c53b582f4, 0x286d2db3ee9a60},
  158. {1, 0, 0, 0}},
  159. {{0x40bbd5081a44af, 0x0995183b13926c, 0xbcefba6f47f6d0, 0x215619e9cc0057},
  160. {0x8bc94d3b0df45e, 0xf11c54a3694f6f, 0x8631b93cdfe8b5, 0xe7e3f4b0982db9},
  161. {1, 0, 0, 0}},
  162. {{0xb17048ab3e1c7b, 0xac38f36ff8a1d8, 0x1c29819435d2c6, 0xc813132f4c07e9},
  163. {0x2891425503b11f, 0x08781030579fea, 0xf5426ba5cc9674, 0x1e28ebf18562bc},
  164. {1, 0, 0, 0}},
  165. {{0x9f31997cc864eb, 0x06cd91d28b5e4c, 0xff17036691a973, 0xf1aef351497c58},
  166. {0xdd1f2d600564ff, 0xdead073b1402db, 0x74a684435bd693, 0xeea7471f962558},
  167. {1, 0, 0, 0}}},
  168. {{{0, 0, 0, 0},
  169. {0, 0, 0, 0},
  170. {0, 0, 0, 0}},
  171. {{0x9665266dddf554, 0x9613d78b60ef2d, 0xce27a34cdba417, 0xd35ab74d6afc31},
  172. {0x85ccdd22deb15e, 0x2137e5783a6aab, 0xa141cffd8c93c6, 0x355a1830e90f2d},
  173. {1, 0, 0, 0}},
  174. {{0x1a494eadaade65, 0xd6da4da77fe53c, 0xe7992996abec86, 0x65c3553c6090e3},
  175. {0xfa610b1fb09346, 0xf1c6540b8a4aaf, 0xc51a13ccd3cbab, 0x02995b1b18c28a},
  176. {1, 0, 0, 0}},
  177. {{0x7874568e7295ef, 0x86b419fbe38d04, 0xdc0690a7550d9a, 0xd3966a44beac33},
  178. {0x2b7280ec29132f, 0xbeaa3b6a032df3, 0xdc7dd88ae41200, 0xd25e2513e3a100},
  179. {1, 0, 0, 0}},
  180. {{0x924857eb2efafd, 0xac2bce41223190, 0x8edaa1445553fc, 0x825800fd3562d5},
  181. {0x8d79148ea96621, 0x23a01c3dd9ed8d, 0xaf8b219f9416b5, 0xd8db0cc277daea},
  182. {1, 0, 0, 0}},
  183. {{0x76a9c3b1a700f0, 0xe9acd29bc7e691, 0x69212d1a6b0327, 0x6322e97fe154be},
  184. {0x469fc5465d62aa, 0x8d41ed18883b05, 0x1f8eae66c52b88, 0xe4fcbe9325be51},
  185. {1, 0, 0, 0}},
  186. {{0x825fdf583cac16, 0x020b857c7b023a, 0x683c17744b0165, 0x14ffd0a2daf2f1},
  187. {0x323b36184218f9, 0x4944ec4e3b47d4, 0xc15b3080841acf, 0x0bced4b01a28bb},
  188. {1, 0, 0, 0}},
  189. {{0x92ac22230df5c4, 0x52f33b4063eda8, 0xcb3f19870c0c93, 0x40064f2ba65233},
  190. {0xfe16f0924f8992, 0x012da25af5b517, 0x1a57bb24f723a6, 0x06f8bc76760def},
  191. {1, 0, 0, 0}},
  192. {{0x4a7084f7817cb9, 0xbcab0738ee9a78, 0x3ec11e11d9c326, 0xdc0fe90e0f1aae},
  193. {0xcf639ea5f98390, 0x5c350aa22ffb74, 0x9afae98a4047b7, 0x956ec2d617fc45},
  194. {1, 0, 0, 0}},
  195. {{0x4306d648c1be6a, 0x9247cd8bc9a462, 0xf5595e377d2f2e, 0xbd1c3caff1a52e},
  196. {0x045e14472409d0, 0x29f3e17078f773, 0x745a602b2d4f7d, 0x191837685cdfbb},
  197. {1, 0, 0, 0}},
  198. {{0x5b6ee254a8cb79, 0x4953433f5e7026, 0xe21faeb1d1def4, 0xc4c225785c09de},
  199. {0x307ce7bba1e518, 0x31b125b1036db8, 0x47e91868839e8f, 0xc765866e33b9f3},
  200. {1, 0, 0, 0}},
  201. {{0x3bfece24f96906, 0x4794da641e5093, 0xde5df64f95db26, 0x297ecd89714b05},
  202. {0x701bd3ebb2c3aa, 0x7073b4f53cb1d5, 0x13c5665658af16, 0x9895089d66fe58},
  203. {1, 0, 0, 0}},
  204. {{0x0fef05f78c4790, 0x2d773633b05d2e, 0x94229c3a951c94, 0xbbbd70df4911bb},
  205. {0xb2c6963d2c1168, 0x105f47a72b0d73, 0x9fdf6111614080, 0x7b7e94b39e67b0},
  206. {1, 0, 0, 0}},
  207. {{0xad1a7d6efbe2b3, 0xf012482c0da69d, 0x6b3bdf12438345, 0x40d7558d7aa4d9},
  208. {0x8a09fffb5c6d3d, 0x9a356e5d9ffd38, 0x5973f15f4f9b1c, 0xdcd5f59f63c3ea},
  209. {1, 0, 0, 0}},
  210. {{0xacf39f4c5ca7ab, 0x4c8071cc5fd737, 0xc64e3602cd1184, 0x0acd4644c9abba},
  211. {0x6c011a36d8bf6e, 0xfecd87ba24e32a, 0x19f6f56574fad8, 0x050b204ced9405},
  212. {1, 0, 0, 0}},
  213. {{0xed4f1cae7d9a96, 0x5ceef7ad94c40a, 0x778e4a3bf3ef9b, 0x7405783dc3b55e},
  214. {0x32477c61b6e8c6, 0xb46a97570f018b, 0x91176d0a7e95d1, 0x3df90fbc4c7d0e},
  215. {1, 0, 0, 0}}}
  216. };
  217. /* Precomputation for the group generator. */
  218. typedef struct {
  219. felem g_pre_comp[2][16][3];
  220. int references;
  221. } NISTP224_PRE_COMP;
  222. const EC_METHOD *EC_GFp_nistp224_method(void)
  223. {
  224. static const EC_METHOD ret = {
  225. EC_FLAGS_DEFAULT_OCT,
  226. NID_X9_62_prime_field,
  227. ec_GFp_nistp224_group_init,
  228. ec_GFp_simple_group_finish,
  229. ec_GFp_simple_group_clear_finish,
  230. ec_GFp_nist_group_copy,
  231. ec_GFp_nistp224_group_set_curve,
  232. ec_GFp_simple_group_get_curve,
  233. ec_GFp_simple_group_get_degree,
  234. ec_GFp_simple_group_check_discriminant,
  235. ec_GFp_simple_point_init,
  236. ec_GFp_simple_point_finish,
  237. ec_GFp_simple_point_clear_finish,
  238. ec_GFp_simple_point_copy,
  239. ec_GFp_simple_point_set_to_infinity,
  240. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  241. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  242. ec_GFp_simple_point_set_affine_coordinates,
  243. ec_GFp_nistp224_point_get_affine_coordinates,
  244. 0 /* point_set_compressed_coordinates */ ,
  245. 0 /* point2oct */ ,
  246. 0 /* oct2point */ ,
  247. ec_GFp_simple_add,
  248. ec_GFp_simple_dbl,
  249. ec_GFp_simple_invert,
  250. ec_GFp_simple_is_at_infinity,
  251. ec_GFp_simple_is_on_curve,
  252. ec_GFp_simple_cmp,
  253. ec_GFp_simple_make_affine,
  254. ec_GFp_simple_points_make_affine,
  255. ec_GFp_nistp224_points_mul,
  256. ec_GFp_nistp224_precompute_mult,
  257. ec_GFp_nistp224_have_precompute_mult,
  258. ec_GFp_nist_field_mul,
  259. ec_GFp_nist_field_sqr,
  260. 0 /* field_div */ ,
  261. 0 /* field_encode */ ,
  262. 0 /* field_decode */ ,
  263. 0 /* field_set_to_one */
  264. };
  265. return &ret;
  266. }
  267. /*
  268. * Helper functions to convert field elements to/from internal representation
  269. */
  270. static void bin28_to_felem(felem out, const u8 in[28])
  271. {
  272. out[0] = *((const uint64_t *)(in)) & 0x00ffffffffffffff;
  273. out[1] = (*((const uint64_t *)(in + 7))) & 0x00ffffffffffffff;
  274. out[2] = (*((const uint64_t *)(in + 14))) & 0x00ffffffffffffff;
  275. out[3] = (*((const uint64_t *)(in+20))) >> 8;
  276. }
  277. static void felem_to_bin28(u8 out[28], const felem in)
  278. {
  279. unsigned i;
  280. for (i = 0; i < 7; ++i) {
  281. out[i] = in[0] >> (8 * i);
  282. out[i + 7] = in[1] >> (8 * i);
  283. out[i + 14] = in[2] >> (8 * i);
  284. out[i + 21] = in[3] >> (8 * i);
  285. }
  286. }
  287. /* To preserve endianness when using BN_bn2bin and BN_bin2bn */
  288. static void flip_endian(u8 *out, const u8 *in, unsigned len)
  289. {
  290. unsigned i;
  291. for (i = 0; i < len; ++i)
  292. out[i] = in[len - 1 - i];
  293. }
  294. /* From OpenSSL BIGNUM to internal representation */
  295. static int BN_to_felem(felem out, const BIGNUM *bn)
  296. {
  297. felem_bytearray b_in;
  298. felem_bytearray b_out;
  299. unsigned num_bytes;
  300. /* BN_bn2bin eats leading zeroes */
  301. memset(b_out, 0, sizeof(b_out));
  302. num_bytes = BN_num_bytes(bn);
  303. if (num_bytes > sizeof b_out) {
  304. ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
  305. return 0;
  306. }
  307. if (BN_is_negative(bn)) {
  308. ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
  309. return 0;
  310. }
  311. num_bytes = BN_bn2bin(bn, b_in);
  312. flip_endian(b_out, b_in, num_bytes);
  313. bin28_to_felem(out, b_out);
  314. return 1;
  315. }
  316. /* From internal representation to OpenSSL BIGNUM */
  317. static BIGNUM *felem_to_BN(BIGNUM *out, const felem in)
  318. {
  319. felem_bytearray b_in, b_out;
  320. felem_to_bin28(b_in, in);
  321. flip_endian(b_out, b_in, sizeof b_out);
  322. return BN_bin2bn(b_out, sizeof b_out, out);
  323. }
  324. /******************************************************************************/
  325. /*-
  326. * FIELD OPERATIONS
  327. *
  328. * Field operations, using the internal representation of field elements.
  329. * NB! These operations are specific to our point multiplication and cannot be
  330. * expected to be correct in general - e.g., multiplication with a large scalar
  331. * will cause an overflow.
  332. *
  333. */
  334. static void felem_one(felem out)
  335. {
  336. out[0] = 1;
  337. out[1] = 0;
  338. out[2] = 0;
  339. out[3] = 0;
  340. }
  341. static void felem_assign(felem out, const felem in)
  342. {
  343. out[0] = in[0];
  344. out[1] = in[1];
  345. out[2] = in[2];
  346. out[3] = in[3];
  347. }
  348. /* Sum two field elements: out += in */
  349. static void felem_sum(felem out, const felem in)
  350. {
  351. out[0] += in[0];
  352. out[1] += in[1];
  353. out[2] += in[2];
  354. out[3] += in[3];
  355. }
  356. /* Get negative value: out = -in */
  357. /* Assumes in[i] < 2^57 */
  358. static void felem_neg(felem out, const felem in)
  359. {
  360. static const limb two58p2 = (((limb) 1) << 58) + (((limb) 1) << 2);
  361. static const limb two58m2 = (((limb) 1) << 58) - (((limb) 1) << 2);
  362. static const limb two58m42m2 = (((limb) 1) << 58) -
  363. (((limb) 1) << 42) - (((limb) 1) << 2);
  364. /* Set to 0 mod 2^224-2^96+1 to ensure out > in */
  365. out[0] = two58p2 - in[0];
  366. out[1] = two58m42m2 - in[1];
  367. out[2] = two58m2 - in[2];
  368. out[3] = two58m2 - in[3];
  369. }
  370. /* Subtract field elements: out -= in */
  371. /* Assumes in[i] < 2^57 */
  372. static void felem_diff(felem out, const felem in)
  373. {
  374. static const limb two58p2 = (((limb) 1) << 58) + (((limb) 1) << 2);
  375. static const limb two58m2 = (((limb) 1) << 58) - (((limb) 1) << 2);
  376. static const limb two58m42m2 = (((limb) 1) << 58) -
  377. (((limb) 1) << 42) - (((limb) 1) << 2);
  378. /* Add 0 mod 2^224-2^96+1 to ensure out > in */
  379. out[0] += two58p2;
  380. out[1] += two58m42m2;
  381. out[2] += two58m2;
  382. out[3] += two58m2;
  383. out[0] -= in[0];
  384. out[1] -= in[1];
  385. out[2] -= in[2];
  386. out[3] -= in[3];
  387. }
  388. /* Subtract in unreduced 128-bit mode: out -= in */
  389. /* Assumes in[i] < 2^119 */
  390. static void widefelem_diff(widefelem out, const widefelem in)
  391. {
  392. static const widelimb two120 = ((widelimb) 1) << 120;
  393. static const widelimb two120m64 = (((widelimb) 1) << 120) -
  394. (((widelimb) 1) << 64);
  395. static const widelimb two120m104m64 = (((widelimb) 1) << 120) -
  396. (((widelimb) 1) << 104) - (((widelimb) 1) << 64);
  397. /* Add 0 mod 2^224-2^96+1 to ensure out > in */
  398. out[0] += two120;
  399. out[1] += two120m64;
  400. out[2] += two120m64;
  401. out[3] += two120;
  402. out[4] += two120m104m64;
  403. out[5] += two120m64;
  404. out[6] += two120m64;
  405. out[0] -= in[0];
  406. out[1] -= in[1];
  407. out[2] -= in[2];
  408. out[3] -= in[3];
  409. out[4] -= in[4];
  410. out[5] -= in[5];
  411. out[6] -= in[6];
  412. }
  413. /* Subtract in mixed mode: out128 -= in64 */
  414. /* in[i] < 2^63 */
  415. static void felem_diff_128_64(widefelem out, const felem in)
  416. {
  417. static const widelimb two64p8 = (((widelimb) 1) << 64) +
  418. (((widelimb) 1) << 8);
  419. static const widelimb two64m8 = (((widelimb) 1) << 64) -
  420. (((widelimb) 1) << 8);
  421. static const widelimb two64m48m8 = (((widelimb) 1) << 64) -
  422. (((widelimb) 1) << 48) - (((widelimb) 1) << 8);
  423. /* Add 0 mod 2^224-2^96+1 to ensure out > in */
  424. out[0] += two64p8;
  425. out[1] += two64m48m8;
  426. out[2] += two64m8;
  427. out[3] += two64m8;
  428. out[0] -= in[0];
  429. out[1] -= in[1];
  430. out[2] -= in[2];
  431. out[3] -= in[3];
  432. }
  433. /*
  434. * Multiply a field element by a scalar: out = out * scalar The scalars we
  435. * actually use are small, so results fit without overflow
  436. */
  437. static void felem_scalar(felem out, const limb scalar)
  438. {
  439. out[0] *= scalar;
  440. out[1] *= scalar;
  441. out[2] *= scalar;
  442. out[3] *= scalar;
  443. }
  444. /*
  445. * Multiply an unreduced field element by a scalar: out = out * scalar The
  446. * scalars we actually use are small, so results fit without overflow
  447. */
  448. static void widefelem_scalar(widefelem out, const widelimb scalar)
  449. {
  450. out[0] *= scalar;
  451. out[1] *= scalar;
  452. out[2] *= scalar;
  453. out[3] *= scalar;
  454. out[4] *= scalar;
  455. out[5] *= scalar;
  456. out[6] *= scalar;
  457. }
  458. /* Square a field element: out = in^2 */
  459. static void felem_square(widefelem out, const felem in)
  460. {
  461. limb tmp0, tmp1, tmp2;
  462. tmp0 = 2 * in[0];
  463. tmp1 = 2 * in[1];
  464. tmp2 = 2 * in[2];
  465. out[0] = ((widelimb) in[0]) * in[0];
  466. out[1] = ((widelimb) in[0]) * tmp1;
  467. out[2] = ((widelimb) in[0]) * tmp2 + ((widelimb) in[1]) * in[1];
  468. out[3] = ((widelimb) in[3]) * tmp0 + ((widelimb) in[1]) * tmp2;
  469. out[4] = ((widelimb) in[3]) * tmp1 + ((widelimb) in[2]) * in[2];
  470. out[5] = ((widelimb) in[3]) * tmp2;
  471. out[6] = ((widelimb) in[3]) * in[3];
  472. }
  473. /* Multiply two field elements: out = in1 * in2 */
  474. static void felem_mul(widefelem out, const felem in1, const felem in2)
  475. {
  476. out[0] = ((widelimb) in1[0]) * in2[0];
  477. out[1] = ((widelimb) in1[0]) * in2[1] + ((widelimb) in1[1]) * in2[0];
  478. out[2] = ((widelimb) in1[0]) * in2[2] + ((widelimb) in1[1]) * in2[1] +
  479. ((widelimb) in1[2]) * in2[0];
  480. out[3] = ((widelimb) in1[0]) * in2[3] + ((widelimb) in1[1]) * in2[2] +
  481. ((widelimb) in1[2]) * in2[1] + ((widelimb) in1[3]) * in2[0];
  482. out[4] = ((widelimb) in1[1]) * in2[3] + ((widelimb) in1[2]) * in2[2] +
  483. ((widelimb) in1[3]) * in2[1];
  484. out[5] = ((widelimb) in1[2]) * in2[3] + ((widelimb) in1[3]) * in2[2];
  485. out[6] = ((widelimb) in1[3]) * in2[3];
  486. }
  487. /*-
  488. * Reduce seven 128-bit coefficients to four 64-bit coefficients.
  489. * Requires in[i] < 2^126,
  490. * ensures out[0] < 2^56, out[1] < 2^56, out[2] < 2^56, out[3] <= 2^56 + 2^16 */
  491. static void felem_reduce(felem out, const widefelem in)
  492. {
  493. static const widelimb two127p15 = (((widelimb) 1) << 127) +
  494. (((widelimb) 1) << 15);
  495. static const widelimb two127m71 = (((widelimb) 1) << 127) -
  496. (((widelimb) 1) << 71);
  497. static const widelimb two127m71m55 = (((widelimb) 1) << 127) -
  498. (((widelimb) 1) << 71) - (((widelimb) 1) << 55);
  499. widelimb output[5];
  500. /* Add 0 mod 2^224-2^96+1 to ensure all differences are positive */
  501. output[0] = in[0] + two127p15;
  502. output[1] = in[1] + two127m71m55;
  503. output[2] = in[2] + two127m71;
  504. output[3] = in[3];
  505. output[4] = in[4];
  506. /* Eliminate in[4], in[5], in[6] */
  507. output[4] += in[6] >> 16;
  508. output[3] += (in[6] & 0xffff) << 40;
  509. output[2] -= in[6];
  510. output[3] += in[5] >> 16;
  511. output[2] += (in[5] & 0xffff) << 40;
  512. output[1] -= in[5];
  513. output[2] += output[4] >> 16;
  514. output[1] += (output[4] & 0xffff) << 40;
  515. output[0] -= output[4];
  516. /* Carry 2 -> 3 -> 4 */
  517. output[3] += output[2] >> 56;
  518. output[2] &= 0x00ffffffffffffff;
  519. output[4] = output[3] >> 56;
  520. output[3] &= 0x00ffffffffffffff;
  521. /* Now output[2] < 2^56, output[3] < 2^56, output[4] < 2^72 */
  522. /* Eliminate output[4] */
  523. output[2] += output[4] >> 16;
  524. /* output[2] < 2^56 + 2^56 = 2^57 */
  525. output[1] += (output[4] & 0xffff) << 40;
  526. output[0] -= output[4];
  527. /* Carry 0 -> 1 -> 2 -> 3 */
  528. output[1] += output[0] >> 56;
  529. out[0] = output[0] & 0x00ffffffffffffff;
  530. output[2] += output[1] >> 56;
  531. /* output[2] < 2^57 + 2^72 */
  532. out[1] = output[1] & 0x00ffffffffffffff;
  533. output[3] += output[2] >> 56;
  534. /* output[3] <= 2^56 + 2^16 */
  535. out[2] = output[2] & 0x00ffffffffffffff;
  536. /*-
  537. * out[0] < 2^56, out[1] < 2^56, out[2] < 2^56,
  538. * out[3] <= 2^56 + 2^16 (due to final carry),
  539. * so out < 2*p
  540. */
  541. out[3] = output[3];
  542. }
  543. static void felem_square_reduce(felem out, const felem in)
  544. {
  545. widefelem tmp;
  546. felem_square(tmp, in);
  547. felem_reduce(out, tmp);
  548. }
  549. static void felem_mul_reduce(felem out, const felem in1, const felem in2)
  550. {
  551. widefelem tmp;
  552. felem_mul(tmp, in1, in2);
  553. felem_reduce(out, tmp);
  554. }
  555. /*
  556. * Reduce to unique minimal representation. Requires 0 <= in < 2*p (always
  557. * call felem_reduce first)
  558. */
  559. static void felem_contract(felem out, const felem in)
  560. {
  561. static const int64_t two56 = ((limb) 1) << 56;
  562. /* 0 <= in < 2*p, p = 2^224 - 2^96 + 1 */
  563. /* if in > p , reduce in = in - 2^224 + 2^96 - 1 */
  564. int64_t tmp[4], a;
  565. tmp[0] = in[0];
  566. tmp[1] = in[1];
  567. tmp[2] = in[2];
  568. tmp[3] = in[3];
  569. /* Case 1: a = 1 iff in >= 2^224 */
  570. a = (in[3] >> 56);
  571. tmp[0] -= a;
  572. tmp[1] += a << 40;
  573. tmp[3] &= 0x00ffffffffffffff;
  574. /*
  575. * Case 2: a = 0 iff p <= in < 2^224, i.e., the high 128 bits are all 1
  576. * and the lower part is non-zero
  577. */
  578. a = ((in[3] & in[2] & (in[1] | 0x000000ffffffffff)) + 1) |
  579. (((int64_t) (in[0] + (in[1] & 0x000000ffffffffff)) - 1) >> 63);
  580. a &= 0x00ffffffffffffff;
  581. /* turn a into an all-one mask (if a = 0) or an all-zero mask */
  582. a = (a - 1) >> 63;
  583. /* subtract 2^224 - 2^96 + 1 if a is all-one */
  584. tmp[3] &= a ^ 0xffffffffffffffff;
  585. tmp[2] &= a ^ 0xffffffffffffffff;
  586. tmp[1] &= (a ^ 0xffffffffffffffff) | 0x000000ffffffffff;
  587. tmp[0] -= 1 & a;
  588. /*
  589. * eliminate negative coefficients: if tmp[0] is negative, tmp[1] must be
  590. * non-zero, so we only need one step
  591. */
  592. a = tmp[0] >> 63;
  593. tmp[0] += two56 & a;
  594. tmp[1] -= 1 & a;
  595. /* carry 1 -> 2 -> 3 */
  596. tmp[2] += tmp[1] >> 56;
  597. tmp[1] &= 0x00ffffffffffffff;
  598. tmp[3] += tmp[2] >> 56;
  599. tmp[2] &= 0x00ffffffffffffff;
  600. /* Now 0 <= out < p */
  601. out[0] = tmp[0];
  602. out[1] = tmp[1];
  603. out[2] = tmp[2];
  604. out[3] = tmp[3];
  605. }
  606. /*
  607. * Zero-check: returns 1 if input is 0, and 0 otherwise. We know that field
  608. * elements are reduced to in < 2^225, so we only need to check three cases:
  609. * 0, 2^224 - 2^96 + 1, and 2^225 - 2^97 + 2
  610. */
  611. static limb felem_is_zero(const felem in)
  612. {
  613. limb zero, two224m96p1, two225m97p2;
  614. zero = in[0] | in[1] | in[2] | in[3];
  615. zero = (((int64_t) (zero) - 1) >> 63) & 1;
  616. two224m96p1 = (in[0] ^ 1) | (in[1] ^ 0x00ffff0000000000)
  617. | (in[2] ^ 0x00ffffffffffffff) | (in[3] ^ 0x00ffffffffffffff);
  618. two224m96p1 = (((int64_t) (two224m96p1) - 1) >> 63) & 1;
  619. two225m97p2 = (in[0] ^ 2) | (in[1] ^ 0x00fffe0000000000)
  620. | (in[2] ^ 0x00ffffffffffffff) | (in[3] ^ 0x01ffffffffffffff);
  621. two225m97p2 = (((int64_t) (two225m97p2) - 1) >> 63) & 1;
  622. return (zero | two224m96p1 | two225m97p2);
  623. }
  624. static limb felem_is_zero_int(const felem in)
  625. {
  626. return (int)(felem_is_zero(in) & ((limb) 1));
  627. }
  628. /* Invert a field element */
  629. /* Computation chain copied from djb's code */
  630. static void felem_inv(felem out, const felem in)
  631. {
  632. felem ftmp, ftmp2, ftmp3, ftmp4;
  633. widefelem tmp;
  634. unsigned i;
  635. felem_square(tmp, in);
  636. felem_reduce(ftmp, tmp); /* 2 */
  637. felem_mul(tmp, in, ftmp);
  638. felem_reduce(ftmp, tmp); /* 2^2 - 1 */
  639. felem_square(tmp, ftmp);
  640. felem_reduce(ftmp, tmp); /* 2^3 - 2 */
  641. felem_mul(tmp, in, ftmp);
  642. felem_reduce(ftmp, tmp); /* 2^3 - 1 */
  643. felem_square(tmp, ftmp);
  644. felem_reduce(ftmp2, tmp); /* 2^4 - 2 */
  645. felem_square(tmp, ftmp2);
  646. felem_reduce(ftmp2, tmp); /* 2^5 - 4 */
  647. felem_square(tmp, ftmp2);
  648. felem_reduce(ftmp2, tmp); /* 2^6 - 8 */
  649. felem_mul(tmp, ftmp2, ftmp);
  650. felem_reduce(ftmp, tmp); /* 2^6 - 1 */
  651. felem_square(tmp, ftmp);
  652. felem_reduce(ftmp2, tmp); /* 2^7 - 2 */
  653. for (i = 0; i < 5; ++i) { /* 2^12 - 2^6 */
  654. felem_square(tmp, ftmp2);
  655. felem_reduce(ftmp2, tmp);
  656. }
  657. felem_mul(tmp, ftmp2, ftmp);
  658. felem_reduce(ftmp2, tmp); /* 2^12 - 1 */
  659. felem_square(tmp, ftmp2);
  660. felem_reduce(ftmp3, tmp); /* 2^13 - 2 */
  661. for (i = 0; i < 11; ++i) { /* 2^24 - 2^12 */
  662. felem_square(tmp, ftmp3);
  663. felem_reduce(ftmp3, tmp);
  664. }
  665. felem_mul(tmp, ftmp3, ftmp2);
  666. felem_reduce(ftmp2, tmp); /* 2^24 - 1 */
  667. felem_square(tmp, ftmp2);
  668. felem_reduce(ftmp3, tmp); /* 2^25 - 2 */
  669. for (i = 0; i < 23; ++i) { /* 2^48 - 2^24 */
  670. felem_square(tmp, ftmp3);
  671. felem_reduce(ftmp3, tmp);
  672. }
  673. felem_mul(tmp, ftmp3, ftmp2);
  674. felem_reduce(ftmp3, tmp); /* 2^48 - 1 */
  675. felem_square(tmp, ftmp3);
  676. felem_reduce(ftmp4, tmp); /* 2^49 - 2 */
  677. for (i = 0; i < 47; ++i) { /* 2^96 - 2^48 */
  678. felem_square(tmp, ftmp4);
  679. felem_reduce(ftmp4, tmp);
  680. }
  681. felem_mul(tmp, ftmp3, ftmp4);
  682. felem_reduce(ftmp3, tmp); /* 2^96 - 1 */
  683. felem_square(tmp, ftmp3);
  684. felem_reduce(ftmp4, tmp); /* 2^97 - 2 */
  685. for (i = 0; i < 23; ++i) { /* 2^120 - 2^24 */
  686. felem_square(tmp, ftmp4);
  687. felem_reduce(ftmp4, tmp);
  688. }
  689. felem_mul(tmp, ftmp2, ftmp4);
  690. felem_reduce(ftmp2, tmp); /* 2^120 - 1 */
  691. for (i = 0; i < 6; ++i) { /* 2^126 - 2^6 */
  692. felem_square(tmp, ftmp2);
  693. felem_reduce(ftmp2, tmp);
  694. }
  695. felem_mul(tmp, ftmp2, ftmp);
  696. felem_reduce(ftmp, tmp); /* 2^126 - 1 */
  697. felem_square(tmp, ftmp);
  698. felem_reduce(ftmp, tmp); /* 2^127 - 2 */
  699. felem_mul(tmp, ftmp, in);
  700. felem_reduce(ftmp, tmp); /* 2^127 - 1 */
  701. for (i = 0; i < 97; ++i) { /* 2^224 - 2^97 */
  702. felem_square(tmp, ftmp);
  703. felem_reduce(ftmp, tmp);
  704. }
  705. felem_mul(tmp, ftmp, ftmp3);
  706. felem_reduce(out, tmp); /* 2^224 - 2^96 - 1 */
  707. }
  708. /*
  709. * Copy in constant time: if icopy == 1, copy in to out, if icopy == 0, copy
  710. * out to itself.
  711. */
  712. static void copy_conditional(felem out, const felem in, limb icopy)
  713. {
  714. unsigned i;
  715. /*
  716. * icopy is a (64-bit) 0 or 1, so copy is either all-zero or all-one
  717. */
  718. const limb copy = -icopy;
  719. for (i = 0; i < 4; ++i) {
  720. const limb tmp = copy & (in[i] ^ out[i]);
  721. out[i] ^= tmp;
  722. }
  723. }
  724. /******************************************************************************/
  725. /*-
  726. * ELLIPTIC CURVE POINT OPERATIONS
  727. *
  728. * Points are represented in Jacobian projective coordinates:
  729. * (X, Y, Z) corresponds to the affine point (X/Z^2, Y/Z^3),
  730. * or to the point at infinity if Z == 0.
  731. *
  732. */
  733. /*-
  734. * Double an elliptic curve point:
  735. * (X', Y', Z') = 2 * (X, Y, Z), where
  736. * X' = (3 * (X - Z^2) * (X + Z^2))^2 - 8 * X * Y^2
  737. * Y' = 3 * (X - Z^2) * (X + Z^2) * (4 * X * Y^2 - X') - 8 * Y^2
  738. * Z' = (Y + Z)^2 - Y^2 - Z^2 = 2 * Y * Z
  739. * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed,
  740. * while x_out == y_in is not (maybe this works, but it's not tested).
  741. */
  742. static void
  743. point_double(felem x_out, felem y_out, felem z_out,
  744. const felem x_in, const felem y_in, const felem z_in)
  745. {
  746. widefelem tmp, tmp2;
  747. felem delta, gamma, beta, alpha, ftmp, ftmp2;
  748. felem_assign(ftmp, x_in);
  749. felem_assign(ftmp2, x_in);
  750. /* delta = z^2 */
  751. felem_square(tmp, z_in);
  752. felem_reduce(delta, tmp);
  753. /* gamma = y^2 */
  754. felem_square(tmp, y_in);
  755. felem_reduce(gamma, tmp);
  756. /* beta = x*gamma */
  757. felem_mul(tmp, x_in, gamma);
  758. felem_reduce(beta, tmp);
  759. /* alpha = 3*(x-delta)*(x+delta) */
  760. felem_diff(ftmp, delta);
  761. /* ftmp[i] < 2^57 + 2^58 + 2 < 2^59 */
  762. felem_sum(ftmp2, delta);
  763. /* ftmp2[i] < 2^57 + 2^57 = 2^58 */
  764. felem_scalar(ftmp2, 3);
  765. /* ftmp2[i] < 3 * 2^58 < 2^60 */
  766. felem_mul(tmp, ftmp, ftmp2);
  767. /* tmp[i] < 2^60 * 2^59 * 4 = 2^121 */
  768. felem_reduce(alpha, tmp);
  769. /* x' = alpha^2 - 8*beta */
  770. felem_square(tmp, alpha);
  771. /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
  772. felem_assign(ftmp, beta);
  773. felem_scalar(ftmp, 8);
  774. /* ftmp[i] < 8 * 2^57 = 2^60 */
  775. felem_diff_128_64(tmp, ftmp);
  776. /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
  777. felem_reduce(x_out, tmp);
  778. /* z' = (y + z)^2 - gamma - delta */
  779. felem_sum(delta, gamma);
  780. /* delta[i] < 2^57 + 2^57 = 2^58 */
  781. felem_assign(ftmp, y_in);
  782. felem_sum(ftmp, z_in);
  783. /* ftmp[i] < 2^57 + 2^57 = 2^58 */
  784. felem_square(tmp, ftmp);
  785. /* tmp[i] < 4 * 2^58 * 2^58 = 2^118 */
  786. felem_diff_128_64(tmp, delta);
  787. /* tmp[i] < 2^118 + 2^64 + 8 < 2^119 */
  788. felem_reduce(z_out, tmp);
  789. /* y' = alpha*(4*beta - x') - 8*gamma^2 */
  790. felem_scalar(beta, 4);
  791. /* beta[i] < 4 * 2^57 = 2^59 */
  792. felem_diff(beta, x_out);
  793. /* beta[i] < 2^59 + 2^58 + 2 < 2^60 */
  794. felem_mul(tmp, alpha, beta);
  795. /* tmp[i] < 4 * 2^57 * 2^60 = 2^119 */
  796. felem_square(tmp2, gamma);
  797. /* tmp2[i] < 4 * 2^57 * 2^57 = 2^116 */
  798. widefelem_scalar(tmp2, 8);
  799. /* tmp2[i] < 8 * 2^116 = 2^119 */
  800. widefelem_diff(tmp, tmp2);
  801. /* tmp[i] < 2^119 + 2^120 < 2^121 */
  802. felem_reduce(y_out, tmp);
  803. }
  804. /*-
  805. * Add two elliptic curve points:
  806. * (X_1, Y_1, Z_1) + (X_2, Y_2, Z_2) = (X_3, Y_3, Z_3), where
  807. * X_3 = (Z_1^3 * Y_2 - Z_2^3 * Y_1)^2 - (Z_1^2 * X_2 - Z_2^2 * X_1)^3 -
  808. * 2 * Z_2^2 * X_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^2
  809. * Y_3 = (Z_1^3 * Y_2 - Z_2^3 * Y_1) * (Z_2^2 * X_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^2 - X_3) -
  810. * Z_2^3 * Y_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^3
  811. * Z_3 = (Z_1^2 * X_2 - Z_2^2 * X_1) * (Z_1 * Z_2)
  812. *
  813. * This runs faster if 'mixed' is set, which requires Z_2 = 1 or Z_2 = 0.
  814. */
  815. /*
  816. * This function is not entirely constant-time: it includes a branch for
  817. * checking whether the two input points are equal, (while not equal to the
  818. * point at infinity). This case never happens during single point
  819. * multiplication, so there is no timing leak for ECDH or ECDSA signing.
  820. */
  821. static void point_add(felem x3, felem y3, felem z3,
  822. const felem x1, const felem y1, const felem z1,
  823. const int mixed, const felem x2, const felem y2,
  824. const felem z2)
  825. {
  826. felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, x_out, y_out, z_out;
  827. widefelem tmp, tmp2;
  828. limb z1_is_zero, z2_is_zero, x_equal, y_equal;
  829. if (!mixed) {
  830. /* ftmp2 = z2^2 */
  831. felem_square(tmp, z2);
  832. felem_reduce(ftmp2, tmp);
  833. /* ftmp4 = z2^3 */
  834. felem_mul(tmp, ftmp2, z2);
  835. felem_reduce(ftmp4, tmp);
  836. /* ftmp4 = z2^3*y1 */
  837. felem_mul(tmp2, ftmp4, y1);
  838. felem_reduce(ftmp4, tmp2);
  839. /* ftmp2 = z2^2*x1 */
  840. felem_mul(tmp2, ftmp2, x1);
  841. felem_reduce(ftmp2, tmp2);
  842. } else {
  843. /*
  844. * We'll assume z2 = 1 (special case z2 = 0 is handled later)
  845. */
  846. /* ftmp4 = z2^3*y1 */
  847. felem_assign(ftmp4, y1);
  848. /* ftmp2 = z2^2*x1 */
  849. felem_assign(ftmp2, x1);
  850. }
  851. /* ftmp = z1^2 */
  852. felem_square(tmp, z1);
  853. felem_reduce(ftmp, tmp);
  854. /* ftmp3 = z1^3 */
  855. felem_mul(tmp, ftmp, z1);
  856. felem_reduce(ftmp3, tmp);
  857. /* tmp = z1^3*y2 */
  858. felem_mul(tmp, ftmp3, y2);
  859. /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
  860. /* ftmp3 = z1^3*y2 - z2^3*y1 */
  861. felem_diff_128_64(tmp, ftmp4);
  862. /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
  863. felem_reduce(ftmp3, tmp);
  864. /* tmp = z1^2*x2 */
  865. felem_mul(tmp, ftmp, x2);
  866. /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
  867. /* ftmp = z1^2*x2 - z2^2*x1 */
  868. felem_diff_128_64(tmp, ftmp2);
  869. /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
  870. felem_reduce(ftmp, tmp);
  871. /*
  872. * the formulae are incorrect if the points are equal so we check for
  873. * this and do doubling if this happens
  874. */
  875. x_equal = felem_is_zero(ftmp);
  876. y_equal = felem_is_zero(ftmp3);
  877. z1_is_zero = felem_is_zero(z1);
  878. z2_is_zero = felem_is_zero(z2);
  879. /* In affine coordinates, (X_1, Y_1) == (X_2, Y_2) */
  880. if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
  881. point_double(x3, y3, z3, x1, y1, z1);
  882. return;
  883. }
  884. /* ftmp5 = z1*z2 */
  885. if (!mixed) {
  886. felem_mul(tmp, z1, z2);
  887. felem_reduce(ftmp5, tmp);
  888. } else {
  889. /* special case z2 = 0 is handled later */
  890. felem_assign(ftmp5, z1);
  891. }
  892. /* z_out = (z1^2*x2 - z2^2*x1)*(z1*z2) */
  893. felem_mul(tmp, ftmp, ftmp5);
  894. felem_reduce(z_out, tmp);
  895. /* ftmp = (z1^2*x2 - z2^2*x1)^2 */
  896. felem_assign(ftmp5, ftmp);
  897. felem_square(tmp, ftmp);
  898. felem_reduce(ftmp, tmp);
  899. /* ftmp5 = (z1^2*x2 - z2^2*x1)^3 */
  900. felem_mul(tmp, ftmp, ftmp5);
  901. felem_reduce(ftmp5, tmp);
  902. /* ftmp2 = z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
  903. felem_mul(tmp, ftmp2, ftmp);
  904. felem_reduce(ftmp2, tmp);
  905. /* tmp = z2^3*y1*(z1^2*x2 - z2^2*x1)^3 */
  906. felem_mul(tmp, ftmp4, ftmp5);
  907. /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
  908. /* tmp2 = (z1^3*y2 - z2^3*y1)^2 */
  909. felem_square(tmp2, ftmp3);
  910. /* tmp2[i] < 4 * 2^57 * 2^57 < 2^116 */
  911. /* tmp2 = (z1^3*y2 - z2^3*y1)^2 - (z1^2*x2 - z2^2*x1)^3 */
  912. felem_diff_128_64(tmp2, ftmp5);
  913. /* tmp2[i] < 2^116 + 2^64 + 8 < 2^117 */
  914. /* ftmp5 = 2*z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
  915. felem_assign(ftmp5, ftmp2);
  916. felem_scalar(ftmp5, 2);
  917. /* ftmp5[i] < 2 * 2^57 = 2^58 */
  918. /*-
  919. * x_out = (z1^3*y2 - z2^3*y1)^2 - (z1^2*x2 - z2^2*x1)^3 -
  920. * 2*z2^2*x1*(z1^2*x2 - z2^2*x1)^2
  921. */
  922. felem_diff_128_64(tmp2, ftmp5);
  923. /* tmp2[i] < 2^117 + 2^64 + 8 < 2^118 */
  924. felem_reduce(x_out, tmp2);
  925. /* ftmp2 = z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out */
  926. felem_diff(ftmp2, x_out);
  927. /* ftmp2[i] < 2^57 + 2^58 + 2 < 2^59 */
  928. /*
  929. * tmp2 = (z1^3*y2 - z2^3*y1)*(z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out)
  930. */
  931. felem_mul(tmp2, ftmp3, ftmp2);
  932. /* tmp2[i] < 4 * 2^57 * 2^59 = 2^118 */
  933. /*-
  934. * y_out = (z1^3*y2 - z2^3*y1)*(z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out) -
  935. * z2^3*y1*(z1^2*x2 - z2^2*x1)^3
  936. */
  937. widefelem_diff(tmp2, tmp);
  938. /* tmp2[i] < 2^118 + 2^120 < 2^121 */
  939. felem_reduce(y_out, tmp2);
  940. /*
  941. * the result (x_out, y_out, z_out) is incorrect if one of the inputs is
  942. * the point at infinity, so we need to check for this separately
  943. */
  944. /*
  945. * if point 1 is at infinity, copy point 2 to output, and vice versa
  946. */
  947. copy_conditional(x_out, x2, z1_is_zero);
  948. copy_conditional(x_out, x1, z2_is_zero);
  949. copy_conditional(y_out, y2, z1_is_zero);
  950. copy_conditional(y_out, y1, z2_is_zero);
  951. copy_conditional(z_out, z2, z1_is_zero);
  952. copy_conditional(z_out, z1, z2_is_zero);
  953. felem_assign(x3, x_out);
  954. felem_assign(y3, y_out);
  955. felem_assign(z3, z_out);
  956. }
  957. /*
  958. * select_point selects the |idx|th point from a precomputation table and
  959. * copies it to out.
  960. * The pre_comp array argument should be size of |size| argument
  961. */
  962. static void select_point(const u64 idx, unsigned int size,
  963. const felem pre_comp[][3], felem out[3])
  964. {
  965. unsigned i, j;
  966. limb *outlimbs = &out[0][0];
  967. memset(out 0, sizeof(out));
  968. for (i = 0; i < size; i++) {
  969. const limb *inlimbs = &pre_comp[i][0][0];
  970. u64 mask = i ^ idx;
  971. mask |= mask >> 4;
  972. mask |= mask >> 2;
  973. mask |= mask >> 1;
  974. mask &= 1;
  975. mask--;
  976. for (j = 0; j < 4 * 3; j++)
  977. outlimbs[j] |= inlimbs[j] & mask;
  978. }
  979. }
  980. /* get_bit returns the |i|th bit in |in| */
  981. static char get_bit(const felem_bytearray in, unsigned i)
  982. {
  983. if (i >= 224)
  984. return 0;
  985. return (in[i >> 3] >> (i & 7)) & 1;
  986. }
  987. /*
  988. * Interleaved point multiplication using precomputed point multiples: The
  989. * small point multiples 0*P, 1*P, ..., 16*P are in pre_comp[], the scalars
  990. * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
  991. * generator, using certain (large) precomputed multiples in g_pre_comp.
  992. * Output point (X, Y, Z) is stored in x_out, y_out, z_out
  993. */
  994. static void batch_mul(felem x_out, felem y_out, felem z_out,
  995. const felem_bytearray scalars[],
  996. const unsigned num_points, const u8 *g_scalar,
  997. const int mixed, const felem pre_comp[][17][3],
  998. const felem g_pre_comp[2][16][3])
  999. {
  1000. int i, skip;
  1001. unsigned num;
  1002. unsigned gen_mul = (g_scalar != NULL);
  1003. felem nq[3], tmp[4];
  1004. u64 bits;
  1005. u8 sign, digit;
  1006. /* set nq to the point at infinity */
  1007. memset(nq, 0, sizeof(nq));
  1008. /*
  1009. * Loop over all scalars msb-to-lsb, interleaving additions of multiples
  1010. * of the generator (two in each of the last 28 rounds) and additions of
  1011. * other points multiples (every 5th round).
  1012. */
  1013. skip = 1; /* save two point operations in the first
  1014. * round */
  1015. for (i = (num_points ? 220 : 27); i >= 0; --i) {
  1016. /* double */
  1017. if (!skip)
  1018. point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
  1019. /* add multiples of the generator */
  1020. if (gen_mul && (i <= 27)) {
  1021. /* first, look 28 bits upwards */
  1022. bits = get_bit(g_scalar, i + 196) << 3;
  1023. bits |= get_bit(g_scalar, i + 140) << 2;
  1024. bits |= get_bit(g_scalar, i + 84) << 1;
  1025. bits |= get_bit(g_scalar, i + 28);
  1026. /* select the point to add, in constant time */
  1027. select_point(bits, 16, g_pre_comp[1], tmp);
  1028. if (!skip) {
  1029. /* value 1 below is argument for "mixed" */
  1030. point_add(nq[0], nq[1], nq[2],
  1031. nq[0], nq[1], nq[2], 1, tmp[0], tmp[1], tmp[2]);
  1032. } else {
  1033. memcpy(nq, tmp, 3 * sizeof(felem));
  1034. skip = 0;
  1035. }
  1036. /* second, look at the current position */
  1037. bits = get_bit(g_scalar, i + 168) << 3;
  1038. bits |= get_bit(g_scalar, i + 112) << 2;
  1039. bits |= get_bit(g_scalar, i + 56) << 1;
  1040. bits |= get_bit(g_scalar, i);
  1041. /* select the point to add, in constant time */
  1042. select_point(bits, 16, g_pre_comp[0], tmp);
  1043. point_add(nq[0], nq[1], nq[2],
  1044. nq[0], nq[1], nq[2],
  1045. 1 /* mixed */ , tmp[0], tmp[1], tmp[2]);
  1046. }
  1047. /* do other additions every 5 doublings */
  1048. if (num_points && (i % 5 == 0)) {
  1049. /* loop over all scalars */
  1050. for (num = 0; num < num_points; ++num) {
  1051. bits = get_bit(scalars[num], i + 4) << 5;
  1052. bits |= get_bit(scalars[num], i + 3) << 4;
  1053. bits |= get_bit(scalars[num], i + 2) << 3;
  1054. bits |= get_bit(scalars[num], i + 1) << 2;
  1055. bits |= get_bit(scalars[num], i) << 1;
  1056. bits |= get_bit(scalars[num], i - 1);
  1057. ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
  1058. /* select the point to add or subtract */
  1059. select_point(digit, 17, pre_comp[num], tmp);
  1060. felem_neg(tmp[3], tmp[1]); /* (X, -Y, Z) is the negative
  1061. * point */
  1062. copy_conditional(tmp[1], tmp[3], sign);
  1063. if (!skip) {
  1064. point_add(nq[0], nq[1], nq[2],
  1065. nq[0], nq[1], nq[2],
  1066. mixed, tmp[0], tmp[1], tmp[2]);
  1067. } else {
  1068. memcpy(nq, tmp, 3 * sizeof(felem));
  1069. skip = 0;
  1070. }
  1071. }
  1072. }
  1073. }
  1074. felem_assign(x_out, nq[0]);
  1075. felem_assign(y_out, nq[1]);
  1076. felem_assign(z_out, nq[2]);
  1077. }
  1078. /******************************************************************************/
  1079. /*
  1080. * FUNCTIONS TO MANAGE PRECOMPUTATION
  1081. */
  1082. static NISTP224_PRE_COMP *nistp224_pre_comp_new()
  1083. {
  1084. NISTP224_PRE_COMP *ret = NULL;
  1085. ret = OPENSSL_malloc(sizeof(*ret));
  1086. if (!ret) {
  1087. ECerr(EC_F_NISTP224_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  1088. return ret;
  1089. }
  1090. memset(ret->g_pre_comp, 0, sizeof(ret->g_pre_comp));
  1091. ret->references = 1;
  1092. return ret;
  1093. }
  1094. static void *nistp224_pre_comp_dup(void *src_)
  1095. {
  1096. NISTP224_PRE_COMP *src = src_;
  1097. /* no need to actually copy, these objects never change! */
  1098. CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
  1099. return src_;
  1100. }
  1101. static void nistp224_pre_comp_free(void *pre_)
  1102. {
  1103. int i;
  1104. NISTP224_PRE_COMP *pre = pre_;
  1105. if (!pre)
  1106. return;
  1107. i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  1108. if (i > 0)
  1109. return;
  1110. OPENSSL_free(pre);
  1111. }
  1112. static void nistp224_pre_comp_clear_free(void *pre_)
  1113. {
  1114. int i;
  1115. NISTP224_PRE_COMP *pre = pre_;
  1116. if (!pre)
  1117. return;
  1118. i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  1119. if (i > 0)
  1120. return;
  1121. OPENSSL_clear_free(pre, sizeof(*pre));
  1122. }
  1123. /******************************************************************************/
  1124. /*
  1125. * OPENSSL EC_METHOD FUNCTIONS
  1126. */
  1127. int ec_GFp_nistp224_group_init(EC_GROUP *group)
  1128. {
  1129. int ret;
  1130. ret = ec_GFp_simple_group_init(group);
  1131. group->a_is_minus3 = 1;
  1132. return ret;
  1133. }
  1134. int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  1135. const BIGNUM *a, const BIGNUM *b,
  1136. BN_CTX *ctx)
  1137. {
  1138. int ret = 0;
  1139. BN_CTX *new_ctx = NULL;
  1140. BIGNUM *curve_p, *curve_a, *curve_b;
  1141. if (ctx == NULL)
  1142. if ((ctx = new_ctx = BN_CTX_new()) == NULL)
  1143. return 0;
  1144. BN_CTX_start(ctx);
  1145. if (((curve_p = BN_CTX_get(ctx)) == NULL) ||
  1146. ((curve_a = BN_CTX_get(ctx)) == NULL) ||
  1147. ((curve_b = BN_CTX_get(ctx)) == NULL))
  1148. goto err;
  1149. BN_bin2bn(nistp224_curve_params[0], sizeof(felem_bytearray), curve_p);
  1150. BN_bin2bn(nistp224_curve_params[1], sizeof(felem_bytearray), curve_a);
  1151. BN_bin2bn(nistp224_curve_params[2], sizeof(felem_bytearray), curve_b);
  1152. if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
  1153. ECerr(EC_F_EC_GFP_NISTP224_GROUP_SET_CURVE,
  1154. EC_R_WRONG_CURVE_PARAMETERS);
  1155. goto err;
  1156. }
  1157. group->field_mod_func = BN_nist_mod_224;
  1158. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  1159. err:
  1160. BN_CTX_end(ctx);
  1161. BN_CTX_free(new_ctx);
  1162. return ret;
  1163. }
  1164. /*
  1165. * Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
  1166. * (X/Z^2, Y/Z^3)
  1167. */
  1168. int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
  1169. const EC_POINT *point,
  1170. BIGNUM *x, BIGNUM *y,
  1171. BN_CTX *ctx)
  1172. {
  1173. felem z1, z2, x_in, y_in, x_out, y_out;
  1174. widefelem tmp;
  1175. if (EC_POINT_is_at_infinity(group, point)) {
  1176. ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
  1177. EC_R_POINT_AT_INFINITY);
  1178. return 0;
  1179. }
  1180. if ((!BN_to_felem(x_in, point->X)) || (!BN_to_felem(y_in, point->Y)) ||
  1181. (!BN_to_felem(z1, point->Z)))
  1182. return 0;
  1183. felem_inv(z2, z1);
  1184. felem_square(tmp, z2);
  1185. felem_reduce(z1, tmp);
  1186. felem_mul(tmp, x_in, z1);
  1187. felem_reduce(x_in, tmp);
  1188. felem_contract(x_out, x_in);
  1189. if (x != NULL) {
  1190. if (!felem_to_BN(x, x_out)) {
  1191. ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
  1192. ERR_R_BN_LIB);
  1193. return 0;
  1194. }
  1195. }
  1196. felem_mul(tmp, z1, z2);
  1197. felem_reduce(z1, tmp);
  1198. felem_mul(tmp, y_in, z1);
  1199. felem_reduce(y_in, tmp);
  1200. felem_contract(y_out, y_in);
  1201. if (y != NULL) {
  1202. if (!felem_to_BN(y, y_out)) {
  1203. ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
  1204. ERR_R_BN_LIB);
  1205. return 0;
  1206. }
  1207. }
  1208. return 1;
  1209. }
  1210. static void make_points_affine(size_t num, felem points[ /* num */ ][3],
  1211. felem tmp_felems[ /* num+1 */ ])
  1212. {
  1213. /*
  1214. * Runs in constant time, unless an input is the point at infinity (which
  1215. * normally shouldn't happen).
  1216. */
  1217. ec_GFp_nistp_points_make_affine_internal(num,
  1218. points,
  1219. sizeof(felem),
  1220. tmp_felems,
  1221. (void (*)(void *))felem_one,
  1222. (int (*)(const void *))
  1223. felem_is_zero_int,
  1224. (void (*)(void *, const void *))
  1225. felem_assign,
  1226. (void (*)(void *, const void *))
  1227. felem_square_reduce, (void (*)
  1228. (void *,
  1229. const void
  1230. *,
  1231. const void
  1232. *))
  1233. felem_mul_reduce,
  1234. (void (*)(void *, const void *))
  1235. felem_inv,
  1236. (void (*)(void *, const void *))
  1237. felem_contract);
  1238. }
  1239. /*
  1240. * Computes scalar*generator + \sum scalars[i]*points[i], ignoring NULL
  1241. * values Result is stored in r (r can equal one of the inputs).
  1242. */
  1243. int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
  1244. const BIGNUM *scalar, size_t num,
  1245. const EC_POINT *points[],
  1246. const BIGNUM *scalars[], BN_CTX *ctx)
  1247. {
  1248. int ret = 0;
  1249. int j;
  1250. unsigned i;
  1251. int mixed = 0;
  1252. BN_CTX *new_ctx = NULL;
  1253. BIGNUM *x, *y, *z, *tmp_scalar;
  1254. felem_bytearray g_secret;
  1255. felem_bytearray *secrets = NULL;
  1256. felem (*pre_comp)[17][3] = NULL;
  1257. felem *tmp_felems = NULL;
  1258. felem_bytearray tmp;
  1259. unsigned num_bytes;
  1260. int have_pre_comp = 0;
  1261. size_t num_points = num;
  1262. felem x_in, y_in, z_in, x_out, y_out, z_out;
  1263. NISTP224_PRE_COMP *pre = NULL;
  1264. const felem(*g_pre_comp)[16][3] = NULL;
  1265. EC_POINT *generator = NULL;
  1266. const EC_POINT *p = NULL;
  1267. const BIGNUM *p_scalar = NULL;
  1268. if (ctx == NULL)
  1269. if ((ctx = new_ctx = BN_CTX_new()) == NULL)
  1270. return 0;
  1271. BN_CTX_start(ctx);
  1272. if (((x = BN_CTX_get(ctx)) == NULL) ||
  1273. ((y = BN_CTX_get(ctx)) == NULL) ||
  1274. ((z = BN_CTX_get(ctx)) == NULL) ||
  1275. ((tmp_scalar = BN_CTX_get(ctx)) == NULL))
  1276. goto err;
  1277. if (scalar != NULL) {
  1278. pre = EC_EX_DATA_get_data(group->extra_data,
  1279. nistp224_pre_comp_dup,
  1280. nistp224_pre_comp_free,
  1281. nistp224_pre_comp_clear_free);
  1282. if (pre)
  1283. /* we have precomputation, try to use it */
  1284. g_pre_comp = (const felem(*)[16][3])pre->g_pre_comp;
  1285. else
  1286. /* try to use the standard precomputation */
  1287. g_pre_comp = &gmul[0];
  1288. generator = EC_POINT_new(group);
  1289. if (generator == NULL)
  1290. goto err;
  1291. /* get the generator from precomputation */
  1292. if (!felem_to_BN(x, g_pre_comp[0][1][0]) ||
  1293. !felem_to_BN(y, g_pre_comp[0][1][1]) ||
  1294. !felem_to_BN(z, g_pre_comp[0][1][2])) {
  1295. ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
  1296. goto err;
  1297. }
  1298. if (!EC_POINT_set_Jprojective_coordinates_GFp(group,
  1299. generator, x, y, z,
  1300. ctx))
  1301. goto err;
  1302. if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
  1303. /* precomputation matches generator */
  1304. have_pre_comp = 1;
  1305. else
  1306. /*
  1307. * we don't have valid precomputation: treat the generator as a
  1308. * random point
  1309. */
  1310. num_points = num_points + 1;
  1311. }
  1312. if (num_points > 0) {
  1313. if (num_points >= 3) {
  1314. /*
  1315. * unless we precompute multiples for just one or two points,
  1316. * converting those into affine form is time well spent
  1317. */
  1318. mixed = 1;
  1319. }
  1320. secrets = OPENSSL_malloc(sizeof(*secrets) * num_points);
  1321. pre_comp = OPENSSL_malloc(sizeof(*pre_comp) * num_points);
  1322. if (mixed)
  1323. tmp_felems =
  1324. OPENSSL_malloc(sizeof(felem) * (num_points * 17 + 1));
  1325. if ((secrets == NULL) || (pre_comp == NULL)
  1326. || (mixed && (tmp_felems == NULL))) {
  1327. ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  1328. goto err;
  1329. }
  1330. /*
  1331. * we treat NULL scalars as 0, and NULL points as points at infinity,
  1332. * i.e., they contribute nothing to the linear combination
  1333. */
  1334. memset(secrets, 0, sizeof(*secrets) * num_points);
  1335. memset(pre_comp, 0, sizeof(*pre_comp) * num_points);
  1336. for (i = 0; i < num_points; ++i) {
  1337. if (i == num)
  1338. /* the generator */
  1339. {
  1340. p = EC_GROUP_get0_generator(group);
  1341. p_scalar = scalar;
  1342. } else
  1343. /* the i^th point */
  1344. {
  1345. p = points[i];
  1346. p_scalar = scalars[i];
  1347. }
  1348. if ((p_scalar != NULL) && (p != NULL)) {
  1349. /* reduce scalar to 0 <= scalar < 2^224 */
  1350. if ((BN_num_bits(p_scalar) > 224)
  1351. || (BN_is_negative(p_scalar))) {
  1352. /*
  1353. * this is an unusual input, and we don't guarantee
  1354. * constant-timeness
  1355. */
  1356. if (!BN_nnmod(tmp_scalar, p_scalar, group->order, ctx)) {
  1357. ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
  1358. goto err;
  1359. }
  1360. num_bytes = BN_bn2bin(tmp_scalar, tmp);
  1361. } else
  1362. num_bytes = BN_bn2bin(p_scalar, tmp);
  1363. flip_endian(secrets[i], tmp, num_bytes);
  1364. /* precompute multiples */
  1365. if ((!BN_to_felem(x_out, p->X)) ||
  1366. (!BN_to_felem(y_out, p->Y)) ||
  1367. (!BN_to_felem(z_out, p->Z)))
  1368. goto err;
  1369. felem_assign(pre_comp[i][1][0], x_out);
  1370. felem_assign(pre_comp[i][1][1], y_out);
  1371. felem_assign(pre_comp[i][1][2], z_out);
  1372. for (j = 2; j <= 16; ++j) {
  1373. if (j & 1) {
  1374. point_add(pre_comp[i][j][0], pre_comp[i][j][1],
  1375. pre_comp[i][j][2], pre_comp[i][1][0],
  1376. pre_comp[i][1][1], pre_comp[i][1][2], 0,
  1377. pre_comp[i][j - 1][0],
  1378. pre_comp[i][j - 1][1],
  1379. pre_comp[i][j - 1][2]);
  1380. } else {
  1381. point_double(pre_comp[i][j][0], pre_comp[i][j][1],
  1382. pre_comp[i][j][2], pre_comp[i][j / 2][0],
  1383. pre_comp[i][j / 2][1],
  1384. pre_comp[i][j / 2][2]);
  1385. }
  1386. }
  1387. }
  1388. }
  1389. if (mixed)
  1390. make_points_affine(num_points * 17, pre_comp[0], tmp_felems);
  1391. }
  1392. /* the scalar for the generator */
  1393. if ((scalar != NULL) && (have_pre_comp)) {
  1394. memset(g_secret, 0, sizeof(g_secret));
  1395. /* reduce scalar to 0 <= scalar < 2^224 */
  1396. if ((BN_num_bits(scalar) > 224) || (BN_is_negative(scalar))) {
  1397. /*
  1398. * this is an unusual input, and we don't guarantee
  1399. * constant-timeness
  1400. */
  1401. if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
  1402. ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
  1403. goto err;
  1404. }
  1405. num_bytes = BN_bn2bin(tmp_scalar, tmp);
  1406. } else
  1407. num_bytes = BN_bn2bin(scalar, tmp);
  1408. flip_endian(g_secret, tmp, num_bytes);
  1409. /* do the multiplication with generator precomputation */
  1410. batch_mul(x_out, y_out, z_out,
  1411. (const felem_bytearray(*))secrets, num_points,
  1412. g_secret,
  1413. mixed, (const felem(*)[17][3])pre_comp, g_pre_comp);
  1414. } else
  1415. /* do the multiplication without generator precomputation */
  1416. batch_mul(x_out, y_out, z_out,
  1417. (const felem_bytearray(*))secrets, num_points,
  1418. NULL, mixed, (const felem(*)[17][3])pre_comp, NULL);
  1419. /* reduce the output to its unique minimal representation */
  1420. felem_contract(x_in, x_out);
  1421. felem_contract(y_in, y_out);
  1422. felem_contract(z_in, z_out);
  1423. if ((!felem_to_BN(x, x_in)) || (!felem_to_BN(y, y_in)) ||
  1424. (!felem_to_BN(z, z_in))) {
  1425. ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
  1426. goto err;
  1427. }
  1428. ret = EC_POINT_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
  1429. err:
  1430. BN_CTX_end(ctx);
  1431. EC_POINT_free(generator);
  1432. BN_CTX_free(new_ctx);
  1433. OPENSSL_free(secrets);
  1434. OPENSSL_free(pre_comp);
  1435. OPENSSL_free(tmp_felems);
  1436. return ret;
  1437. }
  1438. int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  1439. {
  1440. int ret = 0;
  1441. NISTP224_PRE_COMP *pre = NULL;
  1442. int i, j;
  1443. BN_CTX *new_ctx = NULL;
  1444. BIGNUM *x, *y;
  1445. EC_POINT *generator = NULL;
  1446. felem tmp_felems[32];
  1447. /* throw away old precomputation */
  1448. EC_EX_DATA_free_data(&group->extra_data, nistp224_pre_comp_dup,
  1449. nistp224_pre_comp_free,
  1450. nistp224_pre_comp_clear_free);
  1451. if (ctx == NULL)
  1452. if ((ctx = new_ctx = BN_CTX_new()) == NULL)
  1453. return 0;
  1454. BN_CTX_start(ctx);
  1455. if (((x = BN_CTX_get(ctx)) == NULL) || ((y = BN_CTX_get(ctx)) == NULL))
  1456. goto err;
  1457. /* get the generator */
  1458. if (group->generator == NULL)
  1459. goto err;
  1460. generator = EC_POINT_new(group);
  1461. if (generator == NULL)
  1462. goto err;
  1463. BN_bin2bn(nistp224_curve_params[3], sizeof(felem_bytearray), x);
  1464. BN_bin2bn(nistp224_curve_params[4], sizeof(felem_bytearray), y);
  1465. if (!EC_POINT_set_affine_coordinates_GFp(group, generator, x, y, ctx))
  1466. goto err;
  1467. if ((pre = nistp224_pre_comp_new()) == NULL)
  1468. goto err;
  1469. /*
  1470. * if the generator is the standard one, use built-in precomputation
  1471. */
  1472. if (0 == EC_POINT_cmp(group, generator, group->generator, ctx)) {
  1473. memcpy(pre->g_pre_comp, gmul, sizeof(pre->g_pre_comp));
  1474. ret = 1;
  1475. goto err;
  1476. }
  1477. if ((!BN_to_felem(pre->g_pre_comp[0][1][0], group->generator->X)) ||
  1478. (!BN_to_felem(pre->g_pre_comp[0][1][1], group->generator->Y)) ||
  1479. (!BN_to_felem(pre->g_pre_comp[0][1][2], group->generator->Z)))
  1480. goto err;
  1481. /*
  1482. * compute 2^56*G, 2^112*G, 2^168*G for the first table, 2^28*G, 2^84*G,
  1483. * 2^140*G, 2^196*G for the second one
  1484. */
  1485. for (i = 1; i <= 8; i <<= 1) {
  1486. point_double(pre->g_pre_comp[1][i][0], pre->g_pre_comp[1][i][1],
  1487. pre->g_pre_comp[1][i][2], pre->g_pre_comp[0][i][0],
  1488. pre->g_pre_comp[0][i][1], pre->g_pre_comp[0][i][2]);
  1489. for (j = 0; j < 27; ++j) {
  1490. point_double(pre->g_pre_comp[1][i][0], pre->g_pre_comp[1][i][1],
  1491. pre->g_pre_comp[1][i][2], pre->g_pre_comp[1][i][0],
  1492. pre->g_pre_comp[1][i][1], pre->g_pre_comp[1][i][2]);
  1493. }
  1494. if (i == 8)
  1495. break;
  1496. point_double(pre->g_pre_comp[0][2 * i][0],
  1497. pre->g_pre_comp[0][2 * i][1],
  1498. pre->g_pre_comp[0][2 * i][2], pre->g_pre_comp[1][i][0],
  1499. pre->g_pre_comp[1][i][1], pre->g_pre_comp[1][i][2]);
  1500. for (j = 0; j < 27; ++j) {
  1501. point_double(pre->g_pre_comp[0][2 * i][0],
  1502. pre->g_pre_comp[0][2 * i][1],
  1503. pre->g_pre_comp[0][2 * i][2],
  1504. pre->g_pre_comp[0][2 * i][0],
  1505. pre->g_pre_comp[0][2 * i][1],
  1506. pre->g_pre_comp[0][2 * i][2]);
  1507. }
  1508. }
  1509. for (i = 0; i < 2; i++) {
  1510. /* g_pre_comp[i][0] is the point at infinity */
  1511. memset(pre->g_pre_comp[i][0], 0, sizeof(pre->g_pre_comp[i][0]));
  1512. /* the remaining multiples */
  1513. /* 2^56*G + 2^112*G resp. 2^84*G + 2^140*G */
  1514. point_add(pre->g_pre_comp[i][6][0], pre->g_pre_comp[i][6][1],
  1515. pre->g_pre_comp[i][6][2], pre->g_pre_comp[i][4][0],
  1516. pre->g_pre_comp[i][4][1], pre->g_pre_comp[i][4][2],
  1517. 0, pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
  1518. pre->g_pre_comp[i][2][2]);
  1519. /* 2^56*G + 2^168*G resp. 2^84*G + 2^196*G */
  1520. point_add(pre->g_pre_comp[i][10][0], pre->g_pre_comp[i][10][1],
  1521. pre->g_pre_comp[i][10][2], pre->g_pre_comp[i][8][0],
  1522. pre->g_pre_comp[i][8][1], pre->g_pre_comp[i][8][2],
  1523. 0, pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
  1524. pre->g_pre_comp[i][2][2]);
  1525. /* 2^112*G + 2^168*G resp. 2^140*G + 2^196*G */
  1526. point_add(pre->g_pre_comp[i][12][0], pre->g_pre_comp[i][12][1],
  1527. pre->g_pre_comp[i][12][2], pre->g_pre_comp[i][8][0],
  1528. pre->g_pre_comp[i][8][1], pre->g_pre_comp[i][8][2],
  1529. 0, pre->g_pre_comp[i][4][0], pre->g_pre_comp[i][4][1],
  1530. pre->g_pre_comp[i][4][2]);
  1531. /*
  1532. * 2^56*G + 2^112*G + 2^168*G resp. 2^84*G + 2^140*G + 2^196*G
  1533. */
  1534. point_add(pre->g_pre_comp[i][14][0], pre->g_pre_comp[i][14][1],
  1535. pre->g_pre_comp[i][14][2], pre->g_pre_comp[i][12][0],
  1536. pre->g_pre_comp[i][12][1], pre->g_pre_comp[i][12][2],
  1537. 0, pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
  1538. pre->g_pre_comp[i][2][2]);
  1539. for (j = 1; j < 8; ++j) {
  1540. /* odd multiples: add G resp. 2^28*G */
  1541. point_add(pre->g_pre_comp[i][2 * j + 1][0],
  1542. pre->g_pre_comp[i][2 * j + 1][1],
  1543. pre->g_pre_comp[i][2 * j + 1][2],
  1544. pre->g_pre_comp[i][2 * j][0],
  1545. pre->g_pre_comp[i][2 * j][1],
  1546. pre->g_pre_comp[i][2 * j][2], 0,
  1547. pre->g_pre_comp[i][1][0], pre->g_pre_comp[i][1][1],
  1548. pre->g_pre_comp[i][1][2]);
  1549. }
  1550. }
  1551. make_points_affine(31, &(pre->g_pre_comp[0][1]), tmp_felems);
  1552. if (!EC_EX_DATA_set_data(&group->extra_data, pre, nistp224_pre_comp_dup,
  1553. nistp224_pre_comp_free,
  1554. nistp224_pre_comp_clear_free))
  1555. goto err;
  1556. ret = 1;
  1557. pre = NULL;
  1558. err:
  1559. BN_CTX_end(ctx);
  1560. EC_POINT_free(generator);
  1561. BN_CTX_free(new_ctx);
  1562. nistp224_pre_comp_free(pre);
  1563. return ret;
  1564. }
  1565. int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group)
  1566. {
  1567. if (EC_EX_DATA_get_data(group->extra_data, nistp224_pre_comp_dup,
  1568. nistp224_pre_comp_free,
  1569. nistp224_pre_comp_clear_free)
  1570. != NULL)
  1571. return 1;
  1572. else
  1573. return 0;
  1574. }
  1575. #else
  1576. static void *dummy = &dummy;
  1577. #endif