sshecc.c 50 KB

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