ecc-ssh.c 63 KB

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