public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Joseph Hemann" <joseph.hemann@arm.com>
To: devel@edk2.groups.io
Cc: nd@arm.com, Joseph Hemann <Joseph.hemann@arm.com>
Subject: [PATCH 6/6] uefi-sct/SctPkg: TCG2 Protocol: add SubmitCommand test
Date: Sun,  9 Jan 2022 14:58:27 -0600	[thread overview]
Message-ID: <20220109205827.3608758-7-Joseph.hemann@arm.com> (raw)
In-Reply-To: <20220109205827.3608758-1-Joseph.hemann@arm.com>

    -add initial TCG2 protocol test for SubmitCommand()
      -checkpoint for test function with TPM2_Hash Command
V2:
-Change command submitted from GetRandom to TPM2_HASH
-combined to 2 assertions into 1

Signed-off-by: Joseph Hemann <Joseph.hemann@arm.com>
Change-Id: Ibab68cae1d49953d1076c5628ea80319ba7ca831
---
 .../EFI/Protocol/TCG2/BlackBoxTest/Guid.c     |   2 +
 .../EFI/Protocol/TCG2/BlackBoxTest/Guid.h     |   5 +
 .../TCG2/BlackBoxTest/TCG2ProtocolBBTest.h    |  70 +++++++
 .../TCG2ProtocolBBTestConformance.c           | 172 ++++++++++++++++++
 .../BlackBoxTest/TCG2ProtocolBBTestMain.c     |   9 +
 5 files changed, 258 insertions(+)

diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/Guid.c b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/Guid.c
index b5a4e5c7..87541743 100644
--- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/Guid.c
+++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/Guid.c
@@ -57,3 +57,5 @@ EFI_GUID gTcg2ConformanceTestAssertionGuid013 = EFI_TEST_TCG2CONFORMANCE_ASSERTI
 EFI_GUID gTcg2ConformanceTestAssertionGuid014 = EFI_TEST_TCG2CONFORMANCE_ASSERTION_014_GUID;
 
 EFI_GUID gTcg2ConformanceTestAssertionGuid015 = EFI_TEST_TCG2CONFORMANCE_ASSERTION_015_GUID;
+
+EFI_GUID gTcg2ConformanceTestAssertionGuid016 = EFI_TEST_TCG2CONFORMANCE_ASSERTION_016_GUID;
diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/Guid.h b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/Guid.h
index d3a18aee..507cecc6 100644
--- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/Guid.h
+++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/Guid.h
@@ -100,3 +100,8 @@ extern EFI_GUID gTcg2ConformanceTestAssertionGuid014;
 { 0xa76d2903, 0xbbcc, 0x8a72, {0x90, 0x23, 0xd3, 0xaa, 0xba, 0xb1, 0x52, 0x13 }}
 
 extern EFI_GUID gTcg2ConformanceTestAssertionGuid015;
+
+#define EFI_TEST_TCG2CONFORMANCE_ASSERTION_016_GUID \
+{ 0x1689bc3a, 0x2298, 0xa116, {0x28, 0x4c, 0xc1, 0xdd, 0xaa, 0xd8, 0xef, 0x51 }}
+
+extern EFI_GUID gTcg2ConformanceTestAssertionGuid016;
diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTest.h b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTest.h
index d9c23094..c8684f79 100644
--- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTest.h
+++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTest.h
@@ -43,6 +43,9 @@ Abstract:
 #define EFI_TCG2_PROTOCOL_TEST_ENTRY_GUID0103 \
  {0x907a7878, 0xb294, 0xf147, {0xe9, 0x0a, 0x65, 0x43, 0xab, 0x55, 0x76, 0x46} }
 
+#define EFI_TCG2_PROTOCOL_TEST_ENTRY_GUID0104 \
+ {0x9087ad78, 0x9ad2, 0x4172, {0x9a, 0xbc, 0x98, 0x23, 0x08, 0xf5, 0x6d, 0x26} }
+
 #define EV_POST_CODE 0x01
 
 #define EV_NO_ACTION 0x03
@@ -51,6 +54,61 @@ Abstract:
 
 #define PE_COFF_IMAGE 0x0000000000000010
 
