]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/CryptRuntimeDxe/CryptRuntime.c
Add CryptoPkg (from UDK2010.UP3)
[mirror_edk2.git] / CryptoPkg / CryptRuntimeDxe / CryptRuntime.c
diff --git a/CryptoPkg/CryptRuntimeDxe/CryptRuntime.c b/CryptoPkg/CryptRuntimeDxe/CryptRuntime.c
new file mode 100644 (file)
index 0000000..ff20da4
--- /dev/null
@@ -0,0 +1,248 @@
+/** @file\r
+  Runtime Cryptographic Driver Implementation, which produce one crypto\r
+  protocol.\r
+\r
+Copyright (c) 2010, 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
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "CryptRuntime.h"\r
+\r
+//\r
+// The handle onto which the Runtime Crypt Protocol instance is installed\r
+//\r
+EFI_HANDLE  mRuntimeCryptHandle = NULL;\r
+\r
+//\r
+// The Runtime Crypt Protocol instance produced by this driver\r
+//\r
+EFI_RUNTIME_CRYPT_PROTOCOL  mRuntimeCryptProtocol = {\r
+  RuntimeCryptSha256GetContextSize,\r
+  RuntimeCryptSha256Init,\r
+  RuntimeCryptSha256Update,\r
+  RuntimeCryptSha256Final,\r
+  RuntimeCryptRsaNew,\r
+  RuntimeCryptRsaFree,\r
+  RuntimeCryptRsaSetKey,\r
+  RuntimeCryptRsaPkcs1Verify\r
+};\r
+\r
+/**\r
+  Retrieves the size, in bytes, of the context buffer required for SHA-256 operations.\r
+\r
+  @return  The size, in bytes, of the context buffer required for SHA-256 operations.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+RuntimeCryptSha256GetContextSize (\r
+  VOID\r
+  )\r
+{\r
+  return Sha256GetContextSize ();\r
+}\r
+\r
+/**\r
+  Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for\r
+  subsequent use.\r
+\r
+  If Sha256Context is NULL, then ASSERT().\r
+\r
+  @param[in, out]  Sha256Context  Pointer to SHA-256 Context being initialized.\r
+\r
+  @retval TRUE   SHA-256 context initialization succeeded.\r
+  @retval FALSE  SHA-256 context initialization failed.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RuntimeCryptSha256Init (\r
+  IN OUT  VOID  *Sha256Context\r
+  )\r
+{\r
+  return Sha256Init (Sha256Context);\r
+}\r
+\r
+/**\r
+  Performs SHA-256 digest on a data buffer of the specified length. This function can\r
+  be called multiple times to compute the digest of long or discontinuous data streams.\r
+\r
+  If Sha256Context is NULL, then ASSERT().\r
+\r
+  @param[in, out]  Sha256Context  Pointer to the SHA-256 context.\r
+  @param[in]       Data           Pointer to the buffer containing the data to be hashed.\r
+  @param[in]       DataLength     Length of Data buffer in bytes.\r
+\r
+  @retval TRUE   SHA-256 data digest succeeded.\r
+  @retval FALSE  Invalid SHA-256 context. After Sha256Final function has been called, the\r
+                 SHA-256 context cannot be reused.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RuntimeCryptSha256Update (\r
+  IN OUT  VOID        *Sha256Context,\r
+  IN      CONST VOID  *Data,\r
+  IN      UINTN       DataLength\r
+  )\r
+{\r
+  return Sha256Update (Sha256Context, Data, DataLength);\r
+}\r
+\r
+/**\r
+  Completes SHA-256 hash computation and retrieves the digest value into the specified\r
+  memory. After this function has been called, the SHA-256 context cannot be used again.\r
+\r
+  If Sha256Context is NULL, then ASSERT().\r
+  If HashValue is NULL, then ASSERT().\r
+\r
+  @param[in, out]  Sha256Context  Pointer to SHA-256 context\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
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RuntimeCryptSha256Final (\r
+  IN OUT  VOID   *Sha256Context,\r
+  OUT     UINT8  *HashValue\r
+  )\r
+{\r
+  return Sha256Final (Sha256Context, HashValue);\r
+}\r
+\r
+/**\r
+  Allocates and Initializes one RSA Context for subsequent use.\r
+\r
+  @return  Pointer to the RSA Context that has been initialized.\r
+           If the allocations fails, RsaNew() returns NULL.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+RuntimeCryptRsaNew (\r
+  VOID\r
+  )\r
+{\r
+  return RsaNew ();\r
+}\r
+\r
+/**\r
+  Release the specified RSA Context.\r
+\r
+  @param[in]  RsaContext  Pointer to the RSA context to be released.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+RuntimeCryptRsaFree (\r
+  IN  VOID  *RsaContext\r
+  )\r
+{\r
+  RsaFree (RsaContext);\r
+}\r
+\r
+/**\r
+  Sets the tag-designated RSA key component into the established RSA context from\r
+  the user-specified nonnegative integer (octet string format represented in RSA\r
+  PKCS#1).\r
+\r
+  If RsaContext is NULL, then ASSERT().\r
+\r
+  @param[in, out]  RsaContext  Pointer to RSA context being set.\r
+  @param[in]       KeyTag      Tag of RSA key component being set.\r
+  @param[in]       BigNumber   Pointer to octet integer buffer.\r
+  @param[in]       BnLength    Length of big number buffer in bytes.\r
+\r
+  @return  TRUE   RSA key component was set successfully.\r
+  @return  FALSE  Invalid RSA key component tag.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RuntimeCryptRsaSetKey (\r
+  IN OUT VOID         *RsaContext,\r
+  IN     RSA_KEY_TAG  KeyTag,\r
+  IN     CONST UINT8  *BigNumber,\r
+  IN     UINTN        BnLength\r
+  )\r
+{\r
+  return RsaSetKey (RsaContext, KeyTag, BigNumber, BnLength);\r
+}\r
+\r
+/**\r
+  Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in\r
+  RSA PKCS#1.\r
+\r
+  If RsaContext is NULL, then ASSERT().\r
+  If MessageHash is NULL, then ASSERT().\r
+  If Signature is NULL, then ASSERT().\r
+  If HashLength is not equal to the size of MD5, SHA-1 or SHA-256 digest, then ASSERT().\r
+\r
+  @param[in]  RsaContext   Pointer to RSA context for signature verification.\r
+  @param[in]  MessageHash  Pointer to octet message hash to be checked.\r
+  @param[in]  HashLength   Length of the message hash in bytes.\r
+  @param[in]  Signature    Pointer to RSA PKCS1-v1_5 signature to be verified.\r
+  @param[in]  SigLength    Length of signature in bytes.\r
+\r
+  @return  TRUE   Valid signature encoded in PKCS1-v1_5.\r
+  @return  FALSE  Invalid signature or invalid RSA context.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+RuntimeCryptRsaPkcs1Verify (\r
+  IN  VOID         *RsaContext,\r
+  IN  CONST UINT8  *MessageHash,\r
+  IN  UINTN        HashLength,\r
+  IN  UINT8        *Signature,\r
+  IN  UINTN        SigLength\r
+  )\r
+{\r
+  return RsaPkcs1Verify (RsaContext, MessageHash, HashLength, Signature, SigLength);\r
+}\r
+\r
+/**\r
+  Entry Point for Runtime Cryptographic Driver.\r
+\r
+  This function installs Runtime Crypt Protocol.\r
+\r
+  @param ImageHandle     Image handle of this driver.\r
+  @param SystemTable     a Pointer to the EFI System Table.\r
+\r
+  @retval  EFI_SUCEESS  Runtime Crypt Protocol is successfully installed\r
+  @return  Others       Some error occurs when installing Runtime Crypt Protocol.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CryptRuntimeDriverInitialize (\r
+  IN EFI_HANDLE                            ImageHandle,\r
+  IN EFI_SYSTEM_TABLE                      *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Install the Runtime Crypt Protocol onto a new handle\r
+  //\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &mRuntimeCryptHandle,\r
+                  &gEfiRuntimeCryptProtocolGuid,\r
+                  &mRuntimeCryptProtocol,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
+}\r