sshecc.c 48 KB

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