resolve merge conflicts of f6187c733f349b9529006f6d1afbc42f150c2bf0 to master

Test: I solemnly swear I tested this conflict resolution.
diff --git a/Android.bp b/Android.bp
new file mode 100644
index 0000000..934cfda
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,308 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// libtpm2
+// ========================================================
+cc_library_shared {
+    name: "libtpm2",
+    cflags: [
+        "-Wall",
+        "-Wextra",
+        "-Werror",
+        "-Wno-typedef-redefinition",
+        "-Wno-unused-parameter",
+        "-Wno-missing-field-initializers",
+        "-Wno-tautological-compare",
+        "-Wno-sign-compare",
+    ],
+    local_include_dirs: ["include/tpm2"],
+    shared_libs: ["libcrypto"],
+    export_include_dirs: ["include"],
+    srcs: [
+        "ActivateCredential.c",
+        "AlgorithmCap.c",
+        "Attest_spt.c",
+        "Bits.c",
+        "Cancel.c",
+        "Certify.c",
+        "CertifyCreation.c",
+        "ChangeEPS.c",
+        "ChangePPS.c",
+        "Clear.c",
+        "ClearControl.c",
+        "Clock.c",
+        "ClockRateAdjust.c",
+        "ClockSet.c",
+        "CommandAudit.c",
+        "CommandCodeAttributes.c",
+        "CommandDispatcher.c",
+        "Commit.c",
+        "ContextLoad.c",
+        "ContextSave.c",
+        "Context_spt.c",
+        "CpriCryptPri.c",
+        "CpriECC.c",
+        "CpriHash.c",
+        "CpriMisc.c",
+        "CpriRNG.c",
+        "CpriRSA.c",
+        "CpriSym.c",
+        "Create.c",
+        "CreatePrimary.c",
+        "CryptSelfTest.c",
+        "CryptUtil.c",
+        "DA.c",
+        "DRTM.c",
+        "DictionaryAttackLockReset.c",
+        "DictionaryAttackParameters.c",
+        "Duplicate.c",
+        "ECC_Parameters.c",
+        "ECDH_KeyGen.c",
+        "ECDH_ZGen.c",
+        "EC_Ephemeral.c",
+        "EncryptDecrypt.c",
+        "Entity.c",
+        "Entropy.c",
+        "EventSequenceComplete.c",
+        "EvictControl.c",
+        "ExecCommand.c",
+        "FieldUpgradeData.c",
+        "FieldUpgradeStart.c",
+        "FirmwareRead.c",
+        "FlushContext.c",
+        "GetCapability.c",
+        "GetCommandAuditDigest.c",
+        "GetCommandCodeString.c",
+        "GetRandom.c",
+        "GetSessionAuditDigest.c",
+        "GetTestResult.c",
+        "GetTime.c",
+        "Global.c",
+        "HMAC.c",
+        "HMAC_Start.c",
+        "Handle.c",
+        "HandleProcess.c",
+        "Hash.c",
+        "HashSequenceStart.c",
+        "Hierarchy.c",
+        "HierarchyChangeAuth.c",
+        "HierarchyControl.c",
+        "Import.c",
+        "IncrementalSelfTest.c",
+        "Load.c",
+        "LoadExternal.c",
+        "Locality.c",
+        "LocalityPlat.c",
+        "MakeCredential.c",
+        "Marshal_ActivateCredential.c",
+        "Marshal_Certify.c",
+        "Marshal_CertifyCreation.c",
+        "Marshal_ChangeEPS.c",
+        "Marshal_ChangePPS.c",
+        "Marshal_Clear.c",
+        "Marshal_ClearControl.c",
+        "Marshal_ClockRateAdjust.c",
+        "Marshal_ClockSet.c",
+        "Marshal_Commit.c",
+        "Marshal_ContextLoad.c",
+        "Marshal_ContextSave.c",
+        "Marshal_Create.c",
+        "Marshal_CreatePrimary.c",
+        "Marshal_DictionaryAttackLockReset.c",
+        "Marshal_DictionaryAttackParameters.c",
+        "Marshal_Duplicate.c",
+        "Marshal_ECC_Parameters.c",
+        "Marshal_ECDH_KeyGen.c",
+        "Marshal_ECDH_ZGen.c",
+        "Marshal_EC_Ephemeral.c",
+        "Marshal_EncryptDecrypt.c",
+        "Marshal_EventSequenceComplete.c",
+        "Marshal_EvictControl.c",
+        "Marshal_FirmwareRead.c",
+        "Marshal_FlushContext.c",
+        "Marshal_GetCapability.c",
+        "Marshal_GetCommandAuditDigest.c",
+        "Marshal_GetRandom.c",
+        "Marshal_GetSessionAuditDigest.c",
+        "Marshal_GetTestResult.c",
+        "Marshal_GetTime.c",
+        "Marshal_HMAC.c",
+        "Marshal_HMAC_Start.c",
+        "Marshal_Hash.c",
+        "Marshal_HashSequenceStart.c",
+        "Marshal_HierarchyChangeAuth.c",
+        "Marshal_HierarchyControl.c",
+        "Marshal_Import.c",
+        "Marshal_IncrementalSelfTest.c",
+        "Marshal_Load.c",
+        "Marshal_LoadExternal.c",
+        "Marshal_MakeCredential.c",
+        "Marshal_NV_Certify.c",
+        "Marshal_NV_ChangeAuth.c",
+        "Marshal_NV_DefineSpace.c",
+        "Marshal_NV_Extend.c",
+        "Marshal_NV_GlobalWriteLock.c",
+        "Marshal_NV_Increment.c",
+        "Marshal_NV_Read.c",
+        "Marshal_NV_ReadLock.c",
+        "Marshal_NV_ReadPublic.c",
+        "Marshal_NV_SetBits.c",
+        "Marshal_NV_UndefineSpace.c",
+        "Marshal_NV_UndefineSpaceSpecial.c",
+        "Marshal_NV_Write.c",
+        "Marshal_NV_WriteLock.c",
+        "Marshal_ObjectChangeAuth.c",
+        "Marshal_PCR_Allocate.c",
+        "Marshal_PCR_Event.c",
+        "Marshal_PCR_Extend.c",
+        "Marshal_PCR_Read.c",
+        "Marshal_PCR_Reset.c",
+        "Marshal_PCR_SetAuthPolicy.c",
+        "Marshal_PCR_SetAuthValue.c",
+        "Marshal_PP_Commands.c",
+        "Marshal_PolicyAuthValue.c",
+        "Marshal_PolicyAuthorize.c",
+        "Marshal_PolicyCommandCode.c",
+        "Marshal_PolicyCounterTimer.c",
+        "Marshal_PolicyCpHash.c",
+        "Marshal_PolicyDuplicationSelect.c",
+        "Marshal_PolicyGetDigest.c",
+        "Marshal_PolicyLocality.c",
+        "Marshal_PolicyNV.c",
+        "Marshal_PolicyNameHash.c",
+        "Marshal_PolicyNvWritten.c",
+        "Marshal_PolicyOR.c",
+        "Marshal_PolicyPCR.c",
+        "Marshal_PolicyPassword.c",
+        "Marshal_PolicyPhysicalPresence.c",
+        "Marshal_PolicyRestart.c",
+        "Marshal_PolicySecret.c",
+        "Marshal_PolicySigned.c",
+        "Marshal_PolicyTicket.c",
+        "Marshal_Quote.c",
+        "Marshal_RSA_Decrypt.c",
+        "Marshal_RSA_Encrypt.c",
+        "Marshal_ReadClock.c",
+        "Marshal_ReadPublic.c",
+        "Marshal_Rewrap.c",
+        "Marshal_SelfTest.c",
+        "Marshal_SequenceComplete.c",
+        "Marshal_SequenceUpdate.c",
+        "Marshal_SetAlgorithmSet.c",
+        "Marshal_SetCommandCodeAuditStatus.c",
+        "Marshal_SetPrimaryPolicy.c",
+        "Marshal_Shutdown.c",
+        "Marshal_Sign.c",
+        "Marshal_StartAuthSession.c",
+        "Marshal_Startup.c",
+        "Marshal_StirRandom.c",
+        "Marshal_TestParms.c",
+        "Marshal_Unseal.c",
+        "Marshal_VerifySignature.c",
+        "Marshal_ZGen_2Phase.c",
+        "Manufacture.c",
+        "MathFunctions.c",
+        "MemoryLib.c",
+        "NV.c",
+        "NVMem.c",
+        "NV_Certify.c",
+        "NV_ChangeAuth.c",
+        "NV_DefineSpace.c",
+        "NV_Extend.c",
+        "NV_GlobalWriteLock.c",
+        "NV_Increment.c",
+        "NV_Read.c",
+        "NV_ReadLock.c",
+        "NV_ReadPublic.c",
+        "NV_SetBits.c",
+        "NV_UndefineSpace.c",
+        "NV_UndefineSpaceSpecial.c",
+        "NV_Write.c",
+        "NV_WriteLock.c",
+        "NV_spt.c",
+        "Object.c",
+        "ObjectChangeAuth.c",
+        "Object_spt.c",
+        "PCR.c",
+        "PCR_Allocate.c",
+        "PCR_Event.c",
+        "PCR_Extend.c",
+        "PCR_Read.c",
+        "PCR_Reset.c",
+        "PCR_SetAuthPolicy.c",
+        "PCR_SetAuthValue.c",
+        "PP.c",
+        "PPPlat.c",
+        "PP_Commands.c",
+        "PlatformData.c",
+        "PolicyAuthValue.c",
+        "PolicyAuthorize.c",
+        "PolicyCommandCode.c",
+        "PolicyCounterTimer.c",
+        "PolicyCpHash.c",
+        "PolicyDuplicationSelect.c",
+        "PolicyGetDigest.c",
+        "PolicyLocality.c",
+        "PolicyNV.c",
+        "PolicyNameHash.c",
+        "PolicyNvWritten.c",
+        "PolicyOR.c",
+        "PolicyPCR.c",
+        "PolicyPassword.c",
+        "PolicyPhysicalPresence.c",
+        "PolicyRestart.c",
+        "PolicySecret.c",
+        "PolicySigned.c",
+        "PolicyTicket.c",
+        "Policy_spt.c",
+        "Power.c",
+        "PowerPlat.c",
+        "PropertyCap.c",
+        "Quote.c",
+        "RSAData.c",
+        "RSAKeySieve.c",
+        "RSA_Decrypt.c",
+        "RSA_Encrypt.c",
+        "ReadClock.c",
+        "ReadPublic.c",
+        "Rewrap.c",
+        "SelfTest.c",
+        "SequenceComplete.c",
+        "SequenceUpdate.c",
+        "Session.c",
+        "SessionProcess.c",
+        "SetAlgorithmSet.c",
+        "SetCommandCodeAuditStatus.c",
+        "SetPrimaryPolicy.c",
+        "Shutdown.c",
+        "Sign.c",
+        "StartAuthSession.c",
+        "Startup.c",
+        "StirRandom.c",
+        "TestParms.c",
+        "Ticket.c",
+        "Time.c",
+        "TpmFail.c",
+        "Unique.c",
+        "Unseal.c",
+        "VerifySignature.c",
+        "ZGen_2Phase.c",
+        "_TPM_Hash_Data.c",
+        "_TPM_Hash_End.c",
+        "_TPM_Hash_Start.c",
+        "_TPM_Init.c",
+        "tpm_generated.c",
+    ],
+
+}
diff --git a/CpriCryptPri.c b/CpriCryptPri.c
index 2c1210c..6926808 100644
--- a/CpriCryptPri.c
+++ b/CpriCryptPri.c
@@ -5,6 +5,8 @@
 // Level 00 Revision 01.16
 // October 30, 2014
 
