sshecc.c 80 KB

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