+// ST_NO_SESSION as definied in Table 19 of TPM Library Part 2: Structures
+#define ST_NO_SESSIONS (UINT16) 0x8001
+
+// TPM_RC_SUCCESS as definied in Table 16 of TPM Library Spec Part 2: Structures
+#define TPM_RC_SUCCESS (UINT32) 0x0000000
+
+// TPM_CC_Hash as definied in Table 12 of TPM Library Spec Part 2: Structures
+#define TPM_CC_Hash    (UINT32)(0x0000017D)
+
+#define TPM_RH_NULL    (UINT32) 0x40000007
+
+#define TPM_ALG_SHA256 (UINT16) 0x000B
+
+#define SHA256_LENGTH (UINT16) 0x0020
+
+#pragma pack(1)
+// TPM2B_MAX_BUFFER as definied in Table 86 of TPM Library Spec Part 2: Structures
+typedef struct {
+  UINT16 size;
+  UINT8  digest[5];  // Size of buffer in spec is defined to be variable length but for this test will always be 5
+} TPM2B_MAX_BUFFER;
+
+#pragma pack(1)
+// TPM2B_DIGEST as definied in Table 73 of TPM Library Spec Part 2: Structures
+typedef struct {
+  UINT16 size;
+  UINT8  digest[32];  // Size of buffer in spec is defined to be variable length but for this test will always be 32
+} TPM2B_DIGEST;
+
+typedef struct {
+  UINT16           tag;
+  UINT32           hierarchy;
+  UINT16           digest;  //Size of buffer in spec is defined to be variable length but for this test will always be UINT16
+} TPMT_TK_HASHCHECK;
+
+// TPM2_Hash command Structure as defined in Sectin 15.4 of TPM Spec Part 3: Commands
+typedef struct {
+  UINT16 Tag;
+  UINT32 CommandSize;
+  UINT32 CommandCode;
+  TPM2B_MAX_BUFFER data;
+  UINT16 hashAlg;
+  UINT32 hierarchy;
+} TPM2_HASH_COMMAND;
+
+// TPM2_Hash Response Structure as defined in Sectin 15.4 of TPM Spec Part 3: Commands
+typedef struct {
+  UINT16 Tag;
+  UINT32 ResponseSize;
+  UINT32 ResponseCode;
+  TPM2B_DIGEST data;
+  TPMT_TK_HASHCHECK validation;
+} TPM2_HASH_RESPONSE;
+#pragma
+
 EFI_STATUS
 EFIAPI
 BBTestTCG2ProtocolUnload (
@@ -111,6 +169,11 @@ BBTestGetEventLogConformanceTestCheckpoint2 (
   IN EFI_TCG2_PROTOCOL                     *TCG2
   );
 
+EFI_STATUS
+BBTestSubmitCommandConformanceTestCheckpoint1 (
+  IN EFI_STANDARD_TEST_LIBRARY_PROTOCOL    *StandardLib,
+  IN EFI_TCG2_PROTOCOL                     *TCG2
+  );
 
 EFI_STATUS
 BBTestGetCapabilityConformanceTest (
@@ -136,3 +199,10 @@ BBTestHashLogExtendEventConformanceTest (
   IN EFI_HANDLE                 SupportHandle
   );
 
+EFI_STATUS
+BBTestSubmitCommandConformanceTest (
+  IN EFI_BB_TEST_PROTOCOL       *This,
+  IN VOID                       *ClientInterface,
+  IN EFI_TEST_LEVEL             TestLevel,
+  IN EFI_HANDLE                 SupportHandle
+  );
diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTestConformance.c b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTestConformance.c
index a7bec793..c77828df 100644
--- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTestConformance.c
+++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTestConformance.c
@@ -192,6 +192,56 @@ BBTestHashLogExtendEventConformanceTest (
   return EFI_SUCCESS;
 }
 
+/**
+ *  @brief Entrypoint for SubmitCommand() Function Test.
+ *         1 checkpoint will be tested.
+ *  @param This a pointer of EFI_BB_TEST_PROTOCOL
+ *  @param ClientInterface A pointer to the interface array under test
+ *  @param TestLevel Test "thoroughness" control
+ *  @param SupportHandle A handle containing protocols required
+ *  @return EFI_SUCCESS
+ *  @return EFI_NOT_FOUND
+ */
+
+EFI_STATUS
+BBTestSubmitCommandConformanceTest (
+  IN EFI_BB_TEST_PROTOCOL       *This,
+  IN VOID                       *ClientInterface,
+  IN EFI_TEST_LEVEL             TestLevel,
+  IN EFI_HANDLE                 SupportHandle
+  )
+{
+  EFI_STANDARD_TEST_LIBRARY_PROTOCOL    *StandardLib;
+  EFI_STATUS                            Status;
+  EFI_TCG2_PROTOCOL                     *TCG2;
+
+  //
+  // init
+  //
+  TCG2 = (EFI_TCG2_PROTOCOL*)ClientInterface;
+
+  // Ensure Protocol not NULL
+  if (TCG2 == NULL)
+    return EFI_UNSUPPORTED;
+
+  //
+  // Get the Standard Library Interface
+  //
+  Status = gtBS->HandleProtocol (
+                   SupportHandle,
+                   &gEfiStandardTestLibraryGuid,
+                   (VOID **) &StandardLib
+                   );
+  if (EFI_ERROR(Status)) {
+    return Status;
+  }
+
+  // Test GetRandom TPM Command
+  BBTestSubmitCommandConformanceTestCheckpoint1 (StandardLib, TCG2);
+
+  return EFI_SUCCESS;
+}
+
 EFI_STATUS
 BBTestGetCapabilityConformanceTestCheckpoint1 (
   IN EFI_STANDARD_TEST_LIBRARY_PROTOCOL    *StandardLib,
@@ -961,3 +1011,125 @@ BBTestGetEventLogConformanceTestCheckpoint2 (
 
   return EFI_SUCCESS;
 }
+
+//  Expected SHA256 Hash for input "hello"
+UINT8 Tpm2HashOut[32] = {0x2c,0xf2,0x4d,0xba,0x5f,0xb0,0xa3,0x0e,0x26,0xe8,0x3b,0x2a,0xc5,0xb9,0xe2,0x9e,\
+0x1b,0x16,0x1e,0x5c,0x1f,0xa7,0x42,0x5e,0x73,0x04,0x33,0x62,0x93,0x8b,0x98,0x24};
+
+EFI_STATUS
+BBTestSubmitCommandConformanceTestCheckpoint1 (
+  IN EFI_STANDARD_TEST_LIBRARY_PROTOCOL    *StandardLib,
+  IN EFI_TCG2_PROTOCOL                     *TCG2
+  )
+{
+  EFI_TEST_ASSERTION                    AssertionType;
+  EFI_STATUS                            Status;
+  TPM2_HASH_RESPONSE                   CommandResponse;
+  TPM2_HASH_COMMAND                    CommandInput;
+  char *Str ="hello";
+
+  // Build TPM2 Hash commmand to hash string "hello"
+  CommandInput.Tag = SctSwapBytes16(ST_NO_SESSIONS);
+  CommandInput.CommandSize = SctSwapBytes32(sizeof(TPM2_HASH_COMMAND));
+  CommandInput.CommandCode = SctSwapBytes32(TPM_CC_Hash);
+  CommandInput.data.size = SctSwapBytes16(SctAsciiStrLen(Str));
+  SctAsciiStrCpy(CommandInput.data.digest, Str);
+  CommandInput.hashAlg = SctSwapBytes16(TPM_ALG_SHA256);
+  CommandInput.hierarchy = SctSwapBytes32(TPM_RH_NULL);
+
+  // allocate buffer for response
+  SctZeroMem(&CommandResponse, sizeof(TPM2_HASH_RESPONSE));
+
+  Status = TCG2->SubmitCommand (
+                           TCG2,
+                           sizeof(TPM2_HASH_COMMAND),
+                           &CommandInput,
+                           sizeof(TPM2_HASH_RESPONSE),
+                           &CommandResponse);
+
+
+  AssertionType = EFI_TEST_ASSERTION_PASSED;
+
+  // Verify SubmitCommand returns EFI_SUCCESS
+  if (Status != EFI_SUCCESS) {
+    StandardLib->RecordMessage (
+                     StandardLib,
+                     EFI_VERBOSE_LEVEL_DEFAULT,
+                     L"\r\nTCG2 Protocol SubmitCommand Test: SubmitCommand should return EFI_SUCCESS, Status = %r",
+                     Status
+                     );
+
+    AssertionType = EFI_TEST_ASSERTION_FAILED;
+  }
+
+  // Verify SubmitCommand returns correct Response Tag
+  if (SctSwapBytes16(CommandResponse.Tag) != ST_NO_SESSIONS) {
+    StandardLib->RecordMessage (
+                     StandardLib,
+                     EFI_VERBOSE_LEVEL_DEFAULT,
+                     L"\r\nTCG2 Protocol SubmitCommand Test: SubmitCommand should return ST_NO_SESSIONS response Tag"
+                     );
+
+    AssertionType = EFI_TEST_ASSERTION_FAILED;
+  }
+
+  // Verify SubmitCommand returns correct Response Code
+  if (SctSwapBytes32(CommandResponse.ResponseCode) != TPM_RC_SUCCESS) {
+    StandardLib->RecordMessage (
+                     StandardLib,
+                     EFI_VERBOSE_LEVEL_DEFAULT,
+                     L"\r\nTCG2 Protocol SubmitCommand Test: SubmitCommand should return Correct ResponseCode, ResponseCode = %x",
+                     SctSwapBytes32(CommandResponse.ResponseCode)
+                     );
+
+    AssertionType = EFI_TEST_ASSERTION_FAILED;
+  }
+
+
+  // Verify SubmitCommand returns correct Response Size
+  if (SctSwapBytes32(CommandResponse.ResponseSize) != sizeof(TPM2_HASH_RESPONSE)) {
+    StandardLib->RecordMessage (
+                     StandardLib,
+                     EFI_VERBOSE_LEVEL_DEFAULT,
+                     L"\r\nTCG2 Protocol SubmitCommand Test: SubmitCommand should return Correct ResponseSize, Size = %x",
+                     SctSwapBytes32(CommandResponse.ResponseSize)
+                     );
+
+    AssertionType = EFI_TEST_ASSERTION_FAILED;
+  }
+
+  // Check that the size of the buffer returned is size of SHA256 hash
+  if (SctSwapBytes16(CommandResponse.data.size) != 32) {
+    StandardLib->RecordMessage (
+                     StandardLib,
+                     EFI_VERBOSE_LEVEL_DEFAULT,
+                     L"\r\nTCG2 Protocol SubmitCommand Test: SubmitCommand should return correct size digest for SHA256, Size = %x",
+                     SctSwapBytes16(CommandResponse.data.size)
+                     );
+
+    AssertionType = EFI_TEST_ASSERTION_FAILED;
+  }
+
+  // Ensure Hash returned matches expected response for input
+  if (0 != SctCompareMem(Tpm2HashOut, CommandResponse.data.digest, SHA256_LENGTH) ) {
+    StandardLib->RecordMessage (
+                   StandardLib,
+                   EFI_VERBOSE_LEVEL_DEFAULT,\r                    L"\r\nTCG2 Protocol SubmitCommand Test: SubmitCommand should return expected Hash for data that was hashed."
+                   );
+
+    AssertionType = EFI_TEST_ASSERTION_FAILED;
+  }
+
+  StandardLib->RecordAssertion (
+                 StandardLib,
+                 AssertionType,
+                 gTcg2ConformanceTestAssertionGuid016,
+                 L"EFI_TCG2_PROTOCOL. SubmitComand() - SubmitCommand() shall populate the response buffer and return with a status of EFI_SUCCESS when valid command parameters are passed in.",
+                 L"%a:%d: Status - %r",
+                 __FILE__,
+                 (UINTN)__LINE__,
+                 Status
+                 );
+
+  return EFI_SUCCESS;
+}
diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTestMain.c b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTestMain.c
index 892fce26..48a8a450 100644
--- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTestMain.c
+++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/TCG2/BlackBoxTest/TCG2ProtocolBBTestMain.c
@@ -65,6 +65,15 @@ EFI_BB_TEST_ENTRY_FIELD gBBTestEntryField[] = {
     EFI_TEST_CASE_AUTO,
     BBTestHashLogExtendEventConformanceTest
   },
+  {
+    EFI_TCG2_PROTOCOL_TEST_ENTRY_GUID0104,
+    L"SubmitCommand_Conf",
+    L"Test the SubmitCommmand API",
+    EFI_TEST_LEVEL_DEFAULT,
+    gSupportProtocolGuid1,
+    EFI_TEST_CASE_AUTO,
+    BBTestSubmitCommandConformanceTest
+  },
   0
 };
 
-- 
2.25.1


  parent reply	other threads:[~2022-01-09 20:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-09 20:58 [PATCH V2 0/6] Implementation of TCG2 Protocol test Joseph Hemann
2022-01-09 20:58 ` [PATCH 1/6] uefi-sct/SctPkg: TCG2 Protocol: add header with TCG2 protocol definitions Joseph Hemann
2022-01-09 20:58 ` [PATCH 2/6] uefi-sct/SctPkg: TCG2 Protocol: add GetCapability Test Joseph Hemann
2022-02-01 18:32   ` [edk2-devel] " Stuart Yoder
2022-02-07 17:59     ` Joseph Hemann
2022-01-09 20:58 ` [PATCH 3/6] uefi-sct/SctPkg: TCG2 Protocol: add GetActivePcrBanks test Joseph Hemann
2022-01-09 20:58 ` [PATCH 4/6] uefi-sct/SctPkg: TCG2 Protocol: add HashLogExtendEvent test Joseph Hemann
2022-01-09 20:58 ` [PATCH 5/6] uefi-sct/SctPkg: TCG2 Protocol: add GetEventLog test Joseph Hemann
2022-01-09 20:58 ` Joseph Hemann [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-10-20 13:43 [PATCH 0/6] Implementation of TCG2 Protocol test Joseph Hemann
2021-10-20 13:43 ` [PATCH 6/6] uefi-sct/SctPkg: TCG2 Protocol: add SubmitCommand test Joseph Hemann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220109205827.3608758-7-Joseph.hemann@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox