ecc-ssh.c 63 KB

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