sshecc.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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, Ed448)
  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. #include "mpint.h"
  37. #include "ecc.h"
  38. #ifdef MPEXT
  39. int ec_curve_cleanup = 0;
  40. static void finalize_common(struct ec_curve * curve)
  41. {
  42. mp_free(curve->p);
  43. }
  44. static void finalize_wcurve(struct ec_curve *curve)
  45. {
  46. ecc_weierstrass_curve_free(curve->w.wc);
  47. ecc_weierstrass_point_free(curve->w.G);
  48. mp_free(curve->w.G_order);
  49. finalize_common(curve);
  50. }
  51. static void finalize_mcurve(struct ec_curve *curve)
  52. {
  53. ecc_montgomery_curve_free(curve->m.mc);
  54. ecc_montgomery_point_free(curve->m.G);
  55. finalize_common(curve);
  56. }
  57. static void finalize_ecurve(struct ec_curve *curve)
  58. {
  59. ecc_edwards_curve_free(curve->e.ec);
  60. ecc_edwards_point_free(curve->e.G);
  61. mp_free(curve->e.G_order);
  62. finalize_common(curve);
  63. }
  64. #endif
  65. /* ----------------------------------------------------------------------
  66. * Elliptic curve definitions
  67. */
  68. static void initialise_common(
  69. struct ec_curve *curve, EllipticCurveType type, mp_int *p,
  70. unsigned extrabits)
  71. {
  72. curve->type = type;
  73. curve->p = mp_copy(p);
  74. curve->fieldBits = mp_get_nbits(p);
  75. curve->fieldBytes = (curve->fieldBits + extrabits + 7) / 8;
  76. }
  77. static void initialise_wcurve(
  78. struct ec_curve *curve, mp_int *p, mp_int *a, mp_int *b,
  79. mp_int *nonsquare, mp_int *G_x, mp_int *G_y, mp_int *G_order)
  80. {
  81. initialise_common(curve, EC_WEIERSTRASS, p, 0);
  82. curve->w.wc = ecc_weierstrass_curve(p, a, b, nonsquare);
  83. curve->w.G = ecc_weierstrass_point_new(curve->w.wc, G_x, G_y);
  84. curve->w.G_order = mp_copy(G_order);
  85. }
  86. static void initialise_mcurve(
  87. struct ec_curve *curve, mp_int *p, mp_int *a, mp_int *b,
  88. mp_int *G_x, unsigned log2_cofactor)
  89. {
  90. initialise_common(curve, EC_MONTGOMERY, p, 0);
  91. curve->m.mc = ecc_montgomery_curve(p, a, b);
  92. curve->m.log2_cofactor = log2_cofactor;
  93. curve->m.G = ecc_montgomery_point_new(curve->m.mc, G_x);
  94. }
  95. static void initialise_ecurve(
  96. struct ec_curve *curve, mp_int *p, mp_int *d, mp_int *a,
  97. mp_int *nonsquare, mp_int *G_x, mp_int *G_y, mp_int *G_order,
  98. unsigned log2_cofactor)
  99. {
  100. /* Ensure curve->fieldBytes is long enough to store an extra bit
  101. * for a compressed point */
  102. initialise_common(curve, EC_EDWARDS, p, 1);
  103. curve->e.ec = ecc_edwards_curve(p, d, a, nonsquare);
  104. curve->e.log2_cofactor = log2_cofactor;
  105. curve->e.G = ecc_edwards_point_new(curve->e.ec, G_x, G_y);
  106. curve->e.G_order = mp_copy(G_order);
  107. }
  108. static struct ec_curve *ec_p256(void)
  109. {
  110. static struct ec_curve curve = { 0 };
  111. static bool initialised = false;
  112. #ifdef MPEXT
  113. if (ec_curve_cleanup)
  114. {
  115. if (initialised) finalize_wcurve(&curve);
  116. initialised = 0;
  117. return NULL;
  118. }
  119. #endif
  120. if (!initialised)
  121. {
  122. mp_int *p = MP_LITERAL(0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff);
  123. mp_int *a = MP_LITERAL(0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc);
  124. mp_int *b = MP_LITERAL(0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b);
  125. mp_int *G_x = MP_LITERAL(0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296);
  126. mp_int *G_y = MP_LITERAL(0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5);
  127. mp_int *G_order = MP_LITERAL(0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551);
  128. mp_int *nonsquare_mod_p = mp_from_integer(3);
  129. initialise_wcurve(&curve, p, a, b, nonsquare_mod_p, G_x, G_y, G_order);
  130. mp_free(p);
  131. mp_free(a);
  132. mp_free(b);
  133. mp_free(G_x);
  134. mp_free(G_y);
  135. mp_free(G_order);
  136. mp_free(nonsquare_mod_p);
  137. curve.textname = curve.name = "nistp256";
  138. /* Now initialised, no need to do it again */
  139. initialised = true;
  140. }
  141. return &curve;
  142. }
  143. static struct ec_curve *ec_p384(void)
  144. {
  145. static struct ec_curve curve = { 0 };
  146. static bool initialised = false;
  147. #ifdef MPEXT
  148. if (ec_curve_cleanup)
  149. {
  150. if (initialised) finalize_wcurve(&curve);
  151. initialised = 0;
  152. return NULL;
  153. }
  154. #endif
  155. if (!initialised)
  156. {
  157. mp_int *p = MP_LITERAL(0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff);
  158. mp_int *a = MP_LITERAL(0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc);
  159. mp_int *b = MP_LITERAL(0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef);
  160. mp_int *G_x = MP_LITERAL(0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7);
  161. mp_int *G_y = MP_LITERAL(0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f);
  162. mp_int *G_order = MP_LITERAL(0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973);
  163. mp_int *nonsquare_mod_p = mp_from_integer(19);
  164. initialise_wcurve(&curve, p, a, b, nonsquare_mod_p, G_x, G_y, G_order);
  165. mp_free(p);
  166. mp_free(a);
  167. mp_free(b);
  168. mp_free(G_x);
  169. mp_free(G_y);
  170. mp_free(G_order);
  171. mp_free(nonsquare_mod_p);
  172. curve.textname = curve.name = "nistp384";
  173. /* Now initialised, no need to do it again */
  174. initialised = true;
  175. }
  176. return &curve;
  177. }
  178. static struct ec_curve *ec_p521(void)
  179. {
  180. static struct ec_curve curve = { 0 };
  181. static bool initialised = false;
  182. #ifdef MPEXT
  183. if (ec_curve_cleanup)
  184. {
  185. if (initialised) finalize_wcurve(&curve);
  186. initialised = 0;
  187. return NULL;
  188. }
  189. #endif
  190. if (!initialised)
  191. {
  192. mp_int *p = MP_LITERAL(0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
  193. mp_int *a = MP_LITERAL(0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc);
  194. mp_int *b = MP_LITERAL(0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00);
  195. mp_int *G_x = MP_LITERAL(0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66);
  196. mp_int *G_y = MP_LITERAL(0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650);
  197. mp_int *G_order = MP_LITERAL(0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409);
  198. mp_int *nonsquare_mod_p = mp_from_integer(3);
  199. initialise_wcurve(&curve, p, a, b, nonsquare_mod_p, G_x, G_y, G_order);
  200. mp_free(p);
  201. mp_free(a);
  202. mp_free(b);
  203. mp_free(G_x);
  204. mp_free(G_y);
  205. mp_free(G_order);
  206. mp_free(nonsquare_mod_p);
  207. curve.textname = curve.name = "nistp521";
  208. /* Now initialised, no need to do it again */
  209. initialised = true;
  210. }
  211. return &curve;
  212. }
  213. static struct ec_curve *ec_curve25519(void)
  214. {
  215. static struct ec_curve curve = { 0 };
  216. static bool initialised = false;
  217. #ifdef MPEXT
  218. if (ec_curve_cleanup)
  219. {
  220. if (initialised) finalize_mcurve(&curve);
  221. initialised = 0;
  222. return NULL;
  223. }
  224. #endif
  225. if (!initialised)
  226. {
  227. mp_int *p = MP_LITERAL(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed);
  228. mp_int *a = MP_LITERAL(0x0000000000000000000000000000000000000000000000000000000000076d06);
  229. mp_int *b = MP_LITERAL(0x0000000000000000000000000000000000000000000000000000000000000001);
  230. mp_int *G_x = MP_LITERAL(0x0000000000000000000000000000000000000000000000000000000000000009);
  231. initialise_mcurve(&curve, p, a, b, G_x, 3);
  232. mp_free(p);
  233. mp_free(a);
  234. mp_free(b);
  235. mp_free(G_x);
  236. /* This curve doesn't need a name, because it's never used in
  237. * any format that embeds the curve name */
  238. curve.name = NULL;
  239. curve.textname = "Curve25519";
  240. /* Now initialised, no need to do it again */
  241. initialised = true;
  242. }
  243. return &curve;
  244. }
  245. static struct ec_curve *ec_curve448(void)
  246. {
  247. static struct ec_curve curve = { 0 };
  248. static bool initialised = false;
  249. if (!initialised)
  250. {
  251. mp_int *p = MP_LITERAL(0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
  252. mp_int *a = MP_LITERAL(0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000262a6);
  253. mp_int *b = MP_LITERAL(0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
  254. mp_int *G_x = MP_LITERAL(0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005);
  255. initialise_mcurve(&curve, p, a, b, G_x, 2);
  256. mp_free(p);
  257. mp_free(a);
  258. mp_free(b);
  259. mp_free(G_x);
  260. /* This curve doesn't need a name, because it's never used in
  261. * any format that embeds the curve name */
  262. curve.name = NULL;
  263. curve.textname = "Curve448";
  264. /* Now initialised, no need to do it again */
  265. initialised = true;
  266. }
  267. return &curve;
  268. }
  269. static struct ec_curve *ec_ed25519(void)
  270. {
  271. static struct ec_curve curve = { 0 };
  272. static bool initialised = false;
  273. #ifdef MPEXT
  274. if (ec_curve_cleanup)
  275. {
  276. if (initialised) finalize_ecurve(&curve);
  277. initialised = 0;
  278. return NULL;
  279. }
  280. #endif
  281. if (!initialised)
  282. {
  283. mp_int *p = MP_LITERAL(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed);
  284. mp_int *d = MP_LITERAL(0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3);
  285. mp_int *a = MP_LITERAL(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec); /* == p-1 */
  286. mp_int *G_x = MP_LITERAL(0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a);
  287. mp_int *G_y = MP_LITERAL(0x6666666666666666666666666666666666666666666666666666666666666658);
  288. mp_int *G_order = MP_LITERAL(0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed);
  289. mp_int *nonsquare_mod_p = mp_from_integer(2);
  290. initialise_ecurve(&curve, p, d, a, nonsquare_mod_p,
  291. G_x, G_y, G_order, 3);
  292. mp_free(p);
  293. mp_free(d);
  294. mp_free(a);
  295. mp_free(G_x);
  296. mp_free(G_y);
  297. mp_free(G_order);
  298. mp_free(nonsquare_mod_p);
  299. /* This curve doesn't need a name, because it's never used in
  300. * any format that embeds the curve name */
  301. curve.name = NULL;
  302. curve.textname = "Ed25519";
  303. /* Now initialised, no need to do it again */
  304. initialised = true;
  305. }
  306. return &curve;
  307. }
  308. static struct ec_curve *ec_ed448(void)
  309. {
  310. static struct ec_curve curve = { 0 };
  311. static bool initialised = false;
  312. if (!initialised)
  313. {
  314. mp_int *p = MP_LITERAL(0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
  315. mp_int *d = MP_LITERAL(0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffff6756); /* = p - 39081 */
  316. mp_int *a = MP_LITERAL(0x1);
  317. mp_int *G_x = MP_LITERAL(0x4f1970c66bed0ded221d15a622bf36da9e146570470f1767ea6de324a3d3a46412ae1af72ab66511433b80e18b00938e2626a82bc70cc05e);
  318. mp_int *G_y = MP_LITERAL(0x693f46716eb6bc248876203756c9c7624bea73736ca3984087789c1e05a0c2d73ad3ff1ce67c39c4fdbd132c4ed7c8ad9808795bf230fa14);
  319. mp_int *G_order = MP_LITERAL(0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3);
  320. mp_int *nonsquare_mod_p = mp_from_integer(7);
  321. initialise_ecurve(&curve, p, d, a, nonsquare_mod_p,
  322. G_x, G_y, G_order, 2);
  323. mp_free(p);
  324. mp_free(d);
  325. mp_free(a);
  326. mp_free(G_x);
  327. mp_free(G_y);
  328. mp_free(G_order);
  329. mp_free(nonsquare_mod_p);
  330. /* This curve doesn't need a name, because it's never used in
  331. * any format that embeds the curve name */
  332. curve.name = NULL;
  333. curve.textname = "Ed448";
  334. /* Now initialised, no need to do it again */
  335. initialised = true;
  336. }
  337. return &curve;
  338. }
  339. /* ----------------------------------------------------------------------
  340. * Public point from private
  341. */
  342. struct ecsign_extra {
  343. struct ec_curve *(*curve)(void);
  344. const ssh_hashalg *hash;
  345. /* These fields are used by the OpenSSH PEM format importer/exporter */
  346. const unsigned char *oid;
  347. int oidlen;
  348. /* Some EdDSA instances prefix a string to all hash preimages, to
  349. * disambiguate which signature variant they're being used with */
  350. ptrlen hash_prefix;
  351. };
  352. WeierstrassPoint *ecdsa_public(mp_int *private_key, const ssh_keyalg *alg)
  353. {
  354. const struct ecsign_extra *extra =
  355. (const struct ecsign_extra *)alg->extra;
  356. struct ec_curve *curve = extra->curve();
  357. pinitassert(curve->type == EC_WEIERSTRASS);
  358. mp_int *priv_reduced = mp_mod(private_key, curve->p);
  359. WeierstrassPoint *toret = ecc_weierstrass_multiply(
  360. curve->w.G, priv_reduced);
  361. mp_free(priv_reduced);
  362. return toret;
  363. }
  364. static mp_int *eddsa_exponent_from_hash(
  365. ptrlen hash, const struct ec_curve *curve)
  366. {
  367. /*
  368. * Make an integer out of the hash data, little-endian.
  369. */
  370. pinitassert(hash.len >= curve->fieldBytes);
  371. mp_int *e = mp_from_bytes_le(make_ptrlen(hash.ptr, curve->fieldBytes));
  372. /*
  373. * Set the highest bit that fits in the modulus, and clear any
  374. * above that.
  375. */
  376. mp_set_bit(e, curve->fieldBits - 1, 1);
  377. mp_reduce_mod_2to(e, curve->fieldBits);
  378. /*
  379. * Clear a curve-specific number of low bits.
  380. */
  381. { // WINSCP
  382. unsigned int bit; // WINSCP
  383. for (bit = 0; bit < curve->e.log2_cofactor; bit++)
  384. mp_set_bit(e, bit, 0);
  385. } // WINSCP
  386. return e;
  387. }
  388. EdwardsPoint *eddsa_public(mp_int *private_key, const ssh_keyalg *alg)
  389. {
  390. const struct ecsign_extra *extra =
  391. (const struct ecsign_extra *)alg->extra;
  392. struct ec_curve *curve = extra->curve();
  393. pinitassert(curve->type == EC_EDWARDS);
  394. ssh_hash *h = ssh_hash_new(extra->hash);
  395. size_t i; // WINSCP
  396. for (i = 0; i < curve->fieldBytes; ++i)
  397. put_byte(h, mp_get_byte(private_key, i));
  398. { // WINSCP
  399. unsigned char hash[MAX_HASH_LEN];
  400. ssh_hash_final(h, hash);
  401. { // WINSCP
  402. mp_int *exponent = eddsa_exponent_from_hash(
  403. make_ptrlen(hash, extra->hash->hlen), curve);
  404. EdwardsPoint *toret = ecc_edwards_multiply(curve->e.G, exponent);
  405. mp_free(exponent);
  406. return toret;
  407. } // WINSCP
  408. } // WINSCP
  409. }
  410. /* ----------------------------------------------------------------------
  411. * Marshalling and unmarshalling functions
  412. */
  413. static mp_int *BinarySource_get_mp_le(BinarySource *src)
  414. {
  415. return mp_from_bytes_le(get_string(src));
  416. }
  417. #define get_mp_le(src) BinarySource_get_mp_le(BinarySource_UPCAST(src))
  418. static void BinarySink_put_mp_le_fixedlen(BinarySink *bs, mp_int *x,
  419. size_t bytes)
  420. {
  421. put_uint32(bs, bytes);
  422. { // WINSCP
  423. size_t i; // WINSCP
  424. for (i = 0; i < bytes; ++i)
  425. put_byte(bs, mp_get_byte(x, i));
  426. } // WINSCP
  427. }
  428. #define put_mp_le_fixedlen(bs, x, bytes) \
  429. BinarySink_put_mp_le_fixedlen(BinarySink_UPCAST(bs), x, bytes)
  430. static WeierstrassPoint *ecdsa_decode(
  431. ptrlen encoded, const struct ec_curve *curve)
  432. {
  433. pinitassert(curve->type == EC_WEIERSTRASS);
  434. BinarySource src[1];
  435. BinarySource_BARE_INIT_PL(src, encoded);
  436. { // WINSCP
  437. unsigned char format_type = get_byte(src);
  438. WeierstrassPoint *P;
  439. size_t len = get_avail(src);
  440. mp_int *x;
  441. mp_int *y;
  442. switch (format_type) {
  443. case 0:
  444. /* The identity. */
  445. P = ecc_weierstrass_point_new_identity(curve->w.wc);
  446. break;
  447. case 2:
  448. case 3:
  449. /* A compressed point, in which the x-coordinate is stored in
  450. * full, and y is deduced from that and a single bit
  451. * indicating its parity (stored in the format type byte). */
  452. x = mp_from_bytes_be(get_data(src, len));
  453. P = ecc_weierstrass_point_new_from_x(curve->w.wc, x, format_type & 1);
  454. mp_free(x);
  455. if (!P) /* this can fail if the input is invalid */
  456. return NULL;
  457. break;
  458. case 4:
  459. /* An uncompressed point: the x,y coordinates are stored in
  460. * full. We expect the rest of the string to have even length,
  461. * and be divided half and half between the two values. */
  462. if (len % 2 != 0)
  463. return NULL;
  464. len /= 2;
  465. x = mp_from_bytes_be(get_data(src, len));
  466. y = mp_from_bytes_be(get_data(src, len));
  467. P = ecc_weierstrass_point_new(curve->w.wc, x, y);
  468. mp_free(x);
  469. mp_free(y);
  470. break;
  471. default:
  472. /* An unrecognised type byte. */
  473. return NULL;
  474. }
  475. /* Verify the point is on the curve */
  476. if (!ecc_weierstrass_point_valid(P)) {
  477. ecc_weierstrass_point_free(P);
  478. return NULL;
  479. }
  480. return P;
  481. } // WINSCP
  482. }
  483. static WeierstrassPoint *BinarySource_get_wpoint(
  484. BinarySource *src, const struct ec_curve *curve)
  485. {
  486. ptrlen str = get_string(src);
  487. if (get_err(src))
  488. return NULL;
  489. return ecdsa_decode(str, curve);
  490. }
  491. #define get_wpoint(src, curve) \
  492. BinarySource_get_wpoint(BinarySource_UPCAST(src), curve)
  493. static void BinarySink_put_wpoint(
  494. BinarySink *bs, WeierstrassPoint *point, const struct ec_curve *curve,
  495. bool bare)
  496. {
  497. strbuf *sb;
  498. BinarySink *bs_inner;
  499. if (!bare) {
  500. /*
  501. * Encapsulate the raw data inside an outermost string layer.
  502. */
  503. sb = strbuf_new();
  504. bs_inner = BinarySink_UPCAST(sb);
  505. } else {
  506. /*
  507. * Just write the data directly to the output.
  508. */
  509. bs_inner = bs;
  510. }
  511. if (ecc_weierstrass_is_identity(point)) {
  512. put_byte(bs_inner, 0);
  513. } else {
  514. mp_int *x, *y;
  515. ecc_weierstrass_get_affine(point, &x, &y);
  516. /*
  517. * For ECDSA, we only ever output uncompressed points.
  518. */
  519. put_byte(bs_inner, 0x04);
  520. { // WINSCP
  521. size_t i; // WINSCP
  522. for (i = curve->fieldBytes; i--;)
  523. put_byte(bs_inner, mp_get_byte(x, i));
  524. for (i = curve->fieldBytes; i--;)
  525. put_byte(bs_inner, mp_get_byte(y, i));
  526. } // WINSCP
  527. mp_free(x);
  528. mp_free(y);
  529. }
  530. if (!bare)
  531. put_stringsb(bs, sb);
  532. }
  533. #define put_wpoint(bs, point, curve, bare) \
  534. BinarySink_put_wpoint(BinarySink_UPCAST(bs), point, curve, bare)
  535. static EdwardsPoint *eddsa_decode(ptrlen encoded, const struct ec_curve *curve)
  536. {
  537. assert(curve->type == EC_EDWARDS);
  538. { // WINSCP
  539. mp_int *y = mp_from_bytes_le(encoded);
  540. /* The topmost bit of the encoding isn't part of y, so it stores
  541. * the bottom bit of x. Extract it, and zero that bit in y. */
  542. unsigned desired_x_parity = mp_get_bit(y, curve->fieldBytes * 8 - 1);
  543. mp_set_bit(y, curve->fieldBytes * 8 - 1, 0);
  544. /* What's left should now be within the range of the curve's modulus */
  545. if (mp_cmp_hs(y, curve->p)) {
  546. mp_free(y);
  547. return NULL;
  548. }
  549. { // WINSCP
  550. { // WINSCP
  551. EdwardsPoint *P = ecc_edwards_point_new_from_y(
  552. curve->e.ec, y, desired_x_parity);
  553. mp_free(y);
  554. /* A point constructed in this way will always satisfy the curve
  555. * equation, unless ecc.c wasn't able to construct one at all, in
  556. * which case P is now NULL. Either way, return it. */
  557. return P;
  558. } // WINSCP
  559. } // WINSCP
  560. } // WINSCP
  561. }
  562. static EdwardsPoint *BinarySource_get_epoint(
  563. BinarySource *src, const struct ec_curve *curve)
  564. {
  565. ptrlen str = get_string(src);
  566. if (get_err(src))
  567. return NULL;
  568. return eddsa_decode(str, curve);
  569. }
  570. #define get_epoint(src, curve) \
  571. BinarySource_get_epoint(BinarySource_UPCAST(src), curve)
  572. static void BinarySink_put_epoint(
  573. BinarySink *bs, EdwardsPoint *point, const struct ec_curve *curve,
  574. bool bare)
  575. {
  576. mp_int *x, *y;
  577. ecc_edwards_get_affine(point, &x, &y);
  578. assert(curve->fieldBytes >= 2);
  579. /*
  580. * EdDSA requires point compression. We store a single integer,
  581. * with bytes in little-endian order, which mostly contains y but
  582. * in which the topmost bit is the low bit of x.
  583. */
  584. if (!bare)
  585. put_uint32(bs, curve->fieldBytes); /* string length field */
  586. { // WINSCP
  587. size_t i; // WINSCP
  588. for (i = 0; i < curve->fieldBytes - 1; i++)
  589. put_byte(bs, mp_get_byte(y, i));
  590. } // WINSCP
  591. put_byte(bs, (mp_get_byte(y, curve->fieldBytes - 1) & 0x7F) |
  592. (mp_get_bit(x, 0) << 7));
  593. mp_free(x);
  594. mp_free(y);
  595. }
  596. #define put_epoint(bs, point, curve, bare) \
  597. BinarySink_put_epoint(BinarySink_UPCAST(bs), point, curve, bare)
  598. /* ----------------------------------------------------------------------
  599. * Exposed ECDSA interface
  600. */
  601. static void ecdsa_freekey(ssh_key *key)
  602. {
  603. struct ecdsa_key *ek = container_of(key, struct ecdsa_key, sshk);
  604. if (ek->publicKey)
  605. ecc_weierstrass_point_free(ek->publicKey);
  606. if (ek->privateKey)
  607. mp_free(ek->privateKey);
  608. sfree(ek);
  609. }
  610. static void eddsa_freekey(ssh_key *key)
  611. {
  612. struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk);
  613. if (ek->publicKey)
  614. ecc_edwards_point_free(ek->publicKey);
  615. if (ek->privateKey)
  616. mp_free(ek->privateKey);
  617. sfree(ek);
  618. }
  619. static char *ec_signkey_invalid(ssh_key *key, unsigned flags)
  620. {
  621. /* All validity criteria for both ECDSA and EdDSA were checked
  622. * when we loaded the key in the first place */
  623. return NULL;
  624. }
  625. static ssh_key *ecdsa_new_pub(const ssh_keyalg *alg, ptrlen data)
  626. {
  627. const struct ecsign_extra *extra =
  628. (const struct ecsign_extra *)alg->extra;
  629. struct ec_curve *curve = extra->curve();
  630. pinitassert(curve->type == EC_WEIERSTRASS);
  631. BinarySource src[1];
  632. BinarySource_BARE_INIT_PL(src, data);
  633. get_string(src);
  634. /* Curve name is duplicated for Weierstrass form */
  635. if (!ptrlen_eq_string(get_string(src), curve->name))
  636. return NULL;
  637. { // WINSCP
  638. struct ecdsa_key *ek = snew(struct ecdsa_key);
  639. ek->sshk.vt = alg;
  640. ek->curve = curve;
  641. ek->privateKey = NULL;
  642. ek->publicKey = get_wpoint(src, curve);
  643. if (!ek->publicKey) {
  644. ecdsa_freekey(&ek->sshk);
  645. return NULL;
  646. }
  647. return &ek->sshk;
  648. } // WINSCP
  649. }
  650. static ssh_key *eddsa_new_pub(const ssh_keyalg *alg, ptrlen data)
  651. {
  652. const struct ecsign_extra *extra =
  653. (const struct ecsign_extra *)alg->extra;
  654. struct ec_curve *curve = extra->curve();
  655. pinitassert(curve->type == EC_EDWARDS);
  656. BinarySource src[1];
  657. BinarySource_BARE_INIT_PL(src, data);
  658. get_string(src);
  659. { // WINSCP
  660. struct eddsa_key *ek = snew(struct eddsa_key);
  661. ek->sshk.vt = alg;
  662. ek->curve = curve;
  663. ek->privateKey = NULL;
  664. ek->publicKey = get_epoint(src, curve);
  665. if (!ek->publicKey) {
  666. eddsa_freekey(&ek->sshk);
  667. return NULL;
  668. }
  669. return &ek->sshk;
  670. } // WINSCP
  671. }
  672. static char *ecc_cache_str_shared(
  673. const char *curve_name, mp_int *x, mp_int *y)
  674. {
  675. strbuf *sb = strbuf_new();
  676. if (curve_name)
  677. strbuf_catf(sb, "%s,", curve_name);
  678. { // WINSCP
  679. char *hx = mp_get_hex(x);
  680. char *hy = mp_get_hex(y);
  681. strbuf_catf(sb, "0x%s,0x%s", hx, hy);
  682. sfree(hx);
  683. sfree(hy);
  684. } // WINSCP
  685. return strbuf_to_str(sb);
  686. }
  687. static char *ecdsa_cache_str(ssh_key *key)
  688. {
  689. struct ecdsa_key *ek = container_of(key, struct ecdsa_key, sshk);
  690. mp_int *x, *y;
  691. ecc_weierstrass_get_affine(ek->publicKey, &x, &y);
  692. { // WINSCP
  693. char *toret = ecc_cache_str_shared(ek->curve->name, x, y);
  694. mp_free(x);
  695. mp_free(y);
  696. return toret;
  697. } // WINSCP
  698. }
  699. static key_components *ecdsa_components(ssh_key *key)
  700. {
  701. struct ecdsa_key *ek = container_of(key, struct ecdsa_key, sshk);
  702. key_components *kc = key_components_new();
  703. key_components_add_text(kc, "key_type", "ECDSA");
  704. key_components_add_text(kc, "curve_name", ek->curve->textname);
  705. { // WINSCP
  706. mp_int *x, *y;
  707. ecc_weierstrass_get_affine(ek->publicKey, &x, &y);
  708. key_components_add_mp(kc, "public_affine_x", x);
  709. key_components_add_mp(kc, "public_affine_y", y);
  710. mp_free(x);
  711. mp_free(y);
  712. if (ek->privateKey)
  713. key_components_add_mp(kc, "private_exponent", ek->privateKey);
  714. return kc;
  715. } // WINSCP
  716. }
  717. static char *eddsa_cache_str(ssh_key *key)
  718. {
  719. struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk);
  720. mp_int *x, *y;
  721. ecc_edwards_get_affine(ek->publicKey, &x, &y);
  722. { // WINSCP
  723. char *toret = ecc_cache_str_shared(ek->curve->name, x, y);
  724. mp_free(x);
  725. mp_free(y);
  726. return toret;
  727. } // WINSCP
  728. }
  729. static key_components *eddsa_components(ssh_key *key)
  730. {
  731. struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk);
  732. key_components *kc = key_components_new();
  733. key_components_add_text(kc, "key_type", "EdDSA");
  734. key_components_add_text(kc, "curve_name", ek->curve->textname);
  735. { // WINSCP
  736. mp_int *x, *y;
  737. ecc_edwards_get_affine(ek->publicKey, &x, &y);
  738. key_components_add_mp(kc, "public_affine_x", x);
  739. key_components_add_mp(kc, "public_affine_y", y);
  740. mp_free(x);
  741. mp_free(y);
  742. if (ek->privateKey)
  743. key_components_add_mp(kc, "private_exponent", ek->privateKey);
  744. return kc;
  745. } // WINSCP
  746. }
  747. static void ecdsa_public_blob(ssh_key *key, BinarySink *bs)
  748. {
  749. struct ecdsa_key *ek = container_of(key, struct ecdsa_key, sshk);
  750. put_stringz(bs, ek->sshk.vt->ssh_id);
  751. put_stringz(bs, ek->curve->name);
  752. put_wpoint(bs, ek->publicKey, ek->curve, false);
  753. }
  754. static void eddsa_public_blob(ssh_key *key, BinarySink *bs)
  755. {
  756. struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk);
  757. put_stringz(bs, ek->sshk.vt->ssh_id);
  758. put_epoint(bs, ek->publicKey, ek->curve, false);
  759. }
  760. static void ecdsa_private_blob(ssh_key *key, BinarySink *bs)
  761. {
  762. struct ecdsa_key *ek = container_of(key, struct ecdsa_key, sshk);
  763. /* ECDSA uses ordinary SSH-2 mpint format to store the private key */
  764. assert(ek->privateKey);
  765. put_mp_ssh2(bs, ek->privateKey);
  766. }
  767. static void eddsa_private_blob(ssh_key *key, BinarySink *bs)
  768. {
  769. struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk);
  770. /* EdDSA stores the private key integer little-endian and unsigned */
  771. assert(ek->privateKey);
  772. put_mp_le_fixedlen(bs, ek->privateKey, ek->curve->fieldBytes);
  773. }
  774. static ssh_key *ecdsa_new_priv(const ssh_keyalg *alg, ptrlen pub, ptrlen priv)
  775. {
  776. ssh_key *sshk = ecdsa_new_pub(alg, pub);
  777. if (!sshk)
  778. return NULL;
  779. { // WINSCP
  780. struct ecdsa_key *ek = container_of(sshk, struct ecdsa_key, sshk);
  781. BinarySource src[1];
  782. BinarySource_BARE_INIT_PL(src, priv);
  783. ek->privateKey = get_mp_ssh2(src);
  784. return &ek->sshk;
  785. } // WINSCP
  786. }
  787. static ssh_key *eddsa_new_priv(const ssh_keyalg *alg, ptrlen pub, ptrlen priv)
  788. {
  789. ssh_key *sshk = eddsa_new_pub(alg, pub);
  790. if (!sshk)
  791. return NULL;
  792. { // WINSCP
  793. struct eddsa_key *ek = container_of(sshk, struct eddsa_key, sshk);
  794. BinarySource src[1];
  795. BinarySource_BARE_INIT_PL(src, priv);
  796. ek->privateKey = get_mp_le(src);
  797. return &ek->sshk;
  798. } // WINSCP
  799. }
  800. static ssh_key *eddsa_new_priv_openssh(
  801. const ssh_keyalg *alg, BinarySource *src)
  802. {
  803. const struct ecsign_extra *extra =
  804. (const struct ecsign_extra *)alg->extra;
  805. struct ec_curve *curve = extra->curve();
  806. assert(curve->type == EC_EDWARDS);
  807. { // WINSCP
  808. ptrlen pubkey_pl = get_string(src);
  809. ptrlen privkey_extended_pl = get_string(src);
  810. if (get_err(src) || pubkey_pl.len != curve->fieldBytes)
  811. return NULL;
  812. /*
  813. * The OpenSSH format for ed25519 private keys also for some
  814. * reason encodes an extra copy of the public key in the second
  815. * half of the secret-key string. Check that that's present and
  816. * correct as well, otherwise the key we think we've imported
  817. * won't behave identically to the way OpenSSH would have treated
  818. * it.
  819. *
  820. * We assume that Ed448 will work the same way, as and when
  821. * OpenSSH implements it, which at the time of writing this they
  822. * had not.
  823. */
  824. { // WINSCP
  825. BinarySource subsrc[1];
  826. BinarySource_BARE_INIT_PL(subsrc, privkey_extended_pl);
  827. { // WINSCP
  828. ptrlen privkey_pl = get_data(subsrc, curve->fieldBytes);
  829. ptrlen pubkey_copy_pl = get_data(subsrc, curve->fieldBytes);
  830. if (get_err(subsrc) || get_avail(subsrc))
  831. return NULL;
  832. if (!ptrlen_eq_ptrlen(pubkey_pl, pubkey_copy_pl))
  833. return NULL;
  834. { // WINSCP
  835. struct eddsa_key *ek = snew(struct eddsa_key);
  836. ek->sshk.vt = alg;
  837. ek->curve = curve;
  838. ek->privateKey = NULL;
  839. ek->publicKey = eddsa_decode(pubkey_pl, curve);
  840. if (!ek->publicKey) {
  841. eddsa_freekey(&ek->sshk);
  842. return NULL;
  843. }
  844. ek->privateKey = mp_from_bytes_le(privkey_pl);
  845. return &ek->sshk;
  846. } // WINSCP
  847. } // WINSCP
  848. } // WINSCP
  849. } // WINSCP
  850. }
  851. static void eddsa_openssh_blob(ssh_key *key, BinarySink *bs)
  852. {
  853. struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk);
  854. assert(ek->curve->type == EC_EDWARDS);
  855. /* Encode the public and private points as strings */
  856. { // WINSCP
  857. strbuf *pub_sb = strbuf_new();
  858. put_epoint(pub_sb, ek->publicKey, ek->curve, false);
  859. { // WINSCP
  860. ptrlen pub = make_ptrlen(pub_sb->s + 4, pub_sb->len - 4);
  861. strbuf *priv_sb = strbuf_new_nm();
  862. put_mp_le_fixedlen(priv_sb, ek->privateKey, ek->curve->fieldBytes);
  863. { // WINSCP
  864. ptrlen priv = make_ptrlen(priv_sb->s + 4, priv_sb->len - 4);
  865. put_stringpl(bs, pub);
  866. /* Encode the private key as the concatenation of the
  867. * little-endian key integer and the public key again */
  868. put_uint32(bs, priv.len + pub.len);
  869. put_datapl(bs, priv);
  870. put_datapl(bs, pub);
  871. strbuf_free(pub_sb);
  872. strbuf_free(priv_sb);
  873. } // WINSCP
  874. } // WINSCP
  875. } // WINSCP
  876. }
  877. static ssh_key *ecdsa_new_priv_openssh(
  878. const ssh_keyalg *alg, BinarySource *src)
  879. {
  880. const struct ecsign_extra *extra =
  881. (const struct ecsign_extra *)alg->extra;
  882. struct ec_curve *curve = extra->curve();
  883. assert(curve->type == EC_WEIERSTRASS);
  884. get_string(src);
  885. { // WINSCP
  886. struct ecdsa_key *ek = snew(struct ecdsa_key);
  887. ek->sshk.vt = alg;
  888. ek->curve = curve;
  889. ek->privateKey = NULL;
  890. ek->publicKey = get_wpoint(src, curve);
  891. if (!ek->publicKey) {
  892. ecdsa_freekey(&ek->sshk);
  893. return NULL;
  894. }
  895. ek->privateKey = get_mp_ssh2(src);
  896. return &ek->sshk;
  897. } // WINSCP
  898. }
  899. static void ecdsa_openssh_blob(ssh_key *key, BinarySink *bs)
  900. {
  901. struct ecdsa_key *ek = container_of(key, struct ecdsa_key, sshk);
  902. put_stringz(bs, ek->curve->name);
  903. put_wpoint(bs, ek->publicKey, ek->curve, false);
  904. put_mp_ssh2(bs, ek->privateKey);
  905. }
  906. static int ec_shared_pubkey_bits(const ssh_keyalg *alg, ptrlen blob)
  907. {
  908. const struct ecsign_extra *extra =
  909. (const struct ecsign_extra *)alg->extra;
  910. struct ec_curve *curve = extra->curve();
  911. return curve->fieldBits;
  912. }
  913. static mp_int *ecdsa_signing_exponent_from_data(
  914. const struct ec_curve *curve, const struct ecsign_extra *extra,
  915. ptrlen data)
  916. {
  917. /* Hash the data being signed. */
  918. unsigned char hash[MAX_HASH_LEN];
  919. ssh_hash *h = ssh_hash_new(extra->hash);
  920. put_datapl(h, data);
  921. ssh_hash_final(h, hash);
  922. /*
  923. * Take the leftmost b bits of the hash of the signed data (where
  924. * b is the number of bits in order(G)), interpreted big-endian.
  925. */
  926. { // WINSCP
  927. mp_int *z = mp_from_bytes_be(make_ptrlen(hash, extra->hash->hlen));
  928. size_t zbits = mp_get_nbits(z);
  929. size_t nbits = mp_get_nbits(curve->w.G_order);
  930. size_t shift = zbits - nbits;
  931. /* Bound the shift count below at 0, using bit twiddling to avoid
  932. * a conditional branch */
  933. shift &= ~-(int)(shift >> (CHAR_BIT * sizeof(size_t) - 1)); // WINSCP
  934. { // WINSCP
  935. mp_int *toret = mp_rshift_safe(z, shift);
  936. mp_free(z);
  937. return toret;
  938. } // WINSCP
  939. } // WINSCP
  940. }
  941. static bool ecdsa_verify(ssh_key *key, ptrlen sig, ptrlen data)
  942. {
  943. struct ecdsa_key *ek = container_of(key, struct ecdsa_key, sshk);
  944. const struct ecsign_extra *extra =
  945. (const struct ecsign_extra *)ek->sshk.vt->extra;
  946. BinarySource src[1];
  947. BinarySource_BARE_INIT_PL(src, sig);
  948. /* Check the signature starts with the algorithm name */
  949. if (!ptrlen_eq_string(get_string(src), ek->sshk.vt->ssh_id))
  950. return false;
  951. /* Everything else is nested inside a sub-string. Descend into that. */
  952. { // WINSCP
  953. ptrlen sigstr = get_string(src);
  954. if (get_err(src))
  955. return false;
  956. BinarySource_BARE_INIT_PL(src, sigstr);
  957. /* Extract the signature integers r,s */
  958. { // WINSCP
  959. mp_int *r = get_mp_ssh2(src);
  960. mp_int *s = get_mp_ssh2(src);
  961. if (get_err(src)) {
  962. mp_free(r);
  963. mp_free(s);
  964. return false;
  965. }
  966. /* Basic sanity checks: 0 < r,s < order(G) */
  967. { // WINSCP
  968. unsigned invalid = 0;
  969. invalid |= mp_eq_integer(r, 0);
  970. invalid |= mp_eq_integer(s, 0);
  971. invalid |= mp_cmp_hs(r, ek->curve->w.G_order);
  972. invalid |= mp_cmp_hs(s, ek->curve->w.G_order);
  973. /* Get the hash of the signed data, converted to an integer */
  974. { // WINSCP
  975. mp_int *z = ecdsa_signing_exponent_from_data(ek->curve, extra, data);
  976. /* Verify the signature integers against the hash */
  977. mp_int *w = mp_invert(s, ek->curve->w.G_order);
  978. mp_int *u1 = mp_modmul(z, w, ek->curve->w.G_order);
  979. mp_free(z);
  980. { // WINSCP
  981. mp_int *u2 = mp_modmul(r, w, ek->curve->w.G_order);
  982. mp_free(w);
  983. { // WINSCP
  984. WeierstrassPoint *u1G = ecc_weierstrass_multiply(ek->curve->w.G, u1);
  985. mp_free(u1);
  986. { // WINSCP
  987. WeierstrassPoint *u2P = ecc_weierstrass_multiply(ek->publicKey, u2);
  988. mp_free(u2);
  989. { // WINSCP
  990. WeierstrassPoint *sum = ecc_weierstrass_add_general(u1G, u2P);
  991. ecc_weierstrass_point_free(u1G);
  992. ecc_weierstrass_point_free(u2P);
  993. { // WINSCP
  994. mp_int *x;
  995. ecc_weierstrass_get_affine(sum, &x, NULL);
  996. ecc_weierstrass_point_free(sum);
  997. mp_divmod_into(x, ek->curve->w.G_order, NULL, x);
  998. invalid |= (1 ^ mp_cmp_eq(r, x));
  999. mp_free(x);
  1000. mp_free(r);
  1001. mp_free(s);
  1002. return !invalid;
  1003. } // WINSCP
  1004. } // WINSCP
  1005. } // WINSCP
  1006. } // WINSCP
  1007. } // WINSCP
  1008. } // WINSCP
  1009. } // WINSCP
  1010. } // WINSCP
  1011. } // WINSCP
  1012. }
  1013. static mp_int *eddsa_signing_exponent_from_data(
  1014. struct eddsa_key *ek, const struct ecsign_extra *extra,
  1015. ptrlen r_encoded, ptrlen data)
  1016. {
  1017. /* Hash (r || public key || message) */
  1018. unsigned char hash[MAX_HASH_LEN];
  1019. ssh_hash *h = ssh_hash_new(extra->hash);
  1020. put_datapl(h, extra->hash_prefix);
  1021. put_datapl(h, r_encoded);
  1022. put_epoint(h, ek->publicKey, ek->curve, true); /* omit string header */
  1023. put_datapl(h, data);
  1024. ssh_hash_final(h, hash);
  1025. /* Convert to an integer */
  1026. { // WINSCP
  1027. mp_int *toret = mp_from_bytes_le(make_ptrlen(hash, extra->hash->hlen));
  1028. smemclr(hash, extra->hash->hlen);
  1029. return toret;
  1030. } // WINSCP
  1031. }
  1032. static bool eddsa_verify(ssh_key *key, ptrlen sig, ptrlen data)
  1033. {
  1034. struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk);
  1035. const struct ecsign_extra *extra =
  1036. (const struct ecsign_extra *)ek->sshk.vt->extra;
  1037. BinarySource src[1];
  1038. BinarySource_BARE_INIT_PL(src, sig);
  1039. /* Check the signature starts with the algorithm name */
  1040. if (!ptrlen_eq_string(get_string(src), ek->sshk.vt->ssh_id))
  1041. return false;
  1042. /* Now expect a single string which is the concatenation of an
  1043. * encoded curve point r and an integer s. */
  1044. { // WINSCP
  1045. ptrlen sigstr = get_string(src);
  1046. if (get_err(src))
  1047. return false;
  1048. BinarySource_BARE_INIT_PL(src, sigstr);
  1049. { // WINSCP
  1050. ptrlen rstr = get_data(src, ek->curve->fieldBytes);
  1051. ptrlen sstr = get_data(src, ek->curve->fieldBytes);
  1052. if (get_err(src) || get_avail(src))
  1053. return false;
  1054. { // WINSCP
  1055. EdwardsPoint *r = eddsa_decode(rstr, ek->curve);
  1056. if (!r)
  1057. return false;
  1058. { // WINSCP
  1059. mp_int *s = mp_from_bytes_le(sstr);
  1060. mp_int *H = eddsa_signing_exponent_from_data(ek, extra, rstr, data);
  1061. /* Verify that s*G == r + H*publicKey */
  1062. EdwardsPoint *lhs = ecc_edwards_multiply(ek->curve->e.G, s);
  1063. mp_free(s);
  1064. { // WINSCP
  1065. EdwardsPoint *hpk = ecc_edwards_multiply(ek->publicKey, H);
  1066. mp_free(H);
  1067. { // WINSCP
  1068. EdwardsPoint *rhs = ecc_edwards_add(r, hpk);
  1069. ecc_edwards_point_free(hpk);
  1070. { // WINSCP
  1071. unsigned valid = ecc_edwards_eq(lhs, rhs);
  1072. ecc_edwards_point_free(lhs);
  1073. ecc_edwards_point_free(rhs);
  1074. ecc_edwards_point_free(r);
  1075. return valid;
  1076. } // WINSCP
  1077. } // WINSCP
  1078. } // WINSCP
  1079. } // WINSCP
  1080. } // WINSCP
  1081. } // WINSCP
  1082. } // WINSCP
  1083. }
  1084. static void ecdsa_sign(ssh_key *key, ptrlen data,
  1085. unsigned flags, BinarySink *bs)
  1086. {
  1087. struct ecdsa_key *ek = container_of(key, struct ecdsa_key, sshk);
  1088. const struct ecsign_extra *extra =
  1089. (const struct ecsign_extra *)ek->sshk.vt->extra;
  1090. assert(ek->privateKey);
  1091. { // WINSCP
  1092. mp_int *z = ecdsa_signing_exponent_from_data(ek->curve, extra, data);
  1093. /* Generate k between 1 and curve->n, using the same deterministic
  1094. * k generation system we use for conventional DSA. */
  1095. mp_int *k;
  1096. {
  1097. unsigned char digest[20];
  1098. hash_simple(&ssh_sha1, data, digest);
  1099. k = dss_gen_k(
  1100. "ECDSA deterministic k generator", ek->curve->w.G_order,
  1101. ek->privateKey, digest, sizeof(digest));
  1102. }
  1103. { // WINSCP
  1104. WeierstrassPoint *kG = ecc_weierstrass_multiply(ek->curve->w.G, k);
  1105. mp_int *x;
  1106. ecc_weierstrass_get_affine(kG, &x, NULL);
  1107. ecc_weierstrass_point_free(kG);
  1108. /* r = kG.x mod order(G) */
  1109. { // WINSCP
  1110. mp_int *r = mp_mod(x, ek->curve->w.G_order);
  1111. mp_free(x);
  1112. /* s = (z + r * priv)/k mod n */
  1113. { // WINSCP
  1114. mp_int *rPriv = mp_modmul(r, ek->privateKey, ek->curve->w.G_order);
  1115. mp_int *numerator = mp_modadd(z, rPriv, ek->curve->w.G_order);
  1116. mp_free(z);
  1117. mp_free(rPriv);
  1118. { // WINSCP
  1119. mp_int *kInv = mp_invert(k, ek->curve->w.G_order);
  1120. mp_free(k);
  1121. { // WINSCP
  1122. mp_int *s = mp_modmul(numerator, kInv, ek->curve->w.G_order);
  1123. mp_free(numerator);
  1124. mp_free(kInv);
  1125. /* Format the output */
  1126. put_stringz(bs, ek->sshk.vt->ssh_id);
  1127. { // WINSCP
  1128. strbuf *substr = strbuf_new();
  1129. put_mp_ssh2(substr, r);
  1130. put_mp_ssh2(substr, s);
  1131. put_stringsb(bs, substr);
  1132. } // WINSCP
  1133. mp_free(r);
  1134. mp_free(s);
  1135. } // WINSCP
  1136. } // WINSCP
  1137. } // WINSCP
  1138. } // WINSCP
  1139. } // WINSCP
  1140. } // WINSCP
  1141. }
  1142. static void eddsa_sign(ssh_key *key, ptrlen data,
  1143. unsigned flags, BinarySink *bs)
  1144. {
  1145. struct eddsa_key *ek = container_of(key, struct eddsa_key, sshk);
  1146. const struct ecsign_extra *extra =
  1147. (const struct ecsign_extra *)ek->sshk.vt->extra;
  1148. assert(ek->privateKey);
  1149. /*
  1150. * EdDSA prescribes a specific method of generating the random
  1151. * nonce integer for the signature. (A verifier can't tell
  1152. * whether you followed that method, but it's important to
  1153. * follow it anyway, because test vectors will want a specific
  1154. * signature for a given message, and because this preserves
  1155. * determinism of signatures even if the same signature were
  1156. * made twice by different software.)
  1157. */
  1158. /*
  1159. * First, we hash the private key integer (bare, little-endian)
  1160. * into a hash generating 2*fieldBytes of output.
  1161. */
  1162. { // WINSCP
  1163. unsigned char hash[MAX_HASH_LEN];
  1164. ssh_hash *h = ssh_hash_new(extra->hash);
  1165. size_t i; // WINSCP
  1166. for (i = 0; i < ek->curve->fieldBytes; ++i)
  1167. put_byte(h, mp_get_byte(ek->privateKey, i));
  1168. ssh_hash_final(h, hash);
  1169. /*
  1170. * The first half of the output hash is converted into an
  1171. * integer a, by the standard EdDSA transformation.
  1172. */
  1173. { // WINSCP
  1174. mp_int *a = eddsa_exponent_from_hash(
  1175. make_ptrlen(hash, ek->curve->fieldBytes), ek->curve);
  1176. /*
  1177. * The second half of the hash of the private key is hashed again
  1178. * with the message to be signed, and used as an exponent to
  1179. * generate the signature point r.
  1180. */
  1181. h = ssh_hash_new(extra->hash);
  1182. put_datapl(h, extra->hash_prefix);
  1183. put_data(h, hash + ek->curve->fieldBytes,
  1184. extra->hash->hlen - ek->curve->fieldBytes);
  1185. put_datapl(h, data);
  1186. ssh_hash_final(h, hash);
  1187. { // WINSCP
  1188. mp_int *log_r_unreduced = mp_from_bytes_le(
  1189. make_ptrlen(hash, extra->hash->hlen));
  1190. mp_int *log_r = mp_mod(log_r_unreduced, ek->curve->e.G_order);
  1191. mp_free(log_r_unreduced);
  1192. { // WINSCP
  1193. EdwardsPoint *r = ecc_edwards_multiply(ek->curve->e.G, log_r);
  1194. /*
  1195. * Encode r now, because we'll need its encoding for the next
  1196. * hashing step as well as to write into the actual signature.
  1197. */
  1198. strbuf *r_enc = strbuf_new();
  1199. put_epoint(r_enc, r, ek->curve, true); /* omit string header */
  1200. ecc_edwards_point_free(r);
  1201. /*
  1202. * Compute the hash of (r || public key || message) just as
  1203. * eddsa_verify does.
  1204. */
  1205. { // WINSCP
  1206. mp_int *H = eddsa_signing_exponent_from_data(
  1207. ek, extra, ptrlen_from_strbuf(r_enc), data);
  1208. /* And then s = (log(r) + H*a) mod order(G). */
  1209. mp_int *Ha = mp_modmul(H, a, ek->curve->e.G_order);
  1210. mp_int *s = mp_modadd(log_r, Ha, ek->curve->e.G_order);
  1211. mp_free(H);
  1212. mp_free(a);
  1213. mp_free(Ha);
  1214. mp_free(log_r);
  1215. /* Format the output */
  1216. put_stringz(bs, ek->sshk.vt->ssh_id);
  1217. put_uint32(bs, r_enc->len + ek->curve->fieldBytes);
  1218. put_data(bs, r_enc->u, r_enc->len);
  1219. strbuf_free(r_enc);
  1220. { // WINSCP
  1221. size_t i;
  1222. for (i = 0; i < ek->curve->fieldBytes; ++i)
  1223. put_byte(bs, mp_get_byte(s, i));
  1224. mp_free(s);
  1225. } // WINSCP
  1226. } // WINSCP
  1227. } // WINSCP
  1228. } // WINSCP
  1229. } // WINSCP
  1230. } // WINSCP
  1231. }
  1232. static const struct ecsign_extra sign_extra_ed25519 = {
  1233. ec_ed25519, &ssh_sha512,
  1234. NULL, 0, PTRLEN_DECL_LITERAL(""),
  1235. };
  1236. const ssh_keyalg ssh_ecdsa_ed25519 = {
  1237. // WINSCP
  1238. /*.new_pub =*/ eddsa_new_pub,
  1239. /*.new_priv =*/ eddsa_new_priv,
  1240. /*.new_priv_openssh =*/ eddsa_new_priv_openssh,
  1241. /*.freekey =*/ eddsa_freekey,
  1242. /*.invalid =*/ ec_signkey_invalid,
  1243. /*.sign =*/ eddsa_sign,
  1244. /*.verify =*/ eddsa_verify,
  1245. /*.public_blob =*/ eddsa_public_blob,
  1246. /*.private_blob =*/ eddsa_private_blob,
  1247. /*.openssh_blob =*/ eddsa_openssh_blob,
  1248. /*.cache_str =*/ eddsa_cache_str,
  1249. /*.components =*/ eddsa_components,
  1250. /*.pubkey_bits =*/ ec_shared_pubkey_bits,
  1251. /*.ssh_id =*/ "ssh-ed25519",
  1252. /*.cache_id =*/ "ssh-ed25519",
  1253. /*.extra =*/ &sign_extra_ed25519,
  1254. 0, // WINSCP
  1255. };
  1256. static const struct ecsign_extra sign_extra_ed448 = {
  1257. ec_ed448, &ssh_shake256_114bytes,
  1258. NULL, 0, PTRLEN_DECL_LITERAL("SigEd448\0\0"),
  1259. };
  1260. const ssh_keyalg ssh_ecdsa_ed448 = {
  1261. // WINSCP
  1262. /*.new_pub =*/ eddsa_new_pub,
  1263. /*.new_priv =*/ eddsa_new_priv,
  1264. /*.new_priv_openssh =*/ eddsa_new_priv_openssh,
  1265. /*.freekey =*/ eddsa_freekey,
  1266. /*.invalid =*/ ec_signkey_invalid,
  1267. /*.sign =*/ eddsa_sign,
  1268. /*.verify =*/ eddsa_verify,
  1269. /*.public_blob =*/ eddsa_public_blob,
  1270. /*.private_blob =*/ eddsa_private_blob,
  1271. /*.openssh_blob =*/ eddsa_openssh_blob,
  1272. /*.cache_str =*/ eddsa_cache_str,
  1273. /*.components =*/ eddsa_components,
  1274. /*.pubkey_bits =*/ ec_shared_pubkey_bits,
  1275. /*.ssh_id =*/ "ssh-ed448",
  1276. /*.cache_id =*/ "ssh-ed448",
  1277. /*.extra =*/ &sign_extra_ed448,
  1278. 0, // WINSCP
  1279. };
  1280. /* OID: 1.2.840.10045.3.1.7 (ansiX9p256r1) */
  1281. static const unsigned char nistp256_oid[] = {
  1282. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  1283. };
  1284. static const struct ecsign_extra sign_extra_nistp256 = {
  1285. ec_p256, &ssh_sha256,
  1286. nistp256_oid, lenof(nistp256_oid),
  1287. };
  1288. const ssh_keyalg ssh_ecdsa_nistp256 = {
  1289. // WINSCP
  1290. /*.new_pub =*/ ecdsa_new_pub,
  1291. /*.new_priv =*/ ecdsa_new_priv,
  1292. /*.new_priv_openssh =*/ ecdsa_new_priv_openssh,
  1293. /*.freekey =*/ ecdsa_freekey,
  1294. /*.invalid =*/ ec_signkey_invalid,
  1295. /*.sign =*/ ecdsa_sign,
  1296. /*.verify =*/ ecdsa_verify,
  1297. /*.public_blob =*/ ecdsa_public_blob,
  1298. /*.private_blob =*/ ecdsa_private_blob,
  1299. /*.openssh_blob =*/ ecdsa_openssh_blob,
  1300. /*.cache_str =*/ ecdsa_cache_str,
  1301. /*.components =*/ ecdsa_components,
  1302. /*.pubkey_bits =*/ ec_shared_pubkey_bits,
  1303. /*.ssh_id =*/ "ecdsa-sha2-nistp256",
  1304. /*.cache_id =*/ "ecdsa-sha2-nistp256",
  1305. /*.extra =*/ &sign_extra_nistp256,
  1306. 0, // WINSCP
  1307. };
  1308. /* OID: 1.3.132.0.34 (secp384r1) */
  1309. static const unsigned char nistp384_oid[] = {
  1310. 0x2b, 0x81, 0x04, 0x00, 0x22
  1311. };
  1312. static const struct ecsign_extra sign_extra_nistp384 = {
  1313. ec_p384, &ssh_sha384,
  1314. nistp384_oid, lenof(nistp384_oid),
  1315. };
  1316. const ssh_keyalg ssh_ecdsa_nistp384 = {
  1317. // WINSCP
  1318. /*.new_pub =*/ ecdsa_new_pub,
  1319. /*.new_priv =*/ ecdsa_new_priv,
  1320. /*.new_priv_openssh =*/ ecdsa_new_priv_openssh,
  1321. /*.freekey =*/ ecdsa_freekey,
  1322. /*.invalid =*/ ec_signkey_invalid,
  1323. /*.sign =*/ ecdsa_sign,
  1324. /*.verify =*/ ecdsa_verify,
  1325. /*.public_blob =*/ ecdsa_public_blob,
  1326. /*.private_blob =*/ ecdsa_private_blob,
  1327. /*.openssh_blob =*/ ecdsa_openssh_blob,
  1328. /*.cache_str =*/ ecdsa_cache_str,
  1329. /*.components =*/ ecdsa_components,
  1330. /*.pubkey_bits =*/ ec_shared_pubkey_bits,
  1331. /*.ssh_id =*/ "ecdsa-sha2-nistp384",
  1332. /*.cache_id =*/ "ecdsa-sha2-nistp384",
  1333. /*.extra =*/ &sign_extra_nistp384,
  1334. 0, // WINSCP
  1335. };
  1336. /* OID: 1.3.132.0.35 (secp521r1) */
  1337. static const unsigned char nistp521_oid[] = {
  1338. 0x2b, 0x81, 0x04, 0x00, 0x23
  1339. };
  1340. static const struct ecsign_extra sign_extra_nistp521 = {
  1341. ec_p521, &ssh_sha512,
  1342. nistp521_oid, lenof(nistp521_oid),
  1343. };
  1344. const ssh_keyalg ssh_ecdsa_nistp521 = {
  1345. // WINSCP
  1346. /*.new_pub =*/ ecdsa_new_pub,
  1347. /*.new_priv =*/ ecdsa_new_priv,
  1348. /*.new_priv_openssh =*/ ecdsa_new_priv_openssh,
  1349. /*.freekey =*/ ecdsa_freekey,
  1350. /*.invalid =*/ ec_signkey_invalid,
  1351. /*.sign =*/ ecdsa_sign,
  1352. /*.verify =*/ ecdsa_verify,
  1353. /*.public_blob =*/ ecdsa_public_blob,
  1354. /*.private_blob =*/ ecdsa_private_blob,
  1355. /*.openssh_blob =*/ ecdsa_openssh_blob,
  1356. /*.cache_str =*/ ecdsa_cache_str,
  1357. /*.components =*/ ecdsa_components,
  1358. /*.pubkey_bits =*/ ec_shared_pubkey_bits,
  1359. /*.ssh_id =*/ "ecdsa-sha2-nistp521",
  1360. /*.cache_id =*/ "ecdsa-sha2-nistp521",
  1361. /*.extra =*/ &sign_extra_nistp521,
  1362. 0, // WINSCP
  1363. };
  1364. /* ----------------------------------------------------------------------
  1365. * Exposed ECDH interface
  1366. */
  1367. struct eckex_extra {
  1368. struct ec_curve *(*curve)(void);
  1369. void (*setup)(ecdh_key *dh);
  1370. void (*cleanup)(ecdh_key *dh);
  1371. void (*getpublic)(ecdh_key *dh, BinarySink *bs);
  1372. mp_int *(*getkey)(ecdh_key *dh, ptrlen remoteKey);
  1373. };
  1374. struct ecdh_key {
  1375. const struct eckex_extra *extra;
  1376. const struct ec_curve *curve;
  1377. mp_int *private;
  1378. union {
  1379. WeierstrassPoint *w_public;
  1380. MontgomeryPoint *m_public;
  1381. };
  1382. };
  1383. const char *ssh_ecdhkex_curve_textname(const ssh_kex *kex)
  1384. {
  1385. const struct eckex_extra *extra = (const struct eckex_extra *)kex->extra;
  1386. struct ec_curve *curve = extra->curve();
  1387. return curve->textname;
  1388. }
  1389. static void ssh_ecdhkex_w_setup(ecdh_key *dh)
  1390. {
  1391. mp_int *one = mp_from_integer(1);
  1392. dh->private = mp_random_in_range(one, dh->curve->w.G_order);
  1393. mp_free(one);
  1394. dh->w_public = ecc_weierstrass_multiply(dh->curve->w.G, dh->private);
  1395. }
  1396. static void ssh_ecdhkex_m_setup(ecdh_key *dh)
  1397. {
  1398. strbuf *bytes = strbuf_new_nm();
  1399. random_read(strbuf_append(bytes, dh->curve->fieldBytes),
  1400. dh->curve->fieldBytes);
  1401. dh->private = mp_from_bytes_le(ptrlen_from_strbuf(bytes));
  1402. /* Ensure the private key has the highest valid bit set, and no
  1403. * bits _above_ the highest valid one */
  1404. mp_reduce_mod_2to(dh->private, dh->curve->fieldBits);
  1405. mp_set_bit(dh->private, dh->curve->fieldBits - 1, 1);
  1406. /* Clear a curve-specific number of low bits */
  1407. { // WINSCP
  1408. unsigned bit;
  1409. for (bit = 0; bit < dh->curve->m.log2_cofactor; bit++)
  1410. mp_set_bit(dh->private, bit, 0);
  1411. } // WINSCP
  1412. strbuf_free(bytes);
  1413. dh->m_public = ecc_montgomery_multiply(dh->curve->m.G, dh->private);
  1414. }
  1415. ecdh_key *ssh_ecdhkex_newkey(const ssh_kex *kex)
  1416. {
  1417. const struct eckex_extra *extra = (const struct eckex_extra *)kex->extra;
  1418. const struct ec_curve *curve = extra->curve();
  1419. ecdh_key *dh = snew(ecdh_key);
  1420. dh->extra = extra;
  1421. dh->curve = curve;
  1422. dh->extra->setup(dh);
  1423. return dh;
  1424. }
  1425. static void ssh_ecdhkex_w_getpublic(ecdh_key *dh, BinarySink *bs)
  1426. {
  1427. put_wpoint(bs, dh->w_public, dh->curve, true);
  1428. }
  1429. static void ssh_ecdhkex_m_getpublic(ecdh_key *dh, BinarySink *bs)
  1430. {
  1431. mp_int *x;
  1432. size_t i; // WINSCP
  1433. ecc_montgomery_get_affine(dh->m_public, &x);
  1434. for (i = 0; i < dh->curve->fieldBytes; ++i)
  1435. put_byte(bs, mp_get_byte(x, i));
  1436. mp_free(x);
  1437. }
  1438. void ssh_ecdhkex_getpublic(ecdh_key *dh, BinarySink *bs)
  1439. {
  1440. dh->extra->getpublic(dh, bs);
  1441. }
  1442. static mp_int *ssh_ecdhkex_w_getkey(ecdh_key *dh, ptrlen remoteKey)
  1443. {
  1444. WeierstrassPoint *remote_p = ecdsa_decode(remoteKey, dh->curve);
  1445. if (!remote_p)
  1446. return NULL;
  1447. if (ecc_weierstrass_is_identity(remote_p)) {
  1448. /* Not a sensible Diffie-Hellman input value */
  1449. ecc_weierstrass_point_free(remote_p);
  1450. return NULL;
  1451. }
  1452. { // WINSCP
  1453. WeierstrassPoint *p = ecc_weierstrass_multiply(remote_p, dh->private);
  1454. mp_int *x;
  1455. ecc_weierstrass_get_affine(p, &x, NULL);
  1456. ecc_weierstrass_point_free(remote_p);
  1457. ecc_weierstrass_point_free(p);
  1458. return x;
  1459. } // WINSCP
  1460. }
  1461. static mp_int *ssh_ecdhkex_m_getkey(ecdh_key *dh, ptrlen remoteKey)
  1462. {
  1463. mp_int *remote_x = mp_from_bytes_le(remoteKey);
  1464. /* Per RFC 7748 section 5, discard any set bits of the other
  1465. * side's public value beyond the minimum number of bits required
  1466. * to represent all valid values. However, an overlarge value that
  1467. * still fits into the remaining number of bits is accepted, and
  1468. * will be reduced mod p. */
  1469. mp_reduce_mod_2to(remote_x, dh->curve->fieldBits);
  1470. { // WINSCP
  1471. MontgomeryPoint *remote_p = ecc_montgomery_point_new(
  1472. dh->curve->m.mc, remote_x);
  1473. mp_free(remote_x);
  1474. { // WINSCP
  1475. MontgomeryPoint *p = ecc_montgomery_multiply(remote_p, dh->private);
  1476. if (ecc_montgomery_is_identity(p)) {
  1477. ecc_montgomery_point_free(remote_p);
  1478. ecc_montgomery_point_free(p);
  1479. return NULL;
  1480. }
  1481. { // WINSCP
  1482. mp_int *x;
  1483. ecc_montgomery_get_affine(p, &x);
  1484. ecc_montgomery_point_free(remote_p);
  1485. ecc_montgomery_point_free(p);
  1486. /*
  1487. * Endianness-swap. The Curve25519 algorithm definition assumes
  1488. * you were doing your computation in arrays of 32 little-endian
  1489. * bytes, and now specifies that you take your final one of those
  1490. * and convert it into a bignum in _network_ byte order, i.e.
  1491. * big-endian.
  1492. *
  1493. * In particular, the spec says, you convert the _whole_ 32 bytes
  1494. * into a bignum. That is, on the rare occasions that x has come
  1495. * out with the most significant 8 bits zero, we have to imagine
  1496. * that being represented by a 32-byte string with the last byte
  1497. * being zero, so that has to be converted into an SSH-2 bignum
  1498. * with the _low_ byte zero, i.e. a multiple of 256.
  1499. */
  1500. { // WINSCP
  1501. strbuf *sb = strbuf_new();
  1502. size_t i;
  1503. for (i = 0; i < dh->curve->fieldBytes; ++i)
  1504. put_byte(sb, mp_get_byte(x, i));
  1505. mp_free(x);
  1506. x = mp_from_bytes_be(ptrlen_from_strbuf(sb));
  1507. strbuf_free(sb);
  1508. return x;
  1509. } // WINSCP
  1510. } // WINSCP
  1511. } // WINSCP
  1512. } // WINSCP
  1513. }
  1514. mp_int *ssh_ecdhkex_getkey(ecdh_key *dh, ptrlen remoteKey)
  1515. {
  1516. return dh->extra->getkey(dh, remoteKey);
  1517. }
  1518. static void ssh_ecdhkex_w_cleanup(ecdh_key *dh)
  1519. {
  1520. ecc_weierstrass_point_free(dh->w_public);
  1521. }
  1522. static void ssh_ecdhkex_m_cleanup(ecdh_key *dh)
  1523. {
  1524. ecc_montgomery_point_free(dh->m_public);
  1525. }
  1526. void ssh_ecdhkex_freekey(ecdh_key *dh)
  1527. {
  1528. mp_free(dh->private);
  1529. dh->extra->cleanup(dh);
  1530. sfree(dh);
  1531. }
  1532. static const struct eckex_extra kex_extra_curve25519 = {
  1533. ec_curve25519,
  1534. ssh_ecdhkex_m_setup,
  1535. ssh_ecdhkex_m_cleanup,
  1536. ssh_ecdhkex_m_getpublic,
  1537. ssh_ecdhkex_m_getkey,
  1538. };
  1539. const ssh_kex ssh_ec_kex_curve25519 = {
  1540. "curve25519-sha256", NULL, KEXTYPE_ECDH,
  1541. &ssh_sha256, &kex_extra_curve25519,
  1542. };
  1543. /* Pre-RFC alias */
  1544. const ssh_kex ssh_ec_kex_curve25519_libssh = {
  1545. "[email protected]", NULL, KEXTYPE_ECDH,
  1546. &ssh_sha256, &kex_extra_curve25519,
  1547. };
  1548. static const struct eckex_extra kex_extra_curve448 = {
  1549. ec_curve448,
  1550. ssh_ecdhkex_m_setup,
  1551. ssh_ecdhkex_m_cleanup,
  1552. ssh_ecdhkex_m_getpublic,
  1553. ssh_ecdhkex_m_getkey,
  1554. };
  1555. const ssh_kex ssh_ec_kex_curve448 = {
  1556. "curve448-sha512", NULL, KEXTYPE_ECDH,
  1557. &ssh_sha512, &kex_extra_curve448,
  1558. };
  1559. static const struct eckex_extra kex_extra_nistp256 = {
  1560. ec_p256,
  1561. ssh_ecdhkex_w_setup,
  1562. ssh_ecdhkex_w_cleanup,
  1563. ssh_ecdhkex_w_getpublic,
  1564. ssh_ecdhkex_w_getkey,
  1565. };
  1566. const ssh_kex ssh_ec_kex_nistp256 = {
  1567. "ecdh-sha2-nistp256", NULL, KEXTYPE_ECDH,
  1568. &ssh_sha256, &kex_extra_nistp256,
  1569. };
  1570. static const struct eckex_extra kex_extra_nistp384 = {
  1571. ec_p384,
  1572. ssh_ecdhkex_w_setup,
  1573. ssh_ecdhkex_w_cleanup,
  1574. ssh_ecdhkex_w_getpublic,
  1575. ssh_ecdhkex_w_getkey,
  1576. };
  1577. const ssh_kex ssh_ec_kex_nistp384 = {
  1578. "ecdh-sha2-nistp384", NULL, KEXTYPE_ECDH,
  1579. &ssh_sha384, &kex_extra_nistp384,
  1580. };
  1581. static const struct eckex_extra kex_extra_nistp521 = {
  1582. ec_p521,
  1583. ssh_ecdhkex_w_setup,
  1584. ssh_ecdhkex_w_cleanup,
  1585. ssh_ecdhkex_w_getpublic,
  1586. ssh_ecdhkex_w_getkey,
  1587. };
  1588. const ssh_kex ssh_ec_kex_nistp521 = {
  1589. "ecdh-sha2-nistp521", NULL, KEXTYPE_ECDH,
  1590. &ssh_sha512, &kex_extra_nistp521,
  1591. };
  1592. static const ssh_kex *const ec_kex_list[] = {
  1593. &ssh_ec_kex_curve448,
  1594. &ssh_ec_kex_curve25519,
  1595. &ssh_ec_kex_curve25519_libssh,
  1596. &ssh_ec_kex_nistp256,
  1597. &ssh_ec_kex_nistp384,
  1598. &ssh_ec_kex_nistp521,
  1599. };
  1600. const ssh_kexes ssh_ecdh_kex = { lenof(ec_kex_list), ec_kex_list };
  1601. /* ----------------------------------------------------------------------
  1602. * Helper functions for finding key algorithms and returning auxiliary
  1603. * data.
  1604. */
  1605. const ssh_keyalg *ec_alg_by_oid(int len, const void *oid,
  1606. const struct ec_curve **curve)
  1607. {
  1608. static const ssh_keyalg *algs_with_oid[] = {
  1609. &ssh_ecdsa_nistp256,
  1610. &ssh_ecdsa_nistp384,
  1611. &ssh_ecdsa_nistp521,
  1612. };
  1613. int i;
  1614. for (i = 0; i < lenof(algs_with_oid); i++) {
  1615. const ssh_keyalg *alg = algs_with_oid[i];
  1616. const struct ecsign_extra *extra =
  1617. (const struct ecsign_extra *)alg->extra;
  1618. if (len == extra->oidlen && !memcmp(oid, extra->oid, len)) {
  1619. *curve = extra->curve();
  1620. return alg;
  1621. }
  1622. }
  1623. return NULL;
  1624. }
  1625. const unsigned char *ec_alg_oid(const ssh_keyalg *alg,
  1626. int *oidlen)
  1627. {
  1628. const struct ecsign_extra *extra = (const struct ecsign_extra *)alg->extra;
  1629. *oidlen = extra->oidlen;
  1630. return extra->oid;
  1631. }
  1632. const int ec_nist_curve_lengths[] = { 256, 384, 521 };
  1633. const int n_ec_nist_curve_lengths = lenof(ec_nist_curve_lengths);
  1634. const int ec_ed_curve_lengths[] = { 255, 448 };
  1635. const int n_ec_ed_curve_lengths = lenof(ec_ed_curve_lengths);
  1636. bool ec_nist_alg_and_curve_by_bits(
  1637. int bits, const struct ec_curve **curve, const ssh_keyalg **alg)
  1638. {
  1639. switch (bits) {
  1640. case 256: *alg = &ssh_ecdsa_nistp256; break;
  1641. case 384: *alg = &ssh_ecdsa_nistp384; break;
  1642. case 521: *alg = &ssh_ecdsa_nistp521; break;
  1643. default: return false;
  1644. }
  1645. *curve = ((struct ecsign_extra *)(*alg)->extra)->curve();
  1646. return true;
  1647. }
  1648. bool ec_ed_alg_and_curve_by_bits(
  1649. int bits, const struct ec_curve **curve, const ssh_keyalg **alg)
  1650. {
  1651. switch (bits) {
  1652. case 255: case 256: *alg = &ssh_ecdsa_ed25519; break;
  1653. case 448: *alg = &ssh_ecdsa_ed448; break;
  1654. default: return false;
  1655. }
  1656. *curve = ((struct ecsign_extra *)(*alg)->extra)->curve();
  1657. return true;
  1658. }
  1659. #ifdef MPEXT
  1660. void ec_cleanup(void)
  1661. {
  1662. ec_curve_cleanup = 1;
  1663. ec_p256();
  1664. ec_p384();
  1665. ec_p521();
  1666. ec_curve25519();
  1667. ec_ed25519();
  1668. // in case we want to restart (unlikely)
  1669. ec_curve_cleanup = 0;
  1670. }
  1671. #endif