Call marshal functions with correct buffer size.

CL:289647 removed instances of calling marshal functions with size as
NULL. This is correct, but the buffer size was not computed correctly in
some instances, causing data to not be marshaled.

TEST=build tpm2, tpm2-simulator, and trunks, and deploy to DUT

     On a DUT:
     $ sudo tpm2-simulator // in one terminal
     $ sudo trunksd --simulator // in another terminal
     $ trunks_client --clear // in a third terminal
     $ trunks_client --own --owner_password=""
     $ trunks_client --regression_test

     Trunks is able to take ownership of the software TPM, as well as
     pass a few regression tests. Prior to this change, trunks failed on
     taking ownership.
BUG=none

Change-Id: I46e3a5a30307c832405cc7f91623f87cbece0cb9
Reviewed-on: https://chromium-review.googlesource.com/294883
Tested-by: Jocelyn Bohr <bohr@chromium.org>
Reviewed-by: Utkarsh Sanghi <usanghi@chromium.org>
Commit-Queue: Jocelyn Bohr <bohr@chromium.org>
diff --git a/Attest_spt.c b/Attest_spt.c
index d1464c8..7dc489c 100644
--- a/Attest_spt.c
+++ b/Attest_spt.c
@@ -43,7 +43,7 @@
          INT32     bufferSize;
          // For null sign handle, the QN is TPM_RH_NULL
          buffer = attest->qualifiedSigner.t.name;
-         bufferSize = attest->qualifiedSigner.t.size;
+         bufferSize = sizeof(TPM_HANDLE);
          attest->qualifiedSigner.t.size =
               TPM_HANDLE_Marshal(&signHandle, &buffer, &bufferSize);
      }
@@ -136,7 +136,7 @@
    TPM2B_DIGEST                   digest;
    // Marshal TPMS_ATTEST structure for hash
    buffer = attest->t.attestationData;
-   bufferSize = attest->t.size;
+   bufferSize = sizeof(TPMS_ATTEST);
    attest->t.size = TPMS_ATTEST_Marshal(certifyInfo, &buffer, &bufferSize);
    if(signHandle == TPM_RH_NULL)
    {
diff --git a/ContextSave.c b/ContextSave.c
index bee928b..9b93e36 100644
--- a/ContextSave.c
+++ b/ContextSave.c
@@ -185,7 +185,7 @@
 
    // add integrity at the beginning of context blob
    buffer = out->context.contextBlob.t.buffer;
-   bufferSize = out->context.contextBlob.t.size;
+   bufferSize = sizeof(TPM2B_DIGEST);
    TPM2B_DIGEST_Marshal(&integrity, &buffer, &bufferSize);
 
    // orderly state should be cleared because of the update of state reset and
diff --git a/CryptUtil.c b/CryptUtil.c
index 6a675ec..532eeda 100644
--- a/CryptUtil.c
+++ b/CryptUtil.c
@@ -2416,7 +2416,7 @@
            TPM2B_ECC_PARAMETER    eccPrivate;
            TPMS_ECC_POINT         eccSecret;
            BYTE                   *buffer = secret->t.secret;
-           INT32                  bufferSize = secret->t.size;
+           INT32                  bufferSize = sizeof(TPMS_ECC_POINT);
              // Need to make sure that the public point of the key is on the
              // curve defined by the key.
              if(!_cpri__EccIsPointOnCurve(
diff --git a/Entity.c b/Entity.c
index 2f38b0b..76566b8 100644
--- a/Entity.c
+++ b/Entity.c
@@ -325,7 +325,7 @@
     )
 {
     UINT16              nameSize;
-    INT32 bufferSize = sizeof(TPMU_NAME);
+    INT32 bufferSize = sizeof(TPM_HANDLE);
     switch(HandleGetType(handle))
     {
         case TPM_HT_TRANSIENT:
diff --git a/Object.c b/Object.c
index 5a16f8c..06d717f 100644
--- a/Object.c
+++ b/Object.c
@@ -734,7 +734,7 @@
    name->t.size = CryptStartHash(publicArea->nameAlg, &hashState);
    // Marshal the public area into its canonical form
    buffer = marshalBuffer.b.buffer;
-   bufferSize = marshalBuffer.b.size;
+   bufferSize = sizeof(TPMT_PUBLIC);
    marshalBuffer.t.size = TPMT_PUBLIC_Marshal(publicArea, &buffer, &bufferSize);
    // Adding public area
    CryptUpdateDigest2B(&hashState, &marshalBuffer.b);
diff --git a/Object_spt.c b/Object_spt.c
index a1e9dff..05cb254 100644
--- a/Object_spt.c
+++ b/Object_spt.c
@@ -614,7 +614,7 @@
    if(HandleGetType(parentHandle) == TPM_HT_PERMANENT)
    {
        BYTE         *buffer = &outCreation->t.creationData.parentName.t.name[0];
-       INT32         bufferSize = outCreation->t.creationData.parentName.t.size;
+       INT32         bufferSize = sizeof(TPM_HANDLE);
        outCreation->t.creationData.parentName.t.size =
             TPM_HANDLE_Marshal(&parentHandle, &buffer, &bufferSize);
          // Parent qualified name of a Temporary Object is the same as parent's
@@ -737,7 +737,7 @@
          CryptGenerateRandom(ivRNG.t.size, ivRNG.t.buffer);
          // Marshal IV to buffer
          buffer = sensitiveData;
-         bufferSize = dataSize;
+         bufferSize = sizeof(TPM2B_IV);
          TPM2B_IV_Marshal(&ivRNG, &buffer, &bufferSize);
          // adjust sensitive data starting after IV area
          sensitiveData += ivSize;
@@ -757,7 +757,7 @@
                          outerBuffer + integritySize, &integrity);
    // Add integrity at the beginning of outer buffer
    buffer = outerBuffer;
-   bufferSize = integritySize;
+   bufferSize = sizeof(TPM2B_DIGEST);
    TPM2B_DIGEST_Marshal(&integrity, &buffer, &bufferSize);
    // return the total size in outer wrap
    return dataSize + integritySize + ivSize;
diff --git a/Ticket.c b/Ticket.c
index 0f8b7f1..b6bceaf 100644
--- a/Ticket.c
+++ b/Ticket.c
@@ -35,7 +35,7 @@
       if(buffer->size < sizeof(valueToCompare))
           return FALSE;
       marshalBuffer = bufferToCompare;
-      bufferSize = sizeof(valueToCompare);
+      bufferSize = sizeof(TPM_GENERATED);
    TPM_GENERATED_Marshal(&valueToCompare, &marshalBuffer, &bufferSize);
    if(MemoryEqual(buffer->buffer, bufferToCompare, sizeof(valueToCompare)))
        return FALSE;