sshecc.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876
  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. #ifdef MPEXT
  37. int ec_curve_cleanup = 0;
  38. static void finalize_ec_point(struct ec_point *point)
  39. {
  40. if (point->x != NULL) freebn(point->x);
  41. if (point->y != NULL) freebn(point->y);
  42. if (point->z != NULL) freebn(point->z);
  43. }
  44. static void finalize_wcurve(struct ec_curve *curve)
  45. {
  46. if (curve->p != NULL) freebn(curve->p);
  47. if (curve->w.a != NULL) freebn(curve->w.a);
  48. if (curve->w.b != NULL) freebn(curve->w.b);
  49. if (curve->w.n != NULL) freebn(curve->w.n);
  50. finalize_ec_point(&curve->w.G);
  51. }
  52. static void finalize_mcurve(struct ec_curve *curve)
  53. {
  54. if (curve->p != NULL) freebn(curve->p);
  55. if (curve->m.a != NULL) freebn(curve->m.a);
  56. if (curve->m.b != NULL) freebn(curve->m.b);
  57. finalize_ec_point(&curve->m.G);
  58. }
  59. static void finalize_ecurve(struct ec_curve *curve)
  60. {
  61. if (curve->p != NULL) freebn(curve->p);
  62. if (curve->e.l != NULL) freebn(curve->e.l);
  63. if (curve->e.d != NULL) freebn(curve->e.d);
  64. finalize_ec_point(&curve->e.B);
  65. }
  66. #endif
  67. /* ----------------------------------------------------------------------
  68. * Elliptic curve definitions
  69. */
  70. static void initialise_wcurve(struct ec_curve *curve, int bits,
  71. const unsigned char *p,
  72. const unsigned char *a, const unsigned char *b,
  73. const unsigned char *n, const unsigned char *Gx,
  74. const unsigned char *Gy)
  75. {
  76. int length = bits / 8;
  77. if (bits % 8) ++length;
  78. curve->type = EC_WEIERSTRASS;
  79. curve->fieldBits = bits;
  80. curve->p = bignum_from_bytes(p, length);
  81. /* Curve co-efficients */
  82. curve->w.a = bignum_from_bytes(a, length);
  83. curve->w.b = bignum_from_bytes(b, length);
  84. /* Group order and generator */
  85. curve->w.n = bignum_from_bytes(n, length);
  86. curve->w.G.x = bignum_from_bytes(Gx, length);
  87. curve->w.G.y = bignum_from_bytes(Gy, length);
  88. curve->w.G.curve = curve;
  89. curve->w.G.infinity = false;
  90. }
  91. static void initialise_mcurve(struct ec_curve *curve, int bits,
  92. const unsigned char *p,
  93. const unsigned char *a, const unsigned char *b,
  94. const unsigned char *Gx)
  95. {
  96. int length = bits / 8;
  97. if (bits % 8) ++length;
  98. curve->type = EC_MONTGOMERY;
  99. curve->fieldBits = bits;
  100. curve->p = bignum_from_bytes(p, length);
  101. /* Curve co-efficients */
  102. curve->m.a = bignum_from_bytes(a, length);
  103. curve->m.b = bignum_from_bytes(b, length);
  104. /* Generator */
  105. curve->m.G.x = bignum_from_bytes(Gx, length);
  106. curve->m.G.y = NULL;
  107. curve->m.G.z = NULL;
  108. curve->m.G.curve = curve;
  109. curve->m.G.infinity = false;
  110. }
  111. static void initialise_ecurve(struct ec_curve *curve, int bits,
  112. const unsigned char *p,
  113. const unsigned char *l, const unsigned char *d,
  114. const unsigned char *Bx, const unsigned char *By)
  115. {
  116. int length = bits / 8;
  117. if (bits % 8) ++length;
  118. curve->type = EC_EDWARDS;
  119. curve->fieldBits = bits;
  120. curve->p = bignum_from_bytes(p, length);
  121. /* Curve co-efficients */
  122. curve->e.l = bignum_from_bytes(l, length);
  123. curve->e.d = bignum_from_bytes(d, length);
  124. /* Group order and generator */
  125. curve->e.B.x = bignum_from_bytes(Bx, length);
  126. curve->e.B.y = bignum_from_bytes(By, length);
  127. curve->e.B.curve = curve;
  128. curve->e.B.infinity = false;
  129. }
  130. static struct ec_curve *ec_p256(void)
  131. {
  132. static struct ec_curve curve = { 0 };
  133. static bool initialised = false;
  134. #ifdef MPEXT
  135. if (ec_curve_cleanup)
  136. {
  137. if (initialised) finalize_wcurve(&curve);
  138. initialised = 0;
  139. return NULL;
  140. }
  141. #endif
  142. if (!initialised)
  143. {
  144. static const unsigned char p[] = {
  145. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  147. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  148. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  149. };
  150. static const unsigned char a[] = {
  151. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  154. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc
  155. };
  156. static const unsigned char b[] = {
  157. 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7,
  158. 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc,
  159. 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6,
  160. 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b
  161. };
  162. static const unsigned char n[] = {
  163. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  164. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  165. 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84,
  166. 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51
  167. };
  168. static const unsigned char Gx[] = {
  169. 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
  170. 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2,
  171. 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
  172. 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96
  173. };
  174. static const unsigned char Gy[] = {
  175. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b,
  176. 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
  177. 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
  178. 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5
  179. };
  180. initialise_wcurve(&curve, 256, p, a, b, n, Gx, Gy);
  181. curve.textname = curve.name = "nistp256";
  182. /* Now initialised, no need to do it again */
  183. initialised = true;
  184. }
  185. return &curve;
  186. }
  187. static struct ec_curve *ec_p384(void)
  188. {
  189. static struct ec_curve curve = { 0 };
  190. static bool initialised = false;
  191. #ifdef MPEXT
  192. if (ec_curve_cleanup)
  193. {
  194. if (initialised) finalize_wcurve(&curve);
  195. initialised = 0;
  196. return NULL;
  197. }
  198. #endif
  199. if (!initialised)
  200. {
  201. static const unsigned char p[] = {
  202. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  203. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  204. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  205. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  206. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  207. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  208. };
  209. static const unsigned char a[] = {
  210. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  211. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  212. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  213. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  214. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  215. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfc
  216. };
  217. static const unsigned char b[] = {
  218. 0xb3, 0x31, 0x2f, 0xa7, 0xe2, 0x3e, 0xe7, 0xe4,
  219. 0x98, 0x8e, 0x05, 0x6b, 0xe3, 0xf8, 0x2d, 0x19,
  220. 0x18, 0x1d, 0x9c, 0x6e, 0xfe, 0x81, 0x41, 0x12,
  221. 0x03, 0x14, 0x08, 0x8f, 0x50, 0x13, 0x87, 0x5a,
  222. 0xc6, 0x56, 0x39, 0x8d, 0x8a, 0x2e, 0xd1, 0x9d,
  223. 0x2a, 0x85, 0xc8, 0xed, 0xd3, 0xec, 0x2a, 0xef
  224. };
  225. static const unsigned char n[] = {
  226. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  227. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  228. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  229. 0xc7, 0x63, 0x4d, 0x81, 0xf4, 0x37, 0x2d, 0xdf,
  230. 0x58, 0x1a, 0x0d, 0xb2, 0x48, 0xb0, 0xa7, 0x7a,
  231. 0xec, 0xec, 0x19, 0x6a, 0xcc, 0xc5, 0x29, 0x73
  232. };
  233. static const unsigned char Gx[] = {
  234. 0xaa, 0x87, 0xca, 0x22, 0xbe, 0x8b, 0x05, 0x37,
  235. 0x8e, 0xb1, 0xc7, 0x1e, 0xf3, 0x20, 0xad, 0x74,
  236. 0x6e, 0x1d, 0x3b, 0x62, 0x8b, 0xa7, 0x9b, 0x98,
  237. 0x59, 0xf7, 0x41, 0xe0, 0x82, 0x54, 0x2a, 0x38,
  238. 0x55, 0x02, 0xf2, 0x5d, 0xbf, 0x55, 0x29, 0x6c,
  239. 0x3a, 0x54, 0x5e, 0x38, 0x72, 0x76, 0x0a, 0xb7
  240. };
  241. static const unsigned char Gy[] = {
  242. 0x36, 0x17, 0xde, 0x4a, 0x96, 0x26, 0x2c, 0x6f,
  243. 0x5d, 0x9e, 0x98, 0xbf, 0x92, 0x92, 0xdc, 0x29,
  244. 0xf8, 0xf4, 0x1d, 0xbd, 0x28, 0x9a, 0x14, 0x7c,
  245. 0xe9, 0xda, 0x31, 0x13, 0xb5, 0xf0, 0xb8, 0xc0,
  246. 0x0a, 0x60, 0xb1, 0xce, 0x1d, 0x7e, 0x81, 0x9d,
  247. 0x7a, 0x43, 0x1d, 0x7c, 0x90, 0xea, 0x0e, 0x5f
  248. };
  249. initialise_wcurve(&curve, 384, p, a, b, n, Gx, Gy);
  250. curve.textname = curve.name = "nistp384";
  251. /* Now initialised, no need to do it again */
  252. initialised = true;
  253. }
  254. return &curve;
  255. }
  256. static struct ec_curve *ec_p521(void)
  257. {
  258. static struct ec_curve curve = { 0 };
  259. static bool initialised = false;
  260. #ifdef MPEXT
  261. if (ec_curve_cleanup)
  262. {
  263. if (initialised) finalize_wcurve(&curve);
  264. initialised = 0;
  265. return NULL;
  266. }
  267. #endif
  268. if (!initialised)
  269. {
  270. static const unsigned char p[] = {
  271. 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  272. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  273. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  274. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  275. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  276. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  277. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  278. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  279. 0xff, 0xff
  280. };
  281. static const unsigned char a[] = {
  282. 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  283. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  284. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  285. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  286. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  287. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  288. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  289. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  290. 0xff, 0xfc
  291. };
  292. static const unsigned char b[] = {
  293. 0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c,
  294. 0x9a, 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85,
  295. 0x40, 0xee, 0xa2, 0xda, 0x72, 0x5b, 0x99, 0xb3,
  296. 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1,
  297. 0x09, 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e,
  298. 0x93, 0x7b, 0x16, 0x52, 0xc0, 0xbd, 0x3b, 0xb1,
  299. 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c,
  300. 0x34, 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50,
  301. 0x3f, 0x00
  302. };
  303. static const unsigned char n[] = {
  304. 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  305. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  306. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  307. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  308. 0xff, 0xfa, 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f,
  309. 0x96, 0x6b, 0x7f, 0xcc, 0x01, 0x48, 0xf7, 0x09,
  310. 0xa5, 0xd0, 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c,
  311. 0x47, 0xae, 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38,
  312. 0x64, 0x09
  313. };
  314. static const unsigned char Gx[] = {
  315. 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04,
  316. 0xe9, 0xcd, 0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95,
  317. 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
  318. 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d,
  319. 0x3d, 0xba, 0xa1, 0x4b, 0x5e, 0x77, 0xef, 0xe7,
  320. 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
  321. 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a,
  322. 0x42, 0x9b, 0xf9, 0x7e, 0x7e, 0x31, 0xc2, 0xe5,
  323. 0xbd, 0x66
  324. };
  325. static const unsigned char Gy[] = {
  326. 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b,
  327. 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d,
  328. 0x1b, 0xd9, 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b,
  329. 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e,
  330. 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4,
  331. 0x26, 0x40, 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad,
  332. 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72,
  333. 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1,
  334. 0x66, 0x50
  335. };
  336. initialise_wcurve(&curve, 521, p, a, b, n, Gx, Gy);
  337. curve.textname = curve.name = "nistp521";
  338. /* Now initialised, no need to do it again */
  339. initialised = true;
  340. }
  341. return &curve;
  342. }
  343. static struct ec_curve *ec_curve25519(void)
  344. {
  345. static struct ec_curve curve = { 0 };
  346. static bool initialised = false;
  347. #ifdef MPEXT
  348. if (ec_curve_cleanup)
  349. {
  350. if (initialised) finalize_mcurve(&curve);
  351. initialised = 0;
  352. return NULL;
  353. }
  354. #endif
  355. if (!initialised)
  356. {
  357. static const unsigned char p[] = {
  358. 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  359. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  360. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  361. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xed
  362. };
  363. static const unsigned char a[] = {
  364. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  365. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  366. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x6d, 0x06
  368. };
  369. static const unsigned char b[] = {
  370. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  371. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  372. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  373. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  374. };
  375. static const unsigned char gx[32] = {
  376. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  377. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  378. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  379. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09
  380. };
  381. initialise_mcurve(&curve, 256, p, a, b, gx);
  382. /* This curve doesn't need a name, because it's never used in
  383. * any format that embeds the curve name */
  384. curve.name = NULL;
  385. curve.textname = "Curve25519";
  386. /* Now initialised, no need to do it again */
  387. initialised = true;
  388. }
  389. return &curve;
  390. }
  391. static struct ec_curve *ec_ed25519(void)
  392. {
  393. static struct ec_curve curve = { 0 };
  394. static bool initialised = false;
  395. #ifdef MPEXT
  396. if (ec_curve_cleanup)
  397. {
  398. if (initialised) finalize_ecurve(&curve);
  399. initialised = 0;
  400. return NULL;
  401. }
  402. #endif
  403. if (!initialised)
  404. {
  405. static const unsigned char q[] = {
  406. 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  407. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  408. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  409. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xed
  410. };
  411. static const unsigned char l[32] = {
  412. 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  413. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  414. 0x14, 0xde, 0xf9, 0xde, 0xa2, 0xf7, 0x9c, 0xd6,
  415. 0x58, 0x12, 0x63, 0x1a, 0x5c, 0xf5, 0xd3, 0xed
  416. };
  417. static const unsigned char d[32] = {
  418. 0x52, 0x03, 0x6c, 0xee, 0x2b, 0x6f, 0xfe, 0x73,
  419. 0x8c, 0xc7, 0x40, 0x79, 0x77, 0x79, 0xe8, 0x98,
  420. 0x00, 0x70, 0x0a, 0x4d, 0x41, 0x41, 0xd8, 0xab,
  421. 0x75, 0xeb, 0x4d, 0xca, 0x13, 0x59, 0x78, 0xa3
  422. };
  423. static const unsigned char Bx[32] = {
  424. 0x21, 0x69, 0x36, 0xd3, 0xcd, 0x6e, 0x53, 0xfe,
  425. 0xc0, 0xa4, 0xe2, 0x31, 0xfd, 0xd6, 0xdc, 0x5c,
  426. 0x69, 0x2c, 0xc7, 0x60, 0x95, 0x25, 0xa7, 0xb2,
  427. 0xc9, 0x56, 0x2d, 0x60, 0x8f, 0x25, 0xd5, 0x1a
  428. };
  429. static const unsigned char By[32] = {
  430. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  431. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  432. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  433. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x58
  434. };
  435. /* This curve doesn't need a name, because it's never used in
  436. * any format that embeds the curve name */
  437. curve.name = NULL;
  438. initialise_ecurve(&curve, 256, q, l, d, Bx, By);
  439. curve.textname = "Ed25519";
  440. /* Now initialised, no need to do it again */
  441. initialised = true;
  442. }
  443. return &curve;
  444. }
  445. /* Return 1 if a is -3 % p, otherwise return 0
  446. * This is used because there are some maths optimisations */
  447. static bool ec_aminus3(const struct ec_curve *curve)
  448. {
  449. bool ret;
  450. Bignum _p;
  451. if (curve->type != EC_WEIERSTRASS) {
  452. return false;
  453. }
  454. _p = bignum_add_long(curve->w.a, 3);
  455. ret = !bignum_cmp(curve->p, _p);
  456. freebn(_p);
  457. return ret;
  458. }
  459. /* ----------------------------------------------------------------------
  460. * Elliptic curve field maths
  461. */
  462. static Bignum ecf_add(const Bignum a, const Bignum b,
  463. const struct ec_curve *curve)
  464. {
  465. Bignum a1, b1, ab, ret;
  466. a1 = bigmod(a, curve->p);
  467. b1 = bigmod(b, curve->p);
  468. ab = bigadd(a1, b1);
  469. freebn(a1);
  470. freebn(b1);
  471. ret = bigmod(ab, curve->p);
  472. freebn(ab);
  473. return ret;
  474. }
  475. static Bignum ecf_square(const Bignum a, const struct ec_curve *curve)
  476. {
  477. return modmul(a, a, curve->p);
  478. }
  479. static Bignum ecf_treble(const Bignum a, const struct ec_curve *curve)
  480. {
  481. Bignum ret, tmp;
  482. /* Double */
  483. tmp = bignum_lshift(a, 1);
  484. /* Add itself (i.e. treble) */
  485. ret = bigadd(tmp, a);
  486. freebn(tmp);
  487. /* Normalise */
  488. while (bignum_cmp(ret, curve->p) >= 0)
  489. {
  490. tmp = bigsub(ret, curve->p);
  491. assert(tmp);
  492. freebn(ret);
  493. ret = tmp;
  494. }
  495. return ret;
  496. }
  497. static Bignum ecf_double(const Bignum a, const struct ec_curve *curve)
  498. {
  499. Bignum ret = bignum_lshift(a, 1);
  500. if (bignum_cmp(ret, curve->p) >= 0)
  501. {
  502. Bignum tmp = bigsub(ret, curve->p);
  503. assert(tmp);
  504. freebn(ret);
  505. return tmp;
  506. }
  507. else
  508. {
  509. return ret;
  510. }
  511. }
  512. /* ----------------------------------------------------------------------
  513. * Memory functions
  514. */
  515. void ec_point_free(struct ec_point *point)
  516. {
  517. if (point == NULL) return;
  518. point->curve = 0;
  519. if (point->x) freebn(point->x);
  520. if (point->y) freebn(point->y);
  521. if (point->z) freebn(point->z);
  522. point->infinity = false;
  523. sfree(point);
  524. }
  525. static struct ec_point *ec_point_new(const struct ec_curve *curve,
  526. const Bignum x, const Bignum y, const Bignum z,
  527. bool infinity)
  528. {
  529. struct ec_point *point = snewn(1, struct ec_point);
  530. point->curve = curve;
  531. point->x = x;
  532. point->y = y;
  533. point->z = z;
  534. point->infinity = infinity;
  535. return point;
  536. }
  537. static struct ec_point *ec_point_copy(const struct ec_point *a)
  538. {
  539. if (a == NULL) return NULL;
  540. return ec_point_new(a->curve,
  541. a->x ? copybn(a->x) : NULL,
  542. a->y ? copybn(a->y) : NULL,
  543. a->z ? copybn(a->z) : NULL,
  544. a->infinity);
  545. }
  546. static bool ec_point_verify(const struct ec_point *a)
  547. {
  548. if (a->infinity) {
  549. return true;
  550. } else if (a->curve->type == EC_EDWARDS) {
  551. /* Check y^2 - x^2 - 1 - d * x^2 * y^2 == 0 */
  552. Bignum y2, x2, tmp, tmp2, tmp3;
  553. bool ret;
  554. y2 = ecf_square(a->y, a->curve);
  555. x2 = ecf_square(a->x, a->curve);
  556. tmp = modmul(a->curve->e.d, x2, a->curve->p);
  557. tmp2 = modmul(tmp, y2, a->curve->p);
  558. freebn(tmp);
  559. tmp = modsub(y2, x2, a->curve->p);
  560. freebn(y2);
  561. freebn(x2);
  562. tmp3 = modsub(tmp, tmp2, a->curve->p);
  563. freebn(tmp);
  564. freebn(tmp2);
  565. ret = !bignum_cmp(tmp3, One);
  566. freebn(tmp3);
  567. return ret;
  568. } else if (a->curve->type == EC_WEIERSTRASS) {
  569. /* Verify y^2 = x^3 + ax + b */
  570. bool ret = false;
  571. Bignum lhs = NULL, x3 = NULL, ax = NULL, x3ax = NULL, x3axm = NULL, x3axb = NULL, rhs = NULL;
  572. Bignum Three = bignum_from_long(3);
  573. lhs = modmul(a->y, a->y, a->curve->p);
  574. /* This uses montgomery multiplication to optimise */
  575. x3 = modpow(a->x, Three, a->curve->p);
  576. freebn(Three);
  577. ax = modmul(a->curve->w.a, a->x, a->curve->p);
  578. x3ax = bigadd(x3, ax);
  579. freebn(x3); x3 = NULL;
  580. freebn(ax); ax = NULL;
  581. x3axm = bigmod(x3ax, a->curve->p);
  582. freebn(x3ax); x3ax = NULL;
  583. x3axb = bigadd(x3axm, a->curve->w.b);
  584. freebn(x3axm); x3axm = NULL;
  585. rhs = bigmod(x3axb, a->curve->p);
  586. freebn(x3axb);
  587. ret = !bignum_cmp(lhs, rhs);
  588. freebn(lhs);
  589. freebn(rhs);
  590. return ret;
  591. } else {
  592. return false;
  593. }
  594. }
  595. /* ----------------------------------------------------------------------
  596. * Elliptic curve point maths
  597. */
  598. /* Returns true on success and false on memory error */
  599. static bool ecp_normalise(struct ec_point *a)
  600. {
  601. if (!a) {
  602. /* No point */
  603. return false;
  604. }
  605. if (a->infinity) {
  606. /* Point is at infinity - i.e. normalised */
  607. return true;
  608. }
  609. if (a->curve->type == EC_WEIERSTRASS) {
  610. /* In Jacobian Coordinates the triple (X, Y, Z) represents
  611. the affine point (X / Z^2, Y / Z^3) */
  612. Bignum Z2, Z2inv, Z3, Z3inv, tx, ty;
  613. if (!a->x || !a->y) {
  614. /* No point defined */
  615. return false;
  616. } else if (!a->z) {
  617. /* Already normalised */
  618. return true;
  619. }
  620. Z2 = ecf_square(a->z, a->curve);
  621. Z2inv = modinv(Z2, a->curve->p);
  622. if (!Z2inv) {
  623. freebn(Z2);
  624. return false;
  625. }
  626. tx = modmul(a->x, Z2inv, a->curve->p);
  627. freebn(Z2inv);
  628. Z3 = modmul(Z2, a->z, a->curve->p);
  629. freebn(Z2);
  630. Z3inv = modinv(Z3, a->curve->p);
  631. freebn(Z3);
  632. if (!Z3inv) {
  633. freebn(tx);
  634. return false;
  635. }
  636. ty = modmul(a->y, Z3inv, a->curve->p);
  637. freebn(Z3inv);
  638. freebn(a->x);
  639. a->x = tx;
  640. freebn(a->y);
  641. a->y = ty;
  642. freebn(a->z);
  643. a->z = NULL;
  644. return true;
  645. } else if (a->curve->type == EC_MONTGOMERY) {
  646. /* In Montgomery (X : Z) represents the x co-ord (X / Z, ?) */
  647. Bignum tmp, tmp2;
  648. if (!a->x) {
  649. /* No point defined */
  650. return false;
  651. } else if (!a->z) {
  652. /* Already normalised */
  653. return true;
  654. }
  655. tmp = modinv(a->z, a->curve->p);
  656. if (!tmp) {
  657. return false;
  658. }
  659. tmp2 = modmul(a->x, tmp, a->curve->p);
  660. freebn(tmp);
  661. freebn(a->z);
  662. a->z = NULL;
  663. freebn(a->x);
  664. a->x = tmp2;
  665. return true;
  666. } else if (a->curve->type == EC_EDWARDS) {
  667. /* Always normalised */
  668. return true;
  669. } else {
  670. return false;
  671. }
  672. }
  673. static struct ec_point *ecp_doublew(const struct ec_point *a, bool aminus3)
  674. {
  675. Bignum S, M, outx, outy, outz;
  676. if (bignum_cmp(a->y, Zero) == 0)
  677. {
  678. /* Identity */
  679. return ec_point_new(a->curve, NULL, NULL, NULL, true);
  680. }
  681. /* S = 4*X*Y^2 */
  682. {
  683. Bignum Y2, XY2, _2XY2;
  684. Y2 = ecf_square(a->y, a->curve);
  685. XY2 = modmul(a->x, Y2, a->curve->p);
  686. freebn(Y2);
  687. _2XY2 = ecf_double(XY2, a->curve);
  688. freebn(XY2);
  689. S = ecf_double(_2XY2, a->curve);
  690. freebn(_2XY2);
  691. }
  692. /* Faster calculation if a = -3 */
  693. if (aminus3) {
  694. /* if a = -3, then M can also be calculated as M = 3*(X + Z^2)*(X - Z^2) */
  695. Bignum Z2, XpZ2, XmZ2, second;
  696. if (a->z == NULL) {
  697. Z2 = copybn(One);
  698. } else {
  699. Z2 = ecf_square(a->z, a->curve);
  700. }
  701. XpZ2 = ecf_add(a->x, Z2, a->curve);
  702. XmZ2 = modsub(a->x, Z2, a->curve->p);
  703. freebn(Z2);
  704. second = modmul(XpZ2, XmZ2, a->curve->p);
  705. freebn(XpZ2);
  706. freebn(XmZ2);
  707. M = ecf_treble(second, a->curve);
  708. freebn(second);
  709. } else {
  710. /* M = 3*X^2 + a*Z^4 */
  711. Bignum _3X2, X2, aZ4;
  712. if (a->z == NULL) {
  713. aZ4 = copybn(a->curve->w.a);
  714. } else {
  715. Bignum Z2, Z4;
  716. Z2 = ecf_square(a->z, a->curve);
  717. Z4 = ecf_square(Z2, a->curve);
  718. freebn(Z2);
  719. aZ4 = modmul(a->curve->w.a, Z4, a->curve->p);
  720. freebn(Z4);
  721. }
  722. X2 = modmul(a->x, a->x, a->curve->p);
  723. _3X2 = ecf_treble(X2, a->curve);
  724. freebn(X2);
  725. M = ecf_add(_3X2, aZ4, a->curve);
  726. freebn(_3X2);
  727. freebn(aZ4);
  728. }
  729. /* X' = M^2 - 2*S */
  730. {
  731. Bignum M2, _2S;
  732. M2 = ecf_square(M, a->curve);
  733. _2S = ecf_double(S, a->curve);
  734. outx = modsub(M2, _2S, a->curve->p);
  735. freebn(M2);
  736. freebn(_2S);
  737. }
  738. /* Y' = M*(S - X') - 8*Y^4 */
  739. {
  740. Bignum SX, MSX, Eight, Y2, Y4, _8Y4;
  741. SX = modsub(S, outx, a->curve->p);
  742. freebn(S);
  743. MSX = modmul(M, SX, a->curve->p);
  744. freebn(SX);
  745. freebn(M);
  746. Y2 = ecf_square(a->y, a->curve);
  747. Y4 = ecf_square(Y2, a->curve);
  748. freebn(Y2);
  749. Eight = bignum_from_long(8);
  750. _8Y4 = modmul(Eight, Y4, a->curve->p);
  751. freebn(Eight);
  752. freebn(Y4);
  753. outy = modsub(MSX, _8Y4, a->curve->p);
  754. freebn(MSX);
  755. freebn(_8Y4);
  756. }
  757. /* Z' = 2*Y*Z */
  758. {
  759. Bignum YZ;
  760. if (a->z == NULL) {
  761. YZ = copybn(a->y);
  762. } else {
  763. YZ = modmul(a->y, a->z, a->curve->p);
  764. }
  765. outz = ecf_double(YZ, a->curve);
  766. freebn(YZ);
  767. }
  768. return ec_point_new(a->curve, outx, outy, outz, false);
  769. }
  770. static struct ec_point *ecp_doublem(const struct ec_point *a)
  771. {
  772. Bignum z, outx, outz, xpz, xmz;
  773. z = a->z;
  774. if (!z) {
  775. z = One;
  776. }
  777. /* 4xz = (x + z)^2 - (x - z)^2 */
  778. {
  779. Bignum tmp;
  780. tmp = ecf_add(a->x, z, a->curve);
  781. xpz = ecf_square(tmp, a->curve);
  782. freebn(tmp);
  783. tmp = modsub(a->x, z, a->curve->p);
  784. xmz = ecf_square(tmp, a->curve);
  785. freebn(tmp);
  786. }
  787. /* outx = (x + z)^2 * (x - z)^2 */
  788. outx = modmul(xpz, xmz, a->curve->p);
  789. /* outz = 4xz * ((x - z)^2 + ((A + 2) / 4)*4xz) */
  790. {
  791. Bignum _4xz, tmp, tmp2, tmp3;
  792. tmp = bignum_from_long(2);
  793. tmp2 = ecf_add(a->curve->m.a, tmp, a->curve);
  794. freebn(tmp);
  795. _4xz = modsub(xpz, xmz, a->curve->p);
  796. freebn(xpz);
  797. tmp = modmul(tmp2, _4xz, a->curve->p);
  798. freebn(tmp2);
  799. tmp2 = bignum_from_long(4);
  800. tmp3 = modinv(tmp2, a->curve->p);
  801. freebn(tmp2);
  802. if (!tmp3) {
  803. freebn(tmp);
  804. freebn(_4xz);
  805. freebn(outx);
  806. freebn(xmz);
  807. return NULL;
  808. }
  809. tmp2 = modmul(tmp, tmp3, a->curve->p);
  810. freebn(tmp);
  811. freebn(tmp3);
  812. tmp = ecf_add(xmz, tmp2, a->curve);
  813. freebn(xmz);
  814. freebn(tmp2);
  815. outz = modmul(_4xz, tmp, a->curve->p);
  816. freebn(_4xz);
  817. freebn(tmp);
  818. }
  819. return ec_point_new(a->curve, outx, NULL, outz, false);
  820. }
  821. /* Forward declaration for Edwards curve doubling */
  822. static struct ec_point *ecp_add(const struct ec_point *a,
  823. const struct ec_point *b,
  824. bool aminus3);
  825. static struct ec_point *ecp_double(const struct ec_point *a, bool aminus3)
  826. {
  827. if (a->infinity)
  828. {
  829. /* Identity */
  830. return ec_point_new(a->curve, NULL, NULL, NULL, true);
  831. }
  832. if (a->curve->type == EC_EDWARDS)
  833. {
  834. return ecp_add(a, a, aminus3);
  835. }
  836. else if (a->curve->type == EC_WEIERSTRASS)
  837. {
  838. return ecp_doublew(a, aminus3);
  839. }
  840. else
  841. {
  842. return ecp_doublem(a);
  843. }
  844. }
  845. static struct ec_point *ecp_addw(const struct ec_point *a,
  846. const struct ec_point *b,
  847. bool aminus3)
  848. {
  849. Bignum U1, U2, S1, S2, outx, outy, outz;
  850. /* U1 = X1*Z2^2 */
  851. /* S1 = Y1*Z2^3 */
  852. if (b->z) {
  853. Bignum Z2, Z3;
  854. Z2 = ecf_square(b->z, a->curve);
  855. U1 = modmul(a->x, Z2, a->curve->p);
  856. Z3 = modmul(Z2, b->z, a->curve->p);
  857. freebn(Z2);
  858. S1 = modmul(a->y, Z3, a->curve->p);
  859. freebn(Z3);
  860. } else {
  861. U1 = copybn(a->x);
  862. S1 = copybn(a->y);
  863. }
  864. /* U2 = X2*Z1^2 */
  865. /* S2 = Y2*Z1^3 */
  866. if (a->z) {
  867. Bignum Z2, Z3;
  868. Z2 = ecf_square(a->z, b->curve);
  869. U2 = modmul(b->x, Z2, b->curve->p);
  870. Z3 = modmul(Z2, a->z, b->curve->p);
  871. freebn(Z2);
  872. S2 = modmul(b->y, Z3, b->curve->p);
  873. freebn(Z3);
  874. } else {
  875. U2 = copybn(b->x);
  876. S2 = copybn(b->y);
  877. }
  878. /* Check if multiplying by self */
  879. if (bignum_cmp(U1, U2) == 0)
  880. {
  881. freebn(U1);
  882. freebn(U2);
  883. if (bignum_cmp(S1, S2) == 0)
  884. {
  885. freebn(S1);
  886. freebn(S2);
  887. return ecp_double(a, aminus3);
  888. }
  889. else
  890. {
  891. freebn(S1);
  892. freebn(S2);
  893. /* Infinity */
  894. return ec_point_new(a->curve, NULL, NULL, NULL, true);
  895. }
  896. }
  897. {
  898. Bignum H, R, UH2, H3;
  899. /* H = U2 - U1 */
  900. H = modsub(U2, U1, a->curve->p);
  901. freebn(U2);
  902. /* R = S2 - S1 */
  903. R = modsub(S2, S1, a->curve->p);
  904. freebn(S2);
  905. /* X3 = R^2 - H^3 - 2*U1*H^2 */
  906. {
  907. Bignum R2, H2, _2UH2, first;
  908. H2 = ecf_square(H, a->curve);
  909. UH2 = modmul(U1, H2, a->curve->p);
  910. freebn(U1);
  911. H3 = modmul(H2, H, a->curve->p);
  912. freebn(H2);
  913. R2 = ecf_square(R, a->curve);
  914. _2UH2 = ecf_double(UH2, a->curve);
  915. first = modsub(R2, H3, a->curve->p);
  916. freebn(R2);
  917. outx = modsub(first, _2UH2, a->curve->p);
  918. freebn(first);
  919. freebn(_2UH2);
  920. }
  921. /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
  922. {
  923. Bignum RUH2mX, UH2mX, SH3;
  924. UH2mX = modsub(UH2, outx, a->curve->p);
  925. freebn(UH2);
  926. RUH2mX = modmul(R, UH2mX, a->curve->p);
  927. freebn(UH2mX);
  928. freebn(R);
  929. SH3 = modmul(S1, H3, a->curve->p);
  930. freebn(S1);
  931. freebn(H3);
  932. outy = modsub(RUH2mX, SH3, a->curve->p);
  933. freebn(RUH2mX);
  934. freebn(SH3);
  935. }
  936. /* Z3 = H*Z1*Z2 */
  937. if (a->z && b->z) {
  938. Bignum ZZ;
  939. ZZ = modmul(a->z, b->z, a->curve->p);
  940. outz = modmul(H, ZZ, a->curve->p);
  941. freebn(H);
  942. freebn(ZZ);
  943. } else if (a->z) {
  944. outz = modmul(H, a->z, a->curve->p);
  945. freebn(H);
  946. } else if (b->z) {
  947. outz = modmul(H, b->z, a->curve->p);
  948. freebn(H);
  949. } else {
  950. outz = H;
  951. }
  952. }
  953. return ec_point_new(a->curve, outx, outy, outz, false);
  954. }
  955. static struct ec_point *ecp_addm(const struct ec_point *a,
  956. const struct ec_point *b,
  957. const struct ec_point *base)
  958. {
  959. Bignum outx, outz, az, bz;
  960. az = a->z;
  961. if (!az) {
  962. az = One;
  963. }
  964. bz = b->z;
  965. if (!bz) {
  966. bz = One;
  967. }
  968. /* a-b is maintained at 1 due to Montgomery ladder implementation */
  969. /* Xa+b = Za-b * ((Xa - Za)*(Xb + Zb) + (Xa + Za)*(Xb - Zb))^2 */
  970. /* Za+b = Xa-b * ((Xa - Za)*(Xb + Zb) - (Xa + Za)*(Xb - Zb))^2 */
  971. {
  972. Bignum tmp, tmp2, tmp3, tmp4;
  973. /* (Xa + Za) * (Xb - Zb) */
  974. tmp = ecf_add(a->x, az, a->curve);
  975. tmp2 = modsub(b->x, bz, a->curve->p);
  976. tmp3 = modmul(tmp, tmp2, a->curve->p);
  977. freebn(tmp);
  978. freebn(tmp2);
  979. /* (Xa - Za) * (Xb + Zb) */
  980. tmp = modsub(a->x, az, a->curve->p);
  981. tmp2 = ecf_add(b->x, bz, a->curve);
  982. tmp4 = modmul(tmp, tmp2, a->curve->p);
  983. freebn(tmp);
  984. freebn(tmp2);
  985. tmp = ecf_add(tmp3, tmp4, a->curve);
  986. outx = ecf_square(tmp, a->curve);
  987. freebn(tmp);
  988. tmp = modsub(tmp3, tmp4, a->curve->p);
  989. freebn(tmp3);
  990. freebn(tmp4);
  991. tmp2 = ecf_square(tmp, a->curve);
  992. freebn(tmp);
  993. outz = modmul(base->x, tmp2, a->curve->p);
  994. freebn(tmp2);
  995. }
  996. return ec_point_new(a->curve, outx, NULL, outz, false);
  997. }
  998. static struct ec_point *ecp_adde(const struct ec_point *a,
  999. const struct ec_point *b)
  1000. {
  1001. Bignum outx, outy, dmul;
  1002. /* outx = (a->x * b->y + b->x * a->y) /
  1003. * (1 + a->curve->e.d * a->x * b->x * a->y * b->y) */
  1004. {
  1005. Bignum tmp, tmp2, tmp3, tmp4;
  1006. tmp = modmul(a->x, b->y, a->curve->p);
  1007. tmp2 = modmul(b->x, a->y, a->curve->p);
  1008. tmp3 = ecf_add(tmp, tmp2, a->curve);
  1009. tmp4 = modmul(tmp, tmp2, a->curve->p);
  1010. freebn(tmp);
  1011. freebn(tmp2);
  1012. dmul = modmul(a->curve->e.d, tmp4, a->curve->p);
  1013. freebn(tmp4);
  1014. tmp = ecf_add(One, dmul, a->curve);
  1015. tmp2 = modinv(tmp, a->curve->p);
  1016. freebn(tmp);
  1017. if (!tmp2)
  1018. {
  1019. freebn(tmp3);
  1020. freebn(dmul);
  1021. return NULL;
  1022. }
  1023. outx = modmul(tmp3, tmp2, a->curve->p);
  1024. freebn(tmp3);
  1025. freebn(tmp2);
  1026. }
  1027. /* outy = (a->y * b->y + a->x * b->x) /
  1028. * (1 - a->curve->e.d * a->x * b->x * a->y * b->y) */
  1029. {
  1030. Bignum tmp, tmp2, tmp3, tmp4;
  1031. tmp = modsub(One, dmul, a->curve->p);
  1032. freebn(dmul);
  1033. tmp2 = modinv(tmp, a->curve->p);
  1034. freebn(tmp);
  1035. if (!tmp2)
  1036. {
  1037. freebn(outx);
  1038. return NULL;
  1039. }
  1040. tmp = modmul(a->y, b->y, a->curve->p);
  1041. tmp3 = modmul(a->x, b->x, a->curve->p);
  1042. tmp4 = ecf_add(tmp, tmp3, a->curve);
  1043. freebn(tmp);
  1044. freebn(tmp3);
  1045. outy = modmul(tmp4, tmp2, a->curve->p);
  1046. freebn(tmp4);
  1047. freebn(tmp2);
  1048. }
  1049. return ec_point_new(a->curve, outx, outy, NULL, false);
  1050. }
  1051. static struct ec_point *ecp_add(const struct ec_point *a,
  1052. const struct ec_point *b,
  1053. bool aminus3)
  1054. {
  1055. if (a->curve != b->curve) {
  1056. return NULL;
  1057. }
  1058. /* Check if multiplying by infinity */
  1059. if (a->infinity) return ec_point_copy(b);
  1060. if (b->infinity) return ec_point_copy(a);
  1061. if (a->curve->type == EC_EDWARDS)
  1062. {
  1063. return ecp_adde(a, b);
  1064. }
  1065. if (a->curve->type == EC_WEIERSTRASS)
  1066. {
  1067. return ecp_addw(a, b, aminus3);
  1068. }
  1069. return NULL;
  1070. }
  1071. static struct ec_point *ecp_mul_(
  1072. const struct ec_point *a, const Bignum b, bool aminus3)
  1073. {
  1074. struct ec_point *A, *ret;
  1075. int bits, i;
  1076. A = ec_point_copy(a);
  1077. ret = ec_point_new(a->curve, NULL, NULL, NULL, true);
  1078. bits = bignum_bitcount(b);
  1079. for (i = 0; i < bits; ++i)
  1080. {
  1081. if (bignum_bit(b, i))
  1082. {
  1083. struct ec_point *tmp = ecp_add(ret, A, aminus3);
  1084. ec_point_free(ret);
  1085. ret = tmp;
  1086. }
  1087. if (i+1 != bits)
  1088. {
  1089. struct ec_point *tmp = ecp_double(A, aminus3);
  1090. ec_point_free(A);
  1091. A = tmp;
  1092. }
  1093. }
  1094. ec_point_free(A);
  1095. return ret;
  1096. }
  1097. static struct ec_point *ecp_mulw(const struct ec_point *a, const Bignum b)
  1098. {
  1099. struct ec_point *ret = ecp_mul_(a, b, ec_aminus3(a->curve));
  1100. if (!ecp_normalise(ret)) {
  1101. ec_point_free(ret);
  1102. return NULL;
  1103. }
  1104. return ret;
  1105. }
  1106. static struct ec_point *ecp_mule(const struct ec_point *a, const Bignum b)
  1107. {
  1108. int i;
  1109. struct ec_point *ret;
  1110. ret = ec_point_new(a->curve, NULL, NULL, NULL, true);
  1111. for (i = bignum_bitcount(b); i >= 0 && ret; --i)
  1112. {
  1113. {
  1114. struct ec_point *tmp = ecp_double(ret, false);
  1115. ec_point_free(ret);
  1116. ret = tmp;
  1117. }
  1118. if (ret && bignum_bit(b, i))
  1119. {
  1120. struct ec_point *tmp = ecp_add(ret, a, false);
  1121. ec_point_free(ret);
  1122. ret = tmp;
  1123. }
  1124. }
  1125. return ret;
  1126. }
  1127. static struct ec_point *ecp_mulm(const struct ec_point *p, const Bignum n)
  1128. {
  1129. struct ec_point *P1, *P2;
  1130. int bits, i;
  1131. /* P1 <- P and P2 <- [2]P */
  1132. P2 = ecp_double(p, false);
  1133. P1 = ec_point_copy(p);
  1134. /* for i = bits − 2 down to 0 */
  1135. bits = bignum_bitcount(n);
  1136. for (i = bits - 2; i >= 0; --i)
  1137. {
  1138. if (!bignum_bit(n, i))
  1139. {
  1140. /* P2 <- P1 + P2 */
  1141. struct ec_point *tmp = ecp_addm(P1, P2, p);
  1142. ec_point_free(P2);
  1143. P2 = tmp;
  1144. /* P1 <- [2]P1 */
  1145. tmp = ecp_double(P1, false);
  1146. ec_point_free(P1);
  1147. P1 = tmp;
  1148. }
  1149. else
  1150. {
  1151. /* P1 <- P1 + P2 */
  1152. struct ec_point *tmp = ecp_addm(P1, P2, p);
  1153. ec_point_free(P1);
  1154. P1 = tmp;
  1155. /* P2 <- [2]P2 */
  1156. tmp = ecp_double(P2, false);
  1157. ec_point_free(P2);
  1158. P2 = tmp;
  1159. }
  1160. }
  1161. ec_point_free(P2);
  1162. if (!ecp_normalise(P1)) {
  1163. ec_point_free(P1);
  1164. return NULL;
  1165. }
  1166. return P1;
  1167. }
  1168. /* Not static because it is used by sshecdsag.c to generate a new key */
  1169. struct ec_point *ecp_mul(const struct ec_point *a, const Bignum b)
  1170. {
  1171. if (a->curve->type == EC_WEIERSTRASS) {
  1172. return ecp_mulw(a, b);
  1173. } else if (a->curve->type == EC_EDWARDS) {
  1174. return ecp_mule(a, b);
  1175. } else {
  1176. return ecp_mulm(a, b);
  1177. }
  1178. }
  1179. static struct ec_point *ecp_summul(const Bignum a, const Bignum b,
  1180. const struct ec_point *point)
  1181. {
  1182. struct ec_point *aG, *bP, *ret;
  1183. bool aminus3;
  1184. if (point->curve->type != EC_WEIERSTRASS) {
  1185. return NULL;
  1186. }
  1187. aminus3 = ec_aminus3(point->curve);
  1188. aG = ecp_mul_(&point->curve->w.G, a, aminus3);
  1189. if (!aG) return NULL;
  1190. bP = ecp_mul_(point, b, aminus3);
  1191. if (!bP) {
  1192. ec_point_free(aG);
  1193. return NULL;
  1194. }
  1195. ret = ecp_add(aG, bP, aminus3);
  1196. ec_point_free(aG);
  1197. ec_point_free(bP);
  1198. if (!ecp_normalise(ret)) {
  1199. ec_point_free(ret);
  1200. return NULL;
  1201. }
  1202. return ret;
  1203. }
  1204. static Bignum *ecp_edx(const struct ec_curve *curve, const Bignum y)
  1205. {
  1206. /* Get the x value on the given Edwards curve for a given y */
  1207. Bignum x, xx;
  1208. /* xx = (y^2 - 1) / (d * y^2 + 1) */
  1209. {
  1210. Bignum tmp, tmp2, tmp3;
  1211. tmp = ecf_square(y, curve);
  1212. tmp2 = modmul(curve->e.d, tmp, curve->p);
  1213. tmp3 = ecf_add(tmp2, One, curve);
  1214. freebn(tmp2);
  1215. tmp2 = modinv(tmp3, curve->p);
  1216. freebn(tmp3);
  1217. if (!tmp2) {
  1218. freebn(tmp);
  1219. return NULL;
  1220. }
  1221. tmp3 = modsub(tmp, One, curve->p);
  1222. freebn(tmp);
  1223. xx = modmul(tmp3, tmp2, curve->p);
  1224. freebn(tmp3);
  1225. freebn(tmp2);
  1226. }
  1227. /* x = xx^((p + 3) / 8) */
  1228. {
  1229. Bignum tmp, tmp2;
  1230. tmp = bignum_add_long(curve->p, 3);
  1231. tmp2 = bignum_rshift(tmp, 3);
  1232. freebn(tmp);
  1233. x = modpow(xx, tmp2, curve->p);
  1234. freebn(tmp2);
  1235. }
  1236. /* if x^2 - xx != 0 then x = x*(2^((p - 1) / 4)) */
  1237. {
  1238. Bignum tmp, tmp2;
  1239. tmp = ecf_square(x, curve);
  1240. tmp2 = modsub(tmp, xx, curve->p);
  1241. freebn(tmp);
  1242. freebn(xx);
  1243. if (bignum_cmp(tmp2, Zero)) {
  1244. Bignum tmp3;
  1245. freebn(tmp2);
  1246. tmp = modsub(curve->p, One, curve->p);
  1247. tmp2 = bignum_rshift(tmp, 2);
  1248. freebn(tmp);
  1249. tmp = bignum_from_long(2);
  1250. tmp3 = modpow(tmp, tmp2, curve->p);
  1251. freebn(tmp);
  1252. freebn(tmp2);
  1253. tmp = modmul(x, tmp3, curve->p);
  1254. freebn(x);
  1255. freebn(tmp3);
  1256. x = tmp;
  1257. } else {
  1258. freebn(tmp2);
  1259. }
  1260. }
  1261. /* if x % 2 != 0 then x = p - x */
  1262. if (bignum_bit(x, 0)) {
  1263. Bignum tmp = modsub(curve->p, x, curve->p);
  1264. freebn(x);
  1265. x = tmp;
  1266. }
  1267. return x;
  1268. }
  1269. /* ----------------------------------------------------------------------
  1270. * Public point from private
  1271. */
  1272. struct ec_point *ec_public(const Bignum privateKey, const struct ec_curve *curve)
  1273. {
  1274. if (curve->type == EC_WEIERSTRASS) {
  1275. return ecp_mul(&curve->w.G, privateKey);
  1276. } else if (curve->type == EC_EDWARDS) {
  1277. /* hash = H(sk) (where hash creates 2 * fieldBits)
  1278. * b = fieldBits
  1279. * a = 2^(b-2) + SUM(2^i * h_i) for i = 2 -> b-2
  1280. * publicKey = aB */
  1281. struct ec_point *ret;
  1282. unsigned char hash[512/8];
  1283. Bignum a;
  1284. int i, keylen;
  1285. SHA512_State s;
  1286. SHA512_Init(&s);
  1287. keylen = curve->fieldBits / 8;
  1288. for (i = 0; i < keylen; ++i)
  1289. put_byte(&s, bignum_byte(privateKey, i));
  1290. SHA512_Final(&s, hash);
  1291. /* The second part is simply turning the hash into a Bignum,
  1292. * however the 2^(b-2) bit *must* be set, and the bottom 3
  1293. * bits *must* not be */
  1294. hash[0] &= 0xf8; /* Unset bottom 3 bits (if set) */
  1295. hash[31] &= 0x7f; /* Unset above (b-2) */
  1296. hash[31] |= 0x40; /* Set 2^(b-2) */
  1297. /* Chop off the top part and convert to int */
  1298. a = bignum_from_bytes_le(hash, 32);
  1299. ret = ecp_mul(&curve->e.B, a);
  1300. freebn(a);
  1301. return ret;
  1302. } else {
  1303. return NULL;
  1304. }
  1305. }
  1306. /* ----------------------------------------------------------------------
  1307. * Basic sign and verify routines
  1308. */
  1309. static bool _ecdsa_verify(const struct ec_point *publicKey,
  1310. const unsigned char *data, const int dataLen,
  1311. const Bignum r, const Bignum s)
  1312. {
  1313. int z_bits, n_bits;
  1314. Bignum z;
  1315. bool valid = false;
  1316. if (publicKey->curve->type != EC_WEIERSTRASS) {
  1317. return false;
  1318. }
  1319. /* Sanity checks */
  1320. if (bignum_cmp(r, Zero) == 0 || bignum_cmp(r, publicKey->curve->w.n) >= 0
  1321. || bignum_cmp(s, Zero) == 0 || bignum_cmp(s, publicKey->curve->w.n) >= 0)
  1322. {
  1323. return false;
  1324. }
  1325. /* z = left most bitlen(curve->n) of data */
  1326. z = bignum_from_bytes(data, dataLen);
  1327. n_bits = bignum_bitcount(publicKey->curve->w.n);
  1328. z_bits = bignum_bitcount(z);
  1329. if (z_bits > n_bits)
  1330. {
  1331. Bignum tmp = bignum_rshift(z, z_bits - n_bits);
  1332. freebn(z);
  1333. z = tmp;
  1334. }
  1335. /* Ensure z in range of n */
  1336. {
  1337. Bignum tmp = bigmod(z, publicKey->curve->w.n);
  1338. freebn(z);
  1339. z = tmp;
  1340. }
  1341. /* Calculate signature */
  1342. {
  1343. Bignum w, x, u1, u2;
  1344. struct ec_point *tmp;
  1345. w = modinv(s, publicKey->curve->w.n);
  1346. if (!w) {
  1347. freebn(z);
  1348. return false;
  1349. }
  1350. u1 = modmul(z, w, publicKey->curve->w.n);
  1351. u2 = modmul(r, w, publicKey->curve->w.n);
  1352. freebn(w);
  1353. tmp = ecp_summul(u1, u2, publicKey);
  1354. freebn(u1);
  1355. freebn(u2);
  1356. if (!tmp) {
  1357. freebn(z);
  1358. return false;
  1359. }
  1360. x = bigmod(tmp->x, publicKey->curve->w.n);
  1361. ec_point_free(tmp);
  1362. valid = (bignum_cmp(r, x) == 0);
  1363. freebn(x);
  1364. }
  1365. freebn(z);
  1366. return valid;
  1367. }
  1368. static void _ecdsa_sign(const Bignum privateKey, const struct ec_curve *curve,
  1369. const unsigned char *data, const int dataLen,
  1370. Bignum *r, Bignum *s)
  1371. {
  1372. unsigned char digest[20];
  1373. int z_bits, n_bits;
  1374. Bignum z, k;
  1375. struct ec_point *kG;
  1376. *r = NULL;
  1377. *s = NULL;
  1378. if (curve->type != EC_WEIERSTRASS) {
  1379. return;
  1380. }
  1381. /* z = left most bitlen(curve->n) of data */
  1382. z = bignum_from_bytes(data, dataLen);
  1383. n_bits = bignum_bitcount(curve->w.n);
  1384. z_bits = bignum_bitcount(z);
  1385. if (z_bits > n_bits)
  1386. {
  1387. Bignum tmp;
  1388. tmp = bignum_rshift(z, z_bits - n_bits);
  1389. freebn(z);
  1390. z = tmp;
  1391. }
  1392. /* Generate k between 1 and curve->n, using the same deterministic
  1393. * k generation system we use for conventional DSA. */
  1394. SHA_Simple(data, dataLen, digest);
  1395. k = dss_gen_k("ECDSA deterministic k generator", curve->w.n, privateKey,
  1396. digest, sizeof(digest));
  1397. kG = ecp_mul(&curve->w.G, k);
  1398. if (!kG) {
  1399. freebn(z);
  1400. freebn(k);
  1401. return;
  1402. }
  1403. /* r = kG.x mod n */
  1404. *r = bigmod(kG->x, curve->w.n);
  1405. ec_point_free(kG);
  1406. /* s = (z + r * priv)/k mod n */
  1407. {
  1408. Bignum rPriv, zMod, first, firstMod, kInv;
  1409. rPriv = modmul(*r, privateKey, curve->w.n);
  1410. zMod = bigmod(z, curve->w.n);
  1411. freebn(z);
  1412. first = bigadd(rPriv, zMod);
  1413. freebn(rPriv);
  1414. freebn(zMod);
  1415. firstMod = bigmod(first, curve->w.n);
  1416. freebn(first);
  1417. kInv = modinv(k, curve->w.n);
  1418. freebn(k);
  1419. if (!kInv) {
  1420. freebn(firstMod);
  1421. freebn(*r);
  1422. return;
  1423. }
  1424. *s = modmul(firstMod, kInv, curve->w.n);
  1425. freebn(firstMod);
  1426. freebn(kInv);
  1427. }
  1428. }
  1429. /* ----------------------------------------------------------------------
  1430. * Misc functions
  1431. */
  1432. static Bignum BinarySource_get_mp_le(BinarySource *src)
  1433. {
  1434. ptrlen mp_str = get_string(src);
  1435. return bignum_from_bytes_le(mp_str.ptr, mp_str.len);
  1436. }
  1437. #define get_mp_le(src) BinarySource_get_mp_le(BinarySource_UPCAST(src))
  1438. static bool decodepoint_ed(const char *p, int length, struct ec_point *point)
  1439. {
  1440. /* Got some conversion to do, first read in the y co-ord */
  1441. bool negative;
  1442. point->y = bignum_from_bytes_le((const unsigned char*)p, length);
  1443. if ((unsigned)bignum_bitcount(point->y) > point->curve->fieldBits) {
  1444. freebn(point->y);
  1445. point->y = NULL;
  1446. return false;
  1447. }
  1448. /* Read x bit and then reset it */
  1449. negative = bignum_bit(point->y, point->curve->fieldBits - 1);
  1450. bignum_set_bit(point->y, point->curve->fieldBits - 1, 0);
  1451. bn_restore_invariant(point->y);
  1452. /* Get the x from the y */
  1453. point->x = ecp_edx(point->curve, point->y);
  1454. if (!point->x) {
  1455. freebn(point->y);
  1456. point->y = NULL;
  1457. return false;
  1458. }
  1459. if (negative) {
  1460. Bignum tmp = modsub(point->curve->p, point->x, point->curve->p);
  1461. freebn(point->x);
  1462. point->x = tmp;
  1463. }
  1464. /* Verify the point is on the curve */
  1465. if (!ec_point_verify(point)) {
  1466. freebn(point->x);
  1467. point->x = NULL;
  1468. freebn(point->y);
  1469. point->y = NULL;
  1470. return false;
  1471. }
  1472. return true;
  1473. }
  1474. static bool decodepoint(const char *p, int length, struct ec_point *point)
  1475. {
  1476. if (point->curve->type == EC_EDWARDS) {
  1477. return decodepoint_ed(p, length, point);
  1478. }
  1479. if (length < 1 || p[0] != 0x04) /* Only support uncompressed point */
  1480. return false;
  1481. /* Skip compression flag */
  1482. ++p;
  1483. --length;
  1484. /* The two values must be equal length */
  1485. if (length % 2 != 0) {
  1486. point->x = NULL;
  1487. point->y = NULL;
  1488. point->z = NULL;
  1489. return false;
  1490. }
  1491. length = length / 2;
  1492. point->x = bignum_from_bytes(p, length);
  1493. p += length;
  1494. point->y = bignum_from_bytes(p, length);
  1495. point->z = NULL;
  1496. /* Verify the point is on the curve */
  1497. if (!ec_point_verify(point)) {
  1498. freebn(point->x);
  1499. point->x = NULL;
  1500. freebn(point->y);
  1501. point->y = NULL;
  1502. return false;
  1503. }
  1504. return true;
  1505. }
  1506. static bool BinarySource_get_point(BinarySource *src, struct ec_point *point)
  1507. {
  1508. ptrlen str = get_string(src);
  1509. if (get_err(src)) return false;
  1510. return decodepoint(str.ptr, str.len, point);
  1511. }
  1512. #define get_point(src, pt) BinarySource_get_point(BinarySource_UPCAST(src), pt)
  1513. /* ----------------------------------------------------------------------
  1514. * Exposed ECDSA interface
  1515. */
  1516. struct ecsign_extra {
  1517. struct ec_curve *(*curve)(void);
  1518. const struct ssh_hashalg *hash;
  1519. /* These fields are used by the OpenSSH PEM format importer/exporter */
  1520. const unsigned char *oid;
  1521. int oidlen;
  1522. };
  1523. static void ecdsa_freekey(ssh_key *key)
  1524. {
  1525. struct ec_key *ec;
  1526. if (!key) return;
  1527. ec = container_of(key, struct ec_key, sshk);
  1528. if (ec->publicKey.x)
  1529. freebn(ec->publicKey.x);
  1530. if (ec->publicKey.y)
  1531. freebn(ec->publicKey.y);
  1532. if (ec->publicKey.z)
  1533. freebn(ec->publicKey.z);
  1534. if (ec->privateKey)
  1535. freebn(ec->privateKey);
  1536. sfree(ec);
  1537. }
  1538. static ssh_key *ecdsa_new_pub(const ssh_keyalg *self, ptrlen data)
  1539. {
  1540. const struct ecsign_extra *extra =
  1541. (const struct ecsign_extra *)self->extra;
  1542. BinarySource src[1];
  1543. struct ec_key *ec;
  1544. struct ec_curve *curve;
  1545. BinarySource_BARE_INIT(src, data.ptr, data.len);
  1546. get_string(src);
  1547. curve = extra->curve();
  1548. assert(curve->type == EC_WEIERSTRASS || curve->type == EC_EDWARDS);
  1549. /* Curve name is duplicated for Weierstrass form */
  1550. if (curve->type == EC_WEIERSTRASS) {
  1551. if (!ptrlen_eq_string(get_string(src), curve->name))
  1552. return NULL;
  1553. }
  1554. ec = snew(struct ec_key);
  1555. ec->sshk = self;
  1556. ec->publicKey.curve = curve;
  1557. ec->publicKey.infinity = false;
  1558. ec->publicKey.x = NULL;
  1559. ec->publicKey.y = NULL;
  1560. ec->publicKey.z = NULL;
  1561. ec->privateKey = NULL;
  1562. if (!get_point(src, &ec->publicKey)) {
  1563. ecdsa_freekey(&ec->sshk);
  1564. return NULL;
  1565. }
  1566. if (!ec->publicKey.x || !ec->publicKey.y ||
  1567. bignum_cmp(ec->publicKey.x, curve->p) >= 0 ||
  1568. bignum_cmp(ec->publicKey.y, curve->p) >= 0)
  1569. {
  1570. ecdsa_freekey(&ec->sshk);
  1571. ec = NULL;
  1572. }
  1573. return &ec->sshk;
  1574. }
  1575. static char *ecdsa_cache_str(ssh_key *key)
  1576. {
  1577. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1578. char *p;
  1579. int len, i, pos, nibbles;
  1580. static const char hex[] = "0123456789abcdef";
  1581. if (!ec->publicKey.x || !ec->publicKey.y || !ec->publicKey.curve)
  1582. return NULL;
  1583. len = 4 + 2 + 1; /* 2 x "0x", punctuation, \0 */
  1584. if (ec->publicKey.curve->name)
  1585. len += strlen(ec->publicKey.curve->name); /* Curve name */
  1586. len += 4 * (bignum_bitcount(ec->publicKey.x) + 15) / 16;
  1587. len += 4 * (bignum_bitcount(ec->publicKey.y) + 15) / 16;
  1588. p = snewn(len, char);
  1589. pos = 0;
  1590. if (ec->publicKey.curve->name)
  1591. pos += sprintf(p + pos, "%s,", ec->publicKey.curve->name);
  1592. pos += sprintf(p + pos, "0x");
  1593. nibbles = (3 + bignum_bitcount(ec->publicKey.x)) / 4;
  1594. if (nibbles < 1)
  1595. nibbles = 1;
  1596. for (i = nibbles; i--;) {
  1597. p[pos++] =
  1598. hex[(bignum_byte(ec->publicKey.x, i / 2) >> (4 * (i % 2))) & 0xF];
  1599. }
  1600. pos += sprintf(p + pos, ",0x");
  1601. nibbles = (3 + bignum_bitcount(ec->publicKey.y)) / 4;
  1602. if (nibbles < 1)
  1603. nibbles = 1;
  1604. for (i = nibbles; i--;) {
  1605. p[pos++] =
  1606. hex[(bignum_byte(ec->publicKey.y, i / 2) >> (4 * (i % 2))) & 0xF];
  1607. }
  1608. p[pos] = '\0';
  1609. return p;
  1610. }
  1611. static void ecdsa_public_blob(ssh_key *key, BinarySink *bs)
  1612. {
  1613. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1614. int pointlen;
  1615. int i;
  1616. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1617. /* Edwards compressed form "ssh-ed25519" point y[:-1] + x[0:1] */
  1618. pointlen = ec->publicKey.curve->fieldBits / 8;
  1619. assert(pointlen >= 2);
  1620. put_stringz(bs, ec->sshk->ssh_id);
  1621. put_uint32(bs, pointlen);
  1622. /* Unset last bit of y and set first bit of x in its place */
  1623. for (i = 0; i < pointlen - 1; ++i)
  1624. put_byte(bs, bignum_byte(ec->publicKey.y, i));
  1625. /* Unset last bit of y and set first bit of x in its place */
  1626. put_byte(bs, ((bignum_byte(ec->publicKey.y, i) & 0x7f) |
  1627. (bignum_bit(ec->publicKey.x, 0) << 7)));
  1628. } else if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  1629. assert(ec->publicKey.curve->name);
  1630. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  1631. put_stringz(bs, ec->sshk->ssh_id);
  1632. put_stringz(bs, ec->publicKey.curve->name);
  1633. put_uint32(bs, (2 * pointlen) + 1);
  1634. put_byte(bs, 0x04);
  1635. for (i = pointlen; i--;)
  1636. put_byte(bs, bignum_byte(ec->publicKey.x, i));
  1637. for (i = pointlen; i--;)
  1638. put_byte(bs, bignum_byte(ec->publicKey.y, i));
  1639. } else {
  1640. assert(0 && "Bad key type in ecdsa_public_blob");
  1641. }
  1642. }
  1643. static void ecdsa_private_blob(ssh_key *key, BinarySink *bs)
  1644. {
  1645. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1646. int keylen;
  1647. int i;
  1648. assert(ec->privateKey);
  1649. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1650. /* Unsigned */
  1651. keylen = (bignum_bitcount(ec->privateKey) + 7) / 8;
  1652. } else {
  1653. /* Signed */
  1654. keylen = (bignum_bitcount(ec->privateKey) + 8) / 8;
  1655. }
  1656. put_uint32(bs, keylen);
  1657. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1658. /* Little endian */
  1659. for (i = 0; i < keylen; ++i)
  1660. put_byte(bs, bignum_byte(ec->privateKey, i));
  1661. } else {
  1662. for (i = keylen; i--;)
  1663. put_byte(bs, bignum_byte(ec->privateKey, i));
  1664. }
  1665. }
  1666. static ssh_key *ecdsa_new_priv(const ssh_keyalg *self, ptrlen pub, ptrlen priv)
  1667. {
  1668. BinarySource src[1];
  1669. ssh_key *sshk;
  1670. struct ec_key *ec;
  1671. struct ec_point *publicKey;
  1672. sshk = ecdsa_new_pub(self, pub);
  1673. if (!sshk)
  1674. return NULL;
  1675. ec = container_of(sshk, struct ec_key, sshk);
  1676. BinarySource_BARE_INIT(src, priv.ptr, priv.len);
  1677. if (ec->publicKey.curve->type != EC_WEIERSTRASS
  1678. && ec->publicKey.curve->type != EC_EDWARDS) {
  1679. ecdsa_freekey(&ec->sshk);
  1680. return NULL;
  1681. }
  1682. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1683. ec->privateKey = get_mp_le(src);
  1684. } else {
  1685. ec->privateKey = get_mp_ssh2(src);
  1686. }
  1687. if (!ec->privateKey) {
  1688. ecdsa_freekey(&ec->sshk);
  1689. return NULL;
  1690. }
  1691. /* Check that private key generates public key */
  1692. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1693. if (!publicKey ||
  1694. bignum_cmp(publicKey->x, ec->publicKey.x) ||
  1695. bignum_cmp(publicKey->y, ec->publicKey.y))
  1696. {
  1697. ecdsa_freekey(&ec->sshk);
  1698. ec = NULL;
  1699. }
  1700. ec_point_free(publicKey);
  1701. return &ec->sshk;
  1702. }
  1703. static ssh_key *ed25519_new_priv_openssh(const ssh_keyalg *self,
  1704. BinarySource *src)
  1705. {
  1706. struct ec_key *ec;
  1707. struct ec_point *publicKey;
  1708. ptrlen p, q;
  1709. p = get_string(src);
  1710. q = get_string(src);
  1711. if (get_err(src) || p.len != 32 || q.len != 64)
  1712. return NULL;
  1713. ec = snew(struct ec_key);
  1714. ec->sshk = self;
  1715. ec->publicKey.curve = ec_ed25519();
  1716. ec->publicKey.infinity = false;
  1717. ec->privateKey = NULL;
  1718. ec->publicKey.x = NULL;
  1719. ec->publicKey.z = NULL;
  1720. ec->publicKey.y = NULL;
  1721. if (!decodepoint_ed(p.ptr, p.len, &ec->publicKey))
  1722. {
  1723. ecdsa_freekey(&ec->sshk);
  1724. return NULL;
  1725. }
  1726. ec->privateKey = bignum_from_bytes_le(q.ptr, 32);
  1727. /* Check that private key generates public key */
  1728. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1729. if (!publicKey ||
  1730. bignum_cmp(publicKey->x, ec->publicKey.x) ||
  1731. bignum_cmp(publicKey->y, ec->publicKey.y))
  1732. {
  1733. ecdsa_freekey(&ec->sshk);
  1734. ec = NULL;
  1735. }
  1736. ec_point_free(publicKey);
  1737. /* The OpenSSH format for ed25519 private keys also for some
  1738. * reason encodes an extra copy of the public key in the second
  1739. * half of the secret-key string. Check that that's present and
  1740. * correct as well, otherwise the key we think we've imported
  1741. * won't behave identically to the way OpenSSH would have treated
  1742. * it. */
  1743. if (0 != memcmp((const char *)q.ptr + 32, p.ptr, 32)) {
  1744. ecdsa_freekey(&ec->sshk);
  1745. return NULL;
  1746. }
  1747. return &ec->sshk;
  1748. }
  1749. static void ed25519_openssh_blob(ssh_key *key, BinarySink *bs)
  1750. {
  1751. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1752. strbuf *pub;
  1753. int pointlen;
  1754. int keylen;
  1755. int i;
  1756. assert(ec->publicKey.curve->type == EC_EDWARDS);
  1757. pointlen = (bignum_bitcount(ec->publicKey.y) + 7) / 8;
  1758. keylen = (bignum_bitcount(ec->privateKey) + 7) / 8;
  1759. /* Encode the public point */
  1760. pub = strbuf_new();
  1761. put_uint32(pub, pointlen);
  1762. for (i = 0; i < pointlen - 1; ++i)
  1763. put_byte(pub, bignum_byte(ec->publicKey.y, i));
  1764. /* Unset last bit of y and set first bit of x in its place */
  1765. put_byte(pub, ((bignum_byte(ec->publicKey.y, i) & 0x7f) |
  1766. (bignum_bit(ec->publicKey.x, 0) << 7)));
  1767. put_data(bs, pub->s, pub->len);
  1768. put_uint32(bs, keylen + pointlen);
  1769. for (i = 0; i < keylen; ++i)
  1770. put_byte(bs, bignum_byte(ec->privateKey, i));
  1771. /* Now encode an extra copy of the public point as the second half
  1772. * of the private key string, as the OpenSSH format for some
  1773. * reason requires */
  1774. put_data(bs, pub->s + 4, pub->len - 4);
  1775. strbuf_free(pub);
  1776. }
  1777. static ssh_key *ecdsa_new_priv_openssh(const ssh_keyalg *self,
  1778. BinarySource *src)
  1779. {
  1780. const struct ecsign_extra *extra =
  1781. (const struct ecsign_extra *)self->extra;
  1782. struct ec_key *ec;
  1783. struct ec_curve *curve;
  1784. struct ec_point *publicKey;
  1785. get_string(src);
  1786. curve = extra->curve();
  1787. assert(curve->type == EC_WEIERSTRASS);
  1788. ec = snew(struct ec_key);
  1789. ec->sshk = self;
  1790. ec->publicKey.curve = curve;
  1791. ec->publicKey.infinity = false;
  1792. ec->publicKey.x = NULL;
  1793. ec->publicKey.y = NULL;
  1794. ec->publicKey.z = NULL;
  1795. if (!get_point(src, &ec->publicKey)) {
  1796. ecdsa_freekey(&ec->sshk);
  1797. return NULL;
  1798. }
  1799. ec->privateKey = NULL;
  1800. if (!ec->publicKey.x || !ec->publicKey.y ||
  1801. bignum_cmp(ec->publicKey.x, curve->p) >= 0 ||
  1802. bignum_cmp(ec->publicKey.y, curve->p) >= 0)
  1803. {
  1804. ecdsa_freekey(&ec->sshk);
  1805. return NULL;
  1806. }
  1807. ec->privateKey = get_mp_ssh2(src);
  1808. if (ec->privateKey == NULL)
  1809. {
  1810. ecdsa_freekey(&ec->sshk);
  1811. return NULL;
  1812. }
  1813. /* Now check that the private key makes the public key */
  1814. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1815. if (!publicKey)
  1816. {
  1817. ecdsa_freekey(&ec->sshk);
  1818. return NULL;
  1819. }
  1820. if (bignum_cmp(ec->publicKey.x, publicKey->x) ||
  1821. bignum_cmp(ec->publicKey.y, publicKey->y))
  1822. {
  1823. /* Private key doesn't make the public key on the given curve */
  1824. ecdsa_freekey(&ec->sshk);
  1825. ec_point_free(publicKey);
  1826. return NULL;
  1827. }
  1828. ec_point_free(publicKey);
  1829. return &ec->sshk;
  1830. }
  1831. static void ecdsa_openssh_blob(ssh_key *key, BinarySink *bs)
  1832. {
  1833. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1834. int pointlen;
  1835. int i;
  1836. assert(ec->publicKey.curve->type == EC_WEIERSTRASS);
  1837. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  1838. put_stringz(bs, ec->publicKey.curve->name);
  1839. put_uint32(bs, 1 + (pointlen * 2));
  1840. put_byte(bs, 0x04);
  1841. for (i = pointlen; i--; )
  1842. put_byte(bs, bignum_byte(ec->publicKey.x, i));
  1843. for (i = pointlen; i--; )
  1844. put_byte(bs, bignum_byte(ec->publicKey.y, i));
  1845. put_mp_ssh2(bs, ec->privateKey);
  1846. }
  1847. static int ecdsa_pubkey_bits(const ssh_keyalg *self, ptrlen blob)
  1848. {
  1849. ssh_key *sshk;
  1850. struct ec_key *ec;
  1851. int ret;
  1852. sshk = ecdsa_new_pub(self, blob);
  1853. if (!sshk)
  1854. return -1;
  1855. ec = container_of(sshk, struct ec_key, sshk);
  1856. ret = ec->publicKey.curve->fieldBits;
  1857. ecdsa_freekey(&ec->sshk);
  1858. return ret;
  1859. }
  1860. static bool ecdsa_verify(ssh_key *key, ptrlen sig, ptrlen data)
  1861. {
  1862. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1863. const struct ecsign_extra *extra =
  1864. (const struct ecsign_extra *)ec->sshk->extra;
  1865. BinarySource src[1];
  1866. ptrlen sigstr;
  1867. bool ret;
  1868. if (!ec->publicKey.x || !ec->publicKey.y || !ec->publicKey.curve)
  1869. return false;
  1870. BinarySource_BARE_INIT(src, sig.ptr, sig.len);
  1871. /* Check the signature starts with the algorithm name */
  1872. if (!ptrlen_eq_string(get_string(src), ec->sshk->ssh_id))
  1873. return false;
  1874. sigstr = get_string(src);
  1875. if (get_err(src))
  1876. return false;
  1877. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1878. struct ec_point *r;
  1879. int pointlen = ec->publicKey.curve->fieldBits / 8;
  1880. Bignum s, h;
  1881. /* Check that the signature is two times the length of a point */
  1882. if (sigstr.len != pointlen * 2) {
  1883. return false;
  1884. }
  1885. /* Check it's the 256 bit field so that SHA512 is the correct hash */
  1886. if (ec->publicKey.curve->fieldBits != 256) {
  1887. return false;
  1888. }
  1889. /* Get the signature */
  1890. r = ec_point_new(ec->publicKey.curve, NULL, NULL, NULL, false);
  1891. if (!r) {
  1892. return false;
  1893. }
  1894. if (!decodepoint(sigstr.ptr, pointlen, r)) {
  1895. ec_point_free(r);
  1896. return false;
  1897. }
  1898. s = bignum_from_bytes_le(
  1899. (const char *)sigstr.ptr + pointlen, pointlen);
  1900. /* Get the hash of the encoded value of R + encoded value of pk + message */
  1901. {
  1902. int i;
  1903. unsigned char digest[512 / 8];
  1904. SHA512_State hs;
  1905. SHA512_Init(&hs);
  1906. /* Add encoded r (no need to encode it again, it was in
  1907. * the signature) */
  1908. put_data(&hs, sigstr.ptr, pointlen);
  1909. /* Encode pk and add it */
  1910. for (i = 0; i < pointlen - 1; ++i)
  1911. put_byte(&hs, bignum_byte(ec->publicKey.y, i));
  1912. /* Unset last bit of y and set first bit of x in its place */
  1913. put_byte(&hs, ((bignum_byte(ec->publicKey.y, i) & 0x7f) |
  1914. (bignum_bit(ec->publicKey.x, 0) << 7)));
  1915. /* Add the message itself */
  1916. put_data(&hs, data.ptr, data.len);
  1917. /* Get the hash */
  1918. SHA512_Final(&hs, digest);
  1919. /* Convert to Bignum */
  1920. h = bignum_from_bytes_le(digest, sizeof(digest));
  1921. }
  1922. /* Verify sB == r + h*publicKey */
  1923. {
  1924. struct ec_point *lhs, *rhs, *tmp;
  1925. /* lhs = sB */
  1926. lhs = ecp_mul(&ec->publicKey.curve->e.B, s);
  1927. freebn(s);
  1928. if (!lhs) {
  1929. ec_point_free(r);
  1930. freebn(h);
  1931. return false;
  1932. }
  1933. /* rhs = r + h*publicKey */
  1934. tmp = ecp_mul(&ec->publicKey, h);
  1935. freebn(h);
  1936. if (!tmp) {
  1937. ec_point_free(lhs);
  1938. ec_point_free(r);
  1939. return false;
  1940. }
  1941. rhs = ecp_add(r, tmp, false);
  1942. ec_point_free(r);
  1943. ec_point_free(tmp);
  1944. if (!rhs) {
  1945. ec_point_free(lhs);
  1946. return false;
  1947. }
  1948. /* Check the point is the same */
  1949. ret = !bignum_cmp(lhs->x, rhs->x);
  1950. if (ret) {
  1951. ret = !bignum_cmp(lhs->y, rhs->y);
  1952. if (ret) {
  1953. ret = true;
  1954. }
  1955. }
  1956. ec_point_free(lhs);
  1957. ec_point_free(rhs);
  1958. }
  1959. } else {
  1960. Bignum r, s;
  1961. unsigned char digest[512 / 8];
  1962. int digestLen;
  1963. ssh_hash *hashctx;
  1964. BinarySource_BARE_INIT(src, sigstr.ptr, sigstr.len);
  1965. r = get_mp_ssh2(src);
  1966. s = get_mp_ssh2(src);
  1967. if (get_err(src)) {
  1968. freebn(r);
  1969. freebn(s);
  1970. return false;
  1971. }
  1972. digestLen = extra->hash->hlen;
  1973. assert(digestLen <= sizeof(digest));
  1974. hashctx = ssh_hash_new(extra->hash);
  1975. put_data(hashctx, data.ptr, data.len);
  1976. ssh_hash_final(hashctx, digest);
  1977. /* Verify the signature */
  1978. ret = _ecdsa_verify(&ec->publicKey, digest, digestLen, r, s);
  1979. freebn(r);
  1980. freebn(s);
  1981. }
  1982. return ret;
  1983. }
  1984. static void ecdsa_sign(ssh_key *key, const void *data, int datalen,
  1985. unsigned flags, BinarySink *bs)
  1986. {
  1987. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1988. const struct ecsign_extra *extra =
  1989. (const struct ecsign_extra *)ec->sshk->extra;
  1990. unsigned char digest[512 / 8];
  1991. int digestLen;
  1992. Bignum r = NULL, s = NULL;
  1993. int i;
  1994. assert(ec->privateKey);
  1995. assert(ec->publicKey.curve);
  1996. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1997. struct ec_point *rp;
  1998. int pointlen = ec->publicKey.curve->fieldBits / 8;
  1999. /* hash = H(sk) (where hash creates 2 * fieldBits)
  2000. * b = fieldBits
  2001. * a = 2^(b-2) + SUM(2^i * h_i) for i = 2 -> b-2
  2002. * r = H(h[b/8:b/4] + m)
  2003. * R = rB
  2004. * S = (r + H(encodepoint(R) + encodepoint(pk) + m) * a) % l */
  2005. {
  2006. unsigned char hash[512/8];
  2007. Bignum a;
  2008. SHA512_State hs;
  2009. SHA512_Init(&hs);
  2010. for (i = 0; i < pointlen; ++i)
  2011. put_byte(&hs, bignum_byte(ec->privateKey, i));
  2012. SHA512_Final(&hs, hash);
  2013. /* The second part is simply turning the hash into a
  2014. * Bignum, however the 2^(b-2) bit *must* be set, and the
  2015. * bottom 3 bits *must* not be */
  2016. hash[0] &= 0xf8; /* Unset bottom 3 bits (if set) */
  2017. hash[31] &= 0x7f; /* Unset above (b-2) */
  2018. hash[31] |= 0x40; /* Set 2^(b-2) */
  2019. /* Chop off the top part and convert to int */
  2020. a = bignum_from_bytes_le(hash, 32);
  2021. SHA512_Init(&hs);
  2022. put_data(&hs, hash+(ec->publicKey.curve->fieldBits / 8),
  2023. ((ec->publicKey.curve->fieldBits / 4) -
  2024. (ec->publicKey.curve->fieldBits / 8)));
  2025. put_data(&hs, data, datalen);
  2026. SHA512_Final(&hs, hash);
  2027. r = bignum_from_bytes_le(hash, 512/8);
  2028. rp = ecp_mul(&ec->publicKey.curve->e.B, r);
  2029. assert(rp);
  2030. /* Now calculate s */
  2031. SHA512_Init(&hs);
  2032. /* Encode the point R */
  2033. for (i = 0; i < pointlen - 1; ++i)
  2034. put_byte(&hs, bignum_byte(rp->y, i));
  2035. /* Unset last bit of y and set first bit of x in its place */
  2036. put_byte(&hs, ((bignum_byte(rp->y, i) & 0x7f) |
  2037. (bignum_bit(rp->x, 0) << 7)));
  2038. /* Encode the point pk */
  2039. for (i = 0; i < pointlen - 1; ++i)
  2040. put_byte(&hs, bignum_byte(ec->publicKey.y, i));
  2041. /* Unset last bit of y and set first bit of x in its place */
  2042. put_byte(&hs, ((bignum_byte(ec->publicKey.y, i) & 0x7f) |
  2043. (bignum_bit(ec->publicKey.x, 0) << 7)));
  2044. /* Add the message */
  2045. put_data(&hs, data, datalen);
  2046. SHA512_Final(&hs, hash);
  2047. {
  2048. Bignum tmp, tmp2;
  2049. tmp = bignum_from_bytes_le(hash, 512/8);
  2050. tmp2 = modmul(tmp, a, ec->publicKey.curve->e.l);
  2051. freebn(a);
  2052. freebn(tmp);
  2053. tmp = bigadd(r, tmp2);
  2054. freebn(r);
  2055. freebn(tmp2);
  2056. s = bigmod(tmp, ec->publicKey.curve->e.l);
  2057. freebn(tmp);
  2058. }
  2059. }
  2060. /* Format the output */
  2061. put_stringz(bs, ec->sshk->ssh_id);
  2062. pointlen = ec->publicKey.curve->fieldBits / 8;
  2063. put_uint32(bs, pointlen * 2);
  2064. /* Encode the point */
  2065. for (i = 0; i < pointlen - 1; ++i)
  2066. put_byte(bs, bignum_byte(rp->y, i));
  2067. /* Unset last bit of y and set first bit of x in its place */
  2068. put_byte(bs, ((bignum_byte(rp->y, i) & 0x7f) |
  2069. (bignum_bit(rp->x, 0) << 7)));
  2070. ec_point_free(rp);
  2071. /* Encode the int */
  2072. for (i = 0; i < pointlen; ++i)
  2073. put_byte(bs, bignum_byte(s, i));
  2074. freebn(s);
  2075. } else {
  2076. ssh_hash *hashctx;
  2077. strbuf *substr;
  2078. digestLen = extra->hash->hlen;
  2079. assert(digestLen <= sizeof(digest));
  2080. hashctx = ssh_hash_new(extra->hash);
  2081. put_data(hashctx, data, datalen);
  2082. ssh_hash_final(hashctx, digest);
  2083. /* Do the signature */
  2084. _ecdsa_sign(ec->privateKey, ec->publicKey.curve, digest, digestLen, &r, &s);
  2085. assert(r);
  2086. assert(s);
  2087. /* Format the output */
  2088. put_stringz(bs, ec->sshk->ssh_id);
  2089. substr = strbuf_new();
  2090. put_mp_ssh2(substr, r);
  2091. put_mp_ssh2(substr, s);
  2092. put_stringsb(bs, substr);
  2093. freebn(r);
  2094. freebn(s);
  2095. }
  2096. }
  2097. const struct ecsign_extra sign_extra_ed25519 = {
  2098. ec_ed25519, NULL,
  2099. NULL, 0,
  2100. };
  2101. const ssh_keyalg ssh_ecdsa_ed25519 = {
  2102. ecdsa_new_pub,
  2103. ecdsa_new_priv,
  2104. ed25519_new_priv_openssh,
  2105. ecdsa_freekey,
  2106. ecdsa_sign,
  2107. ecdsa_verify,
  2108. ecdsa_public_blob,
  2109. ecdsa_private_blob,
  2110. ed25519_openssh_blob,
  2111. ecdsa_cache_str,
  2112. ecdsa_pubkey_bits,
  2113. "ssh-ed25519",
  2114. "ssh-ed25519",
  2115. &sign_extra_ed25519,
  2116. 0, /* no supported flags */
  2117. };
  2118. /* OID: 1.2.840.10045.3.1.7 (ansiX9p256r1) */
  2119. static const unsigned char nistp256_oid[] = {
  2120. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  2121. };
  2122. const struct ecsign_extra sign_extra_nistp256 = {
  2123. ec_p256, &ssh_sha256,
  2124. nistp256_oid, lenof(nistp256_oid),
  2125. };
  2126. const ssh_keyalg ssh_ecdsa_nistp256 = {
  2127. ecdsa_new_pub,
  2128. ecdsa_new_priv,
  2129. ecdsa_new_priv_openssh,
  2130. ecdsa_freekey,
  2131. ecdsa_sign,
  2132. ecdsa_verify,
  2133. ecdsa_public_blob,
  2134. ecdsa_private_blob,
  2135. ecdsa_openssh_blob,
  2136. ecdsa_cache_str,
  2137. ecdsa_pubkey_bits,
  2138. "ecdsa-sha2-nistp256",
  2139. "ecdsa-sha2-nistp256",
  2140. &sign_extra_nistp256,
  2141. 0, /* no supported flags */
  2142. };
  2143. /* OID: 1.3.132.0.34 (secp384r1) */
  2144. static const unsigned char nistp384_oid[] = {
  2145. 0x2b, 0x81, 0x04, 0x00, 0x22
  2146. };
  2147. const struct ecsign_extra sign_extra_nistp384 = {
  2148. ec_p384, &ssh_sha384,
  2149. nistp384_oid, lenof(nistp384_oid),
  2150. };
  2151. const ssh_keyalg ssh_ecdsa_nistp384 = {
  2152. ecdsa_new_pub,
  2153. ecdsa_new_priv,
  2154. ecdsa_new_priv_openssh,
  2155. ecdsa_freekey,
  2156. ecdsa_sign,
  2157. ecdsa_verify,
  2158. ecdsa_public_blob,
  2159. ecdsa_private_blob,
  2160. ecdsa_openssh_blob,
  2161. ecdsa_cache_str,
  2162. ecdsa_pubkey_bits,
  2163. "ecdsa-sha2-nistp384",
  2164. "ecdsa-sha2-nistp384",
  2165. &sign_extra_nistp384,
  2166. 0, /* no supported flags */
  2167. };
  2168. /* OID: 1.3.132.0.35 (secp521r1) */
  2169. static const unsigned char nistp521_oid[] = {
  2170. 0x2b, 0x81, 0x04, 0x00, 0x23
  2171. };
  2172. const struct ecsign_extra sign_extra_nistp521 = {
  2173. ec_p521, &ssh_sha512,
  2174. nistp521_oid, lenof(nistp521_oid),
  2175. };
  2176. const ssh_keyalg ssh_ecdsa_nistp521 = {
  2177. ecdsa_new_pub,
  2178. ecdsa_new_priv,
  2179. ecdsa_new_priv_openssh,
  2180. ecdsa_freekey,
  2181. ecdsa_sign,
  2182. ecdsa_verify,
  2183. ecdsa_public_blob,
  2184. ecdsa_private_blob,
  2185. ecdsa_openssh_blob,
  2186. ecdsa_cache_str,
  2187. ecdsa_pubkey_bits,
  2188. "ecdsa-sha2-nistp521",
  2189. "ecdsa-sha2-nistp521",
  2190. &sign_extra_nistp521,
  2191. 0, /* no supported flags */
  2192. };
  2193. /* ----------------------------------------------------------------------
  2194. * Exposed ECDH interface
  2195. */
  2196. struct eckex_extra {
  2197. struct ec_curve *(*curve)(void);
  2198. };
  2199. static Bignum ecdh_calculate(const Bignum private,
  2200. const struct ec_point *public)
  2201. {
  2202. struct ec_point *p;
  2203. Bignum ret;
  2204. p = ecp_mul(public, private);
  2205. if (!p) return NULL;
  2206. ret = p->x;
  2207. p->x = NULL;
  2208. if (p->curve->type == EC_MONTGOMERY) {
  2209. /*
  2210. * Endianness-swap. The Curve25519 algorithm definition
  2211. * assumes you were doing your computation in arrays of 32
  2212. * little-endian bytes, and now specifies that you take your
  2213. * final one of those and convert it into a bignum in
  2214. * _network_ byte order, i.e. big-endian.
  2215. *
  2216. * In particular, the spec says, you convert the _whole_ 32
  2217. * bytes into a bignum. That is, on the rare occasions that
  2218. * p->x has come out with the most significant 8 bits zero, we
  2219. * have to imagine that being represented by a 32-byte string
  2220. * with the last byte being zero, so that has to be converted
  2221. * into an SSH-2 bignum with the _low_ byte zero, i.e. a
  2222. * multiple of 256.
  2223. */
  2224. int i;
  2225. int bytes = (p->curve->fieldBits+7) / 8;
  2226. unsigned char *byteorder = snewn(bytes, unsigned char);
  2227. for (i = 0; i < bytes; ++i) {
  2228. byteorder[i] = bignum_byte(ret, i);
  2229. }
  2230. freebn(ret);
  2231. ret = bignum_from_bytes(byteorder, bytes);
  2232. smemclr(byteorder, bytes);
  2233. sfree(byteorder);
  2234. }
  2235. ec_point_free(p);
  2236. return ret;
  2237. }
  2238. const char *ssh_ecdhkex_curve_textname(const struct ssh_kex *kex)
  2239. {
  2240. const struct eckex_extra *extra = (const struct eckex_extra *)kex->extra;
  2241. struct ec_curve *curve = extra->curve();
  2242. return curve->textname;
  2243. }
  2244. struct ec_key *ssh_ecdhkex_newkey(const struct ssh_kex *kex)
  2245. {
  2246. const struct eckex_extra *extra = (const struct eckex_extra *)kex->extra;
  2247. struct ec_curve *curve;
  2248. struct ec_key *key;
  2249. struct ec_point *publicKey;
  2250. curve = extra->curve();
  2251. key = snew(struct ec_key);
  2252. key->sshk = NULL;
  2253. key->publicKey.curve = curve;
  2254. if (curve->type == EC_MONTGOMERY) {
  2255. unsigned char bytes[32] = {0};
  2256. int i;
  2257. for (i = 0; i < sizeof(bytes); ++i)
  2258. {
  2259. bytes[i] = (unsigned char)random_byte();
  2260. }
  2261. bytes[0] &= 248;
  2262. bytes[31] &= 127;
  2263. bytes[31] |= 64;
  2264. key->privateKey = bignum_from_bytes_le(bytes, sizeof(bytes));
  2265. smemclr(bytes, sizeof(bytes));
  2266. if (!key->privateKey) {
  2267. sfree(key);
  2268. return NULL;
  2269. }
  2270. publicKey = ecp_mul(&key->publicKey.curve->m.G, key->privateKey);
  2271. if (!publicKey) {
  2272. freebn(key->privateKey);
  2273. sfree(key);
  2274. return NULL;
  2275. }
  2276. key->publicKey.x = publicKey->x;
  2277. key->publicKey.y = publicKey->y;
  2278. key->publicKey.z = NULL;
  2279. sfree(publicKey);
  2280. } else {
  2281. key->privateKey = bignum_random_in_range(One, key->publicKey.curve->w.n);
  2282. if (!key->privateKey) {
  2283. sfree(key);
  2284. return NULL;
  2285. }
  2286. publicKey = ecp_mul(&key->publicKey.curve->w.G, key->privateKey);
  2287. if (!publicKey) {
  2288. freebn(key->privateKey);
  2289. sfree(key);
  2290. return NULL;
  2291. }
  2292. key->publicKey.x = publicKey->x;
  2293. key->publicKey.y = publicKey->y;
  2294. key->publicKey.z = NULL;
  2295. sfree(publicKey);
  2296. }
  2297. return key;
  2298. }
  2299. void ssh_ecdhkex_getpublic(struct ec_key *ec, BinarySink *bs)
  2300. {
  2301. int i;
  2302. int pointlen;
  2303. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  2304. if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  2305. put_byte(bs, 0x04);
  2306. for (i = pointlen; i--;)
  2307. put_byte(bs, bignum_byte(ec->publicKey.x, i));
  2308. for (i = pointlen; i--;)
  2309. put_byte(bs, bignum_byte(ec->publicKey.y, i));
  2310. } else {
  2311. for (i = 0; i < pointlen; ++i)
  2312. put_byte(bs, bignum_byte(ec->publicKey.x, i));
  2313. }
  2314. }
  2315. Bignum ssh_ecdhkex_getkey(struct ec_key *ec,
  2316. const void *remoteKey, int remoteKeyLen)
  2317. {
  2318. struct ec_point remote;
  2319. Bignum ret;
  2320. if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  2321. remote.curve = ec->publicKey.curve;
  2322. remote.infinity = false;
  2323. if (!decodepoint(remoteKey, remoteKeyLen, &remote)) {
  2324. return NULL;
  2325. }
  2326. } else {
  2327. /* Point length has to be the same length */
  2328. if (remoteKeyLen != (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8) {
  2329. return NULL;
  2330. }
  2331. remote.curve = ec->publicKey.curve;
  2332. remote.infinity = false;
  2333. remote.x = bignum_from_bytes_le((const unsigned char *)remoteKey,
  2334. remoteKeyLen);
  2335. remote.y = NULL;
  2336. remote.z = NULL;
  2337. }
  2338. ret = ecdh_calculate(ec->privateKey, &remote);
  2339. if (remote.x) freebn(remote.x);
  2340. if (remote.y) freebn(remote.y);
  2341. return ret;
  2342. }
  2343. void ssh_ecdhkex_freekey(struct ec_key *key)
  2344. {
  2345. ecdsa_freekey(&key->sshk);
  2346. }
  2347. static const struct eckex_extra kex_extra_curve25519 = { ec_curve25519 };
  2348. static const struct ssh_kex ssh_ec_kex_curve25519 = {
  2349. "[email protected]", NULL, KEXTYPE_ECDH,
  2350. &ssh_sha256, &kex_extra_curve25519,
  2351. };
  2352. const struct eckex_extra kex_extra_nistp256 = { ec_p256 };
  2353. static const struct ssh_kex ssh_ec_kex_nistp256 = {
  2354. "ecdh-sha2-nistp256", NULL, KEXTYPE_ECDH,
  2355. &ssh_sha256, &kex_extra_nistp256,
  2356. };
  2357. const struct eckex_extra kex_extra_nistp384 = { ec_p384 };
  2358. static const struct ssh_kex ssh_ec_kex_nistp384 = {
  2359. "ecdh-sha2-nistp384", NULL, KEXTYPE_ECDH,
  2360. &ssh_sha384, &kex_extra_nistp384,
  2361. };
  2362. const struct eckex_extra kex_extra_nistp521 = { ec_p521 };
  2363. static const struct ssh_kex ssh_ec_kex_nistp521 = {
  2364. "ecdh-sha2-nistp521", NULL, KEXTYPE_ECDH,
  2365. &ssh_sha512, &kex_extra_nistp521,
  2366. };
  2367. static const struct ssh_kex *const ec_kex_list[] = {
  2368. &ssh_ec_kex_curve25519,
  2369. &ssh_ec_kex_nistp256,
  2370. &ssh_ec_kex_nistp384,
  2371. &ssh_ec_kex_nistp521,
  2372. };
  2373. const struct ssh_kexes ssh_ecdh_kex = {
  2374. sizeof(ec_kex_list) / sizeof(*ec_kex_list),
  2375. ec_kex_list
  2376. };
  2377. /* ----------------------------------------------------------------------
  2378. * Helper functions for finding key algorithms and returning auxiliary
  2379. * data.
  2380. */
  2381. const ssh_keyalg *ec_alg_by_oid(int len, const void *oid,
  2382. const struct ec_curve **curve)
  2383. {
  2384. static const ssh_keyalg *algs_with_oid[] = {
  2385. &ssh_ecdsa_nistp256,
  2386. &ssh_ecdsa_nistp384,
  2387. &ssh_ecdsa_nistp521,
  2388. };
  2389. int i;
  2390. for (i = 0; i < lenof(algs_with_oid); i++) {
  2391. const ssh_keyalg *alg = algs_with_oid[i];
  2392. const struct ecsign_extra *extra =
  2393. (const struct ecsign_extra *)alg->extra;
  2394. if (len == extra->oidlen && !memcmp(oid, extra->oid, len)) {
  2395. *curve = extra->curve();
  2396. return alg;
  2397. }
  2398. }
  2399. return NULL;
  2400. }
  2401. const unsigned char *ec_alg_oid(const ssh_keyalg *alg,
  2402. int *oidlen)
  2403. {
  2404. const struct ecsign_extra *extra = (const struct ecsign_extra *)alg->extra;
  2405. *oidlen = extra->oidlen;
  2406. return extra->oid;
  2407. }
  2408. const int ec_nist_curve_lengths[] = { 256, 384, 521 };
  2409. const int n_ec_nist_curve_lengths = lenof(ec_nist_curve_lengths);
  2410. bool ec_nist_alg_and_curve_by_bits(
  2411. int bits, const struct ec_curve **curve, const ssh_keyalg **alg)
  2412. {
  2413. switch (bits) {
  2414. case 256: *alg = &ssh_ecdsa_nistp256; break;
  2415. case 384: *alg = &ssh_ecdsa_nistp384; break;
  2416. case 521: *alg = &ssh_ecdsa_nistp521; break;
  2417. default: return false;
  2418. }
  2419. *curve = ((struct ecsign_extra *)(*alg)->extra)->curve();
  2420. return true;
  2421. }
  2422. bool ec_ed_alg_and_curve_by_bits(
  2423. int bits, const struct ec_curve **curve, const ssh_keyalg **alg)
  2424. {
  2425. switch (bits) {
  2426. case 256: *alg = &ssh_ecdsa_ed25519; break;
  2427. default: return false;
  2428. }
  2429. *curve = ((struct ecsign_extra *)(*alg)->extra)->curve();
  2430. return true;
  2431. }
  2432. #ifdef MPEXT
  2433. void ec_cleanup(void)
  2434. {
  2435. ec_curve_cleanup = 1;
  2436. ec_p256();
  2437. ec_p384();
  2438. ec_p521();
  2439. ec_curve25519();
  2440. ec_ed25519();
  2441. // in case we want to restart (unlikely)
  2442. ec_curve_cleanup = 0;
  2443. }
  2444. #endif