+#include <stdlib.h>
+
 #include "CryptoEngine.h"
 #include "OsslCryptoEngine.h"
 static void Trap(const char *function, int line, int code);
diff --git a/Implementation.h b/Implementation.h
index 5ecf334..5de5f9f 100644
--- a/Implementation.h
+++ b/Implementation.h
@@ -486,8 +486,7 @@
 #define   SHA256_BLOCK_SIZE        64
 #define   SHA256_DER_SIZE          19
 #define   SHA256_DER               \
-   0x30,0x31,0x30,0x0D,0x06,0x09,0x60,0x86,0x48,\
-   0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,0x20
+   0x30,0x31,0x30,0x0D,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,0x20
 //
 //      From TCG Algorithm Registry: Table 14 - Defines for SHA384 Hash Values
 //
@@ -495,8 +494,7 @@
 #define   SHA384_BLOCK_SIZE        128
 #define   SHA384_DER_SIZE          19
 #define   SHA384_DER               \
-   0x30,0x41,0x30,0x0D,0x06,0x09,0x60,0x86,0x48,\
-   0x01,0x65,0x03,0x04,0x02,0x02,0x05,0x00,0x04,0x30
+   0x30,0x41,0x30,0x0D,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x02,0x05,0x00,0x04,0x30
 //
 //      From TCG Algorithm Registry: Table 15 - Defines for SHA512 Hash Values
 //
