uECC.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747
  1. /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
  2. #include "uECC.h"
  3. // NULL
  4. #include "stddef.h"
  5. #ifndef uECC_PLATFORM
  6. #if __AVR__
  7. #define uECC_PLATFORM uECC_avr
  8. #elif defined(__thumb2__) || defined(_M_ARMT) /* I think MSVC only supports Thumb-2 targets */
  9. #define uECC_PLATFORM uECC_arm_thumb2
  10. #elif defined(__thumb__)
  11. #define uECC_PLATFORM uECC_arm_thumb
  12. #elif defined(__arm__) || defined(_M_ARM)
  13. #define uECC_PLATFORM uECC_arm
  14. #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__)
  15. #define uECC_PLATFORM uECC_x86
  16. #elif defined(__amd64__) || defined(_M_X64)
  17. #define uECC_PLATFORM uECC_x86_64
  18. #else
  19. #define uECC_PLATFORM uECC_arch_other
  20. #endif
  21. #endif
  22. #ifndef uECC_WORD_SIZE
  23. #if uECC_PLATFORM == uECC_avr
  24. #define uECC_WORD_SIZE 1
  25. #elif (uECC_PLATFORM == uECC_x86_64)
  26. #define uECC_WORD_SIZE 8
  27. #else
  28. #define uECC_WORD_SIZE 4
  29. #endif
  30. #endif
  31. #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp224r1) && (uECC_WORD_SIZE == 8)
  32. #undef uECC_WORD_SIZE
  33. #define uECC_WORD_SIZE 4
  34. #if (uECC_PLATFORM == uECC_x86_64)
  35. #undef uECC_PLATFORM
  36. #define uECC_PLATFORM uECC_x86
  37. #endif
  38. #endif
  39. #if (uECC_WORD_SIZE != 1) && (uECC_WORD_SIZE != 4) && (uECC_WORD_SIZE != 8)
  40. #error "Unsupported value for uECC_WORD_SIZE"
  41. #endif
  42. #if (uECC_ASM && (uECC_PLATFORM == uECC_avr) && (uECC_WORD_SIZE != 1))
  43. #pragma message ("uECC_WORD_SIZE must be 1 when using AVR asm")
  44. #undef uECC_WORD_SIZE
  45. #define uECC_WORD_SIZE 1
  46. #endif
  47. #if (uECC_ASM && \
  48. (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb) && \
  49. (uECC_WORD_SIZE != 4))
  50. #pragma message ("uECC_WORD_SIZE must be 4 when using ARM asm")
  51. #undef uECC_WORD_SIZE
  52. #define uECC_WORD_SIZE 4
  53. #endif
  54. #if __STDC_VERSION__ >= 199901L
  55. #define RESTRICT restrict
  56. #else
  57. #define RESTRICT
  58. #endif
  59. #if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302)
  60. #define SUPPORTS_INT128 1
  61. #else
  62. #define SUPPORTS_INT128 0
  63. #endif
  64. #define MAX_TRIES 64
  65. #if (uECC_WORD_SIZE == 1)
  66. typedef uint8_t uECC_word_t;
  67. typedef uint16_t uECC_dword_t;
  68. typedef uint8_t wordcount_t;
  69. typedef int8_t swordcount_t;
  70. typedef int16_t bitcount_t;
  71. typedef int8_t cmpresult_t;
  72. #define HIGH_BIT_SET 0x80
  73. #define uECC_WORD_BITS 8
  74. #define uECC_WORD_BITS_SHIFT 3
  75. #define uECC_WORD_BITS_MASK 0x07
  76. #define uECC_WORDS_1 20
  77. #define uECC_WORDS_2 24
  78. #define uECC_WORDS_3 32
  79. #define uECC_WORDS_4 32
  80. #define uECC_WORDS_5 28
  81. #define uECC_N_WORDS_1 21
  82. #define uECC_N_WORDS_2 24
  83. #define uECC_N_WORDS_3 32
  84. #define uECC_N_WORDS_4 32
  85. #define uECC_N_WORDS_5 28
  86. #define Curve_P_1 {0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, \
  87. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  88. 0xFF, 0xFF, 0xFF, 0xFF}
  89. #define Curve_P_2 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  90. 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  91. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
  92. #define Curve_P_3 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  93. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, \
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  95. 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}
  96. #define Curve_P_4 {0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, \
  97. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  98. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  99. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
  100. #define Curve_P_5 {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  101. 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, \
  102. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  103. 0xFF, 0xFF, 0xFF, 0xFF}
  104. #define Curve_B_1 {0x45, 0xFA, 0x65, 0xC5, 0xAD, 0xD4, 0xD4, 0x81, \
  105. 0x9F, 0xF8, 0xAC, 0x65, 0x8B, 0x7A, 0xBD, 0x54, \
  106. 0xFC, 0xBE, 0x97, 0x1C}
  107. #define Curve_B_2 {0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE, \
  108. 0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F, \
  109. 0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64}
  110. #define Curve_B_3 {0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B, \
  111. 0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65, \
  112. 0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3, \
  113. 0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A}
  114. #define Curve_B_4 {0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  118. #define Curve_B_5 {0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27, \
  119. 0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50, \
  120. 0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C, \
  121. 0x85, 0x0A, 0x05, 0xB4}
  122. #define Curve_G_1 { \
  123. {0x82, 0xFC, 0xCB, 0x13, 0xB9, 0x8B, 0xC3, 0x68, \
  124. 0x89, 0x69, 0x64, 0x46, 0x28, 0x73, 0xF5, 0x8E, \
  125. 0x68, 0xB5, 0x96, 0x4A}, \
  126. {0x32, 0xFB, 0xC5, 0x7A, 0x37, 0x51, 0x23, 0x04, \
  127. 0x12, 0xC9, 0xDC, 0x59, 0x7D, 0x94, 0x68, 0x31, \
  128. 0x55, 0x28, 0xA6, 0x23}}
  129. #define Curve_G_2 { \
  130. {0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4, \
  131. 0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C, \
  132. 0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18}, \
  133. {0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73, \
  134. 0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63, \
  135. 0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07}}
  136. #define Curve_G_3 { \
  137. {0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4, \
  138. 0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77, \
  139. 0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8, \
  140. 0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B}, \
  141. {0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB, \
  142. 0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B, \
  143. 0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E, \
  144. 0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F}}
  145. #define Curve_G_4 { \
  146. {0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59, \
  147. 0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02, \
  148. 0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55, \
  149. 0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79}, \
  150. {0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C, \
  151. 0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD, \
  152. 0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D, \
  153. 0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48}}
  154. #define Curve_G_5 { \
  155. {0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34, \
  156. 0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A, \
  157. 0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B, \
  158. 0xBD, 0x0C, 0x0E, 0xB7}, \
  159. {0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44, \
  160. 0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD, \
  161. 0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5, \
  162. 0x88, 0x63, 0x37, 0xBD}}
  163. #define Curve_N_1 {0x57, 0x22, 0x75, 0xCA, 0xD3, 0xAE, 0x27, 0xF9, \
  164. 0xC8, 0xF4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, \
  165. 0x00, 0x00, 0x00, 0x00, 0x01}
  166. #define Curve_N_2 {0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14, \
  167. 0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF, \
  168. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
  169. #define Curve_N_3 {0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3, \
  170. 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC, \
  171. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  172. 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}
  173. #define Curve_N_4 {0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF, \
  174. 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA, \
  175. 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  176. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
  177. #define Curve_N_5 {0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13, \
  178. 0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF, \
  179. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
  180. 0xFF, 0xFF, 0xFF, 0xFF}
  181. #elif (uECC_WORD_SIZE == 4)
  182. typedef uint32_t uECC_word_t;
  183. typedef uint64_t uECC_dword_t;
  184. typedef unsigned wordcount_t;
  185. typedef int swordcount_t;
  186. typedef int bitcount_t;
  187. typedef int cmpresult_t;
  188. #define HIGH_BIT_SET 0x80000000
  189. #define uECC_WORD_BITS 32
  190. #define uECC_WORD_BITS_SHIFT 5
  191. #define uECC_WORD_BITS_MASK 0x01F
  192. #define uECC_WORDS_1 5
  193. #define uECC_WORDS_2 6
  194. #define uECC_WORDS_3 8
  195. #define uECC_WORDS_4 8
  196. #define uECC_WORDS_5 7
  197. #define uECC_N_WORDS_1 6
  198. #define uECC_N_WORDS_2 6
  199. #define uECC_N_WORDS_3 8
  200. #define uECC_N_WORDS_4 8
  201. #define uECC_N_WORDS_5 7
  202. #define Curve_P_1 {0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  203. #define Curve_P_2 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  204. #define Curve_P_3 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, \
  205. 0x00000000, 0x00000000, 0x00000001, 0xFFFFFFFF}
  206. #define Curve_P_4 {0xFFFFFC2F, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, \
  207. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  208. #define Curve_P_5 {0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFF, \
  209. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  210. #define Curve_B_1 {0xC565FA45, 0x81D4D4AD, 0x65ACF89F, 0x54BD7A8B, 0x1C97BEFC}
  211. #define Curve_B_2 {0xC146B9B1, 0xFEB8DEEC, 0x72243049, 0x0FA7E9AB, 0xE59C80E7, 0x64210519}
  212. #define Curve_B_3 {0x27D2604B, 0x3BCE3C3E, 0xCC53B0F6, 0x651D06B0, \
  213. 0x769886BC, 0xB3EBBD55, 0xAA3A93E7, 0x5AC635D8}
  214. #define Curve_B_4 {0x00000007, 0x00000000, 0x00000000, 0x00000000, \
  215. 0x00000000, 0x00000000, 0x00000000, 0x00000000}
  216. #define Curve_B_5 {0x2355FFB4, 0x270B3943, 0xD7BFD8BA, 0x5044B0B7, \
  217. 0xF5413256, 0x0C04B3AB, 0xB4050A85}
  218. #define Curve_G_1 { \
  219. {0x13CBFC82, 0x68C38BB9, 0x46646989, 0x8EF57328, 0x4A96B568}, \
  220. {0x7AC5FB32, 0x04235137, 0x59DCC912, 0x3168947D, 0x23A62855}}
  221. #define Curve_G_2 { \
  222. {0x82FF1012, 0xF4FF0AFD, 0x43A18800, 0x7CBF20EB, 0xB03090F6, 0x188DA80E}, \
  223. {0x1E794811, 0x73F977A1, 0x6B24CDD5, 0x631011ED, 0xFFC8DA78, 0x07192B95}}
  224. #define Curve_G_3 { \
  225. {0xD898C296, 0xF4A13945, 0x2DEB33A0, 0x77037D81, \
  226. 0x63A440F2, 0xF8BCE6E5, 0xE12C4247, 0x6B17D1F2}, \
  227. {0x37BF51F5, 0xCBB64068, 0x6B315ECE, 0x2BCE3357, \
  228. 0x7C0F9E16, 0x8EE7EB4A, 0xFE1A7F9B, 0x4FE342E2}}
  229. #define Curve_G_4 { \
  230. {0x16F81798, 0x59F2815B, 0x2DCE28D9, 0x029BFCDB, \
  231. 0xCE870B07, 0x55A06295, 0xF9DCBBAC, 0x79BE667E}, \
  232. {0xFB10D4B8, 0x9C47D08F, 0xA6855419, 0xFD17B448, \
  233. 0x0E1108A8, 0x5DA4FBFC, 0x26A3C465, 0x483ADA77}}
  234. #define Curve_G_5 { \
  235. {0x115C1D21, 0x343280D6, 0x56C21122, 0x4A03C1D3, \
  236. 0x321390B9, 0x6BB4BF7F, 0xB70E0CBD}, \
  237. {0x85007E34, 0x44D58199, 0x5A074764, 0xCD4375A0, \
  238. 0x4C22DFE6, 0xB5F723FB, 0xBD376388}}
  239. #define Curve_N_1 {0xCA752257, 0xF927AED3, 0x0001F4C8, 0x00000000, 0x00000000, 0x00000001}
  240. #define Curve_N_2 {0xB4D22831, 0x146BC9B1, 0x99DEF836, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  241. #define Curve_N_3 {0xFC632551, 0xF3B9CAC2, 0xA7179E84, 0xBCE6FAAD, \
  242. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF}
  243. #define Curve_N_4 {0xD0364141, 0xBFD25E8C, 0xAF48A03B, 0xBAAEDCE6, \
  244. 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  245. #define Curve_N_5 {0x5C5C2A3D, 0x13DD2945, 0xE0B8F03E, 0xFFFF16A2, \
  246. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
  247. #elif (uECC_WORD_SIZE == 8)
  248. typedef uint64_t uECC_word_t;
  249. #if SUPPORTS_INT128
  250. typedef unsigned __int128 uECC_dword_t;
  251. #endif
  252. typedef unsigned wordcount_t;
  253. typedef int swordcount_t;
  254. typedef int bitcount_t;
  255. typedef int cmpresult_t;
  256. #define HIGH_BIT_SET 0x8000000000000000ull
  257. #define uECC_WORD_BITS 64
  258. #define uECC_WORD_BITS_SHIFT 6
  259. #define uECC_WORD_BITS_MASK 0x03F
  260. #define uECC_WORDS_1 3
  261. #define uECC_WORDS_2 3
  262. #define uECC_WORDS_3 4
  263. #define uECC_WORDS_4 4
  264. #define uECC_WORDS_5 4
  265. #define uECC_N_WORDS_1 3
  266. #define uECC_N_WORDS_2 3
  267. #define uECC_N_WORDS_3 4
  268. #define uECC_N_WORDS_4 4
  269. #define uECC_N_WORDS_5 4
  270. #define Curve_P_1 {0xFFFFFFFF7FFFFFFFull, 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
  271. #define Curve_P_2 {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull}
  272. #define Curve_P_3 {0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, \
  273. 0x0000000000000000ull, 0xFFFFFFFF00000001ull}
  274. #define Curve_P_4 {0xFFFFFFFEFFFFFC2Full, 0xFFFFFFFFFFFFFFFFull, \
  275. 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull}
  276. #define Curve_P_5 {0x0000000000000001ull, 0xFFFFFFFF00000000ull, \
  277. 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
  278. #define Curve_B_1 {0x81D4D4ADC565FA45ull, 0x54BD7A8B65ACF89Full, 0x000000001C97BEFCull}
  279. #define Curve_B_2 {0xFEB8DEECC146B9B1ull, 0x0FA7E9AB72243049ull, 0x64210519E59C80E7ull}
  280. #define Curve_B_3 {0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull, \
  281. 0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull}
  282. #define Curve_B_4 {0x0000000000000007ull, 0x0000000000000000ull, \
  283. 0x0000000000000000ull, 0x0000000000000000ull}
  284. #define Curve_B_5 {0x270B39432355FFB4ull, 0x5044B0B7D7BFD8BAull, \
  285. 0x0C04B3ABF5413256ull, 0x00000000B4050A85ull}
  286. #define Curve_G_1 { \
  287. {0x68C38BB913CBFC82ull, 0x8EF5732846646989ull, 0x000000004A96B568ull}, \
  288. {0x042351377AC5FB32ull, 0x3168947D59DCC912ull, 0x0000000023A62855ull}}
  289. #define Curve_G_2 { \
  290. {0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, 0x188DA80EB03090F6ull}, \
  291. {0x73F977A11E794811ull, 0x631011ED6B24CDD5ull, 0x07192B95FFC8DA78ull}}
  292. #define Curve_G_3 { \
  293. {0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull}, \
  294. {0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull}}
  295. #define Curve_G_4 { \
  296. {0x59F2815B16F81798ull, 0x029BFCDB2DCE28D9ull, 0x55A06295CE870B07ull, 0x79BE667EF9DCBBACull}, \
  297. {0x9C47D08FFB10D4B8ull, 0xFD17B448A6855419ull, 0x5DA4FBFC0E1108A8ull, 0x483ADA7726A3C465ull}}
  298. #define Curve_G_5 { \
  299. {0x343280D6115C1D21ull, 0x4A03C1D356C21122ull, 0x6BB4BF7F321390B9ull, 0x00000000B70E0CBDull}, \
  300. {0x44D5819985007E34ull, 0xCD4375A05A074764ull, 0xB5F723FB4C22DFE6ull, 0x00000000BD376388ull}}
  301. #define Curve_N_1 {0xF927AED3CA752257ull, 0x000000000001F4C8ull, 0x0000000100000000ull}
  302. #define Curve_N_2 {0x146BC9B1B4D22831ull, 0xFFFFFFFF99DEF836ull, 0xFFFFFFFFFFFFFFFFull}
  303. #define Curve_N_3 {0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, \
  304. 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull}
  305. #define Curve_N_4 {0xBFD25E8CD0364141ull, 0xBAAEDCE6AF48A03Bull, \
  306. 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull}
  307. #define Curve_N_5 {0x13DD29455C5C2A3Dull, 0xFFFF16A2E0B8F03Eull, \
  308. 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
  309. #endif /* (uECC_WORD_SIZE == 8) */
  310. #define uECC_WORDS uECC_CONCAT(uECC_WORDS_, uECC_CURVE)
  311. #define uECC_N_WORDS uECC_CONCAT(uECC_N_WORDS_, uECC_CURVE)
  312. typedef struct EccPoint {
  313. uECC_word_t x[uECC_WORDS];
  314. uECC_word_t y[uECC_WORDS];
  315. } EccPoint;
  316. static const uECC_word_t curve_p[uECC_WORDS] = uECC_CONCAT(Curve_P_, uECC_CURVE);
  317. // Global object `curve_b' is only referenced from function `curve_x_side', it should be defined within that functions block scope
  318. static const EccPoint curve_G = uECC_CONCAT(Curve_G_, uECC_CURVE);
  319. static const uECC_word_t curve_n[uECC_N_WORDS] = uECC_CONCAT(Curve_N_, uECC_CURVE);
  320. static void vli_clear(uECC_word_t *vli);
  321. static uECC_word_t vli_isZero(const uECC_word_t *vli);
  322. static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit);
  323. static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words);
  324. static void vli_set(uECC_word_t *dest, const uECC_word_t *src);
  325. static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right);
  326. static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right);
  327. static void vli_rshift1(uECC_word_t *vli);
  328. static uECC_word_t vli_add(uECC_word_t *result,
  329. const uECC_word_t *left,
  330. const uECC_word_t *right);
  331. static uECC_word_t vli_sub(uECC_word_t *result,
  332. const uECC_word_t *left,
  333. const uECC_word_t *right);
  334. static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right);
  335. static void vli_modAdd(uECC_word_t *result,
  336. const uECC_word_t *left,
  337. const uECC_word_t *right,
  338. const uECC_word_t *mod);
  339. static void vli_modSub(uECC_word_t *result,
  340. const uECC_word_t *left,
  341. const uECC_word_t *right,
  342. const uECC_word_t *mod);
  343. static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product);
  344. static void vli_modMult_fast(uECC_word_t *result,
  345. const uECC_word_t *left,
  346. const uECC_word_t *right);
  347. static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod);
  348. #if uECC_SQUARE_FUNC
  349. static void vli_square(uECC_word_t *result, const uECC_word_t *left);
  350. static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left);
  351. #endif
  352. #if (defined(_WIN32) || defined(_WIN64))
  353. /* Windows */
  354. #define WIN32_LEAN_AND_MEAN
  355. #include <windows.h>
  356. #include <wincrypt.h>
  357. static int default_RNG(uint8_t *dest, unsigned size) {
  358. HCRYPTPROV prov;
  359. if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
  360. return 0;
  361. }
  362. CryptGenRandom(prov, size, (BYTE *)dest);
  363. CryptReleaseContext(prov, 0);
  364. return 1;
  365. }
  366. #elif defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \
  367. (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX)
  368. /* Some POSIX-like system with /dev/urandom or /dev/random. */
  369. #include <sys/types.h>
  370. #include <fcntl.h>
  371. #include <unistd.h>
  372. #ifndef O_CLOEXEC
  373. #define O_CLOEXEC 0
  374. #endif
  375. static int default_RNG(uint8_t *dest, unsigned size) {
  376. int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
  377. if (fd == -1) {
  378. fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
  379. if (fd == -1) {
  380. return 0;
  381. }
  382. }
  383. char *ptr = (char *)dest;
  384. size_t left = size;
  385. while (left > 0) {
  386. ssize_t bytes_read = read(fd, ptr, left);
  387. if (bytes_read <= 0) { // read failed
  388. close(fd);
  389. return 0;
  390. }
  391. left -= bytes_read;
  392. ptr += bytes_read;
  393. }
  394. close(fd);
  395. return 1;
  396. }
  397. #else /* Some other platform */
  398. static int default_RNG(uint8_t *dest, unsigned size) {
  399. return 0;
  400. }
  401. #endif
  402. static uECC_RNG_Function g_rng_function = &default_RNG;
  403. void uECC_set_rng(uECC_RNG_Function rng_function) {
  404. g_rng_function = rng_function;
  405. }
  406. #ifdef __GNUC__ /* Only support GCC inline asm for now */
  407. #if (uECC_ASM && (uECC_PLATFORM == uECC_avr))
  408. #include "asm_avr.inc"
  409. #endif
  410. #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \
  411. uECC_PLATFORM == uECC_arm_thumb2))
  412. #include "asm_arm.inc"
  413. #endif
  414. #endif
  415. #if !asm_clear
  416. static void vli_clear(uECC_word_t *vli) {
  417. wordcount_t i;
  418. for (i = 0; i < uECC_WORDS; ++i) {
  419. vli[i] = 0;
  420. }
  421. }
  422. #endif
  423. /* Returns 1 if vli == 0, 0 otherwise. */
  424. #if !asm_isZero
  425. static uECC_word_t vli_isZero(const uECC_word_t *vli) {
  426. wordcount_t i;
  427. for (i = 0; i < uECC_WORDS; ++i) {
  428. if (vli[i]) {
  429. return 0;
  430. }
  431. }
  432. return 1;
  433. }
  434. #endif
  435. /* Returns nonzero if bit 'bit' of vli is set. */
  436. #if !asm_testBit
  437. static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit) {
  438. return (vli[bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
  439. }
  440. #endif
  441. /* Counts the number of words in vli. */
  442. #if !asm_numBits
  443. static wordcount_t vli_numDigits(const uECC_word_t *vli, wordcount_t max_words) {
  444. swordcount_t i;
  445. /* Search from the end until we find a non-zero digit.
  446. We do it in reverse because we expect that most digits will be nonzero. */
  447. for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
  448. }
  449. return (i + 1);
  450. }
  451. /* Counts the number of bits required to represent vli. */
  452. static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words) {
  453. uECC_word_t i;
  454. uECC_word_t digit;
  455. wordcount_t num_digits = vli_numDigits(vli, max_words);
  456. if (num_digits == 0) {
  457. return 0;
  458. }
  459. digit = vli[num_digits - 1];
  460. for (i = 0; digit; ++i) {
  461. digit >>= 1;
  462. }
  463. return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
  464. }
  465. #endif /* !asm_numBits */
  466. /* Sets dest = src. */
  467. #if !asm_set
  468. static void vli_set(uECC_word_t *dest, const uECC_word_t *src) {
  469. wordcount_t i;
  470. for (i = 0; i < uECC_WORDS; ++i) {
  471. dest[i] = src[i];
  472. }
  473. }
  474. #endif
  475. /* Returns sign of left - right. */
  476. #if !asm_cmp
  477. static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right) {
  478. swordcount_t i;
  479. for (i = uECC_WORDS - 1; i >= 0; --i) {
  480. if (left[i] > right[i]) {
  481. return 1;
  482. } else if (left[i] < right[i]) {
  483. return -1;
  484. }
  485. }
  486. return 0;
  487. }
  488. #endif
  489. static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right) {
  490. uECC_word_t result = 0;
  491. swordcount_t i;
  492. for (i = uECC_WORDS - 1; i >= 0; --i) {
  493. result |= (left[i] ^ right[i]);
  494. }
  495. return (result == 0);
  496. }
  497. /* Computes vli = vli >> 1. */
  498. #if !asm_rshift1
  499. static void vli_rshift1(uECC_word_t *vli) {
  500. uECC_word_t *end = vli;
  501. uECC_word_t carry = 0;
  502. vli += uECC_WORDS;
  503. while (vli-- > end) {
  504. uECC_word_t temp = *vli;
  505. *vli = (temp >> 1) | carry;
  506. carry = temp << (uECC_WORD_BITS - 1);
  507. }
  508. }
  509. #endif
  510. /* Computes result = left + right, returning carry. Can modify in place. */
  511. #if !asm_add
  512. static uECC_word_t vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
  513. uECC_word_t carry = 0;
  514. wordcount_t i;
  515. for (i = 0; i < uECC_WORDS; ++i) {
  516. uECC_word_t sum = left[i] + right[i] + carry;
  517. if (sum != left[i]) {
  518. carry = (sum < left[i]);
  519. }
  520. result[i] = sum;
  521. }
  522. return carry;
  523. }
  524. #endif
  525. /* Computes result = left - right, returning borrow. Can modify in place. */
  526. #if !asm_sub
  527. static uECC_word_t vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
  528. uECC_word_t borrow = 0;
  529. wordcount_t i;
  530. for (i = 0; i < uECC_WORDS; ++i) {
  531. uECC_word_t diff = left[i] - right[i] - borrow;
  532. if (diff != left[i]) {
  533. borrow = (diff > left[i]);
  534. }
  535. result[i] = diff;
  536. }
  537. return borrow;
  538. }
  539. #endif
  540. #if (!asm_mult || (uECC_SQUARE_FUNC && !asm_square) || uECC_CURVE == uECC_secp256k1)
  541. static void muladd(uECC_word_t a,
  542. uECC_word_t b,
  543. uECC_word_t *r0,
  544. uECC_word_t *r1,
  545. uECC_word_t *r2) {
  546. #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
  547. uint64_t a0 = a & 0xffffffffull;
  548. uint64_t a1 = a >> 32;
  549. uint64_t b0 = b & 0xffffffffull;
  550. uint64_t b1 = b >> 32;
  551. uint64_t i0 = a0 * b0;
  552. uint64_t i1 = a0 * b1;
  553. uint64_t i2 = a1 * b0;
  554. uint64_t i3 = a1 * b1;
  555. uint64_t p0, p1;
  556. i2 += (i0 >> 32);
  557. i2 += i1;
  558. if (i2 < i1) { // overflow
  559. i3 += 0x100000000ull;
  560. }
  561. p0 = (i0 & 0xffffffffull) | (i2 << 32);
  562. p1 = i3 + (i2 >> 32);
  563. *r0 += p0;
  564. *r1 += (p1 + (*r0 < p0));
  565. *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
  566. #else
  567. uECC_dword_t p = (uECC_dword_t)a * b;
  568. uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
  569. r01 += p;
  570. *r2 += (r01 < p);
  571. *r1 = r01 >> uECC_WORD_BITS;
  572. *r0 = (uECC_word_t)r01;
  573. #endif
  574. }
  575. #define muladd_exists 1
  576. #endif
  577. #if !asm_mult
  578. static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
  579. uECC_word_t r0 = 0;
  580. uECC_word_t r1 = 0;
  581. uECC_word_t r2 = 0;
  582. wordcount_t i, k;
  583. /* Compute each digit of result in sequence, maintaining the carries. */
  584. for (k = 0; k < uECC_WORDS; ++k) {
  585. for (i = 0; i <= k; ++i) {
  586. muladd(left[i], right[k - i], &r0, &r1, &r2);
  587. }
  588. result[k] = r0;
  589. r0 = r1;
  590. r1 = r2;
  591. r2 = 0;
  592. }
  593. for (k = uECC_WORDS; k < uECC_WORDS * 2 - 1; ++k) {
  594. for (i = (k + 1) - uECC_WORDS; i < uECC_WORDS; ++i) {
  595. muladd(left[i], right[k - i], &r0, &r1, &r2);
  596. }
  597. result[k] = r0;
  598. r0 = r1;
  599. r1 = r2;
  600. r2 = 0;
  601. }
  602. result[uECC_WORDS * 2 - 1] = r0;
  603. }
  604. #endif
  605. #if uECC_SQUARE_FUNC
  606. #if !asm_square
  607. static void mul2add(uECC_word_t a,
  608. uECC_word_t b,
  609. uECC_word_t *r0,
  610. uECC_word_t *r1,
  611. uECC_word_t *r2) {
  612. #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
  613. uint64_t a0 = a & 0xffffffffull;
  614. uint64_t a1 = a >> 32;
  615. uint64_t b0 = b & 0xffffffffull;
  616. uint64_t b1 = b >> 32;
  617. uint64_t i0 = a0 * b0;
  618. uint64_t i1 = a0 * b1;
  619. uint64_t i2 = a1 * b0;
  620. uint64_t i3 = a1 * b1;
  621. uint64_t p0, p1;
  622. i2 += (i0 >> 32);
  623. i2 += i1;
  624. if (i2 < i1)
  625. { // overflow
  626. i3 += 0x100000000ull;
  627. }
  628. p0 = (i0 & 0xffffffffull) | (i2 << 32);
  629. p1 = i3 + (i2 >> 32);
  630. *r2 += (p1 >> 63);
  631. p1 = (p1 << 1) | (p0 >> 63);
  632. p0 <<= 1;
  633. *r0 += p0;
  634. *r1 += (p1 + (*r0 < p0));
  635. *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
  636. #else
  637. uECC_dword_t p = (uECC_dword_t)a * b;
  638. uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
  639. *r2 += (p >> (uECC_WORD_BITS * 2 - 1));
  640. p *= 2;
  641. r01 += p;
  642. *r2 += (r01 < p);
  643. *r1 = r01 >> uECC_WORD_BITS;
  644. *r0 = (uECC_word_t)r01;
  645. #endif
  646. }
  647. static void vli_square(uECC_word_t *result, const uECC_word_t *left) {
  648. uECC_word_t r0 = 0;
  649. uECC_word_t r1 = 0;
  650. uECC_word_t r2 = 0;
  651. wordcount_t i, k;
  652. for (k = 0; k < uECC_WORDS * 2 - 1; ++k) {
  653. uECC_word_t min = (k < uECC_WORDS ? 0 : (k + 1) - uECC_WORDS);
  654. for (i = min; i <= k && i <= k - i; ++i) {
  655. if (i < k-i) {
  656. mul2add(left[i], left[k - i], &r0, &r1, &r2);
  657. } else {
  658. muladd(left[i], left[k - i], &r0, &r1, &r2);
  659. }
  660. }
  661. result[k] = r0;
  662. r0 = r1;
  663. r1 = r2;
  664. r2 = 0;
  665. }
  666. result[uECC_WORDS * 2 - 1] = r0;
  667. }
  668. #endif
  669. #else /* uECC_SQUARE_FUNC */
  670. #define vli_square(result, left, size) vli_mult((result), (left), (left), (size))
  671. #endif /* uECC_SQUARE_FUNC */
  672. /* Computes result = (left + right) % mod.
  673. Assumes that left < mod and right < mod, and that result does not overlap mod. */
  674. #if !asm_modAdd
  675. static void vli_modAdd(uECC_word_t *result,
  676. const uECC_word_t *left,
  677. const uECC_word_t *right,
  678. const uECC_word_t *mod) {
  679. uECC_word_t carry = vli_add(result, left, right);
  680. if (carry || vli_cmp(result, mod) >= 0) {
  681. /* result > mod (result = mod + remainder), so subtract mod to get remainder. */
  682. vli_sub(result, result, mod);
  683. }
  684. }
  685. #endif
  686. /* Computes result = (left - right) % mod.
  687. Assumes that left < mod and right < mod, and that result does not overlap mod. */
  688. #if !asm_modSub
  689. static void vli_modSub(uECC_word_t *result,
  690. const uECC_word_t *left,
  691. const uECC_word_t *right,
  692. const uECC_word_t *mod) {
  693. uECC_word_t l_borrow = vli_sub(result, left, right);
  694. if (l_borrow) {
  695. /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
  696. we can get the correct result from result + mod (with overflow). */
  697. vli_add(result, result, mod);
  698. }
  699. }
  700. #endif
  701. #if !asm_modSub_fast
  702. #define vli_modSub_fast(result, left, right) vli_modSub((result), (left), (right), curve_p)
  703. #endif
  704. #if !asm_mmod_fast
  705. #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1)
  706. /* omega_mult() is defined farther below for the different curves / word sizes */
  707. static void omega_mult(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT right);
  708. /* Computes result = product % curve_p
  709. see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354
  710. Note that this only works if log2(omega) < log2(p) / 2 */
  711. static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product) {
  712. uECC_word_t tmp[2 * uECC_WORDS];
  713. uECC_word_t carry;
  714. vli_clear(tmp);
  715. vli_clear(tmp + uECC_WORDS);
  716. omega_mult(tmp, product + uECC_WORDS); /* (Rq, q) = q * c */
  717. carry = vli_add(result, product, tmp); /* (C, r) = r + q */
  718. vli_clear(product);
  719. omega_mult(product, tmp + uECC_WORDS); /* Rq*c */
  720. carry += vli_add(result, result, product); /* (C1, r) = r + Rq*c */
  721. while (carry > 0) {
  722. --carry;
  723. vli_sub(result, result, curve_p);
  724. }
  725. if (vli_cmp(result, curve_p) > 0) {
  726. vli_sub(result, result, curve_p);
  727. }
  728. }
  729. #endif
  730. #if uECC_CURVE == uECC_secp160r1
  731. #if uECC_WORD_SIZE == 1
  732. static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) {
  733. uint8_t carry;
  734. uint8_t i;
  735. /* Multiply by (2^31 + 1). */
  736. vli_set(result + 4, right); /* 2^32 */
  737. vli_rshift1(result + 4); /* 2^31 */
  738. result[3] = right[0] << 7; /* get last bit from shift */
  739. carry = vli_add(result, result, right); /* 2^31 + 1 */
  740. for (i = uECC_WORDS; carry; ++i) {
  741. uint16_t sum = (uint16_t)result[i] + carry;
  742. result[i] = (uint8_t)sum;
  743. carry = sum >> 8;
  744. }
  745. }
  746. #elif uECC_WORD_SIZE == 4
  747. static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) {
  748. uint32_t carry;
  749. unsigned i;
  750. /* Multiply by (2^31 + 1). */
  751. vli_set(result + 1, right); /* 2^32 */
  752. vli_rshift1(result + 1); /* 2^31 */
  753. result[0] = right[0] << 31; /* get last bit from shift */
  754. carry = vli_add(result, result, right); /* 2^31 + 1 */
  755. for (i = uECC_WORDS; carry; ++i) {
  756. uint64_t sum = (uint64_t)result[i] + carry;
  757. result[i] = (uint32_t)sum;
  758. carry = sum >> 32;
  759. }
  760. }
  761. #endif /* uECC_WORD_SIZE */
  762. #elif uECC_CURVE == uECC_secp192r1
  763. /* Computes result = product % curve_p.
  764. See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */
  765. #if uECC_WORD_SIZE == 1
  766. static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
  767. uint8_t tmp[uECC_WORDS];
  768. uint8_t carry;
  769. vli_set(result, product);
  770. vli_set(tmp, &product[24]);
  771. carry = vli_add(result, result, tmp);
  772. tmp[0] = tmp[1] = tmp[2] = tmp[3] = tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
  773. tmp[8] = product[24]; tmp[9] = product[25]; tmp[10] = product[26]; tmp[11] = product[27];
  774. tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31];
  775. tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35];
  776. tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39];
  777. carry += vli_add(result, result, tmp);
  778. tmp[0] = tmp[8] = product[40];
  779. tmp[1] = tmp[9] = product[41];
  780. tmp[2] = tmp[10] = product[42];
  781. tmp[3] = tmp[11] = product[43];
  782. tmp[4] = tmp[12] = product[44];
  783. tmp[5] = tmp[13] = product[45];
  784. tmp[6] = tmp[14] = product[46];
  785. tmp[7] = tmp[15] = product[47];
  786. tmp[16] = tmp[17] = tmp[18] = tmp[19] = tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
  787. carry += vli_add(result, result, tmp);
  788. while (carry || vli_cmp(curve_p, result) != 1) {
  789. carry -= vli_sub(result, result, curve_p);
  790. }
  791. }
  792. #elif uECC_WORD_SIZE == 4
  793. static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) {
  794. uint32_t tmp[uECC_WORDS];
  795. int carry;
  796. vli_set(result, product);
  797. vli_set(tmp, &product[6]);
  798. carry = vli_add(result, result, tmp);
  799. tmp[0] = tmp[1] = 0;
  800. tmp[2] = product[6];
  801. tmp[3] = product[7];
  802. tmp[4] = product[8];
  803. tmp[5] = product[9];
  804. carry += vli_add(result, result, tmp);
  805. tmp[0] = tmp[2] = product[10];
  806. tmp[1] = tmp[3] = product[11];
  807. tmp[4] = tmp[5] = 0;
  808. carry += vli_add(result, result, tmp);
  809. while (carry || vli_cmp(curve_p, result) != 1) {
  810. carry -= vli_sub(result, result, curve_p);
  811. }
  812. }
  813. #else
  814. static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) {
  815. uint64_t tmp[uECC_WORDS];
  816. int carry;
  817. vli_set(result, product);
  818. vli_set(tmp, &product[3]);
  819. carry = vli_add(result, result, tmp);
  820. tmp[0] = 0;
  821. tmp[1] = product[3];
  822. tmp[2] = product[4];
  823. carry += vli_add(result, result, tmp);
  824. tmp[0] = tmp[1] = product[5];
  825. tmp[2] = 0;
  826. carry += vli_add(result, result, tmp);
  827. while (carry || vli_cmp(curve_p, result) != 1) {
  828. carry -= vli_sub(result, result, curve_p);
  829. }
  830. }
  831. #endif /* uECC_WORD_SIZE */
  832. #elif uECC_CURVE == uECC_secp256r1
  833. /* Computes result = product % curve_p
  834. from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  835. #if uECC_WORD_SIZE == 1
  836. static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
  837. uint8_t tmp[uECC_BYTES];
  838. int8_t carry;
  839. /* t */
  840. vli_set(result, product);
  841. /* s1 */
  842. tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
  843. tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
  844. tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
  845. tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47];
  846. tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51];
  847. tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55];
  848. tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59];
  849. tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63];
  850. carry = vli_add(tmp, tmp, tmp);
  851. carry += vli_add(result, result, tmp);
  852. /* s2 */
  853. tmp[12] = product[48]; tmp[13] = product[49]; tmp[14] = product[50]; tmp[15] = product[51];
  854. tmp[16] = product[52]; tmp[17] = product[53]; tmp[18] = product[54]; tmp[19] = product[55];
  855. tmp[20] = product[56]; tmp[21] = product[57]; tmp[22] = product[58]; tmp[23] = product[59];
  856. tmp[24] = product[60]; tmp[25] = product[61]; tmp[26] = product[62]; tmp[27] = product[63];
  857. tmp[28] = tmp[29] = tmp[30] = tmp[31] = 0;
  858. carry += vli_add(tmp, tmp, tmp);
  859. carry += vli_add(result, result, tmp);
  860. /* s3 */
  861. tmp[0] = product[32]; tmp[1] = product[33]; tmp[2] = product[34]; tmp[3] = product[35];
  862. tmp[4] = product[36]; tmp[5] = product[37]; tmp[6] = product[38]; tmp[7] = product[39];
  863. tmp[8] = product[40]; tmp[9] = product[41]; tmp[10] = product[42]; tmp[11] = product[43];
  864. tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
  865. tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
  866. tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
  867. tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59];
  868. tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63];
  869. carry += vli_add(result, result, tmp);
  870. /* s4 */
  871. tmp[0] = product[36]; tmp[1] = product[37]; tmp[2] = product[38]; tmp[3] = product[39];
  872. tmp[4] = product[40]; tmp[5] = product[41]; tmp[6] = product[42]; tmp[7] = product[43];
  873. tmp[8] = product[44]; tmp[9] = product[45]; tmp[10] = product[46]; tmp[11] = product[47];
  874. tmp[12] = product[52]; tmp[13] = product[53]; tmp[14] = product[54]; tmp[15] = product[55];
  875. tmp[16] = product[56]; tmp[17] = product[57]; tmp[18] = product[58]; tmp[19] = product[59];
  876. tmp[20] = product[60]; tmp[21] = product[61]; tmp[22] = product[62]; tmp[23] = product[63];
  877. tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55];
  878. tmp[28] = product[32]; tmp[29] = product[33]; tmp[30] = product[34]; tmp[31] = product[35];
  879. carry += vli_add(result, result, tmp);
  880. /* d1 */
  881. tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47];
  882. tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51];
  883. tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55];
  884. tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
  885. tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
  886. tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
  887. tmp[24] = product[32]; tmp[25] = product[33]; tmp[26] = product[34]; tmp[27] = product[35];
  888. tmp[28] = product[40]; tmp[29] = product[41]; tmp[30] = product[42]; tmp[31] = product[43];
  889. carry -= vli_sub(result, result, tmp);
  890. /* d2 */
  891. tmp[0] = product[48]; tmp[1] = product[49]; tmp[2] = product[50]; tmp[3] = product[51];
  892. tmp[4] = product[52]; tmp[5] = product[53]; tmp[6] = product[54]; tmp[7] = product[55];
  893. tmp[8] = product[56]; tmp[9] = product[57]; tmp[10] = product[58]; tmp[11] = product[59];
  894. tmp[12] = product[60]; tmp[13] = product[61]; tmp[14] = product[62]; tmp[15] = product[63];
  895. tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
  896. tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
  897. tmp[24] = product[36]; tmp[25] = product[37]; tmp[26] = product[38]; tmp[27] = product[39];
  898. tmp[28] = product[44]; tmp[29] = product[45]; tmp[30] = product[46]; tmp[31] = product[47];
  899. carry -= vli_sub(result, result, tmp);
  900. /* d3 */
  901. tmp[0] = product[52]; tmp[1] = product[53]; tmp[2] = product[54]; tmp[3] = product[55];
  902. tmp[4] = product[56]; tmp[5] = product[57]; tmp[6] = product[58]; tmp[7] = product[59];
  903. tmp[8] = product[60]; tmp[9] = product[61]; tmp[10] = product[62]; tmp[11] = product[63];
  904. tmp[12] = product[32]; tmp[13] = product[33]; tmp[14] = product[34]; tmp[15] = product[35];
  905. tmp[16] = product[36]; tmp[17] = product[37]; tmp[18] = product[38]; tmp[19] = product[39];
  906. tmp[20] = product[40]; tmp[21] = product[41]; tmp[22] = product[42]; tmp[23] = product[43];
  907. tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
  908. tmp[28] = product[48]; tmp[29] = product[49]; tmp[30] = product[50]; tmp[31] = product[51];
  909. carry -= vli_sub(result, result, tmp);
  910. /* d4 */
  911. tmp[0] = product[56]; tmp[1] = product[57]; tmp[2] = product[58]; tmp[3] = product[59];
  912. tmp[4] = product[60]; tmp[5] = product[61]; tmp[6] = product[62]; tmp[7] = product[63];
  913. tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
  914. tmp[12] = product[36]; tmp[13] = product[37]; tmp[14] = product[38]; tmp[15] = product[39];
  915. tmp[16] = product[40]; tmp[17] = product[41]; tmp[18] = product[42]; tmp[19] = product[43];
  916. tmp[20] = product[44]; tmp[21] = product[45]; tmp[22] = product[46]; tmp[23] = product[47];
  917. tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
  918. tmp[28] = product[52]; tmp[29] = product[53]; tmp[30] = product[54]; tmp[31] = product[55];
  919. carry -= vli_sub(result, result, tmp);
  920. if (carry < 0) {
  921. do {
  922. carry += vli_add(result, result, curve_p);
  923. } while (carry < 0);
  924. } else {
  925. while (carry || vli_cmp(curve_p, result) != 1) {
  926. carry -= vli_sub(result, result, curve_p);
  927. }
  928. }
  929. }
  930. #elif uECC_WORD_SIZE == 4
  931. static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) {
  932. uint32_t tmp[uECC_WORDS];
  933. int carry;
  934. /* t */
  935. vli_set(result, product);
  936. /* s1 */
  937. tmp[0] = tmp[1] = tmp[2] = 0;
  938. tmp[3] = product[11];
  939. tmp[4] = product[12];
  940. tmp[5] = product[13];
  941. tmp[6] = product[14];
  942. tmp[7] = product[15];
  943. carry = vli_add(tmp, tmp, tmp);
  944. carry += vli_add(result, result, tmp);
  945. /* s2 */
  946. tmp[3] = product[12];
  947. tmp[4] = product[13];
  948. tmp[5] = product[14];
  949. tmp[6] = product[15];
  950. tmp[7] = 0;
  951. carry += vli_add(tmp, tmp, tmp);
  952. carry += vli_add(result, result, tmp);
  953. /* s3 */
  954. tmp[0] = product[8];
  955. tmp[1] = product[9];
  956. tmp[2] = product[10];
  957. tmp[3] = tmp[4] = tmp[5] = 0;
  958. tmp[6] = product[14];
  959. tmp[7] = product[15];
  960. carry += vli_add(result, result, tmp);
  961. /* s4 */
  962. tmp[0] = product[9];
  963. tmp[1] = product[10];
  964. tmp[2] = product[11];
  965. tmp[3] = product[13];
  966. tmp[4] = product[14];
  967. tmp[5] = product[15];
  968. tmp[6] = product[13];
  969. tmp[7] = product[8];
  970. carry += vli_add(result, result, tmp);
  971. /* d1 */
  972. tmp[0] = product[11];
  973. tmp[1] = product[12];
  974. tmp[2] = product[13];
  975. tmp[3] = tmp[4] = tmp[5] = 0;
  976. tmp[6] = product[8];
  977. tmp[7] = product[10];
  978. carry -= vli_sub(result, result, tmp);
  979. /* d2 */
  980. tmp[0] = product[12];
  981. tmp[1] = product[13];
  982. tmp[2] = product[14];
  983. tmp[3] = product[15];
  984. tmp[4] = tmp[5] = 0;
  985. tmp[6] = product[9];
  986. tmp[7] = product[11];
  987. carry -= vli_sub(result, result, tmp);
  988. /* d3 */
  989. tmp[0] = product[13];
  990. tmp[1] = product[14];
  991. tmp[2] = product[15];
  992. tmp[3] = product[8];
  993. tmp[4] = product[9];
  994. tmp[5] = product[10];
  995. tmp[6] = 0;
  996. tmp[7] = product[12];
  997. carry -= vli_sub(result, result, tmp);
  998. /* d4 */
  999. tmp[0] = product[14];
  1000. tmp[1] = product[15];
  1001. tmp[2] = 0;
  1002. tmp[3] = product[9];
  1003. tmp[4] = product[10];
  1004. tmp[5] = product[11];
  1005. tmp[6] = 0;
  1006. tmp[7] = product[13];
  1007. carry -= vli_sub(result, result, tmp);
  1008. if (carry < 0) {
  1009. do {
  1010. carry += vli_add(result, result, curve_p);
  1011. } while (carry < 0);
  1012. } else {
  1013. while (carry || vli_cmp(curve_p, result) != 1) {
  1014. carry -= vli_sub(result, result, curve_p);
  1015. }
  1016. }
  1017. }
  1018. #else
  1019. static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) {
  1020. uint64_t tmp[uECC_WORDS];
  1021. int carry;
  1022. /* t */
  1023. vli_set(result, product);
  1024. /* s1 */
  1025. tmp[0] = 0;
  1026. tmp[1] = product[5] & 0xffffffff00000000ull;
  1027. tmp[2] = product[6];
  1028. tmp[3] = product[7];
  1029. carry = vli_add(tmp, tmp, tmp);
  1030. carry += vli_add(result, result, tmp);
  1031. /* s2 */
  1032. tmp[1] = product[6] << 32;
  1033. tmp[2] = (product[6] >> 32) | (product[7] << 32);
  1034. tmp[3] = product[7] >> 32;
  1035. carry += vli_add(tmp, tmp, tmp);
  1036. carry += vli_add(result, result, tmp);
  1037. /* s3 */
  1038. tmp[0] = product[4];
  1039. tmp[1] = product[5] & 0xffffffff;
  1040. tmp[2] = 0;
  1041. tmp[3] = product[7];
  1042. carry += vli_add(result, result, tmp);
  1043. /* s4 */
  1044. tmp[0] = (product[4] >> 32) | (product[5] << 32);
  1045. tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
  1046. tmp[2] = product[7];
  1047. tmp[3] = (product[6] >> 32) | (product[4] << 32);
  1048. carry += vli_add(result, result, tmp);
  1049. /* d1 */
  1050. tmp[0] = (product[5] >> 32) | (product[6] << 32);
  1051. tmp[1] = (product[6] >> 32);
  1052. tmp[2] = 0;
  1053. tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
  1054. carry -= vli_sub(result, result, tmp);
  1055. /* d2 */
  1056. tmp[0] = product[6];
  1057. tmp[1] = product[7];
  1058. tmp[2] = 0;
  1059. tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
  1060. carry -= vli_sub(result, result, tmp);
  1061. /* d3 */
  1062. tmp[0] = (product[6] >> 32) | (product[7] << 32);
  1063. tmp[1] = (product[7] >> 32) | (product[4] << 32);
  1064. tmp[2] = (product[4] >> 32) | (product[5] << 32);
  1065. tmp[3] = (product[6] << 32);
  1066. carry -= vli_sub(result, result, tmp);
  1067. /* d4 */
  1068. tmp[0] = product[7];
  1069. tmp[1] = product[4] & 0xffffffff00000000ull;
  1070. tmp[2] = product[5];
  1071. tmp[3] = product[6] & 0xffffffff00000000ull;
  1072. carry -= vli_sub(result, result, tmp);
  1073. if (carry < 0) {
  1074. do {
  1075. carry += vli_add(result, result, curve_p);
  1076. } while (carry < 0);
  1077. } else {
  1078. while (carry || vli_cmp(curve_p, result) != 1) {
  1079. carry -= vli_sub(result, result, curve_p);
  1080. }
  1081. }
  1082. }
  1083. #endif /* uECC_WORD_SIZE */
  1084. #elif uECC_CURVE == uECC_secp256k1
  1085. #if uECC_WORD_SIZE == 1
  1086. static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) {
  1087. /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
  1088. uECC_word_t r0 = 0;
  1089. uECC_word_t r1 = 0;
  1090. uECC_word_t r2 = 0;
  1091. wordcount_t k;
  1092. /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
  1093. muladd(0xD1, right[0], &r0, &r1, &r2);
  1094. result[0] = r0;
  1095. r0 = r1;
  1096. r1 = r2;
  1097. /* r2 is still 0 */
  1098. for (k = 1; k < uECC_WORDS; ++k) {
  1099. muladd(0x03, right[k - 1], &r0, &r1, &r2);
  1100. muladd(0xD1, right[k], &r0, &r1, &r2);
  1101. result[k] = r0;
  1102. r0 = r1;
  1103. r1 = r2;
  1104. r2 = 0;
  1105. }
  1106. muladd(0x03, right[uECC_WORDS - 1], &r0, &r1, &r2);
  1107. result[uECC_WORDS] = r0;
  1108. result[uECC_WORDS + 1] = r1;
  1109. result[4 + uECC_WORDS] = vli_add(result + 4, result + 4, right); /* add the 2^32 multiple */
  1110. }
  1111. #elif uECC_WORD_SIZE == 4
  1112. static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) {
  1113. /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
  1114. uint32_t carry = 0;
  1115. wordcount_t k;
  1116. for (k = 0; k < uECC_WORDS; ++k) {
  1117. uint64_t p = (uint64_t)0x3D1 * right[k] + carry;
  1118. result[k] = (p & 0xffffffff);
  1119. carry = p >> 32;
  1120. }
  1121. result[uECC_WORDS] = carry;
  1122. result[1 + uECC_WORDS] = vli_add(result + 1, result + 1, right); /* add the 2^32 multiple */
  1123. }
  1124. #else
  1125. static void omega_mult(uint64_t * RESTRICT result, const uint64_t * RESTRICT right) {
  1126. uECC_word_t r0 = 0;
  1127. uECC_word_t r1 = 0;
  1128. uECC_word_t r2 = 0;
  1129. wordcount_t k;
  1130. /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
  1131. for (k = 0; k < uECC_WORDS; ++k) {
  1132. muladd(0x1000003D1ull, right[k], &r0, &r1, &r2);
  1133. result[k] = r0;
  1134. r0 = r1;
  1135. r1 = r2;
  1136. r2 = 0;
  1137. }
  1138. result[uECC_WORDS] = r0;
  1139. }
  1140. #endif /* uECC_WORD_SIZE */
  1141. #elif uECC_CURVE == uECC_secp224r1
  1142. /* Computes result = product % curve_p
  1143. from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  1144. #if uECC_WORD_SIZE == 1
  1145. // TODO it may be faster to use the omega_mult method when fully asm optimized.
  1146. void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
  1147. uint8_t tmp[uECC_WORDS];
  1148. int8_t carry;
  1149. /* t */
  1150. vli_set(result, product);
  1151. /* s1 */
  1152. tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
  1153. tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
  1154. tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
  1155. tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31];
  1156. tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35];
  1157. tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39];
  1158. tmp[24] = product[40]; tmp[25] = product[41]; tmp[26] = product[42]; tmp[27] = product[43];
  1159. carry = vli_add(result, result, tmp);
  1160. /* s2 */
  1161. tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47];
  1162. tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51];
  1163. tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55];
  1164. tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
  1165. carry += vli_add(result, result, tmp);
  1166. /* d1 */
  1167. tmp[0] = product[28]; tmp[1] = product[29]; tmp[2] = product[30]; tmp[3] = product[31];
  1168. tmp[4] = product[32]; tmp[5] = product[33]; tmp[6] = product[34]; tmp[7] = product[35];
  1169. tmp[8] = product[36]; tmp[9] = product[37]; tmp[10] = product[38]; tmp[11] = product[39];
  1170. tmp[12] = product[40]; tmp[13] = product[41]; tmp[14] = product[42]; tmp[15] = product[43];
  1171. tmp[16] = product[44]; tmp[17] = product[45]; tmp[18] = product[46]; tmp[19] = product[47];
  1172. tmp[20] = product[48]; tmp[21] = product[49]; tmp[22] = product[50]; tmp[23] = product[51];
  1173. tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55];
  1174. carry -= vli_sub(result, result, tmp);
  1175. /* d2 */
  1176. tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47];
  1177. tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51];
  1178. tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55];
  1179. tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
  1180. tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
  1181. tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
  1182. tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
  1183. carry -= vli_sub(result, result, tmp);
  1184. if (carry < 0) {
  1185. do {
  1186. carry += vli_add(result, result, curve_p);
  1187. } while (carry < 0);
  1188. } else {
  1189. while (carry || vli_cmp(curve_p, result) != 1) {
  1190. carry -= vli_sub(result, result, curve_p);
  1191. }
  1192. }
  1193. }
  1194. #elif uECC_WORD_SIZE == 4
  1195. void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product)
  1196. {
  1197. uint32_t tmp[uECC_WORDS];
  1198. int carry;
  1199. /* t */
  1200. vli_set(result, product);
  1201. /* s1 */
  1202. tmp[0] = tmp[1] = tmp[2] = 0;
  1203. tmp[3] = product[7];
  1204. tmp[4] = product[8];
  1205. tmp[5] = product[9];
  1206. tmp[6] = product[10];
  1207. carry = vli_add(result, result, tmp);
  1208. /* s2 */
  1209. tmp[3] = product[11];
  1210. tmp[4] = product[12];
  1211. tmp[5] = product[13];
  1212. tmp[6] = 0;
  1213. carry += vli_add(result, result, tmp);
  1214. /* d1 */
  1215. tmp[0] = product[7];
  1216. tmp[1] = product[8];
  1217. tmp[2] = product[9];
  1218. tmp[3] = product[10];
  1219. tmp[4] = product[11];
  1220. tmp[5] = product[12];
  1221. tmp[6] = product[13];
  1222. carry -= vli_sub(result, result, tmp);
  1223. /* d2 */
  1224. tmp[0] = product[11];
  1225. tmp[1] = product[12];
  1226. tmp[2] = product[13];
  1227. tmp[3] = tmp[4] = tmp[5] = tmp[6] = 0;
  1228. carry -= vli_sub(result, result, tmp);
  1229. if (carry < 0) {
  1230. do {
  1231. carry += vli_add(result, result, curve_p);
  1232. } while (carry < 0);
  1233. } else {
  1234. while (carry || vli_cmp(curve_p, result) != 1) {
  1235. carry -= vli_sub(result, result, curve_p);
  1236. }
  1237. }
  1238. }
  1239. #endif /* uECC_WORD_SIZE */
  1240. #endif /* uECC_CURVE */
  1241. #endif /* !asm_mmod_fast */
  1242. /* Computes result = (left * right) % curve_p. */
  1243. static void vli_modMult_fast(uECC_word_t *result,
  1244. const uECC_word_t *left,
  1245. const uECC_word_t *right) {
  1246. uECC_word_t product[2 * uECC_WORDS];
  1247. vli_mult(product, left, right);
  1248. vli_mmod_fast(result, product);
  1249. }
  1250. #if uECC_SQUARE_FUNC
  1251. /* Computes result = left^2 % curve_p. */
  1252. static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left) {
  1253. uECC_word_t product[2 * uECC_WORDS];
  1254. vli_square(product, left);
  1255. vli_mmod_fast(result, product);
  1256. }
  1257. #else /* uECC_SQUARE_FUNC */
  1258. #define vli_modSquare_fast(result, left) vli_modMult_fast((result), (left), (left))
  1259. #endif /* uECC_SQUARE_FUNC */
  1260. #define EVEN(vli) (!(vli[0] & 1))
  1261. /* Computes result = (1 / input) % mod. All VLIs are the same size.
  1262. See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
  1263. https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */
  1264. #if !asm_modInv
  1265. static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) {
  1266. uECC_word_t a[uECC_WORDS], b[uECC_WORDS], u[uECC_WORDS], v[uECC_WORDS];
  1267. uECC_word_t carry;
  1268. cmpresult_t cmpResult;
  1269. if (vli_isZero(input)) {
  1270. vli_clear(result);
  1271. return;
  1272. }
  1273. vli_set(a, input);
  1274. vli_set(b, mod);
  1275. vli_clear(u);
  1276. u[0] = 1;
  1277. vli_clear(v);
  1278. while ((cmpResult = vli_cmp(a, b)) != 0) {
  1279. carry = 0;
  1280. if (EVEN(a)) {
  1281. vli_rshift1(a);
  1282. if (!EVEN(u)) {
  1283. carry = vli_add(u, u, mod);
  1284. }
  1285. vli_rshift1(u);
  1286. if (carry) {
  1287. u[uECC_WORDS - 1] |= HIGH_BIT_SET;
  1288. }
  1289. } else if (EVEN(b)) {
  1290. vli_rshift1(b);
  1291. if (!EVEN(v)) {
  1292. carry = vli_add(v, v, mod);
  1293. }
  1294. vli_rshift1(v);
  1295. if (carry) {
  1296. v[uECC_WORDS - 1] |= HIGH_BIT_SET;
  1297. }
  1298. } else if (cmpResult > 0) {
  1299. vli_sub(a, a, b);
  1300. vli_rshift1(a);
  1301. if (vli_cmp(u, v) < 0) {
  1302. vli_add(u, u, mod);
  1303. }
  1304. vli_sub(u, u, v);
  1305. if (!EVEN(u)) {
  1306. carry = vli_add(u, u, mod);
  1307. }
  1308. vli_rshift1(u);
  1309. if (carry) {
  1310. u[uECC_WORDS - 1] |= HIGH_BIT_SET;
  1311. }
  1312. } else {
  1313. vli_sub(b, b, a);
  1314. vli_rshift1(b);
  1315. if (vli_cmp(v, u) < 0) {
  1316. vli_add(v, v, mod);
  1317. }
  1318. vli_sub(v, v, u);
  1319. if (!EVEN(v)) {
  1320. carry = vli_add(v, v, mod);
  1321. }
  1322. vli_rshift1(v);
  1323. if (carry) {
  1324. v[uECC_WORDS - 1] |= HIGH_BIT_SET;
  1325. }
  1326. }
  1327. }
  1328. vli_set(result, u);
  1329. }
  1330. #endif /* !asm_modInv */
  1331. /* ------ Point operations ------ */
  1332. /* Returns 1 if 'point' is the point at infinity, 0 otherwise. */
  1333. static cmpresult_t EccPoint_isZero(const EccPoint *point) {
  1334. return (vli_isZero(point->x) && vli_isZero(point->y));
  1335. }
  1336. /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates.
  1337. From http://eprint.iacr.org/2011/338.pdf
  1338. */
  1339. /* Double in place */
  1340. #if (uECC_CURVE == uECC_secp256k1)
  1341. static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1,
  1342. uECC_word_t * RESTRICT Y1,
  1343. uECC_word_t * RESTRICT Z1) {
  1344. /* t1 = X, t2 = Y, t3 = Z */
  1345. uECC_word_t t4[uECC_WORDS];
  1346. uECC_word_t t5[uECC_WORDS];
  1347. if (vli_isZero(Z1)) {
  1348. return;
  1349. }
  1350. vli_modSquare_fast(t5, Y1); /* t5 = y1^2 */
  1351. vli_modMult_fast(t4, X1, t5); /* t4 = x1*y1^2 = A */
  1352. vli_modSquare_fast(X1, X1); /* t1 = x1^2 */
  1353. vli_modSquare_fast(t5, t5); /* t5 = y1^4 */
  1354. vli_modMult_fast(Z1, Y1, Z1); /* t3 = y1*z1 = z3 */
  1355. vli_modAdd(Y1, X1, X1, curve_p); /* t2 = 2*x1^2 */
  1356. vli_modAdd(Y1, Y1, X1, curve_p); /* t2 = 3*x1^2 */
  1357. if (vli_testBit(Y1, 0)) {
  1358. uECC_word_t carry = vli_add(Y1, Y1, curve_p);
  1359. vli_rshift1(Y1);
  1360. Y1[uECC_WORDS - 1] |= carry << (uECC_WORD_BITS - 1);
  1361. } else {
  1362. vli_rshift1(Y1);
  1363. }
  1364. /* t2 = 3/2*(x1^2) = B */
  1365. vli_modSquare_fast(X1, Y1); /* t1 = B^2 */
  1366. vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - A */
  1367. vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - 2A = x3 */
  1368. vli_modSub(t4, t4, X1, curve_p); /* t4 = A - x3 */
  1369. vli_modMult_fast(Y1, Y1, t4); /* t2 = B * (A - x3) */
  1370. vli_modSub(Y1, Y1, t5, curve_p); /* t2 = B * (A - x3) - y1^4 = y3 */
  1371. }
  1372. #else
  1373. static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1,
  1374. uECC_word_t * RESTRICT Y1,
  1375. uECC_word_t * RESTRICT Z1) {
  1376. /* t1 = X, t2 = Y, t3 = Z */
  1377. uECC_word_t t4[uECC_WORDS];
  1378. uECC_word_t t5[uECC_WORDS];
  1379. if (vli_isZero(Z1)) {
  1380. return;
  1381. }
  1382. vli_modSquare_fast(t4, Y1); /* t4 = y1^2 */
  1383. vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
  1384. vli_modSquare_fast(t4, t4); /* t4 = y1^4 */
  1385. vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
  1386. vli_modSquare_fast(Z1, Z1); /* t3 = z1^2 */
  1387. vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
  1388. vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
  1389. vli_modSub_fast(Z1, X1, Z1); /* t3 = x1 - z1^2 */
  1390. vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
  1391. vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
  1392. vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
  1393. if (vli_testBit(X1, 0)) {
  1394. uECC_word_t l_carry = vli_add(X1, X1, curve_p);
  1395. vli_rshift1(X1);
  1396. X1[uECC_WORDS - 1] |= l_carry << (uECC_WORD_BITS - 1);
  1397. } else {
  1398. vli_rshift1(X1);
  1399. }
  1400. /* t1 = 3/2*(x1^2 - z1^4) = B */
  1401. vli_modSquare_fast(Z1, X1); /* t3 = B^2 */
  1402. vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - A */
  1403. vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - 2A = x3 */
  1404. vli_modSub_fast(t5, t5, Z1); /* t5 = A - x3 */
  1405. vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
  1406. vli_modSub_fast(t4, X1, t4); /* t4 = B * (A - x3) - y1^4 = y3 */
  1407. vli_set(X1, Z1);
  1408. vli_set(Z1, Y1);
  1409. vli_set(Y1, t4);
  1410. }
  1411. #endif
  1412. /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
  1413. static void apply_z(uECC_word_t * RESTRICT X1,
  1414. uECC_word_t * RESTRICT Y1,
  1415. const uECC_word_t * RESTRICT Z) {
  1416. uECC_word_t t1[uECC_WORDS];
  1417. vli_modSquare_fast(t1, Z); /* z^2 */
  1418. vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
  1419. vli_modMult_fast(t1, t1, Z); /* z^3 */
  1420. vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
  1421. }
  1422. /* P = (x1, y1) => 2P, (x2, y2) => P' */
  1423. static void XYcZ_initial_double(uECC_word_t * RESTRICT X1,
  1424. uECC_word_t * RESTRICT Y1,
  1425. uECC_word_t * RESTRICT X2,
  1426. uECC_word_t * RESTRICT Y2,
  1427. const uECC_word_t * RESTRICT initial_Z) {
  1428. uECC_word_t z[uECC_WORDS];
  1429. if (initial_Z) {
  1430. vli_set(z, initial_Z);
  1431. } else {
  1432. vli_clear(z);
  1433. z[0] = 1;
  1434. }
  1435. vli_set(X2, X1);
  1436. vli_set(Y2, Y1);
  1437. apply_z(X1, Y1, z);
  1438. EccPoint_double_jacobian(X1, Y1, z);
  1439. apply_z(X2, Y2, z);
  1440. }
  1441. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  1442. Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
  1443. or P => P', Q => P + Q
  1444. */
  1445. static void XYcZ_add(uECC_word_t * RESTRICT X1,
  1446. uECC_word_t * RESTRICT Y1,
  1447. uECC_word_t * RESTRICT X2,
  1448. uECC_word_t * RESTRICT Y2) {
  1449. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  1450. uECC_word_t t5[uECC_WORDS];
  1451. vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */
  1452. vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
  1453. vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
  1454. vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
  1455. vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */
  1456. vli_modSquare_fast(t5, Y2); /* t5 = (y2 - y1)^2 = D */
  1457. vli_modSub_fast(t5, t5, X1); /* t5 = D - B */
  1458. vli_modSub_fast(t5, t5, X2); /* t5 = D - B - C = x3 */
  1459. vli_modSub_fast(X2, X2, X1); /* t3 = C - B */
  1460. vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */
  1461. vli_modSub_fast(X2, X1, t5); /* t3 = B - x3 */
  1462. vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */
  1463. vli_modSub_fast(Y2, Y2, Y1); /* t4 = y3 */
  1464. vli_set(X2, t5);
  1465. }
  1466. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  1467. Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
  1468. or P => P - Q, Q => P + Q
  1469. */
  1470. static void XYcZ_addC(uECC_word_t * RESTRICT X1,
  1471. uECC_word_t * RESTRICT Y1,
  1472. uECC_word_t * RESTRICT X2,
  1473. uECC_word_t * RESTRICT Y2) {
  1474. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  1475. uECC_word_t t5[uECC_WORDS];
  1476. uECC_word_t t6[uECC_WORDS];
  1477. uECC_word_t t7[uECC_WORDS];
  1478. vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */
  1479. vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
  1480. vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
  1481. vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
  1482. vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */
  1483. vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */
  1484. vli_modSub_fast(t6, X2, X1); /* t6 = C - B */
  1485. vli_modMult_fast(Y1, Y1, t6); /* t2 = y1 * (C - B) = E */
  1486. vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
  1487. vli_modSquare_fast(X2, Y2); /* t3 = (y2 - y1)^2 = D */
  1488. vli_modSub_fast(X2, X2, t6); /* t3 = D - (B + C) = x3 */
  1489. vli_modSub_fast(t7, X1, X2); /* t7 = B - x3 */
  1490. vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */
  1491. vli_modSub_fast(Y2, Y2, Y1); /* t4 = (y2 - y1)*(B - x3) - E = y3 */
  1492. vli_modSquare_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */
  1493. vli_modSub_fast(t7, t7, t6); /* t7 = F - (B + C) = x3' */
  1494. vli_modSub_fast(t6, t7, X1); /* t6 = x3' - B */
  1495. vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */
  1496. vli_modSub_fast(Y1, t6, Y1); /* t2 = (y2 + y1)*(x3' - B) - E = y3' */
  1497. vli_set(X1, t7);
  1498. }
  1499. static void EccPoint_mult(EccPoint * RESTRICT result,
  1500. const EccPoint * RESTRICT point,
  1501. const uECC_word_t * RESTRICT scalar,
  1502. const uECC_word_t * RESTRICT initialZ,
  1503. bitcount_t numBits) {
  1504. /* R0 and R1 */
  1505. uECC_word_t Rx[2][uECC_WORDS];
  1506. uECC_word_t Ry[2][uECC_WORDS];
  1507. uECC_word_t z[uECC_WORDS];
  1508. bitcount_t i;
  1509. uECC_word_t nb;
  1510. vli_set(Rx[1], point->x);
  1511. vli_set(Ry[1], point->y);
  1512. XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initialZ);
  1513. for (i = numBits - 2; i > 0; --i) {
  1514. nb = !vli_testBit(scalar, i);
  1515. XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]);
  1516. XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]);
  1517. }
  1518. nb = !vli_testBit(scalar, 0);
  1519. XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]);
  1520. /* Find final 1/Z value. */
  1521. vli_modSub_fast(z, Rx[1], Rx[0]); /* X1 - X0 */
  1522. vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
  1523. vli_modMult_fast(z, z, point->x); /* xP * Yb * (X1 - X0) */
  1524. vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */
  1525. vli_modMult_fast(z, z, point->y); /* yP / (xP * Yb * (X1 - X0)) */
  1526. vli_modMult_fast(z, z, Rx[1 - nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */
  1527. /* End 1/Z calculation */
  1528. XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]);
  1529. apply_z(Rx[0], Ry[0], z);
  1530. vli_set(result->x, Rx[0]);
  1531. vli_set(result->y, Ry[0]);
  1532. }
  1533. static int EccPoint_compute_public_key(EccPoint *result, uECC_word_t *private) {
  1534. uECC_word_t tmp1[uECC_WORDS];
  1535. uECC_word_t tmp2[uECC_WORDS];
  1536. uECC_word_t *p2[2] = {tmp1, tmp2};
  1537. uECC_word_t carry;
  1538. /* Make sure the private key is in the range [1, n-1]. */
  1539. if (vli_isZero(private)) {
  1540. return 0;
  1541. }
  1542. #if (uECC_CURVE == uECC_secp160r1)
  1543. // Don't regularize the bitcount for secp160r1, since it would have a larger performance
  1544. // impact (about 2% slower on average) and requires the vli_xxx_n functions, leading to
  1545. // a significant increase in code size.
  1546. EccPoint_mult(result, &curve_G, private, NULL, vli_numBits(private, uECC_WORDS));
  1547. #else
  1548. if (vli_cmp(curve_n, private) != 1) {
  1549. return 0;
  1550. }
  1551. // Regularize the bitcount for the private key so that attackers cannot use a side channel
  1552. // attack to learn the number of leading zeros.
  1553. carry = vli_add(tmp1, private, curve_n);
  1554. vli_add(tmp2, tmp1, curve_n);
  1555. EccPoint_mult(result, &curve_G, p2[!carry], NULL, (uECC_BYTES * 8) + 1);
  1556. #endif
  1557. if (EccPoint_isZero(result)) {
  1558. return 0;
  1559. }
  1560. return 1;
  1561. }
  1562. #if uECC_CURVE == uECC_secp224r1
  1563. /* Routine 3.2.4 RS; from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  1564. static void mod_sqrt_secp224r1_rs(uECC_word_t *d1,
  1565. uECC_word_t *e1,
  1566. uECC_word_t *f1,
  1567. const uECC_word_t *d0,
  1568. const uECC_word_t *e0,
  1569. const uECC_word_t *f0) {
  1570. uECC_word_t t[uECC_WORDS];
  1571. vli_modSquare_fast(t, d0); /* t <-- d0 ^ 2 */
  1572. vli_modMult_fast(e1, d0, e0); /* e1 <-- d0 * e0 */
  1573. vli_modAdd(d1, t, f0, curve_p); /* d1 <-- t + f0 */
  1574. vli_modAdd(e1, e1, e1, curve_p); /* e1 <-- e1 + e1 */
  1575. vli_modMult_fast(f1, t, f0); /* f1 <-- t * f0 */
  1576. vli_modAdd(f1, f1, f1, curve_p); /* f1 <-- f1 + f1 */
  1577. vli_modAdd(f1, f1, f1, curve_p); /* f1 <-- f1 + f1 */
  1578. }
  1579. /* Routine 3.2.5 RSS; from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  1580. static void mod_sqrt_secp224r1_rss(uECC_word_t *d1,
  1581. uECC_word_t *e1,
  1582. uECC_word_t *f1,
  1583. const uECC_word_t *d0,
  1584. const uECC_word_t *e0,
  1585. const uECC_word_t *f0,
  1586. const bitcount_t j) {
  1587. bitcount_t i;
  1588. vli_set(d1, d0); /* d1 <-- d0 */
  1589. vli_set(e1, e0); /* e1 <-- e0 */
  1590. vli_set(f1, f0); /* f1 <-- f0 */
  1591. for (i = 1; i <= j; i++) {
  1592. mod_sqrt_secp224r1_rs(d1, e1, f1, d1, e1, f1); /* RS (d1,e1,f1,d1,e1,f1) */
  1593. }
  1594. }
  1595. /* Routine 3.2.6 RM; from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  1596. static void mod_sqrt_secp224r1_rm(uECC_word_t *d2,
  1597. uECC_word_t *e2,
  1598. uECC_word_t *f2,
  1599. const uECC_word_t *c,
  1600. const uECC_word_t *d0,
  1601. const uECC_word_t *e0,
  1602. const uECC_word_t *d1,
  1603. const uECC_word_t *e1) {
  1604. uECC_word_t t1[uECC_WORDS];
  1605. uECC_word_t t2[uECC_WORDS];
  1606. vli_modMult_fast(t1, e0, e1); /* t1 <-- e0 * e1 */
  1607. vli_modMult_fast(t1, t1, c); /* t1 <-- t1 * c */
  1608. vli_modSub_fast(t1, curve_p, t1); /* t1 <-- p - t1 */
  1609. vli_modMult_fast(t2, d0, d1); /* t2 <-- d0 * d1 */
  1610. vli_modAdd(t2, t2, t1, curve_p); /* t2 <-- t2 + t1 */
  1611. vli_modMult_fast(t1, d0, e1); /* t1 <-- d0 * e1 */
  1612. vli_modMult_fast(e2, d1, e0); /* e2 <-- d1 * e0 */
  1613. vli_modAdd(e2, e2, t1, curve_p); /* e2 <-- e2 + t1 */
  1614. vli_modSquare_fast(f2, e2); /* f2 <-- e2^2 */
  1615. vli_modMult_fast(f2, f2, c); /* f2 <-- f2 * c */
  1616. vli_modSub_fast(f2, curve_p, f2); /* f2 <-- p - f2 */
  1617. vli_set(d2, t2); /* d2 <-- t2 */
  1618. }
  1619. /* Routine 3.2.7 RP; from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  1620. static void mod_sqrt_secp224r1_rp(uECC_word_t *d1,
  1621. uECC_word_t *e1,
  1622. uECC_word_t *f1,
  1623. const uECC_word_t *c,
  1624. const uECC_word_t *r) {
  1625. wordcount_t i;
  1626. wordcount_t pow2i = 1;
  1627. uECC_word_t d0[uECC_WORDS];
  1628. uECC_word_t e0[uECC_WORDS] = {1}; /* e0 <-- 1 */
  1629. uECC_word_t f0[uECC_WORDS];
  1630. vli_set(d0, r); /* d0 <-- r */
  1631. vli_modSub_fast(f0, curve_p, c); /* f0 <-- p - c */
  1632. for (i = 0; i <= 6; i++) {
  1633. mod_sqrt_secp224r1_rss(d1, e1, f1, d0, e0, f0, pow2i); /* RSS (d1,e1,f1,d0,e0,f0,2^i) */
  1634. mod_sqrt_secp224r1_rm(d1, e1, f1, c, d1, e1, d0, e0); /* RM (d1,e1,f1,c,d1,e1,d0,e0) */
  1635. vli_set(d0, d1); /* d0 <-- d1 */
  1636. vli_set(e0, e1); /* e0 <-- e1 */
  1637. vli_set(f0, f1); /* f0 <-- f1 */
  1638. pow2i *= 2;
  1639. }
  1640. }
  1641. /* Compute a = sqrt(a) (mod curve_p). */
  1642. /* Routine 3.2.8 mp_mod_sqrt_224; from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  1643. static void mod_sqrt(uECC_word_t *a) {
  1644. bitcount_t i;
  1645. uECC_word_t e1[uECC_WORDS];
  1646. uECC_word_t f1[uECC_WORDS];
  1647. uECC_word_t d0[uECC_WORDS];
  1648. uECC_word_t e0[uECC_WORDS];
  1649. uECC_word_t f0[uECC_WORDS];
  1650. uECC_word_t d1[uECC_WORDS];
  1651. // s = a; using constant instead of random value
  1652. mod_sqrt_secp224r1_rp(d0, e0, f0, a, a); /* RP (d0, e0, f0, c, s) */
  1653. mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */
  1654. for (i = 1; i <= 95; i++) {
  1655. vli_set(d0, d1); /* d0 <-- d1 */
  1656. vli_set(e0, e1); /* e0 <-- e1 */
  1657. vli_set(f0, f1); /* f0 <-- f1 */
  1658. mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */
  1659. if (vli_isZero(d1)) { /* if d1 == 0 */
  1660. break;
  1661. }
  1662. }
  1663. vli_modInv(f1, e0, curve_p); /* f1 <-- 1 / e0 */
  1664. vli_modMult_fast(a, d0, f1); /* a <-- d0 / e0 */
  1665. }
  1666. #else /* uECC_CURVE */
  1667. /* Compute a = sqrt(a) (mod curve_p). */
  1668. static void mod_sqrt(uECC_word_t *a) {
  1669. bitcount_t i;
  1670. uECC_word_t p1[uECC_WORDS] = {1};
  1671. uECC_word_t l_result[uECC_WORDS] = {1};
  1672. /* Since curve_p == 3 (mod 4) for all supported curves, we can
  1673. compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */
  1674. vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */
  1675. for (i = vli_numBits(p1, uECC_WORDS) - 1; i > 1; --i) {
  1676. vli_modSquare_fast(l_result, l_result);
  1677. if (vli_testBit(p1, i)) {
  1678. vli_modMult_fast(l_result, l_result, a);
  1679. }
  1680. }
  1681. vli_set(a, l_result);
  1682. }
  1683. #endif /* uECC_CURVE */
  1684. #if uECC_WORD_SIZE == 1
  1685. static void vli_nativeToBytes(uint8_t * RESTRICT dest, const uint8_t * RESTRICT src) {
  1686. uint8_t i;
  1687. for (i = 0; i < uECC_BYTES; ++i) {
  1688. dest[i] = src[(uECC_BYTES - 1) - i];
  1689. }
  1690. }
  1691. #define vli_bytesToNative(dest, src) vli_nativeToBytes((dest), (src))
  1692. #elif uECC_WORD_SIZE == 4
  1693. static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native) {
  1694. unsigned i;
  1695. for (i = 0; i < uECC_WORDS; ++i) {
  1696. uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i);
  1697. digit[0] = native[i] >> 24;
  1698. digit[1] = native[i] >> 16;
  1699. digit[2] = native[i] >> 8;
  1700. digit[3] = native[i];
  1701. }
  1702. }
  1703. static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes) {
  1704. unsigned i;
  1705. for (i = 0; i < uECC_WORDS; ++i) {
  1706. const uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i);
  1707. native[i] = ((uint32_t)digit[0] << 24) | ((uint32_t)digit[1] << 16) |
  1708. ((uint32_t)digit[2] << 8) | (uint32_t)digit[3];
  1709. }
  1710. }
  1711. #else
  1712. static void vli_nativeToBytes(uint8_t *bytes, const uint64_t *native) {
  1713. unsigned i;
  1714. for (i = 0; i < uECC_WORDS; ++i) {
  1715. uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i);
  1716. digit[0] = native[i] >> 56;
  1717. digit[1] = native[i] >> 48;
  1718. digit[2] = native[i] >> 40;
  1719. digit[3] = native[i] >> 32;
  1720. digit[4] = native[i] >> 24;
  1721. digit[5] = native[i] >> 16;
  1722. digit[6] = native[i] >> 8;
  1723. digit[7] = native[i];
  1724. }
  1725. }
  1726. static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes) {
  1727. unsigned i;
  1728. for (i = 0; i < uECC_WORDS; ++i) {
  1729. const uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i);
  1730. native[i] = ((uint64_t)digit[0] << 56) | ((uint64_t)digit[1] << 48) |
  1731. ((uint64_t)digit[2] << 40) | ((uint64_t)digit[3] << 32) |
  1732. ((uint64_t)digit[4] << 24) | ((uint64_t)digit[5] << 16) |
  1733. ((uint64_t)digit[6] << 8) | (uint64_t)digit[7];
  1734. }
  1735. }
  1736. #endif /* uECC_WORD_SIZE */
  1737. int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]) {
  1738. uECC_word_t private[uECC_WORDS];
  1739. EccPoint public;
  1740. uECC_word_t tries;
  1741. for (tries = 0; tries < MAX_TRIES; ++tries) {
  1742. if (g_rng_function((uint8_t *)private, sizeof(private)) &&
  1743. EccPoint_compute_public_key(&public, private)) {
  1744. vli_nativeToBytes(private_key, private);
  1745. vli_nativeToBytes(public_key, public.x);
  1746. vli_nativeToBytes(public_key + uECC_BYTES, public.y);
  1747. return 1;
  1748. }
  1749. }
  1750. return 0;
  1751. }
  1752. int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2],
  1753. const uint8_t private_key[uECC_BYTES],
  1754. uint8_t secret[uECC_BYTES]) {
  1755. EccPoint public;
  1756. EccPoint product;
  1757. uECC_word_t private[uECC_WORDS];
  1758. uECC_word_t tmp[uECC_WORDS];
  1759. uECC_word_t *p2[2] = {private, tmp};
  1760. uECC_word_t random[uECC_WORDS];
  1761. uECC_word_t *initial_Z = NULL;
  1762. uECC_word_t tries;
  1763. uECC_word_t carry;
  1764. // Try to get a random initial Z value to improve protection against side-channel
  1765. // attacks. If the RNG fails every time (eg it was not defined), we continue so that
  1766. // uECC_shared_secret() can still work without an RNG defined.
  1767. for (tries = 0; tries < MAX_TRIES; ++tries) {
  1768. if (g_rng_function((uint8_t *)random, sizeof(random)) && !vli_isZero(random)) {
  1769. initial_Z = random;
  1770. break;
  1771. }
  1772. }
  1773. vli_bytesToNative(private, private_key);
  1774. vli_bytesToNative(public.x, public_key);
  1775. vli_bytesToNative(public.y, public_key + uECC_BYTES);
  1776. #if (uECC_CURVE == uECC_secp160r1)
  1777. // Don't regularize the bitcount for secp160r1.
  1778. EccPoint_mult(&product, &public, private, initial_Z, vli_numBits(private, uECC_WORDS));
  1779. #else
  1780. // Regularize the bitcount for the private key so that attackers cannot use a side channel
  1781. // attack to learn the number of leading zeros.
  1782. carry = vli_add(private, private, curve_n);
  1783. vli_add(tmp, private, curve_n);
  1784. EccPoint_mult(&product, &public, p2[!carry], initial_Z, (uECC_BYTES * 8) + 1);
  1785. #endif
  1786. vli_nativeToBytes(secret, product.x);
  1787. return !EccPoint_isZero(&product);
  1788. }
  1789. void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]) {
  1790. wordcount_t i;
  1791. for (i = 0; i < uECC_BYTES; ++i) {
  1792. compressed[i+1] = public_key[i];
  1793. }
  1794. compressed[0] = 2 + (public_key[uECC_BYTES * 2 - 1] & 0x01);
  1795. }
  1796. /* Computes result = x^3 + ax + b. result must not overlap x. */
  1797. static void curve_x_side(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT x) {
  1798. static const uECC_word_t curve_b[uECC_WORDS] = uECC_CONCAT(Curve_B_, uECC_CURVE);
  1799. #if (uECC_CURVE == uECC_secp256k1)
  1800. vli_modSquare_fast(result, x); /* r = x^2 */
  1801. vli_modMult_fast(result, result, x); /* r = x^3 */
  1802. vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 + b */
  1803. #else
  1804. uECC_word_t _3[uECC_WORDS] = {3}; /* -a = 3 */
  1805. vli_modSquare_fast(result, x); /* r = x^2 */
  1806. vli_modSub_fast(result, result, _3); /* r = x^2 - 3 */
  1807. vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
  1808. vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 - 3x + b */
  1809. #endif
  1810. }
  1811. void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]) {
  1812. EccPoint point;
  1813. vli_bytesToNative(point.x, compressed + 1);
  1814. curve_x_side(point.y, point.x);
  1815. mod_sqrt(point.y);
  1816. if ((point.y[0] & 0x01) != (compressed[0] & 0x01)) {
  1817. vli_sub(point.y, curve_p, point.y);
  1818. }
  1819. vli_nativeToBytes(public_key, point.x);
  1820. vli_nativeToBytes(public_key + uECC_BYTES, point.y);
  1821. }
  1822. int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]) {
  1823. uECC_word_t tmp1[uECC_WORDS];
  1824. uECC_word_t tmp2[uECC_WORDS];
  1825. EccPoint public;
  1826. vli_bytesToNative(public.x, public_key);
  1827. vli_bytesToNative(public.y, public_key + uECC_BYTES);
  1828. // The point at infinity is invalid.
  1829. if (EccPoint_isZero(&public)) {
  1830. return 0;
  1831. }
  1832. // x and y must be smaller than p.
  1833. if (vli_cmp(curve_p, public.x) != 1 || vli_cmp(curve_p, public.y) != 1) {
  1834. return 0;
  1835. }
  1836. vli_modSquare_fast(tmp1, public.y); /* tmp1 = y^2 */
  1837. curve_x_side(tmp2, public.x); /* tmp2 = x^3 + ax + b */
  1838. /* Make sure that y^2 == x^3 + ax + b */
  1839. return (vli_cmp(tmp1, tmp2) == 0);
  1840. }
  1841. int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES],
  1842. uint8_t public_key[uECC_BYTES * 2]) {
  1843. uECC_word_t private[uECC_WORDS];
  1844. EccPoint public;
  1845. vli_bytesToNative(private, private_key);
  1846. if (!EccPoint_compute_public_key(&public, private)) {
  1847. return 0;
  1848. }
  1849. vli_nativeToBytes(public_key, public.x);
  1850. vli_nativeToBytes(public_key + uECC_BYTES, public.y);
  1851. return 1;
  1852. }
  1853. int uECC_bytes(void) {
  1854. return uECC_BYTES;
  1855. }
  1856. int uECC_curve(void) {
  1857. return uECC_CURVE;
  1858. }
  1859. /* -------- ECDSA code -------- */
  1860. #if (uECC_CURVE == uECC_secp160r1)
  1861. static void vli_clear_n(uECC_word_t *vli) {
  1862. vli_clear(vli);
  1863. vli[uECC_N_WORDS - 1] = 0;
  1864. }
  1865. static uECC_word_t vli_isZero_n(const uECC_word_t *vli) {
  1866. if (vli[uECC_N_WORDS - 1]) {
  1867. return 0;
  1868. }
  1869. return vli_isZero(vli);
  1870. }
  1871. static void vli_set_n(uECC_word_t *dest, const uECC_word_t *src) {
  1872. vli_set(dest, src);
  1873. dest[uECC_N_WORDS - 1] = src[uECC_N_WORDS - 1];
  1874. }
  1875. static cmpresult_t vli_cmp_n(const uECC_word_t *left, const uECC_word_t *right) {
  1876. if (left[uECC_N_WORDS - 1] > right[uECC_N_WORDS - 1]) {
  1877. return 1;
  1878. } else if (left[uECC_N_WORDS - 1] < right[uECC_N_WORDS - 1]) {
  1879. return -1;
  1880. }
  1881. return vli_cmp(left, right);
  1882. }
  1883. static void vli_rshift1_n(uECC_word_t *vli) {
  1884. vli_rshift1(vli);
  1885. vli[uECC_N_WORDS - 2] |= vli[uECC_N_WORDS - 1] << (uECC_WORD_BITS - 1);
  1886. vli[uECC_N_WORDS - 1] = vli[uECC_N_WORDS - 1] >> 1;
  1887. }
  1888. static uECC_word_t vli_add_n(uECC_word_t *result,
  1889. const uECC_word_t *left,
  1890. const uECC_word_t *right) {
  1891. uECC_word_t carry = vli_add(result, left, right);
  1892. uECC_word_t sum = left[uECC_N_WORDS - 1] + right[uECC_N_WORDS - 1] + carry;
  1893. if (sum != left[uECC_N_WORDS - 1]) {
  1894. carry = (sum < left[uECC_N_WORDS - 1]);
  1895. }
  1896. result[uECC_N_WORDS - 1] = sum;
  1897. return carry;
  1898. }
  1899. static uECC_word_t vli_sub_n(uECC_word_t *result,
  1900. const uECC_word_t *left,
  1901. const uECC_word_t *right) {
  1902. uECC_word_t borrow = vli_sub(result, left, right);
  1903. uECC_word_t diff = left[uECC_N_WORDS - 1] - right[uECC_N_WORDS - 1] - borrow;
  1904. if (diff != left[uECC_N_WORDS - 1]) {
  1905. borrow = (diff > left[uECC_N_WORDS - 1]);
  1906. }
  1907. result[uECC_N_WORDS - 1] = diff;
  1908. return borrow;
  1909. }
  1910. #if !muladd_exists
  1911. static void muladd(uECC_word_t a,
  1912. uECC_word_t b,
  1913. uECC_word_t *r0,
  1914. uECC_word_t *r1,
  1915. uECC_word_t *r2) {
  1916. uECC_dword_t p = (uECC_dword_t)a * b;
  1917. uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
  1918. r01 += p;
  1919. *r2 += (r01 < p);
  1920. *r1 = r01 >> uECC_WORD_BITS;
  1921. *r0 = (uECC_word_t)r01;
  1922. }
  1923. #define muladd_exists 1
  1924. #endif
  1925. static void vli_mult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
  1926. uECC_word_t r0 = 0;
  1927. uECC_word_t r1 = 0;
  1928. uECC_word_t r2 = 0;
  1929. wordcount_t i, k;
  1930. for (k = 0; k < uECC_N_WORDS * 2 - 1; ++k) {
  1931. wordcount_t min = (k < uECC_N_WORDS ? 0 : (k + 1) - uECC_N_WORDS);
  1932. wordcount_t max = (k < uECC_N_WORDS ? k : uECC_N_WORDS - 1);
  1933. for (i = min; i <= max; ++i) {
  1934. muladd(left[i], right[k - i], &r0, &r1, &r2);
  1935. }
  1936. result[k] = r0;
  1937. r0 = r1;
  1938. r1 = r2;
  1939. r2 = 0;
  1940. }
  1941. result[uECC_N_WORDS * 2 - 1] = r0;
  1942. }
  1943. static void vli_modAdd_n(uECC_word_t *result,
  1944. const uECC_word_t *left,
  1945. const uECC_word_t *right,
  1946. const uECC_word_t *mod) {
  1947. uECC_word_t carry = vli_add_n(result, left, right);
  1948. if (carry || vli_cmp_n(result, mod) >= 0) {
  1949. vli_sub_n(result, result, mod);
  1950. }
  1951. }
  1952. static void vli_modInv_n(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) {
  1953. uECC_word_t a[uECC_N_WORDS], b[uECC_N_WORDS], u[uECC_N_WORDS], v[uECC_N_WORDS];
  1954. uECC_word_t carry;
  1955. cmpresult_t cmpResult;
  1956. if (vli_isZero_n(input)) {
  1957. vli_clear_n(result);
  1958. return;
  1959. }
  1960. vli_set_n(a, input);
  1961. vli_set_n(b, mod);
  1962. vli_clear_n(u);
  1963. u[0] = 1;
  1964. vli_clear_n(v);
  1965. while ((cmpResult = vli_cmp_n(a, b)) != 0) {
  1966. carry = 0;
  1967. if (EVEN(a)) {
  1968. vli_rshift1_n(a);
  1969. if (!EVEN(u)) {
  1970. carry = vli_add_n(u, u, mod);
  1971. }
  1972. vli_rshift1_n(u);
  1973. if (carry) {
  1974. u[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
  1975. }
  1976. } else if (EVEN(b)) {
  1977. vli_rshift1_n(b);
  1978. if (!EVEN(v)) {
  1979. carry = vli_add_n(v, v, mod);
  1980. }
  1981. vli_rshift1_n(v);
  1982. if (carry) {
  1983. v[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
  1984. }
  1985. } else if (cmpResult > 0) {
  1986. vli_sub_n(a, a, b);
  1987. vli_rshift1_n(a);
  1988. if (vli_cmp_n(u, v) < 0) {
  1989. vli_add_n(u, u, mod);
  1990. }
  1991. vli_sub_n(u, u, v);
  1992. if (!EVEN(u)) {
  1993. carry = vli_add_n(u, u, mod);
  1994. }
  1995. vli_rshift1_n(u);
  1996. if (carry) {
  1997. u[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
  1998. }
  1999. } else {
  2000. vli_sub_n(b, b, a);
  2001. vli_rshift1_n(b);
  2002. if (vli_cmp_n(v, u) < 0) {
  2003. vli_add_n(v, v, mod);
  2004. }
  2005. vli_sub_n(v, v, u);
  2006. if (!EVEN(v)) {
  2007. carry = vli_add_n(v, v, mod);
  2008. }
  2009. vli_rshift1_n(v);
  2010. if (carry) {
  2011. v[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
  2012. }
  2013. }
  2014. }
  2015. vli_set_n(result, u);
  2016. }
  2017. static void vli2_rshift1_n(uECC_word_t *vli) {
  2018. vli_rshift1_n(vli);
  2019. vli[uECC_N_WORDS - 1] |= vli[uECC_N_WORDS] << (uECC_WORD_BITS - 1);
  2020. vli_rshift1_n(vli + uECC_N_WORDS);
  2021. }
  2022. static uECC_word_t vli2_sub_n(uECC_word_t *result,
  2023. const uECC_word_t *left,
  2024. const uECC_word_t *right) {
  2025. uECC_word_t borrow = 0;
  2026. wordcount_t i;
  2027. for (i = 0; i < uECC_N_WORDS * 2; ++i) {
  2028. uECC_word_t diff = left[i] - right[i] - borrow;
  2029. if (diff != left[i]) {
  2030. borrow = (diff > left[i]);
  2031. }
  2032. result[i] = diff;
  2033. }
  2034. return borrow;
  2035. }
  2036. /* Computes result = (left * right) % curve_n. */
  2037. static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
  2038. bitcount_t i;
  2039. uECC_word_t product[2 * uECC_N_WORDS];
  2040. uECC_word_t modMultiple[2 * uECC_N_WORDS];
  2041. uECC_word_t tmp[2 * uECC_N_WORDS];
  2042. uECC_word_t *v[2] = {tmp, product};
  2043. uECC_word_t index = 1;
  2044. vli_mult_n(product, left, right);
  2045. vli_clear_n(modMultiple);
  2046. vli_set(modMultiple + uECC_N_WORDS + 1, curve_n);
  2047. vli_rshift1(modMultiple + uECC_N_WORDS + 1);
  2048. modMultiple[2 * uECC_N_WORDS - 1] |= HIGH_BIT_SET;
  2049. modMultiple[uECC_N_WORDS] = HIGH_BIT_SET;
  2050. for (i = 0;
  2051. i <= ((((bitcount_t)uECC_N_WORDS) << uECC_WORD_BITS_SHIFT) + (uECC_WORD_BITS - 1));
  2052. ++i) {
  2053. uECC_word_t borrow = vli2_sub_n(v[1 - index], v[index], modMultiple);
  2054. index = !(index ^ borrow); /* Swap the index if there was no borrow */
  2055. vli2_rshift1_n(modMultiple);
  2056. }
  2057. vli_set_n(result, v[index]);
  2058. }
  2059. #else
  2060. #define vli_cmp_n vli_cmp
  2061. #define vli_modInv_n vli_modInv
  2062. #define vli_modAdd_n vli_modAdd
  2063. static void vli2_rshift1(uECC_word_t *vli) {
  2064. vli_rshift1(vli);
  2065. vli[uECC_WORDS - 1] |= vli[uECC_WORDS] << (uECC_WORD_BITS - 1);
  2066. vli_rshift1(vli + uECC_WORDS);
  2067. }
  2068. static uECC_word_t vli2_sub(uECC_word_t *result,
  2069. const uECC_word_t *left,
  2070. const uECC_word_t *right) {
  2071. uECC_word_t borrow = 0;
  2072. wordcount_t i;
  2073. for (i = 0; i < uECC_WORDS * 2; ++i) {
  2074. uECC_word_t diff = left[i] - right[i] - borrow;
  2075. if (diff != left[i]) {
  2076. borrow = (diff > left[i]);
  2077. }
  2078. result[i] = diff;
  2079. }
  2080. return borrow;
  2081. }
  2082. /* Computes result = (left * right) % curve_n. */
  2083. static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
  2084. uECC_word_t product[2 * uECC_WORDS];
  2085. uECC_word_t modMultiple[2 * uECC_WORDS];
  2086. uECC_word_t tmp[2 * uECC_WORDS];
  2087. uECC_word_t *v[2] = {tmp, product};
  2088. bitcount_t i;
  2089. uECC_word_t index = 1;
  2090. vli_mult(product, left, right);
  2091. vli_set(modMultiple + uECC_WORDS, curve_n); /* works if curve_n has its highest bit set */
  2092. vli_clear(modMultiple);
  2093. for (i = 0; i <= uECC_BYTES * 8; ++i) {
  2094. uECC_word_t borrow = vli2_sub(v[1 - index], v[index], modMultiple);
  2095. index = !(index ^ borrow); /* Swap the index if there was no borrow */
  2096. vli2_rshift1(modMultiple);
  2097. }
  2098. vli_set(result, v[index]);
  2099. }
  2100. #endif /* (uECC_CURVE != uECC_secp160r1) */
  2101. static int uECC_sign_with_k(const uint8_t private_key[uECC_BYTES],
  2102. const uint8_t message_hash[uECC_BYTES],
  2103. uECC_word_t k[uECC_N_WORDS],
  2104. uint8_t signature[uECC_BYTES*2]) {
  2105. uECC_word_t tmp[uECC_N_WORDS];
  2106. uECC_word_t s[uECC_N_WORDS];
  2107. uECC_word_t *k2[2] = {tmp, s};
  2108. EccPoint p;
  2109. uECC_word_t carry;
  2110. uECC_word_t tries;
  2111. /* Make sure 0 < k < curve_n */
  2112. if (vli_isZero(k) || vli_cmp_n(curve_n, k) != 1) {
  2113. return 0;
  2114. }
  2115. #if (uECC_CURVE == uECC_secp160r1)
  2116. /* Make sure that we don't leak timing information about k.
  2117. See http://eprint.iacr.org/2011/232.pdf */
  2118. vli_add_n(tmp, k, curve_n);
  2119. carry = (tmp[uECC_WORDS] & 0x02);
  2120. vli_add_n(s, tmp, curve_n);
  2121. /* p = k * G */
  2122. EccPoint_mult(&p, &curve_G, k2[!carry], NULL, (uECC_BYTES * 8) + 2);
  2123. #else
  2124. /* Make sure that we don't leak timing information about k.
  2125. See http://eprint.iacr.org/2011/232.pdf */
  2126. carry = vli_add(tmp, k, curve_n);
  2127. vli_add(s, tmp, curve_n);
  2128. /* p = k * G */
  2129. EccPoint_mult(&p, &curve_G, k2[!carry], NULL, (uECC_BYTES * 8) + 1);
  2130. /* r = x1 (mod n) */
  2131. if (vli_cmp(curve_n, p.x) != 1) {
  2132. vli_sub(p.x, p.x, curve_n);
  2133. }
  2134. #endif
  2135. if (vli_isZero(p.x)) {
  2136. return 0;
  2137. }
  2138. // Attempt to get a random number to prevent side channel analysis of k.
  2139. // If the RNG fails every time (eg it was not defined), we continue so that
  2140. // deterministic signing can still work (with reduced security) without
  2141. // an RNG defined.
  2142. carry = 0; // use to signal that the RNG succeeded at least once.
  2143. for (tries = 0; tries < MAX_TRIES; ++tries) {
  2144. if (!g_rng_function((uint8_t *)tmp, sizeof(tmp))) {
  2145. continue;
  2146. }
  2147. carry = 1;
  2148. if (!vli_isZero(tmp)) {
  2149. break;
  2150. }
  2151. }
  2152. if (!carry) {
  2153. vli_clear(tmp);
  2154. tmp[0] = 1;
  2155. }
  2156. /* Prevent side channel analysis of vli_modInv() to determine
  2157. bits of k / the private key by premultiplying by a random number */
  2158. vli_modMult_n(k, k, tmp); /* k' = rand * k */
  2159. vli_modInv_n(k, k, curve_n); /* k = 1 / k' */
  2160. vli_modMult_n(k, k, tmp); /* k = 1 / k */
  2161. vli_nativeToBytes(signature, p.x); /* store r */
  2162. tmp[uECC_N_WORDS - 1] = 0;
  2163. vli_bytesToNative(tmp, private_key); /* tmp = d */
  2164. s[uECC_N_WORDS - 1] = 0;
  2165. vli_set(s, p.x);
  2166. vli_modMult_n(s, tmp, s); /* s = r*d */
  2167. vli_bytesToNative(tmp, message_hash);
  2168. vli_modAdd_n(s, tmp, s, curve_n); /* s = e + r*d */
  2169. vli_modMult_n(s, s, k); /* s = (e + r*d) / k */
  2170. #if (uECC_CURVE == uECC_secp160r1)
  2171. if (s[uECC_N_WORDS - 1]) {
  2172. return 0;
  2173. }
  2174. #endif
  2175. vli_nativeToBytes(signature + uECC_BYTES, s);
  2176. return 1;
  2177. }
  2178. int uECC_sign(const uint8_t private_key[uECC_BYTES],
  2179. const uint8_t message_hash[uECC_BYTES],
  2180. uint8_t signature[uECC_BYTES*2]) {
  2181. uECC_word_t k[uECC_N_WORDS];
  2182. uECC_word_t tries;
  2183. for (tries = 0; tries < MAX_TRIES; ++tries) {
  2184. if(g_rng_function((uint8_t *)k, sizeof(k))) {
  2185. #if (uECC_CURVE == uECC_secp160r1)
  2186. k[uECC_WORDS] &= 0x01;
  2187. #endif
  2188. if (uECC_sign_with_k(private_key, message_hash, k, signature)) {
  2189. return 1;
  2190. }
  2191. }
  2192. }
  2193. return 0;
  2194. }
  2195. /* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always
  2196. the same size as the hash result size. */
  2197. static void HMAC_init(uECC_HashContext *hash_context, const uint8_t *K) {
  2198. uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
  2199. unsigned i;
  2200. for (i = 0; i < hash_context->result_size; ++i)
  2201. pad[i] = K[i] ^ 0x36;
  2202. for (; i < hash_context->block_size; ++i)
  2203. pad[i] = 0x36;
  2204. hash_context->init_hash(hash_context);
  2205. hash_context->update_hash(hash_context, pad, hash_context->block_size);
  2206. }
  2207. static void HMAC_update(uECC_HashContext *hash_context,
  2208. const uint8_t *message,
  2209. unsigned message_size) {
  2210. hash_context->update_hash(hash_context, message, message_size);
  2211. }
  2212. static void HMAC_finish(uECC_HashContext *hash_context, const uint8_t *K, uint8_t *result) {
  2213. uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
  2214. unsigned i;
  2215. for (i = 0; i < hash_context->result_size; ++i)
  2216. pad[i] = K[i] ^ 0x5c;
  2217. for (; i < hash_context->block_size; ++i)
  2218. pad[i] = 0x5c;
  2219. hash_context->finish_hash(hash_context, result);
  2220. hash_context->init_hash(hash_context);
  2221. hash_context->update_hash(hash_context, pad, hash_context->block_size);
  2222. hash_context->update_hash(hash_context, result, hash_context->result_size);
  2223. hash_context->finish_hash(hash_context, result);
  2224. }
  2225. /* V = HMAC_K(V) */
  2226. static void update_V(uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) {
  2227. HMAC_init(hash_context, K);
  2228. HMAC_update(hash_context, V, hash_context->result_size);
  2229. HMAC_finish(hash_context, K, V);
  2230. }
  2231. /* Deterministic signing, similar to RFC 6979. Differences are:
  2232. * We just use (truncated) H(m) directly rather than bits2octets(H(m))
  2233. (it is not reduced modulo curve_n).
  2234. * We generate a value for k (aka T) directly rather than converting endianness.
  2235. Layout of hash_context->tmp: <K> | <V> | (1 byte overlapped 0x00 or 0x01) / <HMAC pad> */
  2236. int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES],
  2237. const uint8_t message_hash[uECC_BYTES],
  2238. uECC_HashContext *hash_context,
  2239. uint8_t signature[uECC_BYTES*2]) {
  2240. uint8_t *K = hash_context->tmp;
  2241. uint8_t *V = K + hash_context->result_size;
  2242. uECC_word_t tries;
  2243. unsigned i;
  2244. for (i = 0; i < hash_context->result_size; ++i) {
  2245. V[i] = 0x01;
  2246. K[i] = 0;
  2247. }
  2248. // K = HMAC_K(V || 0x00 || int2octets(x) || h(m))
  2249. HMAC_init(hash_context, K);
  2250. V[hash_context->result_size] = 0x00;
  2251. HMAC_update(hash_context, V, hash_context->result_size + 1);
  2252. HMAC_update(hash_context, private_key, uECC_BYTES);
  2253. HMAC_update(hash_context, message_hash, uECC_BYTES);
  2254. HMAC_finish(hash_context, K, K);
  2255. update_V(hash_context, K, V);
  2256. // K = HMAC_K(V || 0x01 || int2octets(x) || h(m))
  2257. HMAC_init(hash_context, K);
  2258. V[hash_context->result_size] = 0x01;
  2259. HMAC_update(hash_context, V, hash_context->result_size + 1);
  2260. HMAC_update(hash_context, private_key, uECC_BYTES);
  2261. HMAC_update(hash_context, message_hash, uECC_BYTES);
  2262. HMAC_finish(hash_context, K, K);
  2263. update_V(hash_context, K, V);
  2264. for (tries = 0; tries < MAX_TRIES; ++tries) {
  2265. uECC_word_t T[uECC_N_WORDS];
  2266. uint8_t *T_ptr = (uint8_t *)T;
  2267. unsigned T_bytes = 0;
  2268. while (T_bytes < sizeof(T)) {
  2269. update_V(hash_context, K, V);
  2270. for (i = 0; i < hash_context->result_size && T_bytes < sizeof(T); ++i, ++T_bytes) {
  2271. T_ptr[T_bytes] = V[i];
  2272. }
  2273. }
  2274. #if (uECC_CURVE == uECC_secp160r1)
  2275. T[uECC_WORDS] &= 0x01;
  2276. #endif
  2277. if (uECC_sign_with_k(private_key, message_hash, T, signature)) {
  2278. return 1;
  2279. }
  2280. // K = HMAC_K(V || 0x00)
  2281. HMAC_init(hash_context, K);
  2282. V[hash_context->result_size] = 0x00;
  2283. HMAC_update(hash_context, V, hash_context->result_size + 1);
  2284. HMAC_finish(hash_context, K, K);
  2285. update_V(hash_context, K, V);
  2286. }
  2287. return 0;
  2288. }
  2289. static bitcount_t smax(bitcount_t a, bitcount_t b) {
  2290. return (a > b ? a : b);
  2291. }
  2292. int uECC_verify(const uint8_t public_key[uECC_BYTES*2],
  2293. const uint8_t hash[uECC_BYTES],
  2294. const uint8_t signature[uECC_BYTES*2]) {
  2295. uECC_word_t u1[uECC_N_WORDS], u2[uECC_N_WORDS];
  2296. uECC_word_t z[uECC_N_WORDS];
  2297. EccPoint public, sum;
  2298. uECC_word_t rx[uECC_WORDS];
  2299. uECC_word_t ry[uECC_WORDS];
  2300. uECC_word_t tx[uECC_WORDS];
  2301. uECC_word_t ty[uECC_WORDS];
  2302. uECC_word_t tz[uECC_WORDS];
  2303. const EccPoint *points[4];
  2304. const EccPoint *point;
  2305. bitcount_t numBits;
  2306. bitcount_t i;
  2307. uECC_word_t r[uECC_N_WORDS], s[uECC_N_WORDS];
  2308. r[uECC_N_WORDS - 1] = 0;
  2309. s[uECC_N_WORDS - 1] = 0;
  2310. vli_bytesToNative(public.x, public_key);
  2311. vli_bytesToNative(public.y, public_key + uECC_BYTES);
  2312. vli_bytesToNative(r, signature);
  2313. vli_bytesToNative(s, signature + uECC_BYTES);
  2314. if (vli_isZero(r) || vli_isZero(s)) { /* r, s must not be 0. */
  2315. return 0;
  2316. }
  2317. #if (uECC_CURVE != uECC_secp160r1)
  2318. if (vli_cmp(curve_n, r) != 1 || vli_cmp(curve_n, s) != 1) { /* r, s must be < n. */
  2319. return 0;
  2320. }
  2321. #endif
  2322. /* Calculate u1 and u2. */
  2323. vli_modInv_n(z, s, curve_n); /* Z = s^-1 */
  2324. u1[uECC_N_WORDS - 1] = 0;
  2325. vli_bytesToNative(u1, hash);
  2326. vli_modMult_n(u1, u1, z); /* u1 = e/s */
  2327. vli_modMult_n(u2, r, z); /* u2 = r/s */
  2328. /* Calculate sum = G + Q. */
  2329. vli_set(sum.x, public.x);
  2330. vli_set(sum.y, public.y);
  2331. vli_set(tx, curve_G.x);
  2332. vli_set(ty, curve_G.y);
  2333. vli_modSub_fast(z, sum.x, tx); /* Z = x2 - x1 */
  2334. XYcZ_add(tx, ty, sum.x, sum.y);
  2335. vli_modInv(z, z, curve_p); /* Z = 1/Z */
  2336. apply_z(sum.x, sum.y, z);
  2337. /* Use Shamir's trick to calculate u1*G + u2*Q */
  2338. points[0] = 0;
  2339. points[1] = &curve_G;
  2340. points[2] = &public;
  2341. points[3] = &sum;
  2342. numBits = smax(vli_numBits(u1, uECC_N_WORDS), vli_numBits(u2, uECC_N_WORDS));
  2343. point = points[(!!vli_testBit(u1, numBits - 1)) | ((!!vli_testBit(u2, numBits - 1)) << 1)];
  2344. vli_set(rx, point->x);
  2345. vli_set(ry, point->y);
  2346. vli_clear(z);
  2347. z[0] = 1;
  2348. for (i = numBits - 2; i >= 0; --i) {
  2349. uECC_word_t index;
  2350. EccPoint_double_jacobian(rx, ry, z);
  2351. index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1);
  2352. point = points[index];
  2353. if (point) {
  2354. vli_set(tx, point->x);
  2355. vli_set(ty, point->y);
  2356. apply_z(tx, ty, z);
  2357. vli_modSub_fast(tz, rx, tx); /* Z = x2 - x1 */
  2358. XYcZ_add(tx, ty, rx, ry);
  2359. vli_modMult_fast(z, z, tz);
  2360. }
  2361. }
  2362. vli_modInv(z, z, curve_p); /* Z = 1/Z */
  2363. apply_z(rx, ry, z);
  2364. /* v = x1 (mod n) */
  2365. #if (uECC_CURVE != uECC_secp160r1)
  2366. if (vli_cmp(curve_n, rx) != 1) {
  2367. vli_sub(rx, rx, curve_n);
  2368. }
  2369. #endif
  2370. /* Accept only if v == r. */
  2371. return vli_equal(rx, r);
  2372. }