sshecc.c 51 KB

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