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
1049 Computes the HMAC-SHA256 digest of a input data buffer.
1051 This function performs the HMAC-SHA256 digest of a given data buffer, and places
1052 the digest value into the specified memory.
1054 If this interface is not supported, then return FALSE.
1056 @param[in] Data Pointer to the buffer containing the data to be digested.
1057 @param[in] DataSize Size of Data buffer in bytes.
1058 @param[in] Key Pointer to the user-supplied key.
1059 @param[in] KeySize Key size in bytes.
1060 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA256 digest
1063 @retval TRUE HMAC-SHA256 digest computation succeeded.
1064 @retval FALSE HMAC-SHA256 digest computation failed.
1065 @retval FALSE This interface is not supported.
1071 IN CONST VOID
*Data
,
1073 IN CONST UINT8
*Key
,
1075 OUT UINT8
*HmacValue
1079 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA384 use.
1081 @return Pointer to the HMAC_CTX context that has been initialized.
1082 If the allocations fails, HmacSha384New() returns NULL.
1092 Release the specified HMAC_CTX context.
1094 @param[in] HmacSha384Ctx Pointer to the HMAC_CTX context to be released.
1100 IN VOID
*HmacSha384Ctx
1104 Set user-supplied key for subsequent use. It must be done before any
1105 calling to HmacSha384Update().
1107 If HmacSha384Context is NULL, then return FALSE.
1108 If this interface is not supported, then return FALSE.
1110 @param[out] HmacSha384Context Pointer to HMAC-SHA384 context.
1111 @param[in] Key Pointer to the user-supplied key.
1112 @param[in] KeySize Key size in bytes.
1114 @retval TRUE The Key is set successfully.
1115 @retval FALSE The Key is set unsuccessfully.
1116 @retval FALSE This interface is not supported.
1122 OUT VOID
*HmacSha384Context
,
1123 IN CONST UINT8
*Key
,
1128 Makes a copy of an existing HMAC-SHA384 context.
1130 If HmacSha384Context is NULL, then return FALSE.
1131 If NewHmacSha384Context is NULL, then return FALSE.
1132 If this interface is not supported, then return FALSE.
1134 @param[in] HmacSha384Context Pointer to HMAC-SHA384 context being copied.
1135 @param[out] NewHmacSha384Context Pointer to new HMAC-SHA384 context.
1137 @retval TRUE HMAC-SHA384 context copy succeeded.
1138 @retval FALSE HMAC-SHA384 context copy failed.
1139 @retval FALSE This interface is not supported.
1144 HmacSha384Duplicate (
1145 IN CONST VOID
*HmacSha384Context
,
1146 OUT VOID
*NewHmacSha384Context
1150 Digests the input data and updates HMAC-SHA384 context.
1152 This function performs HMAC-SHA384 digest on a data buffer of the specified size.
1153 It can be called multiple times to compute the digest of long or discontinuous data streams.
1154 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
1155 by HmacSha384Final(). Behavior with invalid context is undefined.
1157 If HmacSha384Context is NULL, then return FALSE.
1158 If this interface is not supported, then return FALSE.
1160 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
1161 @param[in] Data Pointer to the buffer containing the data to be digested.
1162 @param[in] DataSize Size of Data buffer in bytes.
1164 @retval TRUE HMAC-SHA384 data digest succeeded.
1165 @retval FALSE HMAC-SHA384 data digest failed.
1166 @retval FALSE This interface is not supported.
1172 IN OUT VOID
*HmacSha384Context
,
1173 IN CONST VOID
*Data
,
1178 Completes computation of the HMAC-SHA384 digest value.
1180 This function completes HMAC-SHA384 hash computation and retrieves the digest value into
1181 the specified memory. After this function has been called, the HMAC-SHA384 context cannot
1183 HMAC-SHA384 context should be initialized by HmacSha384New(), and should not be finalized
1184 by HmacSha384Final(). Behavior with invalid HMAC-SHA384 context is undefined.
1186 If HmacSha384Context is NULL, then return FALSE.
1187 If HmacValue is NULL, then return FALSE.
1188 If this interface is not supported, then return FALSE.
1190 @param[in, out] HmacSha384Context Pointer to the HMAC-SHA384 context.
1191 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA384 digest
1194 @retval TRUE HMAC-SHA384 digest computation succeeded.
1195 @retval FALSE HMAC-SHA384 digest computation failed.
1196 @retval FALSE This interface is not supported.
1202 IN OUT VOID
*HmacSha384Context
,
1203 OUT UINT8
*HmacValue
1207 Computes the HMAC-SHA384 digest of a input data buffer.
1209 This function performs the HMAC-SHA384 digest of a given data buffer, and places
1210 the digest value into the specified memory.
1212 If this interface is not supported, then return FALSE.
1214 @param[in] Data Pointer to the buffer containing the data to be digested.
1215 @param[in] DataSize Size of Data buffer in bytes.
1216 @param[in] Key Pointer to the user-supplied key.
1217 @param[in] KeySize Key size in bytes.
1218 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA384 digest
1221 @retval TRUE HMAC-SHA384 digest computation succeeded.
1222 @retval FALSE HMAC-SHA384 digest computation failed.
1223 @retval FALSE This interface is not supported.
1229 IN CONST VOID
*Data
,
1231 IN CONST UINT8
*Key
,
1233 OUT UINT8
*HmacValue
1236 // =====================================================================================
1237 // Symmetric Cryptography Primitive
1238 // =====================================================================================
1241 Retrieves the size, in bytes, of the context buffer required for AES operations.
1243 If this interface is not supported, then return zero.
1245 @return The size, in bytes, of the context buffer required for AES operations.
1246 @retval 0 This interface is not supported.
1256 Initializes user-supplied memory as AES context for subsequent use.
1258 This function initializes user-supplied memory pointed by AesContext as AES context.
1259 In addition, it sets up all AES key materials for subsequent encryption and decryption
1261 There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
1263 If AesContext is NULL, then return FALSE.
1264 If Key is NULL, then return FALSE.
1265 If KeyLength is not valid, then return FALSE.
1266 If this interface is not supported, then return FALSE.
1268 @param[out] AesContext Pointer to AES context being initialized.
1269 @param[in] Key Pointer to the user-supplied AES key.
1270 @param[in] KeyLength Length of AES key in bits.
1272 @retval TRUE AES context initialization succeeded.
1273 @retval FALSE AES context initialization failed.
1274 @retval FALSE This interface is not supported.
1280 OUT VOID
*AesContext
,
1281 IN CONST UINT8
*Key
,
1286 Performs AES encryption on a data buffer of the specified size in CBC mode.
1288 This function performs AES encryption on data buffer pointed by Input, of specified
1289 size of InputSize, in CBC mode.
1290 InputSize must be multiple of block size (16 bytes). This function does not perform
1291 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1292 Initialization vector should be one block size (16 bytes).
1293 AesContext should be already correctly initialized by AesInit(). Behavior with
1294 invalid AES context is undefined.
1296 If AesContext is NULL, then return FALSE.
1297 If Input is NULL, then return FALSE.
1298 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1299 If Ivec is NULL, then return FALSE.
1300 If Output is NULL, then return FALSE.
1301 If this interface is not supported, then return FALSE.
1303 @param[in] AesContext Pointer to the AES context.
1304 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1305 @param[in] InputSize Size of the Input buffer in bytes.
1306 @param[in] Ivec Pointer to initialization vector.
1307 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1309 @retval TRUE AES encryption succeeded.
1310 @retval FALSE AES encryption failed.
1311 @retval FALSE This interface is not supported.
1317 IN VOID
*AesContext
,
1318 IN CONST UINT8
*Input
,
1320 IN CONST UINT8
*Ivec
,
1325 Performs AES decryption on a data buffer of the specified size in CBC mode.
1327 This function performs AES decryption on data buffer pointed by Input, of specified
1328 size of InputSize, in CBC mode.
1329 InputSize must be multiple of block size (16 bytes). This function does not perform
1330 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1331 Initialization vector should be one block size (16 bytes).
1332 AesContext should be already correctly initialized by AesInit(). Behavior with
1333 invalid AES context is undefined.
1335 If AesContext is NULL, then return FALSE.
1336 If Input is NULL, then return FALSE.
1337 If InputSize is not multiple of block size (16 bytes), then return FALSE.
1338 If Ivec is NULL, then return FALSE.
1339 If Output is NULL, then return FALSE.
1340 If this interface is not supported, then return FALSE.
1342 @param[in] AesContext Pointer to the AES context.
1343 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1344 @param[in] InputSize Size of the Input buffer in bytes.
1345 @param[in] Ivec Pointer to initialization vector.
1346 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1348 @retval TRUE AES decryption succeeded.
1349 @retval FALSE AES decryption failed.
1350 @retval FALSE This interface is not supported.
1356 IN VOID
*AesContext
,
1357 IN CONST UINT8
*Input
,
1359 IN CONST UINT8
*Ivec
,
1363 // =====================================================================================
1364 // Authenticated Encryption with Associated Data (AEAD) Cryptography Primitive
1365 // =====================================================================================
1368 Performs AEAD AES-GCM authenticated encryption on a data buffer and additional authenticated data (AAD).
1370 IvSize must be 12, otherwise FALSE is returned.
1371 KeySize must be 16, 24 or 32, otherwise FALSE is returned.
1372 TagSize must be 12, 13, 14, 15, 16, otherwise FALSE is returned.
1374 @param[in] Key Pointer to the encryption key.
1375 @param[in] KeySize Size of the encryption key in bytes.
1376 @param[in] Iv Pointer to the IV value.
1377 @param[in] IvSize Size of the IV value in bytes.
1378 @param[in] AData Pointer to the additional authenticated data (AAD).
1379 @param[in] ADataSize Size of the additional authenticated data (AAD) in bytes.
1380 @param[in] DataIn Pointer to the input data buffer to be encrypted.
1381 @param[in] DataInSize Size of the input data buffer in bytes.
1382 @param[out] TagOut Pointer to a buffer that receives the authentication tag output.
1383 @param[in] TagSize Size of the authentication tag in bytes.
1384 @param[out] DataOut Pointer to a buffer that receives the encryption output.
1385 @param[out] DataOutSize Size of the output data buffer in bytes.
1387 @retval TRUE AEAD AES-GCM authenticated encryption succeeded.
1388 @retval FALSE AEAD AES-GCM authenticated encryption failed.
1394 IN CONST UINT8
*Key
,
1398 IN CONST UINT8
*AData
,
1400 IN CONST UINT8
*DataIn
,
1401 IN UINTN DataInSize
,
1405 OUT UINTN
*DataOutSize
1409 Performs AEAD AES-GCM authenticated decryption on a data buffer and additional authenticated data (AAD).
1411 IvSize must be 12, otherwise FALSE is returned.
1412 KeySize must be 16, 24 or 32, otherwise FALSE is returned.
1413 TagSize must be 12, 13, 14, 15, 16, otherwise FALSE is returned.
1414 If additional authenticated data verification fails, FALSE is returned.
1416 @param[in] Key Pointer to the encryption key.
1417 @param[in] KeySize Size of the encryption key in bytes.
1418 @param[in] Iv Pointer to the IV value.
1419 @param[in] IvSize Size of the IV value in bytes.
1420 @param[in] AData Pointer to the additional authenticated data (AAD).
1421 @param[in] ADataSize Size of the additional authenticated data (AAD) in bytes.
1422 @param[in] DataIn Pointer to the input data buffer to be decrypted.
1423 @param[in] DataInSize Size of the input data buffer in bytes.
1424 @param[in] Tag Pointer to a buffer that contains the authentication tag.
1425 @param[in] TagSize Size of the authentication tag in bytes.
1426 @param[out] DataOut Pointer to a buffer that receives the decryption output.
1427 @param[out] DataOutSize Size of the output data buffer in bytes.
1429 @retval TRUE AEAD AES-GCM authenticated decryption succeeded.
1430 @retval FALSE AEAD AES-GCM authenticated decryption failed.
1436 IN CONST UINT8
*Key
,
1440 IN CONST UINT8
*AData
,
1442 IN CONST UINT8
*DataIn
,
1443 IN UINTN DataInSize
,
1444 IN CONST UINT8
*Tag
,
1447 OUT UINTN
*DataOutSize
1450 // =====================================================================================
1451 // Asymmetric Cryptography Primitive
1452 // =====================================================================================
1455 Allocates and initializes one RSA context for subsequent use.
1457 @return Pointer to the RSA context that has been initialized.
1458 If the allocations fails, RsaNew() returns NULL.
1468 Release the specified RSA context.
1470 If RsaContext is NULL, then return FALSE.
1472 @param[in] RsaContext Pointer to the RSA context to be released.
1482 Sets the tag-designated key component into the established RSA context.
1484 This function sets the tag-designated RSA key component into the established
1485 RSA context from the user-specified non-negative integer (octet string format
1486 represented in RSA PKCS#1).
1487 If BigNumber is NULL, then the specified key component in RSA context is cleared.
1489 If RsaContext is NULL, then return FALSE.
1491 @param[in, out] RsaContext Pointer to RSA context being set.
1492 @param[in] KeyTag Tag of RSA key component being set.
1493 @param[in] BigNumber Pointer to octet integer buffer.
1494 If NULL, then the specified key component in RSA
1496 @param[in] BnSize Size of big number buffer in bytes.
1497 If BigNumber is NULL, then it is ignored.
1499 @retval TRUE RSA key component was set successfully.
1500 @retval FALSE Invalid RSA key component tag.
1506 IN OUT VOID
*RsaContext
,
1507 IN RSA_KEY_TAG KeyTag
,
1508 IN CONST UINT8
*BigNumber
,
1513 Gets the tag-designated RSA key component from the established RSA context.
1515 This function retrieves the tag-designated RSA key component from the
1516 established RSA context as a non-negative integer (octet string format
1517 represented in RSA PKCS#1).
1518 If specified key component has not been set or has been cleared, then returned
1520 If the BigNumber buffer is too small to hold the contents of the key, FALSE
1521 is returned and BnSize is set to the required buffer size to obtain the key.
1523 If RsaContext is NULL, then return FALSE.
1524 If BnSize is NULL, then return FALSE.
1525 If BnSize is large enough but BigNumber is NULL, then return FALSE.
1526 If this interface is not supported, then return FALSE.
1528 @param[in, out] RsaContext Pointer to RSA context being set.
1529 @param[in] KeyTag Tag of RSA key component being set.
1530 @param[out] BigNumber Pointer to octet integer buffer.
1531 @param[in, out] BnSize On input, the size of big number buffer in bytes.
1532 On output, the size of data returned in big number buffer in bytes.
1534 @retval TRUE RSA key component was retrieved successfully.
1535 @retval FALSE Invalid RSA key component tag.
1536 @retval FALSE BnSize is too small.
1537 @retval FALSE This interface is not supported.
1543 IN OUT VOID
*RsaContext
,
1544 IN RSA_KEY_TAG KeyTag
,
1545 OUT UINT8
*BigNumber
,
1546 IN OUT UINTN
*BnSize
1550 Generates RSA key components.
1552 This function generates RSA key components. It takes RSA public exponent E and
1553 length in bits of RSA modulus N as input, and generates all key components.
1554 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
1556 Before this function can be invoked, pseudorandom number generator must be correctly
1557 initialized by RandomSeed().
1559 If RsaContext is NULL, then return FALSE.
1560 If this interface is not supported, then return FALSE.
1562 @param[in, out] RsaContext Pointer to RSA context being set.
1563 @param[in] ModulusLength Length of RSA modulus N in bits.
1564 @param[in] PublicExponent Pointer to RSA public exponent.
1565 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
1567 @retval TRUE RSA key component was generated successfully.
1568 @retval FALSE Invalid RSA key component tag.
1569 @retval FALSE This interface is not supported.
1575 IN OUT VOID
*RsaContext
,
1576 IN UINTN ModulusLength
,
1577 IN CONST UINT8
*PublicExponent
,
1578 IN UINTN PublicExponentSize
1582 Validates key components of RSA context.
1583 NOTE: This function performs integrity checks on all the RSA key material, so
1584 the RSA key structure must contain all the private key data.
1586 This function validates key components of RSA context in following aspects:
1587 - Whether p is a prime
1588 - Whether q is a prime
1590 - Whether d*e = 1 mod lcm(p-1,q-1)
1592 If RsaContext is NULL, then return FALSE.
1593 If this interface is not supported, then return FALSE.
1595 @param[in] RsaContext Pointer to RSA context to check.
1597 @retval TRUE RSA key components are valid.
1598 @retval FALSE RSA key components are not valid.
1599 @retval FALSE This interface is not supported.
1609 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
1611 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
1613 If the Signature buffer is too small to hold the contents of signature, FALSE
1614 is returned and SigSize is set to the required buffer size to obtain the signature.
1616 If RsaContext is NULL, then return FALSE.
1617 If MessageHash is NULL, then return FALSE.
1618 If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
1619 If SigSize is large enough but Signature is NULL, then return FALSE.
1620 If this interface is not supported, then return FALSE.
1622 @param[in] RsaContext Pointer to RSA context for signature generation.
1623 @param[in] MessageHash Pointer to octet message hash to be signed.
1624 @param[in] HashSize Size of the message hash in bytes.
1625 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
1626 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1627 On output, the size of data returned in Signature buffer in bytes.
1629 @retval TRUE Signature successfully generated in PKCS1-v1_5.
1630 @retval FALSE Signature generation failed.
1631 @retval FALSE SigSize is too small.
1632 @retval FALSE This interface is not supported.
1638 IN VOID
*RsaContext
,
1639 IN CONST UINT8
*MessageHash
,
1641 OUT UINT8
*Signature
,
1642 IN OUT UINTN
*SigSize
1646 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
1649 If RsaContext is NULL, then return FALSE.
1650 If MessageHash is NULL, then return FALSE.
1651 If Signature is NULL, then return FALSE.
1652 If HashSize is not equal to the size of MD5, SHA-1, SHA-256 digest, then return FALSE.
1654 @param[in] RsaContext Pointer to RSA context for signature verification.
1655 @param[in] MessageHash Pointer to octet message hash to be checked.
1656 @param[in] HashSize Size of the message hash in bytes.
1657 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
1658 @param[in] SigSize Size of signature in bytes.
1660 @retval TRUE Valid signature encoded in PKCS1-v1_5.
1661 @retval FALSE Invalid signature or invalid RSA context.
1667 IN VOID
*RsaContext
,
1668 IN CONST UINT8
*MessageHash
,
1670 IN CONST UINT8
*Signature
,
1675 Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.
1677 This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in
1679 Mask generation function is the same as the message digest algorithm.
1680 If the Signature buffer is too small to hold the contents of signature, FALSE
1681 is returned and SigSize is set to the required buffer size to obtain the signature.
1683 If RsaContext is NULL, then return FALSE.
1684 If Message is NULL, then return FALSE.
1685 If MsgSize is zero or > INT_MAX, then return FALSE.
1686 If DigestLen is NOT 32, 48 or 64, return FALSE.
1687 If SaltLen is not equal to DigestLen, then return FALSE.
1688 If SigSize is large enough but Signature is NULL, then return FALSE.
1689 If this interface is not supported, then return FALSE.
1691 @param[in] RsaContext Pointer to RSA context for signature generation.
1692 @param[in] Message Pointer to octet message to be signed.
1693 @param[in] MsgSize Size of the message in bytes.
1694 @param[in] DigestLen Length of the digest in bytes to be used for RSA signature operation.
1695 @param[in] SaltLen Length of the salt in bytes to be used for PSS encoding.
1696 @param[out] Signature Pointer to buffer to receive RSA PSS signature.
1697 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1698 On output, the size of data returned in Signature buffer in bytes.
1700 @retval TRUE Signature successfully generated in RSASSA-PSS.
1701 @retval FALSE Signature generation failed.
1702 @retval FALSE SigSize is too small.
1703 @retval FALSE This interface is not supported.
1709 IN VOID
*RsaContext
,
1710 IN CONST UINT8
*Message
,
1712 IN UINT16 DigestLen
,
1714 OUT UINT8
*Signature
,
1715 IN OUT UINTN
*SigSize
1719 Verifies the RSA signature with RSASSA-PSS signature scheme defined in RFC 8017.
1720 Implementation determines salt length automatically from the signature encoding.
1721 Mask generation function is the same as the message digest algorithm.
1722 Salt length should be equal to digest length.
1724 @param[in] RsaContext Pointer to RSA context for signature verification.
1725 @param[in] Message Pointer to octet message to be verified.
1726 @param[in] MsgSize Size of the message in bytes.
1727 @param[in] Signature Pointer to RSASSA-PSS signature to be verified.
1728 @param[in] SigSize Size of signature in bytes.
1729 @param[in] DigestLen Length of digest for RSA operation.
1730 @param[in] SaltLen Salt length for PSS encoding.
1732 @retval TRUE Valid signature encoded in RSASSA-PSS.
1733 @retval FALSE Invalid signature or invalid RSA context.
1739 IN VOID
*RsaContext
,
1740 IN CONST UINT8
*Message
,
1742 IN CONST UINT8
*Signature
,
1744 IN UINT16 DigestLen
,
1749 Retrieve the RSA Private Key from the password-protected PEM key data.
1751 If PemData is NULL, then return FALSE.
1752 If RsaContext is NULL, then return FALSE.
1753 If this interface is not supported, then return FALSE.
1755 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
1756 @param[in] PemSize Size of the PEM key data in bytes.
1757 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
1758 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1759 RSA private key component. Use RsaFree() function to free the
1762 @retval TRUE RSA Private Key was retrieved successfully.
1763 @retval FALSE Invalid PEM key data or incorrect password.
1764 @retval FALSE This interface is not supported.
1769 RsaGetPrivateKeyFromPem (
1770 IN CONST UINT8
*PemData
,
1772 IN CONST CHAR8
*Password
,
1773 OUT VOID
**RsaContext
1777 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
1779 If Cert is NULL, then return FALSE.
1780 If RsaContext is NULL, then return FALSE.
1781 If this interface is not supported, then return FALSE.
1783 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1784 @param[in] CertSize Size of the X509 certificate in bytes.
1785 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1786 RSA public key component. Use RsaFree() function to free the
1789 @retval TRUE RSA Public Key was retrieved successfully.
1790 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
1791 @retval FALSE This interface is not supported.
1796 RsaGetPublicKeyFromX509 (
1797 IN CONST UINT8
*Cert
,
1799 OUT VOID
**RsaContext
1803 Retrieve the subject bytes from one X.509 certificate.
1805 If Cert is NULL, then return FALSE.
1806 If SubjectSize is NULL, then return FALSE.
1807 If this interface is not supported, then return FALSE.
1809 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1810 @param[in] CertSize Size of the X509 certificate in bytes.
1811 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
1812 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
1813 and the size of buffer returned CertSubject on output.
1815 @retval TRUE The certificate subject retrieved successfully.
1816 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
1817 The SubjectSize will be updated with the required size.
1818 @retval FALSE This interface is not supported.
1823 X509GetSubjectName (
1824 IN CONST UINT8
*Cert
,
1826 OUT UINT8
*CertSubject
,
1827 IN OUT UINTN
*SubjectSize
1831 Retrieve the common name (CN) string from one X.509 certificate.
1833 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1834 @param[in] CertSize Size of the X509 certificate in bytes.
1835 @param[out] CommonName Buffer to contain the retrieved certificate common
1836 name string (UTF8). At most CommonNameSize bytes will be
1837 written and the string will be null terminated. May be
1838 NULL in order to determine the size buffer needed.
1839 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
1840 and the size of buffer returned CommonName on output.
1841 If CommonName is NULL then the amount of space needed
1842 in buffer (including the final null) is returned.
1844 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
1845 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1846 If CommonNameSize is NULL.
1847 If CommonName is not NULL and *CommonNameSize is 0.
1848 If Certificate is invalid.
1849 @retval RETURN_NOT_FOUND If no CommonName entry exists.
1850 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
1851 (including the final null) is returned in the
1852 CommonNameSize parameter.
1853 @retval RETURN_UNSUPPORTED The operation is not supported.
1859 IN CONST UINT8
*Cert
,
1861 OUT CHAR8
*CommonName OPTIONAL
,
1862 IN OUT UINTN
*CommonNameSize
1866 Retrieve the organization name (O) string from one X.509 certificate.
1868 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1869 @param[in] CertSize Size of the X509 certificate in bytes.
1870 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
1871 name string. At most NameBufferSize bytes will be
1872 written and the string will be null terminated. May be
1873 NULL in order to determine the size buffer needed.
1874 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
1875 and the size of buffer returned Name on output.
1876 If NameBuffer is NULL then the amount of space needed
1877 in buffer (including the final null) is returned.
1879 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
1880 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
1881 If NameBufferSize is NULL.
1882 If NameBuffer is not NULL and *CommonNameSize is 0.
1883 If Certificate is invalid.
1884 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
1885 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
1886 (including the final null) is returned in the
1887 CommonNameSize parameter.
1888 @retval RETURN_UNSUPPORTED The operation is not supported.
1893 X509GetOrganizationName (
1894 IN CONST UINT8
*Cert
,
1896 OUT CHAR8
*NameBuffer OPTIONAL
,
1897 IN OUT UINTN
*NameBufferSize
1901 Verify one X509 certificate was issued by the trusted CA.
1903 If Cert is NULL, then return FALSE.
1904 If CACert is NULL, then return FALSE.
1905 If this interface is not supported, then return FALSE.
1907 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
1908 @param[in] CertSize Size of the X509 certificate in bytes.
1909 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
1910 @param[in] CACertSize Size of the CA Certificate in bytes.
1912 @retval TRUE The certificate was issued by the trusted CA.
1913 @retval FALSE Invalid certificate or the certificate was not issued by the given
1915 @retval FALSE This interface is not supported.
1921 IN CONST UINT8
*Cert
,
1923 IN CONST UINT8
*CACert
,
1928 Construct a X509 object from DER-encoded certificate data.
1930 If Cert is NULL, then return FALSE.
1931 If SingleX509Cert is NULL, then return FALSE.
1932 If this interface is not supported, then return FALSE.
1934 @param[in] Cert Pointer to the DER-encoded certificate data.
1935 @param[in] CertSize The size of certificate data in bytes.
1936 @param[out] SingleX509Cert The generated X509 object.
1938 @retval TRUE The X509 object generation succeeded.
1939 @retval FALSE The operation failed.
1940 @retval FALSE This interface is not supported.
1945 X509ConstructCertificate (
1946 IN CONST UINT8
*Cert
,
1948 OUT UINT8
**SingleX509Cert
1952 Construct a X509 stack object from a list of DER-encoded certificate data.
1954 If X509Stack is NULL, then return FALSE.
1955 If this interface is not supported, then return FALSE.
1957 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1958 On output, pointer to the X509 stack object with new
1959 inserted X509 certificate.
1960 @param[in] Args VA_LIST marker for the variable argument list.
1961 A list of DER-encoded single certificate data followed
1962 by certificate size. A NULL terminates the list. The
1963 pairs are the arguments to X509ConstructCertificate().
1965 @retval TRUE The X509 stack construction succeeded.
1966 @retval FALSE The construction operation failed.
1967 @retval FALSE This interface is not supported.
1972 X509ConstructCertificateStackV (
1973 IN OUT UINT8
**X509Stack
,
1978 Construct a X509 stack object from a list of DER-encoded certificate data.
1980 If X509Stack is NULL, then return FALSE.
1981 If this interface is not supported, then return FALSE.
1983 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
1984 On output, pointer to the X509 stack object with new
1985 inserted X509 certificate.
1986 @param ... A list of DER-encoded single certificate data followed
1987 by certificate size. A NULL terminates the list. The
1988 pairs are the arguments to X509ConstructCertificate().
1990 @retval TRUE The X509 stack construction succeeded.
1991 @retval FALSE The construction operation failed.
1992 @retval FALSE This interface is not supported.
1997 X509ConstructCertificateStack (
1998 IN OUT UINT8
**X509Stack
,
2003 Release the specified X509 object.
2005 If the interface is not supported, then ASSERT().
2007 @param[in] X509Cert Pointer to the X509 object to be released.
2017 Release the specified X509 stack object.
2019 If the interface is not supported, then ASSERT().
2021 @param[in] X509Stack Pointer to the X509 stack object to be released.
2031 Retrieve the TBSCertificate from one given X.509 certificate.
2033 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
2034 @param[in] CertSize Size of the X509 certificate in bytes.
2035 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
2036 @param[out] TBSCertSize Size of the TBS certificate in bytes.
2038 If Cert is NULL, then return FALSE.
2039 If TBSCert is NULL, then return FALSE.
2040 If TBSCertSize is NULL, then return FALSE.
2041 If this interface is not supported, then return FALSE.
2043 @retval TRUE The TBSCertificate was retrieved successfully.
2044 @retval FALSE Invalid X.509 certificate.
2050 IN CONST UINT8
*Cert
,
2052 OUT UINT8
**TBSCert
,
2053 OUT UINTN
*TBSCertSize
2057 Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0
2058 password based encryption key derivation function PBKDF2, as specified in RFC 2898.
2060 If Password or Salt or OutKey is NULL, then return FALSE.
2061 If the hash algorithm could not be determined, then return FALSE.
2062 If this interface is not supported, then return FALSE.
2064 @param[in] PasswordLength Length of input password in bytes.
2065 @param[in] Password Pointer to the array for the password.
2066 @param[in] SaltLength Size of the Salt in bytes.
2067 @param[in] Salt Pointer to the Salt.
2068 @param[in] IterationCount Number of iterations to perform. Its value should be
2069 greater than or equal to 1.
2070 @param[in] DigestSize Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).
2071 NOTE: DigestSize will be used to determine the hash algorithm.
2072 Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.
2073 @param[in] KeyLength Size of the derived key buffer in bytes.
2074 @param[out] OutKey Pointer to the output derived key buffer.
2076 @retval TRUE A key was derived successfully.
2077 @retval FALSE One of the pointers was NULL or one of the sizes was too large.
2078 @retval FALSE The hash algorithm could not be determined from the digest size.
2079 @retval FALSE The key derivation operation failed.
2080 @retval FALSE This interface is not supported.
2086 IN UINTN PasswordLength
,
2087 IN CONST CHAR8
*Password
,
2088 IN UINTN SaltLength
,
2089 IN CONST UINT8
*Salt
,
2090 IN UINTN IterationCount
,
2091 IN UINTN DigestSize
,
2097 Encrypts a blob using PKCS1v2 (RSAES-OAEP) schema. On success, will return the
2098 encrypted message in a newly allocated buffer.
2100 Things that can cause a failure include:
2101 - X509 key size does not match any known key size.
2102 - Fail to parse X509 certificate.
2103 - Fail to allocate an intermediate buffer.
2104 - Null pointer provided for a non-optional parameter.
2105 - Data size is too large for the provided key size (max size is a function of key size
2106 and hash digest size).
2108 @param[in] PublicKey A pointer to the DER-encoded X509 certificate that
2109 will be used to encrypt the data.
2110 @param[in] PublicKeySize Size of the X509 cert buffer.
2111 @param[in] InData Data to be encrypted.
2112 @param[in] InDataSize Size of the data buffer.
2113 @param[in] PrngSeed [Optional] If provided, a pointer to a random seed buffer
2114 to be used when initializing the PRNG. NULL otherwise.
2115 @param[in] PrngSeedSize [Optional] If provided, size of the random seed buffer.
2117 @param[out] EncryptedData Pointer to an allocated buffer containing the encrypted
2119 @param[out] EncryptedDataSize Size of the encrypted message buffer.
2121 @retval TRUE Encryption was successful.
2122 @retval FALSE Encryption failed.
2128 IN CONST UINT8
*PublicKey
,
2129 IN UINTN PublicKeySize
,
2131 IN UINTN InDataSize
,
2132 IN CONST UINT8
*PrngSeed OPTIONAL
,
2133 IN UINTN PrngSeedSize OPTIONAL
,
2134 OUT UINT8
**EncryptedData
,
2135 OUT UINTN
*EncryptedDataSize
2139 The 3rd parameter of Pkcs7GetSigners will return all embedded
2140 X.509 certificate in one given PKCS7 signature. The format is:
2142 // UINT8 CertNumber;
2143 // UINT32 Cert1Length;
2145 // UINT32 Cert2Length;
2148 // UINT32 CertnLength;
2152 The two following C-structure are used for parsing CertStack more clearly.
2157 UINT32 CertDataLength
; // The length in bytes of X.509 certificate.
2158 UINT8 CertDataBuffer
[0]; // The X.509 certificate content (DER).
2162 UINT8 CertNumber
; // Number of X.509 certificate.
2163 // EFI_CERT_DATA CertArray[]; // An array of X.509 certificate.
2169 Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
2170 Cryptographic Message Syntax Standard". The input signed data could be wrapped
2171 in a ContentInfo structure.
2173 If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
2174 return FALSE. If P7Length overflow, then return FALSE.
2175 If this interface is not supported, then return FALSE.
2177 @param[in] P7Data Pointer to the PKCS#7 message to verify.
2178 @param[in] P7Length Length of the PKCS#7 message in bytes.
2179 @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
2180 It's caller's responsibility to free the buffer with
2182 This data structure is EFI_CERT_STACK type.
2183 @param[out] StackLength Length of signer's certificates in bytes.
2184 @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
2185 It's caller's responsibility to free the buffer with
2187 @param[out] CertLength Length of the trusted certificate in bytes.
2189 @retval TRUE The operation is finished successfully.
2190 @retval FALSE Error occurs during the operation.
2191 @retval FALSE This interface is not supported.
2197 IN CONST UINT8
*P7Data
,
2199 OUT UINT8
**CertStack
,
2200 OUT UINTN
*StackLength
,
2201 OUT UINT8
**TrustedCert
,
2202 OUT UINTN
*CertLength
2206 Wrap function to use free() to free allocated memory for certificates.
2208 If this interface is not supported, then ASSERT().
2210 @param[in] Certs Pointer to the certificates to be freed.
2220 Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
2221 Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
2222 unchained to the signer's certificates.
2223 The input signed data could be wrapped in a ContentInfo structure.
2225 @param[in] P7Data Pointer to the PKCS#7 message.
2226 @param[in] P7Length Length of the PKCS#7 message in bytes.
2227 @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
2228 certificate. It's caller's responsibility to free the buffer
2229 with Pkcs7FreeSigners().
2230 This data structure is EFI_CERT_STACK type.
2231 @param[out] ChainLength Length of the chained certificates list buffer in bytes.
2232 @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
2233 responsibility to free the buffer with Pkcs7FreeSigners().
2234 This data structure is EFI_CERT_STACK type.
2235 @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
2237 @retval TRUE The operation is finished successfully.
2238 @retval FALSE Error occurs during the operation.
2243 Pkcs7GetCertificatesList (
2244 IN CONST UINT8
*P7Data
,
2246 OUT UINT8
**SignerChainCerts
,
2247 OUT UINTN
*ChainLength
,
2248 OUT UINT8
**UnchainCerts
,
2249 OUT UINTN
*UnchainLength
2253 Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
2254 Syntax Standard, version 1.5". This interface is only intended to be used for
2255 application to perform PKCS#7 functionality validation.
2257 If this interface is not supported, then return FALSE.
2259 @param[in] PrivateKey Pointer to the PEM-formatted private key data for
2261 @param[in] PrivateKeySize Size of the PEM private key data in bytes.
2262 @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
2264 @param[in] InData Pointer to the content to be signed.
2265 @param[in] InDataSize Size of InData in bytes.
2266 @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
2267 @param[in] OtherCerts Pointer to an optional additional set of certificates to
2268 include in the PKCS#7 signedData (e.g. any intermediate
2270 @param[out] SignedData Pointer to output PKCS#7 signedData. It's caller's
2271 responsibility to free the buffer with FreePool().
2272 @param[out] SignedDataSize Size of SignedData in bytes.
2274 @retval TRUE PKCS#7 data signing succeeded.
2275 @retval FALSE PKCS#7 data signing failed.
2276 @retval FALSE This interface is not supported.
2282 IN CONST UINT8
*PrivateKey
,
2283 IN UINTN PrivateKeySize
,
2284 IN CONST UINT8
*KeyPassword
,
2286 IN UINTN InDataSize
,
2288 IN UINT8
*OtherCerts OPTIONAL
,
2289 OUT UINT8
**SignedData
,
2290 OUT UINTN
*SignedDataSize
2294 Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
2295 Cryptographic Message Syntax Standard". The input signed data could be wrapped
2296 in a ContentInfo structure.
2298 If P7Data, TrustedCert or InData is NULL, then return FALSE.
2299 If P7Length, CertLength or DataLength overflow, then return FALSE.
2300 If this interface is not supported, then return FALSE.
2302 @param[in] P7Data Pointer to the PKCS#7 message to verify.
2303 @param[in] P7Length Length of the PKCS#7 message in bytes.
2304 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2305 is used for certificate chain verification.
2306 @param[in] CertLength Length of the trusted certificate in bytes.
2307 @param[in] InData Pointer to the content to be verified.
2308 @param[in] DataLength Length of InData in bytes.
2310 @retval TRUE The specified PKCS#7 signed data is valid.
2311 @retval FALSE Invalid PKCS#7 signed data.
2312 @retval FALSE This interface is not supported.
2318 IN CONST UINT8
*P7Data
,
2320 IN CONST UINT8
*TrustedCert
,
2321 IN UINTN CertLength
,
2322 IN CONST UINT8
*InData
,
2327 This function receives a PKCS7 formatted signature, and then verifies that
2328 the specified Enhanced or Extended Key Usages (EKU's) are present in the end-entity
2329 leaf signing certificate.
2330 Note that this function does not validate the certificate chain.
2332 Applications for custom EKU's are quite flexible. For example, a policy EKU
2333 may be present in an Issuing Certificate Authority (CA), and any sub-ordinate
2334 certificate issued might also contain this EKU, thus constraining the
2335 sub-ordinate certificate. Other applications might allow a certificate
2336 embedded in a device to specify that other Object Identifiers (OIDs) are
2337 present which contains binary data specifying custom capabilities that
2338 the device is able to do.
2340 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
2341 containing the content block with both the signature,
2342 the signer's certificate, and any necessary intermediate
2344 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
2345 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
2346 required EKUs that must be present in the signature.
2347 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
2348 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
2349 must be present in the leaf signer. If it is
2350 FALSE, then we will succeed if we find any
2351 of the specified EKU's.
2353 @retval EFI_SUCCESS The required EKUs were found in the signature.
2354 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2355 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
2360 VerifyEKUsInPkcs7Signature (
2361 IN CONST UINT8
*Pkcs7Signature
,
2362 IN CONST UINT32 SignatureSize
,
2363 IN CONST CHAR8
*RequiredEKUs
[],
2364 IN CONST UINT32 RequiredEKUsSize
,
2365 IN BOOLEAN RequireAllPresent
2369 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
2370 data could be wrapped in a ContentInfo structure.
2372 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
2373 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
2375 Caution: This function may receive untrusted input. So this function will do
2376 basic check for PKCS#7 data structure.
2378 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
2379 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
2380 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
2381 It's caller's responsibility to free the buffer with FreePool().
2382 @param[out] ContentSize The size of the extracted content in bytes.
2384 @retval TRUE The P7Data was correctly formatted for processing.
2385 @retval FALSE The P7Data was not correctly formatted for processing.
2390 Pkcs7GetAttachedContent (
2391 IN CONST UINT8
*P7Data
,
2394 OUT UINTN
*ContentSize
2398 Verifies the validity of a PE/COFF Authenticode Signature as described in "Windows
2399 Authenticode Portable Executable Signature Format".
2401 If AuthData is NULL, then return FALSE.
2402 If ImageHash is NULL, then return FALSE.
2403 If this interface is not supported, then return FALSE.
2405 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2406 PE/COFF image to be verified.
2407 @param[in] DataSize Size of the Authenticode Signature in bytes.
2408 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
2409 is used for certificate chain verification.
2410 @param[in] CertSize Size of the trusted certificate in bytes.
2411 @param[in] ImageHash Pointer to the original image file hash value. The procedure
2412 for calculating the image hash value is described in Authenticode
2414 @param[in] HashSize Size of Image hash value in bytes.
2416 @retval TRUE The specified Authenticode Signature is valid.
2417 @retval FALSE Invalid Authenticode Signature.
2418 @retval FALSE This interface is not supported.
2423 AuthenticodeVerify (
2424 IN CONST UINT8
*AuthData
,
2426 IN CONST UINT8
*TrustedCert
,
2428 IN CONST UINT8
*ImageHash
,
2433 Verifies the validity of a RFC3161 Timestamp CounterSignature embedded in PE/COFF Authenticode
2436 If AuthData is NULL, then return FALSE.
2437 If this interface is not supported, then return FALSE.
2439 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed
2440 PE/COFF image to be verified.
2441 @param[in] DataSize Size of the Authenticode Signature in bytes.
2442 @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER, which
2443 is used for TSA certificate chain verification.
2444 @param[in] CertSize Size of the trusted certificate in bytes.
2445 @param[out] SigningTime Return the time of timestamp generation time if the timestamp
2448 @retval TRUE The specified Authenticode includes a valid RFC3161 Timestamp CounterSignature.
2449 @retval FALSE No valid RFC3161 Timestamp CounterSignature in the specified Authenticode data.
2454 ImageTimestampVerify (
2455 IN CONST UINT8
*AuthData
,
2457 IN CONST UINT8
*TsaCert
,
2459 OUT EFI_TIME
*SigningTime
2462 // =====================================================================================
2463 // DH Key Exchange Primitive
2464 // =====================================================================================
2467 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
2469 @return Pointer to the Diffie-Hellman Context that has been initialized.
2470 If the allocations fails, DhNew() returns NULL.
2471 If the interface is not supported, DhNew() returns NULL.
2481 Release the specified DH context.
2483 If the interface is not supported, then ASSERT().
2485 @param[in] DhContext Pointer to the DH context to be released.
2495 Generates DH parameter.
2497 Given generator g, and length of prime number p in bits, this function generates p,
2498 and sets DH context according to value of g and p.
2500 Before this function can be invoked, pseudorandom number generator must be correctly
2501 initialized by RandomSeed().
2503 If DhContext is NULL, then return FALSE.
2504 If Prime is NULL, then return FALSE.
2505 If this interface is not supported, then return FALSE.
2507 @param[in, out] DhContext Pointer to the DH context.
2508 @param[in] Generator Value of generator.
2509 @param[in] PrimeLength Length in bits of prime to be generated.
2510 @param[out] Prime Pointer to the buffer to receive the generated prime number.
2512 @retval TRUE DH parameter generation succeeded.
2513 @retval FALSE Value of Generator is not supported.
2514 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
2515 @retval FALSE This interface is not supported.
2520 DhGenerateParameter (
2521 IN OUT VOID
*DhContext
,
2523 IN UINTN PrimeLength
,
2528 Sets generator and prime parameters for DH.
2530 Given generator g, and prime number p, this function and sets DH
2531 context accordingly.
2533 If DhContext is NULL, then return FALSE.
2534 If Prime is NULL, then return FALSE.
2535 If this interface is not supported, then return FALSE.
2537 @param[in, out] DhContext Pointer to the DH context.
2538 @param[in] Generator Value of generator.
2539 @param[in] PrimeLength Length in bits of prime to be generated.
2540 @param[in] Prime Pointer to the prime number.
2542 @retval TRUE DH parameter setting succeeded.
2543 @retval FALSE Value of Generator is not supported.
2544 @retval FALSE Value of Generator is not suitable for the Prime.
2545 @retval FALSE Value of Prime is not a prime number.
2546 @retval FALSE Value of Prime is not a safe prime number.
2547 @retval FALSE This interface is not supported.
2553 IN OUT VOID
*DhContext
,
2555 IN UINTN PrimeLength
,
2556 IN CONST UINT8
*Prime
2560 Generates DH public key.
2562 This function generates random secret exponent, and computes the public key, which is
2563 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
2564 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
2565 PublicKeySize is set to the required buffer size to obtain the public key.
2567 If DhContext is NULL, then return FALSE.
2568 If PublicKeySize is NULL, then return FALSE.
2569 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
2570 If this interface is not supported, then return FALSE.
2572 @param[in, out] DhContext Pointer to the DH context.
2573 @param[out] PublicKey Pointer to the buffer to receive generated public key.
2574 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
2575 On output, the size of data returned in PublicKey buffer in bytes.
2577 @retval TRUE DH public key generation succeeded.
2578 @retval FALSE DH public key generation failed.
2579 @retval FALSE PublicKeySize is not large enough.
2580 @retval FALSE This interface is not supported.
2586 IN OUT VOID
*DhContext
,
2587 OUT UINT8
*PublicKey
,
2588 IN OUT UINTN
*PublicKeySize
2592 Computes exchanged common key.
2594 Given peer's public key, this function computes the exchanged common key, based on its own
2595 context including value of prime modulus and random secret exponent.
2597 If DhContext is NULL, then return FALSE.
2598 If PeerPublicKey is NULL, then return FALSE.
2599 If KeySize is NULL, then return FALSE.
2600 If Key is NULL, then return FALSE.
2601 If KeySize is not large enough, then return FALSE.
2602 If this interface is not supported, then return FALSE.
2604 @param[in, out] DhContext Pointer to the DH context.
2605 @param[in] PeerPublicKey Pointer to the peer's public key.
2606 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
2607 @param[out] Key Pointer to the buffer to receive generated key.
2608 @param[in, out] KeySize On input, the size of Key buffer in bytes.
2609 On output, the size of data returned in Key buffer in bytes.
2611 @retval TRUE DH exchanged key generation succeeded.
2612 @retval FALSE DH exchanged key generation failed.
2613 @retval FALSE KeySize is not large enough.
2614 @retval FALSE This interface is not supported.
2620 IN OUT VOID
*DhContext
,
2621 IN CONST UINT8
*PeerPublicKey
,
2622 IN UINTN PeerPublicKeySize
,
2624 IN OUT UINTN
*KeySize
2627 // =====================================================================================
2628 // Pseudo-Random Generation Primitive
2629 // =====================================================================================
2632 Sets up the seed value for the pseudorandom number generator.
2634 This function sets up the seed value for the pseudorandom number generator.
2635 If Seed is not NULL, then the seed passed in is used.
2636 If Seed is NULL, then default seed is used.
2637 If this interface is not supported, then return FALSE.
2639 @param[in] Seed Pointer to seed value.
2640 If NULL, default seed is used.
2641 @param[in] SeedSize Size of seed value.
2642 If Seed is NULL, this parameter is ignored.
2644 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
2645 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
2646 @retval FALSE This interface is not supported.
2652 IN CONST UINT8
*Seed OPTIONAL
,
2657 Generates a pseudorandom byte stream of the specified size.
2659 If Output is NULL, then return FALSE.
2660 If this interface is not supported, then return FALSE.
2662 @param[out] Output Pointer to buffer to receive random value.
2663 @param[in] Size Size of random bytes to generate.
2665 @retval TRUE Pseudorandom byte stream generated successfully.
2666 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
2667 @retval FALSE This interface is not supported.
2677 // =====================================================================================
2678 // Key Derivation Function Primitive
2679 // =====================================================================================
2682 Derive key data using HMAC-SHA256 based KDF.
2684 @param[in] Key Pointer to the user-supplied key.
2685 @param[in] KeySize Key size in bytes.
2686 @param[in] Salt Pointer to the salt(non-secret) value.
2687 @param[in] SaltSize Salt size in bytes.
2688 @param[in] Info Pointer to the application specific info.
2689 @param[in] InfoSize Info size in bytes.
2690 @param[out] Out Pointer to buffer to receive hkdf value.
2691 @param[in] OutSize Size of hkdf bytes to generate.
2693 @retval TRUE Hkdf generated successfully.
2694 @retval FALSE Hkdf generation failed.
2699 HkdfSha256ExtractAndExpand (
2700 IN CONST UINT8
*Key
,
2702 IN CONST UINT8
*Salt
,
2704 IN CONST UINT8
*Info
,
2711 Derive SHA256 HMAC-based Extract key Derivation Function (HKDF).
2713 @param[in] Key Pointer to the user-supplied key.
2714 @param[in] KeySize key size in bytes.
2715 @param[in] Salt Pointer to the salt(non-secret) value.
2716 @param[in] SaltSize salt size in bytes.
2717 @param[out] PrkOut Pointer to buffer to receive hkdf value.
2718 @param[in] PrkOutSize size of hkdf bytes to generate.
2720 @retval true Hkdf generated successfully.
2721 @retval false Hkdf generation failed.
2727 IN CONST UINT8
*Key
,
2729 IN CONST UINT8
*Salt
,
2736 Derive SHA256 HMAC-based Expand Key Derivation Function (HKDF).
2738 @param[in] Prk Pointer to the user-supplied key.
2739 @param[in] PrkSize Key size in bytes.
2740 @param[in] Info Pointer to the application specific info.
2741 @param[in] InfoSize Info size in bytes.
2742 @param[out] Out Pointer to buffer to receive hkdf value.
2743 @param[in] OutSize Size of hkdf bytes to generate.
2745 @retval TRUE Hkdf generated successfully.
2746 @retval FALSE Hkdf generation failed.
2752 IN CONST UINT8
*Prk
,
2754 IN CONST UINT8
*Info
,
2761 Derive SHA384 HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
2763 @param[in] Key Pointer to the user-supplied key.
2764 @param[in] KeySize Key size in bytes.
2765 @param[in] Salt Pointer to the salt(non-secret) value.
2766 @param[in] SaltSize Salt size in bytes.
2767 @param[in] Info Pointer to the application specific info.
2768 @param[in] InfoSize Info size in bytes.
2769 @param[out] Out Pointer to buffer to receive hkdf value.
2770 @param[in] OutSize Size of hkdf bytes to generate.
2772 @retval TRUE Hkdf generated successfully.
2773 @retval FALSE Hkdf generation failed.
2778 HkdfSha384ExtractAndExpand (
2779 IN CONST UINT8
*Key
,
2781 IN CONST UINT8
*Salt
,
2783 IN CONST UINT8
*Info
,
2790 Derive SHA384 HMAC-based Extract key Derivation Function (HKDF).
2792 @param[in] Key Pointer to the user-supplied key.
2793 @param[in] KeySize key size in bytes.
2794 @param[in] Salt Pointer to the salt(non-secret) value.
2795 @param[in] SaltSize salt size in bytes.
2796 @param[out] PrkOut Pointer to buffer to receive hkdf value.
2797 @param[in] PrkOutSize size of hkdf bytes to generate.
2799 @retval true Hkdf generated successfully.
2800 @retval false Hkdf generation failed.
2806 IN CONST UINT8
*Key
,
2808 IN CONST UINT8
*Salt
,
2815 Derive SHA384 HMAC-based Expand Key Derivation Function (HKDF).
2817 @param[in] Prk Pointer to the user-supplied key.
2818 @param[in] PrkSize Key size in bytes.
2819 @param[in] Info Pointer to the application specific info.
2820 @param[in] InfoSize Info size in bytes.
2821 @param[out] Out Pointer to buffer to receive hkdf value.
2822 @param[in] OutSize Size of hkdf bytes to generate.
2824 @retval TRUE Hkdf generated successfully.
2825 @retval FALSE Hkdf generation failed.
2831 IN CONST UINT8
*Prk
,
2833 IN CONST UINT8
*Info
,
2839 // =====================================================================================
2840 // Big number primitives
2841 // =====================================================================================
2844 Allocate new Big Number.
2846 @retval New BigNum opaque structure or NULL on failure.
2855 Allocate new Big Number and assign the provided value to it.
2857 @param[in] Buf Big endian encoded buffer.
2858 @param[in] Len Buffer length.
2860 @retval New BigNum opaque structure or NULL on failure.
2865 IN CONST UINT8
*Buf
,
2870 Convert the absolute value of Bn into big-endian form and store it at Buf.
2871 The Buf array should have at least BigNumBytes() in it.
2873 @param[in] Bn Big number to convert.
2874 @param[out] Buf Output buffer.
2876 @retval The length of the big-endian number placed at Buf or -1 on error.
2886 Free the Big Number.
2888 @param[in] Bn Big number to free.
2889 @param[in] Clear TRUE if the buffer should be cleared.
2899 Calculate the sum of two Big Numbers.
2900 Please note, all "out" Big number arguments should be properly initialized
2901 by calling to BigNumInit() or BigNumFromBin() functions.
2903 @param[in] BnA Big number.
2904 @param[in] BnB Big number.
2905 @param[out] BnRes The result of BnA + BnB.
2907 @retval TRUE On success.
2908 @retval FALSE Otherwise.
2919 Subtract two Big Numbers.
2920 Please note, all "out" Big number arguments should be properly initialized
2921 by calling to BigNumInit() or BigNumFromBin() functions.
2923 @param[in] BnA Big number.
2924 @param[in] BnB Big number.
2925 @param[out] BnRes The result of BnA - BnB.
2927 @retval TRUE On success.
2928 @retval FALSE Otherwise.
2939 Calculate remainder: BnRes = BnA % BnB.
2940 Please note, all "out" Big number arguments should be properly initialized
2941 by calling to BigNumInit() or BigNumFromBin() functions.
2943 @param[in] BnA Big number.
2944 @param[in] BnB Big number.
2945 @param[out] BnRes The result of BnA % BnB.
2947 @retval TRUE On success.
2948 @retval FALSE Otherwise.
2959 Compute BnA to the BnP-th power modulo BnM.
2960 Please note, all "out" Big number arguments should be properly initialized
2961 by calling to BigNumInit() or BigNumFromBin() functions.
2963 @param[in] BnA Big number.
2964 @param[in] BnP Big number (power).
2965 @param[in] BnM Big number (modulo).
2966 @param[out] BnRes The result of (BnA ^ BnP) % BnM.
2968 @retval TRUE On success.
2969 @retval FALSE Otherwise.
2981 Compute BnA inverse modulo BnM.
2982 Please note, all "out" Big number arguments should be properly initialized
2983 by calling to BigNumInit() or BigNumFromBin() functions.
2985 @param[in] BnA Big number.
2986 @param[in] BnM Big number (modulo).
2987 @param[out] BnRes The result, such that (BnA * BnRes) % BnM == 1.
2989 @retval TRUE On success.
2990 @retval FALSE Otherwise.
3001 Divide two Big Numbers.
3002 Please note, all "out" Big number arguments should be properly initialized
3003 by calling to BigNumInit() or BigNumFromBin() functions.
3005 @param[in] BnA Big number.
3006 @param[in] BnB Big number.
3007 @param[out] BnRes The result, such that BnA / BnB.
3009 @retval TRUE On success.
3010 @retval FALSE Otherwise.
3021 Multiply two Big Numbers modulo BnM.
3022 Please note, all "out" Big number arguments should be properly initialized
3023 by calling to BigNumInit() or BigNumFromBin() functions.
3025 @param[in] BnA Big number.
3026 @param[in] BnB Big number.
3027 @param[in] BnM Big number (modulo).
3028 @param[out] BnRes The result, such that (BnA * BnB) % BnM.
3030 @retval TRUE On success.
3031 @retval FALSE Otherwise.
3043 Compare two Big Numbers.
3045 @param[in] BnA Big number.
3046 @param[in] BnB Big number.
3048 @retval 0 BnA == BnB.
3049 @retval 1 BnA > BnB.
3050 @retval -1 BnA < BnB.
3060 Get number of bits in Bn.
3062 @param[in] Bn Big number.
3064 @retval Number of bits.
3074 Get number of bytes in Bn.
3076 @param[in] Bn Big number.
3078 @retval Number of bytes.
3087 Checks if Big Number equals to the given Num.
3089 @param[in] Bn Big number.
3090 @param[in] Num Number.
3092 @retval TRUE iff Bn == Num.
3093 @retval FALSE otherwise.
3103 Checks if Big Number is odd.
3105 @param[in] Bn Big number.
3107 @retval TRUE Bn is odd (Bn % 2 == 1).
3108 @retval FALSE otherwise.
3119 @param[out] BnDst Destination.
3120 @param[in] BnSrc Source.
3122 @retval BnDst on success.
3123 @retval NULL otherwise.
3129 IN CONST VOID
*BnSrc
3133 Get constant Big number with value of "1".
3134 This may be used to save expensive allocations.
3136 @retval Big Number with value of 1.
3145 Shift right Big Number.
3146 Please note, all "out" Big number arguments should be properly initialized
3147 by calling to BigNumInit() or BigNumFromBin() functions.
3149 @param[in] Bn Big number.
3150 @param[in] N Number of bits to shift.
3151 @param[out] BnRes The result.
3153 @retval TRUE On success.
3154 @retval FALSE Otherwise.
3165 Mark Big Number for constant time computations.
3166 This function should be called before any constant time computations are
3167 performed on the given Big number.
3169 @param[in] Bn Big number.
3178 Calculate square modulo.
3179 Please note, all "out" Big number arguments should be properly initialized
3180 by calling to BigNumInit() or BigNumFromBin() functions.
3182 @param[in] BnA Big number.
3183 @param[in] BnM Big number (modulo).
3184 @param[out] BnRes The result, such that (BnA ^ 2) % BnM.
3186 @retval TRUE On success.
3187 @retval FALSE Otherwise.
3198 Create new Big Number computation context. This is an opaque structure
3199 which should be passed to any function that requires it. The BN context is
3200 needed to optimize calculations and expensive allocations.
3202 @retval Big Number context struct or NULL on failure.
3211 Free Big Number context that was allocated with BigNumNewContext().
3213 @param[in] BnCtx Big number context to free.
3222 Set Big Number to a given value.
3224 @param[in] Bn Big number to set.
3225 @param[in] Val Value to set.
3227 @retval TRUE On success.
3228 @retval FALSE Otherwise.
3238 Add two Big Numbers modulo BnM.
3240 @param[in] BnA Big number.
3241 @param[in] BnB Big number.
3242 @param[in] BnM Big number (modulo).
3243 @param[out] BnRes The result, such that (BnA + BnB) % BnM.
3245 @retval TRUE On success.
3246 @retval FALSE Otherwise.
3257 #endif // __BASE_CRYPT_LIB_H__