sshecc.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. /*
  2. * Elliptic-curve crypto module for PuTTY
  3. * Implements the three required curves, no optional curves
  4. *
  5. * NOTE: Only curves on prime field are handled by the maths functions
  6. * in Weierstrass form using Jacobian co-ordinates.
  7. *
  8. * Montgomery form curves are supported for DH. (Curve25519)
  9. *
  10. * Edwards form curves are supported for DSA. (Ed25519)
  11. */
  12. /*
  13. * References:
  14. *
  15. * Elliptic curves in SSH are specified in RFC 5656:
  16. * http://tools.ietf.org/html/rfc5656
  17. *
  18. * That specification delegates details of public key formatting and a
  19. * lot of underlying mechanism to SEC 1:
  20. * http://www.secg.org/sec1-v2.pdf
  21. *
  22. * Montgomery maths from:
  23. * Handbook of elliptic and hyperelliptic curve cryptography, Chapter 13
  24. * http://cs.ucsb.edu/~koc/ccs130h/2013/EllipticHyperelliptic-CohenFrey.pdf
  25. *
  26. * Curve25519 spec from libssh (with reference to other things in the
  27. * libssh code):
  28. * https://git.libssh.org/users/aris/libssh.git/tree/doc/[email protected]
  29. *
  30. * Edwards DSA:
  31. * http://ed25519.cr.yp.to/ed25519-20110926.pdf
  32. */
  33. #include <stdlib.h>
  34. #include <assert.h>
  35. #include "ssh.h"
  36. /* ----------------------------------------------------------------------
  37. * Elliptic curve definitions
  38. */
  39. static void initialise_wcurve(struct ec_curve *curve, int bits,
  40. const unsigned char *p,
  41. const unsigned char *a, const unsigned char *b,
  42. const unsigned char *n, const unsigned char *Gx,
  43. const unsigned char *Gy)
  44. {
  45. int length = bits / 8;
  46. if (bits % 8) ++length;
  47. curve->type = EC_WEIERSTRASS;
  48. curve->fieldBits = bits;
  49. curve->p = bignum_from_bytes(p, length);
  50. /* Curve co-efficients */
  51. curve->w.a = bignum_from_bytes(a, length);
  52. curve->w.b = bignum_from_bytes(b, length);
  53. /* Group order and generator */
  54. curve->w.n = bignum_from_bytes(n, length);
  55. curve->w.G.x = bignum_from_bytes(Gx, length);
  56. curve->w.G.y = bignum_from_bytes(Gy, length);
  57. curve->w.G.curve = curve;
  58. curve->w.G.infinity = 0;
  59. }
  60. static void initialise_mcurve(struct ec_curve *curve, int bits,
  61. const unsigned char *p,
  62. const unsigned char *a, const unsigned char *b,
  63. const unsigned char *Gx)
  64. {
  65. int length = bits / 8;
  66. if (bits % 8) ++length;
  67. curve->type = EC_MONTGOMERY;
  68. curve->fieldBits = bits;
  69. curve->p = bignum_from_bytes(p, length);
  70. /* Curve co-efficients */
  71. curve->m.a = bignum_from_bytes(a, length);
  72. curve->m.b = bignum_from_bytes(b, length);
  73. /* Generator */
  74. curve->m.G.x = bignum_from_bytes(Gx, length);
  75. curve->m.G.y = NULL;
  76. curve->m.G.z = NULL;
  77. curve->m.G.curve = curve;
  78. curve->m.G.infinity = 0;
  79. }
  80. static void initialise_ecurve(struct ec_curve *curve, int bits,
  81. const unsigned char *p,
  82. const unsigned char *l, const unsigned char *d,
  83. const unsigned char *Bx, const unsigned char *By)
  84. {
  85. int length = bits / 8;
  86. if (bits % 8) ++length;
  87. curve->type = EC_EDWARDS;
  88. curve->fieldBits = bits;
  89. curve->p = bignum_from_bytes(p, length);
  90. /* Curve co-efficients */
  91. curve->e.l = bignum_from_bytes(l, length);
  92. curve->e.d = bignum_from_bytes(d, length);
  93. /* Group order and generator */
  94. curve->e.B.x = bignum_from_bytes(Bx, length);
  95. curve->e.B.y = bignum_from_bytes(By, length);
  96. curve->e.B.curve = curve;
  97. curve->e.B.infinity = 0;
  98. }
  99. static struct ec_curve *ec_p256(void)
  100. {
  101. static struct ec_curve curve = { 0 };
  102. static unsigned char initialised = 0;
  103. if (!initialised)
  104. {
  105. static const unsigned char p[] = {
  106. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  109. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  110. };
  111. static const unsigned char a[] = {
  112. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  115. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc
  116. };
  117. static const unsigned char b[] = {
  118. 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7,
  119. 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc,
  120. 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6,
  121. 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b
  122. };
  123. static const unsigned char n[] = {
  124. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  125. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  126. 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84,
  127. 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51
  128. };
  129. static const unsigned char Gx[] = {
  130. 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
  131. 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2,
  132. 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
  133. 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96
  134. };
  135. static const unsigned char Gy[] = {
  136. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b,
  137. 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
  138. 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
  139. 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5
  140. };
  141. initialise_wcurve(&curve, 256, p, a, b, n, Gx, Gy);
  142. curve.textname = curve.name = "nistp256";
  143. /* Now initialised, no need to do it again */
  144. initialised = 1;
  145. }
  146. return &curve;
  147. }
  148. static struct ec_curve *ec_p384(void)
  149. {
  150. static struct ec_curve curve = { 0 };
  151. static unsigned char initialised = 0;
  152. if (!initialised)
  153. {
  154. static const unsigned char p[] = {
  155. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  156. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  157. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  158. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  159. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  160. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  161. };
  162. static const unsigned char a[] = {
  163. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  164. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  165. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  166. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  167. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  168. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfc
  169. };
  170. static const unsigned char b[] = {
  171. 0xb3, 0x31, 0x2f, 0xa7, 0xe2, 0x3e, 0xe7, 0xe4,
  172. 0x98, 0x8e, 0x05, 0x6b, 0xe3, 0xf8, 0x2d, 0x19,
  173. 0x18, 0x1d, 0x9c, 0x6e, 0xfe, 0x81, 0x41, 0x12,
  174. 0x03, 0x14, 0x08, 0x8f, 0x50, 0x13, 0x87, 0x5a,
  175. 0xc6, 0x56, 0x39, 0x8d, 0x8a, 0x2e, 0xd1, 0x9d,
  176. 0x2a, 0x85, 0xc8, 0xed, 0xd3, 0xec, 0x2a, 0xef
  177. };
  178. static const unsigned char n[] = {
  179. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  180. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  181. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  182. 0xc7, 0x63, 0x4d, 0x81, 0xf4, 0x37, 0x2d, 0xdf,
  183. 0x58, 0x1a, 0x0d, 0xb2, 0x48, 0xb0, 0xa7, 0x7a,
  184. 0xec, 0xec, 0x19, 0x6a, 0xcc, 0xc5, 0x29, 0x73
  185. };
  186. static const unsigned char Gx[] = {
  187. 0xaa, 0x87, 0xca, 0x22, 0xbe, 0x8b, 0x05, 0x37,
  188. 0x8e, 0xb1, 0xc7, 0x1e, 0xf3, 0x20, 0xad, 0x74,
  189. 0x6e, 0x1d, 0x3b, 0x62, 0x8b, 0xa7, 0x9b, 0x98,
  190. 0x59, 0xf7, 0x41, 0xe0, 0x82, 0x54, 0x2a, 0x38,
  191. 0x55, 0x02, 0xf2, 0x5d, 0xbf, 0x55, 0x29, 0x6c,
  192. 0x3a, 0x54, 0x5e, 0x38, 0x72, 0x76, 0x0a, 0xb7
  193. };
  194. static const unsigned char Gy[] = {
  195. 0x36, 0x17, 0xde, 0x4a, 0x96, 0x26, 0x2c, 0x6f,
  196. 0x5d, 0x9e, 0x98, 0xbf, 0x92, 0x92, 0xdc, 0x29,
  197. 0xf8, 0xf4, 0x1d, 0xbd, 0x28, 0x9a, 0x14, 0x7c,
  198. 0xe9, 0xda, 0x31, 0x13, 0xb5, 0xf0, 0xb8, 0xc0,
  199. 0x0a, 0x60, 0xb1, 0xce, 0x1d, 0x7e, 0x81, 0x9d,
  200. 0x7a, 0x43, 0x1d, 0x7c, 0x90, 0xea, 0x0e, 0x5f
  201. };
  202. initialise_wcurve(&curve, 384, p, a, b, n, Gx, Gy);
  203. curve.textname = curve.name = "nistp384";
  204. /* Now initialised, no need to do it again */
  205. initialised = 1;
  206. }
  207. return &curve;
  208. }
  209. static struct ec_curve *ec_p521(void)
  210. {
  211. static struct ec_curve curve = { 0 };
  212. static unsigned char initialised = 0;
  213. if (!initialised)
  214. {
  215. static const unsigned char p[] = {
  216. 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  217. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  218. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  219. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  220. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  221. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  222. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  223. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  224. 0xff, 0xff
  225. };
  226. static const unsigned char a[] = {
  227. 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  228. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  229. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  230. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  231. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  232. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  233. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  234. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  235. 0xff, 0xfc
  236. };
  237. static const unsigned char b[] = {
  238. 0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c,
  239. 0x9a, 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85,
  240. 0x40, 0xee, 0xa2, 0xda, 0x72, 0x5b, 0x99, 0xb3,
  241. 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1,
  242. 0x09, 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e,
  243. 0x93, 0x7b, 0x16, 0x52, 0xc0, 0xbd, 0x3b, 0xb1,
  244. 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c,
  245. 0x34, 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50,
  246. 0x3f, 0x00
  247. };
  248. static const unsigned char n[] = {
  249. 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  250. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  251. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  252. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  253. 0xff, 0xfa, 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f,
  254. 0x96, 0x6b, 0x7f, 0xcc, 0x01, 0x48, 0xf7, 0x09,
  255. 0xa5, 0xd0, 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c,
  256. 0x47, 0xae, 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38,
  257. 0x64, 0x09
  258. };
  259. static const unsigned char Gx[] = {
  260. 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04,
  261. 0xe9, 0xcd, 0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95,
  262. 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
  263. 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d,
  264. 0x3d, 0xba, 0xa1, 0x4b, 0x5e, 0x77, 0xef, 0xe7,
  265. 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
  266. 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a,
  267. 0x42, 0x9b, 0xf9, 0x7e, 0x7e, 0x31, 0xc2, 0xe5,
  268. 0xbd, 0x66
  269. };
  270. static const unsigned char Gy[] = {
  271. 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b,
  272. 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d,
  273. 0x1b, 0xd9, 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b,
  274. 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e,
  275. 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4,
  276. 0x26, 0x40, 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad,
  277. 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72,
  278. 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1,
  279. 0x66, 0x50
  280. };
  281. initialise_wcurve(&curve, 521, p, a, b, n, Gx, Gy);
  282. curve.textname = curve.name = "nistp521";
  283. /* Now initialised, no need to do it again */
  284. initialised = 1;
  285. }
  286. return &curve;
  287. }
  288. static struct ec_curve *ec_curve25519(void)
  289. {
  290. static struct ec_curve curve = { 0 };
  291. static unsigned char initialised = 0;
  292. if (!initialised)
  293. {
  294. static const unsigned char p[] = {
  295. 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  296. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  297. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  298. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xed
  299. };
  300. static const unsigned char a[] = {
  301. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  302. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  303. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  304. 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x6d, 0x06
  305. };
  306. static const unsigned char b[] = {
  307. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  308. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  309. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  310. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  311. };
  312. static const unsigned char gx[32] = {
  313. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  314. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  315. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  316. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09
  317. };
  318. initialise_mcurve(&curve, 256, p, a, b, gx);
  319. /* This curve doesn't need a name, because it's never used in
  320. * any format that embeds the curve name */
  321. curve.name = NULL;
  322. curve.textname = "Curve25519";
  323. /* Now initialised, no need to do it again */
  324. initialised = 1;
  325. }
  326. return &curve;
  327. }
  328. static struct ec_curve *ec_ed25519(void)
  329. {
  330. static struct ec_curve curve = { 0 };
  331. static unsigned char initialised = 0;
  332. if (!initialised)
  333. {
  334. static const unsigned char q[] = {
  335. 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  336. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  337. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  338. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xed
  339. };
  340. static const unsigned char l[32] = {
  341. 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  342. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  343. 0x14, 0xde, 0xf9, 0xde, 0xa2, 0xf7, 0x9c, 0xd6,
  344. 0x58, 0x12, 0x63, 0x1a, 0x5c, 0xf5, 0xd3, 0xed
  345. };
  346. static const unsigned char d[32] = {
  347. 0x52, 0x03, 0x6c, 0xee, 0x2b, 0x6f, 0xfe, 0x73,
  348. 0x8c, 0xc7, 0x40, 0x79, 0x77, 0x79, 0xe8, 0x98,
  349. 0x00, 0x70, 0x0a, 0x4d, 0x41, 0x41, 0xd8, 0xab,
  350. 0x75, 0xeb, 0x4d, 0xca, 0x13, 0x59, 0x78, 0xa3
  351. };
  352. static const unsigned char Bx[32] = {
  353. 0x21, 0x69, 0x36, 0xd3, 0xcd, 0x6e, 0x53, 0xfe,
  354. 0xc0, 0xa4, 0xe2, 0x31, 0xfd, 0xd6, 0xdc, 0x5c,
  355. 0x69, 0x2c, 0xc7, 0x60, 0x95, 0x25, 0xa7, 0xb2,
  356. 0xc9, 0x56, 0x2d, 0x60, 0x8f, 0x25, 0xd5, 0x1a
  357. };
  358. static const unsigned char By[32] = {
  359. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  360. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  361. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  362. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x58
  363. };
  364. /* This curve doesn't need a name, because it's never used in
  365. * any format that embeds the curve name */
  366. curve.name = NULL;
  367. initialise_ecurve(&curve, 256, q, l, d, Bx, By);
  368. curve.textname = "Ed25519";
  369. /* Now initialised, no need to do it again */
  370. initialised = 1;
  371. }
  372. return &curve;
  373. }
  374. /* Return 1 if a is -3 % p, otherwise return 0
  375. * This is used because there are some maths optimisations */
  376. static int ec_aminus3(const struct ec_curve *curve)
  377. {
  378. int ret;
  379. Bignum _p;
  380. if (curve->type != EC_WEIERSTRASS) {
  381. return 0;
  382. }
  383. _p = bignum_add_long(curve->w.a, 3);
  384. ret = !bignum_cmp(curve->p, _p);
  385. freebn(_p);
  386. return ret;
  387. }
  388. /* ----------------------------------------------------------------------
  389. * Elliptic curve field maths
  390. */
  391. static Bignum ecf_add(const Bignum a, const Bignum b,
  392. const struct ec_curve *curve)
  393. {
  394. Bignum a1, b1, ab, ret;
  395. a1 = bigmod(a, curve->p);
  396. b1 = bigmod(b, curve->p);
  397. ab = bigadd(a1, b1);
  398. freebn(a1);
  399. freebn(b1);
  400. ret = bigmod(ab, curve->p);
  401. freebn(ab);
  402. return ret;
  403. }
  404. static Bignum ecf_square(const Bignum a, const struct ec_curve *curve)
  405. {
  406. return modmul(a, a, curve->p);
  407. }
  408. static Bignum ecf_treble(const Bignum a, const struct ec_curve *curve)
  409. {
  410. Bignum ret, tmp;
  411. /* Double */
  412. tmp = bignum_lshift(a, 1);
  413. /* Add itself (i.e. treble) */
  414. ret = bigadd(tmp, a);
  415. freebn(tmp);
  416. /* Normalise */
  417. while (bignum_cmp(ret, curve->p) >= 0)
  418. {
  419. tmp = bigsub(ret, curve->p);
  420. assert(tmp);
  421. freebn(ret);
  422. ret = tmp;
  423. }
  424. return ret;
  425. }
  426. static Bignum ecf_double(const Bignum a, const struct ec_curve *curve)
  427. {
  428. Bignum ret = bignum_lshift(a, 1);
  429. if (bignum_cmp(ret, curve->p) >= 0)
  430. {
  431. Bignum tmp = bigsub(ret, curve->p);
  432. assert(tmp);
  433. freebn(ret);
  434. return tmp;
  435. }
  436. else
  437. {
  438. return ret;
  439. }
  440. }
  441. /* ----------------------------------------------------------------------
  442. * Memory functions
  443. */
  444. void ec_point_free(struct ec_point *point)
  445. {
  446. if (point == NULL) return;
  447. point->curve = 0;
  448. if (point->x) freebn(point->x);
  449. if (point->y) freebn(point->y);
  450. if (point->z) freebn(point->z);
  451. point->infinity = 0;
  452. sfree(point);
  453. }
  454. static struct ec_point *ec_point_new(const struct ec_curve *curve,
  455. const Bignum x, const Bignum y, const Bignum z,
  456. unsigned char infinity)
  457. {
  458. struct ec_point *point = snewn(1, struct ec_point);
  459. point->curve = curve;
  460. point->x = x;
  461. point->y = y;
  462. point->z = z;
  463. point->infinity = infinity ? 1 : 0;
  464. return point;
  465. }
  466. static struct ec_point *ec_point_copy(const struct ec_point *a)
  467. {
  468. if (a == NULL) return NULL;
  469. return ec_point_new(a->curve,
  470. a->x ? copybn(a->x) : NULL,
  471. a->y ? copybn(a->y) : NULL,
  472. a->z ? copybn(a->z) : NULL,
  473. a->infinity);
  474. }
  475. static int ec_point_verify(const struct ec_point *a)
  476. {
  477. if (a->infinity) {
  478. return 1;
  479. } else if (a->curve->type == EC_EDWARDS) {
  480. /* Check y^2 - x^2 - 1 - d * x^2 * y^2 == 0 */
  481. Bignum y2, x2, tmp, tmp2, tmp3;
  482. int ret;
  483. y2 = ecf_square(a->y, a->curve);
  484. x2 = ecf_square(a->x, a->curve);
  485. tmp = modmul(a->curve->e.d, x2, a->curve->p);
  486. tmp2 = modmul(tmp, y2, a->curve->p);
  487. freebn(tmp);
  488. tmp = modsub(y2, x2, a->curve->p);
  489. freebn(y2);
  490. freebn(x2);
  491. tmp3 = modsub(tmp, tmp2, a->curve->p);
  492. freebn(tmp);
  493. freebn(tmp2);
  494. ret = !bignum_cmp(tmp3, One);
  495. freebn(tmp3);
  496. return ret;
  497. } else if (a->curve->type == EC_WEIERSTRASS) {
  498. /* Verify y^2 = x^3 + ax + b */
  499. int ret = 0;
  500. Bignum lhs = NULL, x3 = NULL, ax = NULL, x3ax = NULL, x3axm = NULL, x3axb = NULL, rhs = NULL;
  501. Bignum Three = bignum_from_long(3);
  502. lhs = modmul(a->y, a->y, a->curve->p);
  503. /* This uses montgomery multiplication to optimise */
  504. x3 = modpow(a->x, Three, a->curve->p);
  505. freebn(Three);
  506. ax = modmul(a->curve->w.a, a->x, a->curve->p);
  507. x3ax = bigadd(x3, ax);
  508. freebn(x3); x3 = NULL;
  509. freebn(ax); ax = NULL;
  510. x3axm = bigmod(x3ax, a->curve->p);
  511. freebn(x3ax); x3ax = NULL;
  512. x3axb = bigadd(x3axm, a->curve->w.b);
  513. freebn(x3axm); x3axm = NULL;
  514. rhs = bigmod(x3axb, a->curve->p);
  515. freebn(x3axb);
  516. ret = bignum_cmp(lhs, rhs) ? 0 : 1;
  517. freebn(lhs);
  518. freebn(rhs);
  519. return ret;
  520. } else {
  521. return 0;
  522. }
  523. }
  524. /* ----------------------------------------------------------------------
  525. * Elliptic curve point maths
  526. */
  527. /* Returns 1 on success and 0 on memory error */
  528. static int ecp_normalise(struct ec_point *a)
  529. {
  530. if (!a) {
  531. /* No point */
  532. return 0;
  533. }
  534. if (a->infinity) {
  535. /* Point is at infinity - i.e. normalised */
  536. return 1;
  537. }
  538. if (a->curve->type == EC_WEIERSTRASS) {
  539. /* In Jacobian Coordinates the triple (X, Y, Z) represents
  540. the affine point (X / Z^2, Y / Z^3) */
  541. Bignum Z2, Z2inv, Z3, Z3inv, tx, ty;
  542. if (!a->x || !a->y) {
  543. /* No point defined */
  544. return 0;
  545. } else if (!a->z) {
  546. /* Already normalised */
  547. return 1;
  548. }
  549. Z2 = ecf_square(a->z, a->curve);
  550. Z2inv = modinv(Z2, a->curve->p);
  551. if (!Z2inv) {
  552. freebn(Z2);
  553. return 0;
  554. }
  555. tx = modmul(a->x, Z2inv, a->curve->p);
  556. freebn(Z2inv);
  557. Z3 = modmul(Z2, a->z, a->curve->p);
  558. freebn(Z2);
  559. Z3inv = modinv(Z3, a->curve->p);
  560. freebn(Z3);
  561. if (!Z3inv) {
  562. freebn(tx);
  563. return 0;
  564. }
  565. ty = modmul(a->y, Z3inv, a->curve->p);
  566. freebn(Z3inv);
  567. freebn(a->x);
  568. a->x = tx;
  569. freebn(a->y);
  570. a->y = ty;
  571. freebn(a->z);
  572. a->z = NULL;
  573. return 1;
  574. } else if (a->curve->type == EC_MONTGOMERY) {
  575. /* In Montgomery (X : Z) represents the x co-ord (X / Z, ?) */
  576. Bignum tmp, tmp2;
  577. if (!a->x) {
  578. /* No point defined */
  579. return 0;
  580. } else if (!a->z) {
  581. /* Already normalised */
  582. return 1;
  583. }
  584. tmp = modinv(a->z, a->curve->p);
  585. if (!tmp) {
  586. return 0;
  587. }
  588. tmp2 = modmul(a->x, tmp, a->curve->p);
  589. freebn(tmp);
  590. freebn(a->z);
  591. a->z = NULL;
  592. freebn(a->x);
  593. a->x = tmp2;
  594. return 1;
  595. } else if (a->curve->type == EC_EDWARDS) {
  596. /* Always normalised */
  597. return 1;
  598. } else {
  599. return 0;
  600. }
  601. }
  602. static struct ec_point *ecp_doublew(const struct ec_point *a, const int aminus3)
  603. {
  604. Bignum S, M, outx, outy, outz;
  605. if (bignum_cmp(a->y, Zero) == 0)
  606. {
  607. /* Identity */
  608. return ec_point_new(a->curve, NULL, NULL, NULL, 1);
  609. }
  610. /* S = 4*X*Y^2 */
  611. {
  612. Bignum Y2, XY2, _2XY2;
  613. Y2 = ecf_square(a->y, a->curve);
  614. XY2 = modmul(a->x, Y2, a->curve->p);
  615. freebn(Y2);
  616. _2XY2 = ecf_double(XY2, a->curve);
  617. freebn(XY2);
  618. S = ecf_double(_2XY2, a->curve);
  619. freebn(_2XY2);
  620. }
  621. /* Faster calculation if a = -3 */
  622. if (aminus3) {
  623. /* if a = -3, then M can also be calculated as M = 3*(X + Z^2)*(X - Z^2) */
  624. Bignum Z2, XpZ2, XmZ2, second;
  625. if (a->z == NULL) {
  626. Z2 = copybn(One);
  627. } else {
  628. Z2 = ecf_square(a->z, a->curve);
  629. }
  630. XpZ2 = ecf_add(a->x, Z2, a->curve);
  631. XmZ2 = modsub(a->x, Z2, a->curve->p);
  632. freebn(Z2);
  633. second = modmul(XpZ2, XmZ2, a->curve->p);
  634. freebn(XpZ2);
  635. freebn(XmZ2);
  636. M = ecf_treble(second, a->curve);
  637. freebn(second);
  638. } else {
  639. /* M = 3*X^2 + a*Z^4 */
  640. Bignum _3X2, X2, aZ4;
  641. if (a->z == NULL) {
  642. aZ4 = copybn(a->curve->w.a);
  643. } else {
  644. Bignum Z2, Z4;
  645. Z2 = ecf_square(a->z, a->curve);
  646. Z4 = ecf_square(Z2, a->curve);
  647. freebn(Z2);
  648. aZ4 = modmul(a->curve->w.a, Z4, a->curve->p);
  649. freebn(Z4);
  650. }
  651. X2 = modmul(a->x, a->x, a->curve->p);
  652. _3X2 = ecf_treble(X2, a->curve);
  653. freebn(X2);
  654. M = ecf_add(_3X2, aZ4, a->curve);
  655. freebn(_3X2);
  656. freebn(aZ4);
  657. }
  658. /* X' = M^2 - 2*S */
  659. {
  660. Bignum M2, _2S;
  661. M2 = ecf_square(M, a->curve);
  662. _2S = ecf_double(S, a->curve);
  663. outx = modsub(M2, _2S, a->curve->p);
  664. freebn(M2);
  665. freebn(_2S);
  666. }
  667. /* Y' = M*(S - X') - 8*Y^4 */
  668. {
  669. Bignum SX, MSX, Eight, Y2, Y4, _8Y4;
  670. SX = modsub(S, outx, a->curve->p);
  671. freebn(S);
  672. MSX = modmul(M, SX, a->curve->p);
  673. freebn(SX);
  674. freebn(M);
  675. Y2 = ecf_square(a->y, a->curve);
  676. Y4 = ecf_square(Y2, a->curve);
  677. freebn(Y2);
  678. Eight = bignum_from_long(8);
  679. _8Y4 = modmul(Eight, Y4, a->curve->p);
  680. freebn(Eight);
  681. freebn(Y4);
  682. outy = modsub(MSX, _8Y4, a->curve->p);
  683. freebn(MSX);
  684. freebn(_8Y4);
  685. }
  686. /* Z' = 2*Y*Z */
  687. {
  688. Bignum YZ;
  689. if (a->z == NULL) {
  690. YZ = copybn(a->y);
  691. } else {
  692. YZ = modmul(a->y, a->z, a->curve->p);
  693. }
  694. outz = ecf_double(YZ, a->curve);
  695. freebn(YZ);
  696. }
  697. return ec_point_new(a->curve, outx, outy, outz, 0);
  698. }
  699. static struct ec_point *ecp_doublem(const struct ec_point *a)
  700. {
  701. Bignum z, outx, outz, xpz, xmz;
  702. z = a->z;
  703. if (!z) {
  704. z = One;
  705. }
  706. /* 4xz = (x + z)^2 - (x - z)^2 */
  707. {
  708. Bignum tmp;
  709. tmp = ecf_add(a->x, z, a->curve);
  710. xpz = ecf_square(tmp, a->curve);
  711. freebn(tmp);
  712. tmp = modsub(a->x, z, a->curve->p);
  713. xmz = ecf_square(tmp, a->curve);
  714. freebn(tmp);
  715. }
  716. /* outx = (x + z)^2 * (x - z)^2 */
  717. outx = modmul(xpz, xmz, a->curve->p);
  718. /* outz = 4xz * ((x - z)^2 + ((A + 2) / 4)*4xz) */
  719. {
  720. Bignum _4xz, tmp, tmp2, tmp3;
  721. tmp = bignum_from_long(2);
  722. tmp2 = ecf_add(a->curve->m.a, tmp, a->curve);
  723. freebn(tmp);
  724. _4xz = modsub(xpz, xmz, a->curve->p);
  725. freebn(xpz);
  726. tmp = modmul(tmp2, _4xz, a->curve->p);
  727. freebn(tmp2);
  728. tmp2 = bignum_from_long(4);
  729. tmp3 = modinv(tmp2, a->curve->p);
  730. freebn(tmp2);
  731. if (!tmp3) {
  732. freebn(tmp);
  733. freebn(_4xz);
  734. freebn(outx);
  735. freebn(xmz);
  736. return NULL;
  737. }
  738. tmp2 = modmul(tmp, tmp3, a->curve->p);
  739. freebn(tmp);
  740. freebn(tmp3);
  741. tmp = ecf_add(xmz, tmp2, a->curve);
  742. freebn(xmz);
  743. freebn(tmp2);
  744. outz = modmul(_4xz, tmp, a->curve->p);
  745. freebn(_4xz);
  746. freebn(tmp);
  747. }
  748. return ec_point_new(a->curve, outx, NULL, outz, 0);
  749. }
  750. /* Forward declaration for Edwards curve doubling */
  751. static struct ec_point *ecp_add(const struct ec_point *a,
  752. const struct ec_point *b,
  753. const int aminus3);
  754. static struct ec_point *ecp_double(const struct ec_point *a, const int aminus3)
  755. {
  756. if (a->infinity)
  757. {
  758. /* Identity */
  759. return ec_point_new(a->curve, NULL, NULL, NULL, 1);
  760. }
  761. if (a->curve->type == EC_EDWARDS)
  762. {
  763. return ecp_add(a, a, aminus3);
  764. }
  765. else if (a->curve->type == EC_WEIERSTRASS)
  766. {
  767. return ecp_doublew(a, aminus3);
  768. }
  769. else
  770. {
  771. return ecp_doublem(a);
  772. }
  773. }
  774. static struct ec_point *ecp_addw(const struct ec_point *a,
  775. const struct ec_point *b,
  776. const int aminus3)
  777. {
  778. Bignum U1, U2, S1, S2, outx, outy, outz;
  779. /* U1 = X1*Z2^2 */
  780. /* S1 = Y1*Z2^3 */
  781. if (b->z) {
  782. Bignum Z2, Z3;
  783. Z2 = ecf_square(b->z, a->curve);
  784. U1 = modmul(a->x, Z2, a->curve->p);
  785. Z3 = modmul(Z2, b->z, a->curve->p);
  786. freebn(Z2);
  787. S1 = modmul(a->y, Z3, a->curve->p);
  788. freebn(Z3);
  789. } else {
  790. U1 = copybn(a->x);
  791. S1 = copybn(a->y);
  792. }
  793. /* U2 = X2*Z1^2 */
  794. /* S2 = Y2*Z1^3 */
  795. if (a->z) {
  796. Bignum Z2, Z3;
  797. Z2 = ecf_square(a->z, b->curve);
  798. U2 = modmul(b->x, Z2, b->curve->p);
  799. Z3 = modmul(Z2, a->z, b->curve->p);
  800. freebn(Z2);
  801. S2 = modmul(b->y, Z3, b->curve->p);
  802. freebn(Z3);
  803. } else {
  804. U2 = copybn(b->x);
  805. S2 = copybn(b->y);
  806. }
  807. /* Check if multiplying by self */
  808. if (bignum_cmp(U1, U2) == 0)
  809. {
  810. freebn(U1);
  811. freebn(U2);
  812. if (bignum_cmp(S1, S2) == 0)
  813. {
  814. freebn(S1);
  815. freebn(S2);
  816. return ecp_double(a, aminus3);
  817. }
  818. else
  819. {
  820. freebn(S1);
  821. freebn(S2);
  822. /* Infinity */
  823. return ec_point_new(a->curve, NULL, NULL, NULL, 1);
  824. }
  825. }
  826. {
  827. Bignum H, R, UH2, H3;
  828. /* H = U2 - U1 */
  829. H = modsub(U2, U1, a->curve->p);
  830. freebn(U2);
  831. /* R = S2 - S1 */
  832. R = modsub(S2, S1, a->curve->p);
  833. freebn(S2);
  834. /* X3 = R^2 - H^3 - 2*U1*H^2 */
  835. {
  836. Bignum R2, H2, _2UH2, first;
  837. H2 = ecf_square(H, a->curve);
  838. UH2 = modmul(U1, H2, a->curve->p);
  839. freebn(U1);
  840. H3 = modmul(H2, H, a->curve->p);
  841. freebn(H2);
  842. R2 = ecf_square(R, a->curve);
  843. _2UH2 = ecf_double(UH2, a->curve);
  844. first = modsub(R2, H3, a->curve->p);
  845. freebn(R2);
  846. outx = modsub(first, _2UH2, a->curve->p);
  847. freebn(first);
  848. freebn(_2UH2);
  849. }
  850. /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
  851. {
  852. Bignum RUH2mX, UH2mX, SH3;
  853. UH2mX = modsub(UH2, outx, a->curve->p);
  854. freebn(UH2);
  855. RUH2mX = modmul(R, UH2mX, a->curve->p);
  856. freebn(UH2mX);
  857. freebn(R);
  858. SH3 = modmul(S1, H3, a->curve->p);
  859. freebn(S1);
  860. freebn(H3);
  861. outy = modsub(RUH2mX, SH3, a->curve->p);
  862. freebn(RUH2mX);
  863. freebn(SH3);
  864. }
  865. /* Z3 = H*Z1*Z2 */
  866. if (a->z && b->z) {
  867. Bignum ZZ;
  868. ZZ = modmul(a->z, b->z, a->curve->p);
  869. outz = modmul(H, ZZ, a->curve->p);
  870. freebn(H);
  871. freebn(ZZ);
  872. } else if (a->z) {
  873. outz = modmul(H, a->z, a->curve->p);
  874. freebn(H);
  875. } else if (b->z) {
  876. outz = modmul(H, b->z, a->curve->p);
  877. freebn(H);
  878. } else {
  879. outz = H;
  880. }
  881. }
  882. return ec_point_new(a->curve, outx, outy, outz, 0);
  883. }
  884. static struct ec_point *ecp_addm(const struct ec_point *a,
  885. const struct ec_point *b,
  886. const struct ec_point *base)
  887. {
  888. Bignum outx, outz, az, bz;
  889. az = a->z;
  890. if (!az) {
  891. az = One;
  892. }
  893. bz = b->z;
  894. if (!bz) {
  895. bz = One;
  896. }
  897. /* a-b is maintained at 1 due to Montgomery ladder implementation */
  898. /* Xa+b = Za-b * ((Xa - Za)*(Xb + Zb) + (Xa + Za)*(Xb - Zb))^2 */
  899. /* Za+b = Xa-b * ((Xa - Za)*(Xb + Zb) - (Xa + Za)*(Xb - Zb))^2 */
  900. {
  901. Bignum tmp, tmp2, tmp3, tmp4;
  902. /* (Xa + Za) * (Xb - Zb) */
  903. tmp = ecf_add(a->x, az, a->curve);
  904. tmp2 = modsub(b->x, bz, a->curve->p);
  905. tmp3 = modmul(tmp, tmp2, a->curve->p);
  906. freebn(tmp);
  907. freebn(tmp2);
  908. /* (Xa - Za) * (Xb + Zb) */
  909. tmp = modsub(a->x, az, a->curve->p);
  910. tmp2 = ecf_add(b->x, bz, a->curve);
  911. tmp4 = modmul(tmp, tmp2, a->curve->p);
  912. freebn(tmp);
  913. freebn(tmp2);
  914. tmp = ecf_add(tmp3, tmp4, a->curve);
  915. outx = ecf_square(tmp, a->curve);
  916. freebn(tmp);
  917. tmp = modsub(tmp3, tmp4, a->curve->p);
  918. freebn(tmp3);
  919. freebn(tmp4);
  920. tmp2 = ecf_square(tmp, a->curve);
  921. freebn(tmp);
  922. outz = modmul(base->x, tmp2, a->curve->p);
  923. freebn(tmp2);
  924. }
  925. return ec_point_new(a->curve, outx, NULL, outz, 0);
  926. }
  927. static struct ec_point *ecp_adde(const struct ec_point *a,
  928. const struct ec_point *b)
  929. {
  930. Bignum outx, outy, dmul;
  931. /* outx = (a->x * b->y + b->x * a->y) /
  932. * (1 + a->curve->e.d * a->x * b->x * a->y * b->y) */
  933. {
  934. Bignum tmp, tmp2, tmp3, tmp4;
  935. tmp = modmul(a->x, b->y, a->curve->p);
  936. tmp2 = modmul(b->x, a->y, a->curve->p);
  937. tmp3 = ecf_add(tmp, tmp2, a->curve);
  938. tmp4 = modmul(tmp, tmp2, a->curve->p);
  939. freebn(tmp);
  940. freebn(tmp2);
  941. dmul = modmul(a->curve->e.d, tmp4, a->curve->p);
  942. freebn(tmp4);
  943. tmp = ecf_add(One, dmul, a->curve);
  944. tmp2 = modinv(tmp, a->curve->p);
  945. freebn(tmp);
  946. if (!tmp2)
  947. {
  948. freebn(tmp3);
  949. freebn(dmul);
  950. return NULL;
  951. }
  952. outx = modmul(tmp3, tmp2, a->curve->p);
  953. freebn(tmp3);
  954. freebn(tmp2);
  955. }
  956. /* outy = (a->y * b->y + a->x * b->x) /
  957. * (1 - a->curve->e.d * a->x * b->x * a->y * b->y) */
  958. {
  959. Bignum tmp, tmp2, tmp3, tmp4;
  960. tmp = modsub(One, dmul, a->curve->p);
  961. freebn(dmul);
  962. tmp2 = modinv(tmp, a->curve->p);
  963. freebn(tmp);
  964. if (!tmp2)
  965. {
  966. freebn(outx);
  967. return NULL;
  968. }
  969. tmp = modmul(a->y, b->y, a->curve->p);
  970. tmp3 = modmul(a->x, b->x, a->curve->p);
  971. tmp4 = ecf_add(tmp, tmp3, a->curve);
  972. freebn(tmp);
  973. freebn(tmp3);
  974. outy = modmul(tmp4, tmp2, a->curve->p);
  975. freebn(tmp4);
  976. freebn(tmp2);
  977. }
  978. return ec_point_new(a->curve, outx, outy, NULL, 0);
  979. }
  980. static struct ec_point *ecp_add(const struct ec_point *a,
  981. const struct ec_point *b,
  982. const int aminus3)
  983. {
  984. if (a->curve != b->curve) {
  985. return NULL;
  986. }
  987. /* Check if multiplying by infinity */
  988. if (a->infinity) return ec_point_copy(b);
  989. if (b->infinity) return ec_point_copy(a);
  990. if (a->curve->type == EC_EDWARDS)
  991. {
  992. return ecp_adde(a, b);
  993. }
  994. if (a->curve->type == EC_WEIERSTRASS)
  995. {
  996. return ecp_addw(a, b, aminus3);
  997. }
  998. return NULL;
  999. }
  1000. static struct ec_point *ecp_mul_(const struct ec_point *a, const Bignum b, int aminus3)
  1001. {
  1002. struct ec_point *A, *ret;
  1003. int bits, i;
  1004. A = ec_point_copy(a);
  1005. ret = ec_point_new(a->curve, NULL, NULL, NULL, 1);
  1006. bits = bignum_bitcount(b);
  1007. for (i = 0; i < bits; ++i)
  1008. {
  1009. if (bignum_bit(b, i))
  1010. {
  1011. struct ec_point *tmp = ecp_add(ret, A, aminus3);
  1012. ec_point_free(ret);
  1013. ret = tmp;
  1014. }
  1015. if (i+1 != bits)
  1016. {
  1017. struct ec_point *tmp = ecp_double(A, aminus3);
  1018. ec_point_free(A);
  1019. A = tmp;
  1020. }
  1021. }
  1022. ec_point_free(A);
  1023. return ret;
  1024. }
  1025. static struct ec_point *ecp_mulw(const struct ec_point *a, const Bignum b)
  1026. {
  1027. struct ec_point *ret = ecp_mul_(a, b, ec_aminus3(a->curve));
  1028. if (!ecp_normalise(ret)) {
  1029. ec_point_free(ret);
  1030. return NULL;
  1031. }
  1032. return ret;
  1033. }
  1034. static struct ec_point *ecp_mule(const struct ec_point *a, const Bignum b)
  1035. {
  1036. int i;
  1037. struct ec_point *ret;
  1038. ret = ec_point_new(a->curve, NULL, NULL, NULL, 1);
  1039. for (i = bignum_bitcount(b); i >= 0 && ret; --i)
  1040. {
  1041. {
  1042. struct ec_point *tmp = ecp_double(ret, 0);
  1043. ec_point_free(ret);
  1044. ret = tmp;
  1045. }
  1046. if (ret && bignum_bit(b, i))
  1047. {
  1048. struct ec_point *tmp = ecp_add(ret, a, 0);
  1049. ec_point_free(ret);
  1050. ret = tmp;
  1051. }
  1052. }
  1053. return ret;
  1054. }
  1055. static struct ec_point *ecp_mulm(const struct ec_point *p, const Bignum n)
  1056. {
  1057. struct ec_point *P1, *P2;
  1058. int bits, i;
  1059. /* P1 <- P and P2 <- [2]P */
  1060. P2 = ecp_double(p, 0);
  1061. P1 = ec_point_copy(p);
  1062. /* for i = bits − 2 down to 0 */
  1063. bits = bignum_bitcount(n);
  1064. for (i = bits - 2; i >= 0; --i)
  1065. {
  1066. if (!bignum_bit(n, i))
  1067. {
  1068. /* P2 <- P1 + P2 */
  1069. struct ec_point *tmp = ecp_addm(P1, P2, p);
  1070. ec_point_free(P2);
  1071. P2 = tmp;
  1072. /* P1 <- [2]P1 */
  1073. tmp = ecp_double(P1, 0);
  1074. ec_point_free(P1);
  1075. P1 = tmp;
  1076. }
  1077. else
  1078. {
  1079. /* P1 <- P1 + P2 */
  1080. struct ec_point *tmp = ecp_addm(P1, P2, p);
  1081. ec_point_free(P1);
  1082. P1 = tmp;
  1083. /* P2 <- [2]P2 */
  1084. tmp = ecp_double(P2, 0);
  1085. ec_point_free(P2);
  1086. P2 = tmp;
  1087. }
  1088. }
  1089. ec_point_free(P2);
  1090. if (!ecp_normalise(P1)) {
  1091. ec_point_free(P1);
  1092. return NULL;
  1093. }
  1094. return P1;
  1095. }
  1096. /* Not static because it is used by sshecdsag.c to generate a new key */
  1097. struct ec_point *ecp_mul(const struct ec_point *a, const Bignum b)
  1098. {
  1099. if (a->curve->type == EC_WEIERSTRASS) {
  1100. return ecp_mulw(a, b);
  1101. } else if (a->curve->type == EC_EDWARDS) {
  1102. return ecp_mule(a, b);
  1103. } else {
  1104. return ecp_mulm(a, b);
  1105. }
  1106. }
  1107. static struct ec_point *ecp_summul(const Bignum a, const Bignum b,
  1108. const struct ec_point *point)
  1109. {
  1110. struct ec_point *aG, *bP, *ret;
  1111. int aminus3;
  1112. if (point->curve->type != EC_WEIERSTRASS) {
  1113. return NULL;
  1114. }
  1115. aminus3 = ec_aminus3(point->curve);
  1116. aG = ecp_mul_(&point->curve->w.G, a, aminus3);
  1117. if (!aG) return NULL;
  1118. bP = ecp_mul_(point, b, aminus3);
  1119. if (!bP) {
  1120. ec_point_free(aG);
  1121. return NULL;
  1122. }
  1123. ret = ecp_add(aG, bP, aminus3);
  1124. ec_point_free(aG);
  1125. ec_point_free(bP);
  1126. if (!ecp_normalise(ret)) {
  1127. ec_point_free(ret);
  1128. return NULL;
  1129. }
  1130. return ret;
  1131. }
  1132. static Bignum *ecp_edx(const struct ec_curve *curve, const Bignum y)
  1133. {
  1134. /* Get the x value on the given Edwards curve for a given y */
  1135. Bignum x, xx;
  1136. /* xx = (y^2 - 1) / (d * y^2 + 1) */
  1137. {
  1138. Bignum tmp, tmp2, tmp3;
  1139. tmp = ecf_square(y, curve);
  1140. tmp2 = modmul(curve->e.d, tmp, curve->p);
  1141. tmp3 = ecf_add(tmp2, One, curve);
  1142. freebn(tmp2);
  1143. tmp2 = modinv(tmp3, curve->p);
  1144. freebn(tmp3);
  1145. if (!tmp2) {
  1146. freebn(tmp);
  1147. return NULL;
  1148. }
  1149. tmp3 = modsub(tmp, One, curve->p);
  1150. freebn(tmp);
  1151. xx = modmul(tmp3, tmp2, curve->p);
  1152. freebn(tmp3);
  1153. freebn(tmp2);
  1154. }
  1155. /* x = xx^((p + 3) / 8) */
  1156. {
  1157. Bignum tmp, tmp2;
  1158. tmp = bignum_add_long(curve->p, 3);
  1159. tmp2 = bignum_rshift(tmp, 3);
  1160. freebn(tmp);
  1161. x = modpow(xx, tmp2, curve->p);
  1162. freebn(tmp2);
  1163. }
  1164. /* if x^2 - xx != 0 then x = x*(2^((p - 1) / 4)) */
  1165. {
  1166. Bignum tmp, tmp2;
  1167. tmp = ecf_square(x, curve);
  1168. tmp2 = modsub(tmp, xx, curve->p);
  1169. freebn(tmp);
  1170. freebn(xx);
  1171. if (bignum_cmp(tmp2, Zero)) {
  1172. Bignum tmp3;
  1173. freebn(tmp2);
  1174. tmp = modsub(curve->p, One, curve->p);
  1175. tmp2 = bignum_rshift(tmp, 2);
  1176. freebn(tmp);
  1177. tmp = bignum_from_long(2);
  1178. tmp3 = modpow(tmp, tmp2, curve->p);
  1179. freebn(tmp);
  1180. freebn(tmp2);
  1181. tmp = modmul(x, tmp3, curve->p);
  1182. freebn(x);
  1183. freebn(tmp3);
  1184. x = tmp;
  1185. } else {
  1186. freebn(tmp2);
  1187. }
  1188. }
  1189. /* if x % 2 != 0 then x = p - x */
  1190. if (bignum_bit(x, 0)) {
  1191. Bignum tmp = modsub(curve->p, x, curve->p);
  1192. freebn(x);
  1193. x = tmp;
  1194. }
  1195. return x;
  1196. }
  1197. /* ----------------------------------------------------------------------
  1198. * Public point from private
  1199. */
  1200. struct ec_point *ec_public(const Bignum privateKey, const struct ec_curve *curve)
  1201. {
  1202. if (curve->type == EC_WEIERSTRASS) {
  1203. return ecp_mul(&curve->w.G, privateKey);
  1204. } else if (curve->type == EC_EDWARDS) {
  1205. /* hash = H(sk) (where hash creates 2 * fieldBits)
  1206. * b = fieldBits
  1207. * a = 2^(b-2) + SUM(2^i * h_i) for i = 2 -> b-2
  1208. * publicKey = aB */
  1209. struct ec_point *ret;
  1210. unsigned char hash[512/8];
  1211. Bignum a;
  1212. int i, keylen;
  1213. SHA512_State s;
  1214. SHA512_Init(&s);
  1215. keylen = curve->fieldBits / 8;
  1216. for (i = 0; i < keylen; ++i) {
  1217. unsigned char b = bignum_byte(privateKey, i);
  1218. SHA512_Bytes(&s, &b, 1);
  1219. }
  1220. SHA512_Final(&s, hash);
  1221. /* The second part is simply turning the hash into a Bignum,
  1222. * however the 2^(b-2) bit *must* be set, and the bottom 3
  1223. * bits *must* not be */
  1224. hash[0] &= 0xf8; /* Unset bottom 3 bits (if set) */
  1225. hash[31] &= 0x7f; /* Unset above (b-2) */
  1226. hash[31] |= 0x40; /* Set 2^(b-2) */
  1227. /* Chop off the top part and convert to int */
  1228. a = bignum_from_bytes_le(hash, 32);
  1229. ret = ecp_mul(&curve->e.B, a);
  1230. freebn(a);
  1231. return ret;
  1232. } else {
  1233. return NULL;
  1234. }
  1235. }
  1236. /* ----------------------------------------------------------------------
  1237. * Basic sign and verify routines
  1238. */
  1239. static int _ecdsa_verify(const struct ec_point *publicKey,
  1240. const unsigned char *data, const int dataLen,
  1241. const Bignum r, const Bignum s)
  1242. {
  1243. int z_bits, n_bits;
  1244. Bignum z;
  1245. int valid = 0;
  1246. if (publicKey->curve->type != EC_WEIERSTRASS) {
  1247. return 0;
  1248. }
  1249. /* Sanity checks */
  1250. if (bignum_cmp(r, Zero) == 0 || bignum_cmp(r, publicKey->curve->w.n) >= 0
  1251. || bignum_cmp(s, Zero) == 0 || bignum_cmp(s, publicKey->curve->w.n) >= 0)
  1252. {
  1253. return 0;
  1254. }
  1255. /* z = left most bitlen(curve->n) of data */
  1256. z = bignum_from_bytes(data, dataLen);
  1257. n_bits = bignum_bitcount(publicKey->curve->w.n);
  1258. z_bits = bignum_bitcount(z);
  1259. if (z_bits > n_bits)
  1260. {
  1261. Bignum tmp = bignum_rshift(z, z_bits - n_bits);
  1262. freebn(z);
  1263. z = tmp;
  1264. }
  1265. /* Ensure z in range of n */
  1266. {
  1267. Bignum tmp = bigmod(z, publicKey->curve->w.n);
  1268. freebn(z);
  1269. z = tmp;
  1270. }
  1271. /* Calculate signature */
  1272. {
  1273. Bignum w, x, u1, u2;
  1274. struct ec_point *tmp;
  1275. w = modinv(s, publicKey->curve->w.n);
  1276. if (!w) {
  1277. freebn(z);
  1278. return 0;
  1279. }
  1280. u1 = modmul(z, w, publicKey->curve->w.n);
  1281. u2 = modmul(r, w, publicKey->curve->w.n);
  1282. freebn(w);
  1283. tmp = ecp_summul(u1, u2, publicKey);
  1284. freebn(u1);
  1285. freebn(u2);
  1286. if (!tmp) {
  1287. freebn(z);
  1288. return 0;
  1289. }
  1290. x = bigmod(tmp->x, publicKey->curve->w.n);
  1291. ec_point_free(tmp);
  1292. valid = (bignum_cmp(r, x) == 0) ? 1 : 0;
  1293. freebn(x);
  1294. }
  1295. freebn(z);
  1296. return valid;
  1297. }
  1298. static void _ecdsa_sign(const Bignum privateKey, const struct ec_curve *curve,
  1299. const unsigned char *data, const int dataLen,
  1300. Bignum *r, Bignum *s)
  1301. {
  1302. unsigned char digest[20];
  1303. int z_bits, n_bits;
  1304. Bignum z, k;
  1305. struct ec_point *kG;
  1306. *r = NULL;
  1307. *s = NULL;
  1308. if (curve->type != EC_WEIERSTRASS) {
  1309. return;
  1310. }
  1311. /* z = left most bitlen(curve->n) of data */
  1312. z = bignum_from_bytes(data, dataLen);
  1313. n_bits = bignum_bitcount(curve->w.n);
  1314. z_bits = bignum_bitcount(z);
  1315. if (z_bits > n_bits)
  1316. {
  1317. Bignum tmp;
  1318. tmp = bignum_rshift(z, z_bits - n_bits);
  1319. freebn(z);
  1320. z = tmp;
  1321. }
  1322. /* Generate k between 1 and curve->n, using the same deterministic
  1323. * k generation system we use for conventional DSA. */
  1324. SHA_Simple(data, dataLen, digest);
  1325. k = dss_gen_k("ECDSA deterministic k generator", curve->w.n, privateKey,
  1326. digest, sizeof(digest));
  1327. kG = ecp_mul(&curve->w.G, k);
  1328. if (!kG) {
  1329. freebn(z);
  1330. freebn(k);
  1331. return;
  1332. }
  1333. /* r = kG.x mod n */
  1334. *r = bigmod(kG->x, curve->w.n);
  1335. ec_point_free(kG);
  1336. /* s = (z + r * priv)/k mod n */
  1337. {
  1338. Bignum rPriv, zMod, first, firstMod, kInv;
  1339. rPriv = modmul(*r, privateKey, curve->w.n);
  1340. zMod = bigmod(z, curve->w.n);
  1341. freebn(z);
  1342. first = bigadd(rPriv, zMod);
  1343. freebn(rPriv);
  1344. freebn(zMod);
  1345. firstMod = bigmod(first, curve->w.n);
  1346. freebn(first);
  1347. kInv = modinv(k, curve->w.n);
  1348. freebn(k);
  1349. if (!kInv) {
  1350. freebn(firstMod);
  1351. freebn(*r);
  1352. return;
  1353. }
  1354. *s = modmul(firstMod, kInv, curve->w.n);
  1355. freebn(firstMod);
  1356. freebn(kInv);
  1357. }
  1358. }
  1359. /* ----------------------------------------------------------------------
  1360. * Misc functions
  1361. */
  1362. static void getstring(const char **data, int *datalen,
  1363. const char **p, int *length)
  1364. {
  1365. *p = NULL;
  1366. if (*datalen < 4)
  1367. return;
  1368. *length = toint(GET_32BIT(*data));
  1369. if (*length < 0)
  1370. return;
  1371. *datalen -= 4;
  1372. *data += 4;
  1373. if (*datalen < *length)
  1374. return;
  1375. *p = *data;
  1376. *data += *length;
  1377. *datalen -= *length;
  1378. }
  1379. static Bignum getmp(const char **data, int *datalen)
  1380. {
  1381. const char *p;
  1382. int length;
  1383. getstring(data, datalen, &p, &length);
  1384. if (!p)
  1385. return NULL;
  1386. if (p[0] & 0x80)
  1387. return NULL; /* negative mp */
  1388. return bignum_from_bytes((unsigned char *)p, length);
  1389. }
  1390. static Bignum getmp_le(const char **data, int *datalen)
  1391. {
  1392. const char *p;
  1393. int length;
  1394. getstring(data, datalen, &p, &length);
  1395. if (!p)
  1396. return NULL;
  1397. return bignum_from_bytes_le((const unsigned char *)p, length);
  1398. }
  1399. static int decodepoint_ed(const char *p, int length, struct ec_point *point)
  1400. {
  1401. /* Got some conversion to do, first read in the y co-ord */
  1402. int negative;
  1403. point->y = bignum_from_bytes_le((const unsigned char*)p, length);
  1404. if ((unsigned)bignum_bitcount(point->y) > point->curve->fieldBits) {
  1405. freebn(point->y);
  1406. point->y = NULL;
  1407. return 0;
  1408. }
  1409. /* Read x bit and then reset it */
  1410. negative = bignum_bit(point->y, point->curve->fieldBits - 1);
  1411. bignum_set_bit(point->y, point->curve->fieldBits - 1, 0);
  1412. bn_restore_invariant(point->y);
  1413. /* Get the x from the y */
  1414. point->x = ecp_edx(point->curve, point->y);
  1415. if (!point->x) {
  1416. freebn(point->y);
  1417. point->y = NULL;
  1418. return 0;
  1419. }
  1420. if (negative) {
  1421. Bignum tmp = modsub(point->curve->p, point->x, point->curve->p);
  1422. freebn(point->x);
  1423. point->x = tmp;
  1424. }
  1425. /* Verify the point is on the curve */
  1426. if (!ec_point_verify(point)) {
  1427. freebn(point->x);
  1428. point->x = NULL;
  1429. freebn(point->y);
  1430. point->y = NULL;
  1431. return 0;
  1432. }
  1433. return 1;
  1434. }
  1435. static int decodepoint(const char *p, int length, struct ec_point *point)
  1436. {
  1437. if (point->curve->type == EC_EDWARDS) {
  1438. return decodepoint_ed(p, length, point);
  1439. }
  1440. if (length < 1 || p[0] != 0x04) /* Only support uncompressed point */
  1441. return 0;
  1442. /* Skip compression flag */
  1443. ++p;
  1444. --length;
  1445. /* The two values must be equal length */
  1446. if (length % 2 != 0) {
  1447. point->x = NULL;
  1448. point->y = NULL;
  1449. point->z = NULL;
  1450. return 0;
  1451. }
  1452. length = length / 2;
  1453. point->x = bignum_from_bytes((const unsigned char *)p, length);
  1454. p += length;
  1455. point->y = bignum_from_bytes((const unsigned char *)p, length);
  1456. point->z = NULL;
  1457. /* Verify the point is on the curve */
  1458. if (!ec_point_verify(point)) {
  1459. freebn(point->x);
  1460. point->x = NULL;
  1461. freebn(point->y);
  1462. point->y = NULL;
  1463. return 0;
  1464. }
  1465. return 1;
  1466. }
  1467. static int getmppoint(const char **data, int *datalen, struct ec_point *point)
  1468. {
  1469. const char *p;
  1470. int length;
  1471. getstring(data, datalen, &p, &length);
  1472. if (!p) return 0;
  1473. return decodepoint(p, length, point);
  1474. }
  1475. /* ----------------------------------------------------------------------
  1476. * Exposed ECDSA interface
  1477. */
  1478. struct ecsign_extra {
  1479. struct ec_curve *(*curve)(void);
  1480. const struct ssh_hash *hash;
  1481. /* These fields are used by the OpenSSH PEM format importer/exporter */
  1482. const unsigned char *oid;
  1483. int oidlen;
  1484. };
  1485. static void ecdsa_freekey(void *key)
  1486. {
  1487. struct ec_key *ec = (struct ec_key *) key;
  1488. if (!ec) return;
  1489. if (ec->publicKey.x)
  1490. freebn(ec->publicKey.x);
  1491. if (ec->publicKey.y)
  1492. freebn(ec->publicKey.y);
  1493. if (ec->publicKey.z)
  1494. freebn(ec->publicKey.z);
  1495. if (ec->privateKey)
  1496. freebn(ec->privateKey);
  1497. sfree(ec);
  1498. }
  1499. static void *ecdsa_newkey(const struct ssh_signkey *self,
  1500. const char *data, int len)
  1501. {
  1502. const struct ecsign_extra *extra =
  1503. (const struct ecsign_extra *)self->extra;
  1504. const char *p;
  1505. int slen;
  1506. struct ec_key *ec;
  1507. struct ec_curve *curve;
  1508. getstring(&data, &len, &p, &slen);
  1509. if (!p) {
  1510. return NULL;
  1511. }
  1512. curve = extra->curve();
  1513. assert(curve->type == EC_WEIERSTRASS || curve->type == EC_EDWARDS);
  1514. /* Curve name is duplicated for Weierstrass form */
  1515. if (curve->type == EC_WEIERSTRASS) {
  1516. getstring(&data, &len, &p, &slen);
  1517. if (!p) return NULL;
  1518. if (!match_ssh_id(slen, p, curve->name)) return NULL;
  1519. }
  1520. ec = snew(struct ec_key);
  1521. ec->signalg = self;
  1522. ec->publicKey.curve = curve;
  1523. ec->publicKey.infinity = 0;
  1524. ec->publicKey.x = NULL;
  1525. ec->publicKey.y = NULL;
  1526. ec->publicKey.z = NULL;
  1527. ec->privateKey = NULL;
  1528. if (!getmppoint(&data, &len, &ec->publicKey)) {
  1529. ecdsa_freekey(ec);
  1530. return NULL;
  1531. }
  1532. if (!ec->publicKey.x || !ec->publicKey.y ||
  1533. bignum_cmp(ec->publicKey.x, curve->p) >= 0 ||
  1534. bignum_cmp(ec->publicKey.y, curve->p) >= 0)
  1535. {
  1536. ecdsa_freekey(ec);
  1537. ec = NULL;
  1538. }
  1539. return ec;
  1540. }
  1541. static char *ecdsa_fmtkey(void *key)
  1542. {
  1543. struct ec_key *ec = (struct ec_key *) key;
  1544. char *p;
  1545. int len, i, pos, nibbles;
  1546. static const char hex[] = "0123456789abcdef";
  1547. if (!ec->publicKey.x || !ec->publicKey.y || !ec->publicKey.curve)
  1548. return NULL;
  1549. len = 4 + 2 + 1; /* 2 x "0x", punctuation, \0 */
  1550. if (ec->publicKey.curve->name)
  1551. len += strlen(ec->publicKey.curve->name); /* Curve name */
  1552. len += 4 * (bignum_bitcount(ec->publicKey.x) + 15) / 16;
  1553. len += 4 * (bignum_bitcount(ec->publicKey.y) + 15) / 16;
  1554. p = snewn(len, char);
  1555. pos = 0;
  1556. if (ec->publicKey.curve->name)
  1557. pos += sprintf(p + pos, "%s,", ec->publicKey.curve->name);
  1558. pos += sprintf(p + pos, "0x");
  1559. nibbles = (3 + bignum_bitcount(ec->publicKey.x)) / 4;
  1560. if (nibbles < 1)
  1561. nibbles = 1;
  1562. for (i = nibbles; i--;) {
  1563. p[pos++] =
  1564. hex[(bignum_byte(ec->publicKey.x, i / 2) >> (4 * (i % 2))) & 0xF];
  1565. }
  1566. pos += sprintf(p + pos, ",0x");
  1567. nibbles = (3 + bignum_bitcount(ec->publicKey.y)) / 4;
  1568. if (nibbles < 1)
  1569. nibbles = 1;
  1570. for (i = nibbles; i--;) {
  1571. p[pos++] =
  1572. hex[(bignum_byte(ec->publicKey.y, i / 2) >> (4 * (i % 2))) & 0xF];
  1573. }
  1574. p[pos] = '\0';
  1575. return p;
  1576. }
  1577. static unsigned char *ecdsa_public_blob(void *key, int *len)
  1578. {
  1579. struct ec_key *ec = (struct ec_key *) key;
  1580. int pointlen, bloblen, fullnamelen, namelen;
  1581. int i;
  1582. unsigned char *blob, *p;
  1583. fullnamelen = strlen(ec->signalg->name);
  1584. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1585. /* Edwards compressed form "ssh-ed25519" point y[:-1] + x[0:1] */
  1586. pointlen = ec->publicKey.curve->fieldBits / 8;
  1587. /* Can't handle this in our loop */
  1588. if (pointlen < 2) return NULL;
  1589. bloblen = 4 + fullnamelen + 4 + pointlen;
  1590. blob = snewn(bloblen, unsigned char);
  1591. p = blob;
  1592. PUT_32BIT(p, fullnamelen);
  1593. p += 4;
  1594. memcpy(p, ec->signalg->name, fullnamelen);
  1595. p += fullnamelen;
  1596. PUT_32BIT(p, pointlen);
  1597. p += 4;
  1598. /* Unset last bit of y and set first bit of x in its place */
  1599. for (i = 0; i < pointlen - 1; ++i) {
  1600. *p++ = bignum_byte(ec->publicKey.y, i);
  1601. }
  1602. /* Unset last bit of y and set first bit of x in its place */
  1603. *p = bignum_byte(ec->publicKey.y, i) & 0x7f;
  1604. *p++ |= bignum_bit(ec->publicKey.x, 0) << 7;
  1605. } else if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  1606. assert(ec->publicKey.curve->name);
  1607. namelen = strlen(ec->publicKey.curve->name);
  1608. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  1609. /*
  1610. * string "ecdsa-sha2-<name>", string "<name>", 0x04 point x, y.
  1611. */
  1612. bloblen = 4 + fullnamelen + 4 + namelen + 4 + 1 + (pointlen * 2);
  1613. blob = snewn(bloblen, unsigned char);
  1614. p = blob;
  1615. PUT_32BIT(p, fullnamelen);
  1616. p += 4;
  1617. memcpy(p, ec->signalg->name, fullnamelen);
  1618. p += fullnamelen;
  1619. PUT_32BIT(p, namelen);
  1620. p += 4;
  1621. memcpy(p, ec->publicKey.curve->name, namelen);
  1622. p += namelen;
  1623. PUT_32BIT(p, (2 * pointlen) + 1);
  1624. p += 4;
  1625. *p++ = 0x04;
  1626. for (i = pointlen; i--;) {
  1627. *p++ = bignum_byte(ec->publicKey.x, i);
  1628. }
  1629. for (i = pointlen; i--;) {
  1630. *p++ = bignum_byte(ec->publicKey.y, i);
  1631. }
  1632. } else {
  1633. return NULL;
  1634. }
  1635. assert(p == blob + bloblen);
  1636. *len = bloblen;
  1637. return blob;
  1638. }
  1639. static unsigned char *ecdsa_private_blob(void *key, int *len)
  1640. {
  1641. struct ec_key *ec = (struct ec_key *) key;
  1642. int keylen, bloblen;
  1643. int i;
  1644. unsigned char *blob, *p;
  1645. if (!ec->privateKey) return NULL;
  1646. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1647. /* Unsigned */
  1648. keylen = (bignum_bitcount(ec->privateKey) + 7) / 8;
  1649. } else {
  1650. /* Signed */
  1651. keylen = (bignum_bitcount(ec->privateKey) + 8) / 8;
  1652. }
  1653. /*
  1654. * mpint privateKey. Total 4 + keylen.
  1655. */
  1656. bloblen = 4 + keylen;
  1657. blob = snewn(bloblen, unsigned char);
  1658. p = blob;
  1659. PUT_32BIT(p, keylen);
  1660. p += 4;
  1661. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1662. /* Little endian */
  1663. for (i = 0; i < keylen; ++i)
  1664. *p++ = bignum_byte(ec->privateKey, i);
  1665. } else {
  1666. for (i = keylen; i--;)
  1667. *p++ = bignum_byte(ec->privateKey, i);
  1668. }
  1669. assert(p == blob + bloblen);
  1670. *len = bloblen;
  1671. return blob;
  1672. }
  1673. static void *ecdsa_createkey(const struct ssh_signkey *self,
  1674. const unsigned char *pub_blob, int pub_len,
  1675. const unsigned char *priv_blob, int priv_len)
  1676. {
  1677. struct ec_key *ec;
  1678. struct ec_point *publicKey;
  1679. const char *pb = (const char *) priv_blob;
  1680. ec = (struct ec_key*)ecdsa_newkey(self, (const char *) pub_blob, pub_len);
  1681. if (!ec) {
  1682. return NULL;
  1683. }
  1684. if (ec->publicKey.curve->type != EC_WEIERSTRASS
  1685. && ec->publicKey.curve->type != EC_EDWARDS) {
  1686. ecdsa_freekey(ec);
  1687. return NULL;
  1688. }
  1689. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1690. ec->privateKey = getmp_le(&pb, &priv_len);
  1691. } else {
  1692. ec->privateKey = getmp(&pb, &priv_len);
  1693. }
  1694. if (!ec->privateKey) {
  1695. ecdsa_freekey(ec);
  1696. return NULL;
  1697. }
  1698. /* Check that private key generates public key */
  1699. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1700. if (!publicKey ||
  1701. bignum_cmp(publicKey->x, ec->publicKey.x) ||
  1702. bignum_cmp(publicKey->y, ec->publicKey.y))
  1703. {
  1704. ecdsa_freekey(ec);
  1705. ec = NULL;
  1706. }
  1707. ec_point_free(publicKey);
  1708. return ec;
  1709. }
  1710. static void *ed25519_openssh_createkey(const struct ssh_signkey *self,
  1711. const unsigned char **blob, int *len)
  1712. {
  1713. struct ec_key *ec;
  1714. struct ec_point *publicKey;
  1715. const char *p, *q;
  1716. int plen, qlen;
  1717. getstring((const char**)blob, len, &p, &plen);
  1718. if (!p)
  1719. {
  1720. return NULL;
  1721. }
  1722. ec = snew(struct ec_key);
  1723. ec->signalg = self;
  1724. ec->publicKey.curve = ec_ed25519();
  1725. ec->publicKey.infinity = 0;
  1726. ec->privateKey = NULL;
  1727. ec->publicKey.x = NULL;
  1728. ec->publicKey.z = NULL;
  1729. ec->publicKey.y = NULL;
  1730. if (!decodepoint_ed(p, plen, &ec->publicKey))
  1731. {
  1732. ecdsa_freekey(ec);
  1733. return NULL;
  1734. }
  1735. getstring((const char**)blob, len, &q, &qlen);
  1736. if (!q || qlen != 64) {
  1737. ecdsa_freekey(ec);
  1738. return NULL;
  1739. }
  1740. ec->privateKey = bignum_from_bytes_le((const unsigned char *)q, 32);
  1741. /* Check that private key generates public key */
  1742. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1743. if (!publicKey ||
  1744. bignum_cmp(publicKey->x, ec->publicKey.x) ||
  1745. bignum_cmp(publicKey->y, ec->publicKey.y))
  1746. {
  1747. ecdsa_freekey(ec);
  1748. ec = NULL;
  1749. }
  1750. ec_point_free(publicKey);
  1751. /* The OpenSSH format for ed25519 private keys also for some
  1752. * reason encodes an extra copy of the public key in the second
  1753. * half of the secret-key string. Check that that's present and
  1754. * correct as well, otherwise the key we think we've imported
  1755. * won't behave identically to the way OpenSSH would have treated
  1756. * it. */
  1757. if (plen != 32 || 0 != memcmp(q + 32, p, 32)) {
  1758. ecdsa_freekey(ec);
  1759. return NULL;
  1760. }
  1761. return ec;
  1762. }
  1763. static int ed25519_openssh_fmtkey(void *key, unsigned char *blob, int len)
  1764. {
  1765. struct ec_key *ec = (struct ec_key *) key;
  1766. int pointlen;
  1767. int keylen;
  1768. int bloblen;
  1769. int i;
  1770. if (ec->publicKey.curve->type != EC_EDWARDS) {
  1771. return 0;
  1772. }
  1773. pointlen = (bignum_bitcount(ec->publicKey.y) + 7) / 8;
  1774. keylen = (bignum_bitcount(ec->privateKey) + 7) / 8;
  1775. bloblen = 4 + pointlen + 4 + keylen + pointlen;
  1776. if (bloblen > len)
  1777. return bloblen;
  1778. /* Encode the public point */
  1779. PUT_32BIT(blob, pointlen);
  1780. blob += 4;
  1781. for (i = 0; i < pointlen - 1; ++i) {
  1782. *blob++ = bignum_byte(ec->publicKey.y, i);
  1783. }
  1784. /* Unset last bit of y and set first bit of x in its place */
  1785. *blob = bignum_byte(ec->publicKey.y, i) & 0x7f;
  1786. *blob++ |= bignum_bit(ec->publicKey.x, 0) << 7;
  1787. PUT_32BIT(blob, keylen + pointlen);
  1788. blob += 4;
  1789. for (i = 0; i < keylen; ++i) {
  1790. *blob++ = bignum_byte(ec->privateKey, i);
  1791. }
  1792. /* Now encode an extra copy of the public point as the second half
  1793. * of the private key string, as the OpenSSH format for some
  1794. * reason requires */
  1795. for (i = 0; i < pointlen - 1; ++i) {
  1796. *blob++ = bignum_byte(ec->publicKey.y, i);
  1797. }
  1798. /* Unset last bit of y and set first bit of x in its place */
  1799. *blob = bignum_byte(ec->publicKey.y, i) & 0x7f;
  1800. *blob++ |= bignum_bit(ec->publicKey.x, 0) << 7;
  1801. return bloblen;
  1802. }
  1803. static void *ecdsa_openssh_createkey(const struct ssh_signkey *self,
  1804. const unsigned char **blob, int *len)
  1805. {
  1806. const struct ecsign_extra *extra =
  1807. (const struct ecsign_extra *)self->extra;
  1808. const char **b = (const char **) blob;
  1809. const char *p;
  1810. int slen;
  1811. struct ec_key *ec;
  1812. struct ec_curve *curve;
  1813. struct ec_point *publicKey;
  1814. getstring(b, len, &p, &slen);
  1815. if (!p) {
  1816. return NULL;
  1817. }
  1818. curve = extra->curve();
  1819. assert(curve->type == EC_WEIERSTRASS);
  1820. ec = snew(struct ec_key);
  1821. ec->signalg = self;
  1822. ec->publicKey.curve = curve;
  1823. ec->publicKey.infinity = 0;
  1824. ec->publicKey.x = NULL;
  1825. ec->publicKey.y = NULL;
  1826. ec->publicKey.z = NULL;
  1827. if (!getmppoint(b, len, &ec->publicKey)) {
  1828. ecdsa_freekey(ec);
  1829. return NULL;
  1830. }
  1831. ec->privateKey = NULL;
  1832. if (!ec->publicKey.x || !ec->publicKey.y ||
  1833. bignum_cmp(ec->publicKey.x, curve->p) >= 0 ||
  1834. bignum_cmp(ec->publicKey.y, curve->p) >= 0)
  1835. {
  1836. ecdsa_freekey(ec);
  1837. return NULL;
  1838. }
  1839. ec->privateKey = getmp(b, len);
  1840. if (ec->privateKey == NULL)
  1841. {
  1842. ecdsa_freekey(ec);
  1843. return NULL;
  1844. }
  1845. /* Now check that the private key makes the public key */
  1846. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1847. if (!publicKey)
  1848. {
  1849. ecdsa_freekey(ec);
  1850. return NULL;
  1851. }
  1852. if (bignum_cmp(ec->publicKey.x, publicKey->x) ||
  1853. bignum_cmp(ec->publicKey.y, publicKey->y))
  1854. {
  1855. /* Private key doesn't make the public key on the given curve */
  1856. ecdsa_freekey(ec);
  1857. ec_point_free(publicKey);
  1858. return NULL;
  1859. }
  1860. ec_point_free(publicKey);
  1861. return ec;
  1862. }
  1863. static int ecdsa_openssh_fmtkey(void *key, unsigned char *blob, int len)
  1864. {
  1865. struct ec_key *ec = (struct ec_key *) key;
  1866. int pointlen;
  1867. int namelen;
  1868. int bloblen;
  1869. int i;
  1870. if (ec->publicKey.curve->type != EC_WEIERSTRASS) {
  1871. return 0;
  1872. }
  1873. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  1874. namelen = strlen(ec->publicKey.curve->name);
  1875. bloblen =
  1876. 4 + namelen /* <LEN> nistpXXX */
  1877. + 4 + 1 + (pointlen * 2) /* <LEN> 0x04 pX pY */
  1878. + ssh2_bignum_length(ec->privateKey);
  1879. if (bloblen > len)
  1880. return bloblen;
  1881. bloblen = 0;
  1882. PUT_32BIT(blob+bloblen, namelen);
  1883. bloblen += 4;
  1884. memcpy(blob+bloblen, ec->publicKey.curve->name, namelen);
  1885. bloblen += namelen;
  1886. PUT_32BIT(blob+bloblen, 1 + (pointlen * 2));
  1887. bloblen += 4;
  1888. blob[bloblen++] = 0x04;
  1889. for (i = pointlen; i--; )
  1890. blob[bloblen++] = bignum_byte(ec->publicKey.x, i);
  1891. for (i = pointlen; i--; )
  1892. blob[bloblen++] = bignum_byte(ec->publicKey.y, i);
  1893. pointlen = (bignum_bitcount(ec->privateKey) + 8) / 8;
  1894. PUT_32BIT(blob+bloblen, pointlen);
  1895. bloblen += 4;
  1896. for (i = pointlen; i--; )
  1897. blob[bloblen++] = bignum_byte(ec->privateKey, i);
  1898. return bloblen;
  1899. }
  1900. static int ecdsa_pubkey_bits(const struct ssh_signkey *self,
  1901. const void *blob, int len)
  1902. {
  1903. struct ec_key *ec;
  1904. int ret;
  1905. ec = (struct ec_key*)ecdsa_newkey(self, (const char *) blob, len);
  1906. if (!ec)
  1907. return -1;
  1908. ret = ec->publicKey.curve->fieldBits;
  1909. ecdsa_freekey(ec);
  1910. return ret;
  1911. }
  1912. static int ecdsa_verifysig(void *key, const char *sig, int siglen,
  1913. const char *data, int datalen)
  1914. {
  1915. struct ec_key *ec = (struct ec_key *) key;
  1916. const struct ecsign_extra *extra =
  1917. (const struct ecsign_extra *)ec->signalg->extra;
  1918. const char *p;
  1919. int slen;
  1920. int digestLen;
  1921. int ret;
  1922. if (!ec->publicKey.x || !ec->publicKey.y || !ec->publicKey.curve)
  1923. return 0;
  1924. /* Check the signature starts with the algorithm name */
  1925. getstring(&sig, &siglen, &p, &slen);
  1926. if (!p) {
  1927. return 0;
  1928. }
  1929. if (!match_ssh_id(slen, p, ec->signalg->name)) {
  1930. return 0;
  1931. }
  1932. getstring(&sig, &siglen, &p, &slen);
  1933. if (!p) return 0;
  1934. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1935. struct ec_point *r;
  1936. Bignum s, h;
  1937. /* Check that the signature is two times the length of a point */
  1938. if (slen != (ec->publicKey.curve->fieldBits / 8) * 2) {
  1939. return 0;
  1940. }
  1941. /* Check it's the 256 bit field so that SHA512 is the correct hash */
  1942. if (ec->publicKey.curve->fieldBits != 256) {
  1943. return 0;
  1944. }
  1945. /* Get the signature */
  1946. r = ec_point_new(ec->publicKey.curve, NULL, NULL, NULL, 0);
  1947. if (!r) {
  1948. return 0;
  1949. }
  1950. if (!decodepoint(p, ec->publicKey.curve->fieldBits / 8, r)) {
  1951. ec_point_free(r);
  1952. return 0;
  1953. }
  1954. s = bignum_from_bytes_le((unsigned char*)p + (ec->publicKey.curve->fieldBits / 8),
  1955. ec->publicKey.curve->fieldBits / 8);
  1956. /* Get the hash of the encoded value of R + encoded value of pk + message */
  1957. {
  1958. int i, pointlen;
  1959. unsigned char b;
  1960. unsigned char digest[512 / 8];
  1961. SHA512_State hs;
  1962. SHA512_Init(&hs);
  1963. /* Add encoded r (no need to encode it again, it was in the signature) */
  1964. SHA512_Bytes(&hs, p, ec->publicKey.curve->fieldBits / 8);
  1965. /* Encode pk and add it */
  1966. pointlen = ec->publicKey.curve->fieldBits / 8;
  1967. for (i = 0; i < pointlen - 1; ++i) {
  1968. b = bignum_byte(ec->publicKey.y, i);
  1969. SHA512_Bytes(&hs, &b, 1);
  1970. }
  1971. /* Unset last bit of y and set first bit of x in its place */
  1972. b = bignum_byte(ec->publicKey.y, i) & 0x7f;
  1973. b |= bignum_bit(ec->publicKey.x, 0) << 7;
  1974. SHA512_Bytes(&hs, &b, 1);
  1975. /* Add the message itself */
  1976. SHA512_Bytes(&hs, data, datalen);
  1977. /* Get the hash */
  1978. SHA512_Final(&hs, digest);
  1979. /* Convert to Bignum */
  1980. h = bignum_from_bytes_le(digest, sizeof(digest));
  1981. }
  1982. /* Verify sB == r + h*publicKey */
  1983. {
  1984. struct ec_point *lhs, *rhs, *tmp;
  1985. /* lhs = sB */
  1986. lhs = ecp_mul(&ec->publicKey.curve->e.B, s);
  1987. freebn(s);
  1988. if (!lhs) {
  1989. ec_point_free(r);
  1990. freebn(h);
  1991. return 0;
  1992. }
  1993. /* rhs = r + h*publicKey */
  1994. tmp = ecp_mul(&ec->publicKey, h);
  1995. freebn(h);
  1996. if (!tmp) {
  1997. ec_point_free(lhs);
  1998. ec_point_free(r);
  1999. return 0;
  2000. }
  2001. rhs = ecp_add(r, tmp, 0);
  2002. ec_point_free(r);
  2003. ec_point_free(tmp);
  2004. if (!rhs) {
  2005. ec_point_free(lhs);
  2006. return 0;
  2007. }
  2008. /* Check the point is the same */
  2009. ret = !bignum_cmp(lhs->x, rhs->x);
  2010. if (ret) {
  2011. ret = !bignum_cmp(lhs->y, rhs->y);
  2012. if (ret) {
  2013. ret = 1;
  2014. }
  2015. }
  2016. ec_point_free(lhs);
  2017. ec_point_free(rhs);
  2018. }
  2019. } else {
  2020. Bignum r, s;
  2021. unsigned char digest[512 / 8];
  2022. void *hashctx;
  2023. r = getmp(&p, &slen);
  2024. if (!r) return 0;
  2025. s = getmp(&p, &slen);
  2026. if (!s) {
  2027. freebn(r);
  2028. return 0;
  2029. }
  2030. digestLen = extra->hash->hlen;
  2031. assert(digestLen <= sizeof(digest));
  2032. hashctx = extra->hash->init();
  2033. extra->hash->bytes(hashctx, data, datalen);
  2034. extra->hash->final(hashctx, digest);
  2035. /* Verify the signature */
  2036. ret = _ecdsa_verify(&ec->publicKey, digest, digestLen, r, s);
  2037. freebn(r);
  2038. freebn(s);
  2039. }
  2040. return ret;
  2041. }
  2042. static unsigned char *ecdsa_sign(void *key, const char *data, int datalen,
  2043. int *siglen)
  2044. {
  2045. struct ec_key *ec = (struct ec_key *) key;
  2046. const struct ecsign_extra *extra =
  2047. (const struct ecsign_extra *)ec->signalg->extra;
  2048. unsigned char digest[512 / 8];
  2049. int digestLen;
  2050. Bignum r = NULL, s = NULL;
  2051. unsigned char *buf, *p;
  2052. int rlen, slen, namelen;
  2053. int i;
  2054. if (!ec->privateKey || !ec->publicKey.curve) {
  2055. return NULL;
  2056. }
  2057. if (ec->publicKey.curve->type == EC_EDWARDS) {
  2058. struct ec_point *rp;
  2059. int pointlen = ec->publicKey.curve->fieldBits / 8;
  2060. /* hash = H(sk) (where hash creates 2 * fieldBits)
  2061. * b = fieldBits
  2062. * a = 2^(b-2) + SUM(2^i * h_i) for i = 2 -> b-2
  2063. * r = H(h[b/8:b/4] + m)
  2064. * R = rB
  2065. * S = (r + H(encodepoint(R) + encodepoint(pk) + m) * a) % l */
  2066. {
  2067. unsigned char hash[512/8];
  2068. unsigned char b;
  2069. Bignum a;
  2070. SHA512_State hs;
  2071. SHA512_Init(&hs);
  2072. for (i = 0; i < pointlen; ++i) {
  2073. unsigned char b = (unsigned char)bignum_byte(ec->privateKey, i);
  2074. SHA512_Bytes(&hs, &b, 1);
  2075. }
  2076. SHA512_Final(&hs, hash);
  2077. /* The second part is simply turning the hash into a
  2078. * Bignum, however the 2^(b-2) bit *must* be set, and the
  2079. * bottom 3 bits *must* not be */
  2080. hash[0] &= 0xf8; /* Unset bottom 3 bits (if set) */
  2081. hash[31] &= 0x7f; /* Unset above (b-2) */
  2082. hash[31] |= 0x40; /* Set 2^(b-2) */
  2083. /* Chop off the top part and convert to int */
  2084. a = bignum_from_bytes_le(hash, 32);
  2085. SHA512_Init(&hs);
  2086. SHA512_Bytes(&hs,
  2087. hash+(ec->publicKey.curve->fieldBits / 8),
  2088. (ec->publicKey.curve->fieldBits / 4)
  2089. - (ec->publicKey.curve->fieldBits / 8));
  2090. SHA512_Bytes(&hs, data, datalen);
  2091. SHA512_Final(&hs, hash);
  2092. r = bignum_from_bytes_le(hash, 512/8);
  2093. rp = ecp_mul(&ec->publicKey.curve->e.B, r);
  2094. if (!rp) {
  2095. freebn(r);
  2096. freebn(a);
  2097. return NULL;
  2098. }
  2099. /* Now calculate s */
  2100. SHA512_Init(&hs);
  2101. /* Encode the point R */
  2102. for (i = 0; i < pointlen - 1; ++i) {
  2103. b = bignum_byte(rp->y, i);
  2104. SHA512_Bytes(&hs, &b, 1);
  2105. }
  2106. /* Unset last bit of y and set first bit of x in its place */
  2107. b = bignum_byte(rp->y, i) & 0x7f;
  2108. b |= bignum_bit(rp->x, 0) << 7;
  2109. SHA512_Bytes(&hs, &b, 1);
  2110. /* Encode the point pk */
  2111. for (i = 0; i < pointlen - 1; ++i) {
  2112. b = bignum_byte(ec->publicKey.y, i);
  2113. SHA512_Bytes(&hs, &b, 1);
  2114. }
  2115. /* Unset last bit of y and set first bit of x in its place */
  2116. b = bignum_byte(ec->publicKey.y, i) & 0x7f;
  2117. b |= bignum_bit(ec->publicKey.x, 0) << 7;
  2118. SHA512_Bytes(&hs, &b, 1);
  2119. /* Add the message */
  2120. SHA512_Bytes(&hs, data, datalen);
  2121. SHA512_Final(&hs, hash);
  2122. {
  2123. Bignum tmp, tmp2;
  2124. tmp = bignum_from_bytes_le(hash, 512/8);
  2125. tmp2 = modmul(tmp, a, ec->publicKey.curve->e.l);
  2126. freebn(a);
  2127. freebn(tmp);
  2128. tmp = bigadd(r, tmp2);
  2129. freebn(r);
  2130. freebn(tmp2);
  2131. s = bigmod(tmp, ec->publicKey.curve->e.l);
  2132. freebn(tmp);
  2133. }
  2134. }
  2135. /* Format the output */
  2136. namelen = strlen(ec->signalg->name);
  2137. *siglen = 4+namelen+4+((ec->publicKey.curve->fieldBits / 8)*2);
  2138. buf = snewn(*siglen, unsigned char);
  2139. p = buf;
  2140. PUT_32BIT(p, namelen);
  2141. p += 4;
  2142. memcpy(p, ec->signalg->name, namelen);
  2143. p += namelen;
  2144. PUT_32BIT(p, ((ec->publicKey.curve->fieldBits / 8)*2));
  2145. p += 4;
  2146. /* Encode the point */
  2147. pointlen = ec->publicKey.curve->fieldBits / 8;
  2148. for (i = 0; i < pointlen - 1; ++i) {
  2149. *p++ = bignum_byte(rp->y, i);
  2150. }
  2151. /* Unset last bit of y and set first bit of x in its place */
  2152. *p = bignum_byte(rp->y, i) & 0x7f;
  2153. *p++ |= bignum_bit(rp->x, 0) << 7;
  2154. ec_point_free(rp);
  2155. /* Encode the int */
  2156. for (i = 0; i < pointlen; ++i) {
  2157. *p++ = bignum_byte(s, i);
  2158. }
  2159. freebn(s);
  2160. } else {
  2161. void *hashctx;
  2162. digestLen = extra->hash->hlen;
  2163. assert(digestLen <= sizeof(digest));
  2164. hashctx = extra->hash->init();
  2165. extra->hash->bytes(hashctx, data, datalen);
  2166. extra->hash->final(hashctx, digest);
  2167. /* Do the signature */
  2168. _ecdsa_sign(ec->privateKey, ec->publicKey.curve, digest, digestLen, &r, &s);
  2169. if (!r || !s) {
  2170. if (r) freebn(r);
  2171. if (s) freebn(s);
  2172. return NULL;
  2173. }
  2174. rlen = (bignum_bitcount(r) + 8) / 8;
  2175. slen = (bignum_bitcount(s) + 8) / 8;
  2176. namelen = strlen(ec->signalg->name);
  2177. /* Format the output */
  2178. *siglen = 8+namelen+rlen+slen+8;
  2179. buf = snewn(*siglen, unsigned char);
  2180. p = buf;
  2181. PUT_32BIT(p, namelen);
  2182. p += 4;
  2183. memcpy(p, ec->signalg->name, namelen);
  2184. p += namelen;
  2185. PUT_32BIT(p, rlen + slen + 8);
  2186. p += 4;
  2187. PUT_32BIT(p, rlen);
  2188. p += 4;
  2189. for (i = rlen; i--;)
  2190. *p++ = bignum_byte(r, i);
  2191. PUT_32BIT(p, slen);
  2192. p += 4;
  2193. for (i = slen; i--;)
  2194. *p++ = bignum_byte(s, i);
  2195. freebn(r);
  2196. freebn(s);
  2197. }
  2198. return buf;
  2199. }
  2200. const struct ecsign_extra sign_extra_ed25519 = {
  2201. ec_ed25519, NULL,
  2202. NULL, 0,
  2203. };
  2204. const struct ssh_signkey ssh_ecdsa_ed25519 = {
  2205. ecdsa_newkey,
  2206. ecdsa_freekey,
  2207. ecdsa_fmtkey,
  2208. ecdsa_public_blob,
  2209. ecdsa_private_blob,
  2210. ecdsa_createkey,
  2211. ed25519_openssh_createkey,
  2212. ed25519_openssh_fmtkey,
  2213. 2 /* point, private exponent */,
  2214. ecdsa_pubkey_bits,
  2215. ecdsa_verifysig,
  2216. ecdsa_sign,
  2217. "ssh-ed25519",
  2218. "ssh-ed25519",
  2219. &sign_extra_ed25519,
  2220. };
  2221. /* OID: 1.2.840.10045.3.1.7 (ansiX9p256r1) */
  2222. static const unsigned char nistp256_oid[] = {
  2223. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  2224. };
  2225. const struct ecsign_extra sign_extra_nistp256 = {
  2226. ec_p256, &ssh_sha256,
  2227. nistp256_oid, lenof(nistp256_oid),
  2228. };
  2229. const struct ssh_signkey ssh_ecdsa_nistp256 = {
  2230. ecdsa_newkey,
  2231. ecdsa_freekey,
  2232. ecdsa_fmtkey,
  2233. ecdsa_public_blob,
  2234. ecdsa_private_blob,
  2235. ecdsa_createkey,
  2236. ecdsa_openssh_createkey,
  2237. ecdsa_openssh_fmtkey,
  2238. 3 /* curve name, point, private exponent */,
  2239. ecdsa_pubkey_bits,
  2240. ecdsa_verifysig,
  2241. ecdsa_sign,
  2242. "ecdsa-sha2-nistp256",
  2243. "ecdsa-sha2-nistp256",
  2244. &sign_extra_nistp256,
  2245. };
  2246. /* OID: 1.3.132.0.34 (secp384r1) */
  2247. static const unsigned char nistp384_oid[] = {
  2248. 0x2b, 0x81, 0x04, 0x00, 0x22
  2249. };
  2250. const struct ecsign_extra sign_extra_nistp384 = {
  2251. ec_p384, &ssh_sha384,
  2252. nistp384_oid, lenof(nistp384_oid),
  2253. };
  2254. const struct ssh_signkey ssh_ecdsa_nistp384 = {
  2255. ecdsa_newkey,
  2256. ecdsa_freekey,
  2257. ecdsa_fmtkey,
  2258. ecdsa_public_blob,
  2259. ecdsa_private_blob,
  2260. ecdsa_createkey,
  2261. ecdsa_openssh_createkey,
  2262. ecdsa_openssh_fmtkey,
  2263. 3 /* curve name, point, private exponent */,
  2264. ecdsa_pubkey_bits,
  2265. ecdsa_verifysig,
  2266. ecdsa_sign,
  2267. "ecdsa-sha2-nistp384",
  2268. "ecdsa-sha2-nistp384",
  2269. &sign_extra_nistp384,
  2270. };
  2271. /* OID: 1.3.132.0.35 (secp521r1) */
  2272. static const unsigned char nistp521_oid[] = {
  2273. 0x2b, 0x81, 0x04, 0x00, 0x23
  2274. };
  2275. const struct ecsign_extra sign_extra_nistp521 = {
  2276. ec_p521, &ssh_sha512,
  2277. nistp521_oid, lenof(nistp521_oid),
  2278. };
  2279. const struct ssh_signkey ssh_ecdsa_nistp521 = {
  2280. ecdsa_newkey,
  2281. ecdsa_freekey,
  2282. ecdsa_fmtkey,
  2283. ecdsa_public_blob,
  2284. ecdsa_private_blob,
  2285. ecdsa_createkey,
  2286. ecdsa_openssh_createkey,
  2287. ecdsa_openssh_fmtkey,
  2288. 3 /* curve name, point, private exponent */,
  2289. ecdsa_pubkey_bits,
  2290. ecdsa_verifysig,
  2291. ecdsa_sign,
  2292. "ecdsa-sha2-nistp521",
  2293. "ecdsa-sha2-nistp521",
  2294. &sign_extra_nistp521,
  2295. };
  2296. /* ----------------------------------------------------------------------
  2297. * Exposed ECDH interface
  2298. */
  2299. struct eckex_extra {
  2300. struct ec_curve *(*curve)(void);
  2301. };
  2302. static Bignum ecdh_calculate(const Bignum private,
  2303. const struct ec_point *public)
  2304. {
  2305. struct ec_point *p;
  2306. Bignum ret;
  2307. p = ecp_mul(public, private);
  2308. if (!p) return NULL;
  2309. ret = p->x;
  2310. p->x = NULL;
  2311. if (p->curve->type == EC_MONTGOMERY) {
  2312. /*
  2313. * Endianness-swap. The Curve25519 algorithm definition
  2314. * assumes you were doing your computation in arrays of 32
  2315. * little-endian bytes, and now specifies that you take your
  2316. * final one of those and convert it into a bignum in
  2317. * _network_ byte order, i.e. big-endian.
  2318. *
  2319. * In particular, the spec says, you convert the _whole_ 32
  2320. * bytes into a bignum. That is, on the rare occasions that
  2321. * p->x has come out with the most significant 8 bits zero, we
  2322. * have to imagine that being represented by a 32-byte string
  2323. * with the last byte being zero, so that has to be converted
  2324. * into an SSH-2 bignum with the _low_ byte zero, i.e. a
  2325. * multiple of 256.
  2326. */
  2327. int i;
  2328. int bytes = (p->curve->fieldBits+7) / 8;
  2329. unsigned char *byteorder = snewn(bytes, unsigned char);
  2330. for (i = 0; i < bytes; ++i) {
  2331. byteorder[i] = bignum_byte(ret, i);
  2332. }
  2333. freebn(ret);
  2334. ret = bignum_from_bytes(byteorder, bytes);
  2335. smemclr(byteorder, bytes);
  2336. sfree(byteorder);
  2337. }
  2338. ec_point_free(p);
  2339. return ret;
  2340. }
  2341. const char *ssh_ecdhkex_curve_textname(const struct ssh_kex *kex)
  2342. {
  2343. const struct eckex_extra *extra = (const struct eckex_extra *)kex->extra;
  2344. struct ec_curve *curve = extra->curve();
  2345. return curve->textname;
  2346. }
  2347. void *ssh_ecdhkex_newkey(const struct ssh_kex *kex)
  2348. {
  2349. const struct eckex_extra *extra = (const struct eckex_extra *)kex->extra;
  2350. struct ec_curve *curve;
  2351. struct ec_key *key;
  2352. struct ec_point *publicKey;
  2353. curve = extra->curve();
  2354. key = snew(struct ec_key);
  2355. key->signalg = NULL;
  2356. key->publicKey.curve = curve;
  2357. if (curve->type == EC_MONTGOMERY) {
  2358. unsigned char bytes[32] = {0};
  2359. int i;
  2360. for (i = 0; i < sizeof(bytes); ++i)
  2361. {
  2362. bytes[i] = (unsigned char)random_byte();
  2363. }
  2364. bytes[0] &= 248;
  2365. bytes[31] &= 127;
  2366. bytes[31] |= 64;
  2367. key->privateKey = bignum_from_bytes_le(bytes, sizeof(bytes));
  2368. smemclr(bytes, sizeof(bytes));
  2369. if (!key->privateKey) {
  2370. sfree(key);
  2371. return NULL;
  2372. }
  2373. publicKey = ecp_mul(&key->publicKey.curve->m.G, key->privateKey);
  2374. if (!publicKey) {
  2375. freebn(key->privateKey);
  2376. sfree(key);
  2377. return NULL;
  2378. }
  2379. key->publicKey.x = publicKey->x;
  2380. key->publicKey.y = publicKey->y;
  2381. key->publicKey.z = NULL;
  2382. sfree(publicKey);
  2383. } else {
  2384. key->privateKey = bignum_random_in_range(One, key->publicKey.curve->w.n);
  2385. if (!key->privateKey) {
  2386. sfree(key);
  2387. return NULL;
  2388. }
  2389. publicKey = ecp_mul(&key->publicKey.curve->w.G, key->privateKey);
  2390. if (!publicKey) {
  2391. freebn(key->privateKey);
  2392. sfree(key);
  2393. return NULL;
  2394. }
  2395. key->publicKey.x = publicKey->x;
  2396. key->publicKey.y = publicKey->y;
  2397. key->publicKey.z = NULL;
  2398. sfree(publicKey);
  2399. }
  2400. return key;
  2401. }
  2402. char *ssh_ecdhkex_getpublic(void *key, int *len)
  2403. {
  2404. struct ec_key *ec = (struct ec_key*)key;
  2405. char *point, *p;
  2406. int i;
  2407. int pointlen;
  2408. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  2409. if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  2410. *len = 1 + pointlen * 2;
  2411. } else {
  2412. *len = pointlen;
  2413. }
  2414. point = (char*)snewn(*len, char);
  2415. p = point;
  2416. if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  2417. *p++ = 0x04;
  2418. for (i = pointlen; i--;) {
  2419. *p++ = bignum_byte(ec->publicKey.x, i);
  2420. }
  2421. for (i = pointlen; i--;) {
  2422. *p++ = bignum_byte(ec->publicKey.y, i);
  2423. }
  2424. } else {
  2425. for (i = 0; i < pointlen; ++i) {
  2426. *p++ = bignum_byte(ec->publicKey.x, i);
  2427. }
  2428. }
  2429. return point;
  2430. }
  2431. Bignum ssh_ecdhkex_getkey(void *key, char *remoteKey, int remoteKeyLen)
  2432. {
  2433. struct ec_key *ec = (struct ec_key*) key;
  2434. struct ec_point remote;
  2435. Bignum ret;
  2436. if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  2437. remote.curve = ec->publicKey.curve;
  2438. remote.infinity = 0;
  2439. if (!decodepoint(remoteKey, remoteKeyLen, &remote)) {
  2440. return NULL;
  2441. }
  2442. } else {
  2443. /* Point length has to be the same length */
  2444. if (remoteKeyLen != (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8) {
  2445. return NULL;
  2446. }
  2447. remote.curve = ec->publicKey.curve;
  2448. remote.infinity = 0;
  2449. remote.x = bignum_from_bytes_le((unsigned char*)remoteKey, remoteKeyLen);
  2450. remote.y = NULL;
  2451. remote.z = NULL;
  2452. }
  2453. ret = ecdh_calculate(ec->privateKey, &remote);
  2454. if (remote.x) freebn(remote.x);
  2455. if (remote.y) freebn(remote.y);
  2456. return ret;
  2457. }
  2458. void ssh_ecdhkex_freekey(void *key)
  2459. {
  2460. ecdsa_freekey(key);
  2461. }
  2462. static const struct eckex_extra kex_extra_curve25519 = { ec_curve25519 };
  2463. static const struct ssh_kex ssh_ec_kex_curve25519 = {
  2464. "[email protected]", NULL, KEXTYPE_ECDH,
  2465. &ssh_sha256, &kex_extra_curve25519,
  2466. };
  2467. const struct eckex_extra kex_extra_nistp256 = { ec_p256 };
  2468. static const struct ssh_kex ssh_ec_kex_nistp256 = {
  2469. "ecdh-sha2-nistp256", NULL, KEXTYPE_ECDH,
  2470. &ssh_sha256, &kex_extra_nistp256,
  2471. };
  2472. const struct eckex_extra kex_extra_nistp384 = { ec_p384 };
  2473. static const struct ssh_kex ssh_ec_kex_nistp384 = {
  2474. "ecdh-sha2-nistp384", NULL, KEXTYPE_ECDH,
  2475. &ssh_sha384, &kex_extra_nistp384,
  2476. };
  2477. const struct eckex_extra kex_extra_nistp521 = { ec_p521 };
  2478. static const struct ssh_kex ssh_ec_kex_nistp521 = {
  2479. "ecdh-sha2-nistp521", NULL, KEXTYPE_ECDH,
  2480. &ssh_sha512, &kex_extra_nistp521,
  2481. };
  2482. static const struct ssh_kex *const ec_kex_list[] = {
  2483. &ssh_ec_kex_curve25519,
  2484. &ssh_ec_kex_nistp256,
  2485. &ssh_ec_kex_nistp384,
  2486. &ssh_ec_kex_nistp521,
  2487. };
  2488. const struct ssh_kexes ssh_ecdh_kex = {
  2489. sizeof(ec_kex_list) / sizeof(*ec_kex_list),
  2490. ec_kex_list
  2491. };
  2492. /* ----------------------------------------------------------------------
  2493. * Helper functions for finding key algorithms and returning auxiliary
  2494. * data.
  2495. */
  2496. const struct ssh_signkey *ec_alg_by_oid(int len, const void *oid,
  2497. const struct ec_curve **curve)
  2498. {
  2499. static const struct ssh_signkey *algs_with_oid[] = {
  2500. &ssh_ecdsa_nistp256,
  2501. &ssh_ecdsa_nistp384,
  2502. &ssh_ecdsa_nistp521,
  2503. };
  2504. int i;
  2505. for (i = 0; i < lenof(algs_with_oid); i++) {
  2506. const struct ssh_signkey *alg = algs_with_oid[i];
  2507. const struct ecsign_extra *extra =
  2508. (const struct ecsign_extra *)alg->extra;
  2509. if (len == extra->oidlen && !memcmp(oid, extra->oid, len)) {
  2510. *curve = extra->curve();
  2511. return alg;
  2512. }
  2513. }
  2514. return NULL;
  2515. }
  2516. const unsigned char *ec_alg_oid(const struct ssh_signkey *alg,
  2517. int *oidlen)
  2518. {
  2519. const struct ecsign_extra *extra = (const struct ecsign_extra *)alg->extra;
  2520. *oidlen = extra->oidlen;
  2521. return extra->oid;
  2522. }
  2523. const int ec_nist_curve_lengths[] = { 256, 384, 521 };
  2524. const int n_ec_nist_curve_lengths = lenof(ec_nist_curve_lengths);
  2525. const int ec_nist_alg_and_curve_by_bits(int bits,
  2526. const struct ec_curve **curve,
  2527. const struct ssh_signkey **alg)
  2528. {
  2529. switch (bits) {
  2530. case 256: *alg = &ssh_ecdsa_nistp256; break;
  2531. case 384: *alg = &ssh_ecdsa_nistp384; break;
  2532. case 521: *alg = &ssh_ecdsa_nistp521; break;
  2533. default: return FALSE;
  2534. }
  2535. *curve = ((struct ecsign_extra *)(*alg)->extra)->curve();
  2536. return TRUE;
  2537. }
  2538. const int ec_ed_alg_and_curve_by_bits(int bits,
  2539. const struct ec_curve **curve,
  2540. const struct ssh_signkey **alg)
  2541. {
  2542. switch (bits) {
  2543. case 256: *alg = &ssh_ecdsa_ed25519; break;
  2544. default: return FALSE;
  2545. }
  2546. *curve = ((struct ecsign_extra *)(*alg)->extra)->curve();
  2547. return TRUE;
  2548. }