BaseCryptLib.h 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870
  1. /** @file
  2. Defines base cryptographic library APIs.
  3. The Base Cryptographic Library provides implementations of basic cryptography
  4. primitives (Hash Serials, HMAC, RSA, Diffie-Hellman, etc) for UEFI security
  5. functionality enabling.
  6. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #ifndef __BASE_CRYPT_LIB_H__
  10. #define __BASE_CRYPT_LIB_H__
  11. #include <Uefi/UefiBaseType.h>
  12. ///
  13. /// MD4 digest size in bytes
  14. ///
  15. #define MD4_DIGEST_SIZE 16
  16. ///
  17. /// MD5 digest size in bytes
  18. ///
  19. #define MD5_DIGEST_SIZE 16
  20. ///
  21. /// SHA-1 digest size in bytes.
  22. ///
  23. #define SHA1_DIGEST_SIZE 20
  24. ///
  25. /// SHA-256 digest size in bytes
  26. ///
  27. #define SHA256_DIGEST_SIZE 32
  28. ///
  29. /// SHA-384 digest size in bytes
  30. ///
  31. #define SHA384_DIGEST_SIZE 48
  32. ///
  33. /// SHA-512 digest size in bytes
  34. ///
  35. #define SHA512_DIGEST_SIZE 64
  36. ///
  37. /// TDES block size in bytes
  38. ///
  39. #define TDES_BLOCK_SIZE 8
  40. ///
  41. /// AES block size in bytes
  42. ///
  43. #define AES_BLOCK_SIZE 16
  44. ///
  45. /// RSA Key Tags Definition used in RsaSetKey() function for key component identification.
  46. ///
  47. typedef enum {
  48. RsaKeyN, ///< RSA public Modulus (N)
  49. RsaKeyE, ///< RSA Public exponent (e)
  50. RsaKeyD, ///< RSA Private exponent (d)
  51. RsaKeyP, ///< RSA secret prime factor of Modulus (p)
  52. RsaKeyQ, ///< RSA secret prime factor of Modules (q)
  53. RsaKeyDp, ///< p's CRT exponent (== d mod (p - 1))
  54. RsaKeyDq, ///< q's CRT exponent (== d mod (q - 1))
  55. RsaKeyQInv ///< The CRT coefficient (== 1/q mod p)
  56. } RSA_KEY_TAG;
  57. //=====================================================================================
  58. // One-Way Cryptographic Hash Primitives
  59. //=====================================================================================
  60. /**
  61. Retrieves the size, in bytes, of the context buffer required for MD4 hash operations.
  62. If this interface is not supported, then return zero.
  63. @return The size, in bytes, of the context buffer required for MD4 hash operations.
  64. @retval 0 This interface is not supported.
  65. **/
  66. UINTN
  67. EFIAPI
  68. Md4GetContextSize (
  69. VOID
  70. );
  71. /**
  72. Initializes user-supplied memory pointed by Md4Context as MD4 hash context for
  73. subsequent use.
  74. If Md4Context is NULL, then return FALSE.
  75. If this interface is not supported, then return FALSE.
  76. @param[out] Md4Context Pointer to MD4 context being initialized.
  77. @retval TRUE MD4 context initialization succeeded.
  78. @retval FALSE MD4 context initialization failed.
  79. @retval FALSE This interface is not supported.
  80. **/
  81. BOOLEAN
  82. EFIAPI
  83. Md4Init (
  84. OUT VOID *Md4Context
  85. );
  86. /**
  87. Makes a copy of an existing MD4 context.
  88. If Md4Context is NULL, then return FALSE.
  89. If NewMd4Context is NULL, then return FALSE.
  90. If this interface is not supported, then return FALSE.
  91. @param[in] Md4Context Pointer to MD4 context being copied.
  92. @param[out] NewMd4Context Pointer to new MD4 context.
  93. @retval TRUE MD4 context copy succeeded.
  94. @retval FALSE MD4 context copy failed.
  95. @retval FALSE This interface is not supported.
  96. **/
  97. BOOLEAN
  98. EFIAPI
  99. Md4Duplicate (
  100. IN CONST VOID *Md4Context,
  101. OUT VOID *NewMd4Context
  102. );
  103. /**
  104. Digests the input data and updates MD4 context.
  105. This function performs MD4 digest on a data buffer of the specified size.
  106. It can be called multiple times to compute the digest of long or discontinuous data streams.
  107. MD4 context should be already correctly initialized by Md4Init(), and should not be finalized
  108. by Md4Final(). Behavior with invalid context is undefined.
  109. If Md4Context is NULL, then return FALSE.
  110. If this interface is not supported, then return FALSE.
  111. @param[in, out] Md4Context Pointer to the MD4 context.
  112. @param[in] Data Pointer to the buffer containing the data to be hashed.
  113. @param[in] DataSize Size of Data buffer in bytes.
  114. @retval TRUE MD4 data digest succeeded.
  115. @retval FALSE MD4 data digest failed.
  116. @retval FALSE This interface is not supported.
  117. **/
  118. BOOLEAN
  119. EFIAPI
  120. Md4Update (
  121. IN OUT VOID *Md4Context,
  122. IN CONST VOID *Data,
  123. IN UINTN DataSize
  124. );
  125. /**
  126. Completes computation of the MD4 digest value.
  127. This function completes MD4 hash computation and retrieves the digest value into
  128. the specified memory. After this function has been called, the MD4 context cannot
  129. be used again.
  130. MD4 context should be already correctly initialized by Md4Init(), and should not be
  131. finalized by Md4Final(). Behavior with invalid MD4 context is undefined.
  132. If Md4Context is NULL, then return FALSE.
  133. If HashValue is NULL, then return FALSE.
  134. If this interface is not supported, then return FALSE.
  135. @param[in, out] Md4Context Pointer to the MD4 context.
  136. @param[out] HashValue Pointer to a buffer that receives the MD4 digest
  137. value (16 bytes).
  138. @retval TRUE MD4 digest computation succeeded.
  139. @retval FALSE MD4 digest computation failed.
  140. @retval FALSE This interface is not supported.
  141. **/
  142. BOOLEAN
  143. EFIAPI
  144. Md4Final (
  145. IN OUT VOID *Md4Context,
  146. OUT UINT8 *HashValue
  147. );
  148. /**
  149. Computes the MD4 message digest of a input data buffer.
  150. This function performs the MD4 message digest of a given data buffer, and places
  151. the digest value into the specified memory.
  152. If this interface is not supported, then return FALSE.
  153. @param[in] Data Pointer to the buffer containing the data to be hashed.
  154. @param[in] DataSize Size of Data buffer in bytes.
  155. @param[out] HashValue Pointer to a buffer that receives the MD4 digest
  156. value (16 bytes).
  157. @retval TRUE MD4 digest computation succeeded.
  158. @retval FALSE MD4 digest computation failed.
  159. @retval FALSE This interface is not supported.
  160. **/
  161. BOOLEAN
  162. EFIAPI
  163. Md4HashAll (
  164. IN CONST VOID *Data,
  165. IN UINTN DataSize,
  166. OUT UINT8 *HashValue
  167. );
  168. /**
  169. Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
  170. If this interface is not supported, then return zero.
  171. @return The size, in bytes, of the context buffer required for MD5 hash operations.
  172. @retval 0 This interface is not supported.
  173. **/
  174. UINTN
  175. EFIAPI
  176. Md5GetContextSize (
  177. VOID
  178. );
  179. /**
  180. Initializes user-supplied memory pointed by Md5Context as MD5 hash context for
  181. subsequent use.
  182. If Md5Context is NULL, then return FALSE.
  183. If this interface is not supported, then return FALSE.
  184. @param[out] Md5Context Pointer to MD5 context being initialized.
  185. @retval TRUE MD5 context initialization succeeded.
  186. @retval FALSE MD5 context initialization failed.
  187. @retval FALSE This interface is not supported.
  188. **/
  189. BOOLEAN
  190. EFIAPI
  191. Md5Init (
  192. OUT VOID *Md5Context
  193. );
  194. /**
  195. Makes a copy of an existing MD5 context.
  196. If Md5Context is NULL, then return FALSE.
  197. If NewMd5Context is NULL, then return FALSE.
  198. If this interface is not supported, then return FALSE.
  199. @param[in] Md5Context Pointer to MD5 context being copied.
  200. @param[out] NewMd5Context Pointer to new MD5 context.
  201. @retval TRUE MD5 context copy succeeded.
  202. @retval FALSE MD5 context copy failed.
  203. @retval FALSE This interface is not supported.
  204. **/
  205. BOOLEAN
  206. EFIAPI
  207. Md5Duplicate (
  208. IN CONST VOID *Md5Context,
  209. OUT VOID *NewMd5Context
  210. );
  211. /**
  212. Digests the input data and updates MD5 context.
  213. This function performs MD5 digest on a data buffer of the specified size.
  214. It can be called multiple times to compute the digest of long or discontinuous data streams.
  215. MD5 context should be already correctly initialized by Md5Init(), and should not be finalized
  216. by Md5Final(). Behavior with invalid context is undefined.
  217. If Md5Context is NULL, then return FALSE.
  218. If this interface is not supported, then return FALSE.
  219. @param[in, out] Md5Context Pointer to the MD5 context.
  220. @param[in] Data Pointer to the buffer containing the data to be hashed.
  221. @param[in] DataSize Size of Data buffer in bytes.
  222. @retval TRUE MD5 data digest succeeded.
  223. @retval FALSE MD5 data digest failed.
  224. @retval FALSE This interface is not supported.
  225. **/
  226. BOOLEAN
  227. EFIAPI
  228. Md5Update (
  229. IN OUT VOID *Md5Context,
  230. IN CONST VOID *Data,
  231. IN UINTN DataSize
  232. );
  233. /**
  234. Completes computation of the MD5 digest value.
  235. This function completes MD5 hash computation and retrieves the digest value into
  236. the specified memory. After this function has been called, the MD5 context cannot
  237. be used again.
  238. MD5 context should be already correctly initialized by Md5Init(), and should not be
  239. finalized by Md5Final(). Behavior with invalid MD5 context is undefined.
  240. If Md5Context is NULL, then return FALSE.
  241. If HashValue is NULL, then return FALSE.
  242. If this interface is not supported, then return FALSE.
  243. @param[in, out] Md5Context Pointer to the MD5 context.
  244. @param[out] HashValue Pointer to a buffer that receives the MD5 digest
  245. value (16 bytes).
  246. @retval TRUE MD5 digest computation succeeded.
  247. @retval FALSE MD5 digest computation failed.
  248. @retval FALSE This interface is not supported.
  249. **/
  250. BOOLEAN
  251. EFIAPI
  252. Md5Final (
  253. IN OUT VOID *Md5Context,
  254. OUT UINT8 *HashValue
  255. );
  256. /**
  257. Computes the MD5 message digest of a input data buffer.
  258. This function performs the MD5 message digest of a given data buffer, and places
  259. the digest value into the specified memory.
  260. If this interface is not supported, then return FALSE.
  261. @param[in] Data Pointer to the buffer containing the data to be hashed.
  262. @param[in] DataSize Size of Data buffer in bytes.
  263. @param[out] HashValue Pointer to a buffer that receives the MD5 digest
  264. value (16 bytes).
  265. @retval TRUE MD5 digest computation succeeded.
  266. @retval FALSE MD5 digest computation failed.
  267. @retval FALSE This interface is not supported.
  268. **/
  269. BOOLEAN
  270. EFIAPI
  271. Md5HashAll (
  272. IN CONST VOID *Data,
  273. IN UINTN DataSize,
  274. OUT UINT8 *HashValue
  275. );
  276. /**
  277. Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
  278. If this interface is not supported, then return zero.
  279. @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
  280. @retval 0 This interface is not supported.
  281. **/
  282. UINTN
  283. EFIAPI
  284. Sha1GetContextSize (
  285. VOID
  286. );
  287. /**
  288. Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
  289. subsequent use.
  290. If Sha1Context is NULL, then return FALSE.
  291. If this interface is not supported, then return FALSE.
  292. @param[out] Sha1Context Pointer to SHA-1 context being initialized.
  293. @retval TRUE SHA-1 context initialization succeeded.
  294. @retval FALSE SHA-1 context initialization failed.
  295. @retval FALSE This interface is not supported.
  296. **/
  297. BOOLEAN
  298. EFIAPI
  299. Sha1Init (
  300. OUT VOID *Sha1Context
  301. );
  302. /**
  303. Makes a copy of an existing SHA-1 context.
  304. If Sha1Context is NULL, then return FALSE.
  305. If NewSha1Context is NULL, then return FALSE.
  306. If this interface is not supported, then return FALSE.
  307. @param[in] Sha1Context Pointer to SHA-1 context being copied.
  308. @param[out] NewSha1Context Pointer to new SHA-1 context.
  309. @retval TRUE SHA-1 context copy succeeded.
  310. @retval FALSE SHA-1 context copy failed.
  311. @retval FALSE This interface is not supported.
  312. **/
  313. BOOLEAN
  314. EFIAPI
  315. Sha1Duplicate (
  316. IN CONST VOID *Sha1Context,
  317. OUT VOID *NewSha1Context
  318. );
  319. /**
  320. Digests the input data and updates SHA-1 context.
  321. This function performs SHA-1 digest on a data buffer of the specified size.
  322. It can be called multiple times to compute the digest of long or discontinuous data streams.
  323. SHA-1 context should be already correctly initialized by Sha1Init(), and should not be finalized
  324. by Sha1Final(). Behavior with invalid context is undefined.
  325. If Sha1Context is NULL, then return FALSE.
  326. If this interface is not supported, then return FALSE.
  327. @param[in, out] Sha1Context Pointer to the SHA-1 context.
  328. @param[in] Data Pointer to the buffer containing the data to be hashed.
  329. @param[in] DataSize Size of Data buffer in bytes.
  330. @retval TRUE SHA-1 data digest succeeded.
  331. @retval FALSE SHA-1 data digest failed.
  332. @retval FALSE This interface is not supported.
  333. **/
  334. BOOLEAN
  335. EFIAPI
  336. Sha1Update (
  337. IN OUT VOID *Sha1Context,
  338. IN CONST VOID *Data,
  339. IN UINTN DataSize
  340. );
  341. /**
  342. Completes computation of the SHA-1 digest value.
  343. This function completes SHA-1 hash computation and retrieves the digest value into
  344. the specified memory. After this function has been called, the SHA-1 context cannot
  345. be used again.
  346. SHA-1 context should be already correctly initialized by Sha1Init(), and should not be
  347. finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
  348. If Sha1Context is NULL, then return FALSE.
  349. If HashValue is NULL, then return FALSE.
  350. If this interface is not supported, then return FALSE.
  351. @param[in, out] Sha1Context Pointer to the SHA-1 context.
  352. @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
  353. value (20 bytes).
  354. @retval TRUE SHA-1 digest computation succeeded.
  355. @retval FALSE SHA-1 digest computation failed.
  356. @retval FALSE This interface is not supported.
  357. **/
  358. BOOLEAN
  359. EFIAPI
  360. Sha1Final (
  361. IN OUT VOID *Sha1Context,
  362. OUT UINT8 *HashValue
  363. );
  364. /**
  365. Computes the SHA-1 message digest of a input data buffer.
  366. This function performs the SHA-1 message digest of a given data buffer, and places
  367. the digest value into the specified memory.
  368. If this interface is not supported, then return FALSE.
  369. @param[in] Data Pointer to the buffer containing the data to be hashed.
  370. @param[in] DataSize Size of Data buffer in bytes.
  371. @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
  372. value (20 bytes).
  373. @retval TRUE SHA-1 digest computation succeeded.
  374. @retval FALSE SHA-1 digest computation failed.
  375. @retval FALSE This interface is not supported.
  376. **/
  377. BOOLEAN
  378. EFIAPI
  379. Sha1HashAll (
  380. IN CONST VOID *Data,
  381. IN UINTN DataSize,
  382. OUT UINT8 *HashValue
  383. );
  384. /**
  385. Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.
  386. @return The size, in bytes, of the context buffer required for SHA-256 hash operations.
  387. **/
  388. UINTN
  389. EFIAPI
  390. Sha256GetContextSize (
  391. VOID
  392. );
  393. /**
  394. Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
  395. subsequent use.
  396. If Sha256Context is NULL, then return FALSE.
  397. @param[out] Sha256Context Pointer to SHA-256 context being initialized.
  398. @retval TRUE SHA-256 context initialization succeeded.
  399. @retval FALSE SHA-256 context initialization failed.
  400. **/
  401. BOOLEAN
  402. EFIAPI
  403. Sha256Init (
  404. OUT VOID *Sha256Context
  405. );
  406. /**
  407. Makes a copy of an existing SHA-256 context.
  408. If Sha256Context is NULL, then return FALSE.
  409. If NewSha256Context is NULL, then return FALSE.
  410. If this interface is not supported, then return FALSE.
  411. @param[in] Sha256Context Pointer to SHA-256 context being copied.
  412. @param[out] NewSha256Context Pointer to new SHA-256 context.
  413. @retval TRUE SHA-256 context copy succeeded.
  414. @retval FALSE SHA-256 context copy failed.
  415. @retval FALSE This interface is not supported.
  416. **/
  417. BOOLEAN
  418. EFIAPI
  419. Sha256Duplicate (
  420. IN CONST VOID *Sha256Context,
  421. OUT VOID *NewSha256Context
  422. );
  423. /**
  424. Digests the input data and updates SHA-256 context.
  425. This function performs SHA-256 digest on a data buffer of the specified size.
  426. It can be called multiple times to compute the digest of long or discontinuous data streams.
  427. SHA-256 context should be already correctly initialized by Sha256Init(), and should not be finalized
  428. by Sha256Final(). Behavior with invalid context is undefined.
  429. If Sha256Context is NULL, then return FALSE.
  430. @param[in, out] Sha256Context Pointer to the SHA-256 context.
  431. @param[in] Data Pointer to the buffer containing the data to be hashed.
  432. @param[in] DataSize Size of Data buffer in bytes.
  433. @retval TRUE SHA-256 data digest succeeded.
  434. @retval FALSE SHA-256 data digest failed.
  435. **/
  436. BOOLEAN
  437. EFIAPI
  438. Sha256Update (
  439. IN OUT VOID *Sha256Context,
  440. IN CONST VOID *Data,
  441. IN UINTN DataSize
  442. );
  443. /**
  444. Completes computation of the SHA-256 digest value.
  445. This function completes SHA-256 hash computation and retrieves the digest value into
  446. the specified memory. After this function has been called, the SHA-256 context cannot
  447. be used again.
  448. SHA-256 context should be already correctly initialized by Sha256Init(), and should not be
  449. finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.
  450. If Sha256Context is NULL, then return FALSE.
  451. If HashValue is NULL, then return FALSE.
  452. @param[in, out] Sha256Context Pointer to the SHA-256 context.
  453. @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
  454. value (32 bytes).
  455. @retval TRUE SHA-256 digest computation succeeded.
  456. @retval FALSE SHA-256 digest computation failed.
  457. **/
  458. BOOLEAN
  459. EFIAPI
  460. Sha256Final (
  461. IN OUT VOID *Sha256Context,
  462. OUT UINT8 *HashValue
  463. );
  464. /**
  465. Computes the SHA-256 message digest of a input data buffer.
  466. This function performs the SHA-256 message digest of a given data buffer, and places
  467. the digest value into the specified memory.
  468. If this interface is not supported, then return FALSE.
  469. @param[in] Data Pointer to the buffer containing the data to be hashed.
  470. @param[in] DataSize Size of Data buffer in bytes.
  471. @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
  472. value (32 bytes).
  473. @retval TRUE SHA-256 digest computation succeeded.
  474. @retval FALSE SHA-256 digest computation failed.
  475. @retval FALSE This interface is not supported.
  476. **/
  477. BOOLEAN
  478. EFIAPI
  479. Sha256HashAll (
  480. IN CONST VOID *Data,
  481. IN UINTN DataSize,
  482. OUT UINT8 *HashValue
  483. );
  484. /**
  485. Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.
  486. @return The size, in bytes, of the context buffer required for SHA-384 hash operations.
  487. **/
  488. UINTN
  489. EFIAPI
  490. Sha384GetContextSize (
  491. VOID
  492. );
  493. /**
  494. Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for
  495. subsequent use.
  496. If Sha384Context is NULL, then return FALSE.
  497. @param[out] Sha384Context Pointer to SHA-384 context being initialized.
  498. @retval TRUE SHA-384 context initialization succeeded.
  499. @retval FALSE SHA-384 context initialization failed.
  500. **/
  501. BOOLEAN
  502. EFIAPI
  503. Sha384Init (
  504. OUT VOID *Sha384Context
  505. );
  506. /**
  507. Makes a copy of an existing SHA-384 context.
  508. If Sha384Context is NULL, then return FALSE.
  509. If NewSha384Context is NULL, then return FALSE.
  510. If this interface is not supported, then return FALSE.
  511. @param[in] Sha384Context Pointer to SHA-384 context being copied.
  512. @param[out] NewSha384Context Pointer to new SHA-384 context.
  513. @retval TRUE SHA-384 context copy succeeded.
  514. @retval FALSE SHA-384 context copy failed.
  515. @retval FALSE This interface is not supported.
  516. **/
  517. BOOLEAN
  518. EFIAPI
  519. Sha384Duplicate (
  520. IN CONST VOID *Sha384Context,
  521. OUT VOID *NewSha384Context
  522. );
  523. /**
  524. Digests the input data and updates SHA-384 context.
  525. This function performs SHA-384 digest on a data buffer of the specified size.
  526. It can be called multiple times to compute the digest of long or discontinuous data streams.
  527. SHA-384 context should be already correctly initialized by Sha384Init(), and should not be finalized
  528. by Sha384Final(). Behavior with invalid context is undefined.
  529. If Sha384Context is NULL, then return FALSE.
  530. @param[in, out] Sha384Context Pointer to the SHA-384 context.
  531. @param[in] Data Pointer to the buffer containing the data to be hashed.
  532. @param[in] DataSize Size of Data buffer in bytes.
  533. @retval TRUE SHA-384 data digest succeeded.
  534. @retval FALSE SHA-384 data digest failed.
  535. **/
  536. BOOLEAN
  537. EFIAPI
  538. Sha384Update (
  539. IN OUT VOID *Sha384Context,
  540. IN CONST VOID *Data,
  541. IN UINTN DataSize
  542. );
  543. /**
  544. Completes computation of the SHA-384 digest value.
  545. This function completes SHA-384 hash computation and retrieves the digest value into
  546. the specified memory. After this function has been called, the SHA-384 context cannot
  547. be used again.
  548. SHA-384 context should be already correctly initialized by Sha384Init(), and should not be
  549. finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.
  550. If Sha384Context is NULL, then return FALSE.
  551. If HashValue is NULL, then return FALSE.
  552. @param[in, out] Sha384Context Pointer to the SHA-384 context.
  553. @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
  554. value (48 bytes).
  555. @retval TRUE SHA-384 digest computation succeeded.
  556. @retval FALSE SHA-384 digest computation failed.
  557. **/
  558. BOOLEAN
  559. EFIAPI
  560. Sha384Final (
  561. IN OUT VOID *Sha384Context,
  562. OUT UINT8 *HashValue
  563. );
  564. /**
  565. Computes the SHA-384 message digest of a input data buffer.
  566. This function performs the SHA-384 message digest of a given data buffer, and places
  567. the digest value into the specified memory.
  568. If this interface is not supported, then return FALSE.
  569. @param[in] Data Pointer to the buffer containing the data to be hashed.
  570. @param[in] DataSize Size of Data buffer in bytes.
  571. @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
  572. value (48 bytes).
  573. @retval TRUE SHA-384 digest computation succeeded.
  574. @retval FALSE SHA-384 digest computation failed.
  575. @retval FALSE This interface is not supported.
  576. **/
  577. BOOLEAN
  578. EFIAPI
  579. Sha384HashAll (
  580. IN CONST VOID *Data,
  581. IN UINTN DataSize,
  582. OUT UINT8 *HashValue
  583. );
  584. /**
  585. Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.
  586. @return The size, in bytes, of the context buffer required for SHA-512 hash operations.
  587. **/
  588. UINTN
  589. EFIAPI
  590. Sha512GetContextSize (
  591. VOID
  592. );
  593. /**
  594. Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for
  595. subsequent use.
  596. If Sha512Context is NULL, then return FALSE.
  597. @param[out] Sha512Context Pointer to SHA-512 context being initialized.
  598. @retval TRUE SHA-512 context initialization succeeded.
  599. @retval FALSE SHA-512 context initialization failed.
  600. **/
  601. BOOLEAN
  602. EFIAPI
  603. Sha512Init (
  604. OUT VOID *Sha512Context
  605. );
  606. /**
  607. Makes a copy of an existing SHA-512 context.
  608. If Sha512Context is NULL, then return FALSE.
  609. If NewSha512Context is NULL, then return FALSE.
  610. If this interface is not supported, then return FALSE.
  611. @param[in] Sha512Context Pointer to SHA-512 context being copied.
  612. @param[out] NewSha512Context Pointer to new SHA-512 context.
  613. @retval TRUE SHA-512 context copy succeeded.
  614. @retval FALSE SHA-512 context copy failed.
  615. @retval FALSE This interface is not supported.
  616. **/
  617. BOOLEAN
  618. EFIAPI
  619. Sha512Duplicate (
  620. IN CONST VOID *Sha512Context,
  621. OUT VOID *NewSha512Context
  622. );
  623. /**
  624. Digests the input data and updates SHA-512 context.
  625. This function performs SHA-512 digest on a data buffer of the specified size.
  626. It can be called multiple times to compute the digest of long or discontinuous data streams.
  627. SHA-512 context should be already correctly initialized by Sha512Init(), and should not be finalized
  628. by Sha512Final(). Behavior with invalid context is undefined.
  629. If Sha512Context is NULL, then return FALSE.
  630. @param[in, out] Sha512Context Pointer to the SHA-512 context.
  631. @param[in] Data Pointer to the buffer containing the data to be hashed.
  632. @param[in] DataSize Size of Data buffer in bytes.
  633. @retval TRUE SHA-512 data digest succeeded.
  634. @retval FALSE SHA-512 data digest failed.
  635. **/
  636. BOOLEAN
  637. EFIAPI
  638. Sha512Update (
  639. IN OUT VOID *Sha512Context,
  640. IN CONST VOID *Data,
  641. IN UINTN DataSize
  642. );
  643. /**
  644. Completes computation of the SHA-512 digest value.
  645. This function completes SHA-512 hash computation and retrieves the digest value into
  646. the specified memory. After this function has been called, the SHA-512 context cannot
  647. be used again.
  648. SHA-512 context should be already correctly initialized by Sha512Init(), and should not be
  649. finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.
  650. If Sha512Context is NULL, then return FALSE.
  651. If HashValue is NULL, then return FALSE.
  652. @param[in, out] Sha512Context Pointer to the SHA-512 context.
  653. @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
  654. value (64 bytes).
  655. @retval TRUE SHA-512 digest computation succeeded.
  656. @retval FALSE SHA-512 digest computation failed.
  657. **/
  658. BOOLEAN
  659. EFIAPI
  660. Sha512Final (
  661. IN OUT VOID *Sha512Context,
  662. OUT UINT8 *HashValue
  663. );
  664. /**
  665. Computes the SHA-512 message digest of a input data buffer.
  666. This function performs the SHA-512 message digest of a given data buffer, and places
  667. the digest value into the specified memory.
  668. If this interface is not supported, then return FALSE.
  669. @param[in] Data Pointer to the buffer containing the data to be hashed.
  670. @param[in] DataSize Size of Data buffer in bytes.
  671. @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
  672. value (64 bytes).
  673. @retval TRUE SHA-512 digest computation succeeded.
  674. @retval FALSE SHA-512 digest computation failed.
  675. @retval FALSE This interface is not supported.
  676. **/
  677. BOOLEAN
  678. EFIAPI
  679. Sha512HashAll (
  680. IN CONST VOID *Data,
  681. IN UINTN DataSize,
  682. OUT UINT8 *HashValue
  683. );
  684. //=====================================================================================
  685. // MAC (Message Authentication Code) Primitive
  686. //=====================================================================================
  687. /**
  688. Retrieves the size, in bytes, of the context buffer required for HMAC-MD5 operations.
  689. (NOTE: This API is deprecated.
  690. Use HmacMd5New() / HmacMd5Free() for HMAC-MD5 Context operations.)
  691. If this interface is not supported, then return zero.
  692. @return The size, in bytes, of the context buffer required for HMAC-MD5 operations.
  693. @retval 0 This interface is not supported.
  694. **/
  695. UINTN
  696. EFIAPI
  697. HmacMd5GetContextSize (
  698. VOID
  699. );
  700. /**
  701. Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD5 use.
  702. If this interface is not supported, then return NULL.
  703. @return Pointer to the HMAC_CTX context that has been initialized.
  704. If the allocations fails, HmacMd5New() returns NULL.
  705. @retval NULL This interface is not supported.
  706. **/
  707. VOID *
  708. EFIAPI
  709. HmacMd5New (
  710. VOID
  711. );
  712. /**
  713. Release the specified HMAC_CTX context.
  714. If this interface is not supported, then do nothing.
  715. @param[in] HmacMd5Ctx Pointer to the HMAC_CTX context to be released.
  716. **/
  717. VOID
  718. EFIAPI
  719. HmacMd5Free (
  720. IN VOID *HmacMd5Ctx
  721. );
  722. /**
  723. Initializes user-supplied memory pointed by HmacMd5Context as HMAC-MD5 context for
  724. subsequent use.
  725. If HmacMd5Context is NULL, then return FALSE.
  726. If this interface is not supported, then return FALSE.
  727. @param[out] HmacMd5Context Pointer to HMAC-MD5 context being initialized.
  728. @param[in] Key Pointer to the user-supplied key.
  729. @param[in] KeySize Key size in bytes.
  730. @retval TRUE HMAC-MD5 context initialization succeeded.
  731. @retval FALSE HMAC-MD5 context initialization failed.
  732. @retval FALSE This interface is not supported.
  733. **/
  734. BOOLEAN
  735. EFIAPI
  736. HmacMd5Init (
  737. OUT VOID *HmacMd5Context,
  738. IN CONST UINT8 *Key,
  739. IN UINTN KeySize
  740. );
  741. /**
  742. Makes a copy of an existing HMAC-MD5 context.
  743. If HmacMd5Context is NULL, then return FALSE.
  744. If NewHmacMd5Context is NULL, then return FALSE.
  745. If this interface is not supported, then return FALSE.
  746. @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.
  747. @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.
  748. @retval TRUE HMAC-MD5 context copy succeeded.
  749. @retval FALSE HMAC-MD5 context copy failed.
  750. @retval FALSE This interface is not supported.
  751. **/
  752. BOOLEAN
  753. EFIAPI
  754. HmacMd5Duplicate (
  755. IN CONST VOID *HmacMd5Context,
  756. OUT VOID *NewHmacMd5Context
  757. );
  758. /**
  759. Digests the input data and updates HMAC-MD5 context.
  760. This function performs HMAC-MD5 digest on a data buffer of the specified size.
  761. It can be called multiple times to compute the digest of long or discontinuous data streams.
  762. HMAC-MD5 context should be already correctly initialized by HmacMd5Init(), and should not be
  763. finalized by HmacMd5Final(). Behavior with invalid context is undefined.
  764. If HmacMd5Context is NULL, then return FALSE.
  765. If this interface is not supported, then return FALSE.
  766. @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
  767. @param[in] Data Pointer to the buffer containing the data to be digested.
  768. @param[in] DataSize Size of Data buffer in bytes.
  769. @retval TRUE HMAC-MD5 data digest succeeded.
  770. @retval FALSE HMAC-MD5 data digest failed.
  771. @retval FALSE This interface is not supported.
  772. **/
  773. BOOLEAN
  774. EFIAPI
  775. HmacMd5Update (
  776. IN OUT VOID *HmacMd5Context,
  777. IN CONST VOID *Data,
  778. IN UINTN DataSize
  779. );
  780. /**
  781. Completes computation of the HMAC-MD5 digest value.
  782. This function completes HMAC-MD5 hash computation and retrieves the digest value into
  783. the specified memory. After this function has been called, the HMAC-MD5 context cannot
  784. be used again.
  785. HMAC-MD5 context should be already correctly initialized by HmacMd5Init(), and should not be
  786. finalized by HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.
  787. If HmacMd5Context is NULL, then return FALSE.
  788. If HmacValue is NULL, then return FALSE.
  789. If this interface is not supported, then return FALSE.
  790. @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
  791. @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest
  792. value (16 bytes).
  793. @retval TRUE HMAC-MD5 digest computation succeeded.
  794. @retval FALSE HMAC-MD5 digest computation failed.
  795. @retval FALSE This interface is not supported.
  796. **/
  797. BOOLEAN
  798. EFIAPI
  799. HmacMd5Final (
  800. IN OUT VOID *HmacMd5Context,
  801. OUT UINT8 *HmacValue
  802. );
  803. /**
  804. Retrieves the size, in bytes, of the context buffer required for HMAC-SHA1 operations.
  805. (NOTE: This API is deprecated.
  806. Use HmacSha1New() / HmacSha1Free() for HMAC-SHA1 Context operations.)
  807. If this interface is not supported, then return zero.
  808. @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.
  809. @retval 0 This interface is not supported.
  810. **/
  811. UINTN
  812. EFIAPI
  813. HmacSha1GetContextSize (
  814. VOID
  815. );
  816. /**
  817. Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA1 use.
  818. If this interface is not supported, then return NULL.
  819. @return Pointer to the HMAC_CTX context that has been initialized.
  820. If the allocations fails, HmacSha1New() returns NULL.
  821. @return NULL This interface is not supported.
  822. **/
  823. VOID *
  824. EFIAPI
  825. HmacSha1New (
  826. VOID
  827. );
  828. /**
  829. Release the specified HMAC_CTX context.
  830. If this interface is not supported, then do nothing.
  831. @param[in] HmacSha1Ctx Pointer to the HMAC_CTX context to be released.
  832. **/
  833. VOID
  834. EFIAPI
  835. HmacSha1Free (
  836. IN VOID *HmacSha1Ctx
  837. );
  838. /**
  839. Initializes user-supplied memory pointed by HmacSha1Context as HMAC-SHA1 context for
  840. subsequent use.
  841. If HmacSha1Context is NULL, then return FALSE.
  842. If this interface is not supported, then return FALSE.
  843. @param[out] HmacSha1Context Pointer to HMAC-SHA1 context being initialized.
  844. @param[in] Key Pointer to the user-supplied key.
  845. @param[in] KeySize Key size in bytes.
  846. @retval TRUE HMAC-SHA1 context initialization succeeded.
  847. @retval FALSE HMAC-SHA1 context initialization failed.
  848. @retval FALSE This interface is not supported.
  849. **/
  850. BOOLEAN
  851. EFIAPI
  852. HmacSha1Init (
  853. OUT VOID *HmacSha1Context,
  854. IN CONST UINT8 *Key,
  855. IN UINTN KeySize
  856. );
  857. /**
  858. Makes a copy of an existing HMAC-SHA1 context.
  859. If HmacSha1Context is NULL, then return FALSE.
  860. If NewHmacSha1Context is NULL, then return FALSE.
  861. If this interface is not supported, then return FALSE.
  862. @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.
  863. @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.
  864. @retval TRUE HMAC-SHA1 context copy succeeded.
  865. @retval FALSE HMAC-SHA1 context copy failed.
  866. @retval FALSE This interface is not supported.
  867. **/
  868. BOOLEAN
  869. EFIAPI
  870. HmacSha1Duplicate (
  871. IN CONST VOID *HmacSha1Context,
  872. OUT VOID *NewHmacSha1Context
  873. );
  874. /**
  875. Digests the input data and updates HMAC-SHA1 context.
  876. This function performs HMAC-SHA1 digest on a data buffer of the specified size.
  877. It can be called multiple times to compute the digest of long or discontinuous data streams.
  878. HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should not
  879. be finalized by HmacSha1Final(). Behavior with invalid context is undefined.
  880. If HmacSha1Context is NULL, then return FALSE.
  881. If this interface is not supported, then return FALSE.
  882. @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
  883. @param[in] Data Pointer to the buffer containing the data to be digested.
  884. @param[in] DataSize Size of Data buffer in bytes.
  885. @retval TRUE HMAC-SHA1 data digest succeeded.
  886. @retval FALSE HMAC-SHA1 data digest failed.
  887. @retval FALSE This interface is not supported.
  888. **/
  889. BOOLEAN
  890. EFIAPI
  891. HmacSha1Update (
  892. IN OUT VOID *HmacSha1Context,
  893. IN CONST VOID *Data,
  894. IN UINTN DataSize
  895. );
  896. /**
  897. Completes computation of the HMAC-SHA1 digest value.
  898. This function completes HMAC-SHA1 hash computation and retrieves the digest value into
  899. the specified memory. After this function has been called, the HMAC-SHA1 context cannot
  900. be used again.
  901. HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should
  902. not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.
  903. If HmacSha1Context is NULL, then return FALSE.
  904. If HmacValue is NULL, then return FALSE.
  905. If this interface is not supported, then return FALSE.
  906. @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
  907. @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest
  908. value (20 bytes).
  909. @retval TRUE HMAC-SHA1 digest computation succeeded.
  910. @retval FALSE HMAC-SHA1 digest computation failed.
  911. @retval FALSE This interface is not supported.
  912. **/
  913. BOOLEAN
  914. EFIAPI
  915. HmacSha1Final (
  916. IN OUT VOID *HmacSha1Context,
  917. OUT UINT8 *HmacValue
  918. );
  919. /**
  920. Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.
  921. (NOTE: This API is deprecated.
  922. Use HmacSha256New() / HmacSha256Free() for HMAC-SHA256 Context operations.)
  923. If this interface is not supported, then return zero.
  924. @return The size, in bytes, of the context buffer required for HMAC-SHA256 operations.
  925. @retval 0 This interface is not supported.
  926. **/
  927. UINTN
  928. EFIAPI
  929. HmacSha256GetContextSize (
  930. VOID
  931. );
  932. /**
  933. Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
  934. @return Pointer to the HMAC_CTX context that has been initialized.
  935. If the allocations fails, HmacSha256New() returns NULL.
  936. **/
  937. VOID *
  938. EFIAPI
  939. HmacSha256New (
  940. VOID
  941. );
  942. /**
  943. Release the specified HMAC_CTX context.
  944. @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
  945. **/
  946. VOID
  947. EFIAPI
  948. HmacSha256Free (
  949. IN VOID *HmacSha256Ctx
  950. );
  951. /**
  952. Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for
  953. subsequent use.
  954. If HmacSha256Context is NULL, then return FALSE.
  955. If this interface is not supported, then return FALSE.
  956. @param[out] HmacSha256Context Pointer to HMAC-SHA256 context being initialized.
  957. @param[in] Key Pointer to the user-supplied key.
  958. @param[in] KeySize Key size in bytes.
  959. @retval TRUE HMAC-SHA256 context initialization succeeded.
  960. @retval FALSE HMAC-SHA256 context initialization failed.
  961. @retval FALSE This interface is not supported.
  962. **/
  963. BOOLEAN
  964. EFIAPI
  965. HmacSha256Init (
  966. OUT VOID *HmacSha256Context,
  967. IN CONST UINT8 *Key,
  968. IN UINTN KeySize
  969. );
  970. /**
  971. Makes a copy of an existing HMAC-SHA256 context.
  972. If HmacSha256Context is NULL, then return FALSE.
  973. If NewHmacSha256Context is NULL, then return FALSE.
  974. If this interface is not supported, then return FALSE.
  975. @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
  976. @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
  977. @retval TRUE HMAC-SHA256 context copy succeeded.
  978. @retval FALSE HMAC-SHA256 context copy failed.
  979. @retval FALSE This interface is not supported.
  980. **/
  981. BOOLEAN
  982. EFIAPI
  983. HmacSha256Duplicate (
  984. IN CONST VOID *HmacSha256Context,
  985. OUT VOID *NewHmacSha256Context
  986. );
  987. /**
  988. Digests the input data and updates HMAC-SHA256 context.
  989. This function performs HMAC-SHA256 digest on a data buffer of the specified size.
  990. It can be called multiple times to compute the digest of long or discontinuous data streams.
  991. HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not
  992. be finalized by HmacSha256Final(). Behavior with invalid context is undefined.
  993. If HmacSha256Context is NULL, then return FALSE.
  994. If this interface is not supported, then return FALSE.
  995. @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
  996. @param[in] Data Pointer to the buffer containing the data to be digested.
  997. @param[in] DataSize Size of Data buffer in bytes.
  998. @retval TRUE HMAC-SHA256 data digest succeeded.
  999. @retval FALSE HMAC-SHA256 data digest failed.
  1000. @retval FALSE This interface is not supported.
  1001. **/
  1002. BOOLEAN
  1003. EFIAPI
  1004. HmacSha256Update (
  1005. IN OUT VOID *HmacSha256Context,
  1006. IN CONST VOID *Data,
  1007. IN UINTN DataSize
  1008. );
  1009. /**
  1010. Completes computation of the HMAC-SHA256 digest value.
  1011. This function completes HMAC-SHA256 hash computation and retrieves the digest value into
  1012. the specified memory. After this function has been called, the HMAC-SHA256 context cannot
  1013. be used again.
  1014. HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should
  1015. not be finalized by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
  1016. If HmacSha256Context is NULL, then return FALSE.
  1017. If HmacValue is NULL, then return FALSE.
  1018. If this interface is not supported, then return FALSE.
  1019. @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
  1020. @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
  1021. value (32 bytes).
  1022. @retval TRUE HMAC-SHA256 digest computation succeeded.
  1023. @retval FALSE HMAC-SHA256 digest computation failed.
  1024. @retval FALSE This interface is not supported.
  1025. **/
  1026. BOOLEAN
  1027. EFIAPI
  1028. HmacSha256Final (
  1029. IN OUT VOID *HmacSha256Context,
  1030. OUT UINT8 *HmacValue
  1031. );
  1032. //=====================================================================================
  1033. // Symmetric Cryptography Primitive
  1034. //=====================================================================================
  1035. /**
  1036. Retrieves the size, in bytes, of the context buffer required for TDES operations.
  1037. If this interface is not supported, then return zero.
  1038. @return The size, in bytes, of the context buffer required for TDES operations.
  1039. @retval 0 This interface is not supported.
  1040. **/
  1041. UINTN
  1042. EFIAPI
  1043. TdesGetContextSize (
  1044. VOID
  1045. );
  1046. /**
  1047. Initializes user-supplied memory as TDES context for subsequent use.
  1048. This function initializes user-supplied memory pointed by TdesContext as TDES context.
  1049. In addition, it sets up all TDES key materials for subsequent encryption and decryption
  1050. operations.
  1051. There are 3 key options as follows:
  1052. KeyLength = 64, Keying option 1: K1 == K2 == K3 (Backward compatibility with DES)
  1053. KeyLength = 128, Keying option 2: K1 != K2 and K3 = K1 (Less Security)
  1054. KeyLength = 192 Keying option 3: K1 != K2 != K3 (Strongest)
  1055. If TdesContext is NULL, then return FALSE.
  1056. If Key is NULL, then return FALSE.
  1057. If KeyLength is not valid, then return FALSE.
  1058. If this interface is not supported, then return FALSE.
  1059. @param[out] TdesContext Pointer to TDES context being initialized.
  1060. @param[in] Key Pointer to the user-supplied TDES key.
  1061. @param[in] KeyLength Length of TDES key in bits.
  1062. @retval TRUE TDES context initialization succeeded.
  1063. @retval FALSE TDES context initialization failed.
  1064. @retval FALSE This interface is not supported.
  1065. **/
  1066. BOOLEAN
  1067. EFIAPI
  1068. TdesInit (
  1069. OUT VOID *TdesContext,
  1070. IN CONST UINT8 *Key,
  1071. IN UINTN KeyLength
  1072. );
  1073. /**
  1074. Performs TDES encryption on a data buffer of the specified size in ECB mode.
  1075. This function performs TDES encryption on data buffer pointed by Input, of specified
  1076. size of InputSize, in ECB mode.
  1077. InputSize must be multiple of block size (8 bytes). This function does not perform
  1078. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  1079. TdesContext should be already correctly initialized by TdesInit(). Behavior with
  1080. invalid TDES context is undefined.
  1081. If TdesContext is NULL, then return FALSE.
  1082. If Input is NULL, then return FALSE.
  1083. If InputSize is not multiple of block size (8 bytes), then return FALSE.
  1084. If Output is NULL, then return FALSE.
  1085. If this interface is not supported, then return FALSE.
  1086. @param[in] TdesContext Pointer to the TDES context.
  1087. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  1088. @param[in] InputSize Size of the Input buffer in bytes.
  1089. @param[out] Output Pointer to a buffer that receives the TDES encryption output.
  1090. @retval TRUE TDES encryption succeeded.
  1091. @retval FALSE TDES encryption failed.
  1092. @retval FALSE This interface is not supported.
  1093. **/
  1094. BOOLEAN
  1095. EFIAPI
  1096. TdesEcbEncrypt (
  1097. IN VOID *TdesContext,
  1098. IN CONST UINT8 *Input,
  1099. IN UINTN InputSize,
  1100. OUT UINT8 *Output
  1101. );
  1102. /**
  1103. Performs TDES decryption on a data buffer of the specified size in ECB mode.
  1104. This function performs TDES decryption on data buffer pointed by Input, of specified
  1105. size of InputSize, in ECB mode.
  1106. InputSize must be multiple of block size (8 bytes). This function does not perform
  1107. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  1108. TdesContext should be already correctly initialized by TdesInit(). Behavior with
  1109. invalid TDES context is undefined.
  1110. If TdesContext is NULL, then return FALSE.
  1111. If Input is NULL, then return FALSE.
  1112. If InputSize is not multiple of block size (8 bytes), then return FALSE.
  1113. If Output is NULL, then return FALSE.
  1114. If this interface is not supported, then return FALSE.
  1115. @param[in] TdesContext Pointer to the TDES context.
  1116. @param[in] Input Pointer to the buffer containing the data to be decrypted.
  1117. @param[in] InputSize Size of the Input buffer in bytes.
  1118. @param[out] Output Pointer to a buffer that receives the TDES decryption output.
  1119. @retval TRUE TDES decryption succeeded.
  1120. @retval FALSE TDES decryption failed.
  1121. @retval FALSE This interface is not supported.
  1122. **/
  1123. BOOLEAN
  1124. EFIAPI
  1125. TdesEcbDecrypt (
  1126. IN VOID *TdesContext,
  1127. IN CONST UINT8 *Input,
  1128. IN UINTN InputSize,
  1129. OUT UINT8 *Output
  1130. );
  1131. /**
  1132. Performs TDES encryption on a data buffer of the specified size in CBC mode.
  1133. This function performs TDES encryption on data buffer pointed by Input, of specified
  1134. size of InputSize, in CBC mode.
  1135. InputSize must be multiple of block size (8 bytes). This function does not perform
  1136. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  1137. Initialization vector should be one block size (8 bytes).
  1138. TdesContext should be already correctly initialized by TdesInit(). Behavior with
  1139. invalid TDES context is undefined.
  1140. If TdesContext is NULL, then return FALSE.
  1141. If Input is NULL, then return FALSE.
  1142. If InputSize is not multiple of block size (8 bytes), then return FALSE.
  1143. If Ivec is NULL, then return FALSE.
  1144. If Output is NULL, then return FALSE.
  1145. If this interface is not supported, then return FALSE.
  1146. @param[in] TdesContext Pointer to the TDES context.
  1147. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  1148. @param[in] InputSize Size of the Input buffer in bytes.
  1149. @param[in] Ivec Pointer to initialization vector.
  1150. @param[out] Output Pointer to a buffer that receives the TDES encryption output.
  1151. @retval TRUE TDES encryption succeeded.
  1152. @retval FALSE TDES encryption failed.
  1153. @retval FALSE This interface is not supported.
  1154. **/
  1155. BOOLEAN
  1156. EFIAPI
  1157. TdesCbcEncrypt (
  1158. IN VOID *TdesContext,
  1159. IN CONST UINT8 *Input,
  1160. IN UINTN InputSize,
  1161. IN CONST UINT8 *Ivec,
  1162. OUT UINT8 *Output
  1163. );
  1164. /**
  1165. Performs TDES decryption on a data buffer of the specified size in CBC mode.
  1166. This function performs TDES decryption on data buffer pointed by Input, of specified
  1167. size of InputSize, in CBC mode.
  1168. InputSize must be multiple of block size (8 bytes). This function does not perform
  1169. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  1170. Initialization vector should be one block size (8 bytes).
  1171. TdesContext should be already correctly initialized by TdesInit(). Behavior with
  1172. invalid TDES context is undefined.
  1173. If TdesContext is NULL, then return FALSE.
  1174. If Input is NULL, then return FALSE.
  1175. If InputSize is not multiple of block size (8 bytes), then return FALSE.
  1176. If Ivec is NULL, then return FALSE.
  1177. If Output is NULL, then return FALSE.
  1178. If this interface is not supported, then return FALSE.
  1179. @param[in] TdesContext Pointer to the TDES context.
  1180. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  1181. @param[in] InputSize Size of the Input buffer in bytes.
  1182. @param[in] Ivec Pointer to initialization vector.
  1183. @param[out] Output Pointer to a buffer that receives the TDES encryption output.
  1184. @retval TRUE TDES decryption succeeded.
  1185. @retval FALSE TDES decryption failed.
  1186. @retval FALSE This interface is not supported.
  1187. **/
  1188. BOOLEAN
  1189. EFIAPI
  1190. TdesCbcDecrypt (
  1191. IN VOID *TdesContext,
  1192. IN CONST UINT8 *Input,
  1193. IN UINTN InputSize,
  1194. IN CONST UINT8 *Ivec,
  1195. OUT UINT8 *Output
  1196. );
  1197. /**
  1198. Retrieves the size, in bytes, of the context buffer required for AES operations.
  1199. If this interface is not supported, then return zero.
  1200. @return The size, in bytes, of the context buffer required for AES operations.
  1201. @retval 0 This interface is not supported.
  1202. **/
  1203. UINTN
  1204. EFIAPI
  1205. AesGetContextSize (
  1206. VOID
  1207. );
  1208. /**
  1209. Initializes user-supplied memory as AES context for subsequent use.
  1210. This function initializes user-supplied memory pointed by AesContext as AES context.
  1211. In addition, it sets up all AES key materials for subsequent encryption and decryption
  1212. operations.
  1213. There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
  1214. If AesContext is NULL, then return FALSE.
  1215. If Key is NULL, then return FALSE.
  1216. If KeyLength is not valid, then return FALSE.
  1217. If this interface is not supported, then return FALSE.
  1218. @param[out] AesContext Pointer to AES context being initialized.
  1219. @param[in] Key Pointer to the user-supplied AES key.
  1220. @param[in] KeyLength Length of AES key in bits.
  1221. @retval TRUE AES context initialization succeeded.
  1222. @retval FALSE AES context initialization failed.
  1223. @retval FALSE This interface is not supported.
  1224. **/
  1225. BOOLEAN
  1226. EFIAPI
  1227. AesInit (
  1228. OUT VOID *AesContext,
  1229. IN CONST UINT8 *Key,
  1230. IN UINTN KeyLength
  1231. );
  1232. /**
  1233. Performs AES encryption on a data buffer of the specified size in ECB mode.
  1234. This function performs AES encryption on data buffer pointed by Input, of specified
  1235. size of InputSize, in ECB mode.
  1236. InputSize must be multiple of block size (16 bytes). This function does not perform
  1237. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  1238. AesContext should be already correctly initialized by AesInit(). Behavior with
  1239. invalid AES context is undefined.
  1240. If AesContext is NULL, then return FALSE.
  1241. If Input is NULL, then return FALSE.
  1242. If InputSize is not multiple of block size (16 bytes), then return FALSE.
  1243. If Output is NULL, then return FALSE.
  1244. If this interface is not supported, then return FALSE.
  1245. @param[in] AesContext Pointer to the AES context.
  1246. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  1247. @param[in] InputSize Size of the Input buffer in bytes.
  1248. @param[out] Output Pointer to a buffer that receives the AES encryption output.
  1249. @retval TRUE AES encryption succeeded.
  1250. @retval FALSE AES encryption failed.
  1251. @retval FALSE This interface is not supported.
  1252. **/
  1253. BOOLEAN
  1254. EFIAPI
  1255. AesEcbEncrypt (
  1256. IN VOID *AesContext,
  1257. IN CONST UINT8 *Input,
  1258. IN UINTN InputSize,
  1259. OUT UINT8 *Output
  1260. );
  1261. /**
  1262. Performs AES decryption on a data buffer of the specified size in ECB mode.
  1263. This function performs AES decryption on data buffer pointed by Input, of specified
  1264. size of InputSize, in ECB mode.
  1265. InputSize must be multiple of block size (16 bytes). This function does not perform
  1266. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  1267. AesContext should be already correctly initialized by AesInit(). Behavior with
  1268. invalid AES context is undefined.
  1269. If AesContext is NULL, then return FALSE.
  1270. If Input is NULL, then return FALSE.
  1271. If InputSize is not multiple of block size (16 bytes), then return FALSE.
  1272. If Output is NULL, then return FALSE.
  1273. If this interface is not supported, then return FALSE.
  1274. @param[in] AesContext Pointer to the AES context.
  1275. @param[in] Input Pointer to the buffer containing the data to be decrypted.
  1276. @param[in] InputSize Size of the Input buffer in bytes.
  1277. @param[out] Output Pointer to a buffer that receives the AES decryption output.
  1278. @retval TRUE AES decryption succeeded.
  1279. @retval FALSE AES decryption failed.
  1280. @retval FALSE This interface is not supported.
  1281. **/
  1282. BOOLEAN
  1283. EFIAPI
  1284. AesEcbDecrypt (
  1285. IN VOID *AesContext,
  1286. IN CONST UINT8 *Input,
  1287. IN UINTN InputSize,
  1288. OUT UINT8 *Output
  1289. );
  1290. /**
  1291. Performs AES encryption on a data buffer of the specified size in CBC mode.
  1292. This function performs AES encryption on data buffer pointed by Input, of specified
  1293. size of InputSize, in CBC mode.
  1294. InputSize must be multiple of block size (16 bytes). This function does not perform
  1295. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  1296. Initialization vector should be one block size (16 bytes).
  1297. AesContext should be already correctly initialized by AesInit(). Behavior with
  1298. invalid AES context is undefined.
  1299. If AesContext is NULL, then return FALSE.
  1300. If Input is NULL, then return FALSE.
  1301. If InputSize is not multiple of block size (16 bytes), then return FALSE.
  1302. If Ivec is NULL, then return FALSE.
  1303. If Output is NULL, then return FALSE.
  1304. If this interface is not supported, then return FALSE.
  1305. @param[in] AesContext Pointer to the AES context.
  1306. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  1307. @param[in] InputSize Size of the Input buffer in bytes.
  1308. @param[in] Ivec Pointer to initialization vector.
  1309. @param[out] Output Pointer to a buffer that receives the AES encryption output.
  1310. @retval TRUE AES encryption succeeded.
  1311. @retval FALSE AES encryption failed.
  1312. @retval FALSE This interface is not supported.
  1313. **/
  1314. BOOLEAN
  1315. EFIAPI
  1316. AesCbcEncrypt (
  1317. IN VOID *AesContext,
  1318. IN CONST UINT8 *Input,
  1319. IN UINTN InputSize,
  1320. IN CONST UINT8 *Ivec,
  1321. OUT UINT8 *Output
  1322. );
  1323. /**
  1324. Performs AES decryption on a data buffer of the specified size in CBC mode.
  1325. This function performs AES decryption on data buffer pointed by Input, of specified
  1326. size of InputSize, in CBC mode.
  1327. InputSize must be multiple of block size (16 bytes). This function does not perform
  1328. padding. Caller must perform padding, if necessary, to ensure valid input data size.
  1329. Initialization vector should be one block size (16 bytes).
  1330. AesContext should be already correctly initialized by AesInit(). Behavior with
  1331. invalid AES context is undefined.
  1332. If AesContext is NULL, then return FALSE.
  1333. If Input is NULL, then return FALSE.
  1334. If InputSize is not multiple of block size (16 bytes), then return FALSE.
  1335. If Ivec is NULL, then return FALSE.
  1336. If Output is NULL, then return FALSE.
  1337. If this interface is not supported, then return FALSE.
  1338. @param[in] AesContext Pointer to the AES context.
  1339. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  1340. @param[in] InputSize Size of the Input buffer in bytes.
  1341. @param[in] Ivec Pointer to initialization vector.
  1342. @param[out] Output Pointer to a buffer that receives the AES encryption output.
  1343. @retval TRUE AES decryption succeeded.
  1344. @retval FALSE AES decryption failed.
  1345. @retval FALSE This interface is not supported.
  1346. **/
  1347. BOOLEAN
  1348. EFIAPI
  1349. AesCbcDecrypt (
  1350. IN VOID *AesContext,
  1351. IN CONST UINT8 *Input,
  1352. IN UINTN InputSize,
  1353. IN CONST UINT8 *Ivec,
  1354. OUT UINT8 *Output
  1355. );
  1356. /**
  1357. Retrieves the size, in bytes, of the context buffer required for ARC4 operations.
  1358. If this interface is not supported, then return zero.
  1359. @return The size, in bytes, of the context buffer required for ARC4 operations.
  1360. @retval 0 This interface is not supported.
  1361. **/
  1362. UINTN
  1363. EFIAPI
  1364. Arc4GetContextSize (
  1365. VOID
  1366. );
  1367. /**
  1368. Initializes user-supplied memory as ARC4 context for subsequent use.
  1369. This function initializes user-supplied memory pointed by Arc4Context as ARC4 context.
  1370. In addition, it sets up all ARC4 key materials for subsequent encryption and decryption
  1371. operations.
  1372. If Arc4Context is NULL, then return FALSE.
  1373. If Key is NULL, then return FALSE.
  1374. If KeySize does not in the range of [5, 256] bytes, then return FALSE.
  1375. If this interface is not supported, then return FALSE.
  1376. @param[out] Arc4Context Pointer to ARC4 context being initialized.
  1377. @param[in] Key Pointer to the user-supplied ARC4 key.
  1378. @param[in] KeySize Size of ARC4 key in bytes.
  1379. @retval TRUE ARC4 context initialization succeeded.
  1380. @retval FALSE ARC4 context initialization failed.
  1381. @retval FALSE This interface is not supported.
  1382. **/
  1383. BOOLEAN
  1384. EFIAPI
  1385. Arc4Init (
  1386. OUT VOID *Arc4Context,
  1387. IN CONST UINT8 *Key,
  1388. IN UINTN KeySize
  1389. );
  1390. /**
  1391. Performs ARC4 encryption on a data buffer of the specified size.
  1392. This function performs ARC4 encryption on data buffer pointed by Input, of specified
  1393. size of InputSize.
  1394. Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
  1395. invalid ARC4 context is undefined.
  1396. If Arc4Context is NULL, then return FALSE.
  1397. If Input is NULL, then return FALSE.
  1398. If Output is NULL, then return FALSE.
  1399. If this interface is not supported, then return FALSE.
  1400. @param[in, out] Arc4Context Pointer to the ARC4 context.
  1401. @param[in] Input Pointer to the buffer containing the data to be encrypted.
  1402. @param[in] InputSize Size of the Input buffer in bytes.
  1403. @param[out] Output Pointer to a buffer that receives the ARC4 encryption output.
  1404. @retval TRUE ARC4 encryption succeeded.
  1405. @retval FALSE ARC4 encryption failed.
  1406. @retval FALSE This interface is not supported.
  1407. **/
  1408. BOOLEAN
  1409. EFIAPI
  1410. Arc4Encrypt (
  1411. IN OUT VOID *Arc4Context,
  1412. IN CONST UINT8 *Input,
  1413. IN UINTN InputSize,
  1414. OUT UINT8 *Output
  1415. );
  1416. /**
  1417. Performs ARC4 decryption on a data buffer of the specified size.
  1418. This function performs ARC4 decryption on data buffer pointed by Input, of specified
  1419. size of InputSize.
  1420. Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
  1421. invalid ARC4 context is undefined.
  1422. If Arc4Context is NULL, then return FALSE.
  1423. If Input is NULL, then return FALSE.
  1424. If Output is NULL, then return FALSE.
  1425. If this interface is not supported, then return FALSE.
  1426. @param[in, out] Arc4Context Pointer to the ARC4 context.
  1427. @param[in] Input Pointer to the buffer containing the data to be decrypted.
  1428. @param[in] InputSize Size of the Input buffer in bytes.
  1429. @param[out] Output Pointer to a buffer that receives the ARC4 decryption output.
  1430. @retval TRUE ARC4 decryption succeeded.
  1431. @retval FALSE ARC4 decryption failed.
  1432. @retval FALSE This interface is not supported.
  1433. **/
  1434. BOOLEAN
  1435. EFIAPI
  1436. Arc4Decrypt (
  1437. IN OUT VOID *Arc4Context,
  1438. IN UINT8 *Input,
  1439. IN UINTN InputSize,
  1440. OUT UINT8 *Output
  1441. );
  1442. /**
  1443. Resets the ARC4 context to the initial state.
  1444. The function resets the ARC4 context to the state it had immediately after the
  1445. ARC4Init() function call.
  1446. Contrary to ARC4Init(), Arc4Reset() requires no secret key as input, but ARC4 context
  1447. should be already correctly initialized by ARC4Init().
  1448. If Arc4Context is NULL, then return FALSE.
  1449. If this interface is not supported, then return FALSE.
  1450. @param[in, out] Arc4Context Pointer to the ARC4 context.
  1451. @retval TRUE ARC4 reset succeeded.
  1452. @retval FALSE ARC4 reset failed.
  1453. @retval FALSE This interface is not supported.
  1454. **/
  1455. BOOLEAN
  1456. EFIAPI
  1457. Arc4Reset (
  1458. IN OUT VOID *Arc4Context
  1459. );
  1460. //=====================================================================================
  1461. // Asymmetric Cryptography Primitive
  1462. //=====================================================================================
  1463. /**
  1464. Allocates and initializes one RSA context for subsequent use.
  1465. @return Pointer to the RSA context that has been initialized.
  1466. If the allocations fails, RsaNew() returns NULL.
  1467. **/
  1468. VOID *
  1469. EFIAPI
  1470. RsaNew (
  1471. VOID
  1472. );
  1473. /**
  1474. Release the specified RSA context.
  1475. If RsaContext is NULL, then return FALSE.
  1476. @param[in] RsaContext Pointer to the RSA context to be released.
  1477. **/
  1478. VOID
  1479. EFIAPI
  1480. RsaFree (
  1481. IN VOID *RsaContext
  1482. );
  1483. /**
  1484. Sets the tag-designated key component into the established RSA context.
  1485. This function sets the tag-designated RSA key component into the established
  1486. RSA context from the user-specified non-negative integer (octet string format
  1487. represented in RSA PKCS#1).
  1488. If BigNumber is NULL, then the specified key component in RSA context is cleared.
  1489. If RsaContext is NULL, then return FALSE.
  1490. @param[in, out] RsaContext Pointer to RSA context being set.
  1491. @param[in] KeyTag Tag of RSA key component being set.
  1492. @param[in] BigNumber Pointer to octet integer buffer.
  1493. If NULL, then the specified key component in RSA
  1494. context is cleared.
  1495. @param[in] BnSize Size of big number buffer in bytes.
  1496. If BigNumber is NULL, then it is ignored.
  1497. @retval TRUE RSA key component was set successfully.
  1498. @retval FALSE Invalid RSA key component tag.
  1499. **/
  1500. BOOLEAN
  1501. EFIAPI
  1502. RsaSetKey (
  1503. IN OUT VOID *RsaContext,
  1504. IN RSA_KEY_TAG KeyTag,
  1505. IN CONST UINT8 *BigNumber,
  1506. IN UINTN BnSize
  1507. );
  1508. /**
  1509. Gets the tag-designated RSA key component from the established RSA context.
  1510. This function retrieves the tag-designated RSA key component from the
  1511. established RSA context as a non-negative integer (octet string format
  1512. represented in RSA PKCS#1).
  1513. If specified key component has not been set or has been cleared, then returned
  1514. BnSize is set to 0.
  1515. If the BigNumber buffer is too small to hold the contents of the key, FALSE
  1516. is returned and BnSize is set to the required buffer size to obtain the key.
  1517. If RsaContext is NULL, then return FALSE.
  1518. If BnSize is NULL, then return FALSE.
  1519. If BnSize is large enough but BigNumber is NULL, then return FALSE.
  1520. If this interface is not supported, then return FALSE.
  1521. @param[in, out] RsaContext Pointer to RSA context being set.
  1522. @param[in] KeyTag Tag of RSA key component being set.
  1523. @param[out] BigNumber Pointer to octet integer buffer.
  1524. @param[in, out] BnSize On input, the size of big number buffer in bytes.
  1525. On output, the size of data returned in big number buffer in bytes.
  1526. @retval TRUE RSA key component was retrieved successfully.
  1527. @retval FALSE Invalid RSA key component tag.
  1528. @retval FALSE BnSize is too small.
  1529. @retval FALSE This interface is not supported.
  1530. **/
  1531. BOOLEAN
  1532. EFIAPI
  1533. RsaGetKey (
  1534. IN OUT VOID *RsaContext,
  1535. IN RSA_KEY_TAG KeyTag,
  1536. OUT UINT8 *BigNumber,
  1537. IN OUT UINTN *BnSize
  1538. );
  1539. /**
  1540. Generates RSA key components.
  1541. This function generates RSA key components. It takes RSA public exponent E and
  1542. length in bits of RSA modulus N as input, and generates all key components.
  1543. If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
  1544. Before this function can be invoked, pseudorandom number generator must be correctly
  1545. initialized by RandomSeed().
  1546. If RsaContext is NULL, then return FALSE.
  1547. If this interface is not supported, then return FALSE.
  1548. @param[in, out] RsaContext Pointer to RSA context being set.
  1549. @param[in] ModulusLength Length of RSA modulus N in bits.
  1550. @param[in] PublicExponent Pointer to RSA public exponent.
  1551. @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
  1552. @retval TRUE RSA key component was generated successfully.
  1553. @retval FALSE Invalid RSA key component tag.
  1554. @retval FALSE This interface is not supported.
  1555. **/
  1556. BOOLEAN
  1557. EFIAPI
  1558. RsaGenerateKey (
  1559. IN OUT VOID *RsaContext,
  1560. IN UINTN ModulusLength,
  1561. IN CONST UINT8 *PublicExponent,
  1562. IN UINTN PublicExponentSize
  1563. );
  1564. /**
  1565. Validates key components of RSA context.
  1566. NOTE: This function performs integrity checks on all the RSA key material, so
  1567. the RSA key structure must contain all the private key data.
  1568. This function validates key components of RSA context in following aspects:
  1569. - Whether p is a prime
  1570. - Whether q is a prime
  1571. - Whether n = p * q
  1572. - Whether d*e = 1 mod lcm(p-1,q-1)
  1573. If RsaContext is NULL, then return FALSE.
  1574. If this interface is not supported, then return FALSE.
  1575. @param[in] RsaContext Pointer to RSA context to check.
  1576. @retval TRUE RSA key components are valid.
  1577. @retval FALSE RSA key components are not valid.
  1578. @retval FALSE This interface is not supported.
  1579. **/
  1580. BOOLEAN
  1581. EFIAPI
  1582. RsaCheckKey (
  1583. IN VOID *RsaContext
  1584. );
  1585. /**
  1586. Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
  1587. This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
  1588. RSA PKCS#1.
  1589. If the Signature buffer is too small to hold the contents of signature, FALSE
  1590. is returned and SigSize is set to the required buffer size to obtain the signature.
  1591. If RsaContext is NULL, then return FALSE.
  1592. If MessageHash is NULL, then return FALSE.
  1593. If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
  1594. If SigSize is large enough but Signature is NULL, then return FALSE.
  1595. If this interface is not supported, then return FALSE.
  1596. @param[in] RsaContext Pointer to RSA context for signature generation.
  1597. @param[in] MessageHash Pointer to octet message hash to be signed.
  1598. @param[in] HashSize Size of the message hash in bytes.
  1599. @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
  1600. @param[in, out] SigSize On input, the size of Signature buffer in bytes.
  1601. On output, the size of data returned in Signature buffer in bytes.
  1602. @retval TRUE Signature successfully generated in PKCS1-v1_5.
  1603. @retval FALSE Signature generation failed.
  1604. @retval FALSE SigSize is too small.
  1605. @retval FALSE This interface is not supported.
  1606. **/
  1607. BOOLEAN
  1608. EFIAPI
  1609. RsaPkcs1Sign (
  1610. IN VOID *RsaContext,
  1611. IN CONST UINT8 *MessageHash,
  1612. IN UINTN HashSize,
  1613. OUT UINT8 *Signature,
  1614. IN OUT UINTN *SigSize
  1615. );
  1616. /**
  1617. Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
  1618. RSA PKCS#1.
  1619. If RsaContext is NULL, then return FALSE.
  1620. If MessageHash is NULL, then return FALSE.
  1621. If Signature is NULL, then return FALSE.
  1622. If HashSize is not equal to the size of MD5, SHA-1, SHA-256 digest, then return FALSE.
  1623. @param[in] RsaContext Pointer to RSA context for signature verification.
  1624. @param[in] MessageHash Pointer to octet message hash to be checked.
  1625. @param[in] HashSize Size of the message hash in bytes.
  1626. @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
  1627. @param[in] SigSize Size of signature in bytes.
  1628. @retval TRUE Valid signature encoded in PKCS1-v1_5.
  1629. @retval FALSE Invalid signature or invalid RSA context.
  1630. **/
  1631. BOOLEAN
  1632. EFIAPI
  1633. RsaPkcs1Verify (
  1634. IN VOID *RsaContext,
  1635. IN CONST UINT8 *MessageHash,
  1636. IN UINTN HashSize,
  1637. IN CONST UINT8 *Signature,
  1638. IN UINTN SigSize
  1639. );
  1640. /**
  1641. Retrieve the RSA Private Key from the password-protected PEM key data.
  1642. If PemData is NULL, then return FALSE.
  1643. If RsaContext is NULL, then return FALSE.
  1644. If this interface is not supported, then return FALSE.
  1645. @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
  1646. @param[in] PemSize Size of the PEM key data in bytes.
  1647. @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
  1648. @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
  1649. RSA private key component. Use RsaFree() function to free the
  1650. resource.
  1651. @retval TRUE RSA Private Key was retrieved successfully.
  1652. @retval FALSE Invalid PEM key data or incorrect password.
  1653. @retval FALSE This interface is not supported.
  1654. **/
  1655. BOOLEAN
  1656. EFIAPI
  1657. RsaGetPrivateKeyFromPem (
  1658. IN CONST UINT8 *PemData,
  1659. IN UINTN PemSize,
  1660. IN CONST CHAR8 *Password,
  1661. OUT VOID **RsaContext
  1662. );
  1663. /**
  1664. Retrieve the RSA Public Key from one DER-encoded X509 certificate.
  1665. If Cert is NULL, then return FALSE.
  1666. If RsaContext is NULL, then return FALSE.
  1667. If this interface is not supported, then return FALSE.
  1668. @param[in] Cert Pointer to the DER-encoded X509 certificate.
  1669. @param[in] CertSize Size of the X509 certificate in bytes.
  1670. @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
  1671. RSA public key component. Use RsaFree() function to free the
  1672. resource.
  1673. @retval TRUE RSA Public Key was retrieved successfully.
  1674. @retval FALSE Fail to retrieve RSA public key from X509 certificate.
  1675. @retval FALSE This interface is not supported.
  1676. **/
  1677. BOOLEAN
  1678. EFIAPI
  1679. RsaGetPublicKeyFromX509 (
  1680. IN CONST UINT8 *Cert,
  1681. IN UINTN CertSize,
  1682. OUT VOID **RsaContext
  1683. );
  1684. /**
  1685. Retrieve the subject bytes from one X.509 certificate.
  1686. If Cert is NULL, then return FALSE.
  1687. If SubjectSize is NULL, then return FALSE.
  1688. If this interface is not supported, then return FALSE.
  1689. @param[in] Cert Pointer to the DER-encoded X509 certificate.
  1690. @param[in] CertSize Size of the X509 certificate in bytes.
  1691. @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
  1692. @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
  1693. and the size of buffer returned CertSubject on output.
  1694. @retval TRUE The certificate subject retrieved successfully.
  1695. @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
  1696. The SubjectSize will be updated with the required size.
  1697. @retval FALSE This interface is not supported.
  1698. **/
  1699. BOOLEAN
  1700. EFIAPI
  1701. X509GetSubjectName (
  1702. IN CONST UINT8 *Cert,
  1703. IN UINTN CertSize,
  1704. OUT UINT8 *CertSubject,
  1705. IN OUT UINTN *SubjectSize
  1706. );
  1707. /**
  1708. Retrieve the common name (CN) string from one X.509 certificate.
  1709. @param[in] Cert Pointer to the DER-encoded X509 certificate.
  1710. @param[in] CertSize Size of the X509 certificate in bytes.
  1711. @param[out] CommonName Buffer to contain the retrieved certificate common
  1712. name string (UTF8). At most CommonNameSize bytes will be
  1713. written and the string will be null terminated. May be
  1714. NULL in order to determine the size buffer needed.
  1715. @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
  1716. and the size of buffer returned CommonName on output.
  1717. If CommonName is NULL then the amount of space needed
  1718. in buffer (including the final null) is returned.
  1719. @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
  1720. @retval RETURN_INVALID_PARAMETER If Cert is NULL.
  1721. If CommonNameSize is NULL.
  1722. If CommonName is not NULL and *CommonNameSize is 0.
  1723. If Certificate is invalid.
  1724. @retval RETURN_NOT_FOUND If no CommonName entry exists.
  1725. @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
  1726. (including the final null) is returned in the
  1727. CommonNameSize parameter.
  1728. @retval RETURN_UNSUPPORTED The operation is not supported.
  1729. **/
  1730. RETURN_STATUS
  1731. EFIAPI
  1732. X509GetCommonName (
  1733. IN CONST UINT8 *Cert,
  1734. IN UINTN CertSize,
  1735. OUT CHAR8 *CommonName, OPTIONAL
  1736. IN OUT UINTN *CommonNameSize
  1737. );
  1738. /**
  1739. Verify one X509 certificate was issued by the trusted CA.
  1740. If Cert is NULL, then return FALSE.
  1741. If CACert is NULL, then return FALSE.
  1742. If this interface is not supported, then return FALSE.
  1743. @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
  1744. @param[in] CertSize Size of the X509 certificate in bytes.
  1745. @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
  1746. @param[in] CACertSize Size of the CA Certificate in bytes.
  1747. @retval TRUE The certificate was issued by the trusted CA.
  1748. @retval FALSE Invalid certificate or the certificate was not issued by the given
  1749. trusted CA.
  1750. @retval FALSE This interface is not supported.
  1751. **/
  1752. BOOLEAN
  1753. EFIAPI
  1754. X509VerifyCert (
  1755. IN CONST UINT8 *Cert,
  1756. IN UINTN CertSize,
  1757. IN CONST UINT8 *CACert,
  1758. IN UINTN CACertSize
  1759. );
  1760. /**
  1761. Construct a X509 object from DER-encoded certificate data.
  1762. If Cert is NULL, then return FALSE.
  1763. If SingleX509Cert is NULL, then return FALSE.
  1764. If this interface is not supported, then return FALSE.
  1765. @param[in] Cert Pointer to the DER-encoded certificate data.
  1766. @param[in] CertSize The size of certificate data in bytes.
  1767. @param[out] SingleX509Cert The generated X509 object.
  1768. @retval TRUE The X509 object generation succeeded.
  1769. @retval FALSE The operation failed.
  1770. @retval FALSE This interface is not supported.
  1771. **/
  1772. BOOLEAN
  1773. EFIAPI
  1774. X509ConstructCertificate (
  1775. IN CONST UINT8 *Cert,
  1776. IN UINTN CertSize,
  1777. OUT UINT8 **SingleX509Cert
  1778. );
  1779. /**
  1780. Construct a X509 stack object from a list of DER-encoded certificate data.
  1781. If X509Stack is NULL, then return FALSE.
  1782. If this interface is not supported, then return FALSE.
  1783. @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
  1784. On output, pointer to the X509 stack object with new
  1785. inserted X509 certificate.
  1786. @param ... A list of DER-encoded single certificate data followed
  1787. by certificate size. A NULL terminates the list. The
  1788. pairs are the arguments to X509ConstructCertificate().
  1789. @retval TRUE The X509 stack construction succeeded.
  1790. @retval FALSE The construction operation failed.
  1791. @retval FALSE This interface is not supported.
  1792. **/
  1793. BOOLEAN
  1794. EFIAPI
  1795. X509ConstructCertificateStack (
  1796. IN OUT UINT8 **X509Stack,
  1797. ...
  1798. );
  1799. /**
  1800. Release the specified X509 object.
  1801. If the interface is not supported, then ASSERT().
  1802. @param[in] X509Cert Pointer to the X509 object to be released.
  1803. **/
  1804. VOID
  1805. EFIAPI
  1806. X509Free (
  1807. IN VOID *X509Cert
  1808. );
  1809. /**
  1810. Release the specified X509 stack object.
  1811. If the interface is not supported, then ASSERT().
  1812. @param[in] X509Stack Pointer to the X509 stack object to be released.
  1813. **/
  1814. VOID
  1815. EFIAPI
  1816. X509StackFree (
  1817. IN VOID *X509Stack
  1818. );
  1819. /**
  1820. Retrieve the TBSCertificate from one given X.509 certificate.
  1821. @param[in] Cert Pointer to the given DER-encoded X509 certificate.
  1822. @param[in] CertSize Size of the X509 certificate in bytes.
  1823. @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
  1824. @param[out] TBSCertSize Size of the TBS certificate in bytes.
  1825. If Cert is NULL, then return FALSE.
  1826. If TBSCert is NULL, then return FALSE.
  1827. If TBSCertSize is NULL, then return FALSE.
  1828. If this interface is not supported, then return FALSE.
  1829. @retval TRUE The TBSCertificate was retrieved successfully.
  1830. @retval FALSE Invalid X.509 certificate.
  1831. **/
  1832. BOOLEAN
  1833. EFIAPI
  1834. X509GetTBSCert (
  1835. IN CONST UINT8 *Cert,
  1836. IN UINTN CertSize,
  1837. OUT UINT8 **TBSCert,
  1838. OUT UINTN *TBSCertSize
  1839. );
  1840. /**
  1841. Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0
  1842. password based encryption key derivation function PBKDF2, as specified in RFC 2898.
  1843. If Password or Salt or OutKey is NULL, then return FALSE.
  1844. If the hash algorithm could not be determined, then return FALSE.
  1845. If this interface is not supported, then return FALSE.
  1846. @param[in] PasswordLength Length of input password in bytes.
  1847. @param[in] Password Pointer to the array for the password.
  1848. @param[in] SaltLength Size of the Salt in bytes.
  1849. @param[in] Salt Pointer to the Salt.
  1850. @param[in] IterationCount Number of iterations to perform. Its value should be
  1851. greater than or equal to 1.
  1852. @param[in] DigestSize Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).
  1853. NOTE: DigestSize will be used to determine the hash algorithm.
  1854. Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.
  1855. @param[in] KeyLength Size of the derived key buffer in bytes.
  1856. @param[out] OutKey Pointer to the output derived key buffer.
  1857. @retval TRUE A key was derived successfully.
  1858. @retval FALSE One of the pointers was NULL or one of the sizes was too large.
  1859. @retval FALSE The hash algorithm could not be determined from the digest size.
  1860. @retval FALSE The key derivation operation failed.
  1861. @retval FALSE This interface is not supported.
  1862. **/
  1863. BOOLEAN
  1864. EFIAPI
  1865. Pkcs5HashPassword (
  1866. IN UINTN PasswordLength,
  1867. IN CONST CHAR8 *Password,
  1868. IN UINTN SaltLength,
  1869. IN CONST UINT8 *Salt,
  1870. IN UINTN IterationCount,
  1871. IN UINTN DigestSize,
  1872. IN UINTN KeyLength,
  1873. OUT UINT8 *OutKey
  1874. );
  1875. /**
  1876. The 3rd parameter of Pkcs7GetSigners will return all embedded
  1877. X.509 certificate in one given PKCS7 signature. The format is:
  1878. //
  1879. // UINT8 CertNumber;
  1880. // UINT32 Cert1Length;
  1881. // UINT8 Cert1[];
  1882. // UINT32 Cert2Length;
  1883. // UINT8 Cert2[];
  1884. // ...
  1885. // UINT32 CertnLength;
  1886. // UINT8 Certn[];
  1887. //
  1888. The two following C-structure are used for parsing CertStack more clearly.
  1889. **/
  1890. #pragma pack(1)
  1891. typedef struct {
  1892. UINT32 CertDataLength; // The length in bytes of X.509 certificate.
  1893. UINT8 CertDataBuffer[0]; // The X.509 certificate content (DER).
  1894. } EFI_CERT_DATA;
  1895. typedef struct {
  1896. UINT8 CertNumber; // Number of X.509 certificate.
  1897. //EFI_CERT_DATA CertArray[]; // An array of X.509 certificate.
  1898. } EFI_CERT_STACK;
  1899. #pragma pack()
  1900. /**
  1901. Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
  1902. Cryptographic Message Syntax Standard". The input signed data could be wrapped
  1903. in a ContentInfo structure.
  1904. If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
  1905. return FALSE. If P7Length overflow, then return FALSE.
  1906. If this interface is not supported, then return FALSE.
  1907. @param[in] P7Data Pointer to the PKCS#7 message to verify.
  1908. @param[in] P7Length Length of the PKCS#7 message in bytes.
  1909. @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
  1910. It's caller's responsibility to free the buffer with
  1911. Pkcs7FreeSigners().
  1912. This data structure is EFI_CERT_STACK type.
  1913. @param[out] StackLength Length of signer's certificates in bytes.
  1914. @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
  1915. It's caller's responsibility to free the buffer with
  1916. Pkcs7FreeSigners().
  1917. @param[out] CertLength Length of the trusted certificate in bytes.
  1918. @retval TRUE The operation is finished successfully.
  1919. @retval FALSE Error occurs during the operation.
  1920. @retval FALSE This interface is not supported.
  1921. **/
  1922. BOOLEAN
  1923. EFIAPI
  1924. Pkcs7GetSigners (
  1925. IN CONST UINT8 *P7Data,
  1926. IN UINTN P7Length,
  1927. OUT UINT8 **CertStack,
  1928. OUT UINTN *StackLength,
  1929. OUT UINT8 **TrustedCert,
  1930. OUT UINTN *CertLength
  1931. );
  1932. /**
  1933. Wrap function to use free() to free allocated memory for certificates.
  1934. If this interface is not supported, then ASSERT().
  1935. @param[in] Certs Pointer to the certificates to be freed.
  1936. **/
  1937. VOID
  1938. EFIAPI
  1939. Pkcs7FreeSigners (
  1940. IN UINT8 *Certs
  1941. );
  1942. /**
  1943. Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
  1944. Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
  1945. unchained to the signer's certificates.
  1946. The input signed data could be wrapped in a ContentInfo structure.
  1947. @param[in] P7Data Pointer to the PKCS#7 message.
  1948. @param[in] P7Length Length of the PKCS#7 message in bytes.
  1949. @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
  1950. certificate. It's caller's responsibility to free the buffer
  1951. with Pkcs7FreeSigners().
  1952. This data structure is EFI_CERT_STACK type.
  1953. @param[out] ChainLength Length of the chained certificates list buffer in bytes.
  1954. @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
  1955. responsibility to free the buffer with Pkcs7FreeSigners().
  1956. This data structure is EFI_CERT_STACK type.
  1957. @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
  1958. @retval TRUE The operation is finished successfully.
  1959. @retval FALSE Error occurs during the operation.
  1960. **/
  1961. BOOLEAN
  1962. EFIAPI
  1963. Pkcs7GetCertificatesList (
  1964. IN CONST UINT8 *P7Data,
  1965. IN UINTN P7Length,
  1966. OUT UINT8 **SignerChainCerts,
  1967. OUT UINTN *ChainLength,
  1968. OUT UINT8 **UnchainCerts,
  1969. OUT UINTN *UnchainLength
  1970. );
  1971. /**
  1972. Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
  1973. Syntax Standard, version 1.5". This interface is only intended to be used for
  1974. application to perform PKCS#7 functionality validation.
  1975. If this interface is not supported, then return FALSE.
  1976. @param[in] PrivateKey Pointer to the PEM-formatted private key data for
  1977. data signing.
  1978. @param[in] PrivateKeySize Size of the PEM private key data in bytes.
  1979. @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
  1980. key data.
  1981. @param[in] InData Pointer to the content to be signed.
  1982. @param[in] InDataSize Size of InData in bytes.
  1983. @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
  1984. @param[in] OtherCerts Pointer to an optional additional set of certificates to
  1985. include in the PKCS#7 signedData (e.g. any intermediate
  1986. CAs in the chain).
  1987. @param[out] SignedData Pointer to output PKCS#7 signedData. It's caller's
  1988. responsibility to free the buffer with FreePool().
  1989. @param[out] SignedDataSize Size of SignedData in bytes.
  1990. @retval TRUE PKCS#7 data signing succeeded.
  1991. @retval FALSE PKCS#7 data signing failed.
  1992. @retval FALSE This interface is not supported.
  1993. **/
  1994. BOOLEAN
  1995. EFIAPI
  1996. Pkcs7Sign (
  1997. IN CONST UINT8 *PrivateKey,
  1998. IN UINTN PrivateKeySize,
  1999. IN CONST UINT8 *KeyPassword,
  2000. IN UINT8 *InData,
  2001. IN UINTN InDataSize,
  2002. IN UINT8 *SignCert,
  2003. IN UINT8 *OtherCerts OPTIONAL,
  2004. OUT UINT8 **SignedData,
  2005. OUT UINTN *SignedDataSize
  2006. );
  2007. /**
  2008. Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
  2009. Cryptographic Message Syntax Standard". The input signed data could be wrapped
  2010. in a ContentInfo structure.
  2011. If P7Data, TrustedCert or InData is NULL, then return FALSE.
  2012. If P7Length, CertLength or DataLength overflow, then return FALSE.
  2013. If this interface is not supported, then return FALSE.
  2014. @param[in] P7Data Pointer to the PKCS#7 message to verify.
  2015. @param[in] P7Length Length of the PKCS#7 message in bytes.
  2016. @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
  2017. is used for certificate chain verification.
  2018. @param[in] CertLength Length of the trusted certificate in bytes.
  2019. @param[in] InData Pointer to the content to be verified.
  2020. @param[in] DataLength Length of InData in bytes.
  2021. @retval TRUE The specified PKCS#7 signed data is valid.
  2022. @retval FALSE Invalid PKCS#7 signed data.
  2023. @retval FALSE This interface is not supported.
  2024. **/
  2025. BOOLEAN
  2026. EFIAPI
  2027. Pkcs7Verify (
  2028. IN CONST UINT8 *P7Data,
  2029. IN UINTN P7Length,
  2030. IN CONST UINT8 *TrustedCert,
  2031. IN UINTN CertLength,
  2032. IN CONST UINT8 *InData,
  2033. IN UINTN DataLength
  2034. );
  2035. /**
  2036. Extracts the attached content from a PKCS#7 signed data if existed. The input signed
  2037. data could be wrapped in a ContentInfo structure.
  2038. If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
  2039. then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
  2040. Caution: This function may receive untrusted input. So this function will do
  2041. basic check for PKCS#7 data structure.
  2042. @param[in] P7Data Pointer to the PKCS#7 signed data to process.
  2043. @param[in] P7Length Length of the PKCS#7 signed data in bytes.
  2044. @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
  2045. It's caller's responsibility to free the buffer with FreePool().
  2046. @param[out] ContentSize The size of the extracted content in bytes.
  2047. @retval TRUE The P7Data was correctly formatted for processing.
  2048. @retval FALSE The P7Data was not correctly formatted for processing.
  2049. **/
  2050. BOOLEAN
  2051. EFIAPI
  2052. Pkcs7GetAttachedContent (
  2053. IN CONST UINT8 *P7Data,
  2054. IN UINTN P7Length,
  2055. OUT VOID **Content,
  2056. OUT UINTN *ContentSize
  2057. );
  2058. /**
  2059. Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
  2060. Authenticode Portable Executable Signature Format".
  2061. If AuthData is NULL, then return FALSE.
  2062. If ImageHash is NULL, then return FALSE.
  2063. If this interface is not supported, then return FALSE.
  2064. @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
  2065. PE/COFF image to be verified.
  2066. @param[in] DataSize Size of the Authenticode Signature in bytes.
  2067. @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
  2068. is used for certificate chain verification.
  2069. @param[in] CertSize Size of the trusted certificate in bytes.
  2070. @param[in] ImageHash Pointer to the original image file hash value. The procedure
  2071. for calculating the image hash value is described in Authenticode
  2072. specification.
  2073. @param[in] HashSize Size of Image hash value in bytes.
  2074. @retval TRUE The specified Authenticode Signature is valid.
  2075. @retval FALSE Invalid Authenticode Signature.
  2076. @retval FALSE This interface is not supported.
  2077. **/
  2078. BOOLEAN
  2079. EFIAPI
  2080. AuthenticodeVerify (
  2081. IN CONST UINT8 *AuthData,
  2082. IN UINTN DataSize,
  2083. IN CONST UINT8 *TrustedCert,
  2084. IN UINTN CertSize,
  2085. IN CONST UINT8 *ImageHash,
  2086. IN UINTN HashSize
  2087. );
  2088. /**
  2089. Verifies the validity of a RFC3161 Timestamp CounterSignature embedded in PE/COFF Authenticode
  2090. signature.
  2091. If AuthData is NULL, then return FALSE.
  2092. If this interface is not supported, then return FALSE.
  2093. @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
  2094. PE/COFF image to be verified.
  2095. @param[in] DataSize Size of the Authenticode Signature in bytes.
  2096. @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER, which
  2097. is used for TSA certificate chain verification.
  2098. @param[in] CertSize Size of the trusted certificate in bytes.
  2099. @param[out] SigningTime Return the time of timestamp generation time if the timestamp
  2100. signature is valid.
  2101. @retval TRUE The specified Authenticode includes a valid RFC3161 Timestamp CounterSignature.
  2102. @retval FALSE No valid RFC3161 Timestamp CounterSignature in the specified Authenticode data.
  2103. **/
  2104. BOOLEAN
  2105. EFIAPI
  2106. ImageTimestampVerify (
  2107. IN CONST UINT8 *AuthData,
  2108. IN UINTN DataSize,
  2109. IN CONST UINT8 *TsaCert,
  2110. IN UINTN CertSize,
  2111. OUT EFI_TIME *SigningTime
  2112. );
  2113. //=====================================================================================
  2114. // DH Key Exchange Primitive
  2115. //=====================================================================================
  2116. /**
  2117. Allocates and Initializes one Diffie-Hellman Context for subsequent use.
  2118. @return Pointer to the Diffie-Hellman Context that has been initialized.
  2119. If the allocations fails, DhNew() returns NULL.
  2120. If the interface is not supported, DhNew() returns NULL.
  2121. **/
  2122. VOID *
  2123. EFIAPI
  2124. DhNew (
  2125. VOID
  2126. );
  2127. /**
  2128. Release the specified DH context.
  2129. If the interface is not supported, then ASSERT().
  2130. @param[in] DhContext Pointer to the DH context to be released.
  2131. **/
  2132. VOID
  2133. EFIAPI
  2134. DhFree (
  2135. IN VOID *DhContext
  2136. );
  2137. /**
  2138. Generates DH parameter.
  2139. Given generator g, and length of prime number p in bits, this function generates p,
  2140. and sets DH context according to value of g and p.
  2141. Before this function can be invoked, pseudorandom number generator must be correctly
  2142. initialized by RandomSeed().
  2143. If DhContext is NULL, then return FALSE.
  2144. If Prime is NULL, then return FALSE.
  2145. If this interface is not supported, then return FALSE.
  2146. @param[in, out] DhContext Pointer to the DH context.
  2147. @param[in] Generator Value of generator.
  2148. @param[in] PrimeLength Length in bits of prime to be generated.
  2149. @param[out] Prime Pointer to the buffer to receive the generated prime number.
  2150. @retval TRUE DH parameter generation succeeded.
  2151. @retval FALSE Value of Generator is not supported.
  2152. @retval FALSE PRNG fails to generate random prime number with PrimeLength.
  2153. @retval FALSE This interface is not supported.
  2154. **/
  2155. BOOLEAN
  2156. EFIAPI
  2157. DhGenerateParameter (
  2158. IN OUT VOID *DhContext,
  2159. IN UINTN Generator,
  2160. IN UINTN PrimeLength,
  2161. OUT UINT8 *Prime
  2162. );
  2163. /**
  2164. Sets generator and prime parameters for DH.
  2165. Given generator g, and prime number p, this function and sets DH
  2166. context accordingly.
  2167. If DhContext is NULL, then return FALSE.
  2168. If Prime is NULL, then return FALSE.
  2169. If this interface is not supported, then return FALSE.
  2170. @param[in, out] DhContext Pointer to the DH context.
  2171. @param[in] Generator Value of generator.
  2172. @param[in] PrimeLength Length in bits of prime to be generated.
  2173. @param[in] Prime Pointer to the prime number.
  2174. @retval TRUE DH parameter setting succeeded.
  2175. @retval FALSE Value of Generator is not supported.
  2176. @retval FALSE Value of Generator is not suitable for the Prime.
  2177. @retval FALSE Value of Prime is not a prime number.
  2178. @retval FALSE Value of Prime is not a safe prime number.
  2179. @retval FALSE This interface is not supported.
  2180. **/
  2181. BOOLEAN
  2182. EFIAPI
  2183. DhSetParameter (
  2184. IN OUT VOID *DhContext,
  2185. IN UINTN Generator,
  2186. IN UINTN PrimeLength,
  2187. IN CONST UINT8 *Prime
  2188. );
  2189. /**
  2190. Generates DH public key.
  2191. This function generates random secret exponent, and computes the public key, which is
  2192. returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
  2193. If the PublicKey buffer is too small to hold the public key, FALSE is returned and
  2194. PublicKeySize is set to the required buffer size to obtain the public key.
  2195. If DhContext is NULL, then return FALSE.
  2196. If PublicKeySize is NULL, then return FALSE.
  2197. If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
  2198. If this interface is not supported, then return FALSE.
  2199. @param[in, out] DhContext Pointer to the DH context.
  2200. @param[out] PublicKey Pointer to the buffer to receive generated public key.
  2201. @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
  2202. On output, the size of data returned in PublicKey buffer in bytes.
  2203. @retval TRUE DH public key generation succeeded.
  2204. @retval FALSE DH public key generation failed.
  2205. @retval FALSE PublicKeySize is not large enough.
  2206. @retval FALSE This interface is not supported.
  2207. **/
  2208. BOOLEAN
  2209. EFIAPI
  2210. DhGenerateKey (
  2211. IN OUT VOID *DhContext,
  2212. OUT UINT8 *PublicKey,
  2213. IN OUT UINTN *PublicKeySize
  2214. );
  2215. /**
  2216. Computes exchanged common key.
  2217. Given peer's public key, this function computes the exchanged common key, based on its own
  2218. context including value of prime modulus and random secret exponent.
  2219. If DhContext is NULL, then return FALSE.
  2220. If PeerPublicKey is NULL, then return FALSE.
  2221. If KeySize is NULL, then return FALSE.
  2222. If Key is NULL, then return FALSE.
  2223. If KeySize is not large enough, then return FALSE.
  2224. If this interface is not supported, then return FALSE.
  2225. @param[in, out] DhContext Pointer to the DH context.
  2226. @param[in] PeerPublicKey Pointer to the peer's public key.
  2227. @param[in] PeerPublicKeySize Size of peer's public key in bytes.
  2228. @param[out] Key Pointer to the buffer to receive generated key.
  2229. @param[in, out] KeySize On input, the size of Key buffer in bytes.
  2230. On output, the size of data returned in Key buffer in bytes.
  2231. @retval TRUE DH exchanged key generation succeeded.
  2232. @retval FALSE DH exchanged key generation failed.
  2233. @retval FALSE KeySize is not large enough.
  2234. @retval FALSE This interface is not supported.
  2235. **/
  2236. BOOLEAN
  2237. EFIAPI
  2238. DhComputeKey (
  2239. IN OUT VOID *DhContext,
  2240. IN CONST UINT8 *PeerPublicKey,
  2241. IN UINTN PeerPublicKeySize,
  2242. OUT UINT8 *Key,
  2243. IN OUT UINTN *KeySize
  2244. );
  2245. //=====================================================================================
  2246. // Pseudo-Random Generation Primitive
  2247. //=====================================================================================
  2248. /**
  2249. Sets up the seed value for the pseudorandom number generator.
  2250. This function sets up the seed value for the pseudorandom number generator.
  2251. If Seed is not NULL, then the seed passed in is used.
  2252. If Seed is NULL, then default seed is used.
  2253. If this interface is not supported, then return FALSE.
  2254. @param[in] Seed Pointer to seed value.
  2255. If NULL, default seed is used.
  2256. @param[in] SeedSize Size of seed value.
  2257. If Seed is NULL, this parameter is ignored.
  2258. @retval TRUE Pseudorandom number generator has enough entropy for random generation.
  2259. @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
  2260. @retval FALSE This interface is not supported.
  2261. **/
  2262. BOOLEAN
  2263. EFIAPI
  2264. RandomSeed (
  2265. IN CONST UINT8 *Seed OPTIONAL,
  2266. IN UINTN SeedSize
  2267. );
  2268. /**
  2269. Generates a pseudorandom byte stream of the specified size.
  2270. If Output is NULL, then return FALSE.
  2271. If this interface is not supported, then return FALSE.
  2272. @param[out] Output Pointer to buffer to receive random value.
  2273. @param[in] Size Size of random bytes to generate.
  2274. @retval TRUE Pseudorandom byte stream generated successfully.
  2275. @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
  2276. @retval FALSE This interface is not supported.
  2277. **/
  2278. BOOLEAN
  2279. EFIAPI
  2280. RandomBytes (
  2281. OUT UINT8 *Output,
  2282. IN UINTN Size
  2283. );
  2284. #endif // __BASE_CRYPT_LIB_H__