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.
7 Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #ifndef __BASE_CRYPT_LIB_H__
13 #define __BASE_CRYPT_LIB_H__
15 #include <Uefi/UefiBaseType.h>
18 /// MD5 digest size in bytes
20 #define MD5_DIGEST_SIZE 16
23 /// SHA-1 digest size in bytes.
25 #define SHA1_DIGEST_SIZE 20
28 /// SHA-256 digest size in bytes
30 #define SHA256_DIGEST_SIZE 32
33 /// SHA-384 digest size in bytes
35 #define SHA384_DIGEST_SIZE 48
38 /// SHA-512 digest size in bytes
40 #define SHA512_DIGEST_SIZE 64
43 /// SM3 digest size in bytes
45 #define SM3_256_DIGEST_SIZE 32
48 /// TDES block size in bytes
50 #define TDES_BLOCK_SIZE 8
53 /// AES block size in bytes
55 #define AES_BLOCK_SIZE 16
58 /// RSA Key Tags Definition used in RsaSetKey() function for key component identification.
61 RsaKeyN
, ///< RSA public Modulus (N)
62 RsaKeyE
, ///< RSA Public exponent (e)
63 RsaKeyD
, ///< RSA Private exponent (d)
64 RsaKeyP
, ///< RSA secret prime factor of Modulus (p)
65 RsaKeyQ
, ///< RSA secret prime factor of Modules (q)
66 RsaKeyDp
, ///< p's CRT exponent (== d mod (p - 1))
67 RsaKeyDq
, ///< q's CRT exponent (== d mod (q - 1))
68 RsaKeyQInv
///< The CRT coefficient (== 1/q mod p)
71 // =====================================================================================
72 // One-Way Cryptographic Hash Primitives
73 // =====================================================================================
75 #ifdef ENABLE_MD5_DEPRECATED_INTERFACES
78 Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
80 If this interface is not supported, then return zero.
82 @return The size, in bytes, of the context buffer required for MD5 hash operations.
83 @retval 0 This interface is not supported.
93 Initializes user-supplied memory pointed by Md5Context as MD5 hash context for
96 If Md5Context is NULL, then return FALSE.
97 If this interface is not supported, then return FALSE.
99 @param[out] Md5Context Pointer to MD5 context being initialized.
101 @retval TRUE MD5 context initialization succeeded.
102 @retval FALSE MD5 context initialization failed.
103 @retval FALSE This interface is not supported.
113 Makes a copy of an existing MD5 context.
115 If Md5Context is NULL, then return FALSE.
116 If NewMd5Context is NULL, then return FALSE.
117 If this interface is not supported, then return FALSE.
119 @param[in] Md5Context Pointer to MD5 context being copied.
120 @param[out] NewMd5Context Pointer to new MD5 context.
122 @retval TRUE MD5 context copy succeeded.
123 @retval FALSE MD5 context copy failed.
124 @retval FALSE This interface is not supported.
130 IN CONST VOID
*Md5Context
,
131 OUT VOID
*NewMd5Context
135 Digests the input data and updates MD5 context.
137 This function performs MD5 digest on a data buffer of the specified size.
138 It can be called multiple times to compute the digest of long or discontinuous data streams.
139 MD5 context should be already correctly initialized by Md5Init(), and should not be finalized
140 by Md5Final(). Behavior with invalid context is undefined.
142 If Md5Context is NULL, then return FALSE.
143 If this interface is not supported, then return FALSE.
145 @param[in, out] Md5Context Pointer to the MD5 context.
146 @param[in] Data Pointer to the buffer containing the data to be hashed.
147 @param[in] DataSize Size of Data buffer in bytes.
149 @retval TRUE MD5 data digest succeeded.
150 @retval FALSE MD5 data digest failed.
151 @retval FALSE This interface is not supported.
157 IN OUT VOID
*Md5Context
,
163 Completes computation of the MD5 digest value.
165 This function completes MD5 hash computation and retrieves the digest value into
166 the specified memory. After this function has been called, the MD5 context cannot
168 MD5 context should be already correctly initialized by Md5Init(), and should not be
169 finalized by Md5Final(). Behavior with invalid MD5 context is undefined.
171 If Md5Context is NULL, then return FALSE.
172 If HashValue is NULL, then return FALSE.
173 If this interface is not supported, then return FALSE.
175 @param[in, out] Md5Context Pointer to the MD5 context.
176 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
179 @retval TRUE MD5 digest computation succeeded.
180 @retval FALSE MD5 digest computation failed.
181 @retval FALSE This interface is not supported.
187 IN OUT VOID
*Md5Context
,
192 Computes the MD5 message digest of a input data buffer.
194 This function performs the MD5 message digest of a given data buffer, and places
195 the digest value into the specified memory.
197 If this interface is not supported, then return FALSE.
199 @param[in] Data Pointer to the buffer containing the data to be hashed.
200 @param[in] DataSize Size of Data buffer in bytes.
201 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
204 @retval TRUE MD5 digest computation succeeded.
205 @retval FALSE MD5 digest computation failed.
206 @retval FALSE This interface is not supported.
219 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
222 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
224 If this interface is not supported, then return zero.
226 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
227 @retval 0 This interface is not supported.
237 Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
240 If Sha1Context is NULL, then return FALSE.
241 If this interface is not supported, then return FALSE.
243 @param[out] Sha1Context Pointer to SHA-1 context being initialized.
245 @retval TRUE SHA-1 context initialization succeeded.
246 @retval FALSE SHA-1 context initialization failed.
247 @retval FALSE This interface is not supported.
253 OUT VOID
*Sha1Context
257 Makes a copy of an existing SHA-1 context.
259 If Sha1Context is NULL, then return FALSE.
260 If NewSha1Context is NULL, then return FALSE.
261 If this interface is not supported, then return FALSE.
263 @param[in] Sha1Context Pointer to SHA-1 context being copied.
264 @param[out] NewSha1Context Pointer to new SHA-1 context.
266 @retval TRUE SHA-1 context copy succeeded.
267 @retval FALSE SHA-1 context copy failed.
268 @retval FALSE This interface is not supported.
274 IN CONST VOID
*Sha1Context
,
275 OUT VOID
*NewSha1Context
279 Digests the input data and updates SHA-1 context.
281 This function performs SHA-1 digest on a data buffer of the specified size.
282 It can be called multiple times to compute the digest of long or discontinuous data streams.
283 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be finalized
284 by Sha1Final(). Behavior with invalid context is undefined.
286 If Sha1Context is NULL, then return FALSE.
287 If this interface is not supported, then return FALSE.
289 @param[in, out] Sha1Context Pointer to the SHA-1 context.
290 @param[in] Data Pointer to the buffer containing the data to be hashed.
291 @param[in] DataSize Size of Data buffer in bytes.
293 @retval TRUE SHA-1 data digest succeeded.
294 @retval FALSE SHA-1 data digest failed.
295 @retval FALSE This interface is not supported.
301 IN OUT VOID
*Sha1Context
,
307 Completes computation of the SHA-1 digest value.
309 This function completes SHA-1 hash computation and retrieves the digest value into
310 the specified memory. After this function has been called, the SHA-1 context cannot
312 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be
313 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
315 If Sha1Context is NULL, then return FALSE.
316 If HashValue is NULL, then return FALSE.
317 If this interface is not supported, then return FALSE.
319 @param[in, out] Sha1Context Pointer to the SHA-1 context.
320 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
323 @retval TRUE SHA-1 digest computation succeeded.
324 @retval FALSE SHA-1 digest computation failed.
325 @retval FALSE This interface is not supported.
331 IN OUT VOID
*Sha1Context
,
336 Computes the SHA-1 message digest of a input data buffer.
338 This function performs the SHA-1 message digest of a given data buffer, and places
339 the digest value into the specified memory.
341 If this interface is not supported, then return FALSE.
343 @param[in] Data Pointer to the buffer containing the data to be hashed.
344 @param[in] DataSize Size of Data buffer in bytes.
345 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
348 @retval TRUE SHA-1 digest computation succeeded.
349 @retval FALSE SHA-1 digest computation failed.
350 @retval FALSE This interface is not supported.
364 Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.
366 @return The size, in bytes, of the context buffer required for SHA-256 hash operations.
371 Sha256GetContextSize (
376 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
379 If Sha256Context is NULL, then return FALSE.
381 @param[out] Sha256Context Pointer to SHA-256 context being initialized.
383 @retval TRUE SHA-256 context initialization succeeded.
384 @retval FALSE SHA-256 context initialization failed.
390 OUT VOID
*Sha256Context
394 Makes a copy of an existing SHA-256 context.
396 If Sha256Context is NULL, then return FALSE.
397 If NewSha256Context is NULL, then return FALSE.
398 If this interface is not supported, then return FALSE.
400 @param[in] Sha256Context Pointer to SHA-256 context being copied.
401 @param[out] NewSha256Context Pointer to new SHA-256 context.
403 @retval TRUE SHA-256 context copy succeeded.
404 @retval FALSE SHA-256 context copy failed.
405 @retval FALSE This interface is not supported.
411 IN CONST VOID
*Sha256Context
,
412 OUT VOID
*NewSha256Context
416 Digests the input data and updates SHA-256 context.
418 This function performs SHA-256 digest on a data buffer of the specified size.
419 It can be called multiple times to compute the digest of long or discontinuous data streams.
420 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be finalized
421 by Sha256Final(). Behavior with invalid context is undefined.
423 If Sha256Context is NULL, then return FALSE.
425 @param[in, out] Sha256Context Pointer to the SHA-256 context.
426 @param[in] Data Pointer to the buffer containing the data to be hashed.
427 @param[in] DataSize Size of Data buffer in bytes.
429 @retval TRUE SHA-256 data digest succeeded.
430 @retval FALSE SHA-256 data digest failed.
436 IN OUT VOID
*Sha256Context
,
442 Completes computation of the SHA-256 digest value.
444 This function completes SHA-256 hash computation and retrieves the digest value into
445 the specified memory. After this function has been called, the SHA-256 context cannot
447 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be
448 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.
453 @param[in, out] Sha256Context Pointer to the SHA-256 context.
454 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
457 @retval TRUE SHA-256 digest computation succeeded.
458 @retval FALSE SHA-256 digest computation failed.
464 IN OUT VOID
*Sha256Context
,
469 Computes the SHA-256 message digest of a input data buffer.
471 This function performs the SHA-256 message digest of a given data buffer, and places
472 the digest value into the specified memory.
474 If this interface is not supported, then return FALSE.
476 @param[in] Data Pointer to the buffer containing the data to be hashed.
477 @param[in] DataSize Size of Data buffer in bytes.
478 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
481 @retval TRUE SHA-256 digest computation succeeded.
482 @retval FALSE SHA-256 digest computation failed.
483 @retval FALSE This interface is not supported.
495 Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.
497 @return The size, in bytes, of the context buffer required for SHA-384 hash operations.
502 Sha384GetContextSize (
507 Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for
510 If Sha384Context is NULL, then return FALSE.
512 @param[out] Sha384Context Pointer to SHA-384 context being initialized.
514 @retval TRUE SHA-384 context initialization succeeded.
515 @retval FALSE SHA-384 context initialization failed.
521 OUT VOID
*Sha384Context
525 Makes a copy of an existing SHA-384 context.
527 If Sha384Context is NULL, then return FALSE.
528 If NewSha384Context is NULL, then return FALSE.
529 If this interface is not supported, then return FALSE.
531 @param[in] Sha384Context Pointer to SHA-384 context being copied.
532 @param[out] NewSha384Context Pointer to new SHA-384 context.
534 @retval TRUE SHA-384 context copy succeeded.
535 @retval FALSE SHA-384 context copy failed.
536 @retval FALSE This interface is not supported.
542 IN CONST VOID
*Sha384Context
,
543 OUT VOID
*NewSha384Context
547 Digests the input data and updates SHA-384 context.
549 This function performs SHA-384 digest on a data buffer of the specified size.
550 It can be called multiple times to compute the digest of long or discontinuous data streams.
551 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be finalized
552 by Sha384Final(). Behavior with invalid context is undefined.
554 If Sha384Context is NULL, then return FALSE.
556 @param[in, out] Sha384Context Pointer to the SHA-384 context.
557 @param[in] Data Pointer to the buffer containing the data to be hashed.
558 @param[in] DataSize Size of Data buffer in bytes.
560 @retval TRUE SHA-384 data digest succeeded.
561 @retval FALSE SHA-384 data digest failed.
567 IN OUT VOID
*Sha384Context
,
573 Completes computation of the SHA-384 digest value.
575 This function completes SHA-384 hash computation and retrieves the digest value into
576 the specified memory. After this function has been called, the SHA-384 context cannot
578 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be
579 finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.
581 If Sha384Context is NULL, then return FALSE.
582 If HashValue is NULL, then return FALSE.
584 @param[in, out] Sha384Context Pointer to the SHA-384 context.
585 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
588 @retval TRUE SHA-384 digest computation succeeded.
589 @retval FALSE SHA-384 digest computation failed.
595 IN OUT VOID
*Sha384Context
,
600 Computes the SHA-384 message digest of a input data buffer.
602 This function performs the SHA-384 message digest of a given data buffer, and places
603 the digest value into the specified memory.
605 If this interface is not supported, then return FALSE.
607 @param[in] Data Pointer to the buffer containing the data to be hashed.
608 @param[in] DataSize Size of Data buffer in bytes.
609 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest
612 @retval TRUE SHA-384 digest computation succeeded.
613 @retval FALSE SHA-384 digest computation failed.
614 @retval FALSE This interface is not supported.
626 Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.
628 @return The size, in bytes, of the context buffer required for SHA-512 hash operations.
633 Sha512GetContextSize (
638 Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for
641 If Sha512Context is NULL, then return FALSE.
643 @param[out] Sha512Context Pointer to SHA-512 context being initialized.
645 @retval TRUE SHA-512 context initialization succeeded.
646 @retval FALSE SHA-512 context initialization failed.
652 OUT VOID
*Sha512Context
656 Makes a copy of an existing SHA-512 context.
658 If Sha512Context is NULL, then return FALSE.
659 If NewSha512Context is NULL, then return FALSE.
660 If this interface is not supported, then return FALSE.
662 @param[in] Sha512Context Pointer to SHA-512 context being copied.
663 @param[out] NewSha512Context Pointer to new SHA-512 context.
665 @retval TRUE SHA-512 context copy succeeded.
666 @retval FALSE SHA-512 context copy failed.
667 @retval FALSE This interface is not supported.
673 IN CONST VOID
*Sha512Context
,
674 OUT VOID
*NewSha512Context
678 Digests the input data and updates SHA-512 context.
680 This function performs SHA-512 digest on a data buffer of the specified size.
681 It can be called multiple times to compute the digest of long or discontinuous data streams.
682 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be finalized
683 by Sha512Final(). Behavior with invalid context is undefined.
685 If Sha512Context is NULL, then return FALSE.
687 @param[in, out] Sha512Context Pointer to the SHA-512 context.
688 @param[in] Data Pointer to the buffer containing the data to be hashed.
689 @param[in] DataSize Size of Data buffer in bytes.
691 @retval TRUE SHA-512 data digest succeeded.
692 @retval FALSE SHA-512 data digest failed.
698 IN OUT VOID
*Sha512Context
,
704 Completes computation of the SHA-512 digest value.
706 This function completes SHA-512 hash computation and retrieves the digest value into
707 the specified memory. After this function has been called, the SHA-512 context cannot
709 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be
710 finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.
712 If Sha512Context is NULL, then return FALSE.
713 If HashValue is NULL, then return FALSE.
715 @param[in, out] Sha512Context Pointer to the SHA-512 context.
716 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
719 @retval TRUE SHA-512 digest computation succeeded.
720 @retval FALSE SHA-512 digest computation failed.
726 IN OUT VOID
*Sha512Context
,
731 Computes the SHA-512 message digest of a input data buffer.
733 This function performs the SHA-512 message digest of a given data buffer, and places
734 the digest value into the specified memory.
736 If this interface is not supported, then return FALSE.
738 @param[in] Data Pointer to the buffer containing the data to be hashed.
739 @param[in] DataSize Size of Data buffer in bytes.
740 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest
743 @retval TRUE SHA-512 digest computation succeeded.
744 @retval FALSE SHA-512 digest computation failed.
745 @retval FALSE This interface is not supported.
757 Parallel hash function ParallelHash256, as defined in NIST's Special Publication 800-185,
758 published December 2016.
760 @param[in] Input Pointer to the input message (X).
761 @param[in] InputByteLen The number(>0) of input bytes provided for the input data.
762 @param[in] BlockSize The size of each block (B).
763 @param[out] Output Pointer to the output buffer.
764 @param[in] OutputByteLen The desired number of output bytes (L).
765 @param[in] Customization Pointer to the customization string (S).
766 @param[in] CustomByteLen The length of the customization string in bytes.
768 @retval TRUE ParallelHash256 digest computation succeeded.
769 @retval FALSE ParallelHash256 digest computation failed.
770 @retval FALSE This interface is not supported.
775 ParallelHash256HashAll (
776 IN CONST VOID
*Input
,
777 IN UINTN InputByteLen
,
780 IN UINTN OutputByteLen
,
781 IN CONST VOID
*Customization
,
782 IN UINTN CustomByteLen
786 Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.
788 @return The size, in bytes, of the context buffer required for SM3 hash operations.
798 Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for
801 If Sm3Context is NULL, then return FALSE.
803 @param[out] Sm3Context Pointer to SM3 context being initialized.
805 @retval TRUE SM3 context initialization succeeded.
806 @retval FALSE SM3 context initialization failed.
816 Makes a copy of an existing SM3 context.
818 If Sm3Context is NULL, then return FALSE.
819 If NewSm3Context is NULL, then return FALSE.
820 If this interface is not supported, then return FALSE.
822 @param[in] Sm3Context Pointer to SM3 context being copied.
823 @param[out] NewSm3Context Pointer to new SM3 context.
825 @retval TRUE SM3 context copy succeeded.
826 @retval FALSE SM3 context copy failed.
827 @retval FALSE This interface is not supported.
833 IN CONST VOID
*Sm3Context
,
834 OUT VOID
*NewSm3Context
838 Digests the input data and updates SM3 context.
840 This function performs SM3 digest on a data buffer of the specified size.
841 It can be called multiple times to compute the digest of long or discontinuous data streams.
842 SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized
843 by Sm3Final(). Behavior with invalid context is undefined.
845 If Sm3Context is NULL, then return FALSE.
847 @param[in, out] Sm3Context Pointer to the SM3 context.
848 @param[in] Data Pointer to the buffer containing the data to be hashed.
849 @param[in] DataSize Size of Data buffer in bytes.
851 @retval TRUE SM3 data digest succeeded.
852 @retval FALSE SM3 data digest failed.
858 IN OUT VOID
*Sm3Context
,
864 Completes computation of the SM3 digest value.
866 This function completes SM3 hash computation and retrieves the digest value into
867 the specified memory. After this function has been called, the SM3 context cannot
869 SM3 context should be already correctly initialized by Sm3Init(), and should not be
870 finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.
872 If Sm3Context is NULL, then return FALSE.
873 If HashValue is NULL, then return FALSE.
875 @param[in, out] Sm3Context Pointer to the SM3 context.
876 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
879 @retval TRUE SM3 digest computation succeeded.
880 @retval FALSE SM3 digest computation failed.
886 IN OUT VOID
*Sm3Context
,
891 Computes the SM3 message digest of a input data buffer.
893 This function performs the SM3 message digest of a given data buffer, and places
894 the digest value into the specified memory.
896 If this interface is not supported, then return FALSE.
898 @param[in] Data Pointer to the buffer containing the data to be hashed.
899 @param[in] DataSize Size of Data buffer in bytes.
900 @param[out] HashValue Pointer to a buffer that receives the SM3 digest
903 @retval TRUE SM3 digest computation succeeded.
904 @retval FALSE SM3 digest computation failed.
905 @retval FALSE This interface is not supported.
916 // =====================================================================================
917 // MAC (Message Authentication Code) Primitive
918 // =====================================================================================
921 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.
923 @return Pointer to the HMAC_CTX context that has been initialized.
924 If the allocations fails, HmacSha256New() returns NULL.
934 Release the specified HMAC_CTX context.
936 @param[in] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.
942 IN VOID
*HmacSha256Ctx
946 Set user-supplied key for subsequent use. It must be done before any
947 calling to HmacSha256Update().
949 If HmacSha256Context is NULL, then return FALSE.
950 If this interface is not supported, then return FALSE.
952 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.
953 @param[in] Key Pointer to the user-supplied key.
954 @param[in] KeySize Key size in bytes.
956 @retval TRUE The Key is set successfully.
957 @retval FALSE The Key is set unsuccessfully.
958 @retval FALSE This interface is not supported.
964 OUT VOID
*HmacSha256Context
,
970 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.
976 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.
977 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.
979 @retval TRUE HMAC-SHA256 context copy succeeded.
980 @retval FALSE HMAC-SHA256 context copy failed.
981 @retval FALSE This interface is not supported.
986 HmacSha256Duplicate (
987 IN CONST VOID
*HmacSha256Context
,
988 OUT VOID
*NewHmacSha256Context
992 Digests the input data and updates HMAC-SHA256 context.
994 This function performs HMAC-SHA256 digest on a data buffer of the specified size.
995 It can be called multiple times to compute the digest of long or discontinuous data streams.
996 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
997 by HmacSha256Final(). Behavior with invalid context is undefined.
999 If HmacSha256Context is NULL, then return FALSE.
1000 If this interface is not supported, then return FALSE.
1002 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1003 @param[in] Data Pointer to the buffer containing the data to be digested.
1004 @param[in] DataSize Size of Data buffer in bytes.
1006 @retval TRUE HMAC-SHA256 data digest succeeded.
1007 @retval FALSE HMAC-SHA256 data digest failed.
1008 @retval FALSE This interface is not supported.
1014 IN OUT VOID
*HmacSha256Context
,
1015 IN CONST VOID
*Data
,
1020 Completes computation of the HMAC-SHA256 digest value.
1022 This function completes HMAC-SHA256 hash computation and retrieves the digest value into
1023 the specified memory. After this function has been called, the HMAC-SHA256 context cannot
1025 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized
1026 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.
1028 If HmacSha256Context is NULL, then return FALSE.
1029 If HmacValue is NULL, then return FALSE.
1030 If this interface is not supported, then return FALSE.
1032 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.
1033 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest
1036 @retval TRUE HMAC-SHA256 digest computation succeeded.
1037 @retval FALSE HMAC-SHA256 digest computation failed.
1038 @retval FALSE This interface is not supported.
1044 IN OUT VOID
*HmacSha256Context
,
1045 OUT UINT8
*HmacValue
1048 // =====================================================================================
1049 // Symmetric Cryptography Primitive
1050 // =====================================================================================
1053 Retrieves the size, in bytes, of the context buffer required for AES operations.
1055 If this interface is not supported, then return zero.
1057 @return The size, in bytes, of the context buffer required for AES operations.
1058 @retval 0 This interface is not supported.
1068 Initializes user-supplied memory as AES context for subsequent use.
1070 This function initializes user-supplied memory pointed by AesContext as AES context.
1071 In addition, it sets up all AES key materials for subsequent encryption and decryption
1073 There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
1075 If AesContext is NULL, then return FALSE.
1076 If Key is NULL, then return FALSE.
1077 If KeyLength is not valid, then return FALSE.
1078 If this interface is not supported, then return FALSE.
1080 @param[out] AesContext Pointer to AES context being initialized.
1081 @param[in] Key Pointer to the user-supplied AES key.
1082 @param[in] KeyLength Length of AES key in bits.
1084 @retval TRUE AES context initialization succeeded.
1085 @retval FALSE AES context initialization failed.
1086 @retval FALSE This interface is not supported.
1092 OUT VOID
*AesContext
,
1093 IN CONST UINT8
*Key
,
1098 Performs AES encryption on a data buffer of the specified size in CBC mode.
1100 This function performs AES encryption on data buffer pointed by Input, of specified
1101 size of InputSize, in CBC mode.
1102 InputSize must be multiple of block size (16 bytes). This function does not perform
1103 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1104 Initialization vector should be one block size (16 bytes).
1105 AesContext should be already correctly initialized by AesInit(). Behavior with
1106 invalid AES context is undefined.
1108 If AesContext is NULL, then return FALSE.
1109 If Input is NULL, then return FALSE.
1110 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1111 If Ivec is NULL, then return FALSE.
1112 If Output is NULL, then return FALSE.
1113 If this interface is not supported, then return FALSE.
1115 @param[in] AesContext Pointer to the AES context.
1116 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1117 @param[in] InputSize Size of the Input buffer in bytes.
1118 @param[in] Ivec Pointer to initialization vector.
1119 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1121 @retval TRUE AES encryption succeeded.
1122 @retval FALSE AES encryption failed.
1123 @retval FALSE This interface is not supported.
1129 IN VOID
*AesContext
,
1130 IN CONST UINT8
*Input
,
1132 IN CONST UINT8
*Ivec
,
1137 Performs AES decryption on a data buffer of the specified size in CBC mode.
1139 This function performs AES decryption on data buffer pointed by Input, of specified
1140 size of InputSize, in CBC mode.
1141 InputSize must be multiple of block size (16 bytes). This function does not perform
1142 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1143 Initialization vector should be one block size (16 bytes).
1144 AesContext should be already correctly initialized by AesInit(). Behavior with
1145 invalid AES context is undefined.
1147 If AesContext is NULL, then return FALSE.
1148 If Input is NULL, then return FALSE.
1149 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1150 If Ivec is NULL, then return FALSE.
1151 If Output is NULL, then return FALSE.
1152 If this interface is not supported, then return FALSE.
1154 @param[in] AesContext Pointer to the AES context.
1155 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1156 @param[in] InputSize Size of the Input buffer in bytes.
1157 @param[in] Ivec Pointer to initialization vector.
1158 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1160 @retval TRUE AES decryption succeeded.
1161 @retval FALSE AES decryption failed.
1162 @retval FALSE This interface is not supported.
1168 IN VOID
*AesContext
,
1169 IN CONST UINT8
*Input
,
1171 IN CONST UINT8
*Ivec
,
1175 // =====================================================================================
1176 // Asymmetric Cryptography Primitive
1177 // =====================================================================================
1180 Allocates and initializes one RSA context for subsequent use.
1182 @return Pointer to the RSA context that has been initialized.
1183 If the allocations fails, RsaNew() returns NULL.
1193 Release the specified RSA context.
1195 If RsaContext is NULL, then return FALSE.
1197 @param[in] RsaContext Pointer to the RSA context to be released.
1207 Sets the tag-designated key component into the established RSA context.
1209 This function sets the tag-designated RSA key component into the established
1210 RSA context from the user-specified non-negative integer (octet string format
1211 represented in RSA PKCS#1).
1212 If BigNumber is NULL, then the specified key component in RSA context is cleared.
1214 If RsaContext is NULL, then return FALSE.
1216 @param[in, out] RsaContext Pointer to RSA context being set.
1217 @param[in] KeyTag Tag of RSA key component being set.
1218 @param[in] BigNumber Pointer to octet integer buffer.
1219 If NULL, then the specified key component in RSA
1221 @param[in] BnSize Size of big number buffer in bytes.
1222 If BigNumber is NULL, then it is ignored.
1224 @retval TRUE RSA key component was set successfully.
1225 @retval FALSE Invalid RSA key component tag.
1231 IN OUT VOID
*RsaContext
,
1232 IN RSA_KEY_TAG KeyTag
,
1233 IN CONST UINT8
*BigNumber
,
1238 Gets the tag-designated RSA key component from the established RSA context.
1240 This function retrieves the tag-designated RSA key component from the
1241 established RSA context as a non-negative integer (octet string format
1242 represented in RSA PKCS#1).
1243 If specified key component has not been set or has been cleared, then returned
1245 If the BigNumber buffer is too small to hold the contents of the key, FALSE
1246 is returned and BnSize is set to the required buffer size to obtain the key.
1248 If RsaContext is NULL, then return FALSE.
1249 If BnSize is NULL, then return FALSE.
1250 If BnSize is large enough but BigNumber is NULL, then return FALSE.
1251 If this interface is not supported, then return FALSE.
1253 @param[in, out] RsaContext Pointer to RSA context being set.
1254 @param[in] KeyTag Tag of RSA key component being set.
1255 @param[out] BigNumber Pointer to octet integer buffer.
1256 @param[in, out] BnSize On input, the size of big number buffer in bytes.
1257 On output, the size of data returned in big number buffer in bytes.
1259 @retval TRUE RSA key component was retrieved successfully.
1260 @retval FALSE Invalid RSA key component tag.
1261 @retval FALSE BnSize is too small.
1262 @retval FALSE This interface is not supported.
1268 IN OUT VOID
*RsaContext
,
1269 IN RSA_KEY_TAG KeyTag
,
1270 OUT UINT8
*BigNumber
,
1271 IN OUT UINTN
*BnSize
1275 Generates RSA key components.
1277 This function generates RSA key components. It takes RSA public exponent E and
1278 length in bits of RSA modulus N as input, and generates all key components.
1279 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
1281 Before this function can be invoked, pseudorandom number generator must be correctly
1282 initialized by RandomSeed().
1284 If RsaContext is NULL, then return FALSE.
1285 If this interface is not supported, then return FALSE.
1287 @param[in, out] RsaContext Pointer to RSA context being set.
1288 @param[in] ModulusLength Length of RSA modulus N in bits.
1289 @param[in] PublicExponent Pointer to RSA public exponent.
1290 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
1292 @retval TRUE RSA key component was generated successfully.
1293 @retval FALSE Invalid RSA key component tag.
1294 @retval FALSE This interface is not supported.
1300 IN OUT VOID
*RsaContext
,
1301 IN UINTN ModulusLength
,
1302 IN CONST UINT8
*PublicExponent
,
1303 IN UINTN PublicExponentSize
1307 Validates key components of RSA context.
1308 NOTE: This function performs integrity checks on all the RSA key material, so
1309 the RSA key structure must contain all the private key data.
1311 This function validates key components of RSA context in following aspects:
1312 - Whether p is a prime
1313 - Whether q is a prime
1315 - Whether d*e = 1 mod lcm(p-1,q-1)
1317 If RsaContext is NULL, then return FALSE.
1318 If this interface is not supported, then return FALSE.
1320 @param[in] RsaContext Pointer to RSA context to check.
1322 @retval TRUE RSA key components are valid.
1323 @retval FALSE RSA key components are not valid.
1324 @retval FALSE This interface is not supported.
1334 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
1336 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
1338 If the Signature buffer is too small to hold the contents of signature, FALSE
1339 is returned and SigSize is set to the required buffer size to obtain the signature.
1341 If RsaContext is NULL, then return FALSE.
1342 If MessageHash is NULL, then return FALSE.
1343 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
1344 If SigSize is large enough but Signature is NULL, then return FALSE.
1345 If this interface is not supported, then return FALSE.
1347 @param[in] RsaContext Pointer to RSA context for signature generation.
1348 @param[in] MessageHash Pointer to octet message hash to be signed.
1349 @param[in] HashSize Size of the message hash in bytes.
1350 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
1351 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1352 On output, the size of data returned in Signature buffer in bytes.
1354 @retval TRUE Signature successfully generated in PKCS1-v1_5.
1355 @retval FALSE Signature generation failed.
1356 @retval FALSE SigSize is too small.
1357 @retval FALSE This interface is not supported.
1363 IN VOID
*RsaContext
,
1364 IN CONST UINT8
*MessageHash
,
1366 OUT UINT8
*Signature
,
1367 IN OUT UINTN
*SigSize
1371 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
1374 If RsaContext is NULL, then return FALSE.
1375 If MessageHash is NULL, then return FALSE.
1376 If Signature is NULL, then return FALSE.
1377 If HashSize is not equal to the size of MD5, SHA-1, SHA-256 digest, then return FALSE.
1379 @param[in] RsaContext Pointer to RSA context for signature verification.
1380 @param[in] MessageHash Pointer to octet message hash to be checked.
1381 @param[in] HashSize Size of the message hash in bytes.
1382 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
1383 @param[in] SigSize Size of signature in bytes.
1385 @retval TRUE Valid signature encoded in PKCS1-v1_5.
1386 @retval FALSE Invalid signature or invalid RSA context.
1392 IN VOID
*RsaContext
,
1393 IN CONST UINT8
*MessageHash
,
1395 IN CONST UINT8
*Signature
,
1400 Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.
1402 This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in
1404 Mask generation function is the same as the message digest algorithm.
1405 If the Signature buffer is too small to hold the contents of signature, FALSE
1406 is returned and SigSize is set to the required buffer size to obtain the signature.
1408 If RsaContext is NULL, then return FALSE.
1409 If Message is NULL, then return FALSE.
1410 If MsgSize is zero or > INT_MAX, then return FALSE.
1411 If DigestLen is NOT 32, 48 or 64, return FALSE.
1412 If SaltLen is not equal to DigestLen, then return FALSE.
1413 If SigSize is large enough but Signature is NULL, then return FALSE.
1414 If this interface is not supported, then return FALSE.
1416 @param[in] RsaContext Pointer to RSA context for signature generation.
1417 @param[in] Message Pointer to octet message to be signed.
1418 @param[in] MsgSize Size of the message in bytes.
1419 @param[in] DigestLen Length of the digest in bytes to be used for RSA signature operation.
1420 @param[in] SaltLen Length of the salt in bytes to be used for PSS encoding.
1421 @param[out] Signature Pointer to buffer to receive RSA PSS signature.
1422 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1423 On output, the size of data returned in Signature buffer in bytes.
1425 @retval TRUE Signature successfully generated in RSASSA-PSS.
1426 @retval FALSE Signature generation failed.
1427 @retval FALSE SigSize is too small.
1428 @retval FALSE This interface is not supported.
1434 IN VOID
*RsaContext
,
1435 IN CONST UINT8
*Message
,
1437 IN UINT16 DigestLen
,
1439 OUT UINT8
*Signature
,
1440 IN OUT UINTN
*SigSize
1444 Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.
1445 Implementation determines salt length automatically from the signature encoding.
1446 Mask generation function is the same as the message digest algorithm.
1447 Salt length should be equal to digest length.
1449 @param[in] RsaContext Pointer to RSA context for signature verification.
1450 @param[in] Message Pointer to octet message to be verified.
1451 @param[in] MsgSize Size of the message in bytes.
1452 @param[in] Signature Pointer to RSASSA-PSS signature to be verified.
1453 @param[in] SigSize Size of signature in bytes.
1454 @param[in] DigestLen Length of digest for RSA operation.
1455 @param[in] SaltLen Salt length for PSS encoding.
1457 @retval TRUE Valid signature encoded in RSASSA-PSS.
1458 @retval FALSE Invalid signature or invalid RSA context.
1464 IN VOID
*RsaContext
,
1465 IN CONST UINT8
*Message
,
1467 IN CONST UINT8
*Signature
,
1469 IN UINT16 DigestLen
,
1474 Retrieve the RSA Private Key from the password-protected PEM key data.
1476 If PemData is NULL, then return FALSE.
1477 If RsaContext is NULL, then return FALSE.
1478 If this interface is not supported, then return FALSE.
1480 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
1481 @param[in] PemSize Size of the PEM key data in bytes.
1482 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
1483 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1484 RSA private key component. Use RsaFree() function to free the
1487 @retval TRUE RSA Private Key was retrieved successfully.
1488 @retval FALSE Invalid PEM key data or incorrect password.
1489 @retval FALSE This interface is not supported.
1494 RsaGetPrivateKeyFromPem (
1495 IN CONST UINT8
*PemData
,
1497 IN CONST CHAR8
*Password
,
1498 OUT VOID
**RsaContext
1502 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
1504 If Cert is NULL, then return FALSE.
1505 If RsaContext is NULL, then return FALSE.
1506 If this interface is not supported, then return FALSE.
1508 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1509 @param[in] CertSize Size of the X509 certificate in bytes.
1510 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1511 RSA public key component. Use RsaFree() function to free the
1514 @retval TRUE RSA Public Key was retrieved successfully.
1515 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
1516 @retval FALSE This interface is not supported.
1521 RsaGetPublicKeyFromX509 (
1522 IN CONST UINT8
*Cert
,
1524 OUT VOID
**RsaContext
1528 Retrieve the subject bytes from one X.509 certificate.
1530 If Cert is NULL, then return FALSE.
1531 If SubjectSize is NULL, then return FALSE.
1532 If this interface is not supported, then return FALSE.
1534 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1535 @param[in] CertSize Size of the X509 certificate in bytes.
1536 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
1537 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
1538 and the size of buffer returned CertSubject on output.
1540 @retval TRUE The certificate subject retrieved successfully.
1541 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
1542 The SubjectSize will be updated with the required size.
1543 @retval FALSE This interface is not supported.
1548 X509GetSubjectName (
1549 IN CONST UINT8
*Cert
,
1551 OUT UINT8
*CertSubject
,
1552 IN OUT UINTN
*SubjectSize
1556 Retrieve the common name (CN) string from one X.509 certificate.
1558 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1559 @param[in] CertSize Size of the X509 certificate in bytes.
1560 @param[out] CommonName Buffer to contain the retrieved certificate common
1561 name string (UTF8). At most CommonNameSize bytes will be
1562 written and the string will be null terminated. May be
1563 NULL in order to determine the size buffer needed.
1564 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
1565 and the size of buffer returned CommonName on output.
1566 If CommonName is NULL then the amount of space needed
1567 in buffer (including the final null) is returned.
1569 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
1570 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1571 If CommonNameSize is NULL.
1572 If CommonName is not NULL and *CommonNameSize is 0.
1573 If Certificate is invalid.
1574 @retval RETURN_NOT_FOUND If no CommonName entry exists.
1575 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
1576 (including the final null) is returned in the
1577 CommonNameSize parameter.
1578 @retval RETURN_UNSUPPORTED The operation is not supported.
1584 IN CONST UINT8
*Cert
,
1586 OUT CHAR8
*CommonName OPTIONAL
,
1587 IN OUT UINTN
*CommonNameSize
1591 Retrieve the organization name (O) string from one X.509 certificate.
1593 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1594 @param[in] CertSize Size of the X509 certificate in bytes.
1595 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
1596 name string. At most NameBufferSize bytes will be
1597 written and the string will be null terminated. May be
1598 NULL in order to determine the size buffer needed.
1599 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
1600 and the size of buffer returned Name on output.
1601 If NameBuffer is NULL then the amount of space needed
1602 in buffer (including the final null) is returned.
1604 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
1605 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1606 If NameBufferSize is NULL.
1607 If NameBuffer is not NULL and *CommonNameSize is 0.
1608 If Certificate is invalid.
1609 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
1610 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
1611 (including the final null) is returned in the
1612 CommonNameSize parameter.
1613 @retval RETURN_UNSUPPORTED The operation is not supported.
1618 X509GetOrganizationName (
1619 IN CONST UINT8
*Cert
,
1621 OUT CHAR8
*NameBuffer OPTIONAL
,
1622 IN OUT UINTN
*NameBufferSize
1626 Verify one X509 certificate was issued by the trusted CA.
1628 If Cert is NULL, then return FALSE.
1629 If CACert is NULL, then return FALSE.
1630 If this interface is not supported, then return FALSE.
1632 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
1633 @param[in] CertSize Size of the X509 certificate in bytes.
1634 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
1635 @param[in] CACertSize Size of the CA Certificate in bytes.
1637 @retval TRUE The certificate was issued by the trusted CA.
1638 @retval FALSE Invalid certificate or the certificate was not issued by the given
1640 @retval FALSE This interface is not supported.
1646 IN CONST UINT8
*Cert
,
1648 IN CONST UINT8
*CACert
,
1653 Construct a X509 object from DER-encoded certificate data.
1655 If Cert is NULL, then return FALSE.
1656 If SingleX509Cert is NULL, then return FALSE.
1657 If this interface is not supported, then return FALSE.
1659 @param[in] Cert Pointer to the DER-encoded certificate data.
1660 @param[in] CertSize The size of certificate data in bytes.
1661 @param[out] SingleX509Cert The generated X509 object.
1663 @retval TRUE The X509 object generation succeeded.
1664 @retval FALSE The operation failed.
1665 @retval FALSE This interface is not supported.
1670 X509ConstructCertificate (
1671 IN CONST UINT8
*Cert
,
1673 OUT UINT8
**SingleX509Cert
1677 Construct a X509 stack object from a list of DER-encoded certificate data.
1679 If X509Stack is NULL, then return FALSE.
1680 If this interface is not supported, then return FALSE.
1682 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1683 On output, pointer to the X509 stack object with new
1684 inserted X509 certificate.
1685 @param[in] Args VA_LIST marker for the variable argument list.
1686 A list of DER-encoded single certificate data followed
1687 by certificate size. A NULL terminates the list. The
1688 pairs are the arguments to X509ConstructCertificate().
1690 @retval TRUE The X509 stack construction succeeded.
1691 @retval FALSE The construction operation failed.
1692 @retval FALSE This interface is not supported.
1697 X509ConstructCertificateStackV (
1698 IN OUT UINT8
**X509Stack
,
1703 Construct a X509 stack object from a list of DER-encoded certificate data.
1705 If X509Stack is NULL, then return FALSE.
1706 If this interface is not supported, then return FALSE.
1708 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1709 On output, pointer to the X509 stack object with new
1710 inserted X509 certificate.
1711 @param ... A list of DER-encoded single certificate data followed
1712 by certificate size. A NULL terminates the list. The
1713 pairs are the arguments to X509ConstructCertificate().
1715 @retval TRUE The X509 stack construction succeeded.
1716 @retval FALSE The construction operation failed.
1717 @retval FALSE This interface is not supported.
1722 X509ConstructCertificateStack (
1723 IN OUT UINT8
**X509Stack
,
1728 Release the specified X509 object.
1730 If the interface is not supported, then ASSERT().
1732 @param[in] X509Cert Pointer to the X509 object to be released.
1742 Release the specified X509 stack object.
1744 If the interface is not supported, then ASSERT().
1746 @param[in] X509Stack Pointer to the X509 stack object to be released.
1756 Retrieve the TBSCertificate from one given X.509 certificate.
1758 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
1759 @param[in] CertSize Size of the X509 certificate in bytes.
1760 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
1761 @param[out] TBSCertSize Size of the TBS certificate in bytes.
1763 If Cert is NULL, then return FALSE.
1764 If TBSCert is NULL, then return FALSE.
1765 If TBSCertSize is NULL, then return FALSE.
1766 If this interface is not supported, then return FALSE.
1768 @retval TRUE The TBSCertificate was retrieved successfully.
1769 @retval FALSE Invalid X.509 certificate.
1775 IN CONST UINT8
*Cert
,
1777 OUT UINT8
**TBSCert
,
1778 OUT UINTN
*TBSCertSize
1782 Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0
1783 password based encryption key derivation function PBKDF2, as specified in RFC 2898.
1785 If Password or Salt or OutKey is NULL, then return FALSE.
1786 If the hash algorithm could not be determined, then return FALSE.
1787 If this interface is not supported, then return FALSE.
1789 @param[in] PasswordLength Length of input password in bytes.
1790 @param[in] Password Pointer to the array for the password.
1791 @param[in] SaltLength Size of the Salt in bytes.
1792 @param[in] Salt Pointer to the Salt.
1793 @param[in] IterationCount Number of iterations to perform. Its value should be
1794 greater than or equal to 1.
1795 @param[in] DigestSize Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).
1796 NOTE: DigestSize will be used to determine the hash algorithm.
1797 Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.
1798 @param[in] KeyLength Size of the derived key buffer in bytes.
1799 @param[out] OutKey Pointer to the output derived key buffer.
1801 @retval TRUE A key was derived successfully.
1802 @retval FALSE One of the pointers was NULL or one of the sizes was too large.
1803 @retval FALSE The hash algorithm could not be determined from the digest size.
1804 @retval FALSE The key derivation operation failed.
1805 @retval FALSE This interface is not supported.
1811 IN UINTN PasswordLength
,
1812 IN CONST CHAR8
*Password
,
1813 IN UINTN SaltLength
,
1814 IN CONST UINT8
*Salt
,
1815 IN UINTN IterationCount
,
1816 IN UINTN DigestSize
,
1822 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
1823 encrypted message in a newly allocated buffer.
1825 Things that can cause a failure include:
1826 - X509 key size does not match any known key size.
1827 - Fail to parse X509 certificate.
1828 - Fail to allocate an intermediate buffer.
1829 - Null pointer provided for a non-optional parameter.
1830 - Data size is too large for the provided key size (max size is a function of key size
1831 and hash digest size).
1833 @param[in] PublicKey A pointer to the DER-encoded X509 certificate that
1834 will be used to encrypt the data.
1835 @param[in] PublicKeySize Size of the X509 cert buffer.
1836 @param[in] InData Data to be encrypted.
1837 @param[in] InDataSize Size of the data buffer.
1838 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer
1839 to be used when initializing the PRNG. NULL otherwise.
1840 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer.
1842 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted
1844 @param[out] EncryptedDataSize Size of the encrypted message buffer.
1846 @retval TRUE Encryption was successful.
1847 @retval FALSE Encryption failed.
1853 IN CONST UINT8
*PublicKey
,
1854 IN UINTN PublicKeySize
,
1856 IN UINTN InDataSize
,
1857 IN CONST UINT8
*PrngSeed OPTIONAL
,
1858 IN UINTN PrngSeedSize OPTIONAL
,
1859 OUT UINT8
**EncryptedData
,
1860 OUT UINTN
*EncryptedDataSize
1864 The 3rd parameter of Pkcs7GetSigners will return all embedded
1865 X.509 certificate in one given PKCS7 signature. The format is:
1867 // UINT8 CertNumber;
1868 // UINT32 Cert1Length;
1870 // UINT32 Cert2Length;
1873 // UINT32 CertnLength;
1877 The two following C-structure are used for parsing CertStack more clearly.
1882 UINT32 CertDataLength
; // The length in bytes of X.509 certificate.
1883 UINT8 CertDataBuffer
[0]; // The X.509 certificate content (DER).
1887 UINT8 CertNumber
; // Number of X.509 certificate.
1888 // EFI_CERT_DATA CertArray[]; // An array of X.509 certificate.
1894 Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
1895 Cryptographic Message Syntax Standard". The input signed data could be wrapped
1896 in a ContentInfo structure.
1898 If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
1899 return FALSE. If P7Length overflow, then return FALSE.
1900 If this interface is not supported, then return FALSE.
1902 @param[in] P7Data Pointer to the PKCS#7 message to verify.
1903 @param[in] P7Length Length of the PKCS#7 message in bytes.
1904 @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
1905 It's caller's responsibility to free the buffer with
1907 This data structure is EFI_CERT_STACK type.
1908 @param[out] StackLength Length of signer's certificates in bytes.
1909 @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
1910 It's caller's responsibility to free the buffer with
1912 @param[out] CertLength Length of the trusted certificate in bytes.
1914 @retval TRUE The operation is finished successfully.
1915 @retval FALSE Error occurs during the operation.
1916 @retval FALSE This interface is not supported.
1922 IN CONST UINT8
*P7Data
,
1924 OUT UINT8
**CertStack
,
1925 OUT UINTN
*StackLength
,
1926 OUT UINT8
**TrustedCert
,
1927 OUT UINTN
*CertLength
1931 Wrap function to use free() to free allocated memory for certificates.
1933 If this interface is not supported, then ASSERT().
1935 @param[in] Certs Pointer to the certificates to be freed.
1945 Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
1946 Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
1947 unchained to the signer's certificates.
1948 The input signed data could be wrapped in a ContentInfo structure.
1950 @param[in] P7Data Pointer to the PKCS#7 message.
1951 @param[in] P7Length Length of the PKCS#7 message in bytes.
1952 @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
1953 certificate. It's caller's responsibility to free the buffer
1954 with Pkcs7FreeSigners().
1955 This data structure is EFI_CERT_STACK type.
1956 @param[out] ChainLength Length of the chained certificates list buffer in bytes.
1957 @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
1958 responsibility to free the buffer with Pkcs7FreeSigners().
1959 This data structure is EFI_CERT_STACK type.
1960 @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
1962 @retval TRUE The operation is finished successfully.
1963 @retval FALSE Error occurs during the operation.
1968 Pkcs7GetCertificatesList (
1969 IN CONST UINT8
*P7Data
,
1971 OUT UINT8
**SignerChainCerts
,
1972 OUT UINTN
*ChainLength
,
1973 OUT UINT8
**UnchainCerts
,
1974 OUT UINTN
*UnchainLength
1978 Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
1979 Syntax Standard, version 1.5". This interface is only intended to be used for
1980 application to perform PKCS#7 functionality validation.
1982 If this interface is not supported, then return FALSE.
1984 @param[in] PrivateKey Pointer to the PEM-formatted private key data for
1986 @param[in] PrivateKeySize Size of the PEM private key data in bytes.
1987 @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
1989 @param[in] InData Pointer to the content to be signed.
1990 @param[in] InDataSize Size of InData in bytes.
1991 @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
1992 @param[in] OtherCerts Pointer to an optional additional set of certificates to
1993 include in the PKCS#7 signedData (e.g. any intermediate
1995 @param[out] SignedData Pointer to output PKCS#7 signedData. It's caller's
1996 responsibility to free the buffer with FreePool().
1997 @param[out] SignedDataSize Size of SignedData in bytes.
1999 @retval TRUE PKCS#7 data signing succeeded.
2000 @retval FALSE PKCS#7 data signing failed.
2001 @retval FALSE This interface is not supported.
2007 IN CONST UINT8
*PrivateKey
,
2008 IN UINTN PrivateKeySize
,
2009 IN CONST UINT8
*KeyPassword
,
2011 IN UINTN InDataSize
,
2013 IN UINT8
*OtherCerts OPTIONAL
,
2014 OUT UINT8
**SignedData
,
2015 OUT UINTN
*SignedDataSize
2019 Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
2020 Cryptographic Message Syntax Standard". The input signed data could be wrapped
2021 in a ContentInfo structure.
2023 If P7Data, TrustedCert or InData is NULL, then return FALSE.
2024 If P7Length, CertLength or DataLength overflow, then return FALSE.
2025 If this interface is not supported, then return FALSE.
2027 @param[in] P7Data Pointer to the PKCS#7 message to verify.
2028 @param[in] P7Length Length of the PKCS#7 message in bytes.
2029 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2030 is used for certificate chain verification.
2031 @param[in] CertLength Length of the trusted certificate in bytes.
2032 @param[in] InData Pointer to the content to be verified.
2033 @param[in] DataLength Length of InData in bytes.
2035 @retval TRUE The specified PKCS#7 signed data is valid.
2036 @retval FALSE Invalid PKCS#7 signed data.
2037 @retval FALSE This interface is not supported.
2043 IN CONST UINT8
*P7Data
,
2045 IN CONST UINT8
*TrustedCert
,
2046 IN UINTN CertLength
,
2047 IN CONST UINT8
*InData
,
2052 This function receives a PKCS7 formatted signature, and then verifies that
2053 the specified Enhanced or Extended Key Usages (EKU's) are present in the end-entity
2054 leaf signing certificate.
2055 Note that this function does not validate the certificate chain.
2057 Applications for custom EKU's are quite flexible. For example, a policy EKU
2058 may be present in an Issuing Certificate Authority (CA), and any sub-ordinate
2059 certificate issued might also contain this EKU, thus constraining the
2060 sub-ordinate certificate. Other applications might allow a certificate
2061 embedded in a device to specify that other Object Identifiers (OIDs) are
2062 present which contains binary data specifying custom capabilities that
2063 the device is able to do.
2065 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
2066 containing the content block with both the signature,
2067 the signer's certificate, and any necessary intermediate
2069 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
2070 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
2071 required EKUs that must be present in the signature.
2072 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
2073 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
2074 must be present in the leaf signer. If it is
2075 FALSE, then we will succeed if we find any
2076 of the specified EKU's.
2078 @retval EFI_SUCCESS The required EKUs were found in the signature.
2079 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2080 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
2085 VerifyEKUsInPkcs7Signature (
2086 IN CONST UINT8
*Pkcs7Signature
,
2087 IN CONST UINT32 SignatureSize
,
2088 IN CONST CHAR8
*RequiredEKUs
[],
2089 IN CONST UINT32 RequiredEKUsSize
,
2090 IN BOOLEAN RequireAllPresent
2094 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
2095 data could be wrapped in a ContentInfo structure.
2097 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
2098 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
2100 Caution: This function may receive untrusted input. So this function will do
2101 basic check for PKCS#7 data structure.
2103 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
2104 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
2105 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
2106 It's caller's responsibility to free the buffer with FreePool().
2107 @param[out] ContentSize The size of the extracted content in bytes.
2109 @retval TRUE The P7Data was correctly formatted for processing.
2110 @retval FALSE The P7Data was not correctly formatted for processing.
2115 Pkcs7GetAttachedContent (
2116 IN CONST UINT8
*P7Data
,
2119 OUT UINTN
*ContentSize
2123 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
2124 Authenticode Portable Executable Signature Format".
2126 If AuthData is NULL, then return FALSE.
2127 If ImageHash is NULL, then return FALSE.
2128 If this interface is not supported, then return FALSE.
2130 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2131 PE/COFF image to be verified.
2132 @param[in] DataSize Size of the Authenticode Signature in bytes.
2133 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2134 is used for certificate chain verification.
2135 @param[in] CertSize Size of the trusted certificate in bytes.
2136 @param[in] ImageHash Pointer to the original image file hash value. The procedure
2137 for calculating the image hash value is described in Authenticode
2139 @param[in] HashSize Size of Image hash value in bytes.
2141 @retval TRUE The specified Authenticode Signature is valid.
2142 @retval FALSE Invalid Authenticode Signature.
2143 @retval FALSE This interface is not supported.
2148 AuthenticodeVerify (
2149 IN CONST UINT8
*AuthData
,
2151 IN CONST UINT8
*TrustedCert
,
2153 IN CONST UINT8
*ImageHash
,
2158 Verifies the validity of a RFC3161 Timestamp CounterSignature embedded in PE/COFF Authenticode
2161 If AuthData is NULL, then return FALSE.
2162 If this interface is not supported, then return FALSE.
2164 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2165 PE/COFF image to be verified.
2166 @param[in] DataSize Size of the Authenticode Signature in bytes.
2167 @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER, which
2168 is used for TSA certificate chain verification.
2169 @param[in] CertSize Size of the trusted certificate in bytes.
2170 @param[out] SigningTime Return the time of timestamp generation time if the timestamp
2173 @retval TRUE The specified Authenticode includes a valid RFC3161 Timestamp CounterSignature.
2174 @retval FALSE No valid RFC3161 Timestamp CounterSignature in the specified Authenticode data.
2179 ImageTimestampVerify (
2180 IN CONST UINT8
*AuthData
,
2182 IN CONST UINT8
*TsaCert
,
2184 OUT EFI_TIME
*SigningTime
2187 // =====================================================================================
2188 // DH Key Exchange Primitive
2189 // =====================================================================================
2192 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
2194 @return Pointer to the Diffie-Hellman Context that has been initialized.
2195 If the allocations fails, DhNew() returns NULL.
2196 If the interface is not supported, DhNew() returns NULL.
2206 Release the specified DH context.
2208 If the interface is not supported, then ASSERT().
2210 @param[in] DhContext Pointer to the DH context to be released.
2220 Generates DH parameter.
2222 Given generator g, and length of prime number p in bits, this function generates p,
2223 and sets DH context according to value of g and p.
2225 Before this function can be invoked, pseudorandom number generator must be correctly
2226 initialized by RandomSeed().
2228 If DhContext is NULL, then return FALSE.
2229 If Prime is NULL, then return FALSE.
2230 If this interface is not supported, then return FALSE.
2232 @param[in, out] DhContext Pointer to the DH context.
2233 @param[in] Generator Value of generator.
2234 @param[in] PrimeLength Length in bits of prime to be generated.
2235 @param[out] Prime Pointer to the buffer to receive the generated prime number.
2237 @retval TRUE DH parameter generation succeeded.
2238 @retval FALSE Value of Generator is not supported.
2239 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
2240 @retval FALSE This interface is not supported.
2245 DhGenerateParameter (
2246 IN OUT VOID
*DhContext
,
2248 IN UINTN PrimeLength
,
2253 Sets generator and prime parameters for DH.
2255 Given generator g, and prime number p, this function and sets DH
2256 context accordingly.
2258 If DhContext is NULL, then return FALSE.
2259 If Prime is NULL, then return FALSE.
2260 If this interface is not supported, then return FALSE.
2262 @param[in, out] DhContext Pointer to the DH context.
2263 @param[in] Generator Value of generator.
2264 @param[in] PrimeLength Length in bits of prime to be generated.
2265 @param[in] Prime Pointer to the prime number.
2267 @retval TRUE DH parameter setting succeeded.
2268 @retval FALSE Value of Generator is not supported.
2269 @retval FALSE Value of Generator is not suitable for the Prime.
2270 @retval FALSE Value of Prime is not a prime number.
2271 @retval FALSE Value of Prime is not a safe prime number.
2272 @retval FALSE This interface is not supported.
2278 IN OUT VOID
*DhContext
,
2280 IN UINTN PrimeLength
,
2281 IN CONST UINT8
*Prime
2285 Generates DH public key.
2287 This function generates random secret exponent, and computes the public key, which is
2288 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
2289 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
2290 PublicKeySize is set to the required buffer size to obtain the public key.
2292 If DhContext is NULL, then return FALSE.
2293 If PublicKeySize is NULL, then return FALSE.
2294 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
2295 If this interface is not supported, then return FALSE.
2297 @param[in, out] DhContext Pointer to the DH context.
2298 @param[out] PublicKey Pointer to the buffer to receive generated public key.
2299 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
2300 On output, the size of data returned in PublicKey buffer in bytes.
2302 @retval TRUE DH public key generation succeeded.
2303 @retval FALSE DH public key generation failed.
2304 @retval FALSE PublicKeySize is not large enough.
2305 @retval FALSE This interface is not supported.
2311 IN OUT VOID
*DhContext
,
2312 OUT UINT8
*PublicKey
,
2313 IN OUT UINTN
*PublicKeySize
2317 Computes exchanged common key.
2319 Given peer's public key, this function computes the exchanged common key, based on its own
2320 context including value of prime modulus and random secret exponent.
2322 If DhContext is NULL, then return FALSE.
2323 If PeerPublicKey is NULL, then return FALSE.
2324 If KeySize is NULL, then return FALSE.
2325 If Key is NULL, then return FALSE.
2326 If KeySize is not large enough, then return FALSE.
2327 If this interface is not supported, then return FALSE.
2329 @param[in, out] DhContext Pointer to the DH context.
2330 @param[in] PeerPublicKey Pointer to the peer's public key.
2331 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
2332 @param[out] Key Pointer to the buffer to receive generated key.
2333 @param[in, out] KeySize On input, the size of Key buffer in bytes.
2334 On output, the size of data returned in Key buffer in bytes.
2336 @retval TRUE DH exchanged key generation succeeded.
2337 @retval FALSE DH exchanged key generation failed.
2338 @retval FALSE KeySize is not large enough.
2339 @retval FALSE This interface is not supported.
2345 IN OUT VOID
*DhContext
,
2346 IN CONST UINT8
*PeerPublicKey
,
2347 IN UINTN PeerPublicKeySize
,
2349 IN OUT UINTN
*KeySize
2352 // =====================================================================================
2353 // Pseudo-Random Generation Primitive
2354 // =====================================================================================
2357 Sets up the seed value for the pseudorandom number generator.
2359 This function sets up the seed value for the pseudorandom number generator.
2360 If Seed is not NULL, then the seed passed in is used.
2361 If Seed is NULL, then default seed is used.
2362 If this interface is not supported, then return FALSE.
2364 @param[in] Seed Pointer to seed value.
2365 If NULL, default seed is used.
2366 @param[in] SeedSize Size of seed value.
2367 If Seed is NULL, this parameter is ignored.
2369 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
2370 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
2371 @retval FALSE This interface is not supported.
2377 IN CONST UINT8
*Seed OPTIONAL
,
2382 Generates a pseudorandom byte stream of the specified size.
2384 If Output is NULL, then return FALSE.
2385 If this interface is not supported, then return FALSE.
2387 @param[out] Output Pointer to buffer to receive random value.
2388 @param[in] Size Size of random bytes to generate.
2390 @retval TRUE Pseudorandom byte stream generated successfully.
2391 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
2392 @retval FALSE This interface is not supported.
2402 // =====================================================================================
2403 // Key Derivation Function Primitive
2404 // =====================================================================================
2407 Derive key data using HMAC-SHA256 based KDF.
2409 @param[in] Key Pointer to the user-supplied key.
2410 @param[in] KeySize Key size in bytes.
2411 @param[in] Salt Pointer to the salt(non-secret) value.
2412 @param[in] SaltSize Salt size in bytes.
2413 @param[in] Info Pointer to the application specific info.
2414 @param[in] InfoSize Info size in bytes.
2415 @param[out] Out Pointer to buffer to receive hkdf value.
2416 @param[in] OutSize Size of hkdf bytes to generate.
2418 @retval TRUE Hkdf generated successfully.
2419 @retval FALSE Hkdf generation failed.
2424 HkdfSha256ExtractAndExpand (
2425 IN CONST UINT8
*Key
,
2427 IN CONST UINT8
*Salt
,
2429 IN CONST UINT8
*Info
,
2435 #endif // __BASE_CRYPT_LIB_H__