]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Application/RngTest/RngTest.c
Add UEFI RNG Protocol support. The driver will leverage Intel Secure Key technology...
[mirror_edk2.git] / SecurityPkg / Application / RngTest / RngTest.c
diff --git a/SecurityPkg/Application/RngTest/RngTest.c b/SecurityPkg/Application/RngTest/RngTest.c
new file mode 100644 (file)
index 0000000..25edece
--- /dev/null
@@ -0,0 +1,231 @@
+/** @file\r
+  UEFI RNG (Random Number Generator) Protocol test application.\r
+\r
+Copyright (c) 2013, 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 <Uefi.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/UefiApplicationEntryPoint.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Protocol/Rng.h>\r
+\r
+/**\r
+  The user Entry Point for Application. The user code starts with this function\r
+  as the real entry point for the application.\r
+\r
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
+  @param[in] SystemTable    A pointer to the EFI System Table.\r
+  \r
+  @retval EFI_SUCCESS       The entry point is executed successfully.\r
+  @retval other             Some error occurs when executing this entry point.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UefiMain (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS         Status;\r
+  EFI_RNG_PROTOCOL   *Rng;\r
+  UINTN              RngAlgListSize;\r
+  EFI_RNG_ALGORITHM  RngAlgList[10];\r
+  EFI_RNG_ALGORITHM  *PtrRngAlg;\r
+  UINTN              RngAlgCount;\r
+  UINT8              *Rand;\r
+  UINTN              RandSize;\r
+  UINTN              Index;\r
+  UINTN              Index2;\r
+\r
+  Status    = EFI_SUCCESS;\r
+  PtrRngAlg = NULL;\r
+  Rand      = NULL;\r
+    \r
+  Print (L"UEFI RNG Protocol Testing :\n");\r
+  Print (L"----------------------------\n");\r
+\r
+  //-----------------------------------------\r
+  // Basic UEFI RNG Protocol Test\r
+  //-----------------------------------------\r
+  Print (L" -- Locate UEFI RNG Protocol : ");\r
+  Status = gBS->LocateProtocol (&gEfiRngProtocolGuid, NULL, (VOID **)&Rng);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"[Fail - Status = %r]\n", Status);\r
+    goto Exit;\r
+  } else {\r
+    Print (L"[Pass]\n");\r
+  }\r
+\r
+  //-----------------------------------------\r
+  // Rng->GetInfo() interface test.\r
+  //-----------------------------------------\r
+  \r
+  Print (L" -- Call RNG->GetInfo() interface : ");\r
+  RngAlgListSize = 0;\r
+  Status = Rng->GetInfo (Rng, &RngAlgListSize, NULL);\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    Print (L"[Fail - Status = %r]\n", Status);\r
+  }\r
+  //\r
+  // Print out the supported RNG algorithm GUIDs\r
+  //\r
+  RngAlgCount = RngAlgListSize / sizeof (EFI_RNG_ALGORITHM);\r
+  Print (L"\n     >> Supported RNG Algorithm (Count = %d) : ", RngAlgCount);\r
+  Status = Rng->GetInfo (Rng, &RngAlgListSize, RngAlgList);\r
+  for (Index = 0; Index < RngAlgCount; Index++) {\r
+    PtrRngAlg = (EFI_RNG_ALGORITHM *)(&RngAlgList[Index]);\r
+    Print (L"\n          %d) ", Index);\r
+    Print (L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", PtrRngAlg->Data1,\r
+             PtrRngAlg->Data2, PtrRngAlg->Data3, PtrRngAlg->Data4[0], PtrRngAlg->Data4[1],\r
+             PtrRngAlg->Data4[2], PtrRngAlg->Data4[3], PtrRngAlg->Data4[4],\r
+             PtrRngAlg->Data4[5], PtrRngAlg->Data4[6], PtrRngAlg->Data4[7]);    \r
+  }\r
+\r
+  //-----------------------------------------\r
+  // Rng->GetRNG() interface test.\r
+  //-----------------------------------------\r
+  Print (L"\n -- Call RNG->GetRNG() interface : ");\r
+\r
+  //\r
+  // Allocate one buffer to store random data.\r
+  //\r
+  RandSize = 32;\r
+  Rand     = AllocatePool (RandSize);\r
+  \r
+  //\r
+  // RNG with default algorithm\r
+  //\r
+  Print (L"\n     >> RNG with default algorithm : ");\r
+  Status = Rng->GetRNG (Rng, NULL, RandSize, Rand);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"[Fail - Status = %r]", Status);\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+  \r
+  //\r
+  // RNG with SP800-90-HMAC-256\r
+  //\r
+  Print (L"\n     >> RNG with SP800-90-HMAC-256 : ");\r
+  Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmSp80090Hmac256Guid, RandSize, Rand);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"[Fail - Status = %r]", Status);\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+  //\r
+  // RNG with SP800-90-HASH-256\r
+  //\r
+  Print (L"\n     >> RNG with SP800-90-Hash-256 : ");\r
+  Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmSp80090Hash256Guid, RandSize, Rand);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"[Fail - Status = %r]", Status);\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+  //\r
+  // RNG with SP800-90-CTR-256\r
+  //\r
+  Print (L"\n     >> RNG with SP800-90-CTR-256 : ");\r
+  Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmSp80090Ctr256Guid, RandSize, Rand);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"[Fail - Status = %r]", Status);\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+  //\r
+  // RNG with X9.31-3DES\r
+  //\r
+  Print (L"\n     >> RNG with X9.31-3DES : ");\r
+  Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmX9313DesGuid, RandSize, Rand);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"[Fail - Status = %r]", Status);\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+  //\r
+  // RNG with X9.31-AES\r
+  //\r
+  Print (L"\n     >> RNG with X9.31-AES : ");\r
+  Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmX931AesGuid, RandSize, Rand);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"[Fail - Status = %r]", Status);\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+  //\r
+  // RNG with RAW Entropy\r
+  //\r
+  Print (L"\n     >> RNG with RAW Entropy : ");\r
+  Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmRaw, RandSize, Rand);\r
+  if (EFI_ERROR (Status)) {\r
+    Print (L"[Fail - Status = %r]", Status);\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+  //-----------------------------------------\r
+  // Random Number Generator test.\r
+  //-----------------------------------------\r
+  Print (L"\n -- Random Number Generation Test with default RNG Algorithm (20 Rounds): ");\r
+\r
+  RandSize = 1;\r
+  for (Index = 0; Index < 20; Index++) {\r
+    Status = Rng->GetRNG (Rng, NULL, RandSize, Rand);\r
+    if (EFI_ERROR (Status)) {\r
+      Print (L"[Fail - Status = %r]", Status);\r
+      break;\r
+    } else {\r
+      Print (L"\n          %02d) - ", Index + 1);\r
+      for (Index2 = 0; Index2 < RandSize; Index2++) {\r
+        Print (L"%02x", Rand[Index2]);\r
+      }\r
+    }\r
+\r
+    RandSize +=1;\r
+  }\r
+\r
+  //-----------------------------------------\r
+  // Random Number Generator test.\r
+  //-----------------------------------------\r
+  Print (L"\n -- RAW Entropy Generation Test (20 Rounds) : ");\r
+\r
+  RandSize = 32;\r
+  for (Index = 0; Index < 20; Index++) {\r
+    Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmRaw, RandSize, Rand);\r
+    if (EFI_ERROR (Status)) {\r
+      Print (L"[Fail - Status = %r]", Status);\r
+      break;\r
+    } else {\r
+      Print (L"\n          %02d) - ", Index + 1);\r
+      for (Index2 = 0; Index2 < RandSize; Index2++) {\r
+        Print (L"%02x", Rand[Index2]);\r
+      }\r
+    }\r
+  }\r
+\r
+  Print (L"\n -- Exit UEFI RNG Protocol Test (Status = %r).\n", Status);\r
+  \r
+Exit:\r
+  if (Rand != NULL) {\r
+    FreePool (Rand);\r
+  }\r
+  return Status;\r
+}\r