]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg: Add PKCS5 PBKDF2 interface for password derivation.
[mirror_edk2.git] / CryptoPkg / Include / Library / BaseCryptLib.h
index bc36ac7fd1810636e9461ad7bbb1fc4a0a3f1822..67837aec1dd284ff93bb3eab406d60fa490e64f5 100644 (file)
@@ -4,7 +4,7 @@
   primitives (Hash Serials, HMAC, RSA, Diffie-Hellman, etc) for UEFI security\r
   functionality enabling.\r
 \r
-Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -18,6 +18,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #ifndef __BASE_CRYPT_LIB_H__\r
 #define __BASE_CRYPT_LIB_H__\r
 \r
+#include <Uefi/UefiBaseType.h>\r
+\r
 ///\r
 /// MD4 digest size in bytes\r
 ///\r
@@ -190,6 +192,32 @@ Md4Final (
   OUT     UINT8  *HashValue\r
   );\r
 \r
+/**\r
+  Computes the MD4 message digest of a input data buffer.\r
+\r
+  This function performs the MD4 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]   Data        Pointer to the buffer containing the data to be hashed.\r
+  @param[in]   DataSize    Size of Data buffer in bytes.\r
+  @param[out]  HashValue   Pointer to a buffer that receives the MD4 digest\r
+                           value (16 bytes).\r
+\r
+  @retval TRUE   MD4 digest computation succeeded.\r
+  @retval FALSE  MD4 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Md4HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  OUT  UINT8       *HashValue\r
+  );\r
+\r
 /**\r
   Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.\r
 \r
@@ -304,6 +332,32 @@ Md5Final (
   OUT     UINT8  *HashValue\r
   );\r
 \r
+/**\r
+  Computes the MD5 message digest of a input data buffer.\r
+\r
+  This function performs the MD5 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]   Data        Pointer to the buffer containing the data to be hashed.\r
+  @param[in]   DataSize    Size of Data buffer in bytes.\r
+  @param[out]  HashValue   Pointer to a buffer that receives the MD5 digest\r
+                           value (16 bytes).\r
+\r
+  @retval TRUE   MD5 digest computation succeeded.\r
+  @retval FALSE  MD5 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Md5HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  OUT  UINT8       *HashValue\r
+  );\r
+\r
 /**\r
   Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.\r
 \r
@@ -418,6 +472,32 @@ Sha1Final (
   OUT     UINT8  *HashValue\r
   );\r
 \r
+/**\r
+  Computes the SHA-1 message digest of a input data buffer.\r
+\r
+  This function performs the SHA-1 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]   Data        Pointer to the buffer containing the data to be hashed.\r
+  @param[in]   DataSize    Size of Data buffer in bytes.\r
+  @param[out]  HashValue   Pointer to a buffer that receives the SHA-1 digest\r
+                           value (20 bytes).\r
+\r
+  @retval TRUE   SHA-1 digest computation succeeded.\r
+  @retval FALSE  SHA-1 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha1HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  OUT  UINT8       *HashValue\r
+  );\r
+\r
 /**\r
   Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.\r
 \r
@@ -523,6 +603,32 @@ Sha256Final (
   OUT     UINT8  *HashValue\r
   );\r
 \r
+/**\r
+  Computes the SHA-256 message digest of a input data buffer.\r
+\r
+  This function performs the SHA-256 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]   Data        Pointer to the buffer containing the data to be hashed.\r
+  @param[in]   DataSize    Size of Data buffer in bytes.\r
+  @param[out]  HashValue   Pointer to a buffer that receives the SHA-256 digest\r
+                           value (32 bytes).\r
+\r
+  @retval TRUE   SHA-256 digest computation succeeded.\r
+  @retval FALSE  SHA-256 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha256HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  OUT  UINT8       *HashValue\r
+  );\r
+\r
 /**\r
   Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.\r
 \r
@@ -628,6 +734,32 @@ Sha384Final (
   OUT     UINT8  *HashValue\r
   );\r
 \r
+/**\r
+  Computes the SHA-384 message digest of a input data buffer.\r
+\r
+  This function performs the SHA-384 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]   Data        Pointer to the buffer containing the data to be hashed.\r
+  @param[in]   DataSize    Size of Data buffer in bytes.\r
+  @param[out]  HashValue   Pointer to a buffer that receives the SHA-384 digest\r
+                           value (48 bytes).\r
+\r
+  @retval TRUE   SHA-384 digest computation succeeded.\r
+  @retval FALSE  SHA-384 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha384HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  OUT  UINT8       *HashValue\r
+  );\r
+\r
 /**\r
   Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.\r
 \r
@@ -733,6 +865,32 @@ Sha512Final (
   OUT     UINT8  *HashValue\r
   );\r
 \r
+/**\r
+  Computes the SHA-512 message digest of a input data buffer.\r
+\r
+  This function performs the SHA-512 message digest of a given data buffer, and places\r
+  the digest value into the specified memory.\r
+\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]   Data        Pointer to the buffer containing the data to be hashed.\r
+  @param[in]   DataSize    Size of Data buffer in bytes.\r
+  @param[out]  HashValue   Pointer to a buffer that receives the SHA-512 digest\r
+                           value (64 bytes).\r
+\r
+  @retval TRUE   SHA-512 digest computation succeeded.\r
+  @retval FALSE  SHA-512 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Sha512HashAll (\r
+  IN   CONST VOID  *Data,\r
+  IN   UINTN       DataSize,\r
+  OUT  UINT8       *HashValue\r
+  );\r
+\r
 //=====================================================================================\r
 //    MAC (Message Authentication Code) Primitive\r
 //=====================================================================================\r
@@ -973,6 +1131,124 @@ HmacSha1Final (
   OUT     UINT8  *HmacValue\r
   );\r
 \r
+/**\r
+  Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.\r
+\r
+  If this interface is not supported, then return zero.\r
+\r
+  @return  The size, in bytes, of the context buffer required for HMAC-SHA256 operations.\r
+  @retval  0   This interface is not supported.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+HmacSha256GetContextSize (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for\r
+  subsequent use.\r
+\r
+  If HmacSha256Context is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[out]  HmacSha256Context  Pointer to HMAC-SHA256 context being initialized.\r
+  @param[in]   Key                Pointer to the user-supplied key.\r
+  @param[in]   KeySize            Key size in bytes.\r
+\r
+  @retval TRUE   HMAC-SHA256 context initialization succeeded.\r
+  @retval FALSE  HMAC-SHA256 context initialization failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+HmacSha256Init (\r
+  OUT  VOID         *HmacSha256Context,\r
+  IN   CONST UINT8  *Key,\r
+  IN   UINTN        KeySize\r
+  );\r
+\r
+/**\r
+  Makes a copy of an existing HMAC-SHA256 context.\r
+\r
+  If HmacSha256Context is NULL, then return FALSE.\r
+  If NewHmacSha256Context is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]  HmacSha256Context     Pointer to HMAC-SHA256 context being copied.\r
+  @param[out] NewHmacSha256Context  Pointer to new HMAC-SHA256 context.\r
+\r
+  @retval TRUE   HMAC-SHA256 context copy succeeded.\r
+  @retval FALSE  HMAC-SHA256 context copy failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+HmacSha256Duplicate (\r
+  IN   CONST VOID  *HmacSha256Context,\r
+  OUT  VOID        *NewHmacSha256Context\r
+  );\r
+\r
+/**\r
+  Digests the input data and updates HMAC-SHA256 context.\r
+\r
+  This function performs HMAC-SHA256 digest on a data buffer of the specified size.\r
+  It can be called multiple times to compute the digest of long or discontinuous data streams.\r
+  HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not\r
+  be finalized by HmacSha256Final(). Behavior with invalid context is undefined.\r
+\r
+  If HmacSha256Context is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in, out]  HmacSha256Context Pointer to the HMAC-SHA256 context.\r
+  @param[in]       Data              Pointer to the buffer containing the data to be digested.\r
+  @param[in]       DataSize          Size of Data buffer in bytes.\r
+\r
+  @retval TRUE   HMAC-SHA256 data digest succeeded.\r
+  @retval FALSE  HMAC-SHA256 data digest failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+HmacSha256Update (\r
+  IN OUT  VOID        *HmacSha256Context,\r
+  IN      CONST VOID  *Data,\r
+  IN      UINTN       DataSize\r
+  );\r
+\r
+/**\r
+  Completes computation of the HMAC-SHA256 digest value.\r
+\r
+  This function completes HMAC-SHA256 hash computation and retrieves the digest value into\r
+  the specified memory. After this function has been called, the HMAC-SHA256 context cannot\r
+  be used again.\r
+  HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should\r
+  not be finalized by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.\r
+\r
+  If HmacSha256Context is NULL, then return FALSE.\r
+  If HashValue is NULL, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in, out]  HmacSha256Context  Pointer to the HMAC-SHA256 context.\r
+  @param[out]      HashValue          Pointer to a buffer that receives the HMAC-SHA256 digest\r
+                                      value (32 bytes).\r
+\r
+  @retval TRUE   HMAC-SHA256 digest computation succeeded.\r
+  @retval FALSE  HMAC-SHA256 digest computation failed.\r
+  @retval FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+HmacSha256Final (\r
+  IN OUT  VOID   *HmacSha256Context,\r
+  OUT     UINT8  *HmacValue\r
+  );\r
+\r
 //=====================================================================================\r
 //    Symmetric Cryptography Primitive\r
 //=====================================================================================\r
@@ -1631,6 +1907,8 @@ RsaGenerateKey (
 \r
 /**\r
   Validates key components of RSA context.\r
+  NOTE: This function performs integrity checks on all the RSA key material, so\r
+        the RSA key structure must contain all the private key data.\r
 \r
   This function validates key compoents of RSA context in following aspects:\r
   - Whether p is a prime\r
@@ -1859,7 +2137,7 @@ X509ConstructCertificate (
   If X509Stack is NULL, then return FALSE.\r
   If this interface is not supported, then return FALSE.\r
 \r
-  @param[in, out]  X509Stack  On input, pointer to an existing X509 stack object.\r
+  @param[in, out]  X509Stack  On input, pointer to an existing or NULL X509 stack object.\r
                               On output, pointer to the X509 stack object with new\r
                               inserted X509 certificate.\r
   @param           ...        A list of DER-encoded single certificate data followed\r
@@ -1932,6 +2210,46 @@ X509GetTBSCert (
   OUT UINTN        *TBSCertSize\r
   );\r
 \r
+/**\r
+  Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0\r
+  password based encryption key derivation function PBKDF2, as specified in RFC 2898.\r
+\r
+  If Password or Salt or OutKey is NULL, then return FALSE.\r
+  If the hash algorithm could not be determined, then return FALSE.\r
+  If this interface is not supported, then return FALSE.\r
+\r
+  @param[in]  PasswordLength  Length of input password in bytes.\r
+  @param[in]  Password        Pointer to the array for the password.\r
+  @param[in]  SaltLength      Size of the Salt in bytes.\r
+  @param[in]  Salt            Pointer to the Salt.\r
+  @param[in]  IterationCount  Number of iterations to perform. Its value should be\r
+                              greater than or equal to 1.\r
+  @param[in]  DigestSize      Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).\r
+                              NOTE: DigestSize will be used to determine the hash algorithm.\r
+                                    Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.\r
+  @param[in]  KeyLength       Size of the derived key buffer in bytes.\r
+  @param[out] OutKey          Pointer to the output derived key buffer.\r
+\r
+  @retval  TRUE   A key was derived successfully.\r
+  @retval  FALSE  One of the pointers was NULL or one of the sizes was too large.\r
+  @retval  FALSE  The hash algorithm could not be determined from the digest size.\r
+  @retval  FALSE  The key derivation operation failed.\r
+  @retval  FALSE  This interface is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Pkcs5HashPassword (\r
+  IN  UINTN        PasswordLength,\r
+  IN  CONST CHAR8  *Password,\r
+  IN  UINTN        SaltLength,\r
+  IN  CONST UINT8  *Salt,\r
+  IN  UINTN        IterationCount,\r
+  IN  UINTN        DigestSize,\r
+  IN  UINTN        KeyLength,\r
+  OUT UINT8        *OutKey\r
+  );\r
+\r
 /**\r
   Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:\r
   Cryptographic Message Syntax Standard". The input signed data could be wrapped\r
@@ -1980,6 +2298,36 @@ Pkcs7FreeSigners (
   IN  UINT8        *Certs\r
   );\r
 \r
+/**\r
+  Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:\r
+  Cryptographic Message Syntax Standard", and outputs two certificate lists chained and\r
+  unchained to the signer's certificates.\r
+  The input signed data could be wrapped in a ContentInfo structure.\r
+\r
+  @param[in]  P7Data            Pointer to the PKCS#7 message.\r
+  @param[in]  P7Length          Length of the PKCS#7 message in bytes.\r
+  @param[out] SignerChainCerts  Pointer to the certificates list chained to signer's\r
+                                certificate. It's caller's responsiblity to free the buffer.\r
+  @param[out] ChainLength       Length of the chained certificates list buffer in bytes.\r
+  @param[out] UnchainCerts      Pointer to the unchained certificates lists. It's caller's\r
+                                responsiblity to free the buffer.\r
+  @param[out] UnchainLength     Length of the unchained certificates list buffer in bytes.\r
+\r
+  @retval  TRUE         The operation is finished successfully.\r
+  @retval  FALSE        Error occurs during the operation.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+Pkcs7GetCertificatesList (\r
+  IN  CONST UINT8  *P7Data,\r
+  IN  UINTN        P7Length,\r
+  OUT UINT8        **SignerChainCerts,\r
+  OUT UINTN        *ChainLength,\r
+  OUT UINT8        **UnchainCerts,\r
+  OUT UINTN        *UnchainLength\r
+  );\r
+\r
 /**\r
   Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message\r
   Syntax Standard, version 1.5". This interface is only intended to be used for\r
@@ -2053,6 +2401,35 @@ Pkcs7Verify (
   IN  UINTN        DataLength\r
   );\r
 \r
+/**\r
+  Extracts the attached content from a PKCS#7 signed data if existed. The input signed\r
+  data could be wrapped in a ContentInfo structure.\r
+\r
+  If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,\r
+  then return FAlSE. If the P7Data is not correctly formatted, then return FALSE.\r
+\r
+  Caution: This function may receive untrusted input. So this function will do\r
+           basic check for PKCS#7 data structure.\r
+\r
+  @param[in]   P7Data       Pointer to the PKCS#7 signed data to process.\r
+  @param[in]   P7Length     Length of the PKCS#7 signed data in bytes.\r
+  @param[out]  Content      Pointer to the extracted content from the PKCS#7 signedData.\r
+                            It's caller's responsiblity to free the buffer.\r
+  @param[out]  ContentSize  The size of the extracted content in bytes.\r
+\r
+  @retval     TRUE          The P7Data was correctly formatted for processing.\r
+  @retval     FALSE         The P7Data was not correctly formatted for processing.\r
+\r
+*/\r
+BOOLEAN\r
+EFIAPI\r
+Pkcs7GetAttachedContent (\r
+  IN  CONST UINT8  *P7Data,\r
+  IN  UINTN        P7Length,\r
+  OUT VOID         **Content,\r
+  OUT UINTN        *ContentSize\r
+  );\r
+\r
 /**\r
   Verifies the validility of a PE/COFF Authenticode Signature as described in "Windows\r
   Authenticode Portable Executable Signature Format".\r
@@ -2333,4 +2710,4 @@ RandomBytes (
   IN   UINTN  Size\r
   );\r
 \r
-#endif // __BASE_CRYPT_LIB_H__
\ No newline at end of file
+#endif // __BASE_CRYPT_LIB_H__\r