sshecc.c 50 KB

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