@@ -504,8 +502,7 @@
 #define   SHA512_BLOCK_SIZE        128
 #define   SHA512_DER_SIZE          19
 #define   SHA512_DER               \
-   0x30,0x51,0x30,0x0D,0x06,0x09,0x60,0x86,0x48,\
-   0x01,0x65,0x03,0x04,0x02,0x03,0x05,0x00,0x04,0x40
+   0x30,0x51,0x30,0x0D,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x03,0x05,0x00,0x04,0x40
 //
 //      From TCG Algorithm Registry: Table 16 - Defines for SM3_256 Hash Values
 //
@@ -513,8 +510,8 @@
 #define   SM3_256_BLOCK_SIZE        64
 #define   SM3_256_DER_SIZE          18
 #define   SM3_256_DER               \
-   0x30,0x30,0x30,0x0C,0x06,0x08,0x2A,0x81,0x1C,\
-   0x81,0x45,0x01,0x83,0x11,0x05,0x00,0x04,0x20
+//      0x30,0x30,0x30,0x0C,0x06,0x08,0x2A,0x81,0x1C,0x81,0x45,0x01,0x83,0x11,0x05,0x00,0x04,0
+//      x20
 //
 //      From TCG Algorithm Registry: Table 17 - Defines for AES Symmetric Cipher Algorithm Constants
 //
