ecc-ssh.c 63 KB

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