0005-Add-Argon2-algorithm.patch 96 KB

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