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