Add more codes in tpm2-plugin

Implement tpm2_plugin_load_key() and tpm2_plugin_rsa_sign() APIs

Issue-ID: AAF-94
Change-Id: I5f4329fdf973e52264d9e0e8aabc864c5fbdeebf
Signed-off-by: NingSun <ning.sun@intel.com>
diff --git a/TPM2-Plugin/lib/include/files.h b/TPM2-Plugin/lib/include/files.h
index 164e308..a4befc8 100644
--- a/TPM2-Plugin/lib/include/files.h
+++ b/TPM2-Plugin/lib/include/files.h
@@ -34,7 +34,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 /**
  * Reads a series of bytes from a file as a byte array. This is similar to files_read_bytes(),
@@ -214,6 +214,16 @@
 bool files_load_sensitive(const char *path, TPM2B_SENSITIVE *sensitive);
 
 /**
+ * Serializes a TPM2B_SENSITIVE to the file path provided.
+ * @param sensitive
+ *  The TPM2B_SENSITIVE to save to disk.
+ * @param path
+ *  The path to save to.
+ * @return
+ *  true on success, false on error.
+ */
+bool files_save_sensitive(TPM2B_SENSITIVE *sensitive, const char *path);
+/**
  * Serializes a TPMT_TK_HASHCHECK to the file path provided.
  * @param validation
  *  The TPMT_TK_HASHCHECK to save to disk.
@@ -236,6 +246,28 @@
 bool files_load_validation(const char *path, TPMT_TK_HASHCHECK *validation);
 
 /**
+ * Serializes a TPM2B_PRIVATE to the file path provided.
+ * @param private
+ *  The TPM2B_PRIVATE to save to disk.
+ * @param path
+ *  The path to save to.
+ * @return
+ *  true on success, false on error.
+ */
+bool files_save_private(TPM2B_PRIVATE *private, const char *path);
+
+/**
+ * Loads a TPM2B_PRIVATE from disk.
+ * @param private
+ *  The path to load from.
+ * @param validation
+ *  The TPM2B_PRIVATE to load.
+ * @return
+ *  true on success, false on error.
+ */
+bool files_load_private(const char *path, TPM2B_PRIVATE *private);
+
+/**
  * Checks a file for existence.
  * @param path
  *  The file to check for existence.
diff --git a/TPM2-Plugin/lib/include/log.h b/TPM2-Plugin/lib/include/log.h
index c4ae0bd..a93c1c2 100644
--- a/TPM2-Plugin/lib/include/log.h
+++ b/TPM2-Plugin/lib/include/log.h
@@ -34,7 +34,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 #include "tpm2_error.h"
 #include "tpm2_util.h"
diff --git a/TPM2-Plugin/lib/include/plugin_register.h b/TPM2-Plugin/lib/include/plugin_register.h
index a154a24..2bb118d 100644
--- a/TPM2-Plugin/lib/include/plugin_register.h
+++ b/TPM2-Plugin/lib/include/plugin_register.h
@@ -36,17 +36,51 @@
 extern "C" {
 #endif
 
+#define MAX_ID_LENGTH (32)
+
+typedef struct buffer_info_s{
+    char id[MAX_ID_LENGTH+1];
+    int length_of_buffer;
+    unsigned char *buffer;
+}buffer_info_t;
+
+
+typedef struct sshsm_hw_plugin_activate_in_info_s {
+    int num_buffers;
+    buffer_info_t *buffer_info;
+}SSHSM_HW_PLUGIN_ACTIVATE_IN_INFO_t;
+
+typedef struct sshsm_hw_plugin_load_key_in_info_s {
+    int num_buffers;
+    buffer_info_t buffer_info[];
+}SSHSM_HW_PLUGIN_LOAD_KEY_IN_INFO_t;
+
+
+//typedef int (*sshsm_hw_plugin_load_key)(SSHSM_HW_PLUGIN_LOAD_KEY_IN_INFO_t *loadkey_in_info, void **keyHandle);
+
+//typedef int (*sshsm_hw_plugin_activate)(SSHSM_HW_PLUGIN_ACTIVATE_IN_INFO_t *activate_in_info);
+
 /*
  * Callback function definitions
  */
 
