ecc-ssh.c 58 KB

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