sshecc.c 84 KB

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