ecc-ssh.c 58 KB

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