diff --git a/OsslCryptoEngine.h b/OsslCryptoEngine.h
index 7217e8a..3c5f3cb 100644
--- a/OsslCryptoEngine.h
+++ b/OsslCryptoEngine.h
@@ -28,6 +28,38 @@
 #   endif
 #   include    "CpriRSA_fp.h"
 #endif
+
+#ifdef OPENSSL_IS_BORINGSSL
+// libtpm2 reads internal EVP_MD state (e.g. ctx_size). The boringssl headers
+// don't expose this type so define it here.
+struct env_md_st {
+  /* type contains a NID identifing the digest function. (For example,
+   * NID_md5.) */
+  int type;
+
+  /* md_size contains the size, in bytes, of the resulting digest. */
+  unsigned md_size;
+
+  /* flags contains the OR of |EVP_MD_FLAG_*| values. */
+  uint32_t flags;
+
+  /* init initialises the state in |ctx->md_data|. */
+  void (*init)(EVP_MD_CTX *ctx);
+
+  /* update hashes |len| bytes of |data| into the state in |ctx->md_data|. */
+  void (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
+
+  /* final completes the hash and writes |md_size| bytes of digest to |out|. */
+  void (*final)(EVP_MD_CTX *ctx, uint8_t *out);
+
+  /* block_size contains the hash's native block size. */
+  unsigned block_size;
+
+  /* ctx_size contains the size, in bytes, of the state of the hash function. */
+  unsigned ctx_size;
+};
+#endif
+
 //
 //     This is a structure to hold the parameters for the version of KDFa() used by the CryptoEngine(). This
 //     structure allows the state to be passed between multiple functions that use the same pseudo-random
diff --git a/ExecCommand_fp.h b/include/tpm2/ExecCommand_fp.h
similarity index 100%
rename from ExecCommand_fp.h
rename to include/tpm2/ExecCommand_fp.h
diff --git a/Manufacture_fp.h b/include/tpm2/Manufacture_fp.h
similarity index 90%
rename from Manufacture_fp.h
rename to include/tpm2/Manufacture_fp.h
index 5768df6..941f247 100644
--- a/Manufacture_fp.h
+++ b/include/tpm2/Manufacture_fp.h
@@ -7,6 +7,9 @@
 #ifndef __TPM2_MANUFACTURE_FP_H
 #define __TPM2_MANUFACTURE_FP_H
 
+#include "bool.h"
+#include "TpmBuildSwitches.h"
+
 LIB_EXPORT int TPM_Manufacture(
     BOOL firstTime  // IN: indicates if this is the first call from main()
     );
diff --git a/Platform.h b/include/tpm2/Platform.h
similarity index 100%
rename from Platform.h
rename to include/tpm2/Platform.h
diff --git a/TpmBuildSwitches.h b/include/tpm2/TpmBuildSwitches.h
similarity index 100%
rename from TpmBuildSwitches.h
rename to include/tpm2/TpmBuildSwitches.h
diff --git a/TpmError.h b/include/tpm2/TpmError.h
similarity index 100%
rename from TpmError.h
rename to include/tpm2/TpmError.h
diff --git a/_TPM_Init_fp.h b/include/tpm2/_TPM_Init_fp.h
similarity index 100%
rename from _TPM_Init_fp.h
rename to include/tpm2/_TPM_Init_fp.h
diff --git a/bool.h b/include/tpm2/bool.h
similarity index 100%
rename from bool.h
rename to include/tpm2/bool.h