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 - 2010, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #ifndef __BASE_CRYPT_LIB_H__
19 #define __BASE_CRYPT_LIB_H__
22 /// MD4 digest size in bytes
24 #define MD4_DIGEST_SIZE 16
27 /// MD5 digest size in bytes
29 #define MD5_DIGEST_SIZE 16
32 /// SHA-1 digest size in bytes.
34 #define SHA1_DIGEST_SIZE 20
37 /// SHA-256 digest size in bytes
39 #define SHA256_DIGEST_SIZE 32
42 /// TDES block size in bytes
44 #define TDES_BLOCK_SIZE 8
47 /// AES block size in bytes
49 #define AES_BLOCK_SIZE 16
52 /// RSA Key Tags Definition used in RsaSetKey() function for key component identification.
55 RsaKeyN
, ///< RSA public Modulus (N)
56 RsaKeyE
, ///< RSA Public exponent (e)
57 RsaKeyD
, ///< RSA Private exponent (d)
58 RsaKeyP
, ///< RSA secret prime factor of Modulus (p)
59 RsaKeyQ
, ///< RSA secret prime factor of Modules (q)
60 RsaKeyDp
, ///< p's CRT exponent (== d mod (p - 1))
61 RsaKeyDq
, ///< q's CRT exponent (== d mod (q - 1))
62 RsaKeyQInv
///< The CRT coefficient (== 1/q mod p)
65 //=====================================================================================
66 // One-Way Cryptographic Hash Primitives
67 //=====================================================================================
70 Retrieves the size, in bytes, of the context buffer required for MD4 hash operations.
72 @return The size, in bytes, of the context buffer required for MD4 hash operations.
82 Initializes user-supplied memory pointed by Md4Context as MD4 hash context for
85 If Md4Context is NULL, then ASSERT().
87 @param[out] Md4Context Pointer to MD4 context being initialized.
89 @retval TRUE MD4 context initialization succeeded.
90 @retval FALSE MD4 context initialization failed.
100 Makes a copy of an existing MD4 context.
102 If Md4Context is NULL, then ASSERT().
103 If NewMd4Context is NULL, then ASSERT().
105 @param[in] Md4Context Pointer to MD4 context being copied.
106 @param[out] NewMd4Context Pointer to new MD4 context.
108 @retval TRUE MD4 context copy succeeded.
109 @retval FALSE MD4 context copy failed.
115 IN CONST VOID
*Md4Context
,
116 OUT VOID
*NewMd4Context
120 Digests the input data and updates MD4 context.
122 This function performs MD4 digest on a data buffer of the specified size.
123 It can be called multiple times to compute the digest of long or discontinuous data streams.
124 MD4 context should be already correctly intialized by Md4Init(), and should not be finalized
125 by Md4Final(). Behavior with invalid context is undefined.
127 If Md4Context is NULL, then ASSERT().
129 @param[in, out] Md4Context Pointer to the MD4 context.
130 @param[in] Data Pointer to the buffer containing the data to be hashed.
131 @param[in] DataSize Size of Data buffer in bytes.
133 @retval TRUE MD4 data digest succeeded.
134 @retval FALSE MD4 data digest failed.
140 IN OUT VOID
*Md4Context
,
146 Completes computation of the MD4 digest value.
148 This function completes MD4 hash computation and retrieves the digest value into
149 the specified memory. After this function has been called, the MD4 context cannot
151 MD4 context should be already correctly intialized by Md4Init(), and should not be
152 finalized by Md4Final(). Behavior with invalid MD4 context is undefined.
154 If Md4Context is NULL, then ASSERT().
155 If HashValue is NULL, then ASSERT().
157 @param[in, out] Md4Context Pointer to the MD4 context.
158 @param[out] HashValue Pointer to a buffer that receives the MD4 digest
161 @retval TRUE MD4 digest computation succeeded.
162 @retval FALSE MD4 digest computation failed.
168 IN OUT VOID
*Md4Context
,
173 Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
175 @return The size, in bytes, of the context buffer required for MD5 hash operations.
185 Initializes user-supplied memory pointed by Md5Context as MD5 hash context for
188 If Md5Context is NULL, then ASSERT().
190 @param[out] Md5Context Pointer to MD5 context being initialized.
192 @retval TRUE MD5 context initialization succeeded.
193 @retval FALSE MD5 context initialization failed.
203 Makes a copy of an existing MD5 context.
205 If Md5Context is NULL, then ASSERT().
206 If NewMd5Context is NULL, then ASSERT().
208 @param[in] Md5Context Pointer to MD5 context being copied.
209 @param[out] NewMd5Context Pointer to new MD5 context.
211 @retval TRUE MD5 context copy succeeded.
212 @retval FALSE MD5 context copy failed.
218 IN CONST VOID
*Md5Context
,
219 OUT VOID
*NewMd5Context
223 Digests the input data and updates MD5 context.
225 This function performs MD5 digest on a data buffer of the specified size.
226 It can be called multiple times to compute the digest of long or discontinuous data streams.
227 MD5 context should be already correctly intialized by Md5Init(), and should not be finalized
228 by Md5Final(). Behavior with invalid context is undefined.
230 If Md5Context is NULL, then ASSERT().
232 @param[in, out] Md5Context Pointer to the MD5 context.
233 @param[in] Data Pointer to the buffer containing the data to be hashed.
234 @param[in] DataSize Size of Data buffer in bytes.
236 @retval TRUE MD5 data digest succeeded.
237 @retval FALSE MD5 data digest failed.
243 IN OUT VOID
*Md5Context
,
249 Completes computation of the MD5 digest value.
251 This function completes MD5 hash computation and retrieves the digest value into
252 the specified memory. After this function has been called, the MD5 context cannot
254 MD5 context should be already correctly intialized by Md5Init(), and should not be
255 finalized by Md5Final(). Behavior with invalid MD5 context is undefined.
257 If Md5Context is NULL, then ASSERT().
258 If HashValue is NULL, then ASSERT().
260 @param[in, out] Md5Context Pointer to the MD5 context.
261 @param[out] HashValue Pointer to a buffer that receives the MD5 digest
264 @retval TRUE MD5 digest computation succeeded.
265 @retval FALSE MD5 digest computation failed.
271 IN OUT VOID
*Md5Context
,
276 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
278 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
288 Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
291 If Sha1Context is NULL, then ASSERT().
293 @param[out] Sha1Context Pointer to SHA-1 context being initialized.
295 @retval TRUE SHA-1 context initialization succeeded.
296 @retval FALSE SHA-1 context initialization failed.
302 OUT VOID
*Sha1Context
306 Makes a copy of an existing SHA-1 context.
308 If Sha1Context is NULL, then ASSERT().
309 If NewSha1Context is NULL, then ASSERT().
311 @param[in] Sha1Context Pointer to SHA-1 context being copied.
312 @param[out] NewSha1Context Pointer to new SHA-1 context.
314 @retval TRUE SHA-1 context copy succeeded.
315 @retval FALSE SHA-1 context copy failed.
321 IN CONST VOID
*Sha1Context
,
322 OUT VOID
*NewSha1Context
326 Digests the input data and updates SHA-1 context.
328 This function performs SHA-1 digest on a data buffer of the specified size.
329 It can be called multiple times to compute the digest of long or discontinuous data streams.
330 SHA-1 context should be already correctly intialized by Sha1Init(), and should not be finalized
331 by Sha1Final(). Behavior with invalid context is undefined.
333 If Sha1Context is NULL, then ASSERT().
335 @param[in, out] Sha1Context Pointer to the SHA-1 context.
336 @param[in] Data Pointer to the buffer containing the data to be hashed.
337 @param[in] DataSize Size of Data buffer in bytes.
339 @retval TRUE SHA-1 data digest succeeded.
340 @retval FALSE SHA-1 data digest failed.
346 IN OUT VOID
*Sha1Context
,
352 Completes computation of the SHA-1 digest value.
354 This function completes SHA-1 hash computation and retrieves the digest value into
355 the specified memory. After this function has been called, the SHA-1 context cannot
357 SHA-1 context should be already correctly intialized by Sha1Init(), and should not be
358 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
360 If Sha1Context is NULL, then ASSERT().
361 If HashValue is NULL, then ASSERT().
363 @param[in, out] Sha1Context Pointer to the SHA-1 context.
364 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
367 @retval TRUE SHA-1 digest computation succeeded.
368 @retval FALSE SHA-1 digest computation failed.
374 IN OUT VOID
*Sha1Context
,
379 Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.
381 @return The size, in bytes, of the context buffer required for SHA-256 hash operations.
386 Sha256GetContextSize (
391 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
394 If Sha256Context is NULL, then ASSERT().
396 @param[out] Sha256Context Pointer to SHA-256 context being initialized.
398 @retval TRUE SHA-256 context initialization succeeded.
399 @retval FALSE SHA-256 context initialization failed.
405 OUT VOID
*Sha256Context
409 Makes a copy of an existing SHA-256 context.
411 If Sha256Context is NULL, then ASSERT().
412 If NewSha256Context is NULL, then ASSERT().
414 @param[in] Sha256Context Pointer to SHA-256 context being copied.
415 @param[out] NewSha256Context Pointer to new SHA-256 context.
417 @retval TRUE SHA-256 context copy succeeded.
418 @retval FALSE SHA-256 context copy failed.
424 IN CONST VOID
*Sha256Context
,
425 OUT VOID
*NewSha256Context
429 Digests the input data and updates SHA-256 context.
431 This function performs SHA-256 digest on a data buffer of the specified size.
432 It can be called multiple times to compute the digest of long or discontinuous data streams.
433 SHA-256 context should be already correctly intialized by Sha256Init(), and should not be finalized
434 by Sha256Final(). Behavior with invalid context is undefined.
436 If Sha256Context is NULL, then ASSERT().
438 @param[in, out] Sha256Context Pointer to the SHA-256 context.
439 @param[in] Data Pointer to the buffer containing the data to be hashed.
440 @param[in] DataSize Size of Data buffer in bytes.
442 @retval TRUE SHA-256 data digest succeeded.
443 @retval FALSE SHA-256 data digest failed.
449 IN OUT VOID
*Sha256Context
,
455 Completes computation of the SHA-256 digest value.
457 This function completes SHA-256 hash computation and retrieves the digest value into
458 the specified memory. After this function has been called, the SHA-256 context cannot
460 SHA-256 context should be already correctly intialized by Sha256Init(), and should not be
461 finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.
463 If Sha256Context is NULL, then ASSERT().
464 If HashValue is NULL, then ASSERT().
466 @param[in, out] Sha256Context Pointer to the SHA-256 context.
467 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest
470 @retval TRUE SHA-256 digest computation succeeded.
471 @retval FALSE SHA-256 digest computation failed.
477 IN OUT VOID
*Sha256Context
,
482 //=====================================================================================
483 // MAC (Message Authentication Code) Primitive
484 //=====================================================================================
487 Retrieves the size, in bytes, of the context buffer required for HMAC-MD5 operations.
489 @return The size, in bytes, of the context buffer required for HMAC-MD5 operations.
494 HmacMd5GetContextSize (
499 Initializes user-supplied memory pointed by HmacMd5Context as HMAC-MD5 context for
502 If HmacMd5Context is NULL, then ASSERT().
504 @param[out] HmacMd5Context Pointer to HMAC-MD5 context being initialized.
505 @param[in] Key Pointer to the user-supplied key.
506 @param[in] KeySize Key size in bytes.
508 @retval TRUE HMAC-MD5 context initialization succeeded.
509 @retval FALSE HMAC-MD5 context initialization failed.
515 OUT VOID
*HmacMd5Context
,
521 Makes a copy of an existing HMAC-MD5 context.
523 If HmacMd5Context is NULL, then ASSERT().
524 If NewHmacMd5Context is NULL, then ASSERT().
526 @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.
527 @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.
529 @retval TRUE HMAC-MD5 context copy succeeded.
530 @retval FALSE HMAC-MD5 context copy failed.
536 IN CONST VOID
*HmacMd5Context
,
537 OUT VOID
*NewHmacMd5Context
541 Digests the input data and updates HMAC-MD5 context.
543 This function performs HMAC-MD5 digest on a data buffer of the specified size.
544 It can be called multiple times to compute the digest of long or discontinuous data streams.
545 HMAC-MD5 context should be already correctly intialized by HmacMd5Init(), and should not be
546 finalized by HmacMd5Final(). Behavior with invalid context is undefined.
548 If HmacMd5Context is NULL, then ASSERT().
550 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
551 @param[in] Data Pointer to the buffer containing the data to be digested.
552 @param[in] DataSize Size of Data buffer in bytes.
554 @retval TRUE HMAC-MD5 data digest succeeded.
555 @retval FALSE HMAC-MD5 data digest failed.
561 IN OUT VOID
*HmacMd5Context
,
567 Completes computation of the HMAC-MD5 digest value.
569 This function completes HMAC-MD5 hash computation and retrieves the digest value into
570 the specified memory. After this function has been called, the HMAC-MD5 context cannot
572 HMAC-MD5 context should be already correctly intialized by HmacMd5Init(), and should not be
573 finalized by HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.
575 If HmacMd5Context is NULL, then ASSERT().
576 If HashValue is NULL, then ASSERT().
578 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.
579 @param[out] HashValue Pointer to a buffer that receives the HMAC-MD5 digest
582 @retval TRUE HMAC-MD5 digest computation succeeded.
583 @retval FALSE HMAC-MD5 digest computation failed.
589 IN OUT VOID
*HmacMd5Context
,
594 Retrieves the size, in bytes, of the context buffer required for HMAC-SHA1 operations.
596 @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.
601 HmacSha1GetContextSize (
606 Initializes user-supplied memory pointed by HmacSha1Context as HMAC-SHA1 context for
609 If HmacSha1Context is NULL, then ASSERT().
611 @param[out] HmacSha1Context Pointer to HMAC-SHA1 context being initialized.
612 @param[in] Key Pointer to the user-supplied key.
613 @param[in] KeySize Key size in bytes.
615 @retval TRUE HMAC-SHA1 context initialization succeeded.
616 @retval FALSE HMAC-SHA1 context initialization failed.
622 OUT VOID
*HmacSha1Context
,
628 Makes a copy of an existing HMAC-SHA1 context.
630 If HmacSha1Context is NULL, then ASSERT().
631 If NewHmacSha1Context is NULL, then ASSERT().
633 @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.
634 @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.
636 @retval TRUE HMAC-SHA1 context copy succeeded.
637 @retval FALSE HMAC-SHA1 context copy failed.
643 IN CONST VOID
*HmacSha1Context
,
644 OUT VOID
*NewHmacSha1Context
648 Digests the input data and updates HMAC-SHA1 context.
650 This function performs HMAC-SHA1 digest on a data buffer of the specified size.
651 It can be called multiple times to compute the digest of long or discontinuous data streams.
652 HMAC-SHA1 context should be already correctly intialized by HmacSha1Init(), and should not
653 be finalized by HmacSha1Final(). Behavior with invalid context is undefined.
655 If HmacSha1Context is NULL, then ASSERT().
657 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
658 @param[in] Data Pointer to the buffer containing the data to be digested.
659 @param[in] DataSize Size of Data buffer in bytes.
661 @retval TRUE HMAC-SHA1 data digest succeeded.
662 @retval FALSE HMAC-SHA1 data digest failed.
668 IN OUT VOID
*HmacSha1Context
,
674 Completes computation of the HMAC-SHA1 digest value.
676 This function completes HMAC-SHA1 hash computation and retrieves the digest value into
677 the specified memory. After this function has been called, the HMAC-SHA1 context cannot
679 HMAC-SHA1 context should be already correctly intialized by HmacSha1Init(), and should
680 not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.
682 If HmacSha1Context is NULL, then ASSERT().
683 If HashValue is NULL, then ASSERT().
685 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
686 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA1 digest
689 @retval TRUE HMAC-SHA1 digest computation succeeded.
690 @retval FALSE HMAC-SHA1 digest computation failed.
696 IN OUT VOID
*HmacSha1Context
,
701 //=====================================================================================
702 // Symmetric Cryptography Primitive
703 //=====================================================================================
706 Retrieves the size, in bytes, of the context buffer required for TDES operations.
708 @return The size, in bytes, of the context buffer required for TDES operations.
718 Initializes user-supplied memory as TDES context for subsequent use.
720 This function initializes user-supplied memory pointed by TdesContext as TDES context.
721 In addtion, it sets up all TDES key materials for subsequent encryption and decryption
723 There are 3 key options as follows:
724 KeyLength = 64, Keying option 1: K1 == K2 == K3 (Backward compatibility with DES)
725 KeyLength = 128, Keying option 2: K1 != K2 and K3 = K1 (Less Security)
726 KeyLength = 192 Keying option 3: K1 != K2 != K3 (Strongest)
728 If TdesContext is NULL, then ASSERT().
729 If Key is NULL, then ASSERT().
730 If KeyLength is not valid, then ASSERT().
732 @param[out] TdesContext Pointer to TDES context being initialized.
733 @param[in] Key Pointer to the user-supplied TDES key.
734 @param[in] KeyLength Length of TDES key in bits.
736 @retval TRUE TDES context initialization succeeded.
737 @retval FALSE TDES context initialization failed.
743 OUT VOID
*TdesContext
,
749 Performs TDES encryption on a data buffer of the specified size in ECB mode.
751 This function performs TDES encryption on data buffer pointed by Input, of specified
752 size of InputSize, in ECB mode.
753 InputSize must be multiple of block size (8 bytes). This function does not perform
754 padding. Caller must perform padding, if necessary, to ensure valid input data size.
755 TdesContext should be already correctly initialized by TdesInit(). Behavior with
756 invalid TDES context is undefined.
758 If TdesContext is NULL, then ASSERT().
759 If Input is NULL, then ASSERT().
760 If InputSize is not multiple of block size (8 bytes), then ASSERT().
761 If Output is NULL, then ASSERT().
763 @param[in] TdesContext Pointer to the TDES context.
764 @param[in] Input Pointer to the buffer containing the data to be encrypted.
765 @param[in] InputSize Size of the Input buffer in bytes.
766 @param[out] Output Pointer to a buffer that receives the TDES encryption output.
768 @retval TRUE TDES encryption succeeded.
769 @retval FALSE TDES encryption failed.
775 IN VOID
*TdesContext
,
776 IN CONST UINT8
*Input
,
782 Performs TDES decryption on a data buffer of the specified size in ECB mode.
784 This function performs TDES decryption on data buffer pointed by Input, of specified
785 size of InputSize, in ECB mode.
786 InputSize must be multiple of block size (8 bytes). This function does not perform
787 padding. Caller must perform padding, if necessary, to ensure valid input data size.
788 TdesContext should be already correctly initialized by TdesInit(). Behavior with
789 invalid TDES context is undefined.
791 If TdesContext is NULL, then ASSERT().
792 If Input is NULL, then ASSERT().
793 If InputSize is not multiple of block size (8 bytes), then ASSERT().
794 If Output is NULL, then ASSERT().
796 @param[in] TdesContext Pointer to the TDES context.
797 @param[in] Input Pointer to the buffer containing the data to be decrypted.
798 @param[in] InputSize Size of the Input buffer in bytes.
799 @param[out] Output Pointer to a buffer that receives the TDES decryption output.
801 @retval TRUE TDES decryption succeeded.
802 @retval FALSE TDES decryption failed.
808 IN VOID
*TdesContext
,
809 IN CONST UINT8
*Input
,
815 Performs TDES encryption on a data buffer of the specified size in CBC mode.
817 This function performs TDES encryption on data buffer pointed by Input, of specified
818 size of InputSize, in CBC mode.
819 InputSize must be multiple of block size (8 bytes). This function does not perform
820 padding. Caller must perform padding, if necessary, to ensure valid input data size.
821 Initialization vector should be one block size (8 bytes).
822 TdesContext should be already correctly initialized by TdesInit(). Behavior with
823 invalid TDES context is undefined.
825 If TdesContext is NULL, then ASSERT().
826 If Input is NULL, then ASSERT().
827 If InputSize is not multiple of block size (8 bytes), then ASSERT().
828 If Ivec is NULL, then ASSERT().
829 If Output is NULL, then ASSERT().
831 @param[in] TdesContext Pointer to the TDES context.
832 @param[in] Input Pointer to the buffer containing the data to be encrypted.
833 @param[in] InputSize Size of the Input buffer in bytes.
834 @param[in] Ivec Pointer to initialization vector.
835 @param[out] Output Pointer to a buffer that receives the TDES encryption output.
837 @retval TRUE TDES encryption succeeded.
838 @retval FALSE TDES encryption failed.
844 IN VOID
*TdesContext
,
845 IN CONST UINT8
*Input
,
847 IN CONST UINT8
*Ivec
,
852 Performs TDES decryption on a data buffer of the specified size in CBC mode.
854 This function performs TDES decryption on data buffer pointed by Input, of specified
855 size of InputSize, in CBC mode.
856 InputSize must be multiple of block size (8 bytes). This function does not perform
857 padding. Caller must perform padding, if necessary, to ensure valid input data size.
858 Initialization vector should be one block size (8 bytes).
859 TdesContext should be already correctly initialized by TdesInit(). Behavior with
860 invalid TDES context is undefined.
862 If TdesContext is NULL, then ASSERT().
863 If Input is NULL, then ASSERT().
864 If InputSize is not multiple of block size (8 bytes), then ASSERT().
865 If Ivec is NULL, then ASSERT().
866 If Output is NULL, then ASSERT().
868 @param[in] TdesContext Pointer to the TDES context.
869 @param[in] Input Pointer to the buffer containing the data to be encrypted.
870 @param[in] InputSize Size of the Input buffer in bytes.
871 @param[in] Ivec Pointer to initialization vector.
872 @param[out] Output Pointer to a buffer that receives the TDES encryption output.
874 @retval TRUE TDES decryption succeeded.
875 @retval FALSE TDES decryption failed.
881 IN VOID
*TdesContext
,
882 IN CONST UINT8
*Input
,
884 IN CONST UINT8
*Ivec
,
889 Retrieves the size, in bytes, of the context buffer required for AES operations.
891 @return The size, in bytes, of the context buffer required for AES operations.
901 Initializes user-supplied memory as AES context for subsequent use.
903 This function initializes user-supplied memory pointed by AesContext as AES context.
904 In addtion, it sets up all AES key materials for subsequent encryption and decryption
906 There are 3 options for key length, 128 bits, 192 bits, and 256 bits.
908 If AesContext is NULL, then ASSERT().
909 If Key is NULL, then ASSERT().
910 If KeyLength is not valid, then ASSERT().
912 @param[out] AesContext Pointer to AES context being initialized.
913 @param[in] Key Pointer to the user-supplied AES key.
914 @param[in] KeyLength Length of AES key in bits.
916 @retval TRUE AES context initialization succeeded.
917 @retval FALSE AES context initialization failed.
923 OUT VOID
*AesContext
,
929 Performs AES encryption on a data buffer of the specified size in ECB mode.
931 This function performs AES encryption on data buffer pointed by Input, of specified
932 size of InputSize, in ECB mode.
933 InputSize must be multiple of block size (16 bytes). This function does not perform
934 padding. Caller must perform padding, if necessary, to ensure valid input data size.
935 AesContext should be already correctly initialized by AesInit(). Behavior with
936 invalid AES context is undefined.
938 If AesContext is NULL, then ASSERT().
939 If Input is NULL, then ASSERT().
940 If InputSize is not multiple of block size (16 bytes), then ASSERT().
941 If Output is NULL, then ASSERT().
943 @param[in] AesContext Pointer to the AES context.
944 @param[in] Input Pointer to the buffer containing the data to be encrypted.
945 @param[in] InputSize Size of the Input buffer in bytes.
946 @param[out] Output Pointer to a buffer that receives the AES encryption output.
948 @retval TRUE AES encryption succeeded.
949 @retval FALSE AES encryption failed.
956 IN CONST UINT8
*Input
,
962 Performs AES decryption on a data buffer of the specified size in ECB mode.
964 This function performs AES decryption on data buffer pointed by Input, of specified
965 size of InputSize, in ECB mode.
966 InputSize must be multiple of block size (16 bytes). This function does not perform
967 padding. Caller must perform padding, if necessary, to ensure valid input data size.
968 AesContext should be already correctly initialized by AesInit(). Behavior with
969 invalid AES context is undefined.
971 If AesContext is NULL, then ASSERT().
972 If Input is NULL, then ASSERT().
973 If InputSize is not multiple of block size (16 bytes), then ASSERT().
974 If Output is NULL, then ASSERT().
976 @param[in] AesContext Pointer to the AES context.
977 @param[in] Input Pointer to the buffer containing the data to be decrypted.
978 @param[in] InputSize Size of the Input buffer in bytes.
979 @param[out] Output Pointer to a buffer that receives the AES decryption output.
981 @retval TRUE AES decryption succeeded.
982 @retval FALSE AES decryption failed.
989 IN CONST UINT8
*Input
,
995 Performs AES encryption on a data buffer of the specified size in CBC mode.
997 This function performs AES encryption on data buffer pointed by Input, of specified
998 size of InputSize, in CBC mode.
999 InputSize must be multiple of block size (16 bytes). This function does not perform
1000 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1001 Initialization vector should be one block size (16 bytes).
1002 AesContext should be already correctly initialized by AesInit(). Behavior with
1003 invalid AES context is undefined.
1005 If AesContext is NULL, then ASSERT().
1006 If Input is NULL, then ASSERT().
1007 If InputSize is not multiple of block size (16 bytes), then ASSERT().
1008 If Ivec is NULL, then ASSERT().
1009 If Output is NULL, then ASSERT().
1011 @param[in] AesContext Pointer to the AES context.
1012 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1013 @param[in] InputSize Size of the Input buffer in bytes.
1014 @param[in] Ivec Pointer to initialization vector.
1015 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1017 @retval TRUE AES encryption succeeded.
1018 @retval FALSE AES encryption failed.
1024 IN VOID
*AesContext
,
1025 IN CONST UINT8
*Input
,
1027 IN CONST UINT8
*Ivec
,
1032 Performs AES decryption on a data buffer of the specified size in CBC mode.
1034 This function performs AES decryption on data buffer pointed by Input, of specified
1035 size of InputSize, in CBC mode.
1036 InputSize must be multiple of block size (16 bytes). This function does not perform
1037 padding. Caller must perform padding, if necessary, to ensure valid input data size.
1038 Initialization vector should be one block size (16 bytes).
1039 AesContext should be already correctly initialized by AesInit(). Behavior with
1040 invalid AES context is undefined.
1042 If AesContext is NULL, then ASSERT().
1043 If Input is NULL, then ASSERT().
1044 If InputSize is not multiple of block size (16 bytes), then ASSERT().
1045 If Ivec is NULL, then ASSERT().
1046 If Output is NULL, then ASSERT().
1048 @param[in] AesContext Pointer to the AES context.
1049 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1050 @param[in] InputSize Size of the Input buffer in bytes.
1051 @param[in] Ivec Pointer to initialization vector.
1052 @param[out] Output Pointer to a buffer that receives the AES encryption output.
1054 @retval TRUE AES decryption succeeded.
1055 @retval FALSE AES decryption failed.
1061 IN VOID
*AesContext
,
1062 IN CONST UINT8
*Input
,
1064 IN CONST UINT8
*Ivec
,
1069 Retrieves the size, in bytes, of the context buffer required for ARC4 operations.
1071 @return The size, in bytes, of the context buffer required for ARC4 operations.
1076 Arc4GetContextSize (
1081 Initializes user-supplied memory as ARC4 context for subsequent use.
1083 This function initializes user-supplied memory pointed by Arc4Context as ARC4 context.
1084 In addtion, it sets up all ARC4 key materials for subsequent encryption and decryption
1087 If Arc4Context is NULL, then ASSERT().
1088 If Key is NULL, then ASSERT().
1089 If KeySize does not in the range of [5, 256] bytes, then ASSERT().
1091 @param[out] Arc4Context Pointer to ARC4 context being initialized.
1092 @param[in] Key Pointer to the user-supplied ARC4 key.
1093 @param[in] KeySize Size of ARC4 key in bytes.
1095 @retval TRUE ARC4 context initialization succeeded.
1096 @retval FALSE ARC4 context initialization failed.
1102 OUT VOID
*Arc4Context
,
1103 IN CONST UINT8
*Key
,
1108 Performs ARC4 encryption on a data buffer of the specified size.
1110 This function performs ARC4 encryption on data buffer pointed by Input, of specified
1112 Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
1113 invalid ARC4 context is undefined.
1115 If Arc4Context is NULL, then ASSERT().
1116 If Input is NULL, then ASSERT().
1117 If Output is NULL, then ASSERT().
1119 @param[in] Arc4Context Pointer to the ARC4 context.
1120 @param[in] Input Pointer to the buffer containing the data to be encrypted.
1121 @param[in] InputSize Size of the Input buffer in bytes.
1122 @param[out] Output Pointer to a buffer that receives the ARC4 encryption output.
1124 @retval TRUE ARC4 encryption succeeded.
1125 @retval FALSE ARC4 encryption failed.
1131 IN OUT VOID
*Arc4Context
,
1132 IN CONST UINT8
*Input
,
1138 Performs ARC4 decryption on a data buffer of the specified size.
1140 This function performs ARC4 decryption on data buffer pointed by Input, of specified
1142 Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
1143 invalid ARC4 context is undefined.
1145 If Arc4Context is NULL, then ASSERT().
1146 If Input is NULL, then ASSERT().
1147 If Output is NULL, then ASSERT().
1149 @param[in] Arc4Context Pointer to the ARC4 context.
1150 @param[in] Input Pointer to the buffer containing the data to be decrypted.
1151 @param[in] InputSize Size of the Input buffer in bytes.
1152 @param[out] Output Pointer to a buffer that receives the ARC4 decryption output.
1154 @retval TRUE ARC4 decryption succeeded.
1155 @retval FALSE ARC4 decryption failed.
1161 IN OUT VOID
*Arc4Context
,
1168 Resets the ARC4 context to the initial state.
1170 The function resets the ARC4 context to the state it had immediately after the
1171 ARC4Init() function call.
1172 Contrary to ARC4Init(), Arc4Reset() requires no secret key as input, but ARC4 context
1173 should be already correctly initialized by ARC4Init().
1175 If Arc4Context is NULL, then ASSERT().
1177 @param[in, out] Arc4Context Pointer to the ARC4 context.
1179 @retval TRUE ARC4 reset succeeded.
1180 @retval FALSE ARC4 reset failed.
1186 IN OUT VOID
*Arc4Context
1189 //=====================================================================================
1190 // Asymmetric Cryptography Primitive
1191 //=====================================================================================
1194 Allocates and initializes one RSA context for subsequent use.
1196 @return Pointer to the RSA context that has been initialized.
1197 If the allocations fails, RsaNew() returns NULL.
1207 Release the specified RSA context.
1209 If RsaContext is NULL, then ASSERT().
1211 @param[in] RsaContext Pointer to the RSA context to be released.
1221 Sets the tag-designated key component into the established RSA context.
1223 This function sets the tag-designated RSA key component into the established
1224 RSA context from the user-specified non-negative integer (octet string format
1225 represented in RSA PKCS#1).
1226 If BigNumber is NULL, then the specified key componenet in RSA context is cleared.
1228 If RsaContext is NULL, then ASSERT().
1230 @param[in, out] RsaContext Pointer to RSA context being set.
1231 @param[in] KeyTag Tag of RSA key component being set.
1232 @param[in] BigNumber Pointer to octet integer buffer.
1233 If NULL, then the specified key componenet in RSA
1235 @param[in] BnSize Size of big number buffer in bytes.
1236 If BigNumber is NULL, then it is ignored.
1238 @retval TRUE RSA key component was set successfully.
1239 @retval FALSE Invalid RSA key component tag.
1245 IN OUT VOID
*RsaContext
,
1246 IN RSA_KEY_TAG KeyTag
,
1247 IN CONST UINT8
*BigNumber
,
1252 Gets the tag-designated RSA key component from the established RSA context.
1254 This function retrieves the tag-designated RSA key component from the
1255 established RSA context as a non-negative integer (octet string format
1256 represented in RSA PKCS#1).
1257 If specified key component has not been set or has been cleared, then returned
1259 If the BigNumber buffer is too small to hold the contents of the key, FALSE
1260 is returned and BnSize is set to the required buffer size to obtain the key.
1262 If RsaContext is NULL, then ASSERT().
1263 If BnSize is NULL, then ASSERT().
1264 If BnSize is large enough but BigNumber is NULL, then ASSERT().
1266 @param[in, out] RsaContext Pointer to RSA context being set.
1267 @param[in] KeyTag Tag of RSA key component being set.
1268 @param[out] BigNumber Pointer to octet integer buffer.
1269 @param[in, out] BnSize On input, the size of big number buffer in bytes.
1270 On output, the size of data returned in big number buffer in bytes.
1272 @retval TRUE RSA key component was retrieved successfully.
1273 @retval FALSE Invalid RSA key component tag.
1274 @retval FALSE BnSize is too small.
1280 IN OUT VOID
*RsaContext
,
1281 IN RSA_KEY_TAG KeyTag
,
1282 OUT UINT8
*BigNumber
,
1283 IN OUT UINTN
*BnSize
1287 Generates RSA key components.
1289 This function generates RSA key components. It takes RSA public exponent E and
1290 length in bits of RSA modulus N as input, and generates all key components.
1291 If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
1293 Before this function can be invoked, pseudorandom number generator must be correctly
1294 initialized by RandomSeed().
1296 If RsaContext is NULL, then ASSERT().
1298 @param[in, out] RsaContext Pointer to RSA context being set.
1299 @param[in] ModulusLength Length of RSA modulus N in bits.
1300 @param[in] PublicExponent Pointer to RSA public exponent.
1301 @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
1303 @retval TRUE RSA key component was generated successfully.
1304 @retval FALSE Invalid RSA key component tag.
1310 IN OUT VOID
*RsaContext
,
1311 IN UINTN ModulusLength
,
1312 IN CONST UINT8
*PublicExponent
,
1313 IN UINTN PublicExponentSize
1317 Validates key components of RSA context.
1319 This function validates key compoents of RSA context in following aspects:
1320 - Whether p is a prime
1321 - Whether q is a prime
1323 - Whether d*e = 1 mod lcm(p-1,q-1)
1325 If RsaContext is NULL, then ASSERT().
1327 @param[in] RsaContext Pointer to RSA context to check.
1329 @retval TRUE RSA key components are valid.
1330 @retval FALSE RSA key components are not valid.
1340 Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
1342 This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
1344 If the Signature buffer is too small to hold the contents of signature, FALSE
1345 is returned and SigSize is set to the required buffer size to obtain the signature.
1347 If RsaContext is NULL, then ASSERT().
1348 If MessageHash is NULL, then ASSERT().
1349 If HashSize is not equal to the size of MD5, SHA-1, SHA-256, SHA-224, SHA-512 or SHA-384 digest, then ASSERT().
1350 If SigSize is large enough but Signature is NULL, then ASSERT().
1352 @param[in] RsaContext Pointer to RSA context for signature generation.
1353 @param[in] MessageHash Pointer to octet message hash to be signed.
1354 @param[in] HashSize Size of the message hash in bytes.
1355 @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
1356 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
1357 On output, the size of data returned in Signature buffer in bytes.
1359 @retval TRUE Signature successfully generated in PKCS1-v1_5.
1360 @retval FALSE Signature generation failed.
1361 @retval FALSE SigSize is too small.
1367 IN VOID
*RsaContext
,
1368 IN CONST UINT8
*MessageHash
,
1370 OUT UINT8
*Signature
,
1371 IN OUT UINTN
*SigSize
1375 Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
1378 If RsaContext is NULL, then ASSERT().
1379 If MessageHash is NULL, then ASSERT().
1380 If Signature is NULL, then ASSERT().
1381 If HashSize is not equal to the size of MD5, SHA-1, SHA-256, SHA-224, SHA-512 or SHA-384 digest, then ASSERT().
1383 @param[in] RsaContext Pointer to RSA context for signature verification.
1384 @param[in] MessageHash Pointer to octet message hash to be checked.
1385 @param[in] HashSize Size of the message hash in bytes.
1386 @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
1387 @param[in] SigSize Size of signature in bytes.
1389 @retval TRUE Valid signature encoded in PKCS1-v1_5.
1390 @retval FALSE Invalid signature or invalid RSA context.
1396 IN VOID
*RsaContext
,
1397 IN CONST UINT8
*MessageHash
,
1399 IN UINT8
*Signature
,
1404 Retrieve the RSA Private Key from the password-protected PEM key data.
1406 @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
1407 @param[in] PemSize Size of the PEM key data in bytes.
1408 @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
1409 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1410 RSA private key component. Use RsaFree() function to free the
1413 If PemData is NULL, then ASSERT().
1414 If RsaContext is NULL, then ASSERT().
1416 @retval TRUE RSA Private Key was retrieved successfully.
1417 @retval FALSE Invalid PEM key data or incorrect password.
1422 RsaGetPrivateKeyFromPem (
1423 IN CONST UINT8
*PemData
,
1425 IN CONST CHAR8
*Password
,
1426 OUT VOID
**RsaContext
1430 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
1432 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1433 @param[in] CertSize Size of the X509 certificate in bytes.
1434 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
1435 RSA public key component. Use RsaFree() function to free the
1438 If Cert is NULL, then ASSERT().
1439 If RsaContext is NULL, then ASSERT().
1441 @retval TRUE RSA Public Key was retrieved successfully.
1442 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
1447 RsaGetPublicKeyFromX509 (
1448 IN CONST UINT8
*Cert
,
1450 OUT VOID
**RsaContext
1454 Retrieve the subject bytes from one X.509 certificate.
1456 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1457 @param[in] CertSize Size of the X509 certificate in bytes.
1458 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
1459 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
1460 and the size of buffer returned CertSubject on output.
1462 If Cert is NULL, then ASSERT().
1463 If SubjectSize is NULL, then ASSERT().
1465 @retval TRUE The certificate subject retrieved successfully.
1466 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
1467 The SubjectSize will be updated with the required size.
1472 X509GetSubjectName (
1473 IN CONST UINT8
*Cert
,
1475 OUT UINT8
*CertSubject
,
1476 IN OUT UINTN
*SubjectSize
1480 Verify one X509 certificate was issued by the trusted CA.
1482 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
1483 @param[in] CertSize Size of the X509 certificate in bytes.
1484 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
1485 @param[in] CACertSize Size of the CA Certificate in bytes.
1487 If Cert is NULL, then ASSERT().
1488 If CACert is NULL, then ASSERT().
1490 @retval TRUE The certificate was issued by the trusted CA.
1491 @retval FALSE Invalid certificate or the certificate was not issued by the given
1498 IN CONST UINT8
*Cert
,
1500 IN CONST UINT8
*CACert
,
1505 Verifies the validility of a PKCS#7 signed data as described in "PKCS #7: Cryptographic
1506 Message Syntax Standard".
1508 If P7Data is NULL, then ASSERT().
1510 @param[in] P7Data Pointer to the PKCS#7 message to verify.
1511 @param[in] P7Size Size of the PKCS#7 message in bytes.
1512 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
1513 is used for certificate chain verification.
1514 @param[in] CertSize Size of the trusted certificate in bytes.
1515 @param[in] InData Pointer to the content to be verified.
1516 @param[in] DataSize Size of InData in bytes.
1518 @retval TRUE The specified PKCS#7 signed data is valid.
1519 @retval FALSE Invalid PKCS#7 signed data.
1525 IN CONST UINT8
*P7Data
,
1527 IN CONST UINT8
*TrustedCert
,
1529 IN CONST UINT8
*InData
,
1533 //=====================================================================================
1534 // DH Key Exchange Primitive
1535 //=====================================================================================
1538 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
1540 @return Pointer to the Diffie-Hellman Context that has been initialized.
1541 If the allocations fails, DhNew() returns NULL.
1551 Release the specified DH context.
1553 If DhContext is NULL, then ASSERT().
1555 @param[in] DhContext Pointer to the DH context to be released.
1565 Generates DH parameter.
1567 Given generator g, and length of prime number p in bits, this function generates p,
1568 and sets DH context according to value of g and p.
1570 Before this function can be invoked, pseudorandom number generator must be correctly
1571 initialized by RandomSeed().
1573 If DhContext is NULL, then ASSERT().
1574 If Prime is NULL, then ASSERT().
1576 @param[in, out] DhContext Pointer to the DH context.
1577 @param[in] Generator Value of generator.
1578 @param[in] PrimeLength Length in bits of prime to be generated.
1579 @param[out] Prime Pointer to the buffer to receive the generated prime number.
1581 @retval TRUE DH pamameter generation succeeded.
1582 @retval FALSE Value of Generator is not supported.
1583 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
1588 DhGenerateParameter (
1589 IN OUT VOID
*DhContext
,
1591 IN UINTN PrimeLength
,
1596 Sets generator and prime parameters for DH.
1598 Given generator g, and prime number p, this function and sets DH
1599 context accordingly.
1601 If DhContext is NULL, then ASSERT().
1602 If Prime is NULL, then ASSERT().
1604 @param[in, out] DhContext Pointer to the DH context.
1605 @param[in] Generator Value of generator.
1606 @param[in] PrimeLength Length in bits of prime to be generated.
1607 @param[in] Prime Pointer to the prime number.
1609 @retval TRUE DH pamameter setting succeeded.
1610 @retval FALSE Value of Generator is not supported.
1611 @retval FALSE Value of Generator is not suitable for the Prime.
1612 @retval FALSE Value of Prime is not a prime number.
1613 @retval FALSE Value of Prime is not a safe prime number.
1619 IN OUT VOID
*DhContext
,
1621 IN UINTN PrimeLength
,
1622 IN CONST UINT8
*Prime
1626 Generates DH public key.
1628 This function generates random secret exponent, and computes the public key, which is
1629 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
1630 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
1631 PublicKeySize is set to the required buffer size to obtain the public key.
1633 If DhContext is NULL, then ASSERT().
1634 If PublicKeySize is NULL, then ASSERT().
1635 If PublicKeySize is large enough but PublicKey is NULL, then ASSERT().
1637 @param[in, out] DhContext Pointer to the DH context.
1638 @param[out] PublicKey Pointer to the buffer to receive generated public key.
1639 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
1640 On output, the size of data returned in PublicKey buffer in bytes.
1642 @retval TRUE DH public key generation succeeded.
1643 @retval FALSE DH public key generation failed.
1644 @retval FALSE PublicKeySize is not large enough.
1650 IN OUT VOID
*DhContext
,
1651 OUT UINT8
*PublicKey
,
1652 IN OUT UINTN
*PublicKeySize
1656 Computes exchanged common key.
1658 Given peer's public key, this function computes the exchanged common key, based on its own
1659 context including value of prime modulus and random secret exponent.
1661 If DhContext is NULL, then ASSERT().
1662 If PeerPublicKey is NULL, then ASSERT().
1663 If KeySize is NULL, then ASSERT().
1664 If KeySize is large enough but Key is NULL, then ASSERT().
1666 @param[in, out] DhContext Pointer to the DH context.
1667 @param[in] PeerPublicKey Pointer to the peer's public key.
1668 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
1669 @param[out] Key Pointer to the buffer to receive generated key.
1670 @param[in, out] KeySize On input, the size of Key buffer in bytes.
1671 On output, the size of data returned in Key buffer in bytes.
1673 @retval TRUE DH exchanged key generation succeeded.
1674 @retval FALSE DH exchanged key generation failed.
1675 @retval FALSE KeySize is not large enough.
1681 IN OUT VOID
*DhContext
,
1682 IN CONST UINT8
*PeerPublicKey
,
1683 IN UINTN PeerPublicKeySize
,
1685 IN OUT UINTN
*KeySize
1688 //=====================================================================================
1689 // Pseudo-Random Generation Primitive
1690 //=====================================================================================
1693 Sets up the seed value for the pseudorandom number generator.
1695 This function sets up the seed value for the pseudorandom number generator.
1696 If Seed is not NULL, then the seed passed in is used.
1697 If Seed is NULL, then default seed is used.
1699 @param[in] Seed Pointer to seed value.
1700 If NULL, default seed is used.
1701 @param[in] SeedSize Size of seed value.
1702 If Seed is NULL, this parameter is ignored.
1704 @retval TRUE Pseudorandom number generator has enough entropy for random generation.
1705 @retval FALSE Pseudorandom number generator does not have enough entropy for random generation.
1711 IN CONST UINT8
*Seed OPTIONAL
,
1716 Generates a pseudorandom byte stream of the specified size.
1718 If Output is NULL, then ASSERT().
1720 @param[out] Output Pointer to buffer to receive random value.
1721 @param[in] Size Size of randome bytes to generate.
1723 @retval TRUE Pseudorandom byte stream generated successfully.
1724 @retval FALSE Pseudorandom number generator fails to generate due to lack of entropy.
1734 #endif // __BASE_CRYPT_LIB_H__