ecc-ssh.c 54 KB

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