+typedef int (*fp_crypto_hw_plugin_init) ( );
+typedef int (*fp_crypto_hw_plugin_uninit) ( );
+typedef int (*fp_crypto_hw_plugin_activate)(
+           SSHSM_HW_PLUGIN_ACTIVATE_IN_INFO_t *activate_in_info
+        );
+
+typedef int (*fp_crypto_hw_plugin_load_key)(
+           SSHSM_HW_PLUGIN_LOAD_KEY_IN_INFO_t *loadkey_in_info,
+           void **keyHandle
+        );
 typedef int (*fp_crypto_rsa_decrypt_init) (
     /* IN */
     unsigned long mechanism,    /* PKCS#11 Mechanism */
     void *param,                /* PKCS#11 Paramter */
     unsigned long param_len,    /* PKCS#11 Parameter len */
     /* OUT */
-    void *cb                   /* Address of pointer to store context block */ 
+    void *cb                   /* Address of pointer to store context block */
     );
 
 typedef int (*fp_crypto_rsa_decrypt) (
@@ -167,10 +201,13 @@
     );
 
 
-typedef struct 
+typedef struct
 {
-    fp_crypto_rsa_decrypt_init     cb_crypto_rsa_decrypt_init;
-    fp_crypto_rsa_decrypt          cb_crypto_rsa_decrypt;	
+    fp_crypto_hw_plugin_init       cb_crypto_hw_plugin_init;
+    fp_crypto_hw_plugin_uninit     cb_crypto_hw_plugin_uninit;
+    fp_crypto_hw_plugin_activate   cb_crypto_hw_plugin_activate;
+    fp_crypto_hw_plugin_load_key   cb_crypto_hw_plugin_load_key;
+    fp_crypto_rsa_decrypt          cb_crypto_rsa_decrypt;
     fp_crypto_rsa_sign_init	   cb_crypto_rsa_sign_init;
     fp_crypto_rsa_sign_update 	   cb_crypto_rsa_sign_update;
     fp_crypto_rsa_sign_final	   cb_crypto_rsa_sign_final;
diff --git a/TPM2-Plugin/lib/include/tpm2_alg_util.h b/TPM2-Plugin/lib/include/tpm2_alg_util.h
index ce4083c..b9511dc 100644
--- a/TPM2-Plugin/lib/include/tpm2_alg_util.h
+++ b/TPM2-Plugin/lib/include/tpm2_alg_util.h
@@ -33,7 +33,7 @@
 
 #include <stdbool.h>
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 /**
  * Iterator callback routine for iterating over known algorithm name and value
diff --git a/TPM2-Plugin/lib/include/tpm2_attr_util.h b/TPM2-Plugin/lib/include/tpm2_attr_util.h
index 2487982..5964174 100644
--- a/TPM2-Plugin/lib/include/tpm2_attr_util.h
+++ b/TPM2-Plugin/lib/include/tpm2_attr_util.h
@@ -33,7 +33,7 @@
 
 #include <stdbool.h>
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 /**
  * Converts a list of | (pipe) separated attributes as defined in tavle 204
diff --git a/TPM2-Plugin/lib/include/tpm2_convert.h b/TPM2-Plugin/lib/include/tpm2_convert.h
new file mode 100644
index 0000000..275d96a
--- /dev/null
+++ b/TPM2-Plugin/lib/include/tpm2_convert.h
@@ -0,0 +1,99 @@
+//**********************************************************************;
+// Copyright (c) 2017, SUSE GmbH
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+// THE POSSIBILITY OF SUCH DAMAGE.
+//**********************************************************************;
+
+#ifndef CONVERSION_H
+#define CONVERSION_H
+
+#include <stdbool.h>
+
+#include <tss2/tss2_sys.h>
+
+typedef enum tpm2_convert_pubkey_fmt tpm2_convert_pubkey_fmt;
+enum tpm2_convert_pubkey_fmt {
+    pubkey_format_tss,
+    pubkey_format_pem,
+    pubkey_format_der,
+    pubkey_format_err
+};
+
+typedef enum tpm2_convert_sig_fmt tpm2_convert_sig_fmt;
+enum tpm2_convert_sig_fmt {
+    signature_format_tss,
+    signature_format_plain,
+    signature_format_err
+};
+
+/**
+ * Parses the given command line public key format option string and returns
+ * the corresponding pubkey_format enum value.
+ *
+ * LOG_ERR is used to communicate errors.
+ *
+ * @return
+ *   On error pubkey_format_err is returned.
+ */
+tpm2_convert_pubkey_fmt tpm2_convert_pubkey_fmt_from_optarg(const char *label);
+
+/**
+ * Converts the given public key structure into the requested target format
+ * and writes the result to the given file system path.
+ *
+ * LOG_ERR is used to communicate errors.
+ */
+bool tpm2_convert_pubkey_save(TPM2B_PUBLIC *public, tpm2_convert_pubkey_fmt format, const char *path);
+
+/**
+ * Loads a public key in the TSS format from a file.
+ * @param public
+ *  The public key to load
+ * @param format
+ * @param path
+ * @return
+ */
+bool tpm2_convert_pubkey_load(TPM2B_PUBLIC *public, const char *path);
+
+/**
+ * Parses the given command line signature format option string and returns
+ * the corresponding signature_format enum value.
+ *
+ * LOG_ERR is used to communicate errors.
+ *
+ * @return
+ *   On error signature_format_err is returned.
+ */
+tpm2_convert_sig_fmt tpm2_convert_sig_fmt_from_optarg(const char *label);
+
+/**
+ * Converts the given signature data into the requested target format and
+ * writes the result to the given file system path.
+ *
+ * LOG_ERR is used to communicate errors.
+ */
+bool tpm2_convert_sig(TPMT_SIGNATURE *signature, tpm2_convert_sig_fmt format,
+        const char *path);
+
+#endif /* CONVERSION_H */
diff --git a/TPM2-Plugin/lib/include/tpm2_error.h b/TPM2-Plugin/lib/include/tpm2_error.h
index 0549edc..01ec043 100644
--- a/TPM2-Plugin/lib/include/tpm2_error.h
+++ b/TPM2-Plugin/lib/include/tpm2_error.h
@@ -30,7 +30,7 @@
 
 #include <stdbool.h>
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 /**
  * Number of error layers
diff --git a/TPM2-Plugin/lib/include/tpm2_hash.h b/TPM2-Plugin/lib/include/tpm2_hash.h
index 7fab882..627a95a 100644
--- a/TPM2-Plugin/lib/include/tpm2_hash.h
+++ b/TPM2-Plugin/lib/include/tpm2_hash.h
@@ -33,7 +33,7 @@
 
 #include <stdbool.h>
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 /**
  * Hashes a BYTE array via the tpm.
diff --git a/TPM2-Plugin/lib/include/tpm2_options.h b/TPM2-Plugin/lib/include/tpm2_options.h
new file mode 100644
index 0000000..860d9b0
--- /dev/null
+++ b/TPM2-Plugin/lib/include/tpm2_options.h
@@ -0,0 +1,208 @@
+/*
+ * Copyright (c) 2016, Intel Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef OPTIONS_H
+#define OPTIONS_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include <getopt.h>
+
+#include <tss2/tss2_sys.h>
+
+typedef union tpm2_option_flags tpm2_option_flags;
+union tpm2_option_flags {
+    struct {
+        UINT8 verbose : 1;
+        UINT8 quiet   : 1;
+        UINT8 enable_errata  : 1;
+    };
+    UINT8 all;
+};
+
+/**
+ * This function pointer defines the interface for tcti initialization.
+ * ALL tool supported TCTIs should implement this interface.
+ * @param opts
+ *  An option string, that is defined by the tcti, and is passed
+ *  via the --tcti= or -T options.
+ *
+ *  Anything following the : in the --tcti option is provides as opts.
+ * @return
+ *   NULL on error or an initialized TCTI.
+ */
+typedef TSS2_TCTI_CONTEXT *(*tcti_init)(char *opts);
+
+/**
+ * Tools may implement this optional interface if they need
+ * to handle options.
+ * @param key
+ *  The key of the option, ie short option return value from getopt_long().
+ * @param value
+ *  The getopt_long optarg value.
+ * @return
+ *  true on success, false on error.
+ * @note
+ *  LOG_INFO and TOOL_OUTPUT will not work correctly during this callback.
+ *  This is called after onstart() finishes, but before
+ *  onrun() is invoked.
+ *
+ */
+typedef bool (*tpm2_option_handler)(char key, char *value);
+
+/**
+ * Called after option handling to process arguments, if specified.
+ * @param argc
+ *  The number of args in argv.
+ * @param argv
+ *  The arguments.
+ * @return
+ *  true on success, false otherwise.
+ * @note
+ *  LOG_INFO adn TOOL_OUTPUT will not work correctly during this callback.
+ *  This is called after onstart() and tpm2_option_handler() (if specified),
+ *  but before onrun() is invoked.
+ *
+ */
+typedef bool (*tpm2_arg_handler)(int argc, char **argv);
+
+/**
+ * TPM2_OPTIONS_* flags change default behavior of the argument parser
+ *
+ * TPM2_OPTIONS_SHOW_USAGE:
+ *  Enable printing a short usage summary (I.e. help)
+ * TPM2_OPTIONS_NO_SAPI:
+ *  Skip SAPI initialization. Removes the "-T" common option.
+ */
+#define TPM2_OPTIONS_SHOW_USAGE 0x1
+#define TPM2_OPTIONS_NO_SAPI 0x2
+
+struct tpm2_options {
+    struct {
+        tpm2_option_handler on_opt;
+        tpm2_arg_handler on_arg;
+    } callbacks;
+    char *short_opts;
+    size_t len;
+    UINT32 flags;
+    struct option long_opts[];
+};
+
+typedef struct tpm2_options tpm2_options;
+
+/**
+ * The onstart() routine expects a return of NULL or a tpm2_options structure.
+ * This routine initializes said object.
+ * @param short_opts
+ *  Any short options you wish to specify to getopt_long.
+ * @param len
+ *  The length of the long_opts array.
+ * @param long_opts
+ *  Any long options you wish to specify to getopt_long().
+ * @param on_opt
+ *  An option handling callback, which may be null if you don't wish
+ *  to handle options.
+ * @param on_arg
+ *  An argument handling callback, which may be null if you don't wish
+ *  to handle arguments.
+ * @param flags
+ *  TPM2_OPTIONS_* bit flags
+ * @return
+ *  NULL on failure or an initialized tpm2_options object.
+ */
+tpm2_options *tpm2_options_new(const char *short_opts, size_t len,
+        const struct option *long_opts, tpm2_option_handler on_opt,
+        tpm2_arg_handler on_arg, UINT32 flags);
+
+/**
+ * Concatenates two tpm2_options objects, with src appended on
+ * dest. The internal callbacks for tpm2_arg_handler and tpm2_option_handler
+ * which were specified during tpm2_options_new() are copied from src to
+ * dest, thus overwriting dest. Short and long options are concatenated.
+ * @param dest
+ *  The tpm2_options object to append to.
+ * @param src
+ *  The source tpm2_options to append onto dest.
+ * @return
+ *  true on success, false otherwise.
+ */
+bool tpm2_options_cat(tpm2_options **dest, tpm2_options *src);
+
+/**
+ * Free's a tpm2_options created via tpm2_options_new().
+ * @param opts
+ *  The tpm2_options object to deallocate.
+ */
+void tpm2_options_free(tpm2_options *opts);
+
+typedef enum tpm2_option_code tpm2_option_code;
+enum tpm2_option_code {
+    tpm2_option_code_continue,
+    tpm2_option_code_stop,
+    tpm2_option_code_err
+};
+
+/**
+ * Parses the tpm2_tool command line.
+ *
+ * @param argc
+ *  The argc from main.
+ * @param argv
+ *  The argv from main.
+ * @param tool_opts
+ *  The tool options gathered during onstart() lifecycle call.
+ * @param flags
+ *  The tpm2_option_flags to set during parsing.
+ * @param tcti
+ *  The tcti initialized from the tcti options.
+ * @return
+ *  A tpm option code indicating if an error, further processing
+ *  or an immediate exit is desired.
+ * @note
+ *  Used by tpm2_tool, and likely should only be used there.
+ *
+ */
+tpm2_option_code tpm2_handle_options (int argc, char **argv,
+        tpm2_options *tool_opts, tpm2_option_flags *flags,
+        TSS2_TCTI_CONTEXT **tcti);
+
+/**
+ * Print usage summary for a given tpm2 tool.
+ *
+ * @param command
+ *  The command to print its usage summary text.
+ * @param tool_opts
+ *  The tpm2_options array that contains the tool options to print as a summary.
+ */
+void tpm2_print_usage(const char *command, struct tpm2_options *tool_opts);
+
+#endif /* OPTIONS_H */
diff --git a/TPM2-Plugin/lib/include/tpm2_plugin_api.h b/TPM2-Plugin/lib/include/tpm2_plugin_api.h
index 238af99..4c3ad63 100644
--- a/TPM2-Plugin/lib/include/tpm2_plugin_api.h
+++ b/TPM2-Plugin/lib/include/tpm2_plugin_api.h
@@ -35,7 +35,7 @@
 #include <ctype.h>
 #include <getopt.h>
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 #include "plugin_register.h"
 
@@ -90,6 +90,14 @@
 
 TSS2_RC TeardownTctiResMgrContext( TSS2_TCTI_CONTEXT *tctiContext );
 
+int tpm2_plugin_init();
+int tpm2_plugin_uninit();
+int tpm2_plugin_activate(SSHSM_HW_PLUGIN_ACTIVATE_IN_INFO_t *activate_in_info);
+int tpm2_plugin_load_key(
+           SSHSM_HW_PLUGIN_LOAD_KEY_IN_INFO_t *loadkey_in_info,
+           void **keyHandle
+        );
+
 int tpm2_rsa_create_object(
                         unsigned long appHandle,
                         //DhsmWPKRSAFormat* wpk,
@@ -104,13 +112,13 @@
 int tpm2_rsa_delete_object(
                         void *cb_object);
 
-int tpm2_rsa_sign_init(
+int tpm2_plugin_rsa_sign_init(
         unsigned long mechanish,
         void *param,
         size_t len,
         void *ctx);
 
-int tpm2_rsa_sign(
+int tpm2_plugin_rsa_sign(
         void *ctx,
         unsigned char *msg,
         int msg_len,
@@ -119,12 +127,12 @@
 
 
 int tpm2_import_object(
-        unsigned long appHandle, 
-        unsigned char* tlvbuffer, 
-        int buflen, 
-        unsigned char* iv, 
-        int iv_len, 
-        unsigned char* tpm_pwd, 
+        unsigned long appHandle,
+        unsigned char* tlvbuffer,
+        int buflen,
+        unsigned char* iv,
+        int iv_len,
+        unsigned char* tpm_pwd,
         int tpm_pwd_len);
 
 
diff --git a/TPM2-Plugin/lib/include/tpm2_tcti_ldr.h b/TPM2-Plugin/lib/include/tpm2_tcti_ldr.h
index 1e20d3d..684e5e2 100644
--- a/TPM2-Plugin/lib/include/tpm2_tcti_ldr.h
+++ b/TPM2-Plugin/lib/include/tpm2_tcti_ldr.h
@@ -25,7 +25,7 @@
 // THE POSSIBILITY OF SUCH DAMAGE.
 //**********************************************************************;
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 #ifndef LIB_TPM2_TCTI_LDR_H_
 #define LIB_TPM2_TCTI_LDR_H_
@@ -34,8 +34,8 @@
  * Loads a TCTI from a friendly name, library name, or path.
  * For example
  *  friendly:     path = tabrmd
- *  library name: path = libtcti-socket.so
- *  full path:    path = /home/user/lib/libtcti-custom.so
+ *  library name: path = libtss2-tcti-mssim.so
+ *  full path:    path = /home/user/lib/libtss2-tcti-custom.so
  * @param path
  *  The path/library to load.
  * @param opts
@@ -55,6 +55,16 @@
 const TSS2_TCTI_INFO *tpm2_tcti_ldr_getinfo(void);
 
 /**
+ * Given a tcti name, like mssim, tells you if the
+ * library is present using dlopen(3).
+ * @param name
+ *   The friendly name of the tcti.
+ * @return
+ *  True if present, false otherwise.
+ */
+bool tpm2_tcti_ldr_is_tcti_present(const char *name);
+
+/**
  * Unloads the tcti loaded via tpm2_tcti_ldr_load();
  */
 void tpm2_tcti_ldr_unload(void);
diff --git a/TPM2-Plugin/lib/include/tpm2_tool.h b/TPM2-Plugin/lib/include/tpm2_tool.h
new file mode 100644
index 0000000..f24be38
--- /dev/null
+++ b/TPM2-Plugin/lib/include/tpm2_tool.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2016, Intel Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef MAIN_H
+#define MAIN_H
+
+#include <tss2/tss2_sys.h>
+#include <stdbool.h>
+
+#include "tpm2_options.h"
+
+extern bool output_enabled;
+
+/**
+ * An optional interface for tools to specify what options they support.
+ * They are concatenated with main's options and passed to getopt_long.
+ * @param opts
+ *  The callee can choose to set *opts to a tpm_options pointer allocated
+ *  via tpm2_options_new(). Setting *opts to NULL is not an error, and
+ *  Indicates that no options are specified by the tool.
+ *
+ * @return
+ *  True on success, false on error.
+ */
+bool tpm2_tool_onstart(tpm2_options **opts) __attribute__((weak));
+
+/**
+ * This is the main interface for tools, after tcti and sapi initialization
+ * are performed.
+ * @param sapi_context
+ *  The system api context.
+ * @param flags
+ *  Flags that tools may wish to respect.
+ * @return
+ *  0 on success.
+ */
+int tpm2_tool_onrun (TSS2_SYS_CONTEXT *sapi_context, tpm2_option_flags flags) __attribute__((weak));
+
+/**
+ * Called when the tool is exiting, useful for cleanup.
+ */
+void tpm2_tool_onexit(void) __attribute__((weak));
+
+/**
+ * prints output to stdout respecting the quiet option.
+ * Ie when quiet, don't print.
+ * @param fmt
+ *  The format specifier, ala printf.
+ * @param ...
+ *  The varargs, just like printf.
+ */
+#define tpm2_tool_output(fmt, ...)                   \
+    do {                                        \
+        if (output_enabled) {                   \
+            printf(fmt, ##__VA_ARGS__);         \
+        }                                       \
+    } while (0)
+
+#endif /* MAIN_H */
diff --git a/TPM2-Plugin/lib/include/tpm2_util.h b/TPM2-Plugin/lib/include/tpm2_util.h
index edc759d..de02777 100644
--- a/TPM2-Plugin/lib/include/tpm2_util.h
+++ b/TPM2-Plugin/lib/include/tpm2_util.h
@@ -35,7 +35,7 @@
 #include <stdint.h>
 #include <stdio.h>
 
-#include <sapi/tpm20.h>
+#include <tss2/tss2_sys.h>
 
 #include "tpm2_error.h"
 
@@ -118,20 +118,10 @@
         __result;                                          \
     })
 
-/**
- * prints output to stdout respecting the quiet option.
- * Ie when quiet, don't print.
- * @param fmt
- *  The format specifier, ala printf.
- * @param ...
- *  The varargs, just like printf.
- */
-#define tpm2_tool_output(fmt, ...)                   \
-    do {                                        \
-        if (output_enabled) {                   \
-            printf(fmt, ##__VA_ARGS__);         \
-        }                                       \
-    } while (0)
+typedef struct {
+    UINT16 size;
+    BYTE buffer[0];
+} TPM2B;
 
 int tpm2_util_hex_to_byte_structure(const char *inStr, UINT16 *byteLenth, BYTE *byteBuffer);
 
@@ -176,28 +166,23 @@
  *  The data to print.
  * @param len
  *  The length of the data.
- * @param plain
- *  true for a plain hex string false for an xxd compatable
- *  dump.
  */
-void tpm2_util_hexdump(const BYTE *data, size_t len, bool plain);
+void tpm2_util_hexdump(const BYTE *data, size_t len);
 
 /**
- * Prints an xxd compatible hexdump to stdout if output is enabled,
+ * Prints a file as a hex string to stdout if quiet mode
+ * is not enabled.
  * ie no -Q option.
  *
  * @param fd
  *  A readable open file.
  * @param len
  *  The length of the data to read and print.
- * @param plain
- *  true for a plain hex string false for an xxd compatable
- *  dump.
  * @return
  *  true if len bytes were successfully read and printed,
  *  false otherwise
  */
-bool tpm2_util_hexdump_file(FILE *fd, size_t len, bool plain);
+bool tpm2_util_hexdump_file(FILE *fd, size_t len);
 
 /**
  * Prints a TPM2B as a hex dump.
@@ -205,7 +190,7 @@
  */
 static inline void tpm2_util_print_tpm2b(TPM2B *buffer) {
 
-    return tpm2_util_hexdump(buffer->buffer, buffer->size, true);
+    return tpm2_util_hexdump(buffer->buffer, buffer->size);
 }
 
 /**
@@ -216,18 +201,6 @@
 bool tpm2_util_print_tpm2b_file(FILE *fd);
 
 /**
- * Copies a tpm2b from dest to src and clears dest if src is NULL.
- * If src is NULL, it is a NOP.
- * @param dest
- *  The destination TPM2B
- * @param src
- *  The source TPM2B
- * @return
- *  The number of bytes copied.
- */
-UINT16 tpm2_util_copy_tpm2b(TPM2B *dest, TPM2B *src);
-
-/**
  * Checks if the host is big endian
  * @return
  *  True of the host is big endian false otherwise.