ecc-ssh.c 56 KB

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