0005-Add-Argon2-algorithm.patch 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612
  1. From 9bc9e32ace3f103ff12aab063c8a250c8ba6a642 Mon Sep 17 00:00:00 2001
  2. From: Ax333l <main@axelen.xyz>
  3. Date: Thu, 17 Aug 2023 00:00:00 +0000
  4. Subject: [PATCH 05/13] Add Argon2 algorithm
  5. Signed-off-by: Nicholas Johnson <nick@nicholasjohnson.ch>
  6. ---
  7. docs/grub-dev.texi | 64 +++
  8. grub-core/Makefile.core.def | 8 +
  9. grub-core/lib/argon2/LICENSE | 314 +++++++++++
  10. grub-core/lib/argon2/argon2.c | 232 ++++++++
  11. grub-core/lib/argon2/argon2.h | 264 +++++++++
  12. grub-core/lib/argon2/blake2/blake2-impl.h | 151 ++++++
  13. grub-core/lib/argon2/blake2/blake2.h | 89 +++
  14. grub-core/lib/argon2/blake2/blake2b.c | 388 ++++++++++++++
  15. .../lib/argon2/blake2/blamka-round-ref.h | 56 ++
  16. grub-core/lib/argon2/core.c | 506 ++++++++++++++++++
  17. grub-core/lib/argon2/core.h | 228 ++++++++
  18. grub-core/lib/argon2/ref.c | 190 +++++++
  19. 12 files changed, 2490 insertions(+)
  20. create mode 100644 grub-core/lib/argon2/LICENSE
  21. create mode 100644 grub-core/lib/argon2/argon2.c
  22. create mode 100644 grub-core/lib/argon2/argon2.h
  23. create mode 100644 grub-core/lib/argon2/blake2/blake2-impl.h
  24. create mode 100644 grub-core/lib/argon2/blake2/blake2.h
  25. create mode 100644 grub-core/lib/argon2/blake2/blake2b.c
  26. create mode 100644 grub-core/lib/argon2/blake2/blamka-round-ref.h
  27. create mode 100644 grub-core/lib/argon2/core.c
  28. create mode 100644 grub-core/lib/argon2/core.h
  29. create mode 100644 grub-core/lib/argon2/ref.c
  30. diff --git a/docs/grub-dev.texi b/docs/grub-dev.texi
  31. index 1276c5930..cd6fb0e1e 100644
  32. --- a/docs/grub-dev.texi
  33. +++ b/docs/grub-dev.texi
  34. @@ -503,11 +503,75 @@ GRUB includes some code from other projects, and it is sometimes necessary
  35. to update it.
  36. @menu
  37. +* Argon2::
  38. * Gnulib::
  39. * jsmn::
  40. * minilzo::
  41. @end menu
  42. +@node Argon2
  43. +@section Argon2
  44. +
  45. +Argon2 is a key derivation function used by LUKS2 in order to derive encryption
  46. +keys from a user-provided password. GRUB imports the official reference
  47. +implementation of Argon2 from @url{https://github.com/P-H-C/phc-winner-argon2}.
  48. +In order to make the library usable for GRUB, we need to perform various
  49. +conversions. This is mainly due to the fact that the imported code makes use of
  50. +types and functions defined in the C standard library, which isn't available.
  51. +Furthermore, using the POSIX wrapper library is not possible as the code needs
  52. +to be part of the kernel.
  53. +
  54. +Updating the code can thus be performed like following:
  55. +
  56. +@example
  57. +$ git clone https://github.com/P-H-C/phc-winner-argon2 argon2
  58. +$ cp argon2/include/argon2.h argon2/src/@{argon2.c,core.c,core.h,ref.c@} \
  59. + grub-core/lib/argon2/
  60. +$ cp argon2/src/blake2/@{blake2-impl.h,blake2.h,blake2b.c,blamka-round-ref.h@} \
  61. + grub-core/lib/argon2/blake2/
  62. +$ sed -e 's/UINT32_C/GRUB_UINT32_C/g' \
  63. + -e 's/UINT64_C/GRUB_UINT64_C/g' \
  64. + -e 's/UINT32_MAX/GRUB_UINT32_MAX/g' \
  65. + -e 's/CHAR_BIT/GRUB_CHAR_BIT/g' \
  66. + -e 's/UINT_MAX/GRUB_UINT_MAX/g' \
  67. + -e 's/uintptr_t/grub_addr_t/g' \
  68. + -e 's/size_t/grub_size_t/g' \
  69. + -e 's/uint32_t/grub_uint32_t/g' \
  70. + -e 's/uint64_t/grub_uint64_t/g' \
  71. + -e 's/uint8_t/grub_uint8_t/g' \
  72. + -e 's/memset/grub_memset/g' \
  73. + -e 's/memcpy/grub_memcpy/g' \
  74. + -e 's/malloc/grub_malloc/g' \
  75. + -e 's/free/grub_free/g' \
  76. + -e 's/#elif _MSC_VER/#elif defined(_MSC_VER)/' \
  77. + grub-core/lib/argon2/@{*,blake2/*@}.@{c,h@} -i
  78. +@end example
  79. +
  80. +Afterwards, you need to perform the following manual steps:
  81. +
  82. +@enumerate
  83. +@item Remove all includes of standard library headers, "encoding.h" and
  84. + "thread.h".
  85. +@item Add includes <grub/mm.h> and <grub/misc.h> to "argon2.h".
  86. +@item Add include <grub/dl.h> and module license declaration to "argon2.c".
  87. +@item Remove the following declarations and functions from "argon2.h" and
  88. + "argon2.c": argon2_type2string, argon2i_hash_encoded, argon2i_hash_raw,
  89. + argon2d_hash_encoded, argon2d_hash_raw, argon2id_hash_encoded,
  90. + argon2id_hash_raw, argon2_compare, argon2_verify, argon2i_verify,
  91. + argon2d_verify, argon2id_verify, argon2d_ctx, argon2i_ctx, argon2id_ctx,
  92. + argon2_verify_ctx, argon2d_verify_ctx, argon2i_verify_ctx,
  93. + argon2id_verify_ctx, argon2_encodedlen.
  94. +@item Move the declaration of `clear_internal_memory()` in "blake2-impl.h" to
  95. + "blake2b.c".
  96. +@item Remove code guarded by the ARGON2_NO_THREADS macro.
  97. +@item Remove parameters `encoded` and `encodedlen` from `argon2_hash` and remove
  98. + the encoding block in that function.
  99. +@item Remove parameter verifications in `validate_inputs()` for
  100. + ARGON2_MIN_PWD_LENGTH, ARGON2_MIN_SECRET, ARGON2_MIN_AD_LENGTH and
  101. + ARGON2_MAX_MEMORY to fix compiler warnings.
  102. +@item Mark the function argon2_ctx as static.
  103. +@end enumerate
  104. +
  105. @node Gnulib
  106. @section Gnulib
  107. diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
  108. index 705d73fab..452f11b20 100644
  109. --- a/grub-core/Makefile.core.def
  110. +++ b/grub-core/Makefile.core.def
  111. @@ -1219,6 +1219,14 @@ module = {
  112. common = lib/json/json.c;
  113. };
  114. +module = {
  115. + name = argon2;
  116. + common = lib/argon2/argon2.c;
  117. + common = lib/argon2/core.c;
  118. + common = lib/argon2/ref.c;
  119. + common = lib/argon2/blake2/blake2b.c;
  120. +};
  121. +
  122. module = {
  123. name = afsplitter;
  124. common = disk/AFSplitter.c;
  125. diff --git a/grub-core/lib/argon2/LICENSE b/grub-core/lib/argon2/LICENSE
  126. new file mode 100644
  127. index 000000000..97aae2925
  128. --- /dev/null
  129. +++ b/grub-core/lib/argon2/LICENSE
  130. @@ -0,0 +1,314 @@
  131. +Argon2 reference source code package - reference C implementations
  132. +
  133. +Copyright 2015
  134. +Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  135. +
  136. +You may use this work under the terms of a Creative Commons CC0 1.0
  137. +License/Waiver or the Apache Public License 2.0, at your option. The terms of
  138. +these licenses can be found at:
  139. +
  140. +- CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  141. +- Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  142. +
  143. +The terms of the licenses are reproduced below.
  144. +
  145. +--------------------------------------------------------------------------------
  146. +
  147. +Creative Commons Legal Code
  148. +
  149. +CC0 1.0 Universal
  150. +
  151. + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
  152. + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
  153. + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
  154. + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
  155. + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
  156. + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
  157. + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
  158. + HEREUNDER.
  159. +
  160. +Statement of Purpose
  161. +
  162. +The laws of most jurisdictions throughout the world automatically confer
  163. +exclusive Copyright and Related Rights (defined below) upon the creator
  164. +and subsequent owner(s) (each and all, an "owner") of an original work of
  165. +authorship and/or a database (each, a "Work").
  166. +
  167. +Certain owners wish to permanently relinquish those rights to a Work for
  168. +the purpose of contributing to a commons of creative, cultural and
  169. +scientific works ("Commons") that the public can reliably and without fear
  170. +of later claims of infringement build upon, modify, incorporate in other
  171. +works, reuse and redistribute as freely as possible in any form whatsoever
  172. +and for any purposes, including without limitation commercial purposes.
  173. +These owners may contribute to the Commons to promote the ideal of a free
  174. +culture and the further production of creative, cultural and scientific
  175. +works, or to gain reputation or greater distribution for their Work in
  176. +part through the use and efforts of others.
  177. +
  178. +For these and/or other purposes and motivations, and without any
  179. +expectation of additional consideration or compensation, the person
  180. +associating CC0 with a Work (the "Affirmer"), to the extent that he or she
  181. +is an owner of Copyright and Related Rights in the Work, voluntarily
  182. +elects to apply CC0 to the Work and publicly distribute the Work under its
  183. +terms, with knowledge of his or her Copyright and Related Rights in the
  184. +Work and the meaning and intended legal effect of CC0 on those rights.
  185. +
  186. +1. Copyright and Related Rights. A Work made available under CC0 may be
  187. +protected by copyright and related or neighboring rights ("Copyright and
  188. +Related Rights"). Copyright and Related Rights include, but are not
  189. +limited to, the following:
  190. +
  191. + i. the right to reproduce, adapt, distribute, perform, display,
  192. + communicate, and translate a Work;
  193. + ii. moral rights retained by the original author(s) and/or performer(s);
  194. +iii. publicity and privacy rights pertaining to a person's image or
  195. + likeness depicted in a Work;
  196. + iv. rights protecting against unfair competition in regards to a Work,
  197. + subject to the limitations in paragraph 4(a), below;
  198. + v. rights protecting the extraction, dissemination, use and reuse of data
  199. + in a Work;
  200. + vi. database rights (such as those arising under Directive 96/9/EC of the
  201. + European Parliament and of the Council of 11 March 1996 on the legal
  202. + protection of databases, and under any national implementation
  203. + thereof, including any amended or successor version of such
  204. + directive); and
  205. +vii. other similar, equivalent or corresponding rights throughout the
  206. + world based on applicable law or treaty, and any national
  207. + implementations thereof.
  208. +
  209. +2. Waiver. To the greatest extent permitted by, but not in contravention
  210. +of, applicable law, Affirmer hereby overtly, fully, permanently,
  211. +irrevocably and unconditionally waives, abandons, and surrenders all of
  212. +Affirmer's Copyright and Related Rights and associated claims and causes
  213. +of action, whether now known or unknown (including existing as well as
  214. +future claims and causes of action), in the Work (i) in all territories
  215. +worldwide, (ii) for the maximum duration provided by applicable law or
  216. +treaty (including future time extensions), (iii) in any current or future
  217. +medium and for any number of copies, and (iv) for any purpose whatsoever,
  218. +including without limitation commercial, advertising or promotional
  219. +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
  220. +member of the public at large and to the detriment of Affirmer's heirs and
  221. +successors, fully intending that such Waiver shall not be subject to
  222. +revocation, rescission, cancellation, termination, or any other legal or
  223. +equitable action to disrupt the quiet enjoyment of the Work by the public
  224. +as contemplated by Affirmer's express Statement of Purpose.
  225. +
  226. +3. Public License Fallback. Should any part of the Waiver for any reason
  227. +be judged legally invalid or ineffective under applicable law, then the
  228. +Waiver shall be preserved to the maximum extent permitted taking into
  229. +account Affirmer's express Statement of Purpose. In addition, to the
  230. +extent the Waiver is so judged Affirmer hereby grants to each affected
  231. +person a royalty-free, non transferable, non sublicensable, non exclusive,
  232. +irrevocable and unconditional license to exercise Affirmer's Copyright and
  233. +Related Rights in the Work (i) in all territories worldwide, (ii) for the
  234. +maximum duration provided by applicable law or treaty (including future
  235. +time extensions), (iii) in any current or future medium and for any number
  236. +of copies, and (iv) for any purpose whatsoever, including without
  237. +limitation commercial, advertising or promotional purposes (the
  238. +"License"). The License shall be deemed effective as of the date CC0 was
  239. +applied by Affirmer to the Work. Should any part of the License for any
  240. +reason be judged legally invalid or ineffective under applicable law, such
  241. +partial invalidity or ineffectiveness shall not invalidate the remainder
  242. +of the License, and in such case Affirmer hereby affirms that he or she
  243. +will not (i) exercise any of his or her remaining Copyright and Related
  244. +Rights in the Work or (ii) assert any associated claims and causes of
  245. +action with respect to the Work, in either case contrary to Affirmer's
  246. +express Statement of Purpose.
  247. +
  248. +4. Limitations and Disclaimers.
  249. +
  250. + a. No trademark or patent rights held by Affirmer are waived, abandoned,
  251. + surrendered, licensed or otherwise affected by this document.
  252. + b. Affirmer offers the Work as-is and makes no representations or
  253. + warranties of any kind concerning the Work, express, implied,
  254. + statutory or otherwise, including without limitation warranties of
  255. + title, merchantability, fitness for a particular purpose, non
  256. + infringement, or the absence of latent or other defects, accuracy, or
  257. + the present or absence of errors, whether or not discoverable, all to
  258. + the greatest extent permissible under applicable law.
  259. + c. Affirmer disclaims responsibility for clearing rights of other persons
  260. + that may apply to the Work or any use thereof, including without
  261. + limitation any person's Copyright and Related Rights in the Work.
  262. + Further, Affirmer disclaims responsibility for obtaining any necessary
  263. + consents, permissions or other rights required for any use of the
  264. + Work.
  265. + d. Affirmer understands and acknowledges that Creative Commons is not a
  266. + party to this document and has no duty or obligation with respect to
  267. + this CC0 or use of the Work.
  268. +
  269. +--------------------------------------------------------------------------------
  270. +
  271. + Apache License
  272. + Version 2.0, January 2004
  273. + http://www.apache.org/licenses/
  274. +
  275. + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
  276. +
  277. + 1. Definitions.
  278. +
  279. + "License" shall mean the terms and conditions for use, reproduction,
  280. + and distribution as defined by Sections 1 through 9 of this document.
  281. +
  282. + "Licensor" shall mean the copyright owner or entity authorized by
  283. + the copyright owner that is granting the License.
  284. +
  285. + "Legal Entity" shall mean the union of the acting entity and all
  286. + other entities that control, are controlled by, or are under common
  287. + control with that entity. For the purposes of this definition,
  288. + "control" means (i) the power, direct or indirect, to cause the
  289. + direction or management of such entity, whether by contract or
  290. + otherwise, or (ii) ownership of fifty percent (50%) or more of the
  291. + outstanding shares, or (iii) beneficial ownership of such entity.
  292. +
  293. + "You" (or "Your") shall mean an individual or Legal Entity
  294. + exercising permissions granted by this License.
  295. +
  296. + "Source" form shall mean the preferred form for making modifications,
  297. + including but not limited to software source code, documentation
  298. + source, and configuration files.
  299. +
  300. + "Object" form shall mean any form resulting from mechanical
  301. + transformation or translation of a Source form, including but
  302. + not limited to compiled object code, generated documentation,
  303. + and conversions to other media types.
  304. +
  305. + "Work" shall mean the work of authorship, whether in Source or
  306. + Object form, made available under the License, as indicated by a
  307. + copyright notice that is included in or attached to the work
  308. + (an example is provided in the Appendix below).
  309. +
  310. + "Derivative Works" shall mean any work, whether in Source or Object
  311. + form, that is based on (or derived from) the Work and for which the
  312. + editorial revisions, annotations, elaborations, or other modifications
  313. + represent, as a whole, an original work of authorship. For the purposes
  314. + of this License, Derivative Works shall not include works that remain
  315. + separable from, or merely link (or bind by name) to the interfaces of,
  316. + the Work and Derivative Works thereof.
  317. +
  318. + "Contribution" shall mean any work of authorship, including
  319. + the original version of the Work and any modifications or additions
  320. + to that Work or Derivative Works thereof, that is intentionally
  321. + submitted to Licensor for inclusion in the Work by the copyright owner
  322. + or by an individual or Legal Entity authorized to submit on behalf of
  323. + the copyright owner. For the purposes of this definition, "submitted"
  324. + means any form of electronic, verbal, or written communication sent
  325. + to the Licensor or its representatives, including but not limited to
  326. + communication on electronic mailing lists, source code control systems,
  327. + and issue tracking systems that are managed by, or on behalf of, the
  328. + Licensor for the purpose of discussing and improving the Work, but
  329. + excluding communication that is conspicuously marked or otherwise
  330. + designated in writing by the copyright owner as "Not a Contribution."
  331. +
  332. + "Contributor" shall mean Licensor and any individual or Legal Entity
  333. + on behalf of whom a Contribution has been received by Licensor and
  334. + subsequently incorporated within the Work.
  335. +
  336. + 2. Grant of Copyright License. Subject to the terms and conditions of
  337. + this License, each Contributor hereby grants to You a perpetual,
  338. + worldwide, non-exclusive, no-charge, royalty-free, irrevocable
  339. + copyright license to reproduce, prepare Derivative Works of,
  340. + publicly display, publicly perform, sublicense, and distribute the
  341. + Work and such Derivative Works in Source or Object form.
  342. +
  343. + 3. Grant of Patent License. Subject to the terms and conditions of
  344. + this License, each Contributor hereby grants to You a perpetual,
  345. + worldwide, non-exclusive, no-charge, royalty-free, irrevocable
  346. + (except as stated in this section) patent license to make, have made,
  347. + use, offer to sell, sell, import, and otherwise transfer the Work,
  348. + where such license applies only to those patent claims licensable
  349. + by such Contributor that are necessarily infringed by their
  350. + Contribution(s) alone or by combination of their Contribution(s)
  351. + with the Work to which such Contribution(s) was submitted. If You
  352. + institute patent litigation against any entity (including a
  353. + cross-claim or counterclaim in a lawsuit) alleging that the Work
  354. + or a Contribution incorporated within the Work constitutes direct
  355. + or contributory patent infringement, then any patent licenses
  356. + granted to You under this License for that Work shall terminate
  357. + as of the date such litigation is filed.
  358. +
  359. + 4. Redistribution. You may reproduce and distribute copies of the
  360. + Work or Derivative Works thereof in any medium, with or without
  361. + modifications, and in Source or Object form, provided that You
  362. + meet the following conditions:
  363. +
  364. + (a) You must give any other recipients of the Work or
  365. + Derivative Works a copy of this License; and
  366. +
  367. + (b) You must cause any modified files to carry prominent notices
  368. + stating that You changed the files; and
  369. +
  370. + (c) You must retain, in the Source form of any Derivative Works
  371. + that You distribute, all copyright, patent, trademark, and
  372. + attribution notices from the Source form of the Work,
  373. + excluding those notices that do not pertain to any part of
  374. + the Derivative Works; and
  375. +
  376. + (d) If the Work includes a "NOTICE" text file as part of its
  377. + distribution, then any Derivative Works that You distribute must
  378. + include a readable copy of the attribution notices contained
  379. + within such NOTICE file, excluding those notices that do not
  380. + pertain to any part of the Derivative Works, in at least one
  381. + of the following places: within a NOTICE text file distributed
  382. + as part of the Derivative Works; within the Source form or
  383. + documentation, if provided along with the Derivative Works; or,
  384. + within a display generated by the Derivative Works, if and
  385. + wherever such third-party notices normally appear. The contents
  386. + of the NOTICE file are for informational purposes only and
  387. + do not modify the License. You may add Your own attribution
  388. + notices within Derivative Works that You distribute, alongside
  389. + or as an addendum to the NOTICE text from the Work, provided
  390. + that such additional attribution notices cannot be construed
  391. + as modifying the License.
  392. +
  393. + You may add Your own copyright statement to Your modifications and
  394. + may provide additional or different license terms and conditions
  395. + for use, reproduction, or distribution of Your modifications, or
  396. + for any such Derivative Works as a whole, provided Your use,
  397. + reproduction, and distribution of the Work otherwise complies with
  398. + the conditions stated in this License.
  399. +
  400. + 5. Submission of Contributions. Unless You explicitly state otherwise,
  401. + any Contribution intentionally submitted for inclusion in the Work
  402. + by You to the Licensor shall be under the terms and conditions of
  403. + this License, without any additional terms or conditions.
  404. + Notwithstanding the above, nothing herein shall supersede or modify
  405. + the terms of any separate license agreement you may have executed
  406. + with Licensor regarding such Contributions.
  407. +
  408. + 6. Trademarks. This License does not grant permission to use the trade
  409. + names, trademarks, service marks, or product names of the Licensor,
  410. + except as required for reasonable and customary use in describing the
  411. + origin of the Work and reproducing the content of the NOTICE file.
  412. +
  413. + 7. Disclaimer of Warranty. Unless required by applicable law or
  414. + agreed to in writing, Licensor provides the Work (and each
  415. + Contributor provides its Contributions) on an "AS IS" BASIS,
  416. + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  417. + implied, including, without limitation, any warranties or conditions
  418. + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
  419. + PARTICULAR PURPOSE. You are solely responsible for determining the
  420. + appropriateness of using or redistributing the Work and assume any
  421. + risks associated with Your exercise of permissions under this License.
  422. +
  423. + 8. Limitation of Liability. In no event and under no legal theory,
  424. + whether in tort (including negligence), contract, or otherwise,
  425. + unless required by applicable law (such as deliberate and grossly
  426. + negligent acts) or agreed to in writing, shall any Contributor be
  427. + liable to You for damages, including any direct, indirect, special,
  428. + incidental, or consequential damages of any character arising as a
  429. + result of this License or out of the use or inability to use the
  430. + Work (including but not limited to damages for loss of goodwill,
  431. + work stoppage, computer failure or malfunction, or any and all
  432. + other commercial damages or losses), even if such Contributor
  433. + has been advised of the possibility of such damages.
  434. +
  435. + 9. Accepting Warranty or Additional Liability. While redistributing
  436. + the Work or Derivative Works thereof, You may choose to offer,
  437. + and charge a fee for, acceptance of support, warranty, indemnity,
  438. + or other liability obligations and/or rights consistent with this
  439. + License. However, in accepting such obligations, You may act only
  440. + on Your own behalf and on Your sole responsibility, not on behalf
  441. + of any other Contributor, and only if You agree to indemnify,
  442. + defend, and hold each Contributor harmless for any liability
  443. + incurred by, or claims asserted against, such Contributor by reason
  444. + of your accepting any such warranty or additional liability.
  445. diff --git a/grub-core/lib/argon2/argon2.c b/grub-core/lib/argon2/argon2.c
  446. new file mode 100644
  447. index 000000000..49532fe80
  448. --- /dev/null
  449. +++ b/grub-core/lib/argon2/argon2.c
  450. @@ -0,0 +1,232 @@
  451. +/*
  452. + * Argon2 reference source code package - reference C implementations
  453. + *
  454. + * Copyright 2015
  455. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  456. + *
  457. + * You may use this work under the terms of a Creative Commons CC0 1.0
  458. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  459. + * these licenses can be found at:
  460. + *
  461. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  462. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  463. + *
  464. + * You should have received a copy of both of these licenses along with this
  465. + * software. If not, they may be obtained at the above URLs.
  466. + */
  467. +
  468. +#include <grub/dl.h>
  469. +
  470. +#include "argon2.h"
  471. +#include "core.h"
  472. +
  473. +GRUB_MOD_LICENSE ("CC0");
  474. +
  475. +static int argon2_ctx(argon2_context *context, argon2_type type) {
  476. + /* 1. Validate all inputs */
  477. + int result = validate_inputs(context);
  478. + grub_uint32_t memory_blocks, segment_length;
  479. + argon2_instance_t instance;
  480. +
  481. + if (ARGON2_OK != result) {
  482. + return result;
  483. + }
  484. +
  485. + if (Argon2_d != type && Argon2_i != type && Argon2_id != type) {
  486. + return ARGON2_INCORRECT_TYPE;
  487. + }
  488. +
  489. + /* 2. Align memory size */
  490. + /* Minimum memory_blocks = 8L blocks, where L is the number of lanes */
  491. + memory_blocks = context->m_cost;
  492. +
  493. + if (memory_blocks < 2 * ARGON2_SYNC_POINTS * context->lanes) {
  494. + memory_blocks = 2 * ARGON2_SYNC_POINTS * context->lanes;
  495. + }
  496. +
  497. + segment_length = memory_blocks / (context->lanes * ARGON2_SYNC_POINTS);
  498. + /* Ensure that all segments have equal length */
  499. + memory_blocks = segment_length * (context->lanes * ARGON2_SYNC_POINTS);
  500. +
  501. + instance.version = context->version;
  502. + instance.memory = NULL;
  503. + instance.passes = context->t_cost;
  504. + instance.memory_blocks = memory_blocks;
  505. + instance.segment_length = segment_length;
  506. + instance.lane_length = segment_length * ARGON2_SYNC_POINTS;
  507. + instance.lanes = context->lanes;
  508. + instance.threads = context->threads;
  509. + instance.type = type;
  510. +
  511. + if (instance.threads > instance.lanes) {
  512. + instance.threads = instance.lanes;
  513. + }
  514. +
  515. + /* 3. Initialization: Hashing inputs, allocating memory, filling first
  516. + * blocks
  517. + */
  518. + result = initialize(&instance, context);
  519. +
  520. + if (ARGON2_OK != result) {
  521. + return result;
  522. + }
  523. +
  524. + /* 4. Filling memory */
  525. + result = fill_memory_blocks(&instance);
  526. +
  527. + if (ARGON2_OK != result) {
  528. + return result;
  529. + }
  530. + /* 5. Finalization */
  531. + finalize(context, &instance);
  532. +
  533. + return ARGON2_OK;
  534. +}
  535. +
  536. +int argon2_hash(const grub_uint32_t t_cost, const grub_uint32_t m_cost,
  537. + const grub_uint32_t parallelism, const void *pwd,
  538. + const grub_size_t pwdlen, const void *salt, const grub_size_t saltlen,
  539. + void *hash, const grub_size_t hashlen, argon2_type type,
  540. + const grub_uint32_t version){
  541. +
  542. + argon2_context context;
  543. + int result;
  544. + grub_uint8_t *out;
  545. +
  546. + if (pwdlen > ARGON2_MAX_PWD_LENGTH) {
  547. + return ARGON2_PWD_TOO_LONG;
  548. + }
  549. +
  550. + if (saltlen > ARGON2_MAX_SALT_LENGTH) {
  551. + return ARGON2_SALT_TOO_LONG;
  552. + }
  553. +
  554. + if (hashlen > ARGON2_MAX_OUTLEN) {
  555. + return ARGON2_OUTPUT_TOO_LONG;
  556. + }
  557. +
  558. + if (hashlen < ARGON2_MIN_OUTLEN) {
  559. + return ARGON2_OUTPUT_TOO_SHORT;
  560. + }
  561. +
  562. + out = grub_malloc(hashlen);
  563. + if (!out) {
  564. + return ARGON2_MEMORY_ALLOCATION_ERROR;
  565. + }
  566. +
  567. + context.out = (grub_uint8_t *)out;
  568. + context.outlen = (grub_uint32_t)hashlen;
  569. + context.pwd = CONST_CAST(grub_uint8_t *)pwd;
  570. + context.pwdlen = (grub_uint32_t)pwdlen;
  571. + context.salt = CONST_CAST(grub_uint8_t *)salt;
  572. + context.saltlen = (grub_uint32_t)saltlen;
  573. + context.secret = NULL;
  574. + context.secretlen = 0;
  575. + context.ad = NULL;
  576. + context.adlen = 0;
  577. + context.t_cost = t_cost;
  578. + context.m_cost = m_cost;
  579. + context.lanes = parallelism;
  580. + context.threads = parallelism;
  581. + context.allocate_cbk = NULL;
  582. + context.grub_free_cbk = NULL;
  583. + context.flags = ARGON2_DEFAULT_FLAGS;
  584. + context.version = version;
  585. +
  586. + result = argon2_ctx(&context, type);
  587. +
  588. + if (result != ARGON2_OK) {
  589. + clear_internal_memory(out, hashlen);
  590. + grub_free(out);
  591. + return result;
  592. + }
  593. +
  594. + /* if raw hash requested, write it */
  595. + if (hash) {
  596. + grub_memcpy(hash, out, hashlen);
  597. + }
  598. +
  599. + clear_internal_memory(out, hashlen);
  600. + grub_free(out);
  601. +
  602. + return ARGON2_OK;
  603. +}
  604. +
  605. +const char *argon2_error_message(int error_code) {
  606. + switch (error_code) {
  607. + case ARGON2_OK:
  608. + return "OK";
  609. + case ARGON2_OUTPUT_PTR_NULL:
  610. + return "Output pointer is NULL";
  611. + case ARGON2_OUTPUT_TOO_SHORT:
  612. + return "Output is too short";
  613. + case ARGON2_OUTPUT_TOO_LONG:
  614. + return "Output is too long";
  615. + case ARGON2_PWD_TOO_SHORT:
  616. + return "Password is too short";
  617. + case ARGON2_PWD_TOO_LONG:
  618. + return "Password is too long";
  619. + case ARGON2_SALT_TOO_SHORT:
  620. + return "Salt is too short";
  621. + case ARGON2_SALT_TOO_LONG:
  622. + return "Salt is too long";
  623. + case ARGON2_AD_TOO_SHORT:
  624. + return "Associated data is too short";
  625. + case ARGON2_AD_TOO_LONG:
  626. + return "Associated data is too long";
  627. + case ARGON2_SECRET_TOO_SHORT:
  628. + return "Secret is too short";
  629. + case ARGON2_SECRET_TOO_LONG:
  630. + return "Secret is too long";
  631. + case ARGON2_TIME_TOO_SMALL:
  632. + return "Time cost is too small";
  633. + case ARGON2_TIME_TOO_LARGE:
  634. + return "Time cost is too large";
  635. + case ARGON2_MEMORY_TOO_LITTLE:
  636. + return "Memory cost is too small";
  637. + case ARGON2_MEMORY_TOO_MUCH:
  638. + return "Memory cost is too large";
  639. + case ARGON2_LANES_TOO_FEW:
  640. + return "Too few lanes";
  641. + case ARGON2_LANES_TOO_MANY:
  642. + return "Too many lanes";
  643. + case ARGON2_PWD_PTR_MISMATCH:
  644. + return "Password pointer is NULL, but password length is not 0";
  645. + case ARGON2_SALT_PTR_MISMATCH:
  646. + return "Salt pointer is NULL, but salt length is not 0";
  647. + case ARGON2_SECRET_PTR_MISMATCH:
  648. + return "Secret pointer is NULL, but secret length is not 0";
  649. + case ARGON2_AD_PTR_MISMATCH:
  650. + return "Associated data pointer is NULL, but ad length is not 0";
  651. + case ARGON2_MEMORY_ALLOCATION_ERROR:
  652. + return "Memory allocation error";
  653. + case ARGON2_FREE_MEMORY_CBK_NULL:
  654. + return "The grub_free memory callback is NULL";
  655. + case ARGON2_ALLOCATE_MEMORY_CBK_NULL:
  656. + return "The allocate memory callback is NULL";
  657. + case ARGON2_INCORRECT_PARAMETER:
  658. + return "Argon2_Context context is NULL";
  659. + case ARGON2_INCORRECT_TYPE:
  660. + return "There is no such version of Argon2";
  661. + case ARGON2_OUT_PTR_MISMATCH:
  662. + return "Output pointer mismatch";
  663. + case ARGON2_THREADS_TOO_FEW:
  664. + return "Not enough threads";
  665. + case ARGON2_THREADS_TOO_MANY:
  666. + return "Too many threads";
  667. + case ARGON2_MISSING_ARGS:
  668. + return "Missing arguments";
  669. + case ARGON2_ENCODING_FAIL:
  670. + return "Encoding failed";
  671. + case ARGON2_DECODING_FAIL:
  672. + return "Decoding failed";
  673. + case ARGON2_THREAD_FAIL:
  674. + return "Threading failure";
  675. + case ARGON2_DECODING_LENGTH_FAIL:
  676. + return "Some of encoded parameters are too long or too short";
  677. + case ARGON2_VERIFY_MISMATCH:
  678. + return "The password does not match the supplied hash";
  679. + default:
  680. + return "Unknown error code";
  681. + }
  682. +}
  683. diff --git a/grub-core/lib/argon2/argon2.h b/grub-core/lib/argon2/argon2.h
  684. new file mode 100644
  685. index 000000000..129f7efbd
  686. --- /dev/null
  687. +++ b/grub-core/lib/argon2/argon2.h
  688. @@ -0,0 +1,264 @@
  689. +/*
  690. + * Argon2 reference source code package - reference C implementations
  691. + *
  692. + * Copyright 2015
  693. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  694. + *
  695. + * You may use this work under the terms of a Creative Commons CC0 1.0
  696. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  697. + * these licenses can be found at:
  698. + *
  699. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  700. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  701. + *
  702. + * You should have received a copy of both of these licenses along with this
  703. + * software. If not, they may be obtained at the above URLs.
  704. + */
  705. +
  706. +#ifndef ARGON2_H
  707. +#define ARGON2_H
  708. +
  709. +#include <grub/misc.h>
  710. +#include <grub/mm.h>
  711. +
  712. +#if defined(__cplusplus)
  713. +extern "C" {
  714. +#endif
  715. +
  716. +/* Symbols visibility control */
  717. +#ifdef A2_VISCTL
  718. +#define ARGON2_PUBLIC __attribute__((visibility("default")))
  719. +#define ARGON2_LOCAL __attribute__ ((visibility ("hidden")))
  720. +#elif defined(_MSC_VER)
  721. +#define ARGON2_PUBLIC __declspec(dllexport)
  722. +#define ARGON2_LOCAL
  723. +#else
  724. +#define ARGON2_PUBLIC
  725. +#define ARGON2_LOCAL
  726. +#endif
  727. +
  728. +/*
  729. + * Argon2 input parameter restrictions
  730. + */
  731. +
  732. +/* Minimum and maximum number of lanes (degree of parallelism) */
  733. +#define ARGON2_MIN_LANES GRUB_UINT32_C(1)
  734. +#define ARGON2_MAX_LANES GRUB_UINT32_C(0xFFFFFF)
  735. +
  736. +/* Minimum and maximum number of threads */
  737. +#define ARGON2_MIN_THREADS GRUB_UINT32_C(1)
  738. +#define ARGON2_MAX_THREADS GRUB_UINT32_C(0xFFFFFF)
  739. +
  740. +/* Number of synchronization points between lanes per pass */
  741. +#define ARGON2_SYNC_POINTS GRUB_UINT32_C(4)
  742. +
  743. +/* Minimum and maximum digest size in bytes */
  744. +#define ARGON2_MIN_OUTLEN GRUB_UINT32_C(4)
  745. +#define ARGON2_MAX_OUTLEN GRUB_UINT32_C(0xFFFFFFFF)
  746. +
  747. +/* Minimum and maximum number of memory blocks (each of BLOCK_SIZE bytes) */
  748. +#define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS) /* 2 blocks per slice */
  749. +
  750. +#define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b))
  751. +/* Max memory size is addressing-space/2, topping at 2^32 blocks (4 TB) */
  752. +#define ARGON2_MAX_MEMORY_BITS \
  753. + ARGON2_MIN(GRUB_UINT32_C(32), (sizeof(void *) * GRUB_CHAR_BIT - 10 - 1))
  754. +#define ARGON2_MAX_MEMORY \
  755. + ARGON2_MIN(GRUB_UINT32_C(0xFFFFFFFF), GRUB_UINT64_C(1) << ARGON2_MAX_MEMORY_BITS)
  756. +
  757. +/* Minimum and maximum number of passes */
  758. +#define ARGON2_MIN_TIME GRUB_UINT32_C(1)
  759. +#define ARGON2_MAX_TIME GRUB_UINT32_C(0xFFFFFFFF)
  760. +
  761. +/* Minimum and maximum password length in bytes */
  762. +#define ARGON2_MIN_PWD_LENGTH GRUB_UINT32_C(0)
  763. +#define ARGON2_MAX_PWD_LENGTH GRUB_UINT32_C(0xFFFFFFFF)
  764. +
  765. +/* Minimum and maximum associated data length in bytes */
  766. +#define ARGON2_MIN_AD_LENGTH GRUB_UINT32_C(0)
  767. +#define ARGON2_MAX_AD_LENGTH GRUB_UINT32_C(0xFFFFFFFF)
  768. +
  769. +/* Minimum and maximum salt length in bytes */
  770. +#define ARGON2_MIN_SALT_LENGTH GRUB_UINT32_C(8)
  771. +#define ARGON2_MAX_SALT_LENGTH GRUB_UINT32_C(0xFFFFFFFF)
  772. +
  773. +/* Minimum and maximum key length in bytes */
  774. +#define ARGON2_MIN_SECRET GRUB_UINT32_C(0)
  775. +#define ARGON2_MAX_SECRET GRUB_UINT32_C(0xFFFFFFFF)
  776. +
  777. +/* Flags to determine which fields are securely wiped (default = no wipe). */
  778. +#define ARGON2_DEFAULT_FLAGS GRUB_UINT32_C(0)
  779. +#define ARGON2_FLAG_CLEAR_PASSWORD (GRUB_UINT32_C(1) << 0)
  780. +#define ARGON2_FLAG_CLEAR_SECRET (GRUB_UINT32_C(1) << 1)
  781. +
  782. +/* Global flag to determine if we are wiping internal memory buffers. This flag
  783. + * is defined in core.c and defaults to 1 (wipe internal memory). */
  784. +extern int FLAG_clear_internal_memory;
  785. +
  786. +/* Error codes */
  787. +typedef enum Argon2_ErrorCodes {
  788. + ARGON2_OK = 0,
  789. +
  790. + ARGON2_OUTPUT_PTR_NULL = -1,
  791. +
  792. + ARGON2_OUTPUT_TOO_SHORT = -2,
  793. + ARGON2_OUTPUT_TOO_LONG = -3,
  794. +
  795. + ARGON2_PWD_TOO_SHORT = -4,
  796. + ARGON2_PWD_TOO_LONG = -5,
  797. +
  798. + ARGON2_SALT_TOO_SHORT = -6,
  799. + ARGON2_SALT_TOO_LONG = -7,
  800. +
  801. + ARGON2_AD_TOO_SHORT = -8,
  802. + ARGON2_AD_TOO_LONG = -9,
  803. +
  804. + ARGON2_SECRET_TOO_SHORT = -10,
  805. + ARGON2_SECRET_TOO_LONG = -11,
  806. +
  807. + ARGON2_TIME_TOO_SMALL = -12,
  808. + ARGON2_TIME_TOO_LARGE = -13,
  809. +
  810. + ARGON2_MEMORY_TOO_LITTLE = -14,
  811. + ARGON2_MEMORY_TOO_MUCH = -15,
  812. +
  813. + ARGON2_LANES_TOO_FEW = -16,
  814. + ARGON2_LANES_TOO_MANY = -17,
  815. +
  816. + ARGON2_PWD_PTR_MISMATCH = -18, /* NULL ptr with non-zero length */
  817. + ARGON2_SALT_PTR_MISMATCH = -19, /* NULL ptr with non-zero length */
  818. + ARGON2_SECRET_PTR_MISMATCH = -20, /* NULL ptr with non-zero length */
  819. + ARGON2_AD_PTR_MISMATCH = -21, /* NULL ptr with non-zero length */
  820. +
  821. + ARGON2_MEMORY_ALLOCATION_ERROR = -22,
  822. +
  823. + ARGON2_FREE_MEMORY_CBK_NULL = -23,
  824. + ARGON2_ALLOCATE_MEMORY_CBK_NULL = -24,
  825. +
  826. + ARGON2_INCORRECT_PARAMETER = -25,
  827. + ARGON2_INCORRECT_TYPE = -26,
  828. +
  829. + ARGON2_OUT_PTR_MISMATCH = -27,
  830. +
  831. + ARGON2_THREADS_TOO_FEW = -28,
  832. + ARGON2_THREADS_TOO_MANY = -29,
  833. +
  834. + ARGON2_MISSING_ARGS = -30,
  835. +
  836. + ARGON2_ENCODING_FAIL = -31,
  837. +
  838. + ARGON2_DECODING_FAIL = -32,
  839. +
  840. + ARGON2_THREAD_FAIL = -33,
  841. +
  842. + ARGON2_DECODING_LENGTH_FAIL = -34,
  843. +
  844. + ARGON2_VERIFY_MISMATCH = -35
  845. +} argon2_error_codes;
  846. +
  847. +/* Memory allocator types --- for external allocation */
  848. +typedef int (*allocate_fptr)(grub_uint8_t **memory, grub_size_t bytes_to_allocate);
  849. +typedef void (*deallocate_fptr)(grub_uint8_t *memory, grub_size_t bytes_to_allocate);
  850. +
  851. +/* Argon2 external data structures */
  852. +
  853. +/*
  854. + *****
  855. + * Context: structure to hold Argon2 inputs:
  856. + * output array and its length,
  857. + * password and its length,
  858. + * salt and its length,
  859. + * secret and its length,
  860. + * associated data and its length,
  861. + * number of passes, amount of used memory (in KBytes, can be rounded up a bit)
  862. + * number of parallel threads that will be run.
  863. + * All the parameters above affect the output hash value.
  864. + * Additionally, two function pointers can be provided to allocate and
  865. + * deallocate the memory (if NULL, memory will be allocated internally).
  866. + * Also, three flags indicate whether to erase password, secret as soon as they
  867. + * are pre-hashed (and thus not needed anymore), and the entire memory
  868. + *****
  869. + * Simplest situation: you have output array out[8], password is stored in
  870. + * pwd[32], salt is stored in salt[16], you do not have keys nor associated
  871. + * data. You need to spend 1 GB of RAM and you run 5 passes of Argon2d with
  872. + * 4 parallel lanes.
  873. + * You want to erase the password, but you're OK with last pass not being
  874. + * erased. You want to use the default memory allocator.
  875. + * Then you initialize:
  876. + Argon2_Context(out,8,pwd,32,salt,16,NULL,0,NULL,0,5,1<<20,4,4,NULL,NULL,true,false,false,false)
  877. + */
  878. +typedef struct Argon2_Context {
  879. + grub_uint8_t *out; /* output array */
  880. + grub_uint32_t outlen; /* digest length */
  881. +
  882. + grub_uint8_t *pwd; /* password array */
  883. + grub_uint32_t pwdlen; /* password length */
  884. +
  885. + grub_uint8_t *salt; /* salt array */
  886. + grub_uint32_t saltlen; /* salt length */
  887. +
  888. + grub_uint8_t *secret; /* key array */
  889. + grub_uint32_t secretlen; /* key length */
  890. +
  891. + grub_uint8_t *ad; /* associated data array */
  892. + grub_uint32_t adlen; /* associated data length */
  893. +
  894. + grub_uint32_t t_cost; /* number of passes */
  895. + grub_uint32_t m_cost; /* amount of memory requested (KB) */
  896. + grub_uint32_t lanes; /* number of lanes */
  897. + grub_uint32_t threads; /* maximum number of threads */
  898. +
  899. + grub_uint32_t version; /* version number */
  900. +
  901. + allocate_fptr allocate_cbk; /* pointer to memory allocator */
  902. + deallocate_fptr grub_free_cbk; /* pointer to memory deallocator */
  903. +
  904. + grub_uint32_t flags; /* array of bool options */
  905. +} argon2_context;
  906. +
  907. +/* Argon2 primitive type */
  908. +typedef enum Argon2_type {
  909. + Argon2_d = 0,
  910. + Argon2_i = 1,
  911. + Argon2_id = 2
  912. +} argon2_type;
  913. +
  914. +/* Version of the algorithm */
  915. +typedef enum Argon2_version {
  916. + ARGON2_VERSION_10 = 0x10,
  917. + ARGON2_VERSION_13 = 0x13,
  918. + ARGON2_VERSION_NUMBER = ARGON2_VERSION_13
  919. +} argon2_version;
  920. +
  921. +/**
  922. + * Hashes a password with Argon2, producing a raw hash at @hash
  923. + * @param t_cost Number of iterations
  924. + * @param m_cost Sets memory usage to m_cost kibibytes
  925. + * @param parallelism Number of threads and compute lanes
  926. + * @param pwd Pointer to password
  927. + * @param pwdlen Password size in bytes
  928. + * @param salt Pointer to salt
  929. + * @param saltlen Salt size in bytes
  930. + * @param hash Buffer where to write the raw hash - updated by the function
  931. + * @param hashlen Desired length of the hash in bytes
  932. + * @pre Different parallelism levels will give different results
  933. + * @pre Returns ARGON2_OK if successful
  934. + */
  935. +ARGON2_PUBLIC int argon2_hash(const grub_uint32_t t_cost, const grub_uint32_t m_cost,
  936. + const grub_uint32_t parallelism, const void *pwd,
  937. + const grub_size_t pwdlen, const void *salt,
  938. + const grub_size_t saltlen, void *hash,
  939. + const grub_size_t hashlen, argon2_type type,
  940. + const grub_uint32_t version);
  941. +
  942. +/**
  943. + * Get the associated error message for given error code
  944. + * @return The error message associated with the given error code
  945. + */
  946. +ARGON2_PUBLIC const char *argon2_error_message(int error_code);
  947. +
  948. +#if defined(__cplusplus)
  949. +}
  950. +#endif
  951. +
  952. +#endif
  953. diff --git a/grub-core/lib/argon2/blake2/blake2-impl.h b/grub-core/lib/argon2/blake2/blake2-impl.h
  954. new file mode 100644
  955. index 000000000..3a795680b
  956. --- /dev/null
  957. +++ b/grub-core/lib/argon2/blake2/blake2-impl.h
  958. @@ -0,0 +1,151 @@
  959. +/*
  960. + * Argon2 reference source code package - reference C implementations
  961. + *
  962. + * Copyright 2015
  963. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  964. + *
  965. + * You may use this work under the terms of a Creative Commons CC0 1.0
  966. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  967. + * these licenses can be found at:
  968. + *
  969. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  970. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  971. + *
  972. + * You should have received a copy of both of these licenses along with this
  973. + * software. If not, they may be obtained at the above URLs.
  974. + */
  975. +
  976. +#ifndef PORTABLE_BLAKE2_IMPL_H
  977. +#define PORTABLE_BLAKE2_IMPL_H
  978. +
  979. +#if defined(_MSC_VER)
  980. +#define BLAKE2_INLINE __inline
  981. +#elif defined(__GNUC__) || defined(__clang__)
  982. +#define BLAKE2_INLINE __inline__
  983. +#else
  984. +#define BLAKE2_INLINE
  985. +#endif
  986. +
  987. +/* Argon2 Team - Begin Code */
  988. +/*
  989. + Not an exhaustive list, but should cover the majority of modern platforms
  990. + Additionally, the code will always be correct---this is only a performance
  991. + tweak.
  992. +*/
  993. +#if (defined(__BYTE_ORDER__) && \
  994. + (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
  995. + defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
  996. + defined(__AARCH64EL__) || defined(__amd64__) || defined(__i386__) || \
  997. + defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || \
  998. + defined(_M_ARM)
  999. +#define NATIVE_LITTLE_ENDIAN
  1000. +#endif
  1001. +/* Argon2 Team - End Code */
  1002. +
  1003. +static BLAKE2_INLINE grub_uint32_t load32(const void *src) {
  1004. +#if defined(NATIVE_LITTLE_ENDIAN)
  1005. + grub_uint32_t w;
  1006. + grub_memcpy(&w, src, sizeof w);
  1007. + return w;
  1008. +#else
  1009. + const grub_uint8_t *p = (const grub_uint8_t *)src;
  1010. + grub_uint32_t w = *p++;
  1011. + w |= (grub_uint32_t)(*p++) << 8;
  1012. + w |= (grub_uint32_t)(*p++) << 16;
  1013. + w |= (grub_uint32_t)(*p++) << 24;
  1014. + return w;
  1015. +#endif
  1016. +}
  1017. +
  1018. +static BLAKE2_INLINE grub_uint64_t load64(const void *src) {
  1019. +#if defined(NATIVE_LITTLE_ENDIAN)
  1020. + grub_uint64_t w;
  1021. + grub_memcpy(&w, src, sizeof w);
  1022. + return w;
  1023. +#else
  1024. + const grub_uint8_t *p = (const grub_uint8_t *)src;
  1025. + grub_uint64_t w = *p++;
  1026. + w |= (grub_uint64_t)(*p++) << 8;
  1027. + w |= (grub_uint64_t)(*p++) << 16;
  1028. + w |= (grub_uint64_t)(*p++) << 24;
  1029. + w |= (grub_uint64_t)(*p++) << 32;
  1030. + w |= (grub_uint64_t)(*p++) << 40;
  1031. + w |= (grub_uint64_t)(*p++) << 48;
  1032. + w |= (grub_uint64_t)(*p++) << 56;
  1033. + return w;
  1034. +#endif
  1035. +}
  1036. +
  1037. +static BLAKE2_INLINE void store32(void *dst, grub_uint32_t w) {
  1038. +#if defined(NATIVE_LITTLE_ENDIAN)
  1039. + grub_memcpy(dst, &w, sizeof w);
  1040. +#else
  1041. + grub_uint8_t *p = (grub_uint8_t *)dst;
  1042. + *p++ = (grub_uint8_t)w;
  1043. + w >>= 8;
  1044. + *p++ = (grub_uint8_t)w;
  1045. + w >>= 8;
  1046. + *p++ = (grub_uint8_t)w;
  1047. + w >>= 8;
  1048. + *p++ = (grub_uint8_t)w;
  1049. +#endif
  1050. +}
  1051. +
  1052. +static BLAKE2_INLINE void store64(void *dst, grub_uint64_t w) {
  1053. +#if defined(NATIVE_LITTLE_ENDIAN)
  1054. + grub_memcpy(dst, &w, sizeof w);
  1055. +#else
  1056. + grub_uint8_t *p = (grub_uint8_t *)dst;
  1057. + *p++ = (grub_uint8_t)w;
  1058. + w >>= 8;
  1059. + *p++ = (grub_uint8_t)w;
  1060. + w >>= 8;
  1061. + *p++ = (grub_uint8_t)w;
  1062. + w >>= 8;
  1063. + *p++ = (grub_uint8_t)w;
  1064. + w >>= 8;
  1065. + *p++ = (grub_uint8_t)w;
  1066. + w >>= 8;
  1067. + *p++ = (grub_uint8_t)w;
  1068. + w >>= 8;
  1069. + *p++ = (grub_uint8_t)w;
  1070. + w >>= 8;
  1071. + *p++ = (grub_uint8_t)w;
  1072. +#endif
  1073. +}
  1074. +
  1075. +static BLAKE2_INLINE grub_uint64_t load48(const void *src) {
  1076. + const grub_uint8_t *p = (const grub_uint8_t *)src;
  1077. + grub_uint64_t w = *p++;
  1078. + w |= (grub_uint64_t)(*p++) << 8;
  1079. + w |= (grub_uint64_t)(*p++) << 16;
  1080. + w |= (grub_uint64_t)(*p++) << 24;
  1081. + w |= (grub_uint64_t)(*p++) << 32;
  1082. + w |= (grub_uint64_t)(*p++) << 40;
  1083. + return w;
  1084. +}
  1085. +
  1086. +static BLAKE2_INLINE void store48(void *dst, grub_uint64_t w) {
  1087. + grub_uint8_t *p = (grub_uint8_t *)dst;
  1088. + *p++ = (grub_uint8_t)w;
  1089. + w >>= 8;
  1090. + *p++ = (grub_uint8_t)w;
  1091. + w >>= 8;
  1092. + *p++ = (grub_uint8_t)w;
  1093. + w >>= 8;
  1094. + *p++ = (grub_uint8_t)w;
  1095. + w >>= 8;
  1096. + *p++ = (grub_uint8_t)w;
  1097. + w >>= 8;
  1098. + *p++ = (grub_uint8_t)w;
  1099. +}
  1100. +
  1101. +static BLAKE2_INLINE grub_uint32_t rotr32(const grub_uint32_t w, const unsigned c) {
  1102. + return (w >> c) | (w << (32 - c));
  1103. +}
  1104. +
  1105. +static BLAKE2_INLINE grub_uint64_t rotr64(const grub_uint64_t w, const unsigned c) {
  1106. + return (w >> c) | (w << (64 - c));
  1107. +}
  1108. +
  1109. +#endif
  1110. diff --git a/grub-core/lib/argon2/blake2/blake2.h b/grub-core/lib/argon2/blake2/blake2.h
  1111. new file mode 100644
  1112. index 000000000..4e8efeb22
  1113. --- /dev/null
  1114. +++ b/grub-core/lib/argon2/blake2/blake2.h
  1115. @@ -0,0 +1,89 @@
  1116. +/*
  1117. + * Argon2 reference source code package - reference C implementations
  1118. + *
  1119. + * Copyright 2015
  1120. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  1121. + *
  1122. + * You may use this work under the terms of a Creative Commons CC0 1.0
  1123. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  1124. + * these licenses can be found at:
  1125. + *
  1126. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  1127. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  1128. + *
  1129. + * You should have received a copy of both of these licenses along with this
  1130. + * software. If not, they may be obtained at the above URLs.
  1131. + */
  1132. +
  1133. +#ifndef PORTABLE_BLAKE2_H
  1134. +#define PORTABLE_BLAKE2_H
  1135. +
  1136. +#include "../argon2.h"
  1137. +
  1138. +#if defined(__cplusplus)
  1139. +extern "C" {
  1140. +#endif
  1141. +
  1142. +enum blake2b_constant {
  1143. + BLAKE2B_BLOCKBYTES = 128,
  1144. + BLAKE2B_OUTBYTES = 64,
  1145. + BLAKE2B_KEYBYTES = 64,
  1146. + BLAKE2B_SALTBYTES = 16,
  1147. + BLAKE2B_PERSONALBYTES = 16
  1148. +};
  1149. +
  1150. +#pragma pack(push, 1)
  1151. +typedef struct __blake2b_param {
  1152. + grub_uint8_t digest_length; /* 1 */
  1153. + grub_uint8_t key_length; /* 2 */
  1154. + grub_uint8_t fanout; /* 3 */
  1155. + grub_uint8_t depth; /* 4 */
  1156. + grub_uint32_t leaf_length; /* 8 */
  1157. + grub_uint64_t node_offset; /* 16 */
  1158. + grub_uint8_t node_depth; /* 17 */
  1159. + grub_uint8_t inner_length; /* 18 */
  1160. + grub_uint8_t reserved[14]; /* 32 */
  1161. + grub_uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
  1162. + grub_uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
  1163. +} blake2b_param;
  1164. +#pragma pack(pop)
  1165. +
  1166. +typedef struct __blake2b_state {
  1167. + grub_uint64_t h[8];
  1168. + grub_uint64_t t[2];
  1169. + grub_uint64_t f[2];
  1170. + grub_uint8_t buf[BLAKE2B_BLOCKBYTES];
  1171. + unsigned buflen;
  1172. + unsigned outlen;
  1173. + grub_uint8_t last_node;
  1174. +} blake2b_state;
  1175. +
  1176. +/* Ensure param structs have not been wrongly padded */
  1177. +/* Poor man's static_assert */
  1178. +enum {
  1179. + blake2_size_check_0 = 1 / !!(GRUB_CHAR_BIT == 8),
  1180. + blake2_size_check_2 =
  1181. + 1 / !!(sizeof(blake2b_param) == sizeof(grub_uint64_t) * GRUB_CHAR_BIT)
  1182. +};
  1183. +
  1184. +/* Streaming API */
  1185. +ARGON2_LOCAL int blake2b_init(blake2b_state *S, grub_size_t outlen);
  1186. +ARGON2_LOCAL int blake2b_init_key(blake2b_state *S, grub_size_t outlen, const void *key,
  1187. + grub_size_t keylen);
  1188. +ARGON2_LOCAL int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
  1189. +ARGON2_LOCAL int blake2b_update(blake2b_state *S, const void *in, grub_size_t inlen);
  1190. +ARGON2_LOCAL int blake2b_final(blake2b_state *S, void *out, grub_size_t outlen);
  1191. +
  1192. +/* Simple API */
  1193. +ARGON2_LOCAL int blake2b(void *out, grub_size_t outlen, const void *in, grub_size_t inlen,
  1194. + const void *key, grub_size_t keylen);
  1195. +
  1196. +/* Argon2 Team - Begin Code */
  1197. +ARGON2_LOCAL int blake2b_long(void *out, grub_size_t outlen, const void *in, grub_size_t inlen);
  1198. +/* Argon2 Team - End Code */
  1199. +
  1200. +#if defined(__cplusplus)
  1201. +}
  1202. +#endif
  1203. +
  1204. +#endif
  1205. diff --git a/grub-core/lib/argon2/blake2/blake2b.c b/grub-core/lib/argon2/blake2/blake2b.c
  1206. new file mode 100644
  1207. index 000000000..53abd7bef
  1208. --- /dev/null
  1209. +++ b/grub-core/lib/argon2/blake2/blake2b.c
  1210. @@ -0,0 +1,388 @@
  1211. +/*
  1212. + * Argon2 reference source code package - reference C implementations
  1213. + *
  1214. + * Copyright 2015
  1215. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  1216. + *
  1217. + * You may use this work under the terms of a Creative Commons CC0 1.0
  1218. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  1219. + * these licenses can be found at:
  1220. + *
  1221. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  1222. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  1223. + *
  1224. + * You should have received a copy of both of these licenses along with this
  1225. + * software. If not, they may be obtained at the above URLs.
  1226. + */
  1227. +
  1228. +#include "blake2.h"
  1229. +#include "blake2-impl.h"
  1230. +
  1231. +static const grub_uint64_t blake2b_IV[8] = {
  1232. + GRUB_UINT64_C(0x6a09e667f3bcc908), GRUB_UINT64_C(0xbb67ae8584caa73b),
  1233. + GRUB_UINT64_C(0x3c6ef372fe94f82b), GRUB_UINT64_C(0xa54ff53a5f1d36f1),
  1234. + GRUB_UINT64_C(0x510e527fade682d1), GRUB_UINT64_C(0x9b05688c2b3e6c1f),
  1235. + GRUB_UINT64_C(0x1f83d9abfb41bd6b), GRUB_UINT64_C(0x5be0cd19137e2179)};
  1236. +
  1237. +static const unsigned int blake2b_sigma[12][16] = {
  1238. + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  1239. + {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  1240. + {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
  1241. + {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
  1242. + {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
  1243. + {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
  1244. + {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
  1245. + {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
  1246. + {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
  1247. + {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
  1248. + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  1249. + {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  1250. +};
  1251. +
  1252. +void clear_internal_memory(void *v, grub_size_t n);
  1253. +
  1254. +static BLAKE2_INLINE void blake2b_set_lastnode(blake2b_state *S) {
  1255. + S->f[1] = (grub_uint64_t)-1;
  1256. +}
  1257. +
  1258. +static BLAKE2_INLINE void blake2b_set_lastblock(blake2b_state *S) {
  1259. + if (S->last_node) {
  1260. + blake2b_set_lastnode(S);
  1261. + }
  1262. + S->f[0] = (grub_uint64_t)-1;
  1263. +}
  1264. +
  1265. +static BLAKE2_INLINE void blake2b_increment_counter(blake2b_state *S,
  1266. + grub_uint64_t inc) {
  1267. + S->t[0] += inc;
  1268. + S->t[1] += (S->t[0] < inc);
  1269. +}
  1270. +
  1271. +static BLAKE2_INLINE void blake2b_invalidate_state(blake2b_state *S) {
  1272. + clear_internal_memory(S, sizeof(*S)); /* wipe */
  1273. + blake2b_set_lastblock(S); /* invalidate for further use */
  1274. +}
  1275. +
  1276. +static BLAKE2_INLINE void blake2b_init0(blake2b_state *S) {
  1277. + grub_memset(S, 0, sizeof(*S));
  1278. + grub_memcpy(S->h, blake2b_IV, sizeof(S->h));
  1279. +}
  1280. +
  1281. +int blake2b_init_param(blake2b_state *S, const blake2b_param *P) {
  1282. + const unsigned char *p = (const unsigned char *)P;
  1283. + unsigned int i;
  1284. +
  1285. + if (NULL == P || NULL == S) {
  1286. + return -1;
  1287. + }
  1288. +
  1289. + blake2b_init0(S);
  1290. + /* IV XOR Parameter Block */
  1291. + for (i = 0; i < 8; ++i) {
  1292. + S->h[i] ^= load64(&p[i * sizeof(S->h[i])]);
  1293. + }
  1294. + S->outlen = P->digest_length;
  1295. + return 0;
  1296. +}
  1297. +
  1298. +/* Sequential blake2b initialization */
  1299. +int blake2b_init(blake2b_state *S, grub_size_t outlen) {
  1300. + blake2b_param P;
  1301. +
  1302. + if (S == NULL) {
  1303. + return -1;
  1304. + }
  1305. +
  1306. + if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) {
  1307. + blake2b_invalidate_state(S);
  1308. + return -1;
  1309. + }
  1310. +
  1311. + /* Setup Parameter Block for unkeyed BLAKE2 */
  1312. + P.digest_length = (grub_uint8_t)outlen;
  1313. + P.key_length = 0;
  1314. + P.fanout = 1;
  1315. + P.depth = 1;
  1316. + P.leaf_length = 0;
  1317. + P.node_offset = 0;
  1318. + P.node_depth = 0;
  1319. + P.inner_length = 0;
  1320. + grub_memset(P.reserved, 0, sizeof(P.reserved));
  1321. + grub_memset(P.salt, 0, sizeof(P.salt));
  1322. + grub_memset(P.personal, 0, sizeof(P.personal));
  1323. +
  1324. + return blake2b_init_param(S, &P);
  1325. +}
  1326. +
  1327. +int blake2b_init_key(blake2b_state *S, grub_size_t outlen, const void *key,
  1328. + grub_size_t keylen) {
  1329. + blake2b_param P;
  1330. +
  1331. + if (S == NULL) {
  1332. + return -1;
  1333. + }
  1334. +
  1335. + if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) {
  1336. + blake2b_invalidate_state(S);
  1337. + return -1;
  1338. + }
  1339. +
  1340. + if ((key == 0) || (keylen == 0) || (keylen > BLAKE2B_KEYBYTES)) {
  1341. + blake2b_invalidate_state(S);
  1342. + return -1;
  1343. + }
  1344. +
  1345. + /* Setup Parameter Block for keyed BLAKE2 */
  1346. + P.digest_length = (grub_uint8_t)outlen;
  1347. + P.key_length = (grub_uint8_t)keylen;
  1348. + P.fanout = 1;
  1349. + P.depth = 1;
  1350. + P.leaf_length = 0;
  1351. + P.node_offset = 0;
  1352. + P.node_depth = 0;
  1353. + P.inner_length = 0;
  1354. + grub_memset(P.reserved, 0, sizeof(P.reserved));
  1355. + grub_memset(P.salt, 0, sizeof(P.salt));
  1356. + grub_memset(P.personal, 0, sizeof(P.personal));
  1357. +
  1358. + if (blake2b_init_param(S, &P) < 0) {
  1359. + blake2b_invalidate_state(S);
  1360. + return -1;
  1361. + }
  1362. +
  1363. + {
  1364. + grub_uint8_t block[BLAKE2B_BLOCKBYTES];
  1365. + grub_memset(block, 0, BLAKE2B_BLOCKBYTES);
  1366. + grub_memcpy(block, key, keylen);
  1367. + blake2b_update(S, block, BLAKE2B_BLOCKBYTES);
  1368. + /* Burn the key from stack */
  1369. + clear_internal_memory(block, BLAKE2B_BLOCKBYTES);
  1370. + }
  1371. + return 0;
  1372. +}
  1373. +
  1374. +static void blake2b_compress(blake2b_state *S, const grub_uint8_t *block) {
  1375. + grub_uint64_t m[16];
  1376. + grub_uint64_t v[16];
  1377. + unsigned int i, r;
  1378. +
  1379. + for (i = 0; i < 16; ++i) {
  1380. + m[i] = load64(block + i * sizeof(m[i]));
  1381. + }
  1382. +
  1383. + for (i = 0; i < 8; ++i) {
  1384. + v[i] = S->h[i];
  1385. + }
  1386. +
  1387. + v[8] = blake2b_IV[0];
  1388. + v[9] = blake2b_IV[1];
  1389. + v[10] = blake2b_IV[2];
  1390. + v[11] = blake2b_IV[3];
  1391. + v[12] = blake2b_IV[4] ^ S->t[0];
  1392. + v[13] = blake2b_IV[5] ^ S->t[1];
  1393. + v[14] = blake2b_IV[6] ^ S->f[0];
  1394. + v[15] = blake2b_IV[7] ^ S->f[1];
  1395. +
  1396. +#define G(r, i, a, b, c, d) \
  1397. + do { \
  1398. + a = a + b + m[blake2b_sigma[r][2 * i + 0]]; \
  1399. + d = rotr64(d ^ a, 32); \
  1400. + c = c + d; \
  1401. + b = rotr64(b ^ c, 24); \
  1402. + a = a + b + m[blake2b_sigma[r][2 * i + 1]]; \
  1403. + d = rotr64(d ^ a, 16); \
  1404. + c = c + d; \
  1405. + b = rotr64(b ^ c, 63); \
  1406. + } while ((void)0, 0)
  1407. +
  1408. +#define ROUND(r) \
  1409. + do { \
  1410. + G(r, 0, v[0], v[4], v[8], v[12]); \
  1411. + G(r, 1, v[1], v[5], v[9], v[13]); \
  1412. + G(r, 2, v[2], v[6], v[10], v[14]); \
  1413. + G(r, 3, v[3], v[7], v[11], v[15]); \
  1414. + G(r, 4, v[0], v[5], v[10], v[15]); \
  1415. + G(r, 5, v[1], v[6], v[11], v[12]); \
  1416. + G(r, 6, v[2], v[7], v[8], v[13]); \
  1417. + G(r, 7, v[3], v[4], v[9], v[14]); \
  1418. + } while ((void)0, 0)
  1419. +
  1420. + for (r = 0; r < 12; ++r) {
  1421. + ROUND(r);
  1422. + }
  1423. +
  1424. + for (i = 0; i < 8; ++i) {
  1425. + S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
  1426. + }
  1427. +
  1428. +#undef G
  1429. +#undef ROUND
  1430. +}
  1431. +
  1432. +int blake2b_update(blake2b_state *S, const void *in, grub_size_t inlen) {
  1433. + const grub_uint8_t *pin = (const grub_uint8_t *)in;
  1434. +
  1435. + if (inlen == 0) {
  1436. + return 0;
  1437. + }
  1438. +
  1439. + /* Sanity check */
  1440. + if (S == NULL || in == NULL) {
  1441. + return -1;
  1442. + }
  1443. +
  1444. + /* Is this a reused state? */
  1445. + if (S->f[0] != 0) {
  1446. + return -1;
  1447. + }
  1448. +
  1449. + if (S->buflen + inlen > BLAKE2B_BLOCKBYTES) {
  1450. + /* Complete current block */
  1451. + grub_size_t left = S->buflen;
  1452. + grub_size_t fill = BLAKE2B_BLOCKBYTES - left;
  1453. + grub_memcpy(&S->buf[left], pin, fill);
  1454. + blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
  1455. + blake2b_compress(S, S->buf);
  1456. + S->buflen = 0;
  1457. + inlen -= fill;
  1458. + pin += fill;
  1459. + /* Avoid buffer copies when possible */
  1460. + while (inlen > BLAKE2B_BLOCKBYTES) {
  1461. + blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
  1462. + blake2b_compress(S, pin);
  1463. + inlen -= BLAKE2B_BLOCKBYTES;
  1464. + pin += BLAKE2B_BLOCKBYTES;
  1465. + }
  1466. + }
  1467. + grub_memcpy(&S->buf[S->buflen], pin, inlen);
  1468. + S->buflen += (unsigned int)inlen;
  1469. + return 0;
  1470. +}
  1471. +
  1472. +int blake2b_final(blake2b_state *S, void *out, grub_size_t outlen) {
  1473. + grub_uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
  1474. + unsigned int i;
  1475. +
  1476. + /* Sanity checks */
  1477. + if (S == NULL || out == NULL || outlen < S->outlen) {
  1478. + return -1;
  1479. + }
  1480. +
  1481. + /* Is this a reused state? */
  1482. + if (S->f[0] != 0) {
  1483. + return -1;
  1484. + }
  1485. +
  1486. + blake2b_increment_counter(S, S->buflen);
  1487. + blake2b_set_lastblock(S);
  1488. + grub_memset(&S->buf[S->buflen], 0, BLAKE2B_BLOCKBYTES - S->buflen); /* Padding */
  1489. + blake2b_compress(S, S->buf);
  1490. +
  1491. + for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */
  1492. + store64(buffer + sizeof(S->h[i]) * i, S->h[i]);
  1493. + }
  1494. +
  1495. + grub_memcpy(out, buffer, S->outlen);
  1496. + clear_internal_memory(buffer, sizeof(buffer));
  1497. + clear_internal_memory(S->buf, sizeof(S->buf));
  1498. + clear_internal_memory(S->h, sizeof(S->h));
  1499. + return 0;
  1500. +}
  1501. +
  1502. +int blake2b(void *out, grub_size_t outlen, const void *in, grub_size_t inlen,
  1503. + const void *key, grub_size_t keylen) {
  1504. + blake2b_state S;
  1505. + int ret = -1;
  1506. +
  1507. + /* Verify parameters */
  1508. + if (NULL == in && inlen > 0) {
  1509. + goto fail;
  1510. + }
  1511. +
  1512. + if (NULL == out || outlen == 0 || outlen > BLAKE2B_OUTBYTES) {
  1513. + goto fail;
  1514. + }
  1515. +
  1516. + if ((NULL == key && keylen > 0) || keylen > BLAKE2B_KEYBYTES) {
  1517. + goto fail;
  1518. + }
  1519. +
  1520. + if (keylen > 0) {
  1521. + if (blake2b_init_key(&S, outlen, key, keylen) < 0) {
  1522. + goto fail;
  1523. + }
  1524. + } else {
  1525. + if (blake2b_init(&S, outlen) < 0) {
  1526. + goto fail;
  1527. + }
  1528. + }
  1529. +
  1530. + if (blake2b_update(&S, in, inlen) < 0) {
  1531. + goto fail;
  1532. + }
  1533. + ret = blake2b_final(&S, out, outlen);
  1534. +
  1535. +fail:
  1536. + clear_internal_memory(&S, sizeof(S));
  1537. + return ret;
  1538. +}
  1539. +
  1540. +/* Argon2 Team - Begin Code */
  1541. +int blake2b_long(void *pout, grub_size_t outlen, const void *in, grub_size_t inlen) {
  1542. + grub_uint8_t *out = (grub_uint8_t *)pout;
  1543. + blake2b_state blake_state;
  1544. + grub_uint8_t outlen_bytes[sizeof(grub_uint32_t)] = {0};
  1545. + int ret = -1;
  1546. +
  1547. + if (outlen > GRUB_UINT32_MAX) {
  1548. + goto fail;
  1549. + }
  1550. +
  1551. + /* Ensure little-endian byte order! */
  1552. + store32(outlen_bytes, (grub_uint32_t)outlen);
  1553. +
  1554. +#define TRY(statement) \
  1555. + do { \
  1556. + ret = statement; \
  1557. + if (ret < 0) { \
  1558. + goto fail; \
  1559. + } \
  1560. + } while ((void)0, 0)
  1561. +
  1562. + if (outlen <= BLAKE2B_OUTBYTES) {
  1563. + TRY(blake2b_init(&blake_state, outlen));
  1564. + TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)));
  1565. + TRY(blake2b_update(&blake_state, in, inlen));
  1566. + TRY(blake2b_final(&blake_state, out, outlen));
  1567. + } else {
  1568. + grub_uint32_t toproduce;
  1569. + grub_uint8_t out_buffer[BLAKE2B_OUTBYTES];
  1570. + grub_uint8_t in_buffer[BLAKE2B_OUTBYTES];
  1571. + TRY(blake2b_init(&blake_state, BLAKE2B_OUTBYTES));
  1572. + TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)));
  1573. + TRY(blake2b_update(&blake_state, in, inlen));
  1574. + TRY(blake2b_final(&blake_state, out_buffer, BLAKE2B_OUTBYTES));
  1575. + grub_memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2);
  1576. + out += BLAKE2B_OUTBYTES / 2;
  1577. + toproduce = (grub_uint32_t)outlen - BLAKE2B_OUTBYTES / 2;
  1578. +
  1579. + while (toproduce > BLAKE2B_OUTBYTES) {
  1580. + grub_memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES);
  1581. + TRY(blake2b(out_buffer, BLAKE2B_OUTBYTES, in_buffer,
  1582. + BLAKE2B_OUTBYTES, NULL, 0));
  1583. + grub_memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2);
  1584. + out += BLAKE2B_OUTBYTES / 2;
  1585. + toproduce -= BLAKE2B_OUTBYTES / 2;
  1586. + }
  1587. +
  1588. + grub_memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES);
  1589. + TRY(blake2b(out_buffer, toproduce, in_buffer, BLAKE2B_OUTBYTES, NULL,
  1590. + 0));
  1591. + grub_memcpy(out, out_buffer, toproduce);
  1592. + }
  1593. +fail:
  1594. + clear_internal_memory(&blake_state, sizeof(blake_state));
  1595. + return ret;
  1596. +#undef TRY
  1597. +}
  1598. +/* Argon2 Team - End Code */
  1599. diff --git a/grub-core/lib/argon2/blake2/blamka-round-ref.h b/grub-core/lib/argon2/blake2/blamka-round-ref.h
  1600. new file mode 100644
  1601. index 000000000..7f0071ada
  1602. --- /dev/null
  1603. +++ b/grub-core/lib/argon2/blake2/blamka-round-ref.h
  1604. @@ -0,0 +1,56 @@
  1605. +/*
  1606. + * Argon2 reference source code package - reference C implementations
  1607. + *
  1608. + * Copyright 2015
  1609. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  1610. + *
  1611. + * You may use this work under the terms of a Creative Commons CC0 1.0
  1612. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  1613. + * these licenses can be found at:
  1614. + *
  1615. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  1616. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  1617. + *
  1618. + * You should have received a copy of both of these licenses along with this
  1619. + * software. If not, they may be obtained at the above URLs.
  1620. + */
  1621. +
  1622. +#ifndef BLAKE_ROUND_MKA_H
  1623. +#define BLAKE_ROUND_MKA_H
  1624. +
  1625. +#include "blake2.h"
  1626. +#include "blake2-impl.h"
  1627. +
  1628. +/* designed by the Lyra PHC team */
  1629. +static BLAKE2_INLINE grub_uint64_t fBlaMka(grub_uint64_t x, grub_uint64_t y) {
  1630. + const grub_uint64_t m = GRUB_UINT64_C(0xFFFFFFFF);
  1631. + const grub_uint64_t xy = (x & m) * (y & m);
  1632. + return x + y + 2 * xy;
  1633. +}
  1634. +
  1635. +#define G(a, b, c, d) \
  1636. + do { \
  1637. + a = fBlaMka(a, b); \
  1638. + d = rotr64(d ^ a, 32); \
  1639. + c = fBlaMka(c, d); \
  1640. + b = rotr64(b ^ c, 24); \
  1641. + a = fBlaMka(a, b); \
  1642. + d = rotr64(d ^ a, 16); \
  1643. + c = fBlaMka(c, d); \
  1644. + b = rotr64(b ^ c, 63); \
  1645. + } while ((void)0, 0)
  1646. +
  1647. +#define BLAKE2_ROUND_NOMSG(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, \
  1648. + v12, v13, v14, v15) \
  1649. + do { \
  1650. + G(v0, v4, v8, v12); \
  1651. + G(v1, v5, v9, v13); \
  1652. + G(v2, v6, v10, v14); \
  1653. + G(v3, v7, v11, v15); \
  1654. + G(v0, v5, v10, v15); \
  1655. + G(v1, v6, v11, v12); \
  1656. + G(v2, v7, v8, v13); \
  1657. + G(v3, v4, v9, v14); \
  1658. + } while ((void)0, 0)
  1659. +
  1660. +#endif
  1661. diff --git a/grub-core/lib/argon2/core.c b/grub-core/lib/argon2/core.c
  1662. new file mode 100644
  1663. index 000000000..0fe5b74cb
  1664. --- /dev/null
  1665. +++ b/grub-core/lib/argon2/core.c
  1666. @@ -0,0 +1,506 @@
  1667. +/*
  1668. + * Argon2 reference source code package - reference C implementations
  1669. + *
  1670. + * Copyright 2015
  1671. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  1672. + *
  1673. + * You may use this work under the terms of a Creative Commons CC0 1.0
  1674. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  1675. + * these licenses can be found at:
  1676. + *
  1677. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  1678. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  1679. + *
  1680. + * You should have received a copy of both of these licenses along with this
  1681. + * software. If not, they may be obtained at the above URLs.
  1682. + */
  1683. +
  1684. +/*For memory wiping*/
  1685. +#ifdef _MSC_VER
  1686. +#include <windows.h>
  1687. +#include <winbase.h> /* For SecureZeroMemory */
  1688. +#endif
  1689. +#if defined __STDC_LIB_EXT1__
  1690. +#define __STDC_WANT_LIB_EXT1__ 1
  1691. +#endif
  1692. +#define VC_GE_2005(version) (version >= 1400)
  1693. +
  1694. +#include "core.h"
  1695. +#include "blake2/blake2.h"
  1696. +#include "blake2/blake2-impl.h"
  1697. +
  1698. +#ifdef GENKAT
  1699. +#include "genkat.h"
  1700. +#endif
  1701. +
  1702. +#if defined(__clang__)
  1703. +#if __has_attribute(optnone)
  1704. +#define NOT_OPTIMIZED __attribute__((optnone))
  1705. +#endif
  1706. +#elif defined(__GNUC__)
  1707. +#define GCC_VERSION \
  1708. + (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  1709. +#if GCC_VERSION >= 40400
  1710. +#define NOT_OPTIMIZED __attribute__((optimize("O0")))
  1711. +#endif
  1712. +#endif
  1713. +#ifndef NOT_OPTIMIZED
  1714. +#define NOT_OPTIMIZED
  1715. +#endif
  1716. +
  1717. +/***************Instance and Position constructors**********/
  1718. +void init_block_value(block *b, grub_uint8_t in) { grub_memset(b->v, in, sizeof(b->v)); }
  1719. +
  1720. +void copy_block(block *dst, const block *src) {
  1721. + grub_memcpy(dst->v, src->v, sizeof(grub_uint64_t) * ARGON2_QWORDS_IN_BLOCK);
  1722. +}
  1723. +
  1724. +void xor_block(block *dst, const block *src) {
  1725. + int i;
  1726. + for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
  1727. + dst->v[i] ^= src->v[i];
  1728. + }
  1729. +}
  1730. +
  1731. +static void load_block(block *dst, const void *input) {
  1732. + unsigned i;
  1733. + for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
  1734. + dst->v[i] = load64((const grub_uint8_t *)input + i * sizeof(dst->v[i]));
  1735. + }
  1736. +}
  1737. +
  1738. +static void store_block(void *output, const block *src) {
  1739. + unsigned i;
  1740. + for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
  1741. + store64((grub_uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
  1742. + }
  1743. +}
  1744. +
  1745. +/***************Memory functions*****************/
  1746. +
  1747. +int allocate_memory(const argon2_context *context, grub_uint8_t **memory,
  1748. + grub_size_t num, grub_size_t size) {
  1749. + grub_size_t memory_size = num*size;
  1750. + if (memory == NULL) {
  1751. + return ARGON2_MEMORY_ALLOCATION_ERROR;
  1752. + }
  1753. +
  1754. + /* 1. Check for multiplication overflow */
  1755. + if (size != 0 && memory_size / size != num) {
  1756. + return ARGON2_MEMORY_ALLOCATION_ERROR;
  1757. + }
  1758. +
  1759. + /* 2. Try to allocate with appropriate allocator */
  1760. + if (context->allocate_cbk) {
  1761. + (context->allocate_cbk)(memory, memory_size);
  1762. + } else {
  1763. + *memory = grub_malloc(memory_size);
  1764. + }
  1765. +
  1766. + if (*memory == NULL) {
  1767. + return ARGON2_MEMORY_ALLOCATION_ERROR;
  1768. + }
  1769. +
  1770. + return ARGON2_OK;
  1771. +}
  1772. +
  1773. +void grub_free_memory(const argon2_context *context, grub_uint8_t *memory,
  1774. + grub_size_t num, grub_size_t size) {
  1775. + grub_size_t memory_size = num*size;
  1776. + clear_internal_memory(memory, memory_size);
  1777. + if (context->grub_free_cbk) {
  1778. + (context->grub_free_cbk)(memory, memory_size);
  1779. + } else {
  1780. + grub_free(memory);
  1781. + }
  1782. +}
  1783. +
  1784. +void NOT_OPTIMIZED secure_wipe_memory(void *v, grub_size_t n) {
  1785. + static void *(*const volatile grub_memset_sec)(void *, int, grub_size_t) = &grub_memset;
  1786. + grub_memset_sec(v, 0, n);
  1787. +}
  1788. +
  1789. +/* Memory clear flag defaults to true. */
  1790. +int FLAG_clear_internal_memory = 1;
  1791. +void clear_internal_memory(void *v, grub_size_t n) {
  1792. + if (FLAG_clear_internal_memory && v) {
  1793. + secure_wipe_memory(v, n);
  1794. + }
  1795. +}
  1796. +
  1797. +void finalize(const argon2_context *context, argon2_instance_t *instance) {
  1798. + if (context != NULL && instance != NULL) {
  1799. + block blockhash;
  1800. + grub_uint32_t l;
  1801. +
  1802. + copy_block(&blockhash, instance->memory + instance->lane_length - 1);
  1803. +
  1804. + /* XOR the last blocks */
  1805. + for (l = 1; l < instance->lanes; ++l) {
  1806. + grub_uint32_t last_block_in_lane =
  1807. + l * instance->lane_length + (instance->lane_length - 1);
  1808. + xor_block(&blockhash, instance->memory + last_block_in_lane);
  1809. + }
  1810. +
  1811. + /* Hash the result */
  1812. + {
  1813. + grub_uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
  1814. + store_block(blockhash_bytes, &blockhash);
  1815. + blake2b_long(context->out, context->outlen, blockhash_bytes,
  1816. + ARGON2_BLOCK_SIZE);
  1817. + /* clear blockhash and blockhash_bytes */
  1818. + clear_internal_memory(blockhash.v, ARGON2_BLOCK_SIZE);
  1819. + clear_internal_memory(blockhash_bytes, ARGON2_BLOCK_SIZE);
  1820. + }
  1821. +
  1822. +#ifdef GENKAT
  1823. + print_tag(context->out, context->outlen);
  1824. +#endif
  1825. +
  1826. + grub_free_memory(context, (grub_uint8_t *)instance->memory,
  1827. + instance->memory_blocks, sizeof(block));
  1828. + }
  1829. +}
  1830. +
  1831. +grub_uint32_t index_alpha(const argon2_instance_t *instance,
  1832. + const argon2_position_t *position, grub_uint32_t pseudo_rand,
  1833. + int same_lane) {
  1834. + /*
  1835. + * Pass 0:
  1836. + * This lane : all already finished segments plus already constructed
  1837. + * blocks in this segment
  1838. + * Other lanes : all already finished segments
  1839. + * Pass 1+:
  1840. + * This lane : (SYNC_POINTS - 1) last segments plus already constructed
  1841. + * blocks in this segment
  1842. + * Other lanes : (SYNC_POINTS - 1) last segments
  1843. + */
  1844. + grub_uint32_t reference_area_size;
  1845. + grub_uint64_t relative_position;
  1846. + grub_uint64_t start_position, absolute_position;
  1847. +
  1848. + if (0 == position->pass) {
  1849. + /* First pass */
  1850. + if (0 == position->slice) {
  1851. + /* First slice */
  1852. + reference_area_size =
  1853. + position->index - 1; /* all but the previous */
  1854. + } else {
  1855. + if (same_lane) {
  1856. + /* The same lane => add current segment */
  1857. + reference_area_size =
  1858. + position->slice * instance->segment_length +
  1859. + position->index - 1;
  1860. + } else {
  1861. + reference_area_size =
  1862. + position->slice * instance->segment_length +
  1863. + ((position->index == 0) ? (-1) : 0);
  1864. + }
  1865. + }
  1866. + } else {
  1867. + /* Second pass */
  1868. + if (same_lane) {
  1869. + reference_area_size = instance->lane_length -
  1870. + instance->segment_length + position->index -
  1871. + 1;
  1872. + } else {
  1873. + reference_area_size = instance->lane_length -
  1874. + instance->segment_length +
  1875. + ((position->index == 0) ? (-1) : 0);
  1876. + }
  1877. + }
  1878. +
  1879. + /* 1.2.4. Mapping pseudo_rand to 0..<reference_area_size-1> and produce
  1880. + * relative position */
  1881. + relative_position = pseudo_rand;
  1882. + relative_position = relative_position * relative_position >> 32;
  1883. + relative_position = reference_area_size - 1 -
  1884. + (reference_area_size * relative_position >> 32);
  1885. +
  1886. + /* 1.2.5 Computing starting position */
  1887. + start_position = 0;
  1888. +
  1889. + if (0 != position->pass) {
  1890. + start_position = (position->slice == ARGON2_SYNC_POINTS - 1)
  1891. + ? 0
  1892. + : (position->slice + 1) * instance->segment_length;
  1893. + }
  1894. +
  1895. + /* 1.2.6. Computing absolute position */
  1896. + grub_divmod64 (start_position + relative_position, instance->lane_length,
  1897. + &absolute_position); /* absolute position */
  1898. + return absolute_position;
  1899. +}
  1900. +
  1901. +/* Single-threaded version for p=1 case */
  1902. +static int fill_memory_blocks_st(argon2_instance_t *instance) {
  1903. + grub_uint32_t r, s, l;
  1904. +
  1905. + for (r = 0; r < instance->passes; ++r) {
  1906. + for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
  1907. + for (l = 0; l < instance->lanes; ++l) {
  1908. + argon2_position_t position = {r, l, (grub_uint8_t)s, 0};
  1909. + fill_segment(instance, position);
  1910. + }
  1911. + }
  1912. +#ifdef GENKAT
  1913. + internal_kat(instance, r); /* Print all memory blocks */
  1914. +#endif
  1915. + }
  1916. + return ARGON2_OK;
  1917. +}
  1918. +
  1919. +int fill_memory_blocks(argon2_instance_t *instance) {
  1920. + if (instance == NULL || instance->lanes == 0) {
  1921. + return ARGON2_INCORRECT_PARAMETER;
  1922. + }
  1923. + return fill_memory_blocks_st(instance);
  1924. +}
  1925. +
  1926. +int validate_inputs(const argon2_context *context) {
  1927. + if (NULL == context) {
  1928. + return ARGON2_INCORRECT_PARAMETER;
  1929. + }
  1930. +
  1931. + if (NULL == context->out) {
  1932. + return ARGON2_OUTPUT_PTR_NULL;
  1933. + }
  1934. +
  1935. + /* Validate output length */
  1936. + if (ARGON2_MIN_OUTLEN > context->outlen) {
  1937. + return ARGON2_OUTPUT_TOO_SHORT;
  1938. + }
  1939. +
  1940. + if (ARGON2_MAX_OUTLEN < context->outlen) {
  1941. + return ARGON2_OUTPUT_TOO_LONG;
  1942. + }
  1943. +
  1944. + /* Validate password (required param) */
  1945. + if (NULL == context->pwd) {
  1946. + if (0 != context->pwdlen) {
  1947. + return ARGON2_PWD_PTR_MISMATCH;
  1948. + }
  1949. + }
  1950. +
  1951. + if (ARGON2_MAX_PWD_LENGTH < context->pwdlen) {
  1952. + return ARGON2_PWD_TOO_LONG;
  1953. + }
  1954. +
  1955. + /* Validate salt (required param) */
  1956. + if (NULL == context->salt) {
  1957. + if (0 != context->saltlen) {
  1958. + return ARGON2_SALT_PTR_MISMATCH;
  1959. + }
  1960. + }
  1961. +
  1962. + if (ARGON2_MIN_SALT_LENGTH > context->saltlen) {
  1963. + return ARGON2_SALT_TOO_SHORT;
  1964. + }
  1965. +
  1966. + if (ARGON2_MAX_SALT_LENGTH < context->saltlen) {
  1967. + return ARGON2_SALT_TOO_LONG;
  1968. + }
  1969. +
  1970. + /* Validate secret (optional param) */
  1971. + if (NULL == context->secret) {
  1972. + if (0 != context->secretlen) {
  1973. + return ARGON2_SECRET_PTR_MISMATCH;
  1974. + }
  1975. + } else {
  1976. + if (ARGON2_MAX_SECRET < context->secretlen) {
  1977. + return ARGON2_SECRET_TOO_LONG;
  1978. + }
  1979. + }
  1980. +
  1981. + /* Validate associated data (optional param) */
  1982. + if (NULL == context->ad) {
  1983. + if (0 != context->adlen) {
  1984. + return ARGON2_AD_PTR_MISMATCH;
  1985. + }
  1986. + } else {
  1987. + if (ARGON2_MAX_AD_LENGTH < context->adlen) {
  1988. + return ARGON2_AD_TOO_LONG;
  1989. + }
  1990. + }
  1991. +
  1992. + /* Validate memory cost */
  1993. + if (ARGON2_MIN_MEMORY > context->m_cost) {
  1994. + return ARGON2_MEMORY_TOO_LITTLE;
  1995. + }
  1996. +
  1997. + if (context->m_cost < 8 * context->lanes) {
  1998. + return ARGON2_MEMORY_TOO_LITTLE;
  1999. + }
  2000. +
  2001. + /* Validate time cost */
  2002. + if (ARGON2_MIN_TIME > context->t_cost) {
  2003. + return ARGON2_TIME_TOO_SMALL;
  2004. + }
  2005. +
  2006. + if (ARGON2_MAX_TIME < context->t_cost) {
  2007. + return ARGON2_TIME_TOO_LARGE;
  2008. + }
  2009. +
  2010. + /* Validate lanes */
  2011. + if (ARGON2_MIN_LANES > context->lanes) {
  2012. + return ARGON2_LANES_TOO_FEW;
  2013. + }
  2014. +
  2015. + if (ARGON2_MAX_LANES < context->lanes) {
  2016. + return ARGON2_LANES_TOO_MANY;
  2017. + }
  2018. +
  2019. + /* Validate threads */
  2020. + if (ARGON2_MIN_THREADS > context->threads) {
  2021. + return ARGON2_THREADS_TOO_FEW;
  2022. + }
  2023. +
  2024. + if (ARGON2_MAX_THREADS < context->threads) {
  2025. + return ARGON2_THREADS_TOO_MANY;
  2026. + }
  2027. +
  2028. + if (NULL != context->allocate_cbk && NULL == context->grub_free_cbk) {
  2029. + return ARGON2_FREE_MEMORY_CBK_NULL;
  2030. + }
  2031. +
  2032. + if (NULL == context->allocate_cbk && NULL != context->grub_free_cbk) {
  2033. + return ARGON2_ALLOCATE_MEMORY_CBK_NULL;
  2034. + }
  2035. +
  2036. + return ARGON2_OK;
  2037. +}
  2038. +
  2039. +void fill_first_blocks(grub_uint8_t *blockhash, const argon2_instance_t *instance) {
  2040. + grub_uint32_t l;
  2041. + /* Make the first and second block in each lane as G(H0||0||i) or
  2042. + G(H0||1||i) */
  2043. + grub_uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
  2044. + for (l = 0; l < instance->lanes; ++l) {
  2045. +
  2046. + store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
  2047. + store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
  2048. + blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
  2049. + ARGON2_PREHASH_SEED_LENGTH);
  2050. + load_block(&instance->memory[l * instance->lane_length + 0],
  2051. + blockhash_bytes);
  2052. +
  2053. + store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
  2054. + blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
  2055. + ARGON2_PREHASH_SEED_LENGTH);
  2056. + load_block(&instance->memory[l * instance->lane_length + 1],
  2057. + blockhash_bytes);
  2058. + }
  2059. + clear_internal_memory(blockhash_bytes, ARGON2_BLOCK_SIZE);
  2060. +}
  2061. +
  2062. +void initial_hash(grub_uint8_t *blockhash, argon2_context *context,
  2063. + argon2_type type) {
  2064. + blake2b_state BlakeHash;
  2065. + grub_uint8_t value[sizeof(grub_uint32_t)];
  2066. +
  2067. + if (NULL == context || NULL == blockhash) {
  2068. + return;
  2069. + }
  2070. +
  2071. + blake2b_init(&BlakeHash, ARGON2_PREHASH_DIGEST_LENGTH);
  2072. +
  2073. + store32(&value, context->lanes);
  2074. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2075. +
  2076. + store32(&value, context->outlen);
  2077. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2078. +
  2079. + store32(&value, context->m_cost);
  2080. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2081. +
  2082. + store32(&value, context->t_cost);
  2083. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2084. +
  2085. + store32(&value, context->version);
  2086. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2087. +
  2088. + store32(&value, (grub_uint32_t)type);
  2089. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2090. +
  2091. + store32(&value, context->pwdlen);
  2092. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2093. +
  2094. + if (context->pwd != NULL) {
  2095. + blake2b_update(&BlakeHash, (const grub_uint8_t *)context->pwd,
  2096. + context->pwdlen);
  2097. +
  2098. + if (context->flags & ARGON2_FLAG_CLEAR_PASSWORD) {
  2099. + secure_wipe_memory(context->pwd, context->pwdlen);
  2100. + context->pwdlen = 0;
  2101. + }
  2102. + }
  2103. +
  2104. + store32(&value, context->saltlen);
  2105. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2106. +
  2107. + if (context->salt != NULL) {
  2108. + blake2b_update(&BlakeHash, (const grub_uint8_t *)context->salt,
  2109. + context->saltlen);
  2110. + }
  2111. +
  2112. + store32(&value, context->secretlen);
  2113. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2114. +
  2115. + if (context->secret != NULL) {
  2116. + blake2b_update(&BlakeHash, (const grub_uint8_t *)context->secret,
  2117. + context->secretlen);
  2118. +
  2119. + if (context->flags & ARGON2_FLAG_CLEAR_SECRET) {
  2120. + secure_wipe_memory(context->secret, context->secretlen);
  2121. + context->secretlen = 0;
  2122. + }
  2123. + }
  2124. +
  2125. + store32(&value, context->adlen);
  2126. + blake2b_update(&BlakeHash, (const grub_uint8_t *)&value, sizeof(value));
  2127. +
  2128. + if (context->ad != NULL) {
  2129. + blake2b_update(&BlakeHash, (const grub_uint8_t *)context->ad,
  2130. + context->adlen);
  2131. + }
  2132. +
  2133. + blake2b_final(&BlakeHash, blockhash, ARGON2_PREHASH_DIGEST_LENGTH);
  2134. +}
  2135. +
  2136. +int initialize(argon2_instance_t *instance, argon2_context *context) {
  2137. + grub_uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
  2138. + int result = ARGON2_OK;
  2139. +
  2140. + if (instance == NULL || context == NULL)
  2141. + return ARGON2_INCORRECT_PARAMETER;
  2142. + instance->context_ptr = context;
  2143. +
  2144. + /* 1. Memory allocation */
  2145. + result = allocate_memory(context, (grub_uint8_t **)&(instance->memory),
  2146. + instance->memory_blocks, sizeof(block));
  2147. + if (result != ARGON2_OK) {
  2148. + return result;
  2149. + }
  2150. +
  2151. + /* 2. Initial hashing */
  2152. + /* H_0 + 8 extra bytes to produce the first blocks */
  2153. + /* grub_uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH]; */
  2154. + /* Hashing all inputs */
  2155. + initial_hash(blockhash, context, instance->type);
  2156. + /* Zeroing 8 extra bytes */
  2157. + clear_internal_memory(blockhash + ARGON2_PREHASH_DIGEST_LENGTH,
  2158. + ARGON2_PREHASH_SEED_LENGTH -
  2159. + ARGON2_PREHASH_DIGEST_LENGTH);
  2160. +
  2161. +#ifdef GENKAT
  2162. + initial_kat(blockhash, context, instance->type);
  2163. +#endif
  2164. +
  2165. + /* 3. Creating first blocks, we always have at least two blocks in a slice
  2166. + */
  2167. + fill_first_blocks(blockhash, instance);
  2168. + /* Clearing the hash */
  2169. + clear_internal_memory(blockhash, ARGON2_PREHASH_SEED_LENGTH);
  2170. +
  2171. + return ARGON2_OK;
  2172. +}
  2173. diff --git a/grub-core/lib/argon2/core.h b/grub-core/lib/argon2/core.h
  2174. new file mode 100644
  2175. index 000000000..bbcd56998
  2176. --- /dev/null
  2177. +++ b/grub-core/lib/argon2/core.h
  2178. @@ -0,0 +1,228 @@
  2179. +/*
  2180. + * Argon2 reference source code package - reference C implementations
  2181. + *
  2182. + * Copyright 2015
  2183. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  2184. + *
  2185. + * You may use this work under the terms of a Creative Commons CC0 1.0
  2186. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  2187. + * these licenses can be found at:
  2188. + *
  2189. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  2190. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  2191. + *
  2192. + * You should have received a copy of both of these licenses along with this
  2193. + * software. If not, they may be obtained at the above URLs.
  2194. + */
  2195. +
  2196. +#ifndef ARGON2_CORE_H
  2197. +#define ARGON2_CORE_H
  2198. +
  2199. +#include "argon2.h"
  2200. +
  2201. +#define CONST_CAST(x) (x)(grub_addr_t)
  2202. +
  2203. +/**********************Argon2 internal constants*******************************/
  2204. +
  2205. +enum argon2_core_constants {
  2206. + /* Memory block size in bytes */
  2207. + ARGON2_BLOCK_SIZE = 1024,
  2208. + ARGON2_QWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 8,
  2209. + ARGON2_OWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 16,
  2210. + ARGON2_HWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 32,
  2211. + ARGON2_512BIT_WORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 64,
  2212. +
  2213. + /* Number of pseudo-random values generated by one call to Blake in Argon2i
  2214. + to
  2215. + generate reference block positions */
  2216. + ARGON2_ADDRESSES_IN_BLOCK = 128,
  2217. +
  2218. + /* Pre-hashing digest length and its extension*/
  2219. + ARGON2_PREHASH_DIGEST_LENGTH = 64,
  2220. + ARGON2_PREHASH_SEED_LENGTH = 72
  2221. +};
  2222. +
  2223. +/*************************Argon2 internal data types***********************/
  2224. +
  2225. +/*
  2226. + * Structure for the (1KB) memory block implemented as 128 64-bit words.
  2227. + * Memory blocks can be copied, XORed. Internal words can be accessed by [] (no
  2228. + * bounds checking).
  2229. + */
  2230. +typedef struct block_ { grub_uint64_t v[ARGON2_QWORDS_IN_BLOCK]; } block;
  2231. +
  2232. +/*****************Functions that work with the block******************/
  2233. +
  2234. +/* Initialize each byte of the block with @in */
  2235. +void init_block_value(block *b, grub_uint8_t in);
  2236. +
  2237. +/* Copy block @src to block @dst */
  2238. +void copy_block(block *dst, const block *src);
  2239. +
  2240. +/* XOR @src onto @dst bytewise */
  2241. +void xor_block(block *dst, const block *src);
  2242. +
  2243. +/*
  2244. + * Argon2 instance: memory pointer, number of passes, amount of memory, type,
  2245. + * and derived values.
  2246. + * Used to evaluate the number and location of blocks to construct in each
  2247. + * thread
  2248. + */
  2249. +typedef struct Argon2_instance_t {
  2250. + block *memory; /* Memory pointer */
  2251. + grub_uint32_t version;
  2252. + grub_uint32_t passes; /* Number of passes */
  2253. + grub_uint32_t memory_blocks; /* Number of blocks in memory */
  2254. + grub_uint32_t segment_length;
  2255. + grub_uint32_t lane_length;
  2256. + grub_uint32_t lanes;
  2257. + grub_uint32_t threads;
  2258. + argon2_type type;
  2259. + int print_internals; /* whether to print the memory blocks */
  2260. + argon2_context *context_ptr; /* points back to original context */
  2261. +} argon2_instance_t;
  2262. +
  2263. +/*
  2264. + * Argon2 position: where we construct the block right now. Used to distribute
  2265. + * work between threads.
  2266. + */
  2267. +typedef struct Argon2_position_t {
  2268. + grub_uint32_t pass;
  2269. + grub_uint32_t lane;
  2270. + grub_uint8_t slice;
  2271. + grub_uint32_t index;
  2272. +} argon2_position_t;
  2273. +
  2274. +/*Struct that holds the inputs for thread handling FillSegment*/
  2275. +typedef struct Argon2_thread_data {
  2276. + argon2_instance_t *instance_ptr;
  2277. + argon2_position_t pos;
  2278. +} argon2_thread_data;
  2279. +
  2280. +/*************************Argon2 core functions********************************/
  2281. +
  2282. +/* Allocates memory to the given pointer, uses the appropriate allocator as
  2283. + * specified in the context. Total allocated memory is num*size.
  2284. + * @param context argon2_context which specifies the allocator
  2285. + * @param memory pointer to the pointer to the memory
  2286. + * @param size the size in bytes for each element to be allocated
  2287. + * @param num the number of elements to be allocated
  2288. + * @return ARGON2_OK if @memory is a valid pointer and memory is allocated
  2289. + */
  2290. +int allocate_memory(const argon2_context *context, grub_uint8_t **memory,
  2291. + grub_size_t num, grub_size_t size);
  2292. +
  2293. +/*
  2294. + * Frees memory at the given pointer, uses the appropriate deallocator as
  2295. + * specified in the context. Also cleans the memory using clear_internal_memory.
  2296. + * @param context argon2_context which specifies the deallocator
  2297. + * @param memory pointer to buffer to be grub_freed
  2298. + * @param size the size in bytes for each element to be deallocated
  2299. + * @param num the number of elements to be deallocated
  2300. + */
  2301. +void grub_free_memory(const argon2_context *context, grub_uint8_t *memory,
  2302. + grub_size_t num, grub_size_t size);
  2303. +
  2304. +/* Function that securely cleans the memory. This ignores any flags set
  2305. + * regarding clearing memory. Usually one just calls clear_internal_memory.
  2306. + * @param mem Pointer to the memory
  2307. + * @param s Memory size in bytes
  2308. + */
  2309. +void secure_wipe_memory(void *v, grub_size_t n);
  2310. +
  2311. +/* Function that securely clears the memory if FLAG_clear_internal_memory is
  2312. + * set. If the flag isn't set, this function does nothing.
  2313. + * @param mem Pointer to the memory
  2314. + * @param s Memory size in bytes
  2315. + */
  2316. +void clear_internal_memory(void *v, grub_size_t n);
  2317. +
  2318. +/*
  2319. + * Computes absolute position of reference block in the lane following a skewed
  2320. + * distribution and using a pseudo-random value as input
  2321. + * @param instance Pointer to the current instance
  2322. + * @param position Pointer to the current position
  2323. + * @param pseudo_rand 32-bit pseudo-random value used to determine the position
  2324. + * @param same_lane Indicates if the block will be taken from the current lane.
  2325. + * If so we can reference the current segment
  2326. + * @pre All pointers must be valid
  2327. + */
  2328. +grub_uint32_t index_alpha(const argon2_instance_t *instance,
  2329. + const argon2_position_t *position, grub_uint32_t pseudo_rand,
  2330. + int same_lane);
  2331. +
  2332. +/*
  2333. + * Function that validates all inputs against predefined restrictions and return
  2334. + * an error code
  2335. + * @param context Pointer to current Argon2 context
  2336. + * @return ARGON2_OK if everything is all right, otherwise one of error codes
  2337. + * (all defined in <argon2.h>
  2338. + */
  2339. +int validate_inputs(const argon2_context *context);
  2340. +
  2341. +/*
  2342. + * Hashes all the inputs into @a blockhash[PREHASH_DIGEST_LENGTH], clears
  2343. + * password and secret if needed
  2344. + * @param context Pointer to the Argon2 internal structure containing memory
  2345. + * pointer, and parameters for time and space requirements.
  2346. + * @param blockhash Buffer for pre-hashing digest
  2347. + * @param type Argon2 type
  2348. + * @pre @a blockhash must have at least @a PREHASH_DIGEST_LENGTH bytes
  2349. + * allocated
  2350. + */
  2351. +void initial_hash(grub_uint8_t *blockhash, argon2_context *context,
  2352. + argon2_type type);
  2353. +
  2354. +/*
  2355. + * Function creates first 2 blocks per lane
  2356. + * @param instance Pointer to the current instance
  2357. + * @param blockhash Pointer to the pre-hashing digest
  2358. + * @pre blockhash must point to @a PREHASH_SEED_LENGTH allocated values
  2359. + */
  2360. +void fill_first_blocks(grub_uint8_t *blockhash, const argon2_instance_t *instance);
  2361. +
  2362. +/*
  2363. + * Function allocates memory, hashes the inputs with Blake, and creates first
  2364. + * two blocks. Returns the pointer to the main memory with 2 blocks per lane
  2365. + * initialized
  2366. + * @param context Pointer to the Argon2 internal structure containing memory
  2367. + * pointer, and parameters for time and space requirements.
  2368. + * @param instance Current Argon2 instance
  2369. + * @return Zero if successful, -1 if memory failed to allocate. @context->state
  2370. + * will be modified if successful.
  2371. + */
  2372. +int initialize(argon2_instance_t *instance, argon2_context *context);
  2373. +
  2374. +/*
  2375. + * XORing the last block of each lane, hashing it, making the tag. Deallocates
  2376. + * the memory.
  2377. + * @param context Pointer to current Argon2 context (use only the out parameters
  2378. + * from it)
  2379. + * @param instance Pointer to current instance of Argon2
  2380. + * @pre instance->state must point to necessary amount of memory
  2381. + * @pre context->out must point to outlen bytes of memory
  2382. + * @pre if context->grub_free_cbk is not NULL, it should point to a function that
  2383. + * deallocates memory
  2384. + */
  2385. +void finalize(const argon2_context *context, argon2_instance_t *instance);
  2386. +
  2387. +/*
  2388. + * Function that fills the segment using previous segments also from other
  2389. + * threads
  2390. + * @param context current context
  2391. + * @param instance Pointer to the current instance
  2392. + * @param position Current position
  2393. + * @pre all block pointers must be valid
  2394. + */
  2395. +void fill_segment(const argon2_instance_t *instance,
  2396. + argon2_position_t position);
  2397. +
  2398. +/*
  2399. + * Function that fills the entire memory t_cost times based on the first two
  2400. + * blocks in each lane
  2401. + * @param instance Pointer to the current instance
  2402. + * @return ARGON2_OK if successful, @context->state
  2403. + */
  2404. +int fill_memory_blocks(argon2_instance_t *instance);
  2405. +
  2406. +#endif
  2407. diff --git a/grub-core/lib/argon2/ref.c b/grub-core/lib/argon2/ref.c
  2408. new file mode 100644
  2409. index 000000000..c933df80d
  2410. --- /dev/null
  2411. +++ b/grub-core/lib/argon2/ref.c
  2412. @@ -0,0 +1,190 @@
  2413. +/*
  2414. + * Argon2 reference source code package - reference C implementations
  2415. + *
  2416. + * Copyright 2015
  2417. + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  2418. + *
  2419. + * You may use this work under the terms of a Creative Commons CC0 1.0
  2420. + * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  2421. + * these licenses can be found at:
  2422. + *
  2423. + * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  2424. + * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  2425. + *
  2426. + * You should have received a copy of both of these licenses along with this
  2427. + * software. If not, they may be obtained at the above URLs.
  2428. + */
  2429. +
  2430. +#include "argon2.h"
  2431. +#include "core.h"
  2432. +
  2433. +#include "blake2/blamka-round-ref.h"
  2434. +#include "blake2/blake2-impl.h"
  2435. +#include "blake2/blake2.h"
  2436. +
  2437. +
  2438. +/*
  2439. + * Function fills a new memory block and optionally XORs the old block over the new one.
  2440. + * @next_block must be initialized.
  2441. + * @param prev_block Pointer to the previous block
  2442. + * @param ref_block Pointer to the reference block
  2443. + * @param next_block Pointer to the block to be constructed
  2444. + * @param with_xor Whether to XOR into the new block (1) or just overwrite (0)
  2445. + * @pre all block pointers must be valid
  2446. + */
  2447. +static void fill_block(const block *prev_block, const block *ref_block,
  2448. + block *next_block, int with_xor) {
  2449. + block blockR, block_tmp;
  2450. + unsigned i;
  2451. +
  2452. + copy_block(&blockR, ref_block);
  2453. + xor_block(&blockR, prev_block);
  2454. + copy_block(&block_tmp, &blockR);
  2455. + /* Now blockR = ref_block + prev_block and block_tmp = ref_block + prev_block */
  2456. + if (with_xor) {
  2457. + /* Saving the next block contents for XOR over: */
  2458. + xor_block(&block_tmp, next_block);
  2459. + /* Now blockR = ref_block + prev_block and
  2460. + block_tmp = ref_block + prev_block + next_block */
  2461. + }
  2462. +
  2463. + /* Apply Blake2 on columns of 64-bit words: (0,1,...,15) , then
  2464. + (16,17,..31)... finally (112,113,...127) */
  2465. + for (i = 0; i < 8; ++i) {
  2466. + BLAKE2_ROUND_NOMSG(
  2467. + blockR.v[16 * i], blockR.v[16 * i + 1], blockR.v[16 * i + 2],
  2468. + blockR.v[16 * i + 3], blockR.v[16 * i + 4], blockR.v[16 * i + 5],
  2469. + blockR.v[16 * i + 6], blockR.v[16 * i + 7], blockR.v[16 * i + 8],
  2470. + blockR.v[16 * i + 9], blockR.v[16 * i + 10], blockR.v[16 * i + 11],
  2471. + blockR.v[16 * i + 12], blockR.v[16 * i + 13], blockR.v[16 * i + 14],
  2472. + blockR.v[16 * i + 15]);
  2473. + }
  2474. +
  2475. + /* Apply Blake2 on rows of 64-bit words: (0,1,16,17,...112,113), then
  2476. + (2,3,18,19,...,114,115).. finally (14,15,30,31,...,126,127) */
  2477. + for (i = 0; i < 8; i++) {
  2478. + BLAKE2_ROUND_NOMSG(
  2479. + blockR.v[2 * i], blockR.v[2 * i + 1], blockR.v[2 * i + 16],
  2480. + blockR.v[2 * i + 17], blockR.v[2 * i + 32], blockR.v[2 * i + 33],
  2481. + blockR.v[2 * i + 48], blockR.v[2 * i + 49], blockR.v[2 * i + 64],
  2482. + blockR.v[2 * i + 65], blockR.v[2 * i + 80], blockR.v[2 * i + 81],
  2483. + blockR.v[2 * i + 96], blockR.v[2 * i + 97], blockR.v[2 * i + 112],
  2484. + blockR.v[2 * i + 113]);
  2485. + }
  2486. +
  2487. + copy_block(next_block, &block_tmp);
  2488. + xor_block(next_block, &blockR);
  2489. +}
  2490. +
  2491. +static void next_addresses(block *address_block, block *input_block,
  2492. + const block *zero_block) {
  2493. + input_block->v[6]++;
  2494. + fill_block(zero_block, input_block, address_block, 0);
  2495. + fill_block(zero_block, address_block, address_block, 0);
  2496. +}
  2497. +
  2498. +void fill_segment(const argon2_instance_t *instance,
  2499. + argon2_position_t position) {
  2500. + block *ref_block = NULL, *curr_block = NULL;
  2501. + block address_block, input_block, zero_block;
  2502. + grub_uint64_t pseudo_rand, ref_index, ref_lane;
  2503. + grub_uint32_t prev_offset, curr_offset;
  2504. + grub_uint32_t starting_index;
  2505. + grub_uint32_t i;
  2506. + int data_independent_addressing;
  2507. +
  2508. + if (instance == NULL) {
  2509. + return;
  2510. + }
  2511. +
  2512. + data_independent_addressing =
  2513. + (instance->type == Argon2_i) ||
  2514. + (instance->type == Argon2_id && (position.pass == 0) &&
  2515. + (position.slice < ARGON2_SYNC_POINTS / 2));
  2516. +
  2517. + if (data_independent_addressing) {
  2518. + init_block_value(&zero_block, 0);
  2519. + init_block_value(&input_block, 0);
  2520. +
  2521. + input_block.v[0] = position.pass;
  2522. + input_block.v[1] = position.lane;
  2523. + input_block.v[2] = position.slice;
  2524. + input_block.v[3] = instance->memory_blocks;
  2525. + input_block.v[4] = instance->passes;
  2526. + input_block.v[5] = instance->type;
  2527. + }
  2528. +
  2529. + starting_index = 0;
  2530. +
  2531. + if ((0 == position.pass) && (0 == position.slice)) {
  2532. + starting_index = 2; /* we have already generated the first two blocks */
  2533. +
  2534. + /* Don't forget to generate the first block of addresses: */
  2535. + if (data_independent_addressing) {
  2536. + next_addresses(&address_block, &input_block, &zero_block);
  2537. + }
  2538. + }
  2539. +
  2540. + /* Offset of the current block */
  2541. + curr_offset = position.lane * instance->lane_length +
  2542. + position.slice * instance->segment_length + starting_index;
  2543. +
  2544. + if (0 == curr_offset % instance->lane_length) {
  2545. + /* Last block in this lane */
  2546. + prev_offset = curr_offset + instance->lane_length - 1;
  2547. + } else {
  2548. + /* Previous block */
  2549. + prev_offset = curr_offset - 1;
  2550. + }
  2551. +
  2552. + for (i = starting_index; i < instance->segment_length;
  2553. + ++i, ++curr_offset, ++prev_offset) {
  2554. + /*1.1 Rotating prev_offset if needed */
  2555. + if (curr_offset % instance->lane_length == 1) {
  2556. + prev_offset = curr_offset - 1;
  2557. + }
  2558. +
  2559. + /* 1.2 Computing the index of the reference block */
  2560. + /* 1.2.1 Taking pseudo-random value from the previous block */
  2561. + if (data_independent_addressing) {
  2562. + if (i % ARGON2_ADDRESSES_IN_BLOCK == 0) {
  2563. + next_addresses(&address_block, &input_block, &zero_block);
  2564. + }
  2565. + pseudo_rand = address_block.v[i % ARGON2_ADDRESSES_IN_BLOCK];
  2566. + } else {
  2567. + pseudo_rand = instance->memory[prev_offset].v[0];
  2568. + }
  2569. +
  2570. + /* 1.2.2 Computing the lane of the reference block */
  2571. + grub_divmod64 (pseudo_rand >> 32, instance->lanes, &ref_lane);
  2572. +
  2573. + if ((position.pass == 0) && (position.slice == 0)) {
  2574. + /* Can not reference other lanes yet */
  2575. + ref_lane = position.lane;
  2576. + }
  2577. +
  2578. + /* 1.2.3 Computing the number of possible reference block within the
  2579. + * lane.
  2580. + */
  2581. + position.index = i;
  2582. + ref_index = index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
  2583. + ref_lane == position.lane);
  2584. +
  2585. + /* 2 Creating a new block */
  2586. + ref_block =
  2587. + instance->memory + instance->lane_length * ref_lane + ref_index;
  2588. + curr_block = instance->memory + curr_offset;
  2589. + if (ARGON2_VERSION_10 == instance->version) {
  2590. + /* version 1.2.1 and earlier: overwrite, not XOR */
  2591. + fill_block(instance->memory + prev_offset, ref_block, curr_block, 0);
  2592. + } else {
  2593. + if(0 == position.pass) {
  2594. + fill_block(instance->memory + prev_offset, ref_block,
  2595. + curr_block, 0);
  2596. + } else {
  2597. + fill_block(instance->memory + prev_offset, ref_block,
  2598. + curr_block, 1);
  2599. + }
  2600. + }
  2601. + }
  2602. +}
  2603. --
  2604. 2.39.2