sshecc.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873
  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 = 0;
  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 = 0;
  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 = 0;
  129. }
  130. static struct ec_curve *ec_p256(void)
  131. {
  132. static struct ec_curve curve = { 0 };
  133. static unsigned char initialised = 0;
  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 = 1;
  184. }
  185. return &curve;
  186. }
  187. static struct ec_curve *ec_p384(void)
  188. {
  189. static struct ec_curve curve = { 0 };
  190. static unsigned char initialised = 0;
  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 = 1;
  253. }
  254. return &curve;
  255. }
  256. static struct ec_curve *ec_p521(void)
  257. {
  258. static struct ec_curve curve = { 0 };
  259. static unsigned char initialised = 0;
  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 = 1;
  340. }
  341. return &curve;
  342. }
  343. static struct ec_curve *ec_curve25519(void)
  344. {
  345. static struct ec_curve curve = { 0 };
  346. static unsigned char initialised = 0;
  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 = 1;
  388. }
  389. return &curve;
  390. }
  391. static struct ec_curve *ec_ed25519(void)
  392. {
  393. static struct ec_curve curve = { 0 };
  394. static unsigned char initialised = 0;
  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 = 1;
  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 int ec_aminus3(const struct ec_curve *curve)
  448. {
  449. int ret;
  450. Bignum _p;
  451. if (curve->type != EC_WEIERSTRASS) {
  452. return 0;
  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 = 0;
  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. unsigned char 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 ? 1 : 0;
  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 int ec_point_verify(const struct ec_point *a)
  547. {
  548. if (a->infinity) {
  549. return 1;
  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. int 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. int ret = 0;
  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) ? 0 : 1;
  588. freebn(lhs);
  589. freebn(rhs);
  590. return ret;
  591. } else {
  592. return 0;
  593. }
  594. }
  595. /* ----------------------------------------------------------------------
  596. * Elliptic curve point maths
  597. */
  598. /* Returns 1 on success and 0 on memory error */
  599. static int ecp_normalise(struct ec_point *a)
  600. {
  601. if (!a) {
  602. /* No point */
  603. return 0;
  604. }
  605. if (a->infinity) {
  606. /* Point is at infinity - i.e. normalised */
  607. return 1;
  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 0;
  616. } else if (!a->z) {
  617. /* Already normalised */
  618. return 1;
  619. }
  620. Z2 = ecf_square(a->z, a->curve);
  621. Z2inv = modinv(Z2, a->curve->p);
  622. if (!Z2inv) {
  623. freebn(Z2);
  624. return 0;
  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 0;
  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 1;
  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 0;
  651. } else if (!a->z) {
  652. /* Already normalised */
  653. return 1;
  654. }
  655. tmp = modinv(a->z, a->curve->p);
  656. if (!tmp) {
  657. return 0;
  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 1;
  666. } else if (a->curve->type == EC_EDWARDS) {
  667. /* Always normalised */
  668. return 1;
  669. } else {
  670. return 0;
  671. }
  672. }
  673. static struct ec_point *ecp_doublew(const struct ec_point *a, const int 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, 1);
  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, 0);
  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, 0);
  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. const int aminus3);
  825. static struct ec_point *ecp_double(const struct ec_point *a, const int aminus3)
  826. {
  827. if (a->infinity)
  828. {
  829. /* Identity */
  830. return ec_point_new(a->curve, NULL, NULL, NULL, 1);
  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. const int 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, 1);
  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, 0);
  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, 0);
  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, 0);
  1050. }
  1051. static struct ec_point *ecp_add(const struct ec_point *a,
  1052. const struct ec_point *b,
  1053. const int 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_(const struct ec_point *a, const Bignum b, int aminus3)
  1072. {
  1073. struct ec_point *A, *ret;
  1074. int bits, i;
  1075. A = ec_point_copy(a);
  1076. ret = ec_point_new(a->curve, NULL, NULL, NULL, 1);
  1077. bits = bignum_bitcount(b);
  1078. for (i = 0; i < bits; ++i)
  1079. {
  1080. if (bignum_bit(b, i))
  1081. {
  1082. struct ec_point *tmp = ecp_add(ret, A, aminus3);
  1083. ec_point_free(ret);
  1084. ret = tmp;
  1085. }
  1086. if (i+1 != bits)
  1087. {
  1088. struct ec_point *tmp = ecp_double(A, aminus3);
  1089. ec_point_free(A);
  1090. A = tmp;
  1091. }
  1092. }
  1093. ec_point_free(A);
  1094. return ret;
  1095. }
  1096. static struct ec_point *ecp_mulw(const struct ec_point *a, const Bignum b)
  1097. {
  1098. struct ec_point *ret = ecp_mul_(a, b, ec_aminus3(a->curve));
  1099. if (!ecp_normalise(ret)) {
  1100. ec_point_free(ret);
  1101. return NULL;
  1102. }
  1103. return ret;
  1104. }
  1105. static struct ec_point *ecp_mule(const struct ec_point *a, const Bignum b)
  1106. {
  1107. int i;
  1108. struct ec_point *ret;
  1109. ret = ec_point_new(a->curve, NULL, NULL, NULL, 1);
  1110. for (i = bignum_bitcount(b); i >= 0 && ret; --i)
  1111. {
  1112. {
  1113. struct ec_point *tmp = ecp_double(ret, 0);
  1114. ec_point_free(ret);
  1115. ret = tmp;
  1116. }
  1117. if (ret && bignum_bit(b, i))
  1118. {
  1119. struct ec_point *tmp = ecp_add(ret, a, 0);
  1120. ec_point_free(ret);
  1121. ret = tmp;
  1122. }
  1123. }
  1124. return ret;
  1125. }
  1126. static struct ec_point *ecp_mulm(const struct ec_point *p, const Bignum n)
  1127. {
  1128. struct ec_point *P1, *P2;
  1129. int bits, i;
  1130. /* P1 <- P and P2 <- [2]P */
  1131. P2 = ecp_double(p, 0);
  1132. P1 = ec_point_copy(p);
  1133. /* for i = bits − 2 down to 0 */
  1134. bits = bignum_bitcount(n);
  1135. for (i = bits - 2; i >= 0; --i)
  1136. {
  1137. if (!bignum_bit(n, i))
  1138. {
  1139. /* P2 <- P1 + P2 */
  1140. struct ec_point *tmp = ecp_addm(P1, P2, p);
  1141. ec_point_free(P2);
  1142. P2 = tmp;
  1143. /* P1 <- [2]P1 */
  1144. tmp = ecp_double(P1, 0);
  1145. ec_point_free(P1);
  1146. P1 = tmp;
  1147. }
  1148. else
  1149. {
  1150. /* P1 <- P1 + P2 */
  1151. struct ec_point *tmp = ecp_addm(P1, P2, p);
  1152. ec_point_free(P1);
  1153. P1 = tmp;
  1154. /* P2 <- [2]P2 */
  1155. tmp = ecp_double(P2, 0);
  1156. ec_point_free(P2);
  1157. P2 = tmp;
  1158. }
  1159. }
  1160. ec_point_free(P2);
  1161. if (!ecp_normalise(P1)) {
  1162. ec_point_free(P1);
  1163. return NULL;
  1164. }
  1165. return P1;
  1166. }
  1167. /* Not static because it is used by sshecdsag.c to generate a new key */
  1168. struct ec_point *ecp_mul(const struct ec_point *a, const Bignum b)
  1169. {
  1170. if (a->curve->type == EC_WEIERSTRASS) {
  1171. return ecp_mulw(a, b);
  1172. } else if (a->curve->type == EC_EDWARDS) {
  1173. return ecp_mule(a, b);
  1174. } else {
  1175. return ecp_mulm(a, b);
  1176. }
  1177. }
  1178. static struct ec_point *ecp_summul(const Bignum a, const Bignum b,
  1179. const struct ec_point *point)
  1180. {
  1181. struct ec_point *aG, *bP, *ret;
  1182. int aminus3;
  1183. if (point->curve->type != EC_WEIERSTRASS) {
  1184. return NULL;
  1185. }
  1186. aminus3 = ec_aminus3(point->curve);
  1187. aG = ecp_mul_(&point->curve->w.G, a, aminus3);
  1188. if (!aG) return NULL;
  1189. bP = ecp_mul_(point, b, aminus3);
  1190. if (!bP) {
  1191. ec_point_free(aG);
  1192. return NULL;
  1193. }
  1194. ret = ecp_add(aG, bP, aminus3);
  1195. ec_point_free(aG);
  1196. ec_point_free(bP);
  1197. if (!ecp_normalise(ret)) {
  1198. ec_point_free(ret);
  1199. return NULL;
  1200. }
  1201. return ret;
  1202. }
  1203. static Bignum *ecp_edx(const struct ec_curve *curve, const Bignum y)
  1204. {
  1205. /* Get the x value on the given Edwards curve for a given y */
  1206. Bignum x, xx;
  1207. /* xx = (y^2 - 1) / (d * y^2 + 1) */
  1208. {
  1209. Bignum tmp, tmp2, tmp3;
  1210. tmp = ecf_square(y, curve);
  1211. tmp2 = modmul(curve->e.d, tmp, curve->p);
  1212. tmp3 = ecf_add(tmp2, One, curve);
  1213. freebn(tmp2);
  1214. tmp2 = modinv(tmp3, curve->p);
  1215. freebn(tmp3);
  1216. if (!tmp2) {
  1217. freebn(tmp);
  1218. return NULL;
  1219. }
  1220. tmp3 = modsub(tmp, One, curve->p);
  1221. freebn(tmp);
  1222. xx = modmul(tmp3, tmp2, curve->p);
  1223. freebn(tmp3);
  1224. freebn(tmp2);
  1225. }
  1226. /* x = xx^((p + 3) / 8) */
  1227. {
  1228. Bignum tmp, tmp2;
  1229. tmp = bignum_add_long(curve->p, 3);
  1230. tmp2 = bignum_rshift(tmp, 3);
  1231. freebn(tmp);
  1232. x = modpow(xx, tmp2, curve->p);
  1233. freebn(tmp2);
  1234. }
  1235. /* if x^2 - xx != 0 then x = x*(2^((p - 1) / 4)) */
  1236. {
  1237. Bignum tmp, tmp2;
  1238. tmp = ecf_square(x, curve);
  1239. tmp2 = modsub(tmp, xx, curve->p);
  1240. freebn(tmp);
  1241. freebn(xx);
  1242. if (bignum_cmp(tmp2, Zero)) {
  1243. Bignum tmp3;
  1244. freebn(tmp2);
  1245. tmp = modsub(curve->p, One, curve->p);
  1246. tmp2 = bignum_rshift(tmp, 2);
  1247. freebn(tmp);
  1248. tmp = bignum_from_long(2);
  1249. tmp3 = modpow(tmp, tmp2, curve->p);
  1250. freebn(tmp);
  1251. freebn(tmp2);
  1252. tmp = modmul(x, tmp3, curve->p);
  1253. freebn(x);
  1254. freebn(tmp3);
  1255. x = tmp;
  1256. } else {
  1257. freebn(tmp2);
  1258. }
  1259. }
  1260. /* if x % 2 != 0 then x = p - x */
  1261. if (bignum_bit(x, 0)) {
  1262. Bignum tmp = modsub(curve->p, x, curve->p);
  1263. freebn(x);
  1264. x = tmp;
  1265. }
  1266. return x;
  1267. }
  1268. /* ----------------------------------------------------------------------
  1269. * Public point from private
  1270. */
  1271. struct ec_point *ec_public(const Bignum privateKey, const struct ec_curve *curve)
  1272. {
  1273. if (curve->type == EC_WEIERSTRASS) {
  1274. return ecp_mul(&curve->w.G, privateKey);
  1275. } else if (curve->type == EC_EDWARDS) {
  1276. /* hash = H(sk) (where hash creates 2 * fieldBits)
  1277. * b = fieldBits
  1278. * a = 2^(b-2) + SUM(2^i * h_i) for i = 2 -> b-2
  1279. * publicKey = aB */
  1280. struct ec_point *ret;
  1281. unsigned char hash[512/8];
  1282. Bignum a;
  1283. int i, keylen;
  1284. SHA512_State s;
  1285. SHA512_Init(&s);
  1286. keylen = curve->fieldBits / 8;
  1287. for (i = 0; i < keylen; ++i)
  1288. put_byte(&s, bignum_byte(privateKey, i));
  1289. SHA512_Final(&s, hash);
  1290. /* The second part is simply turning the hash into a Bignum,
  1291. * however the 2^(b-2) bit *must* be set, and the bottom 3
  1292. * bits *must* not be */
  1293. hash[0] &= 0xf8; /* Unset bottom 3 bits (if set) */
  1294. hash[31] &= 0x7f; /* Unset above (b-2) */
  1295. hash[31] |= 0x40; /* Set 2^(b-2) */
  1296. /* Chop off the top part and convert to int */
  1297. a = bignum_from_bytes_le(hash, 32);
  1298. ret = ecp_mul(&curve->e.B, a);
  1299. freebn(a);
  1300. return ret;
  1301. } else {
  1302. return NULL;
  1303. }
  1304. }
  1305. /* ----------------------------------------------------------------------
  1306. * Basic sign and verify routines
  1307. */
  1308. static int _ecdsa_verify(const struct ec_point *publicKey,
  1309. const unsigned char *data, const int dataLen,
  1310. const Bignum r, const Bignum s)
  1311. {
  1312. int z_bits, n_bits;
  1313. Bignum z;
  1314. int valid = 0;
  1315. if (publicKey->curve->type != EC_WEIERSTRASS) {
  1316. return 0;
  1317. }
  1318. /* Sanity checks */
  1319. if (bignum_cmp(r, Zero) == 0 || bignum_cmp(r, publicKey->curve->w.n) >= 0
  1320. || bignum_cmp(s, Zero) == 0 || bignum_cmp(s, publicKey->curve->w.n) >= 0)
  1321. {
  1322. return 0;
  1323. }
  1324. /* z = left most bitlen(curve->n) of data */
  1325. z = bignum_from_bytes(data, dataLen);
  1326. n_bits = bignum_bitcount(publicKey->curve->w.n);
  1327. z_bits = bignum_bitcount(z);
  1328. if (z_bits > n_bits)
  1329. {
  1330. Bignum tmp = bignum_rshift(z, z_bits - n_bits);
  1331. freebn(z);
  1332. z = tmp;
  1333. }
  1334. /* Ensure z in range of n */
  1335. {
  1336. Bignum tmp = bigmod(z, publicKey->curve->w.n);
  1337. freebn(z);
  1338. z = tmp;
  1339. }
  1340. /* Calculate signature */
  1341. {
  1342. Bignum w, x, u1, u2;
  1343. struct ec_point *tmp;
  1344. w = modinv(s, publicKey->curve->w.n);
  1345. if (!w) {
  1346. freebn(z);
  1347. return 0;
  1348. }
  1349. u1 = modmul(z, w, publicKey->curve->w.n);
  1350. u2 = modmul(r, w, publicKey->curve->w.n);
  1351. freebn(w);
  1352. tmp = ecp_summul(u1, u2, publicKey);
  1353. freebn(u1);
  1354. freebn(u2);
  1355. if (!tmp) {
  1356. freebn(z);
  1357. return 0;
  1358. }
  1359. x = bigmod(tmp->x, publicKey->curve->w.n);
  1360. ec_point_free(tmp);
  1361. valid = (bignum_cmp(r, x) == 0) ? 1 : 0;
  1362. freebn(x);
  1363. }
  1364. freebn(z);
  1365. return valid;
  1366. }
  1367. static void _ecdsa_sign(const Bignum privateKey, const struct ec_curve *curve,
  1368. const unsigned char *data, const int dataLen,
  1369. Bignum *r, Bignum *s)
  1370. {
  1371. unsigned char digest[20];
  1372. int z_bits, n_bits;
  1373. Bignum z, k;
  1374. struct ec_point *kG;
  1375. *r = NULL;
  1376. *s = NULL;
  1377. if (curve->type != EC_WEIERSTRASS) {
  1378. return;
  1379. }
  1380. /* z = left most bitlen(curve->n) of data */
  1381. z = bignum_from_bytes(data, dataLen);
  1382. n_bits = bignum_bitcount(curve->w.n);
  1383. z_bits = bignum_bitcount(z);
  1384. if (z_bits > n_bits)
  1385. {
  1386. Bignum tmp;
  1387. tmp = bignum_rshift(z, z_bits - n_bits);
  1388. freebn(z);
  1389. z = tmp;
  1390. }
  1391. /* Generate k between 1 and curve->n, using the same deterministic
  1392. * k generation system we use for conventional DSA. */
  1393. SHA_Simple(data, dataLen, digest);
  1394. k = dss_gen_k("ECDSA deterministic k generator", curve->w.n, privateKey,
  1395. digest, sizeof(digest));
  1396. kG = ecp_mul(&curve->w.G, k);
  1397. if (!kG) {
  1398. freebn(z);
  1399. freebn(k);
  1400. return;
  1401. }
  1402. /* r = kG.x mod n */
  1403. *r = bigmod(kG->x, curve->w.n);
  1404. ec_point_free(kG);
  1405. /* s = (z + r * priv)/k mod n */
  1406. {
  1407. Bignum rPriv, zMod, first, firstMod, kInv;
  1408. rPriv = modmul(*r, privateKey, curve->w.n);
  1409. zMod = bigmod(z, curve->w.n);
  1410. freebn(z);
  1411. first = bigadd(rPriv, zMod);
  1412. freebn(rPriv);
  1413. freebn(zMod);
  1414. firstMod = bigmod(first, curve->w.n);
  1415. freebn(first);
  1416. kInv = modinv(k, curve->w.n);
  1417. freebn(k);
  1418. if (!kInv) {
  1419. freebn(firstMod);
  1420. freebn(*r);
  1421. return;
  1422. }
  1423. *s = modmul(firstMod, kInv, curve->w.n);
  1424. freebn(firstMod);
  1425. freebn(kInv);
  1426. }
  1427. }
  1428. /* ----------------------------------------------------------------------
  1429. * Misc functions
  1430. */
  1431. static Bignum BinarySource_get_mp_le(BinarySource *src)
  1432. {
  1433. ptrlen mp_str = get_string(src);
  1434. return bignum_from_bytes_le(mp_str.ptr, mp_str.len);
  1435. }
  1436. #define get_mp_le(src) BinarySource_get_mp_le(BinarySource_UPCAST(src))
  1437. static int decodepoint_ed(const char *p, int length, struct ec_point *point)
  1438. {
  1439. /* Got some conversion to do, first read in the y co-ord */
  1440. int negative;
  1441. point->y = bignum_from_bytes_le((const unsigned char*)p, length);
  1442. if ((unsigned)bignum_bitcount(point->y) > point->curve->fieldBits) {
  1443. freebn(point->y);
  1444. point->y = NULL;
  1445. return 0;
  1446. }
  1447. /* Read x bit and then reset it */
  1448. negative = bignum_bit(point->y, point->curve->fieldBits - 1);
  1449. bignum_set_bit(point->y, point->curve->fieldBits - 1, 0);
  1450. bn_restore_invariant(point->y);
  1451. /* Get the x from the y */
  1452. point->x = ecp_edx(point->curve, point->y);
  1453. if (!point->x) {
  1454. freebn(point->y);
  1455. point->y = NULL;
  1456. return 0;
  1457. }
  1458. if (negative) {
  1459. Bignum tmp = modsub(point->curve->p, point->x, point->curve->p);
  1460. freebn(point->x);
  1461. point->x = tmp;
  1462. }
  1463. /* Verify the point is on the curve */
  1464. if (!ec_point_verify(point)) {
  1465. freebn(point->x);
  1466. point->x = NULL;
  1467. freebn(point->y);
  1468. point->y = NULL;
  1469. return 0;
  1470. }
  1471. return 1;
  1472. }
  1473. static int decodepoint(const char *p, int length, struct ec_point *point)
  1474. {
  1475. if (point->curve->type == EC_EDWARDS) {
  1476. return decodepoint_ed(p, length, point);
  1477. }
  1478. if (length < 1 || p[0] != 0x04) /* Only support uncompressed point */
  1479. return 0;
  1480. /* Skip compression flag */
  1481. ++p;
  1482. --length;
  1483. /* The two values must be equal length */
  1484. if (length % 2 != 0) {
  1485. point->x = NULL;
  1486. point->y = NULL;
  1487. point->z = NULL;
  1488. return 0;
  1489. }
  1490. length = length / 2;
  1491. point->x = bignum_from_bytes(p, length);
  1492. p += length;
  1493. point->y = bignum_from_bytes(p, length);
  1494. point->z = NULL;
  1495. /* Verify the point is on the curve */
  1496. if (!ec_point_verify(point)) {
  1497. freebn(point->x);
  1498. point->x = NULL;
  1499. freebn(point->y);
  1500. point->y = NULL;
  1501. return 0;
  1502. }
  1503. return 1;
  1504. }
  1505. static int BinarySource_get_point(BinarySource *src, struct ec_point *point)
  1506. {
  1507. ptrlen str = get_string(src);
  1508. if (get_err(src)) return 0;
  1509. return decodepoint(str.ptr, str.len, point);
  1510. }
  1511. #define get_point(src, pt) BinarySource_get_point(BinarySource_UPCAST(src), pt)
  1512. /* ----------------------------------------------------------------------
  1513. * Exposed ECDSA interface
  1514. */
  1515. struct ecsign_extra {
  1516. struct ec_curve *(*curve)(void);
  1517. const struct ssh_hashalg *hash;
  1518. /* These fields are used by the OpenSSH PEM format importer/exporter */
  1519. const unsigned char *oid;
  1520. int oidlen;
  1521. };
  1522. static void ecdsa_freekey(ssh_key *key)
  1523. {
  1524. struct ec_key *ec;
  1525. if (!key) return;
  1526. ec = container_of(key, struct ec_key, sshk);
  1527. if (ec->publicKey.x)
  1528. freebn(ec->publicKey.x);
  1529. if (ec->publicKey.y)
  1530. freebn(ec->publicKey.y);
  1531. if (ec->publicKey.z)
  1532. freebn(ec->publicKey.z);
  1533. if (ec->privateKey)
  1534. freebn(ec->privateKey);
  1535. sfree(ec);
  1536. }
  1537. static ssh_key *ecdsa_new_pub(const ssh_keyalg *self, ptrlen data)
  1538. {
  1539. const struct ecsign_extra *extra =
  1540. (const struct ecsign_extra *)self->extra;
  1541. BinarySource src[1];
  1542. struct ec_key *ec;
  1543. struct ec_curve *curve;
  1544. BinarySource_BARE_INIT(src, data.ptr, data.len);
  1545. get_string(src);
  1546. curve = extra->curve();
  1547. assert(curve->type == EC_WEIERSTRASS || curve->type == EC_EDWARDS);
  1548. /* Curve name is duplicated for Weierstrass form */
  1549. if (curve->type == EC_WEIERSTRASS) {
  1550. if (!ptrlen_eq_string(get_string(src), curve->name))
  1551. return NULL;
  1552. }
  1553. ec = snew(struct ec_key);
  1554. ec->sshk = self;
  1555. ec->publicKey.curve = curve;
  1556. ec->publicKey.infinity = 0;
  1557. ec->publicKey.x = NULL;
  1558. ec->publicKey.y = NULL;
  1559. ec->publicKey.z = NULL;
  1560. ec->privateKey = NULL;
  1561. if (!get_point(src, &ec->publicKey)) {
  1562. ecdsa_freekey(&ec->sshk);
  1563. return NULL;
  1564. }
  1565. if (!ec->publicKey.x || !ec->publicKey.y ||
  1566. bignum_cmp(ec->publicKey.x, curve->p) >= 0 ||
  1567. bignum_cmp(ec->publicKey.y, curve->p) >= 0)
  1568. {
  1569. ecdsa_freekey(&ec->sshk);
  1570. ec = NULL;
  1571. }
  1572. return &ec->sshk;
  1573. }
  1574. static char *ecdsa_cache_str(ssh_key *key)
  1575. {
  1576. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1577. char *p;
  1578. int len, i, pos, nibbles;
  1579. static const char hex[] = "0123456789abcdef";
  1580. if (!ec->publicKey.x || !ec->publicKey.y || !ec->publicKey.curve)
  1581. return NULL;
  1582. len = 4 + 2 + 1; /* 2 x "0x", punctuation, \0 */
  1583. if (ec->publicKey.curve->name)
  1584. len += strlen(ec->publicKey.curve->name); /* Curve name */
  1585. len += 4 * (bignum_bitcount(ec->publicKey.x) + 15) / 16;
  1586. len += 4 * (bignum_bitcount(ec->publicKey.y) + 15) / 16;
  1587. p = snewn(len, char);
  1588. pos = 0;
  1589. if (ec->publicKey.curve->name)
  1590. pos += sprintf(p + pos, "%s,", ec->publicKey.curve->name);
  1591. pos += sprintf(p + pos, "0x");
  1592. nibbles = (3 + bignum_bitcount(ec->publicKey.x)) / 4;
  1593. if (nibbles < 1)
  1594. nibbles = 1;
  1595. for (i = nibbles; i--;) {
  1596. p[pos++] =
  1597. hex[(bignum_byte(ec->publicKey.x, i / 2) >> (4 * (i % 2))) & 0xF];
  1598. }
  1599. pos += sprintf(p + pos, ",0x");
  1600. nibbles = (3 + bignum_bitcount(ec->publicKey.y)) / 4;
  1601. if (nibbles < 1)
  1602. nibbles = 1;
  1603. for (i = nibbles; i--;) {
  1604. p[pos++] =
  1605. hex[(bignum_byte(ec->publicKey.y, i / 2) >> (4 * (i % 2))) & 0xF];
  1606. }
  1607. p[pos] = '\0';
  1608. return p;
  1609. }
  1610. static void ecdsa_public_blob(ssh_key *key, BinarySink *bs)
  1611. {
  1612. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1613. int pointlen;
  1614. int i;
  1615. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1616. /* Edwards compressed form "ssh-ed25519" point y[:-1] + x[0:1] */
  1617. pointlen = ec->publicKey.curve->fieldBits / 8;
  1618. assert(pointlen >= 2);
  1619. put_stringz(bs, ec->sshk->ssh_id);
  1620. put_uint32(bs, pointlen);
  1621. /* Unset last bit of y and set first bit of x in its place */
  1622. for (i = 0; i < pointlen - 1; ++i)
  1623. put_byte(bs, bignum_byte(ec->publicKey.y, i));
  1624. /* Unset last bit of y and set first bit of x in its place */
  1625. put_byte(bs, ((bignum_byte(ec->publicKey.y, i) & 0x7f) |
  1626. (bignum_bit(ec->publicKey.x, 0) << 7)));
  1627. } else if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  1628. assert(ec->publicKey.curve->name);
  1629. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  1630. put_stringz(bs, ec->sshk->ssh_id);
  1631. put_stringz(bs, ec->publicKey.curve->name);
  1632. put_uint32(bs, (2 * pointlen) + 1);
  1633. put_byte(bs, 0x04);
  1634. for (i = pointlen; i--;)
  1635. put_byte(bs, bignum_byte(ec->publicKey.x, i));
  1636. for (i = pointlen; i--;)
  1637. put_byte(bs, bignum_byte(ec->publicKey.y, i));
  1638. } else {
  1639. assert(0 && "Bad key type in ecdsa_public_blob");
  1640. }
  1641. }
  1642. static void ecdsa_private_blob(ssh_key *key, BinarySink *bs)
  1643. {
  1644. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1645. int keylen;
  1646. int i;
  1647. assert(ec->privateKey);
  1648. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1649. /* Unsigned */
  1650. keylen = (bignum_bitcount(ec->privateKey) + 7) / 8;
  1651. } else {
  1652. /* Signed */
  1653. keylen = (bignum_bitcount(ec->privateKey) + 8) / 8;
  1654. }
  1655. put_uint32(bs, keylen);
  1656. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1657. /* Little endian */
  1658. for (i = 0; i < keylen; ++i)
  1659. put_byte(bs, bignum_byte(ec->privateKey, i));
  1660. } else {
  1661. for (i = keylen; i--;)
  1662. put_byte(bs, bignum_byte(ec->privateKey, i));
  1663. }
  1664. }
  1665. static ssh_key *ecdsa_new_priv(const ssh_keyalg *self, ptrlen pub, ptrlen priv)
  1666. {
  1667. BinarySource src[1];
  1668. ssh_key *sshk;
  1669. struct ec_key *ec;
  1670. struct ec_point *publicKey;
  1671. sshk = ecdsa_new_pub(self, pub);
  1672. if (!sshk)
  1673. return NULL;
  1674. ec = container_of(sshk, struct ec_key, sshk);
  1675. BinarySource_BARE_INIT(src, priv.ptr, priv.len);
  1676. if (ec->publicKey.curve->type != EC_WEIERSTRASS
  1677. && ec->publicKey.curve->type != EC_EDWARDS) {
  1678. ecdsa_freekey(&ec->sshk);
  1679. return NULL;
  1680. }
  1681. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1682. ec->privateKey = get_mp_le(src);
  1683. } else {
  1684. ec->privateKey = get_mp_ssh2(src);
  1685. }
  1686. if (!ec->privateKey) {
  1687. ecdsa_freekey(&ec->sshk);
  1688. return NULL;
  1689. }
  1690. /* Check that private key generates public key */
  1691. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1692. if (!publicKey ||
  1693. bignum_cmp(publicKey->x, ec->publicKey.x) ||
  1694. bignum_cmp(publicKey->y, ec->publicKey.y))
  1695. {
  1696. ecdsa_freekey(&ec->sshk);
  1697. ec = NULL;
  1698. }
  1699. ec_point_free(publicKey);
  1700. return &ec->sshk;
  1701. }
  1702. static ssh_key *ed25519_new_priv_openssh(const ssh_keyalg *self,
  1703. BinarySource *src)
  1704. {
  1705. struct ec_key *ec;
  1706. struct ec_point *publicKey;
  1707. ptrlen p, q;
  1708. p = get_string(src);
  1709. q = get_string(src);
  1710. if (get_err(src) || p.len != 32 || q.len != 64)
  1711. return NULL;
  1712. ec = snew(struct ec_key);
  1713. ec->sshk = self;
  1714. ec->publicKey.curve = ec_ed25519();
  1715. ec->publicKey.infinity = 0;
  1716. ec->privateKey = NULL;
  1717. ec->publicKey.x = NULL;
  1718. ec->publicKey.z = NULL;
  1719. ec->publicKey.y = NULL;
  1720. if (!decodepoint_ed(p.ptr, p.len, &ec->publicKey))
  1721. {
  1722. ecdsa_freekey(&ec->sshk);
  1723. return NULL;
  1724. }
  1725. ec->privateKey = bignum_from_bytes_le(q.ptr, 32);
  1726. /* Check that private key generates public key */
  1727. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1728. if (!publicKey ||
  1729. bignum_cmp(publicKey->x, ec->publicKey.x) ||
  1730. bignum_cmp(publicKey->y, ec->publicKey.y))
  1731. {
  1732. ecdsa_freekey(&ec->sshk);
  1733. ec = NULL;
  1734. }
  1735. ec_point_free(publicKey);
  1736. /* The OpenSSH format for ed25519 private keys also for some
  1737. * reason encodes an extra copy of the public key in the second
  1738. * half of the secret-key string. Check that that's present and
  1739. * correct as well, otherwise the key we think we've imported
  1740. * won't behave identically to the way OpenSSH would have treated
  1741. * it. */
  1742. if (0 != memcmp((const char *)q.ptr + 32, p.ptr, 32)) {
  1743. ecdsa_freekey(&ec->sshk);
  1744. return NULL;
  1745. }
  1746. return &ec->sshk;
  1747. }
  1748. static void ed25519_openssh_blob(ssh_key *key, BinarySink *bs)
  1749. {
  1750. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1751. strbuf *pub;
  1752. int pointlen;
  1753. int keylen;
  1754. int i;
  1755. assert(ec->publicKey.curve->type == EC_EDWARDS);
  1756. pointlen = (bignum_bitcount(ec->publicKey.y) + 7) / 8;
  1757. keylen = (bignum_bitcount(ec->privateKey) + 7) / 8;
  1758. /* Encode the public point */
  1759. pub = strbuf_new();
  1760. put_uint32(pub, pointlen);
  1761. for (i = 0; i < pointlen - 1; ++i)
  1762. put_byte(pub, bignum_byte(ec->publicKey.y, i));
  1763. /* Unset last bit of y and set first bit of x in its place */
  1764. put_byte(pub, ((bignum_byte(ec->publicKey.y, i) & 0x7f) |
  1765. (bignum_bit(ec->publicKey.x, 0) << 7)));
  1766. put_data(bs, pub->s, pub->len);
  1767. put_uint32(bs, keylen + pointlen);
  1768. for (i = 0; i < keylen; ++i)
  1769. put_byte(bs, bignum_byte(ec->privateKey, i));
  1770. /* Now encode an extra copy of the public point as the second half
  1771. * of the private key string, as the OpenSSH format for some
  1772. * reason requires */
  1773. put_data(bs, pub->s + 4, pub->len - 4);
  1774. strbuf_free(pub);
  1775. }
  1776. static ssh_key *ecdsa_new_priv_openssh(const ssh_keyalg *self,
  1777. BinarySource *src)
  1778. {
  1779. const struct ecsign_extra *extra =
  1780. (const struct ecsign_extra *)self->extra;
  1781. struct ec_key *ec;
  1782. struct ec_curve *curve;
  1783. struct ec_point *publicKey;
  1784. get_string(src);
  1785. curve = extra->curve();
  1786. assert(curve->type == EC_WEIERSTRASS);
  1787. ec = snew(struct ec_key);
  1788. ec->sshk = self;
  1789. ec->publicKey.curve = curve;
  1790. ec->publicKey.infinity = 0;
  1791. ec->publicKey.x = NULL;
  1792. ec->publicKey.y = NULL;
  1793. ec->publicKey.z = NULL;
  1794. if (!get_point(src, &ec->publicKey)) {
  1795. ecdsa_freekey(&ec->sshk);
  1796. return NULL;
  1797. }
  1798. ec->privateKey = NULL;
  1799. if (!ec->publicKey.x || !ec->publicKey.y ||
  1800. bignum_cmp(ec->publicKey.x, curve->p) >= 0 ||
  1801. bignum_cmp(ec->publicKey.y, curve->p) >= 0)
  1802. {
  1803. ecdsa_freekey(&ec->sshk);
  1804. return NULL;
  1805. }
  1806. ec->privateKey = get_mp_ssh2(src);
  1807. if (ec->privateKey == NULL)
  1808. {
  1809. ecdsa_freekey(&ec->sshk);
  1810. return NULL;
  1811. }
  1812. /* Now check that the private key makes the public key */
  1813. publicKey = ec_public(ec->privateKey, ec->publicKey.curve);
  1814. if (!publicKey)
  1815. {
  1816. ecdsa_freekey(&ec->sshk);
  1817. return NULL;
  1818. }
  1819. if (bignum_cmp(ec->publicKey.x, publicKey->x) ||
  1820. bignum_cmp(ec->publicKey.y, publicKey->y))
  1821. {
  1822. /* Private key doesn't make the public key on the given curve */
  1823. ecdsa_freekey(&ec->sshk);
  1824. ec_point_free(publicKey);
  1825. return NULL;
  1826. }
  1827. ec_point_free(publicKey);
  1828. return &ec->sshk;
  1829. }
  1830. static void ecdsa_openssh_blob(ssh_key *key, BinarySink *bs)
  1831. {
  1832. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1833. int pointlen;
  1834. int i;
  1835. assert(ec->publicKey.curve->type == EC_WEIERSTRASS);
  1836. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  1837. put_stringz(bs, ec->publicKey.curve->name);
  1838. put_uint32(bs, 1 + (pointlen * 2));
  1839. put_byte(bs, 0x04);
  1840. for (i = pointlen; i--; )
  1841. put_byte(bs, bignum_byte(ec->publicKey.x, i));
  1842. for (i = pointlen; i--; )
  1843. put_byte(bs, bignum_byte(ec->publicKey.y, i));
  1844. put_mp_ssh2(bs, ec->privateKey);
  1845. }
  1846. static int ecdsa_pubkey_bits(const ssh_keyalg *self, ptrlen blob)
  1847. {
  1848. ssh_key *sshk;
  1849. struct ec_key *ec;
  1850. int ret;
  1851. sshk = ecdsa_new_pub(self, blob);
  1852. if (!sshk)
  1853. return -1;
  1854. ec = container_of(sshk, struct ec_key, sshk);
  1855. ret = ec->publicKey.curve->fieldBits;
  1856. ecdsa_freekey(&ec->sshk);
  1857. return ret;
  1858. }
  1859. static int ecdsa_verify(ssh_key *key, ptrlen sig, ptrlen data)
  1860. {
  1861. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1862. const struct ecsign_extra *extra =
  1863. (const struct ecsign_extra *)ec->sshk->extra;
  1864. BinarySource src[1];
  1865. ptrlen sigstr;
  1866. int ret;
  1867. if (!ec->publicKey.x || !ec->publicKey.y || !ec->publicKey.curve)
  1868. return 0;
  1869. BinarySource_BARE_INIT(src, sig.ptr, sig.len);
  1870. /* Check the signature starts with the algorithm name */
  1871. if (!ptrlen_eq_string(get_string(src), ec->sshk->ssh_id))
  1872. return 0;
  1873. sigstr = get_string(src);
  1874. if (get_err(src))
  1875. return 0;
  1876. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1877. struct ec_point *r;
  1878. int pointlen = ec->publicKey.curve->fieldBits / 8;
  1879. Bignum s, h;
  1880. /* Check that the signature is two times the length of a point */
  1881. if (sigstr.len != pointlen * 2) {
  1882. return 0;
  1883. }
  1884. /* Check it's the 256 bit field so that SHA512 is the correct hash */
  1885. if (ec->publicKey.curve->fieldBits != 256) {
  1886. return 0;
  1887. }
  1888. /* Get the signature */
  1889. r = ec_point_new(ec->publicKey.curve, NULL, NULL, NULL, 0);
  1890. if (!r) {
  1891. return 0;
  1892. }
  1893. if (!decodepoint(sigstr.ptr, pointlen, r)) {
  1894. ec_point_free(r);
  1895. return 0;
  1896. }
  1897. s = bignum_from_bytes_le(
  1898. (const char *)sigstr.ptr + pointlen, pointlen);
  1899. /* Get the hash of the encoded value of R + encoded value of pk + message */
  1900. {
  1901. int i;
  1902. unsigned char digest[512 / 8];
  1903. SHA512_State hs;
  1904. SHA512_Init(&hs);
  1905. /* Add encoded r (no need to encode it again, it was in
  1906. * the signature) */
  1907. put_data(&hs, sigstr.ptr, pointlen);
  1908. /* Encode pk and add it */
  1909. for (i = 0; i < pointlen - 1; ++i)
  1910. put_byte(&hs, bignum_byte(ec->publicKey.y, i));
  1911. /* Unset last bit of y and set first bit of x in its place */
  1912. put_byte(&hs, ((bignum_byte(ec->publicKey.y, i) & 0x7f) |
  1913. (bignum_bit(ec->publicKey.x, 0) << 7)));
  1914. /* Add the message itself */
  1915. put_data(&hs, data.ptr, data.len);
  1916. /* Get the hash */
  1917. SHA512_Final(&hs, digest);
  1918. /* Convert to Bignum */
  1919. h = bignum_from_bytes_le(digest, sizeof(digest));
  1920. }
  1921. /* Verify sB == r + h*publicKey */
  1922. {
  1923. struct ec_point *lhs, *rhs, *tmp;
  1924. /* lhs = sB */
  1925. lhs = ecp_mul(&ec->publicKey.curve->e.B, s);
  1926. freebn(s);
  1927. if (!lhs) {
  1928. ec_point_free(r);
  1929. freebn(h);
  1930. return 0;
  1931. }
  1932. /* rhs = r + h*publicKey */
  1933. tmp = ecp_mul(&ec->publicKey, h);
  1934. freebn(h);
  1935. if (!tmp) {
  1936. ec_point_free(lhs);
  1937. ec_point_free(r);
  1938. return 0;
  1939. }
  1940. rhs = ecp_add(r, tmp, 0);
  1941. ec_point_free(r);
  1942. ec_point_free(tmp);
  1943. if (!rhs) {
  1944. ec_point_free(lhs);
  1945. return 0;
  1946. }
  1947. /* Check the point is the same */
  1948. ret = !bignum_cmp(lhs->x, rhs->x);
  1949. if (ret) {
  1950. ret = !bignum_cmp(lhs->y, rhs->y);
  1951. if (ret) {
  1952. ret = 1;
  1953. }
  1954. }
  1955. ec_point_free(lhs);
  1956. ec_point_free(rhs);
  1957. }
  1958. } else {
  1959. Bignum r, s;
  1960. unsigned char digest[512 / 8];
  1961. int digestLen;
  1962. ssh_hash *hashctx;
  1963. BinarySource_BARE_INIT(src, sigstr.ptr, sigstr.len);
  1964. r = get_mp_ssh2(src);
  1965. s = get_mp_ssh2(src);
  1966. if (get_err(src)) {
  1967. freebn(r);
  1968. freebn(s);
  1969. return 0;
  1970. }
  1971. digestLen = extra->hash->hlen;
  1972. assert(digestLen <= sizeof(digest));
  1973. hashctx = ssh_hash_new(extra->hash);
  1974. put_data(hashctx, data.ptr, data.len);
  1975. ssh_hash_final(hashctx, digest);
  1976. /* Verify the signature */
  1977. ret = _ecdsa_verify(&ec->publicKey, digest, digestLen, r, s);
  1978. freebn(r);
  1979. freebn(s);
  1980. }
  1981. return ret;
  1982. }
  1983. static void ecdsa_sign(ssh_key *key, const void *data, int datalen,
  1984. BinarySink *bs)
  1985. {
  1986. struct ec_key *ec = container_of(key, struct ec_key, sshk);
  1987. const struct ecsign_extra *extra =
  1988. (const struct ecsign_extra *)ec->sshk->extra;
  1989. unsigned char digest[512 / 8];
  1990. int digestLen;
  1991. Bignum r = NULL, s = NULL;
  1992. int i;
  1993. assert(ec->privateKey);
  1994. assert(ec->publicKey.curve);
  1995. if (ec->publicKey.curve->type == EC_EDWARDS) {
  1996. struct ec_point *rp;
  1997. int pointlen = ec->publicKey.curve->fieldBits / 8;
  1998. /* hash = H(sk) (where hash creates 2 * fieldBits)
  1999. * b = fieldBits
  2000. * a = 2^(b-2) + SUM(2^i * h_i) for i = 2 -> b-2
  2001. * r = H(h[b/8:b/4] + m)
  2002. * R = rB
  2003. * S = (r + H(encodepoint(R) + encodepoint(pk) + m) * a) % l */
  2004. {
  2005. unsigned char hash[512/8];
  2006. Bignum a;
  2007. SHA512_State hs;
  2008. SHA512_Init(&hs);
  2009. for (i = 0; i < pointlen; ++i)
  2010. put_byte(&hs, bignum_byte(ec->privateKey, i));
  2011. SHA512_Final(&hs, hash);
  2012. /* The second part is simply turning the hash into a
  2013. * Bignum, however the 2^(b-2) bit *must* be set, and the
  2014. * bottom 3 bits *must* not be */
  2015. hash[0] &= 0xf8; /* Unset bottom 3 bits (if set) */
  2016. hash[31] &= 0x7f; /* Unset above (b-2) */
  2017. hash[31] |= 0x40; /* Set 2^(b-2) */
  2018. /* Chop off the top part and convert to int */
  2019. a = bignum_from_bytes_le(hash, 32);
  2020. SHA512_Init(&hs);
  2021. put_data(&hs, hash+(ec->publicKey.curve->fieldBits / 8),
  2022. ((ec->publicKey.curve->fieldBits / 4) -
  2023. (ec->publicKey.curve->fieldBits / 8)));
  2024. put_data(&hs, data, datalen);
  2025. SHA512_Final(&hs, hash);
  2026. r = bignum_from_bytes_le(hash, 512/8);
  2027. rp = ecp_mul(&ec->publicKey.curve->e.B, r);
  2028. assert(rp);
  2029. /* Now calculate s */
  2030. SHA512_Init(&hs);
  2031. /* Encode the point R */
  2032. for (i = 0; i < pointlen - 1; ++i)
  2033. put_byte(&hs, bignum_byte(rp->y, i));
  2034. /* Unset last bit of y and set first bit of x in its place */
  2035. put_byte(&hs, ((bignum_byte(rp->y, i) & 0x7f) |
  2036. (bignum_bit(rp->x, 0) << 7)));
  2037. /* Encode the point pk */
  2038. for (i = 0; i < pointlen - 1; ++i)
  2039. put_byte(&hs, bignum_byte(ec->publicKey.y, i));
  2040. /* Unset last bit of y and set first bit of x in its place */
  2041. put_byte(&hs, ((bignum_byte(ec->publicKey.y, i) & 0x7f) |
  2042. (bignum_bit(ec->publicKey.x, 0) << 7)));
  2043. /* Add the message */
  2044. put_data(&hs, data, datalen);
  2045. SHA512_Final(&hs, hash);
  2046. {
  2047. Bignum tmp, tmp2;
  2048. tmp = bignum_from_bytes_le(hash, 512/8);
  2049. tmp2 = modmul(tmp, a, ec->publicKey.curve->e.l);
  2050. freebn(a);
  2051. freebn(tmp);
  2052. tmp = bigadd(r, tmp2);
  2053. freebn(r);
  2054. freebn(tmp2);
  2055. s = bigmod(tmp, ec->publicKey.curve->e.l);
  2056. freebn(tmp);
  2057. }
  2058. }
  2059. /* Format the output */
  2060. put_stringz(bs, ec->sshk->ssh_id);
  2061. pointlen = ec->publicKey.curve->fieldBits / 8;
  2062. put_uint32(bs, pointlen * 2);
  2063. /* Encode the point */
  2064. for (i = 0; i < pointlen - 1; ++i)
  2065. put_byte(bs, bignum_byte(rp->y, i));
  2066. /* Unset last bit of y and set first bit of x in its place */
  2067. put_byte(bs, ((bignum_byte(rp->y, i) & 0x7f) |
  2068. (bignum_bit(rp->x, 0) << 7)));
  2069. ec_point_free(rp);
  2070. /* Encode the int */
  2071. for (i = 0; i < pointlen; ++i)
  2072. put_byte(bs, bignum_byte(s, i));
  2073. freebn(s);
  2074. } else {
  2075. ssh_hash *hashctx;
  2076. strbuf *substr;
  2077. digestLen = extra->hash->hlen;
  2078. assert(digestLen <= sizeof(digest));
  2079. hashctx = ssh_hash_new(extra->hash);
  2080. put_data(hashctx, data, datalen);
  2081. ssh_hash_final(hashctx, digest);
  2082. /* Do the signature */
  2083. _ecdsa_sign(ec->privateKey, ec->publicKey.curve, digest, digestLen, &r, &s);
  2084. assert(r);
  2085. assert(s);
  2086. /* Format the output */
  2087. put_stringz(bs, ec->sshk->ssh_id);
  2088. substr = strbuf_new();
  2089. put_mp_ssh2(substr, r);
  2090. put_mp_ssh2(substr, s);
  2091. put_stringsb(bs, substr);
  2092. freebn(r);
  2093. freebn(s);
  2094. }
  2095. }
  2096. const struct ecsign_extra sign_extra_ed25519 = {
  2097. ec_ed25519, NULL,
  2098. NULL, 0,
  2099. };
  2100. const ssh_keyalg ssh_ecdsa_ed25519 = {
  2101. ecdsa_new_pub,
  2102. ecdsa_new_priv,
  2103. ed25519_new_priv_openssh,
  2104. ecdsa_freekey,
  2105. ecdsa_sign,
  2106. ecdsa_verify,
  2107. ecdsa_public_blob,
  2108. ecdsa_private_blob,
  2109. ed25519_openssh_blob,
  2110. ecdsa_cache_str,
  2111. ecdsa_pubkey_bits,
  2112. "ssh-ed25519",
  2113. "ssh-ed25519",
  2114. &sign_extra_ed25519,
  2115. };
  2116. /* OID: 1.2.840.10045.3.1.7 (ansiX9p256r1) */
  2117. static const unsigned char nistp256_oid[] = {
  2118. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  2119. };
  2120. const struct ecsign_extra sign_extra_nistp256 = {
  2121. ec_p256, &ssh_sha256,
  2122. nistp256_oid, lenof(nistp256_oid),
  2123. };
  2124. const ssh_keyalg ssh_ecdsa_nistp256 = {
  2125. ecdsa_new_pub,
  2126. ecdsa_new_priv,
  2127. ecdsa_new_priv_openssh,
  2128. ecdsa_freekey,
  2129. ecdsa_sign,
  2130. ecdsa_verify,
  2131. ecdsa_public_blob,
  2132. ecdsa_private_blob,
  2133. ecdsa_openssh_blob,
  2134. ecdsa_cache_str,
  2135. ecdsa_pubkey_bits,
  2136. "ecdsa-sha2-nistp256",
  2137. "ecdsa-sha2-nistp256",
  2138. &sign_extra_nistp256,
  2139. };
  2140. /* OID: 1.3.132.0.34 (secp384r1) */
  2141. static const unsigned char nistp384_oid[] = {
  2142. 0x2b, 0x81, 0x04, 0x00, 0x22
  2143. };
  2144. const struct ecsign_extra sign_extra_nistp384 = {
  2145. ec_p384, &ssh_sha384,
  2146. nistp384_oid, lenof(nistp384_oid),
  2147. };
  2148. const ssh_keyalg ssh_ecdsa_nistp384 = {
  2149. ecdsa_new_pub,
  2150. ecdsa_new_priv,
  2151. ecdsa_new_priv_openssh,
  2152. ecdsa_freekey,
  2153. ecdsa_sign,
  2154. ecdsa_verify,
  2155. ecdsa_public_blob,
  2156. ecdsa_private_blob,
  2157. ecdsa_openssh_blob,
  2158. ecdsa_cache_str,
  2159. ecdsa_pubkey_bits,
  2160. "ecdsa-sha2-nistp384",
  2161. "ecdsa-sha2-nistp384",
  2162. &sign_extra_nistp384,
  2163. };
  2164. /* OID: 1.3.132.0.35 (secp521r1) */
  2165. static const unsigned char nistp521_oid[] = {
  2166. 0x2b, 0x81, 0x04, 0x00, 0x23
  2167. };
  2168. const struct ecsign_extra sign_extra_nistp521 = {
  2169. ec_p521, &ssh_sha512,
  2170. nistp521_oid, lenof(nistp521_oid),
  2171. };
  2172. const ssh_keyalg ssh_ecdsa_nistp521 = {
  2173. ecdsa_new_pub,
  2174. ecdsa_new_priv,
  2175. ecdsa_new_priv_openssh,
  2176. ecdsa_freekey,
  2177. ecdsa_sign,
  2178. ecdsa_verify,
  2179. ecdsa_public_blob,
  2180. ecdsa_private_blob,
  2181. ecdsa_openssh_blob,
  2182. ecdsa_cache_str,
  2183. ecdsa_pubkey_bits,
  2184. "ecdsa-sha2-nistp521",
  2185. "ecdsa-sha2-nistp521",
  2186. &sign_extra_nistp521,
  2187. };
  2188. /* ----------------------------------------------------------------------
  2189. * Exposed ECDH interface
  2190. */
  2191. struct eckex_extra {
  2192. struct ec_curve *(*curve)(void);
  2193. };
  2194. static Bignum ecdh_calculate(const Bignum private,
  2195. const struct ec_point *public)
  2196. {
  2197. struct ec_point *p;
  2198. Bignum ret;
  2199. p = ecp_mul(public, private);
  2200. if (!p) return NULL;
  2201. ret = p->x;
  2202. p->x = NULL;
  2203. if (p->curve->type == EC_MONTGOMERY) {
  2204. /*
  2205. * Endianness-swap. The Curve25519 algorithm definition
  2206. * assumes you were doing your computation in arrays of 32
  2207. * little-endian bytes, and now specifies that you take your
  2208. * final one of those and convert it into a bignum in
  2209. * _network_ byte order, i.e. big-endian.
  2210. *
  2211. * In particular, the spec says, you convert the _whole_ 32
  2212. * bytes into a bignum. That is, on the rare occasions that
  2213. * p->x has come out with the most significant 8 bits zero, we
  2214. * have to imagine that being represented by a 32-byte string
  2215. * with the last byte being zero, so that has to be converted
  2216. * into an SSH-2 bignum with the _low_ byte zero, i.e. a
  2217. * multiple of 256.
  2218. */
  2219. int i;
  2220. int bytes = (p->curve->fieldBits+7) / 8;
  2221. unsigned char *byteorder = snewn(bytes, unsigned char);
  2222. for (i = 0; i < bytes; ++i) {
  2223. byteorder[i] = bignum_byte(ret, i);
  2224. }
  2225. freebn(ret);
  2226. ret = bignum_from_bytes(byteorder, bytes);
  2227. smemclr(byteorder, bytes);
  2228. sfree(byteorder);
  2229. }
  2230. ec_point_free(p);
  2231. return ret;
  2232. }
  2233. const char *ssh_ecdhkex_curve_textname(const struct ssh_kex *kex)
  2234. {
  2235. const struct eckex_extra *extra = (const struct eckex_extra *)kex->extra;
  2236. struct ec_curve *curve = extra->curve();
  2237. return curve->textname;
  2238. }
  2239. struct ec_key *ssh_ecdhkex_newkey(const struct ssh_kex *kex)
  2240. {
  2241. const struct eckex_extra *extra = (const struct eckex_extra *)kex->extra;
  2242. struct ec_curve *curve;
  2243. struct ec_key *key;
  2244. struct ec_point *publicKey;
  2245. curve = extra->curve();
  2246. key = snew(struct ec_key);
  2247. key->sshk = NULL;
  2248. key->publicKey.curve = curve;
  2249. if (curve->type == EC_MONTGOMERY) {
  2250. unsigned char bytes[32] = {0};
  2251. int i;
  2252. for (i = 0; i < sizeof(bytes); ++i)
  2253. {
  2254. bytes[i] = (unsigned char)random_byte();
  2255. }
  2256. bytes[0] &= 248;
  2257. bytes[31] &= 127;
  2258. bytes[31] |= 64;
  2259. key->privateKey = bignum_from_bytes_le(bytes, sizeof(bytes));
  2260. smemclr(bytes, sizeof(bytes));
  2261. if (!key->privateKey) {
  2262. sfree(key);
  2263. return NULL;
  2264. }
  2265. publicKey = ecp_mul(&key->publicKey.curve->m.G, key->privateKey);
  2266. if (!publicKey) {
  2267. freebn(key->privateKey);
  2268. sfree(key);
  2269. return NULL;
  2270. }
  2271. key->publicKey.x = publicKey->x;
  2272. key->publicKey.y = publicKey->y;
  2273. key->publicKey.z = NULL;
  2274. sfree(publicKey);
  2275. } else {
  2276. key->privateKey = bignum_random_in_range(One, key->publicKey.curve->w.n);
  2277. if (!key->privateKey) {
  2278. sfree(key);
  2279. return NULL;
  2280. }
  2281. publicKey = ecp_mul(&key->publicKey.curve->w.G, key->privateKey);
  2282. if (!publicKey) {
  2283. freebn(key->privateKey);
  2284. sfree(key);
  2285. return NULL;
  2286. }
  2287. key->publicKey.x = publicKey->x;
  2288. key->publicKey.y = publicKey->y;
  2289. key->publicKey.z = NULL;
  2290. sfree(publicKey);
  2291. }
  2292. return key;
  2293. }
  2294. void ssh_ecdhkex_getpublic(struct ec_key *ec, BinarySink *bs)
  2295. {
  2296. int i;
  2297. int pointlen;
  2298. pointlen = (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8;
  2299. if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  2300. put_byte(bs, 0x04);
  2301. for (i = pointlen; i--;)
  2302. put_byte(bs, bignum_byte(ec->publicKey.x, i));
  2303. for (i = pointlen; i--;)
  2304. put_byte(bs, bignum_byte(ec->publicKey.y, i));
  2305. } else {
  2306. for (i = 0; i < pointlen; ++i)
  2307. put_byte(bs, bignum_byte(ec->publicKey.x, i));
  2308. }
  2309. }
  2310. Bignum ssh_ecdhkex_getkey(struct ec_key *ec,
  2311. const void *remoteKey, int remoteKeyLen)
  2312. {
  2313. struct ec_point remote;
  2314. Bignum ret;
  2315. if (ec->publicKey.curve->type == EC_WEIERSTRASS) {
  2316. remote.curve = ec->publicKey.curve;
  2317. remote.infinity = 0;
  2318. if (!decodepoint(remoteKey, remoteKeyLen, &remote)) {
  2319. return NULL;
  2320. }
  2321. } else {
  2322. /* Point length has to be the same length */
  2323. if (remoteKeyLen != (bignum_bitcount(ec->publicKey.curve->p) + 7) / 8) {
  2324. return NULL;
  2325. }
  2326. remote.curve = ec->publicKey.curve;
  2327. remote.infinity = 0;
  2328. remote.x = bignum_from_bytes_le((const unsigned char *)remoteKey,
  2329. remoteKeyLen);
  2330. remote.y = NULL;
  2331. remote.z = NULL;
  2332. }
  2333. ret = ecdh_calculate(ec->privateKey, &remote);
  2334. if (remote.x) freebn(remote.x);
  2335. if (remote.y) freebn(remote.y);
  2336. return ret;
  2337. }
  2338. void ssh_ecdhkex_freekey(struct ec_key *key)
  2339. {
  2340. ecdsa_freekey(&key->sshk);
  2341. }
  2342. static const struct eckex_extra kex_extra_curve25519 = { ec_curve25519 };
  2343. static const struct ssh_kex ssh_ec_kex_curve25519 = {
  2344. "[email protected]", NULL, KEXTYPE_ECDH,
  2345. &ssh_sha256, &kex_extra_curve25519,
  2346. };
  2347. const struct eckex_extra kex_extra_nistp256 = { ec_p256 };
  2348. static const struct ssh_kex ssh_ec_kex_nistp256 = {
  2349. "ecdh-sha2-nistp256", NULL, KEXTYPE_ECDH,
  2350. &ssh_sha256, &kex_extra_nistp256,
  2351. };
  2352. const struct eckex_extra kex_extra_nistp384 = { ec_p384 };
  2353. static const struct ssh_kex ssh_ec_kex_nistp384 = {
  2354. "ecdh-sha2-nistp384", NULL, KEXTYPE_ECDH,
  2355. &ssh_sha384, &kex_extra_nistp384,
  2356. };
  2357. const struct eckex_extra kex_extra_nistp521 = { ec_p521 };
  2358. static const struct ssh_kex ssh_ec_kex_nistp521 = {
  2359. "ecdh-sha2-nistp521", NULL, KEXTYPE_ECDH,
  2360. &ssh_sha512, &kex_extra_nistp521,
  2361. };
  2362. static const struct ssh_kex *const ec_kex_list[] = {
  2363. &ssh_ec_kex_curve25519,
  2364. &ssh_ec_kex_nistp256,
  2365. &ssh_ec_kex_nistp384,
  2366. &ssh_ec_kex_nistp521,
  2367. };
  2368. const struct ssh_kexes ssh_ecdh_kex = {
  2369. sizeof(ec_kex_list) / sizeof(*ec_kex_list),
  2370. ec_kex_list
  2371. };
  2372. /* ----------------------------------------------------------------------
  2373. * Helper functions for finding key algorithms and returning auxiliary
  2374. * data.
  2375. */
  2376. const ssh_keyalg *ec_alg_by_oid(int len, const void *oid,
  2377. const struct ec_curve **curve)
  2378. {
  2379. static const ssh_keyalg *algs_with_oid[] = {
  2380. &ssh_ecdsa_nistp256,
  2381. &ssh_ecdsa_nistp384,
  2382. &ssh_ecdsa_nistp521,
  2383. };
  2384. int i;
  2385. for (i = 0; i < lenof(algs_with_oid); i++) {
  2386. const ssh_keyalg *alg = algs_with_oid[i];
  2387. const struct ecsign_extra *extra =
  2388. (const struct ecsign_extra *)alg->extra;
  2389. if (len == extra->oidlen && !memcmp(oid, extra->oid, len)) {
  2390. *curve = extra->curve();
  2391. return alg;
  2392. }
  2393. }
  2394. return NULL;
  2395. }
  2396. const unsigned char *ec_alg_oid(const ssh_keyalg *alg,
  2397. int *oidlen)
  2398. {
  2399. const struct ecsign_extra *extra = (const struct ecsign_extra *)alg->extra;
  2400. *oidlen = extra->oidlen;
  2401. return extra->oid;
  2402. }
  2403. const int ec_nist_curve_lengths[] = { 256, 384, 521 };
  2404. const int n_ec_nist_curve_lengths = lenof(ec_nist_curve_lengths);
  2405. int ec_nist_alg_and_curve_by_bits(int bits,
  2406. const struct ec_curve **curve,
  2407. const ssh_keyalg **alg)
  2408. {
  2409. switch (bits) {
  2410. case 256: *alg = &ssh_ecdsa_nistp256; break;
  2411. case 384: *alg = &ssh_ecdsa_nistp384; break;
  2412. case 521: *alg = &ssh_ecdsa_nistp521; break;
  2413. default: return FALSE;
  2414. }
  2415. *curve = ((struct ecsign_extra *)(*alg)->extra)->curve();
  2416. return TRUE;
  2417. }
  2418. int ec_ed_alg_and_curve_by_bits(int bits,
  2419. const struct ec_curve **curve,
  2420. const ssh_keyalg **alg)
  2421. {
  2422. switch (bits) {
  2423. case 256: *alg = &ssh_ecdsa_ed25519; break;
  2424. default: return FALSE;
  2425. }
  2426. *curve = ((struct ecsign_extra *)(*alg)->extra)->curve();
  2427. return TRUE;
  2428. }
  2429. #ifdef MPEXT
  2430. void ec_cleanup(void)
  2431. {
  2432. ec_curve_cleanup = 1;
  2433. ec_p256();
  2434. ec_p384();
  2435. ec_p521();
  2436. ec_curve25519();
  2437. ec_ed25519();
  2438. // in case we want to restart (unlikely)
  2439. ec_curve_cleanup = 0;
  2440. }
  2441. #endif