sshecc.c 52 KB

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