ia64/xen-unstable

changeset 10774:6b08035e10c5

VTPM_TOOLS: Updated vtpm for new release of tpm_emulator 0.4, and set
up easier tracking of the emulator. Also fixed a few minor bugs in the
manager.

Signed-off-by: Vincent Scarlata <vincent.r.scarlata@intel.com>
author kfraser@localhost.localdomain
date Tue Jul 25 11:30:30 2006 +0100 (2006-07-25)
parents 7d6f0791c650
children f07effe5097b
files tools/vtpm/Makefile tools/vtpm/tpm_emulator-0.3-x86_64.patch tools/vtpm/tpm_emulator.patch tools/vtpm/vtpm.patch tools/vtpm_manager/manager/dmictl.c tools/vtpm_manager/manager/vtpm_manager_handler.c tools/vtpm_manager/manager/vtpmd.c tools/vtpm_manager/tcs/tcs.c tools/vtpm_manager/tcs/transmit.c
line diff
     1.1 --- a/tools/vtpm/Makefile	Tue Jul 25 11:19:48 2006 +0100
     1.2 +++ b/tools/vtpm/Makefile	Tue Jul 25 11:30:30 2006 +0100
     1.3 @@ -7,9 +7,11 @@ include $(XEN_ROOT)/tools/vtpm/Rules.mk
     1.4  TPM_EMULATOR_DIR = tpm_emulator
     1.5  # Dir name for vtpm instance
     1.6  VTPM_DIR = vtpm
     1.7 +ORIG_DIR = orig
     1.8  
     1.9  # Emulator tarball name
    1.10 -TPM_EMULATOR_TARFILE = tpm_emulator-0.3.tar.gz
    1.11 +TPM_EMULATOR_NAME = tpm_emulator-0.4
    1.12 +TPM_EMULATOR_TARFILE = $(TPM_EMULATOR_NAME).tar.gz
    1.13  
    1.14  GMP_HEADER = /usr/include/gmp.h
    1.15  
    1.16 @@ -17,7 +19,7 @@ GMP_HEADER = /usr/include/gmp.h
    1.17  all: build
    1.18  
    1.19  .PHONY: build
    1.20 -build: $(TPM_EMULATOR_DIR) $(VTPM_DIR) build_sub
    1.21 +build: $(VTPM_DIR) $(TPM_EMULATOR_DIR) build_sub
    1.22  
    1.23  .PHONY: install
    1.24  install: build
    1.25 @@ -37,9 +39,8 @@ clean:
    1.26  
    1.27  .PHONY: mrproper
    1.28  mrproper:
    1.29 -	rm -f $(TPM_EMULATOR_TARFILE)
    1.30 -	rm -rf $(TPM_EMULATOR_DIR)
    1.31 -	rm -rf $(VTPM_DIR)
    1.32 +	rm -f $(TPM_EMULATOR_TARFILE) tpm_emulator.patch.old vtpm.patch.old
    1.33 +	rm -rf $(TPM_EMULATOR_DIR) $(VTPM_DIR) $(ORIG_DIR)
    1.34  
    1.35  # Download Swiss emulator
    1.36  $(TPM_EMULATOR_TARFILE):
    1.37 @@ -47,24 +48,38 @@ mrproper:
    1.38  
    1.39  # Create vtpm and TPM emulator dirs
    1.40  # apply patches for 1) used as dom0 tpm driver 2) used as vtpm device instance
    1.41 -$(TPM_EMULATOR_DIR): $(TPM_EMULATOR_TARFILE) tpm_emulator.patch tpm_emulator-0.3-x86_64.patch
    1.42 +$(TPM_EMULATOR_DIR): $(TPM_EMULATOR_TARFILE) tpm_emulator.patch 
    1.43  	if [ "$(BUILD_EMULATOR)" = "y" ]; then \
    1.44 +		rm -rf $(TPM_EMULATOR_DIR); \
    1.45  		tar -xzf $(TPM_EMULATOR_TARFILE); \
    1.46 -		rm -rf $(TPM_EMULATOR_DIR); \
    1.47 -		mv tpm_emulator-0.3 $(TPM_EMULATOR_DIR); \
    1.48 +		mv $(TPM_EMULATOR_NAME) $(TPM_EMULATOR_DIR); \
    1.49  		cd $(TPM_EMULATOR_DIR); \
    1.50 -		patch -p1 < ../tpm_emulator-0.3-x86_64.patch; \
    1.51  		patch -p1 <../tpm_emulator.patch; \
    1.52  	fi
    1.53  
    1.54 -$(VTPM_DIR): $(TPM_EMULATOR_TARFILE) tpm_emulator-0.3-x86_64.patch vtpm.patch
    1.55 +$(VTPM_DIR): $(TPM_EMULATOR_TARFILE) vtpm.patch
    1.56 +	rm -rf $(VTPM_DIR)
    1.57  	tar -xzf $(TPM_EMULATOR_TARFILE);  
    1.58 -	rm -rf $(VTPM_DIR)
    1.59 -	mv tpm_emulator-0.3 $(VTPM_DIR); 
    1.60 +	mv $(TPM_EMULATOR_NAME) $(VTPM_DIR); 
    1.61  
    1.62  	cd $(VTPM_DIR); \
    1.63 -	patch -p1 < ../tpm_emulator-0.3-x86_64.patch; \
    1.64 -	patch -p1 <../vtpm.patch
    1.65 +	patch -p1 < ../tpm_emulator.patch; \
    1.66 +	patch -p1 < ../vtpm.patch
    1.67 +
    1.68 +orig: $(TPM_EMULATOR_TARFILE)
    1.69 +	mkdir $(ORIG_DIR);
    1.70 +	cd $(ORIG_DIR); \
    1.71 +	tar -xzf ../$(TPM_EMULATOR_TARFILE);
    1.72 +
    1.73 +updatepatches: clean orig
    1.74 +	if [ "$(BUILD_EMULATOR)" = "y" ]; then \
    1.75 +		find $(TPM_EMULATOR_DIR) -name "*.orig" -print | xargs rm -f; \
    1.76 +		mv tpm_emulator.patch tpm_emulator.patch.old; \
    1.77 +		diff -uprN orig/$(TPM_EMULATOR_NAME) $(TPM_EMULATOR_DIR) > tpm_emulator.patch || true; \
    1.78 +        fi; 
    1.79 +	find $(VTPM_DIR) -name "*.orig" -print | xargs rm -f; 
    1.80 +	mv vtpm.patch vtpm.patch.old; 
    1.81 +	diff -uprN $(TPM_EMULATOR_DIR) $(VTPM_DIR) > vtpm.patch || true;
    1.82  
    1.83  .PHONY: build_sub
    1.84  build_sub:
     2.1 --- a/tools/vtpm/tpm_emulator-0.3-x86_64.patch	Tue Jul 25 11:19:48 2006 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,737 +0,0 @@
     2.4 -diff -uprN orig/tpm_emulator-0.3/Makefile tpm_emulator-0.3-x86_64/Makefile
     2.5 ---- orig/tpm_emulator-0.3/Makefile	2006-01-10 04:21:45.000000000 -0800
     2.6 -+++ tpm_emulator-0.3-x86_64/Makefile	2006-08-29 15:08:20.532342768 -0700
     2.7 -@@ -7,6 +7,7 @@
     2.8 - KERNEL_RELEASE := $(shell uname -r)
     2.9 - KERNEL_BUILD   := /lib/modules/$(KERNEL_RELEASE)/build
    2.10 - MOD_SUBDIR     := misc
    2.11 -+COMPILE_ARCH    ?= $(shell uname -m | sed -e s/i.86/x86_32/)
    2.12 - 
    2.13 - # module settings
    2.14 - MODULE_NAME    := tpm_emulator
    2.15 -@@ -17,8 +18,14 @@ VERSION_BUILD  := $(shell date +"%s")
    2.16 - # enable/disable DEBUG messages
    2.17 - EXTRA_CFLAGS   += -Wall -DDEBUG -g  
    2.18 - 
    2.19 -+ifeq ($(COMPILE_ARCH),x86_64)
    2.20 -+LIBDIR = lib64
    2.21 -+else
    2.22 -+LIBDIR = lib
    2.23 -+endif
    2.24 -+
    2.25 - # GNU MP configuration
    2.26 --GMP_LIB        := /usr/lib/libgmp.a
    2.27 -+GMP_LIB        := /usr/$(LIBDIR)/libgmp.a
    2.28 - GMP_HEADER     := /usr/include/gmp.h
    2.29 - 
    2.30 - # sources and objects
    2.31 -diff -uprN orig/tpm_emulator-0.3/README tpm_emulator-0.3-x86_64/README
    2.32 ---- orig/tpm_emulator-0.3/README	2006-01-10 04:21:45.000000000 -0800
    2.33 -+++ tpm_emulator-0.3-x86_64/README	2006-08-29 15:07:43.530967832 -0700
    2.34 -@@ -43,6 +43,12 @@ Example:
    2.35 - GMP_LIB        := /usr/lib/libgmp.a
    2.36 - GMP_HEADER     := /usr/include/gmp.h
    2.37 - 
    2.38 -+GNU MP Library on 64 bit Systems
    2.39 -+--------------------------------------------------------------------------
    2.40 -+Some 64-bit kernels have problems with importing the user-space gmp 
    2.41 -+library (/usr/lib*/libgmp.a) into kernel space.  These kernels will require
    2.42 -+that the gmp library be recompiled for kernel space with -mcmodel=kernel.
    2.43 -+
    2.44 - Installation
    2.45 - --------------------------------------------------------------------------
    2.46 - The compilation and installation process uses the build environment for 
    2.47 -diff -uprN orig/tpm_emulator-0.3/crypto/gmp_kernel_wrapper.c tpm_emulator-0.3-x86_64/crypto/gmp_kernel_wrapper.c
    2.48 ---- orig/tpm_emulator-0.3/crypto/gmp_kernel_wrapper.c	2006-01-10 04:21:45.000000000 -0800
    2.49 -+++ tpm_emulator-0.3-x86_64/crypto/gmp_kernel_wrapper.c	2006-08-29 15:07:43.525968592 -0700
    2.50 -@@ -79,7 +79,7 @@ void __attribute__ ((regparm(0))) *kerne
    2.51 - {
    2.52 -   void *ret  = (void*)kmalloc(size, GFP_KERNEL);
    2.53 -   if (!ret) panic(KERN_CRIT TPM_MODULE_NAME 
    2.54 --    "GMP: cannot allocate memory (size=%u)\n", size);
    2.55 -+    "GMP: cannot allocate memory (size=%Zu)\n", size);
    2.56 -   return ret;
    2.57 - }
    2.58 - 
    2.59 -@@ -88,7 +88,7 @@ void __attribute__ ((regparm(0))) *kerne
    2.60 - {
    2.61 -   void *ret = (void*)kmalloc(new_size, GFP_KERNEL);
    2.62 -   if (!ret) panic(KERN_CRIT TPM_MODULE_NAME "GMP: Cannot reallocate memory "
    2.63 --    "(old_size=%u new_size=%u)\n", old_size, new_size);
    2.64 -+    "(old_size=%Zu new_size=%Zu)\n", old_size, new_size);
    2.65 -   memcpy(ret, oldptr, old_size);
    2.66 -   kfree(oldptr);
    2.67 -   return ret;
    2.68 -diff -uprN orig/tpm_emulator-0.3/linux_module.c tpm_emulator-0.3-x86_64/linux_module.c
    2.69 ---- orig/tpm_emulator-0.3/linux_module.c	2006-01-10 04:21:45.000000000 -0800
    2.70 -+++ tpm_emulator-0.3-x86_64/linux_module.c	2006-08-29 15:07:43.526968440 -0700
    2.71 -@@ -72,7 +72,7 @@ static int tpm_release(struct inode *ino
    2.72 - 
    2.73 - static ssize_t tpm_read(struct file *file, char *buf, size_t count, loff_t *ppos)
    2.74 - {
    2.75 --  debug("%s(%d)", __FUNCTION__, count);
    2.76 -+  debug("%s(%Zu)", __FUNCTION__, count);
    2.77 -   down(&tpm_mutex);
    2.78 -   if (tpm_response.data != NULL) {
    2.79 -     count = min(count, (size_t)tpm_response.size - (size_t)*ppos);
    2.80 -@@ -91,7 +91,7 @@ static ssize_t tpm_read(struct file *fil
    2.81 - 
    2.82 - static ssize_t tpm_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
    2.83 - {
    2.84 --  debug("%s(%d)", __FUNCTION__, count);
    2.85 -+  debug("%s(%Zu)", __FUNCTION__, count);
    2.86 -   down(&tpm_mutex);
    2.87 -   *ppos = 0;
    2.88 -   if (tpm_response.data != NULL) kfree(tpm_response.data);
    2.89 -diff -uprN orig/tpm_emulator-0.3/linux_module.h tpm_emulator-0.3-x86_64/linux_module.h
    2.90 ---- orig/tpm_emulator-0.3/linux_module.h	2006-01-10 04:21:45.000000000 -0800
    2.91 -+++ tpm_emulator-0.3-x86_64/linux_module.h	2006-08-29 15:07:43.527968288 -0700
    2.92 -@@ -28,8 +28,10 @@
    2.93 - 
    2.94 - /* module settings */
    2.95 - 
    2.96 -+#ifndef STR
    2.97 - #define STR(s) __STR__(s)
    2.98 - #define __STR__(s) #s
    2.99 -+#endif
   2.100 - #include "tpm_version.h"
   2.101 - 
   2.102 - #define TPM_DEVICE_MINOR	224
   2.103 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_credentials.c tpm_emulator-0.3-x86_64/tpm/tpm_credentials.c
   2.104 ---- orig/tpm_emulator-0.3/tpm/tpm_credentials.c	2006-01-10 04:21:45.000000000 -0800
   2.105 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_credentials.c	2006-08-29 15:07:43.530967832 -0700
   2.106 -@@ -47,16 +47,16 @@ int tpm_compute_pubkey_checksum(TPM_NONC
   2.107 - 
   2.108 - TPM_RESULT tpm_get_pubek(TPM_PUBKEY *pubEndorsementKey)
   2.109 - {
   2.110 --  UINT32 key_length;
   2.111 -+  size_t key_length;
   2.112 -   if (!tpmData.permanent.data.endorsementKey.size) return TPM_NO_ENDORSEMENT;
   2.113 -   /* setup TPM_PUBKEY structure */
   2.114 --  key_length = tpmData.permanent.data.endorsementKey.size;
   2.115 --  pubEndorsementKey->pubKey.keyLength = key_length >> 3;
   2.116 -+  pubEndorsementKey->pubKey.keyLength = tpmData.permanent.data.endorsementKey.size >> 3;
   2.117 -   pubEndorsementKey->pubKey.key = tpm_malloc(pubEndorsementKey->pubKey.keyLength);
   2.118 -   if (pubEndorsementKey->pubKey.key == NULL) return TPM_FAIL;
   2.119 -   rsa_export_modulus(&tpmData.permanent.data.endorsementKey,
   2.120 --    pubEndorsementKey->pubKey.key,
   2.121 --    &pubEndorsementKey->pubKey.keyLength);
   2.122 -+		     pubEndorsementKey->pubKey.key,
   2.123 -+		     &key_length);
   2.124 -+  pubEndorsementKey->pubKey.keyLength = key_length;
   2.125 -   pubEndorsementKey->algorithmParms.algorithmID = TPM_ALG_RSA;
   2.126 -   pubEndorsementKey->algorithmParms.encScheme = TPM_ES_RSAESOAEP_SHA1_MGF1;
   2.127 -   pubEndorsementKey->algorithmParms.sigScheme = TPM_SS_NONE;
   2.128 -@@ -175,6 +175,7 @@ TPM_RESULT TPM_OwnerReadInternalPub(TPM_
   2.129 - {
   2.130 -   TPM_RESULT res;
   2.131 -   TPM_KEY_DATA *srk = &tpmData.permanent.data.srk;
   2.132 -+  size_t key_length;
   2.133 -   info("TPM_OwnerReadInternalPub()");
   2.134 -   /* verify authorization */
   2.135 -   res = tpm_verify_auth(auth1, tpmData.permanent.data.ownerAuth, TPM_KH_OWNER);
   2.136 -@@ -186,7 +187,8 @@ TPM_RESULT TPM_OwnerReadInternalPub(TPM_
   2.137 -     publicPortion->pubKey.key = tpm_malloc(publicPortion->pubKey.keyLength);
   2.138 -     if (publicPortion->pubKey.key == NULL) return TPM_FAIL;
   2.139 -     rsa_export_modulus(&srk->key, publicPortion->pubKey.key, 
   2.140 --      &publicPortion->pubKey.keyLength);
   2.141 -+      &key_length);
   2.142 -+    publicPortion->pubKey.keyLength = key_length;
   2.143 -     publicPortion->algorithmParms.algorithmID = TPM_ALG_RSA;
   2.144 -     publicPortion->algorithmParms.encScheme = srk->encScheme;
   2.145 -     publicPortion->algorithmParms.sigScheme = srk->sigScheme;
   2.146 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_crypto.c tpm_emulator-0.3-x86_64/tpm/tpm_crypto.c
   2.147 ---- orig/tpm_emulator-0.3/tpm/tpm_crypto.c	2006-01-10 04:21:45.000000000 -0800
   2.148 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_crypto.c	2006-08-29 15:07:43.531967680 -0700
   2.149 -@@ -182,7 +182,8 @@ TPM_RESULT TPM_CertifyKey(TPM_KEY_HANDLE
   2.150 -   TPM_KEY_DATA *cert, *key;
   2.151 -   sha1_ctx_t sha1_ctx;
   2.152 -   BYTE *buf, *p;
   2.153 --  UINT32 length;
   2.154 -+  UINT32 length32;
   2.155 -+  size_t length;
   2.156 -   info("TPM_CertifyKey()");
   2.157 -   /* get keys */
   2.158 -   cert = tpm_get_key(certHandle);
   2.159 -@@ -264,14 +265,15 @@ TPM_RESULT TPM_CertifyKey(TPM_KEY_HANDLE
   2.160 -   /* compute the digest of the CERTIFY_INFO[2] structure and sign it */
   2.161 -   length = sizeof_TPM_CERTIFY_INFO((*certifyInfo));
   2.162 -   p = buf = tpm_malloc(length);
   2.163 -+  length32=(UINT32) length;
   2.164 -   if (buf == NULL
   2.165 --      || tpm_marshal_TPM_CERTIFY_INFO(&p, &length, certifyInfo)) {
   2.166 -+      || tpm_marshal_TPM_CERTIFY_INFO(&p, &length32, certifyInfo)) {
   2.167 -     free_TPM_KEY_PARMS(certifyInfo->algorithmParms);
   2.168 -     return TPM_FAIL;
   2.169 -   }
   2.170 -   length = sizeof_TPM_CERTIFY_INFO((*certifyInfo));
   2.171 -   sha1_init(&sha1_ctx);
   2.172 --  sha1_update(&sha1_ctx, buf, length);
   2.173 -+  sha1_update(&sha1_ctx, buf, (size_t) length);
   2.174 -   sha1_final(&sha1_ctx, buf);
   2.175 -   res = tpm_sign(cert, auth1, FALSE, buf, SHA1_DIGEST_LENGTH, outData, outDataSize);
   2.176 -   tpm_free(buf);
   2.177 -@@ -292,7 +294,8 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
   2.178 -   TPM_KEY_DATA *cert, *key;
   2.179 -   sha1_ctx_t sha1_ctx;
   2.180 -   BYTE *buf, *p;
   2.181 --  UINT32 length;
   2.182 -+  size_t length;
   2.183 -+  UINT32 length32;
   2.184 -   info("TPM_CertifyKey2()");
   2.185 -   /* get keys */
   2.186 -   cert = tpm_get_key(certHandle);
   2.187 -@@ -362,8 +365,9 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
   2.188 -   /* compute the digest of the CERTIFY_INFO[2] structure and sign it */
   2.189 -   length = sizeof_TPM_CERTIFY_INFO((*certifyInfo));
   2.190 -   p = buf = tpm_malloc(length);
   2.191 -+  length32 = (UINT32) length;
   2.192 -   if (buf == NULL
   2.193 --      || tpm_marshal_TPM_CERTIFY_INFO(&p, &length, certifyInfo)) {
   2.194 -+      || tpm_marshal_TPM_CERTIFY_INFO(&p, &length32, certifyInfo)) {
   2.195 -     free_TPM_KEY_PARMS(certifyInfo->algorithmParms);
   2.196 -     return TPM_FAIL;
   2.197 -   }
   2.198 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_data.c tpm_emulator-0.3-x86_64/tpm/tpm_data.c
   2.199 ---- orig/tpm_emulator-0.3/tpm/tpm_data.c	2006-01-10 04:21:45.000000000 -0800
   2.200 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_data.c	2006-08-29 15:08:20.535342312 -0700
   2.201 -@@ -214,23 +214,30 @@ static int read_from_file(uint8_t **data
   2.202 - int tpm_store_permanent_data(void)
   2.203 - {
   2.204 -   uint8_t *buf, *ptr;
   2.205 --  size_t buf_length, len;
   2.206 -+  UINT32 buf_length, len;
   2.207 - 
   2.208 -   /* marshal data */
   2.209 --  buf_length = len = sizeof_TPM_STCLEAR_FLAGS(tpmData.stclear.flags)
   2.210 --    + sizeof_TPM_PERMANENT_FLAGS(tpmData.permanent.flags) + 2
   2.211 --    + sizeof_TPM_PERMANENT_DATA(tpmData.permanent.data);
   2.212 -+  buf_length = len = 4 + sizeof_TPM_STCLEAR_FLAGS(tpmData.stclear.flags)
   2.213 -+    + sizeof_TPM_PERMANENT_FLAGS(tpmData.permanent.flags) 
   2.214 -+    + sizeof_TPM_STANY_FLAGS(tpmData.stany.flags) + 2
   2.215 -+    + sizeof_TPM_STCLEAR_DATA(tpmData.stclear.data) 
   2.216 -+    + sizeof_TPM_PERMANENT_DATA(tpmData.permanent.data)
   2.217 -+    + sizeof_TPM_STANY_DATA(tpmData.stany.data);
   2.218 -   buf = ptr = tpm_malloc(buf_length);
   2.219 -   if (buf == NULL
   2.220 -       || tpm_marshal_TPM_VERSION(&ptr, &len, &tpmData.permanent.data.version)
   2.221 -       || tpm_marshal_TPM_STCLEAR_FLAGS(&ptr, &len, &tpmData.stclear.flags)
   2.222 -       || tpm_marshal_TPM_PERMANENT_FLAGS(&ptr, &len, &tpmData.permanent.flags)
   2.223 -+      || tpm_marshal_TPM_STANY_FLAGS(&ptr, &len, &tpmData.stany.flags)
   2.224 -       || tpm_marshal_BOOL(&ptr, &len, tpmData.permanent.flags.selfTestSucceeded)
   2.225 -       || tpm_marshal_BOOL(&ptr, &len, tpmData.permanent.flags.owned)
   2.226 --      || tpm_marshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)) {
   2.227 -+      || tpm_marshal_TPM_STCLEAR_DATA(&ptr, &len, &tpmData.stclear.data)
   2.228 -+      || tpm_marshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)
   2.229 -+      || tpm_marshal_TPM_STANY_DATA(&ptr, &len, &tpmData.stany.data)) {
   2.230 -     tpm_free(buf);
   2.231 -     return -1;
   2.232 -   }
   2.233 -+
   2.234 -   if (write_to_file(buf, buf_length - len)) {
   2.235 -     tpm_free(buf);
   2.236 -     return -1; 
   2.237 -@@ -242,24 +249,29 @@ int tpm_store_permanent_data(void)
   2.238 - int tpm_restore_permanent_data(void)
   2.239 - {
   2.240 -   uint8_t *buf, *ptr;
   2.241 --  size_t buf_length, len;
   2.242 -+  size_t buf_length;
   2.243 -+  UINT32 len;
   2.244 -   TPM_VERSION ver;
   2.245 - 
   2.246 -   /* read data */
   2.247 -   if (read_from_file(&buf, &buf_length)) return -1;
   2.248 -   ptr = buf;
   2.249 --  len = buf_length;
   2.250 -+  len = (uint32_t) buf_length;
   2.251 -   /* unmarshal data */
   2.252 -   if (tpm_unmarshal_TPM_VERSION(&ptr, &len, &ver)
   2.253 -       || memcmp(&ver, &tpmData.permanent.data.version, sizeof(TPM_VERSION))
   2.254 -       || tpm_unmarshal_TPM_STCLEAR_FLAGS(&ptr, &len, &tpmData.stclear.flags)
   2.255 -       || tpm_unmarshal_TPM_PERMANENT_FLAGS(&ptr, &len, &tpmData.permanent.flags)
   2.256 -+      || tpm_unmarshal_TPM_STANY_FLAGS(&ptr, &len, &tpmData.stany.flags)
   2.257 -       || tpm_unmarshal_BOOL(&ptr, &len, &tpmData.permanent.flags.selfTestSucceeded)
   2.258 -       || tpm_unmarshal_BOOL(&ptr, &len, &tpmData.permanent.flags.owned)
   2.259 --      || tpm_unmarshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)) {
   2.260 -+      || tpm_unmarshal_TPM_STCLEAR_DATA(&ptr, &len, &tpmData.stclear.data)
   2.261 -+      || tpm_unmarshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)
   2.262 -+      || tpm_unmarshal_TPM_STANY_DATA(&ptr, &len, &tpmData.stany.data)) {
   2.263 -     tpm_free(buf);
   2.264 -     return -1;
   2.265 -   }
   2.266 -+
   2.267 -   tpm_free(buf);
   2.268 -   return 0;
   2.269 - }
   2.270 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_marshalling.c tpm_emulator-0.3-x86_64/tpm/tpm_marshalling.c
   2.271 ---- orig/tpm_emulator-0.3/tpm/tpm_marshalling.c	2006-01-10 04:21:45.000000000 -0800
   2.272 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_marshalling.c	2006-08-29 15:08:20.537342008 -0700
   2.273 -@@ -1212,7 +1212,7 @@ int tpm_unmarshal_TPM_STANY_FLAGS(BYTE *
   2.274 - 
   2.275 - int tpm_marshal_RSA(BYTE **ptr, UINT32 *length, rsa_private_key_t *v)
   2.276 - {
   2.277 --  UINT32 m_len, e_len, q_len;
   2.278 -+  size_t m_len, e_len, q_len;
   2.279 -   if (*length < sizeof_RSA((*v))) return -1;
   2.280 -   if (v->size > 0) {
   2.281 -     rsa_export_modulus(v, &(*ptr)[6], &m_len);
   2.282 -@@ -1356,6 +1356,66 @@ int tpm_unmarshal_TPM_PERMANENT_DATA(BYT
   2.283 -   return 0;
   2.284 - }
   2.285 - 
   2.286 -+int tpm_marshal_TPM_STCLEAR_DATA(BYTE **ptr, UINT32 *length, TPM_STCLEAR_DATA *v)
   2.287 -+{
   2.288 -+  if (tpm_marshal_TPM_STRUCTURE_TAG(ptr, length, v->tag)
   2.289 -+    || tpm_marshal_TPM_NONCE(ptr, length, &v->contextNonceKey)
   2.290 -+    || tpm_marshal_TPM_COUNT_ID(ptr, length, v->countID) ) return -1;
   2.291 -+
   2.292 -+  return 0;
   2.293 -+}
   2.294 -+
   2.295 -+int tpm_unmarshal_TPM_STCLEAR_DATA(BYTE **ptr, UINT32 *length, TPM_STCLEAR_DATA *v)
   2.296 -+{
   2.297 -+  if (tpm_unmarshal_TPM_STRUCTURE_TAG(ptr, length, &v->tag)
   2.298 -+    || tpm_unmarshal_TPM_NONCE(ptr, length, &v->contextNonceKey)
   2.299 -+    || tpm_unmarshal_TPM_COUNT_ID(ptr, length, &v->countID) ) return -1;
   2.300 -+
   2.301 -+  return 0;
   2.302 -+}
   2.303 -+
   2.304 -+int tpm_marshal_TPM_STANY_DATA(BYTE **ptr, UINT32 *length, TPM_STANY_DATA *v)
   2.305 -+{
   2.306 -+  UINT32 i;
   2.307 -+  if (tpm_marshal_TPM_STRUCTURE_TAG(ptr, length, v->tag)
   2.308 -+    || tpm_marshal_TPM_NONCE(ptr, length, &v->contextNonceSession)
   2.309 -+    || tpm_marshal_TPM_DIGEST(ptr, length, &v->auditDigest)
   2.310 -+    || tpm_marshal_BOOL(ptr, length, v->auditSession)
   2.311 -+    || tpm_marshal_TPM_CURRENT_TICKS(ptr, length, &v->currentTicks)
   2.312 -+    || tpm_marshal_UINT32(ptr, length, v->contextCount)
   2.313 -+    || tpm_marshal_UINT32_ARRAY(ptr, length, v->contextList, TPM_MAX_SESSION_LIST)) return -1;
   2.314 -+  for (i = 0; i < TPM_MAX_SESSIONS; i++) {
   2.315 -+    if (tpm_marshal_TPM_SESSION_DATA(ptr, length, &v->sessions[i])) return -1;
   2.316 -+  }
   2.317 -+  for (i = 0; i < TPM_MAX_SESSIONS_DAA; i++) {
   2.318 -+    if (tpm_marshal_TPM_DAA_SESSION_DATA(ptr, length, &v->sessionsDAA[i])) return -1;
   2.319 -+  }
   2.320 -+  if (tpm_marshal_TPM_TRANSHANDLE(ptr, length, v->transExclusive)) return -1;
   2.321 -+
   2.322 -+  return 0;
   2.323 -+}
   2.324 -+
   2.325 -+int tpm_unmarshal_TPM_STANY_DATA(BYTE **ptr, UINT32 *length, TPM_STANY_DATA *v)
   2.326 -+{
   2.327 -+  UINT32 i;
   2.328 -+  if (tpm_unmarshal_TPM_STRUCTURE_TAG(ptr, length, &v->tag)
   2.329 -+    || tpm_unmarshal_TPM_NONCE(ptr, length, &v->contextNonceSession)
   2.330 -+    || tpm_unmarshal_TPM_DIGEST(ptr, length, &v->auditDigest)
   2.331 -+    || tpm_unmarshal_BOOL(ptr, length, &v->auditSession)
   2.332 -+    || tpm_unmarshal_TPM_CURRENT_TICKS(ptr, length, &v->currentTicks)
   2.333 -+    || tpm_unmarshal_UINT32(ptr, length, &v->contextCount)
   2.334 -+    || tpm_unmarshal_UINT32_ARRAY(ptr, length, v->contextList, TPM_MAX_SESSION_LIST)) return -1;
   2.335 -+  for (i = 0; i < TPM_MAX_SESSIONS; i++) {
   2.336 -+    if (tpm_unmarshal_TPM_SESSION_DATA(ptr, length, &v->sessions[i])) return -1;
   2.337 -+  }
   2.338 -+  for (i = 0; i < TPM_MAX_SESSIONS_DAA; i++) {
   2.339 -+    if (tpm_unmarshal_TPM_DAA_SESSION_DATA(ptr, length, &v->sessionsDAA[i])) return -1;
   2.340 -+  }
   2.341 -+  if (tpm_unmarshal_TPM_TRANSHANDLE(ptr, length, &v->transExclusive)) return -1;
   2.342 -+
   2.343 -+  return 0;
   2.344 -+}
   2.345 -+
   2.346 - int tpm_marshal_TPM_SESSION_DATA(BYTE **ptr, UINT32 *length, TPM_SESSION_DATA *v)
   2.347 - {
   2.348 -   if (tpm_marshal_BYTE(ptr, length, v->type)
   2.349 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_marshalling.h tpm_emulator-0.3-x86_64/tpm/tpm_marshalling.h
   2.350 ---- orig/tpm_emulator-0.3/tpm/tpm_marshalling.h	2006-01-10 04:21:45.000000000 -0800
   2.351 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_marshalling.h	2006-08-29 15:08:20.538341856 -0700
   2.352 -@@ -420,6 +420,12 @@ int tpm_unmarshal_TPM_KEY_DATA(BYTE **pt
   2.353 - int tpm_marshal_TPM_PERMANENT_DATA(BYTE **ptr, UINT32 *length, TPM_PERMANENT_DATA *);
   2.354 - int tpm_unmarshal_TPM_PERMANENT_DATA(BYTE **ptr, UINT32 *length, TPM_PERMANENT_DATA *);
   2.355 - 
   2.356 -+int tpm_marshal_TPM_STCLEAR_DATA(BYTE **ptr, UINT32 *length, TPM_STCLEAR_DATA *v);
   2.357 -+int tpm_unmarshal_TPM_STCLEAR_DATA(BYTE **ptr, UINT32 *length, TPM_STCLEAR_DATA *v);
   2.358 -+
   2.359 -+int tpm_marshal_TPM_STANY_DATA(BYTE **ptr, UINT32 *length, TPM_STANY_DATA *v);
   2.360 -+int tpm_unmarshal_TPM_STANY_DATA(BYTE **ptr, UINT32 *length, TPM_STANY_DATA *v);
   2.361 -+
   2.362 - int tpm_marshal_TPM_SESSION_DATA(BYTE **ptr, UINT32 *length, TPM_SESSION_DATA *v);
   2.363 - int tpm_unmarshal_TPM_SESSION_DATA(BYTE **ptr, UINT32 *length, TPM_SESSION_DATA *v);
   2.364 - 
   2.365 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_owner.c tpm_emulator-0.3-x86_64/tpm/tpm_owner.c
   2.366 ---- orig/tpm_emulator-0.3/tpm/tpm_owner.c	2006-01-10 04:21:45.000000000 -0800
   2.367 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_owner.c	2006-08-29 15:07:43.535967072 -0700
   2.368 -@@ -108,7 +108,7 @@ TPM_RESULT TPM_TakeOwnership(TPM_PROTOCO
   2.369 -   TPM_RESULT res;
   2.370 -   rsa_private_key_t *ek = &tpmData.permanent.data.endorsementKey;
   2.371 -   TPM_KEY_DATA *srk = &tpmData.permanent.data.srk;
   2.372 --  UINT32 buf_size = ek->size >> 3;
   2.373 -+  size_t buf_size = ek->size >> 3, key_length; 
   2.374 -   BYTE buf[buf_size];
   2.375 - 
   2.376 -   info("TPM_TakeOwnership()");
   2.377 -@@ -172,7 +172,8 @@ TPM_RESULT TPM_TakeOwnership(TPM_PROTOCO
   2.378 -     return TPM_FAIL;
   2.379 -   }
   2.380 -   rsa_export_modulus(&srk->key, srkPub->pubKey.key,
   2.381 --    &srkPub->pubKey.keyLength);
   2.382 -+		     &key_length);
   2.383 -+  srkPub->pubKey.keyLength = (UINT32) key_length;
   2.384 -   /* setup tpmProof and set state to owned */
   2.385 -   tpm_get_random_bytes(tpmData.permanent.data.tpmProof.nonce, 
   2.386 -     sizeof(tpmData.permanent.data.tpmProof.nonce));
   2.387 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_startup.c tpm_emulator-0.3-x86_64/tpm/tpm_startup.c
   2.388 ---- orig/tpm_emulator-0.3/tpm/tpm_startup.c	2006-01-10 04:21:45.000000000 -0800
   2.389 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_startup.c	2006-08-29 15:08:20.538341856 -0700
   2.390 -@@ -41,24 +41,29 @@ void TPM_Init(TPM_STARTUP_TYPE startupTy
   2.391 - TPM_RESULT TPM_Startup(TPM_STARTUP_TYPE startupType)
   2.392 - {
   2.393 -   int i;
   2.394 -+  int restore_fail;
   2.395 -   info("TPM_Startup(%d)", startupType);
   2.396 -   if (tpmData.stany.flags.postInitialise == FALSE) return TPM_INVALID_POSTINIT;
   2.397 --  /* reset STANY_FLAGS */
   2.398 --  SET_TO_ZERO(&tpmData.stany.flags);
   2.399 --  tpmData.stany.flags.tag = TPM_TAG_STANY_FLAGS;
   2.400 --  /* reset STANY_DATA (invalidates ALL sessions) */
   2.401 --  SET_TO_ZERO(&tpmData.stany.data);
   2.402 --  tpmData.stany.data.tag = TPM_TAG_STANY_DATA;
   2.403 --  /* init session-context nonce */
   2.404 --  SET_TO_RAND(&tpmData.stany.data.contextNonceSession);
   2.405 -+
   2.406 -+  /* try and restore state to get EK, SRK, etc */
   2.407 -+  restore_fail = tpm_restore_permanent_data();
   2.408 -+
   2.409 -   /* set data and flags according to the given startup type */
   2.410 -   if (startupType == TPM_ST_CLEAR) {
   2.411 -+    /* reset STANY_FLAGS */
   2.412 -+    SET_TO_ZERO(&tpmData.stany.flags);
   2.413 -+    tpmData.stany.flags.tag = TPM_TAG_STANY_FLAGS;
   2.414 -+    /* reset STANY_DATA (invalidates ALL sessions) */
   2.415 -+    SET_TO_ZERO(&tpmData.stany.data);
   2.416 -+    tpmData.stany.data.tag = TPM_TAG_STANY_DATA;
   2.417 -+    /* init session-context nonce */
   2.418 -+    SET_TO_RAND(&tpmData.stany.data.contextNonceSession);
   2.419 -     /* reset PCR values */
   2.420 -     for (i = 0; i < TPM_NUM_PCR; i++) {
   2.421 --      if (tpmData.permanent.data.pcrAttrib[i].pcrReset)
   2.422 --        SET_TO_ZERO(tpmData.permanent.data.pcrValue[i].digest);
   2.423 -+      if (!tpmData.permanent.data.pcrAttrib[i].pcrReset)
   2.424 -+        SET_TO_ZERO(&tpmData.permanent.data.pcrValue[i].digest);
   2.425 -       else
   2.426 --        SET_TO_0xFF(tpmData.permanent.data.pcrValue[i].digest);
   2.427 -+        SET_TO_0xFF(&tpmData.permanent.data.pcrValue[i].digest);
   2.428 -     }
   2.429 -     /* reset STCLEAR_FLAGS */
   2.430 -     SET_TO_ZERO(&tpmData.stclear.flags);
   2.431 -@@ -77,7 +82,8 @@ TPM_RESULT TPM_Startup(TPM_STARTUP_TYPE 
   2.432 -     /* init key-context nonce */
   2.433 -     SET_TO_RAND(&tpmData.stclear.data.contextNonceKey);
   2.434 -   } else if (startupType == TPM_ST_STATE) {
   2.435 --    if (tpm_restore_permanent_data()) {
   2.436 -+    /* restore must have been successful for TPM_ST_STATE */
   2.437 -+    if (restore_fail) {
   2.438 -       error("restoring permanent data failed");
   2.439 -       tpmData.permanent.data.testResult = "tpm_restore_permanent_data() failed";
   2.440 -       tpmData.permanent.flags.selfTestSucceeded = FALSE;
   2.441 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_storage.c tpm_emulator-0.3-x86_64/tpm/tpm_storage.c
   2.442 ---- orig/tpm_emulator-0.3/tpm/tpm_storage.c	2006-01-10 04:21:45.000000000 -0800
   2.443 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_storage.c	2006-08-29 15:07:43.537966768 -0700
   2.444 -@@ -58,6 +58,7 @@ int encrypt_sealed_data(TPM_KEY_DATA *ke
   2.445 -                         BYTE *enc, UINT32 *enc_size)
   2.446 - {
   2.447 -   UINT32 len;
   2.448 -+  size_t enc_size32 = *enc_size;
   2.449 -   BYTE *buf, *ptr;
   2.450 -   rsa_public_key_t pub_key;
   2.451 -   int scheme;
   2.452 -@@ -72,7 +73,7 @@ int encrypt_sealed_data(TPM_KEY_DATA *ke
   2.453 -   if (buf == NULL
   2.454 -       || tpm_marshal_TPM_SEALED_DATA(&ptr, &len, seal)
   2.455 -       || rsa_encrypt(&pub_key, scheme, buf, sizeof_TPM_SEALED_DATA((*seal)),
   2.456 --                     enc, enc_size)) {
   2.457 -+                     enc, &enc_size32)) {
   2.458 -     tpm_free(buf);
   2.459 -     rsa_release_public_key(&pub_key);
   2.460 -     return -1;
   2.461 -@@ -85,7 +86,8 @@ int encrypt_sealed_data(TPM_KEY_DATA *ke
   2.462 - int decrypt_sealed_data(TPM_KEY_DATA *key, BYTE *enc, UINT32 enc_size,
   2.463 -                         TPM_SEALED_DATA *seal, BYTE **buf) 
   2.464 - {
   2.465 --  UINT32 len;
   2.466 -+  size_t len;
   2.467 -+  UINT32 len32;
   2.468 -   BYTE *ptr;
   2.469 -   int scheme;
   2.470 -   switch (key->encScheme) {
   2.471 -@@ -96,8 +98,12 @@ int decrypt_sealed_data(TPM_KEY_DATA *ke
   2.472 -   len = enc_size;
   2.473 -   *buf = ptr = tpm_malloc(len);
   2.474 -   if (*buf == NULL
   2.475 --      || rsa_decrypt(&key->key, scheme, enc, enc_size, *buf, &len)
   2.476 --      || tpm_unmarshal_TPM_SEALED_DATA(&ptr, &len, seal)) {
   2.477 -+      || rsa_decrypt(&key->key, scheme, enc, enc_size, *buf, &len) ){
   2.478 -+    tpm_free(*buf);
   2.479 -+    return -1;
   2.480 -+  }
   2.481 -+  len32 = len;
   2.482 -+  if (tpm_unmarshal_TPM_SEALED_DATA(&ptr, &len32, seal)) {
   2.483 -     tpm_free(*buf);
   2.484 -     return -1;
   2.485 -   }
   2.486 -@@ -237,11 +243,12 @@ TPM_RESULT TPM_Unseal(TPM_KEY_HANDLE par
   2.487 - 
   2.488 - TPM_RESULT TPM_UnBind(TPM_KEY_HANDLE keyHandle, UINT32 inDataSize,
   2.489 -                       BYTE *inData, TPM_AUTH *auth1, 
   2.490 --                      UINT32 *outDataSize, BYTE **outData)
   2.491 -+                      UINT32 *outDataSize32, BYTE **outData)
   2.492 - {
   2.493 -   TPM_RESULT res;
   2.494 -   TPM_KEY_DATA *key;
   2.495 -   int scheme;
   2.496 -+  size_t outDataSize;
   2.497 -   info("TPM_UnBind()");
   2.498 -   /* get key */
   2.499 -   key = tpm_get_key(keyHandle);
   2.500 -@@ -258,8 +265,8 @@ TPM_RESULT TPM_UnBind(TPM_KEY_HANDLE key
   2.501 -   /* the size of the input data muss be greater than zero */
   2.502 -   if (inDataSize == 0) return TPM_BAD_PARAMETER;
   2.503 -   /* decrypt data */
   2.504 --  *outDataSize = inDataSize;
   2.505 --  *outData = tpm_malloc(*outDataSize);
   2.506 -+  outDataSize = inDataSize;
   2.507 -+  *outData = tpm_malloc(outDataSize);
   2.508 -   if (*outData == NULL) return TPM_FAIL;
   2.509 -   switch (key->encScheme) {
   2.510 -     case TPM_ES_RSAESOAEP_SHA1_MGF1: scheme = RSA_ES_OAEP_SHA1; break;
   2.511 -@@ -267,20 +274,21 @@ TPM_RESULT TPM_UnBind(TPM_KEY_HANDLE key
   2.512 -     default: tpm_free(*outData); return TPM_DECRYPT_ERROR;
   2.513 -   }
   2.514 -   if (rsa_decrypt(&key->key, scheme, inData, inDataSize, 
   2.515 --      *outData, outDataSize)) {
   2.516 -+      *outData, &outDataSize)) {
   2.517 -     tpm_free(*outData);
   2.518 -     return TPM_DECRYPT_ERROR;
   2.519 -   }
   2.520 -   /* verify data if it is of type TPM_BOUND_DATA */
   2.521 -   if (key->encScheme == TPM_ES_RSAESOAEP_SHA1_MGF1 
   2.522 -       || key->keyUsage != TPM_KEY_LEGACY) {
   2.523 --    if (*outDataSize < 5 || memcmp(*outData, "\x01\x01\00\x00\x02", 5) != 0) {
   2.524 -+    if (outDataSize < 5 || memcmp(*outData, "\x01\x01\00\x00\x02", 5) != 0) {
   2.525 -       tpm_free(*outData);
   2.526 -       return TPM_DECRYPT_ERROR;
   2.527 -     }
   2.528 --    *outDataSize -= 5;
   2.529 --    memmove(*outData, &(*outData)[5], *outDataSize);
   2.530 -+    outDataSize -= 5;
   2.531 -+    memmove(*outData, &(*outData)[5], outDataSize);
   2.532 -   }
   2.533 -+  *outDataSize32 = (UINT32) outDataSize;
   2.534 -   return TPM_SUCCESS;
   2.535 - }
   2.536 - 
   2.537 -@@ -311,12 +319,13 @@ static int verify_key_digest(TPM_KEY *ke
   2.538 - }
   2.539 - 
   2.540 - int encrypt_private_key(TPM_KEY_DATA *key, TPM_STORE_ASYMKEY *store,
   2.541 --                        BYTE *enc, UINT32 *enc_size)
   2.542 -+                        BYTE *enc, UINT32 *enc_size32)
   2.543 - {
   2.544 -   UINT32 len;
   2.545 -   BYTE *buf, *ptr;
   2.546 -   rsa_public_key_t pub_key;
   2.547 -   int scheme;
   2.548 -+  size_t enc_size;
   2.549 -   switch (key->encScheme) {
   2.550 -     case TPM_ES_RSAESOAEP_SHA1_MGF1: scheme = RSA_ES_OAEP_SHA1; break;
   2.551 -     case TPM_ES_RSAESPKCSv15: scheme = RSA_ES_PKCSV15; break;
   2.552 -@@ -328,11 +337,12 @@ int encrypt_private_key(TPM_KEY_DATA *ke
   2.553 -   if (buf == NULL
   2.554 -       || tpm_marshal_TPM_STORE_ASYMKEY(&ptr, &len, store)
   2.555 -       || rsa_encrypt(&pub_key, scheme, buf, sizeof_TPM_STORE_ASYMKEY((*store)),
   2.556 --                     enc, enc_size)) {
   2.557 -+                     enc, &enc_size)) {
   2.558 -     tpm_free(buf);
   2.559 -     rsa_release_public_key(&pub_key);
   2.560 -     return -1;
   2.561 -   }
   2.562 -+  *enc_size32 = (UINT32) enc_size;
   2.563 -   tpm_free(buf);
   2.564 -   rsa_release_public_key(&pub_key);
   2.565 -   return 0;
   2.566 -@@ -341,7 +351,8 @@ int encrypt_private_key(TPM_KEY_DATA *ke
   2.567 - int decrypt_private_key(TPM_KEY_DATA *key, BYTE *enc, UINT32 enc_size, 
   2.568 -                         TPM_STORE_ASYMKEY *store, BYTE **buf) 
   2.569 - {
   2.570 --  UINT32 len;
   2.571 -+  UINT32 len32;
   2.572 -+  size_t len;
   2.573 -   BYTE *ptr;
   2.574 -   int scheme;
   2.575 -   switch (key->encScheme) {
   2.576 -@@ -352,8 +363,12 @@ int decrypt_private_key(TPM_KEY_DATA *ke
   2.577 -   len = enc_size;
   2.578 -   *buf = ptr = tpm_malloc(len);
   2.579 -   if (*buf == NULL
   2.580 --      || rsa_decrypt(&key->key, scheme, enc, enc_size, *buf, &len)
   2.581 --      || tpm_unmarshal_TPM_STORE_ASYMKEY(&ptr, &len, store)) {
   2.582 -+      || rsa_decrypt(&key->key, scheme, enc, enc_size, *buf, &len) ) {
   2.583 -+    tpm_free(*buf);
   2.584 -+    return -1;
   2.585 -+  }
   2.586 -+  len32 = (UINT32) len;
   2.587 -+  if (tpm_unmarshal_TPM_STORE_ASYMKEY(&ptr, &len32, store)) {  
   2.588 -     tpm_free(*buf);
   2.589 -     return -1;
   2.590 -   }
   2.591 -@@ -371,7 +386,7 @@ TPM_RESULT TPM_CreateWrapKey(TPM_KEY_HAN
   2.592 -   TPM_SESSION_DATA *session;
   2.593 -   TPM_STORE_ASYMKEY store;
   2.594 -   rsa_private_key_t rsa;
   2.595 --  UINT32 key_length;
   2.596 -+  size_t key_length;
   2.597 - 
   2.598 -   info("TPM_CreateWrapKey()");
   2.599 -   /* get parent key */
   2.600 -@@ -428,11 +443,11 @@ TPM_RESULT TPM_CreateWrapKey(TPM_KEY_HAN
   2.601 -   }
   2.602 -   if (compute_key_digest(wrappedKey, &store.pubDataDigest)) return TPM_FAIL;
   2.603 -   /* generate key and store it */
   2.604 --  key_length = keyInfo->algorithmParms.parms.rsa.keyLength;
   2.605 --  if (rsa_generate_key(&rsa, key_length)) return TPM_FAIL;
   2.606 --  wrappedKey->pubKey.keyLength = key_length >> 3;
   2.607 -+  if (rsa_generate_key(&rsa, keyInfo->algorithmParms.parms.rsa.keyLength)) 
   2.608 -+    return TPM_FAIL;
   2.609 -+  wrappedKey->pubKey.keyLength = keyInfo->algorithmParms.parms.rsa.keyLength >> 3;
   2.610 -   wrappedKey->pubKey.key = tpm_malloc(wrappedKey->pubKey.keyLength);
   2.611 --  store.privKey.keyLength = key_length >> 4;
   2.612 -+  store.privKey.keyLength = keyInfo->algorithmParms.parms.rsa.keyLength >> 4;
   2.613 -   store.privKey.key = tpm_malloc(store.privKey.keyLength);
   2.614 -   wrappedKey->encDataSize = parent->key.size >> 3;
   2.615 -   wrappedKey->encData = tpm_malloc(wrappedKey->encDataSize);
   2.616 -@@ -444,9 +459,11 @@ TPM_RESULT TPM_CreateWrapKey(TPM_KEY_HAN
   2.617 -     tpm_free(wrappedKey->encData);
   2.618 -     return TPM_FAIL;
   2.619 -   }
   2.620 --  rsa_export_modulus(&rsa, wrappedKey->pubKey.key, 
   2.621 --    &wrappedKey->pubKey.keyLength);
   2.622 --  rsa_export_prime1(&rsa, store.privKey.key, &store.privKey.keyLength);
   2.623 -+  rsa_export_modulus(&rsa, wrappedKey->pubKey.key,
   2.624 -+		     &key_length);
   2.625 -+  wrappedKey->pubKey.keyLength = (UINT32) key_length;
   2.626 -+  rsa_export_prime1(&rsa, store.privKey.key, &key_length);
   2.627 -+  store.privKey.keyLength = (UINT32) key_length;
   2.628 -   rsa_release_private_key(&rsa);
   2.629 -   /* encrypt private key data */
   2.630 -   if (encrypt_private_key(parent, &store, wrappedKey->encData, 
   2.631 -@@ -567,6 +584,7 @@ TPM_RESULT TPM_LoadKey2(TPM_KEY_HANDLE p
   2.632 - 
   2.633 - int tpm_setup_key_parms(TPM_KEY_DATA *key, TPM_KEY_PARMS *parms)
   2.634 - {
   2.635 -+  size_t key_length;
   2.636 -   parms->algorithmID = TPM_ALG_RSA;
   2.637 -   parms->encScheme = key->encScheme;
   2.638 -   parms->sigScheme = key->sigScheme;
   2.639 -@@ -576,7 +594,8 @@ int tpm_setup_key_parms(TPM_KEY_DATA *ke
   2.640 -   parms->parms.rsa.exponent = tpm_malloc(parms->parms.rsa.exponentSize);
   2.641 -   if (parms->parms.rsa.exponent == NULL) return -1;
   2.642 -   rsa_export_exponent(&key->key, parms->parms.rsa.exponent,
   2.643 --    &parms->parms.rsa.exponentSize);
   2.644 -+    &key_length);
   2.645 -+  parms->parms.rsa.exponentSize = (UINT32) key_length;
   2.646 -   parms->parmSize = 12 + parms->parms.rsa.exponentSize;
   2.647 -   return 0;
   2.648 - }
   2.649 -@@ -587,6 +606,7 @@ TPM_RESULT TPM_GetPubKey(TPM_KEY_HANDLE 
   2.650 -   TPM_RESULT res;
   2.651 -   TPM_KEY_DATA *key;
   2.652 -   TPM_DIGEST digest;
   2.653 -+  size_t key_length;
   2.654 -   info("TPM_GetPubKey()");
   2.655 -   /* get key */
   2.656 -   if (keyHandle == TPM_KH_SRK) return TPM_BAD_PARAMETER;
   2.657 -@@ -614,8 +634,8 @@ TPM_RESULT TPM_GetPubKey(TPM_KEY_HANDLE 
   2.658 -   pubKey->pubKey.keyLength = key->key.size >> 3;
   2.659 -   pubKey->pubKey.key = tpm_malloc(pubKey->pubKey.keyLength);
   2.660 -   if (pubKey->pubKey.key == NULL) return TPM_FAIL;
   2.661 --  rsa_export_modulus(&key->key, pubKey->pubKey.key, 
   2.662 --    &pubKey->pubKey.keyLength);
   2.663 -+  rsa_export_modulus(&key->key, pubKey->pubKey.key, &key_length); 
   2.664 -+  pubKey->pubKey.keyLength = (UINT32) key_length;
   2.665 -   if (tpm_setup_key_parms(key, &pubKey->algorithmParms) != 0) {
   2.666 -     tpm_free(pubKey->pubKey.key);
   2.667 -     return TPM_FAIL;
   2.668 -diff -uprN orig/tpm_emulator-0.3/tpm/tpm_structures.h tpm_emulator-0.3-x86_64/tpm/tpm_structures.h
   2.669 ---- orig/tpm_emulator-0.3/tpm/tpm_structures.h	2006-01-10 04:21:45.000000000 -0800
   2.670 -+++ tpm_emulator-0.3-x86_64/tpm/tpm_structures.h	2006-08-29 15:08:20.545340792 -0700
   2.671 -@@ -1723,6 +1723,7 @@ typedef struct tdTPM_DAA_ISSUER {
   2.672 -   TPM_DIGEST DAA_digest_gamma;
   2.673 -   BYTE DAA_generic_q[26];
   2.674 - } TPM_DAA_ISSUER;
   2.675 -+#define sizeof_TPM_DAA_ISSUER(s) (2 + (20 * 6) + 26 )
   2.676 - 
   2.677 - /*
   2.678 -  * TPM_DAA_TPM ([TPM_Part2], Section 22.8)
   2.679 -@@ -1738,6 +1739,7 @@ typedef struct tdTPM_DAA_TPM {
   2.680 -   TPM_DIGEST DAA_rekey;
   2.681 -   UINT32 DAA_count;
   2.682 - } TPM_DAA_TPM;
   2.683 -+#define sizeof_TPM_DAA_TPM(s) (2 + (4 * 20) + 4)
   2.684 - 
   2.685 - /*
   2.686 -  * TPM_DAA_CONTEXT ([TPM_Part2], Section 22.9)
   2.687 -@@ -1752,6 +1754,7 @@ typedef struct tdTPM_DAA_CONTEXT {
   2.688 -   BYTE DAA_scratch[256];
   2.689 -   BYTE DAA_stage;
   2.690 - } TPM_DAA_CONTEXT;
   2.691 -+#define sizeof_TPM_DAA_CONTEXT(s) (2 + (3 * 20) + 256 + 1)
   2.692 - 
   2.693 - /*
   2.694 -  * TPM_DAA_JOINDATA ([TPM_Part2], Section 22.10)
   2.695 -@@ -1763,6 +1766,7 @@ typedef struct tdTPM_DAA_JOINDATA {
   2.696 -   BYTE DAA_join_u1[138]; /* WATCH: 138 (v1.2 rev 85) */
   2.697 -   TPM_DIGEST DAA_digest_n0;
   2.698 - } TPM_DAA_JOINDATA;
   2.699 -+#define sizeof_TPM_DAA_JOINDATA(s) (1 + 1 + 20)
   2.700 - 
   2.701 - /*
   2.702 -  * TPM_DAA_BLOB ([TPM_Part2], Section 22.12)
   2.703 -@@ -2033,6 +2037,7 @@ typedef struct tdTPM_STCLEAR_DATA {
   2.704 -   TPM_COUNT_ID countID;
   2.705 -   //UINT32 ownerReference;
   2.706 - } TPM_STCLEAR_DATA;
   2.707 -+#define sizeof_TPM_STCLEAR_DATA(s) (2 + 20 + 4)
   2.708 - 
   2.709 - /*
   2.710 -  * TPM_SESSION_DATA
   2.711 -@@ -2069,6 +2074,11 @@ typedef struct tdTPM_DAA_SESSION_DATA {
   2.712 -   TPM_DAA_JOINDATA DAA_joinSession;
   2.713 -   TPM_HANDLE handle;
   2.714 - } TPM_DAA_SESSION_DATA;
   2.715 -+#define sizeof_TPM_DAA_SESSION_DATA(s) ( 1 \
   2.716 -+  + sizeof_TPM_DAA_ISSUER(s.DAA_issuerSettings) \
   2.717 -+  + sizeof_TPM_DAA_TPM(s.DAA_tpmSpecific) \
   2.718 -+  + sizeof_TPM_DAA_CONTEXT(s.DAA_session) \
   2.719 -+  + sizeof_TPM_DAA_JOINDATA(s.DAA_joinSession) + 4)
   2.720 - 
   2.721 - /*
   2.722 -  * TPM_STANY_DATA ([TPM_Part2], Section 7.6)
   2.723 -@@ -2095,6 +2105,17 @@ typedef struct tdTPM_STANY_DATA {
   2.724 -   TPM_DAA_SESSION_DATA sessionsDAA[TPM_MAX_SESSIONS_DAA];
   2.725 -   TPM_TRANSHANDLE transExclusive;
   2.726 - } TPM_STANY_DATA;
   2.727 -+#define sizeof_TPM_STANY_DATA(s) (2 + 20 + 20 + 1 \
   2.728 -+  + sizeof_TPM_CURRENT_TICKS(s.currentTicks) \
   2.729 -+  + 4 + (4 * TPM_MAX_SESSION_LIST) \
   2.730 -+  + (sizeof_TPM_SESSION_DATA(s.sessions[0]) * TPM_MAX_SESSION_LIST) \
   2.731 -+  + (sizeof_TPM_DAA_SESSION_DATA(s.sessionsDAA[0]) * TPM_MAX_SESSIONS_DAA) + 4)
   2.732 -+
   2.733 -+#define sizeof_TPM_PERMANENT_DATA(s) (2 + 4 + 4*20 \
   2.734 -+  + sizeof_RSA(s.endorsementKey) + TPM_ORD_MAX/8 \
   2.735 -+  + (1+TPM_MAX_KEYS)*sizeof_TPM_KEY_DATA(s.srk) \
   2.736 -+  + TPM_NUM_PCR*(sizeof_TPM_PCR_ATTRIBUTES(x)+20) \
   2.737 -+  + TPM_MAX_COUNTERS*sizeof_TPM_COUNTER_VALUE2(x) + 1 + 4 + 20)
   2.738 - 
   2.739 - /*
   2.740 -  * TPM_DATA
     3.1 --- a/tools/vtpm/tpm_emulator.patch	Tue Jul 25 11:19:48 2006 +0100
     3.2 +++ b/tools/vtpm/tpm_emulator.patch	Tue Jul 25 11:30:30 2006 +0100
     3.3 @@ -1,136 +1,23 @@
     3.4 -diff -uprN tpm_emulator-0.3-x86_64/AUTHORS tpm_emulator/AUTHORS
     3.5 ---- tpm_emulator-0.3-x86_64/AUTHORS	2006-08-29 15:07:21.618299064 -0700
     3.6 -+++ tpm_emulator/AUTHORS	2006-08-29 15:26:17.099679656 -0700
     3.7 +diff -uprN orig/tpm_emulator-0.4/AUTHORS tpm_emulator/AUTHORS
     3.8 +--- orig/tpm_emulator-0.4/AUTHORS	2006-06-23 03:37:07.000000000 -0700
     3.9 ++++ tpm_emulator/AUTHORS	2006-07-24 14:35:35.000000000 -0700
    3.10  @@ -1,2 +1,3 @@
    3.11   Mario Strasser <mast@gmx.net>
    3.12   Heiko Stamer <stamer@gaos.org> [DAA]
    3.13  +INTEL Corp <> [Dropped to Ring3]
    3.14 -diff -uprN tpm_emulator-0.3-x86_64/ChangeLog tpm_emulator/ChangeLog
    3.15 ---- tpm_emulator-0.3-x86_64/ChangeLog	2006-08-29 15:07:21.618299064 -0700
    3.16 -+++ tpm_emulator/ChangeLog	2006-08-29 15:26:17.100679504 -0700
    3.17 +diff -uprN orig/tpm_emulator-0.4/ChangeLog tpm_emulator/ChangeLog
    3.18 +--- orig/tpm_emulator-0.4/ChangeLog	2006-06-23 03:37:07.000000000 -0700
    3.19 ++++ tpm_emulator/ChangeLog	2006-07-24 14:35:35.000000000 -0700
    3.20  @@ -1,3 +1,6 @@
    3.21 -+2005-08-16 Intel Corp
    3.22 ++????-??-?? Intel Corp
    3.23  +	* Moved module out of kernel to run as a ring 3 app
    3.24  +
    3.25 - 2005-12-24  Mario Strasser <mast@gmx.net>
    3.26 - 	* tpm_transport.c, tpm_marshalling.c, tpm_structures.h:
    3.27 - 		Transport session functionality added
    3.28 -diff -uprN tpm_emulator-0.3-x86_64/Makefile tpm_emulator/Makefile
    3.29 ---- tpm_emulator-0.3-x86_64/Makefile	2006-08-29 15:08:20.532342768 -0700
    3.30 -+++ tpm_emulator/Makefile	2006-08-29 15:27:39.559143912 -0700
    3.31 -@@ -1,22 +1,31 @@
    3.32 - # Software-Based Trusted Platform Module (TPM) Emulator for Linux
    3.33 - # Copyright (C) 2004 Mario Strasser <mast@gmx.net>
    3.34 -+# Copyright (C) 2006 INTEL Corp.
    3.35 - #
    3.36 - # $Id: Makefile 69 2005-12-13 12:55:52Z mast $
    3.37 - 
    3.38 --# kernel settings
    3.39 --KERNEL_RELEASE := $(shell uname -r)
    3.40 --KERNEL_BUILD   := /lib/modules/$(KERNEL_RELEASE)/build
    3.41 --MOD_SUBDIR     := misc
    3.42 - COMPILE_ARCH    ?= $(shell uname -m | sed -e s/i.86/x86_32/)
    3.43 - 
    3.44 - # module settings
    3.45 --MODULE_NAME    := tpm_emulator
    3.46 -+BIN            := tpm_emulator
    3.47 - VERSION_MAJOR  := 0
    3.48 - VERSION_MINOR  := 3
    3.49 - VERSION_BUILD  := $(shell date +"%s")
    3.50 - 
    3.51 --# enable/disable DEBUG messages
    3.52 --EXTRA_CFLAGS   += -Wall -DDEBUG -g  
    3.53 -+# Installation program and options
    3.54 -+INSTALL         = install
    3.55 -+INSTALL_PROG    = $(INSTALL) -m0755
    3.56 -+INSTALL_DIR     = $(INSTALL) -d -m0755
    3.57 -+
    3.58 -+# Xen tools installation directory
    3.59 -+TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
    3.60 -+
    3.61 -+CC      := gcc
    3.62 -+CFLAGS  += -g -Wall $(INCLUDE) -DDEBUG
    3.63 -+CFLAGS  += -I. -Itpm
    3.64 -+
    3.65 -+# Is the simulator running in it's own vm?
    3.66 -+#CFLAGS += -DVTPM_MULTI_VM
    3.67 - 
    3.68 - ifeq ($(COMPILE_ARCH),x86_64)
    3.69 - LIBDIR = lib64
    3.70 -@@ -34,38 +43,31 @@ DIRS           := . crypto tpm 
    3.71 - SRCS           := $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.c))
    3.72 - OBJS           := $(patsubst %.c, %.o, $(SRCS))
    3.73 - SRCS           += $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.h))
    3.74 --DISTSRC        := ./README ./AUTHORS ./ChangeLog ./Makefile $(SRCS)
    3.75 --DISTDIR        := tpm_emulator-$(VERSION_MAJOR).$(VERSION_MINOR)
    3.76 - 
    3.77 --obj-m               := $(MODULE_NAME).o
    3.78 --$(MODULE_NAME)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
    3.79 -+obj-m               := $(BIN)
    3.80 -+$(BIN)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
    3.81 - 
    3.82 - EXTRA_CFLAGS   += -I$(src) -I$(src)/crypto -I$(src)/tpm 
    3.83 - 
    3.84 - # do not print "Entering directory ..."
    3.85 - MAKEFLAGS      += --no-print-directory
    3.86 - 
    3.87 --all:	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version
    3.88 --	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules
    3.89 -+all: $(BIN)
    3.90 -+
    3.91 -+$(BIN):	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version $(SRCS) $(OBJS)
    3.92 -+	$(CC) $(CFLAGS) $(OBJS) $(src)/crypto/libgmp.a -o $(BIN)
    3.93 -+
    3.94 -+%.o: %.c
    3.95 -+	$(CC) $(CFLAGS) -c $< -o $@
    3.96 - 
    3.97 --install:
    3.98 --	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules_install
    3.99 --	test -d /var/tpm || mkdir /var/tpm
   3.100 --	test -c /dev/tpm || mknod /dev/tpm c 10 224
   3.101 --	chmod 666 /dev/tpm
   3.102 --	depmod -a
   3.103 -+install: $(BIN)
   3.104 -+	$(INSTALL_PROG) $(BIN) $(TOOLS_INSTALL_DIR)
   3.105 - 
   3.106 - clean:
   3.107 --	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) clean
   3.108 --	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a
   3.109 -+	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a $(OBJS)
   3.110 - 
   3.111 --dist:	$(DISTSRC)
   3.112 --	rm -rf $(DISTDIR)
   3.113 --	mkdir $(DISTDIR)
   3.114 --	cp --parents $(DISTSRC) $(DISTDIR)/
   3.115 --	rm -f $(DISTDIR)/crypto/gmp.h 
   3.116 --	tar -chzf $(DISTDIR).tar.gz $(DISTDIR)
   3.117 --	rm -rf $(DISTDIR)
   3.118 -+mrproper: clean
   3.119 -+	rm -f $(BIN) tpm_version.h
   3.120 - 
   3.121 - $(src)/crypto/libgmp.a:
   3.122 - 	test -f $(src)/crypto/libgmp.a || ln -s $(GMP_LIB) $(src)/crypto/libgmp.a
   3.123 -diff -uprN tpm_emulator-0.3-x86_64/README tpm_emulator/README
   3.124 ---- tpm_emulator-0.3-x86_64/README	2006-08-29 15:07:43.530967832 -0700
   3.125 -+++ tpm_emulator/README	2006-08-29 15:26:17.105678744 -0700
   3.126 -@@ -13,7 +13,8 @@ $Id: README 78 2006-01-07 10:45:39Z mast
   3.127 - Copyright
   3.128 - --------------------------------------------------------------------------
   3.129 - Copyright (C) 2004 Mario Strasser <mast@gmx.net> and Swiss Federal 
   3.130 --Institute of Technology (ETH) Zurich.
   3.131 -+                   Institute of Technology (ETH) Zurich.
   3.132 -+Copyright (C) 2005 INTEL Corp 
   3.133 -               
   3.134 - This program is free software; you can redistribute it and/or modify
   3.135 - it under the terms of the GNU General Public License as published by
   3.136 -diff -uprN tpm_emulator-0.3-x86_64/README.1st tpm_emulator/README.1st
   3.137 ---- tpm_emulator-0.3-x86_64/README.1st	1969-12-31 16:00:00.000000000 -0800
   3.138 -+++ tpm_emulator/README.1st	2006-08-29 15:26:17.105678744 -0700
   3.139 -@@ -0,0 +1 @@
   3.140 -+Note that you must manually create /tmp/tpm_in.fifo and /tmp/tpm_out.fifo for this emulator to work.
   3.141 -diff -uprN tpm_emulator-0.3-x86_64/crypto/gmp_kernel_wrapper.c tpm_emulator/crypto/gmp_kernel_wrapper.c
   3.142 ---- tpm_emulator-0.3-x86_64/crypto/gmp_kernel_wrapper.c	2006-08-29 15:07:43.525968592 -0700
   3.143 -+++ tpm_emulator/crypto/gmp_kernel_wrapper.c	2006-08-29 15:26:17.101679352 -0700
   3.144 + 2006-06-23  Mario Strasser <mast@gmx.net>
   3.145 + 	* tpm_startup.c: behaviour of ST_CLEAR and storage of
   3.146 + 		persistent data adapted
   3.147 +diff -uprN orig/tpm_emulator-0.4/crypto/gmp_kernel_wrapper.c tpm_emulator/crypto/gmp_kernel_wrapper.c
   3.148 +--- orig/tpm_emulator-0.4/crypto/gmp_kernel_wrapper.c	2006-06-23 03:37:07.000000000 -0700
   3.149 ++++ tpm_emulator/crypto/gmp_kernel_wrapper.c	2006-07-24 14:35:35.000000000 -0700
   3.150  @@ -1,5 +1,6 @@
   3.151   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.152    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.153 @@ -161,7 +48,7 @@ diff -uprN tpm_emulator-0.3-x86_64/crypt
   3.154   {
   3.155  -  void *ret  = (void*)kmalloc(size, GFP_KERNEL);
   3.156  -  if (!ret) panic(KERN_CRIT TPM_MODULE_NAME 
   3.157 --    "GMP: cannot allocate memory (size=%Zu)\n", size);
   3.158 +-    "GMP: cannot allocate memory (size=%u)\n", size);
   3.159  +  void *ret  = (void*)malloc(size);
   3.160  +  if (!ret) error("GMP: cannot allocate memory (size=%Zu)\n", size);
   3.161     return ret;
   3.162 @@ -172,9 +59,10 @@ diff -uprN tpm_emulator-0.3-x86_64/crypt
   3.163   {
   3.164  -  void *ret = (void*)kmalloc(new_size, GFP_KERNEL);
   3.165  -  if (!ret) panic(KERN_CRIT TPM_MODULE_NAME "GMP: Cannot reallocate memory "
   3.166 +-    "(old_size=%u new_size=%u)\n", old_size, new_size);
   3.167  +  void *ret = (void*)malloc(new_size);
   3.168  +  if (!ret) error("GMP: Cannot reallocate memory "
   3.169 -     "(old_size=%Zu new_size=%Zu)\n", old_size, new_size);
   3.170 ++    "(old_size=%Zu new_size=%Zu)\n", old_size, new_size);
   3.171     memcpy(ret, oldptr, old_size);
   3.172  -  kfree(oldptr);
   3.173  +  free(oldptr);
   3.174 @@ -190,9 +78,9 @@ diff -uprN tpm_emulator-0.3-x86_64/crypt
   3.175     }
   3.176   }
   3.177   
   3.178 -diff -uprN tpm_emulator-0.3-x86_64/crypto/rsa.c tpm_emulator/crypto/rsa.c
   3.179 ---- tpm_emulator-0.3-x86_64/crypto/rsa.c	2006-08-29 15:07:21.618299064 -0700
   3.180 -+++ tpm_emulator/crypto/rsa.c	2006-08-29 15:26:17.102679200 -0700
   3.181 +diff -uprN orig/tpm_emulator-0.4/crypto/rsa.c tpm_emulator/crypto/rsa.c
   3.182 +--- orig/tpm_emulator-0.4/crypto/rsa.c	2006-06-23 03:37:07.000000000 -0700
   3.183 ++++ tpm_emulator/crypto/rsa.c	2006-07-24 14:35:35.000000000 -0700
   3.184  @@ -1,5 +1,6 @@
   3.185   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.186    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.187 @@ -218,10 +106,10 @@ diff -uprN tpm_emulator-0.3-x86_64/crypt
   3.188         sha1_final(&ctx, &msg[1]);
   3.189         if (memcmp(&msg[1], &msg[1 + SHA1_DIGEST_LENGTH], 
   3.190             SHA1_DIGEST_LENGTH) != 0) return -1;
   3.191 -diff -uprN tpm_emulator-0.3-x86_64/linux_module.c tpm_emulator/linux_module.c
   3.192 ---- tpm_emulator-0.3-x86_64/linux_module.c	2006-08-29 15:07:43.526968440 -0700
   3.193 +diff -uprN orig/tpm_emulator-0.4/linux_module.c tpm_emulator/linux_module.c
   3.194 +--- orig/tpm_emulator-0.4/linux_module.c	2006-06-23 03:37:07.000000000 -0700
   3.195  +++ tpm_emulator/linux_module.c	1969-12-31 16:00:00.000000000 -0800
   3.196 -@@ -1,194 +0,0 @@
   3.197 +@@ -1,195 +0,0 @@
   3.198  -/* Software-Based Trusted Platform Module (TPM) Emulator for Linux 
   3.199  - * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.200  - *
   3.201 @@ -235,7 +123,7 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.202  - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
   3.203  - * GNU General Public License for more details.
   3.204  - *
   3.205 -- * $Id: linux_module.c 76 2006-01-02 22:17:58Z hstamer $
   3.206 +- * $Id: linux_module.c 91 2006-03-13 13:51:41Z mast $
   3.207  - */
   3.208  -
   3.209  -#include <linux/module.h>
   3.210 @@ -273,6 +161,7 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.211  -/* module state */
   3.212  -#define STATE_IS_OPEN 0
   3.213  -static uint32_t module_state;
   3.214 +-static struct timespec old_time;
   3.215  -
   3.216  -static int tpm_open(struct inode *inode, struct file *file)
   3.217  -{
   3.218 @@ -296,7 +185,7 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.219  -
   3.220  -static ssize_t tpm_read(struct file *file, char *buf, size_t count, loff_t *ppos)
   3.221  -{
   3.222 --  debug("%s(%Zu)", __FUNCTION__, count);
   3.223 +-  debug("%s(%d)", __FUNCTION__, count);
   3.224  -  down(&tpm_mutex);
   3.225  -  if (tpm_response.data != NULL) {
   3.226  -    count = min(count, (size_t)tpm_response.size - (size_t)*ppos);
   3.227 @@ -315,7 +204,7 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.228  -
   3.229  -static ssize_t tpm_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
   3.230  -{
   3.231 --  debug("%s(%Zu)", __FUNCTION__, count);
   3.232 +-  debug("%s(%d)", __FUNCTION__, count);
   3.233  -  down(&tpm_mutex);
   3.234  -  *ppos = 0;
   3.235  -  if (tpm_response.data != NULL) kfree(tpm_response.data);
   3.236 @@ -379,7 +268,8 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.237  -  /* initialize variables */
   3.238  -  sema_init(&tpm_mutex, 1);
   3.239  -  module_state = 0;
   3.240 --  tpm_response.data = NULL;    
   3.241 +-  tpm_response.data = NULL;
   3.242 +-  old_time = current_kernel_time();
   3.243  -  /* initialize TPM emulator */
   3.244  -  if (!strcmp(startup, "clear")) {
   3.245  -    tpm_emulator_init(1);
   3.246 @@ -408,17 +298,16 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.247  -
   3.248  -uint64_t tpm_get_ticks(void)
   3.249  -{
   3.250 --  static struct timespec old_time = {0, 0};
   3.251  -  struct timespec new_time = current_kernel_time();
   3.252 --  uint64_t ticks = (uint64_t)(old_time.tv_sec - new_time.tv_sec) * 1000000
   3.253 --                   + (old_time.tv_nsec - new_time.tv_nsec) / 1000;
   3.254 +-  uint64_t ticks = (uint64_t)(new_time.tv_sec - old_time.tv_sec) * 1000000
   3.255 +-                   + (new_time.tv_nsec - old_time.tv_nsec) / 1000;
   3.256  -  old_time = new_time;
   3.257  -  return (ticks > 0) ? ticks : 1;
   3.258  -}
   3.259  -
   3.260 -diff -uprN tpm_emulator-0.3-x86_64/linux_module.h tpm_emulator/linux_module.h
   3.261 ---- tpm_emulator-0.3-x86_64/linux_module.h	2006-08-29 15:07:43.527968288 -0700
   3.262 -+++ tpm_emulator/linux_module.h	2006-08-29 15:26:17.103679048 -0700
   3.263 +diff -uprN orig/tpm_emulator-0.4/linux_module.h tpm_emulator/linux_module.h
   3.264 +--- orig/tpm_emulator-0.4/linux_module.h	2006-06-23 03:37:07.000000000 -0700
   3.265 ++++ tpm_emulator/linux_module.h	2006-07-24 14:35:35.000000000 -0700
   3.266  @@ -1,5 +1,6 @@
   3.267   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.268    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.269 @@ -426,7 +315,7 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.270    *
   3.271    * This module is free software; you can redistribute it and/or modify
   3.272    * it under the terms of the GNU General Public License as published
   3.273 -@@ -17,17 +18,22 @@
   3.274 +@@ -17,54 +18,62 @@
   3.275   #ifndef _LINUX_MODULE_H_
   3.276   #define _LINUX_MODULE_H_
   3.277   
   3.278 @@ -454,12 +343,15 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.279   
   3.280  +/* module settings */
   3.281  +#define min(A,B) ((A)<(B)?(A):(B))
   3.282 - #ifndef STR
   3.283 ++#ifndef STR
   3.284   #define STR(s) __STR__(s)
   3.285   #define __STR__(s) #s
   3.286 -@@ -38,35 +44,36 @@
   3.287 - #define TPM_DEVICE_NAME         "tpm"
   3.288 - #define TPM_MODULE_NAME 	"tpm_emulator"
   3.289 ++#endif
   3.290 + #include "tpm_version.h"
   3.291 + 
   3.292 + #define TPM_DEVICE_MINOR  224
   3.293 + #define TPM_DEVICE_NAME   "tpm"
   3.294 + #define TPM_MODULE_NAME   "tpm_emulator"
   3.295   
   3.296  -/* debug and log output functions */
   3.297  -
   3.298 @@ -506,7 +398,7 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.299   static inline void tpm_get_random_bytes(void *buf, int nbytes)
   3.300   {
   3.301     get_random_bytes(buf, nbytes);
   3.302 -@@ -86,9 +93,9 @@ uint64_t tpm_get_ticks(void);
   3.303 +@@ -84,9 +93,9 @@ uint64_t tpm_get_ticks(void);
   3.304   #define CPU_TO_LE16(x) __cpu_to_le16(x)
   3.305   
   3.306   #define BE64_TO_CPU(x) __be64_to_cpu(x)
   3.307 @@ -518,205 +410,242 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   3.308   #define BE16_TO_CPU(x) __be16_to_cpu(x)
   3.309   #define LE16_TO_CPU(x) __le16_to_cpu(x)
   3.310   
   3.311 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_audit.c tpm_emulator/tpm/tpm_audit.c
   3.312 ---- tpm_emulator-0.3-x86_64/tpm/tpm_audit.c	2006-08-29 15:07:21.620298760 -0700
   3.313 -+++ tpm_emulator/tpm/tpm_audit.c	2006-08-29 15:26:17.107678440 -0700
   3.314 -@@ -1,6 +1,7 @@
   3.315 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.316 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.317 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   3.318 -+ * Copyright (C) 2005 INTEL Corp
   3.319 -  *
   3.320 -  * This module is free software; you can redistribute it and/or modify
   3.321 -  * it under the terms of the GNU General Public License as published
   3.322 -@@ -45,14 +46,14 @@ void tpm_audit_request(TPM_COMMAND_CODE 
   3.323 -       tpmData.permanent.data.auditMonotonicCounter++;
   3.324 -     }
   3.325 -     /* update audit digest */
   3.326 --    *((UINT16*)&buf[0])  = cpu_to_be16(TPM_TAG_AUDIT_EVENT_IN);
   3.327 --    *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
   3.328 -+    *((UINT16*)&buf[0])  = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_IN);
   3.329 -+    *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
   3.330 -     sha1_init(&sha1_ctx);
   3.331 -     sha1_update(&sha1_ctx, req->param, req->paramSize);
   3.332 -     sha1_final(&sha1_ctx, &buf[6]);
   3.333 --    *((UINT16*)&buf[26])  = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
   3.334 -+    *((UINT16*)&buf[26])  = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
   3.335 -     memset(&buf[30], 0, 4);
   3.336 --    *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
   3.337 -+    *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
   3.338 -     sha1_init(&sha1_ctx);
   3.339 -     sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest, 
   3.340 -       sizeof(TPM_DIGEST));
   3.341 -@@ -70,15 +71,15 @@ void tpm_audit_response(TPM_COMMAND_CODE
   3.342 -       && (AUDIT_STATUS[ord / 8] & (1 << (ord & 0x07)))) {
   3.343 -     info("tpm_audit_response()");
   3.344 -     /* update audit digest */
   3.345 --    *((UINT16*)&buf[0])  = cpu_to_be16(TPM_TAG_AUDIT_EVENT_OUT);
   3.346 --    *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
   3.347 -+    *((UINT16*)&buf[0])  = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_OUT);
   3.348 -+    *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
   3.349 -     sha1_init(&sha1_ctx);
   3.350 -     sha1_update(&sha1_ctx, rsp->param, rsp->paramSize);
   3.351 -     sha1_final(&sha1_ctx, &buf[6]);
   3.352 --    *((UINT16*)&buf[26])  = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
   3.353 -+    *((UINT16*)&buf[26])  = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
   3.354 -     memset(&buf[30], 0, 4);
   3.355 --    *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
   3.356 --    *((UINT32*)&buf[34]) = cpu_to_be32(rsp->result);
   3.357 -+    *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
   3.358 -+    *((UINT32*)&buf[34]) = CPU_TO_BE32(rsp->result);
   3.359 -     sha1_init(&sha1_ctx);
   3.360 -     sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest, 
   3.361 -       sizeof(TPM_DIGEST));
   3.362 -@@ -158,7 +159,7 @@ TPM_RESULT TPM_GetAuditDigestSigned(TPM_
   3.363 -   }
   3.364 -   memcpy(&buf[0], "\x05\x00ADIG", 6);
   3.365 -   memcpy(&buf[6], antiReplay->nonce, 20);
   3.366 --  *(UINT32*)&buf[26] = cpu_to_be32(buf_size - 30);
   3.367 -+  *(UINT32*)&buf[26] = CPU_TO_BE32(buf_size - 30);
   3.368 -   memcpy(&buf[30], auditDigest->digest, 20);
   3.369 -   ptr = &buf[50];
   3.370 -   len = buf_size - 50;
   3.371 -@@ -198,4 +199,3 @@ TPM_RESULT TPM_SetOrdinalAuditStatus(TPM
   3.372 -   }
   3.373 -   return TPM_SUCCESS;
   3.374 - }
   3.375 +diff -uprN orig/tpm_emulator-0.4/Makefile tpm_emulator/Makefile
   3.376 +--- orig/tpm_emulator-0.4/Makefile	2006-06-23 03:37:07.000000000 -0700
   3.377 ++++ tpm_emulator/Makefile	2006-07-24 14:35:35.000000000 -0700
   3.378 +@@ -1,24 +1,40 @@
   3.379 + # Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.380 + # Copyright (C) 2004 Mario Strasser <mast@gmx.net>
   3.381 ++# Copyright (C) 2006 INTEL Corp.
   3.382 + #
   3.383 + # $Id: Makefile 115 2006-06-23 10:36:44Z mast $
   3.384 + 
   3.385 +-# kernel settings
   3.386 +-KERNEL_RELEASE := $(shell uname -r)
   3.387 +-KERNEL_BUILD   := /lib/modules/$(KERNEL_RELEASE)/build
   3.388 +-MOD_SUBDIR     := misc
   3.389 ++COMPILE_ARCH    ?= $(shell uname -m | sed -e s/i.86/x86_32/)
   3.390 + 
   3.391 + # module settings
   3.392 +-MODULE_NAME    := tpm_emulator
   3.393 ++BIN            := tpm_emulator
   3.394 + VERSION_MAJOR  := 0
   3.395 + VERSION_MINOR  := 4
   3.396 + VERSION_BUILD  := $(shell date +"%s")
   3.397 + 
   3.398 +-# enable/disable DEBUG messages
   3.399 +-EXTRA_CFLAGS   += -Wall -DDEBUG -g  
   3.400 ++# Installation program and options
   3.401 ++INSTALL         = install
   3.402 ++INSTALL_PROG    = $(INSTALL) -m0755
   3.403 ++INSTALL_DIR     = $(INSTALL) -d -m0755
   3.404 ++
   3.405 ++# Xen tools installation directory
   3.406 ++TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
   3.407 ++
   3.408 ++CC      := gcc
   3.409 ++CFLAGS  += -g -Wall $(INCLUDE) -DDEBUG
   3.410 ++CFLAGS  += -I. -Itpm
   3.411 ++
   3.412 ++# Is the simulator running in it's own vm?
   3.413 ++#CFLAGS += -DVTPM_MULTI_VM
   3.414 ++
   3.415 ++ifeq ($(COMPILE_ARCH),x86_64)
   3.416 ++LIBDIR = lib64
   3.417 ++else
   3.418 ++LIBDIR = lib
   3.419 ++endif
   3.420 + 
   3.421 + # GNU MP configuration
   3.422 +-GMP_LIB        := /usr/lib/libgmp.a
   3.423 ++GMP_LIB        := /usr/$(LIBDIR)/libgmp.a
   3.424 + GMP_HEADER     := /usr/include/gmp.h
   3.425 + 
   3.426 + # sources and objects
   3.427 +@@ -27,38 +43,32 @@ DIRS           := . crypto tpm 
   3.428 + SRCS           := $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.c))
   3.429 + OBJS           := $(patsubst %.c, %.o, $(SRCS))
   3.430 + SRCS           += $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.h))
   3.431 +-DISTSRC        := ./README ./AUTHORS ./ChangeLog ./Makefile $(SRCS)
   3.432 +-DISTDIR        := tpm_emulator-$(VERSION_MAJOR).$(VERSION_MINOR)
   3.433 + 
   3.434 +-obj-m               := $(MODULE_NAME).o
   3.435 +-$(MODULE_NAME)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
   3.436 ++obj-m               := $(BIN)
   3.437 ++$(BIN)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
   3.438 + 
   3.439 + EXTRA_CFLAGS   += -I$(src) -I$(src)/crypto -I$(src)/tpm 
   3.440 + 
   3.441 + # do not print "Entering directory ..."
   3.442 + MAKEFLAGS      += --no-print-directory
   3.443 + 
   3.444 +-all:	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version
   3.445 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules
   3.446 ++all: $(BIN)
   3.447 + 
   3.448 +-install:
   3.449 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules_install
   3.450 +-	test -d /var/tpm || mkdir /var/tpm
   3.451 +-	test -c /dev/tpm || mknod /dev/tpm c 10 224
   3.452 +-	chmod 666 /dev/tpm
   3.453 +-	depmod -a
   3.454 ++$(BIN):	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version $(SRCS) $(OBJS)
   3.455 ++	$(CC) $(CFLAGS) $(OBJS) $(src)/crypto/libgmp.a -o $(BIN)
   3.456 ++
   3.457 ++%.o: %.c
   3.458 ++	$(CC) $(CFLAGS) -c $< -o $@
   3.459 ++
   3.460 ++install: $(BIN)
   3.461 ++	$(INSTALL_PROG) $(BIN) $(TOOLS_INSTALL_DIR)
   3.462 ++	@if [ ! -d "/var/tpm" ]; then mkdir /var/tpm; fi
   3.463 + 
   3.464 + clean:
   3.465 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) clean
   3.466 +-	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a
   3.467 ++	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a $(OBJS)
   3.468 + 
   3.469 +-dist:	$(DISTSRC)
   3.470 +-	rm -rf $(DISTDIR)
   3.471 +-	mkdir $(DISTDIR)
   3.472 +-	cp --parents $(DISTSRC) $(DISTDIR)/
   3.473 +-	rm -f $(DISTDIR)/crypto/gmp.h 
   3.474 +-	tar -chzf $(DISTDIR).tar.gz $(DISTDIR)
   3.475 +-	rm -rf $(DISTDIR)
   3.476 ++mrproper: clean
   3.477 ++	rm -f $(BIN) tpm_version.h
   3.478 + 
   3.479 + $(src)/crypto/libgmp.a:
   3.480 + 	test -f $(src)/crypto/libgmp.a || ln -s $(GMP_LIB) $(src)/crypto/libgmp.a
   3.481 +@@ -88,4 +98,3 @@ version:
   3.482 + 	@echo "#endif /* _TPM_VERSION_H_ */" >> $(src)/tpm_version.h
   3.483 + 
   3.484 + .PHONY: all install clean dist gmp version
   3.485  -
   3.486 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_authorization.c tpm_emulator/tpm/tpm_authorization.c
   3.487 ---- tpm_emulator-0.3-x86_64/tpm/tpm_authorization.c	2006-08-29 15:07:21.620298760 -0700
   3.488 -+++ tpm_emulator/tpm/tpm_authorization.c	2006-08-29 15:26:17.108678288 -0700
   3.489 -@@ -1,6 +1,7 @@
   3.490 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.491 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.492 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   3.493 -+ * Copyright (C) 2005 INTEL Corp
   3.494 -  *
   3.495 -  * This module is free software; you can redistribute it and/or modify
   3.496 -  * it under the terms of the GNU General Public License as published
   3.497 -@@ -279,7 +280,7 @@ TPM_RESULT tpm_verify_auth(TPM_AUTH *aut
   3.498 +diff -uprN orig/tpm_emulator-0.4/README tpm_emulator/README
   3.499 +--- orig/tpm_emulator-0.4/README	2006-06-23 03:37:07.000000000 -0700
   3.500 ++++ tpm_emulator/README	2006-07-24 14:35:35.000000000 -0700
   3.501 +@@ -13,7 +13,8 @@ $Id: README 113 2006-06-18 12:38:13Z hst
   3.502 + Copyright
   3.503 + --------------------------------------------------------------------------
   3.504 + Copyright (C) 2004 Mario Strasser <mast@gmx.net> and Swiss Federal 
   3.505 +-Institute of Technology (ETH) Zurich.
   3.506 ++                   Institute of Technology (ETH) Zurich.
   3.507 ++Copyright (C) 2005 INTEL Corp 
   3.508 +               
   3.509 + This program is free software; you can redistribute it and/or modify
   3.510 + it under the terms of the GNU General Public License as published by
   3.511 +@@ -43,6 +44,12 @@ Example:
   3.512 + GMP_LIB        := /usr/lib/libgmp.a
   3.513 + GMP_HEADER     := /usr/include/gmp.h
   3.514 + 
   3.515 ++GNU MP Library on 64 bit Systems
   3.516 ++--------------------------------------------------------------------------
   3.517 ++Some 64-bit kernels have problems with importing the user-space gmp 
   3.518 ++library (/usr/lib*/libgmp.a) into kernel space.  These kernels will require
   3.519 ++that the gmp library be recompiled for kernel space with -mcmodel=kernel.
   3.520 ++
   3.521 + Installation
   3.522 + --------------------------------------------------------------------------
   3.523 + The compilation and installation process uses the build environment for 
   3.524 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_credentials.c tpm_emulator/tpm/tpm_credentials.c
   3.525 +--- orig/tpm_emulator-0.4/tpm/tpm_credentials.c	2006-06-23 03:37:07.000000000 -0700
   3.526 ++++ tpm_emulator/tpm/tpm_credentials.c	2006-07-24 14:35:35.000000000 -0700
   3.527 +@@ -47,16 +47,16 @@ int tpm_compute_pubkey_checksum(TPM_NONC
   3.528 + 
   3.529 + TPM_RESULT tpm_get_pubek(TPM_PUBKEY *pubEndorsementKey)
   3.530   {
   3.531 -   hmac_ctx_t ctx;
   3.532 -   TPM_SESSION_DATA *session;
   3.533 --  UINT32 auth_handle = cpu_to_be32(auth->authHandle);
   3.534 -+  UINT32 auth_handle = CPU_TO_BE32(auth->authHandle);
   3.535 -   
   3.536 -   info("tpm_verify_auth(%08x)", auth->authHandle);
   3.537 -   /* get dedicated authorization or transport session */
   3.538 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_capability.c tpm_emulator/tpm/tpm_capability.c
   3.539 ---- tpm_emulator-0.3-x86_64/tpm/tpm_capability.c	2006-08-29 15:07:21.620298760 -0700
   3.540 -+++ tpm_emulator/tpm/tpm_capability.c	2006-08-29 15:26:17.109678136 -0700
   3.541 -@@ -1,6 +1,7 @@
   3.542 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.543 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.544 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   3.545 -+ * Copyright (C) 2005 INTEL Corp
   3.546 -  *
   3.547 -  * This module is free software; you can redistribute it and/or modify
   3.548 -  * it under the terms of the GNU General Public License as published
   3.549 -@@ -406,7 +407,7 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
   3.550 - 
   3.551 -     case TPM_CAP_KEY_HANDLE:
   3.552 -       debug("[TPM_CAP_KEY_HANDLE]");
   3.553 --      subCapSize = cpu_to_be32(TPM_RT_KEY);
   3.554 -+      subCapSize = CPU_TO_BE32(TPM_RT_KEY);
   3.555 -       return cap_handle(4, (BYTE*)&subCapSize, respSize, resp);
   3.556 - 
   3.557 -     case TPM_CAP_CHECK_LOADED:
   3.558 -@@ -480,4 +481,3 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
   3.559 -       return TPM_BAD_MODE;
   3.560 -   }
   3.561 - }
   3.562 --
   3.563 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_cmd_handler.c tpm_emulator/tpm/tpm_cmd_handler.c
   3.564 ---- tpm_emulator-0.3-x86_64/tpm/tpm_cmd_handler.c	2006-08-29 15:07:21.621298608 -0700
   3.565 -+++ tpm_emulator/tpm/tpm_cmd_handler.c	2006-08-29 15:26:17.113677528 -0700
   3.566 -@@ -1,6 +1,7 @@
   3.567 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.568 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.569 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   3.570 -+ * Copyright (C) 2005 INTEL Corp
   3.571 -  *
   3.572 -  * This module is free software; you can redistribute it and/or modify
   3.573 -  * it under the terms of the GNU General Public License as published
   3.574 -@@ -73,7 +74,7 @@ void tpm_compute_in_param_digest(TPM_REQ
   3.575 - {
   3.576 -   sha1_ctx_t sha1;
   3.577 -   UINT32 offset = tpm_get_param_offset(req->ordinal);
   3.578 --  UINT32 ord = cpu_to_be32(req->ordinal);
   3.579 -+  UINT32 ord = CPU_TO_BE32(req->ordinal);
   3.580 - 
   3.581 -   /* compute SHA1 hash */
   3.582 -   if (offset <= req->paramSize) {
   3.583 -@@ -89,8 +90,8 @@ void tpm_compute_in_param_digest(TPM_REQ
   3.584 - void tpm_compute_out_param_digest(TPM_COMMAND_CODE ordinal, TPM_RESPONSE *rsp)
   3.585 - {
   3.586 -   sha1_ctx_t sha1;
   3.587 --  UINT32 res = cpu_to_be32(rsp->result);
   3.588 --  UINT32 ord = cpu_to_be32(ordinal);
   3.589 -+  UINT32 res = CPU_TO_BE32(rsp->result);
   3.590 -+  UINT32 ord = CPU_TO_BE32(ordinal);
   3.591 - 
   3.592 -   /* compute SHA1 hash */
   3.593 -   sha1_init(&sha1);
   3.594 -@@ -3123,7 +3124,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
   3.595 -       hmac_update(&hmac, rsp->auth2->digest, sizeof(rsp->auth2->digest));
   3.596 - #if 0
   3.597 -       if (tpm_get_auth(rsp->auth2->authHandle)->type == TPM_ST_OIAP) {
   3.598 --        UINT32 handle = cpu_to_be32(rsp->auth2->authHandle);
   3.599 -+        UINT32 handle = CPU_TO_BE32(rsp->auth2->authHandle);
   3.600 -         hmac_update(&hmac, (BYTE*)&handle, 4);
   3.601 -       }
   3.602 - #endif
   3.603 -@@ -3138,7 +3139,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
   3.604 -       hmac_update(&hmac, rsp->auth1->digest, sizeof(rsp->auth1->digest));
   3.605 - #if 0
   3.606 -       if (tpm_get_auth(rsp->auth1->authHandle)->type == TPM_ST_OIAP) {
   3.607 --        UINT32 handle = cpu_to_be32(rsp->auth1->authHandle);
   3.608 -+        UINT32 handle = CPU_TO_BE32(rsp->auth1->authHandle);
   3.609 -         hmac_update(&hmac, (BYTE*)&handle, 4);
   3.610 -       }
   3.611 - #endif
   3.612 -@@ -3221,7 +3222,9 @@ extern const char *tpm_error_to_string(T
   3.613 - void tpm_execute_command(TPM_REQUEST *req, TPM_RESPONSE *rsp)
   3.614 +-  UINT32 key_length;
   3.615 ++  size_t key_length;
   3.616 +   if (!tpmData.permanent.data.endorsementKey.size) return TPM_NO_ENDORSEMENT;
   3.617 +   /* setup TPM_PUBKEY structure */
   3.618 +-  key_length = tpmData.permanent.data.endorsementKey.size;
   3.619 +-  pubEndorsementKey->pubKey.keyLength = key_length >> 3;
   3.620 ++  pubEndorsementKey->pubKey.keyLength = tpmData.permanent.data.endorsementKey.size >> 3;
   3.621 +   pubEndorsementKey->pubKey.key = tpm_malloc(pubEndorsementKey->pubKey.keyLength);
   3.622 +   if (pubEndorsementKey->pubKey.key == NULL) return TPM_FAIL;
   3.623 +   rsa_export_modulus(&tpmData.permanent.data.endorsementKey,
   3.624 +-    pubEndorsementKey->pubKey.key,
   3.625 +-    &pubEndorsementKey->pubKey.keyLength);
   3.626 ++		     pubEndorsementKey->pubKey.key,
   3.627 ++		     &key_length);
   3.628 ++  pubEndorsementKey->pubKey.keyLength = key_length;
   3.629 +   pubEndorsementKey->algorithmParms.algorithmID = TPM_ALG_RSA;
   3.630 +   pubEndorsementKey->algorithmParms.encScheme = TPM_ES_RSAESOAEP_SHA1_MGF1;
   3.631 +   pubEndorsementKey->algorithmParms.sigScheme = TPM_SS_NONE;
   3.632 +@@ -175,6 +175,7 @@ TPM_RESULT TPM_OwnerReadInternalPub(TPM_
   3.633   {
   3.634     TPM_RESULT res;
   3.635 --  
   3.636 -+
   3.637 -+  req->tag = (BYTE) req->tag;  // FIXME: Why is this here
   3.638 -+
   3.639 -   /* setup authorisation as well as response tag and size */
   3.640 -   memset(rsp, 0, sizeof(*rsp));
   3.641 -   switch (req->tag) {
   3.642 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_crypto.c tpm_emulator/tpm/tpm_crypto.c
   3.643 ---- tpm_emulator-0.3-x86_64/tpm/tpm_crypto.c	2006-08-29 15:07:43.531967680 -0700
   3.644 -+++ tpm_emulator/tpm/tpm_crypto.c	2006-08-29 15:26:17.114677376 -0700
   3.645 -@@ -1,6 +1,7 @@
   3.646 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   3.647 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   3.648 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   3.649 -+ * Copyright (C) 2005 INTEL Corp
   3.650 -  *
   3.651 -  * This module is free software; you can redistribute it and/or modify
   3.652 -  * it under the terms of the GNU General Public License as published
   3.653 -@@ -106,7 +107,7 @@ TPM_RESULT tpm_sign(TPM_KEY_DATA *key, T
   3.654 -     /* setup TPM_SIGN_INFO structure */
   3.655 -     memcpy(&buf[0], "\x05\x00SIGN", 6);
   3.656 -     memcpy(&buf[6], auth->nonceOdd.nonce, 20);
   3.657 --    *(UINT32*)&buf[26] = cpu_to_be32(areaToSignSize);
   3.658 -+    *(UINT32*)&buf[26] = CPU_TO_BE32(areaToSignSize);
   3.659 -     memcpy(&buf[30], areaToSign, areaToSignSize);
   3.660 -     if (rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1, 
   3.661 -         buf, areaToSignSize + 30, *sig)) {
   3.662 -@@ -383,4 +384,3 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
   3.663 +   TPM_KEY_DATA *srk = &tpmData.permanent.data.srk;
   3.664 ++  size_t key_length;
   3.665 +   info("TPM_OwnerReadInternalPub()");
   3.666 +   /* verify authorization */
   3.667 +   res = tpm_verify_auth(auth1, tpmData.permanent.data.ownerAuth, TPM_KH_OWNER);
   3.668 +@@ -186,7 +187,8 @@ TPM_RESULT TPM_OwnerReadInternalPub(TPM_
   3.669 +     publicPortion->pubKey.key = tpm_malloc(publicPortion->pubKey.keyLength);
   3.670 +     if (publicPortion->pubKey.key == NULL) return TPM_FAIL;
   3.671 +     rsa_export_modulus(&srk->key, publicPortion->pubKey.key, 
   3.672 +-      &publicPortion->pubKey.keyLength);
   3.673 ++      &key_length);
   3.674 ++    publicPortion->pubKey.keyLength = key_length;
   3.675 +     publicPortion->algorithmParms.algorithmID = TPM_ALG_RSA;
   3.676 +     publicPortion->algorithmParms.encScheme = srk->encScheme;
   3.677 +     publicPortion->algorithmParms.sigScheme = srk->sigScheme;
   3.678 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_crypto.c tpm_emulator/tpm/tpm_crypto.c
   3.679 +--- orig/tpm_emulator-0.4/tpm/tpm_crypto.c	2006-06-23 03:37:07.000000000 -0700
   3.680 ++++ tpm_emulator/tpm/tpm_crypto.c	2006-07-24 14:35:35.000000000 -0700
   3.681 +@@ -182,7 +182,8 @@ TPM_RESULT TPM_CertifyKey(TPM_KEY_HANDLE
   3.682 +   TPM_KEY_DATA *cert, *key;
   3.683 +   sha1_ctx_t sha1_ctx;
   3.684 +   BYTE *buf, *p;
   3.685 +-  UINT32 length;
   3.686 ++  UINT32 length32;
   3.687 ++  size_t length;
   3.688 +   info("TPM_CertifyKey()");
   3.689 +   /* get keys */
   3.690 +   cert = tpm_get_key(certHandle);
   3.691 +@@ -264,14 +265,15 @@ TPM_RESULT TPM_CertifyKey(TPM_KEY_HANDLE
   3.692 +   /* compute the digest of the CERTIFY_INFO[2] structure and sign it */
   3.693 +   length = sizeof_TPM_CERTIFY_INFO((*certifyInfo));
   3.694 +   p = buf = tpm_malloc(length);
   3.695 ++  length32=(UINT32) length;
   3.696 +   if (buf == NULL
   3.697 +-      || tpm_marshal_TPM_CERTIFY_INFO(&p, &length, certifyInfo)) {
   3.698 ++      || tpm_marshal_TPM_CERTIFY_INFO(&p, &length32, certifyInfo)) {
   3.699 +     free_TPM_KEY_PARMS(certifyInfo->algorithmParms);
   3.700 +     return TPM_FAIL;
   3.701     }
   3.702 -   return TPM_SUCCESS;
   3.703 - }
   3.704 --
   3.705 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_daa.c tpm_emulator/tpm/tpm_daa.c
   3.706 ---- tpm_emulator-0.3-x86_64/tpm/tpm_daa.c	2006-08-29 15:07:21.622298456 -0700
   3.707 -+++ tpm_emulator/tpm/tpm_daa.c	2006-08-29 15:26:17.119676616 -0700
   3.708 -@@ -700,14 +700,14 @@ info("tested until here");
   3.709 +   length = sizeof_TPM_CERTIFY_INFO((*certifyInfo));
   3.710 +   sha1_init(&sha1_ctx);
   3.711 +-  sha1_update(&sha1_ctx, buf, length);
   3.712 ++  sha1_update(&sha1_ctx, buf, (size_t) length);
   3.713 +   sha1_final(&sha1_ctx, buf);
   3.714 +   res = tpm_sign(cert, auth1, FALSE, buf, SHA1_DIGEST_LENGTH, outData, outDataSize);
   3.715 +   tpm_free(buf);
   3.716 +@@ -292,7 +294,8 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
   3.717 +   TPM_KEY_DATA *cert, *key;
   3.718 +   sha1_ctx_t sha1_ctx;
   3.719 +   BYTE *buf, *p;
   3.720 +-  UINT32 length;
   3.721 ++  size_t length;
   3.722 ++  UINT32 length32;
   3.723 +   info("TPM_CertifyKey2()");
   3.724 +   /* get keys */
   3.725 +   cert = tpm_get_key(certHandle);
   3.726 +@@ -362,8 +365,9 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
   3.727 +   /* compute the digest of the CERTIFY_INFO[2] structure and sign it */
   3.728 +   length = sizeof_TPM_CERTIFY_INFO((*certifyInfo));
   3.729 +   p = buf = tpm_malloc(length);
   3.730 ++  length32 = (UINT32) length;
   3.731 +   if (buf == NULL
   3.732 +-      || tpm_marshal_TPM_CERTIFY_INFO(&p, &length, certifyInfo)) {
   3.733 ++      || tpm_marshal_TPM_CERTIFY_INFO(&p, &length32, certifyInfo)) {
   3.734 +     free_TPM_KEY_PARMS(certifyInfo->algorithmParms);
   3.735 +     return TPM_FAIL;
   3.736 +   }
   3.737 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_daa.c tpm_emulator/tpm/tpm_daa.c
   3.738 +--- orig/tpm_emulator-0.4/tpm/tpm_daa.c	2006-06-23 03:37:07.000000000 -0700
   3.739 ++++ tpm_emulator/tpm/tpm_daa.c	2006-07-24 14:35:35.000000000 -0700
   3.740 +@@ -716,14 +716,14 @@ TPM_RESULT TPM_DAA_Join(TPM_HANDLE handl
   3.741             sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.742         sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.743             sizeof(session->DAA_tpmSpecific.DAA_count));
   3.744 @@ -733,58 +662,7 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   3.745         sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.746         mpz_init(f), mpz_init(q);
   3.747         mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.748 -@@ -787,14 +787,14 @@ info("tested until here");
   3.749 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.750 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.751 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.752 --      sha1_update(&sha1, "\x00", 1);
   3.753 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   3.754 -       sha1_final(&sha1, scratch);
   3.755 -       sha1_init(&sha1);
   3.756 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   3.757 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.758 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.759 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.760 --      sha1_update(&sha1, "\x01", 1);
   3.761 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.762 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.763 -       mpz_init(f), mpz_init(q);
   3.764 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.765 -@@ -1440,14 +1440,14 @@ info("tested until here");
   3.766 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.767 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.768 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.769 --      sha1_update(&sha1, "\x00", 1);
   3.770 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   3.771 -       sha1_final(&sha1, scratch);
   3.772 -       sha1_init(&sha1);
   3.773 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   3.774 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.775 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.776 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.777 --      sha1_update(&sha1, "\x01", 1);
   3.778 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.779 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.780 -       mpz_init(f), mpz_init(q);
   3.781 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.782 -@@ -1660,14 +1660,14 @@ info("tested until here");
   3.783 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.784 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.785 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.786 --      sha1_update(&sha1, "\x00", 1);
   3.787 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   3.788 -       sha1_final(&sha1, scratch);
   3.789 -       sha1_init(&sha1);
   3.790 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   3.791 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.792 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.793 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.794 --      sha1_update(&sha1, "\x01", 1);
   3.795 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.796 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.797 -       mpz_init(f), mpz_init(q);
   3.798 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.799 -@@ -1740,14 +1740,14 @@ info("tested until here");
   3.800 +@@ -805,14 +805,14 @@ TPM_RESULT TPM_DAA_Join(TPM_HANDLE handl
   3.801             sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.802         sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.803             sizeof(session->DAA_tpmSpecific.DAA_count));
   3.804 @@ -801,7 +679,7 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   3.805         sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.806         mpz_init(f), mpz_init(q);
   3.807         mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.808 -@@ -2828,14 +2828,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.809 +@@ -1489,14 +1489,14 @@ TPM_RESULT TPM_DAA_Join(TPM_HANDLE handl
   3.810             sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.811         sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.812             sizeof(session->DAA_tpmSpecific.DAA_count));
   3.813 @@ -818,25 +696,7 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   3.814         sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.815         mpz_init(f), mpz_init(q);
   3.816         mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.817 -@@ -3050,7 +3050,7 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.818 -         sha1_init(&sha1);
   3.819 -         sha1_update(&sha1, (BYTE*) &session->DAA_session.DAA_digest, 
   3.820 -           sizeof(session->DAA_session.DAA_digest));
   3.821 --        sha1_update(&sha1, "\x01", 1);
   3.822 -+        sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.823 -         sha1_update(&sha1, inputData1, inputSize1);
   3.824 -         sha1_final(&sha1, (BYTE*) &session->DAA_session.DAA_digest);
   3.825 -       }
   3.826 -@@ -3078,7 +3078,7 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.827 -         sha1_init(&sha1);
   3.828 -         sha1_update(&sha1, (BYTE*) &session->DAA_session.DAA_digest, 
   3.829 -           sizeof(session->DAA_session.DAA_digest));
   3.830 --        sha1_update(&sha1, "\x01", 1);
   3.831 -+        sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.832 -         rsa_export_modulus(&aikData->key, scratch, &size);
   3.833 -         sha1_update(&sha1, scratch, size);
   3.834 -         sha1_final(&sha1, (BYTE*) &session->DAA_session.DAA_digest);
   3.835 -@@ -3134,14 +3134,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.836 +@@ -1712,14 +1712,14 @@ TPM_RESULT TPM_DAA_Join(TPM_HANDLE handl
   3.837             sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.838         sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.839             sizeof(session->DAA_tpmSpecific.DAA_count));
   3.840 @@ -853,7 +713,7 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   3.841         sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.842         mpz_init(f), mpz_init(q);
   3.843         mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.844 -@@ -3213,14 +3213,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.845 +@@ -1793,14 +1793,14 @@ TPM_RESULT TPM_DAA_Join(TPM_HANDLE handl
   3.846             sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.847         sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.848             sizeof(session->DAA_tpmSpecific.DAA_count));
   3.849 @@ -870,10 +730,96 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   3.850         sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.851         mpz_init(f), mpz_init(q);
   3.852         mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.853 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_data.c tpm_emulator/tpm/tpm_data.c
   3.854 ---- tpm_emulator-0.3-x86_64/tpm/tpm_data.c	2006-08-29 15:08:20.535342312 -0700
   3.855 -+++ tpm_emulator/tpm/tpm_data.c	2006-08-29 15:26:17.121676312 -0700
   3.856 -@@ -150,44 +150,43 @@ void tpm_release_data(void)
   3.857 +@@ -2918,14 +2918,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.858 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.859 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.860 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.861 +-      sha1_update(&sha1, "\x00", 1);
   3.862 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   3.863 +       sha1_final(&sha1, scratch);
   3.864 +       sha1_init(&sha1);
   3.865 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   3.866 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.867 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.868 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.869 +-      sha1_update(&sha1, "\x01", 1);
   3.870 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.871 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.872 +       mpz_init(f), mpz_init(q);
   3.873 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.874 +@@ -3143,7 +3143,7 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.875 +         sha1_init(&sha1);
   3.876 +         sha1_update(&sha1, (BYTE*) &session->DAA_session.DAA_digest, 
   3.877 +           sizeof(session->DAA_session.DAA_digest));
   3.878 +-        sha1_update(&sha1, "\x01", 1);
   3.879 ++        sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.880 +         sha1_update(&sha1, inputData1, inputSize1);
   3.881 +         sha1_final(&sha1, (BYTE*) &session->DAA_session.DAA_digest);
   3.882 +       }
   3.883 +@@ -3172,7 +3172,7 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.884 +         sha1_init(&sha1);
   3.885 +         sha1_update(&sha1, (BYTE*) &session->DAA_session.DAA_digest, 
   3.886 +           sizeof(session->DAA_session.DAA_digest));
   3.887 +-        sha1_update(&sha1, "\x00", 1);
   3.888 ++        sha1_update(&sha1, (BYTE*) "\x00", 1);
   3.889 +         rsa_export_modulus(&aikData->key, scratch, &size);
   3.890 +         sha1_update(&sha1, scratch, size);
   3.891 +         sha1_final(&sha1, (BYTE*) &session->DAA_session.DAA_digest);
   3.892 +@@ -3229,14 +3229,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.893 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.894 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.895 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.896 +-      sha1_update(&sha1, "\x00", 1);
   3.897 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   3.898 +       sha1_final(&sha1, scratch);
   3.899 +       sha1_init(&sha1);
   3.900 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   3.901 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.902 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.903 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.904 +-      sha1_update(&sha1, "\x01", 1);
   3.905 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.906 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.907 +       mpz_init(f), mpz_init(q);
   3.908 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.909 +@@ -3309,14 +3309,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   3.910 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.911 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.912 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.913 +-      sha1_update(&sha1, "\x00", 1);
   3.914 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   3.915 +       sha1_final(&sha1, scratch);
   3.916 +       sha1_init(&sha1);
   3.917 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   3.918 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   3.919 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   3.920 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   3.921 +-      sha1_update(&sha1, "\x01", 1);
   3.922 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   3.923 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   3.924 +       mpz_init(f), mpz_init(q);
   3.925 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   3.926 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_data.c tpm_emulator/tpm/tpm_data.c
   3.927 +--- orig/tpm_emulator-0.4/tpm/tpm_data.c	2006-06-23 03:37:07.000000000 -0700
   3.928 ++++ tpm_emulator/tpm/tpm_data.c	2006-07-24 14:35:35.000000000 -0700
   3.929 +@@ -40,6 +40,7 @@ static inline void init_pcr_attr(int pcr
   3.930 + void tpm_init_data(void)
   3.931 + {
   3.932 +   /* endorsement key */
   3.933 ++#ifndef TPM_GENERATE_EK
   3.934 +   uint8_t ek_n[] =  "\xa8\xdb\xa9\x42\xa8\xf3\xb8\x06\x85\x90\x76\x93\xad\xf7"
   3.935 +     "\x74\xec\x3f\xd3\x3d\x9d\xe8\x2e\xff\x15\xed\x0e\xce\x5f\x93"
   3.936 +     "\x92\xeb\xd1\x96\x2b\x72\x18\x81\x79\x12\x9d\x9c\x40\xd7\x1a"
   3.937 +@@ -77,6 +78,8 @@ void tpm_init_data(void)
   3.938 +     "\xd1\xc0\x8b\x5b\xa2\x2e\xa7\x15\xca\x50\x75\x10\x48\x9c\x2b"
   3.939 +     "\x18\xb9\x67\x8f\x5d\x64\xc3\x28\x9f\x2f\x16\x2f\x08\xda\x47"
   3.940 +     "\xec\x86\x43\x0c\x80\x99\x07\x34\x0f";
   3.941 ++#endif
   3.942 ++
   3.943 +   int i;
   3.944 +   /* reset all data to NULL, FALSE or 0 */
   3.945 +   memset(&tpmData, 0, sizeof(tpmData));
   3.946 +@@ -152,44 +155,43 @@ void tpm_release_data(void)
   3.947   
   3.948   #ifdef TPM_STORE_TO_FILE
   3.949   
   3.950 @@ -939,7 +885,75 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   3.951     if (res != *data_length) {
   3.952       tpm_free(*data);
   3.953       return -1;
   3.954 -@@ -278,7 +277,7 @@ int tpm_restore_permanent_data(void)
   3.955 +@@ -216,23 +218,30 @@ static int read_from_file(uint8_t **data
   3.956 + int tpm_store_permanent_data(void)
   3.957 + {
   3.958 +   uint8_t *buf, *ptr;
   3.959 +-  size_t buf_length, len;
   3.960 ++  UINT32 buf_length, len;
   3.961 + 
   3.962 +   /* marshal data */
   3.963 +-  buf_length = len = sizeof_TPM_STCLEAR_FLAGS(tpmData.stclear.flags)
   3.964 +-    + sizeof_TPM_PERMANENT_FLAGS(tpmData.permanent.flags) + 2
   3.965 +-    + sizeof_TPM_PERMANENT_DATA(tpmData.permanent.data);
   3.966 ++  buf_length = len = 4 + sizeof_TPM_STCLEAR_FLAGS(tpmData.stclear.flags)
   3.967 ++    + sizeof_TPM_PERMANENT_FLAGS(tpmData.permanent.flags) 
   3.968 ++    + sizeof_TPM_STANY_FLAGS(tpmData.stany.flags) + 2
   3.969 ++    + sizeof_TPM_STCLEAR_DATA(tpmData.stclear.data) 
   3.970 ++    + sizeof_TPM_PERMANENT_DATA(tpmData.permanent.data)
   3.971 ++    + sizeof_TPM_STANY_DATA(tpmData.stany.data);
   3.972 +   buf = ptr = tpm_malloc(buf_length);
   3.973 +   if (buf == NULL
   3.974 +       || tpm_marshal_TPM_VERSION(&ptr, &len, &tpmData.permanent.data.version)
   3.975 +       || tpm_marshal_TPM_STCLEAR_FLAGS(&ptr, &len, &tpmData.stclear.flags)
   3.976 +       || tpm_marshal_TPM_PERMANENT_FLAGS(&ptr, &len, &tpmData.permanent.flags)
   3.977 ++      || tpm_marshal_TPM_STANY_FLAGS(&ptr, &len, &tpmData.stany.flags)
   3.978 +       || tpm_marshal_BOOL(&ptr, &len, tpmData.permanent.flags.selfTestSucceeded)
   3.979 +       || tpm_marshal_BOOL(&ptr, &len, tpmData.permanent.flags.owned)
   3.980 +-      || tpm_marshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)) {
   3.981 ++      || tpm_marshal_TPM_STCLEAR_DATA(&ptr, &len, &tpmData.stclear.data)
   3.982 ++      || tpm_marshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)
   3.983 ++      || tpm_marshal_TPM_STANY_DATA(&ptr, &len, &tpmData.stany.data)) {
   3.984 +     tpm_free(buf);
   3.985 +     return -1;
   3.986 +   }
   3.987 ++
   3.988 +   if (write_to_file(buf, buf_length - len)) {
   3.989 +     tpm_free(buf);
   3.990 +     return -1; 
   3.991 +@@ -244,31 +253,36 @@ int tpm_store_permanent_data(void)
   3.992 + int tpm_restore_permanent_data(void)
   3.993 + {
   3.994 +   uint8_t *buf, *ptr;
   3.995 +-  size_t buf_length, len;
   3.996 ++  size_t buf_length;
   3.997 ++  UINT32 len;
   3.998 +   TPM_VERSION ver;
   3.999 + 
  3.1000 +   /* read data */
  3.1001 +   if (read_from_file(&buf, &buf_length)) return -1;
  3.1002 +   ptr = buf;
  3.1003 +-  len = buf_length;
  3.1004 ++  len = (uint32_t) buf_length;
  3.1005 +   /* unmarshal data */
  3.1006 +   if (tpm_unmarshal_TPM_VERSION(&ptr, &len, &ver)
  3.1007 +       || memcmp(&ver, &tpmData.permanent.data.version, sizeof(TPM_VERSION))
  3.1008 +       || tpm_unmarshal_TPM_STCLEAR_FLAGS(&ptr, &len, &tpmData.stclear.flags)
  3.1009 +       || tpm_unmarshal_TPM_PERMANENT_FLAGS(&ptr, &len, &tpmData.permanent.flags)
  3.1010 ++      || tpm_unmarshal_TPM_STANY_FLAGS(&ptr, &len, &tpmData.stany.flags)
  3.1011 +       || tpm_unmarshal_BOOL(&ptr, &len, &tpmData.permanent.flags.selfTestSucceeded)
  3.1012 +       || tpm_unmarshal_BOOL(&ptr, &len, &tpmData.permanent.flags.owned)
  3.1013 +-      || tpm_unmarshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)) {
  3.1014 ++      || tpm_unmarshal_TPM_STCLEAR_DATA(&ptr, &len, &tpmData.stclear.data)
  3.1015 ++      || tpm_unmarshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)
  3.1016 ++      || tpm_unmarshal_TPM_STANY_DATA(&ptr, &len, &tpmData.stany.data)) {
  3.1017 +     tpm_free(buf);
  3.1018 +     return -1;
  3.1019 +   }
  3.1020 ++
  3.1021 +   tpm_free(buf);
  3.1022 +   return 0;
  3.1023 + }
  3.1024   
  3.1025   int tpm_erase_permanent_data(void)
  3.1026   {
  3.1027 @@ -948,297 +962,9 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  3.1028     return res;
  3.1029   }
  3.1030   
  3.1031 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_data.c.orig tpm_emulator/tpm/tpm_data.c.orig
  3.1032 ---- tpm_emulator-0.3-x86_64/tpm/tpm_data.c.orig	1969-12-31 16:00:00.000000000 -0800
  3.1033 -+++ tpm_emulator/tpm/tpm_data.c.orig	2006-08-29 15:26:08.469991568 -0700
  3.1034 -@@ -0,0 +1,284 @@
  3.1035 -+/* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  3.1036 -+ * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  3.1037 -+ *                    Swiss Federal Institute of Technology (ETH) Zurich
  3.1038 -+ *
  3.1039 -+ * This module is free software; you can redistribute it and/or modify
  3.1040 -+ * it under the terms of the GNU General Public License as published
  3.1041 -+ * by the Free Software Foundation; either version 2 of the License,
  3.1042 -+ * or (at your option) any later version.
  3.1043 -+ *
  3.1044 -+ * This module is distributed in the hope that it will be useful,
  3.1045 -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3.1046 -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  3.1047 -+ * GNU General Public License for more details.
  3.1048 -+ *
  3.1049 -+ * $Id: tpm_data.c 36 2005-10-26 20:31:19Z hstamer $
  3.1050 -+ */
  3.1051 -+
  3.1052 -+#include "tpm_emulator.h"
  3.1053 -+#include "tpm_structures.h"
  3.1054 -+#include "tpm_marshalling.h"
  3.1055 -+#include "linux_module.h"
  3.1056 -+
  3.1057 -+TPM_DATA tpmData;
  3.1058 -+
  3.1059 -+BOOL tpm_get_physical_presence(void)
  3.1060 -+{
  3.1061 -+  return (tpmData.stclear.flags.physicalPresence || TRUE);
  3.1062 -+}
  3.1063 -+
  3.1064 -+static inline void init_pcr_attr(int pcr, BOOL reset, BYTE rl, BYTE el)
  3.1065 -+{
  3.1066 -+  int i;
  3.1067 -+  tpmData.permanent.data.pcrAttrib[pcr].pcrReset = reset;
  3.1068 -+  for (i = 0; i < TPM_NUM_LOCALITY; i++) {
  3.1069 -+    tpmData.permanent.data.pcrAttrib[pcr].pcrResetLocal[i] = (rl & (1 << i));
  3.1070 -+    tpmData.permanent.data.pcrAttrib[pcr].pcrExtendLocal[i] = (el & (1 << i));
  3.1071 -+  }
  3.1072 -+}
  3.1073 -+
  3.1074 -+void tpm_init_data(void)
  3.1075 -+{
  3.1076 -+  /* endorsement key */
  3.1077 -+  uint8_t ek_n[] =  "\xa8\xdb\xa9\x42\xa8\xf3\xb8\x06\x85\x90\x76\x93\xad\xf7"
  3.1078 -+    "\x74\xec\x3f\xd3\x3d\x9d\xe8\x2e\xff\x15\xed\x0e\xce\x5f\x93"
  3.1079 -+    "\x92\xeb\xd1\x96\x2b\x72\x18\x81\x79\x12\x9d\x9c\x40\xd7\x1a"
  3.1080 -+    "\x21\xda\x5f\x56\xe0\xc9\x48\x31\xdd\x96\xdc\xbb\x45\xc6\x8e"
  3.1081 -+    "\xad\x58\x23\xcb\xbe\xbb\x13\x2d\x6b\x86\xc5\x57\xf5\xdd\x48"
  3.1082 -+    "\xc1\x3d\xcd\x4d\xda\x81\xc4\x43\x17\xaa\x05\x40\x33\x62\x0a"
  3.1083 -+    "\x59\xdb\x28\xcd\xb5\x08\x31\xbb\x06\xf5\xf7\x71\xae\x21\xa8"
  3.1084 -+    "\xf2\x2f\x0e\x17\x80\x5d\x9c\xdf\xaa\xe9\x89\x09\x54\x65\x2b"
  3.1085 -+    "\x46\xfb\x9d\xb2\x00\x70\x63\x0d\x9a\x6d\x3d\x5e\x11\x78\x65"
  3.1086 -+    "\x90\xe6\x26\xee\x77\xbe\x08\xff\x07\x60\x5a\xcc\xf1\x0a\xbd"
  3.1087 -+    "\x44\x92\x6b\xca\xb6\xce\x66\xf9\x93\x40\xae\xf3\x3e\x53\x02"
  3.1088 -+    "\x3c\xa6\x81\xb3\xbe\xad\x6e\x6c\xa6\xf0\xeb\xdf\xe9\xa2\x83"
  3.1089 -+    "\x36\x0e\x52\x0d\x64\x17\xd9\xff\xa1\x74\x7c\x2b\xbc\x6a\xcc"
  3.1090 -+    "\xe5\x4e\xb4\x52\xd9\xec\x43\xbd\x26\x6a\x2b\x19\x19\x6e\x97"
  3.1091 -+    "\xb8\x1d\x9f\x7b\xe7\x32\x2d\xdd\x7c\x51\xc8\xe4\xf3\x02\xd4"
  3.1092 -+    "\x7c\x90\x44\xa0\x33\x72\x81\x75\xa9\x16\x27\x5c\x00\x1d\x07"
  3.1093 -+    "\x81\xd4\xf7\xac\xcb\xfe\xd6\x60\x03\x6f\x7a\xcc\x00\xd1\xc4"
  3.1094 -+    "\x85\x37";
  3.1095 -+  uint8_t ek_e[] = "\x01\x00\x01";
  3.1096 -+  uint8_t ek_p[] = "\xd7\xea\x61\x15\x8b\xa3\x71\xdf\xa8\x74\x77\xca\x88\x95"
  3.1097 -+    "\xd0\x76\x17\x43\x2c\xf6\x23\x27\x44\xb9\x0e\x18\x35\x7e\xe4"
  3.1098 -+    "\xc3\xcb\x13\x6e\xfc\x38\x02\x1e\x77\x26\x40\x9d\x17\xb2\x39"
  3.1099 -+    "\x9c\x7f\x5f\x98\xe6\xf2\x55\x0c\x12\x05\x4c\xb3\x51\xae\x29"
  3.1100 -+    "\xe7\xcd\xce\x41\x0b\x28\x4d\x97\x13\x4b\x60\xc8\xd8\x70\x81"
  3.1101 -+    "\xf9\x1c\x12\x44\xdf\x53\x0a\x87\x9d\x33\x92\x4a\x34\x69\xf0"
  3.1102 -+    "\x70\x5e\x1b\x5d\x65\xc7\x84\x90\xa2\x62\xdf\x83\x14\x10\x69"
  3.1103 -+    "\xe2\xa7\x18\x43\xd7\x1f\x60\xc9\x03\x8f\xd6\xa4\xce\xb2\x9d"
  3.1104 -+    "\x40\x37\x70\x17\x4c\xe3\x69\xd4\x59";
  3.1105 -+  uint8_t ek_q[] = "\xc8\x34\xd2\xd0\x7c\xfa\xdc\x68\xe2\x72\xd7\x92\xe2\x50"
  3.1106 -+    "\x93\xfc\xbb\x72\x55\x4d\x6b\x7a\x0c\x0b\xcf\x87\x66\x1f\x81"
  3.1107 -+    "\x71\xf3\x50\xcb\xaa\xe6\x43\x7e\xbe\x11\xc4\xec\x00\x53\xf4"
  3.1108 -+    "\x78\x13\x2b\x59\x26\x4a\x9f\x91\x61\x8f\xa7\x07\x64\x11\x5a"
  3.1109 -+    "\xf4\xaf\x9c\x9b\x5a\x5d\x69\x20\x17\x55\x74\xba\xd8\xe4\x59"
  3.1110 -+    "\x39\x1a\x0a\x7b\x4a\x30\xf0\xc8\x7f\xd9\xaf\x72\xc5\xb6\x71"
  3.1111 -+    "\xd1\xc0\x8b\x5b\xa2\x2e\xa7\x15\xca\x50\x75\x10\x48\x9c\x2b"
  3.1112 -+    "\x18\xb9\x67\x8f\x5d\x64\xc3\x28\x9f\x2f\x16\x2f\x08\xda\x47"
  3.1113 -+    "\xec\x86\x43\x0c\x80\x99\x07\x34\x0f";
  3.1114 -+  int i;
  3.1115 -+  /* reset all data to NULL, FALSE or 0 */
  3.1116 -+  memset(&tpmData, 0, sizeof(tpmData));
  3.1117 -+  tpmData.permanent.data.tag = TPM_TAG_PERMANENT_DATA;
  3.1118 -+  /* set permanent flags */
  3.1119 -+  tpmData.permanent.flags.tag = TPM_TAG_PERMANENT_FLAGS;
  3.1120 -+  tpmData.permanent.flags.disable = FALSE;
  3.1121 -+  tpmData.permanent.flags.deactivated = FALSE;
  3.1122 -+  tpmData.permanent.flags.ownership = TRUE;
  3.1123 -+  tpmData.permanent.flags.readPubek = TRUE;
  3.1124 -+  tpmData.permanent.flags.allowMaintenance = TRUE;
  3.1125 -+  tpmData.permanent.flags.enableRevokeEK = TRUE;
  3.1126 -+  /* set TPM vision */
  3.1127 -+  tpmData.permanent.data.version.major = 1;
  3.1128 -+  tpmData.permanent.data.version.minor = 2;
  3.1129 -+  tpmData.permanent.data.version.revMajor = VERSION_MAJOR;
  3.1130 -+  tpmData.permanent.data.version.revMinor = VERSION_MINOR;
  3.1131 -+  /* setup PCR attributes */
  3.1132 -+  for (i = 0; i < min(16, TPM_NUM_PCR); i++) {
  3.1133 -+    init_pcr_attr(i, FALSE, 0x00, 0x1f);
  3.1134 -+  }
  3.1135 -+  if (TPM_NUM_PCR >= 24) {
  3.1136 -+    init_pcr_attr(16, TRUE, 0x1f, 0x1f);
  3.1137 -+    init_pcr_attr(17, TRUE, 0x10, 0x1c);
  3.1138 -+    init_pcr_attr(18, TRUE, 0x10, 0x1c);
  3.1139 -+    init_pcr_attr(19, TRUE, 0x10, 0x0c);
  3.1140 -+    init_pcr_attr(20, TRUE, 0x14, 0x0e);
  3.1141 -+    init_pcr_attr(21, TRUE, 0x04, 0x04);
  3.1142 -+    init_pcr_attr(22, TRUE, 0x04, 0x04);
  3.1143 -+    init_pcr_attr(23, TRUE, 0x1f, 0x1f);
  3.1144 -+  }
  3.1145 -+  for (i = 24; i < TPM_NUM_PCR; i++) {
  3.1146 -+    init_pcr_attr(i, TRUE, 0x00, 0x00);
  3.1147 -+  }
  3.1148 -+  /* set tick type */
  3.1149 -+  tpmData.permanent.data.tickType = TICK_INC;
  3.1150 -+#ifdef TPM_GENERATE_EK
  3.1151 -+  /* generate a new endorsement key */
  3.1152 -+  rsa_generate_key(&tpmData.permanent.data.endorsementKey, 2048);
  3.1153 -+#else
  3.1154 -+  /* setup endorsement key */
  3.1155 -+  rsa_import_key(&tpmData.permanent.data.endorsementKey, 
  3.1156 -+    RSA_MSB_FIRST, ek_n, 256, ek_e, 3, ek_p, ek_q);
  3.1157 -+#endif
  3.1158 -+#ifdef TPM_GENERATE_SEED_DAA
  3.1159 -+  /* generate the DAA seed (cf. [TPM_Part2], v1.2 rev 85, Section 7.4) */
  3.1160 -+  tpm_get_random_bytes(tpmData.permanent.data.tpmDAASeed.digest, 
  3.1161 -+    sizeof(tpmData.permanent.data.tpmDAASeed.digest));
  3.1162 -+#else
  3.1163 -+  /* FIXME: setup DAA seed */
  3.1164 -+  memcpy(tpmData.permanent.data.tpmDAASeed.digest, 
  3.1165 -+    "\x77\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  3.1166 -+    "\x00\x00\x00\x77", 20);
  3.1167 -+#endif
  3.1168 -+
  3.1169 -+  memcpy(tpmData.permanent.data.ekReset.nonce, "\xde\xad\xbe\xef", 4);
  3.1170 -+}
  3.1171 -+
  3.1172 -+void tpm_release_data(void)
  3.1173 -+{
  3.1174 -+  int i;
  3.1175 -+  /* release the EK, SRK as well as all other rsa keys */
  3.1176 -+  if (tpmData.permanent.data.endorsementKey.size > 0)
  3.1177 -+    rsa_release_private_key(&tpmData.permanent.data.endorsementKey);
  3.1178 -+  if (tpmData.permanent.data.srk.valid)
  3.1179 -+    rsa_release_private_key(&tpmData.permanent.data.srk.key);
  3.1180 -+  for (i = 0; i < TPM_MAX_KEYS; i++)
  3.1181 -+    if (tpmData.permanent.data.keys[i].valid)
  3.1182 -+      rsa_release_private_key(&tpmData.permanent.data.keys[i].key);
  3.1183 -+}
  3.1184 -+
  3.1185 -+#ifdef TPM_STORE_TO_FILE
  3.1186 -+
  3.1187 -+#include <linux/fs.h>
  3.1188 -+#include <linux/unistd.h>
  3.1189 -+#include <asm/uaccess.h>
  3.1190 -+
  3.1191 -+#define TPM_STORAGE_FILE "/var/tpm/tpm_emulator-1.2." STR(VERSION_MAJOR) "." STR(VERSION_MINOR) 
  3.1192 -+
  3.1193 -+static int write_to_file(uint8_t *data, size_t data_length)
  3.1194 -+{
  3.1195 -+  int res;
  3.1196 -+  struct file *fp;
  3.1197 -+  mm_segment_t old_fs = get_fs();
  3.1198 -+  fp = filp_open(TPM_STORAGE_FILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
  3.1199 -+  if (IS_ERR(fp)) return -1;
  3.1200 -+  set_fs(get_ds());
  3.1201 -+  res = fp->f_op->write(fp, data, data_length, &fp->f_pos);
  3.1202 -+  set_fs(old_fs);
  3.1203 -+  filp_close(fp, NULL);
  3.1204 -+  return (res == data_length) ? 0 : -1;
  3.1205 -+}
  3.1206 -+
  3.1207 -+static int read_from_file(uint8_t **data, size_t *data_length)
  3.1208 -+{
  3.1209 -+  int res;
  3.1210 -+  struct file *fp;
  3.1211 -+  mm_segment_t old_fs = get_fs();
  3.1212 -+  fp = filp_open(TPM_STORAGE_FILE, O_RDONLY, 0);
  3.1213 -+  if (IS_ERR(fp)) return -1;
  3.1214 -+  *data_length = (size_t)fp->f_dentry->d_inode->i_size;
  3.1215 -+  /* *data_length = i_size_read(fp->f_dentry->d_inode); */
  3.1216 -+  *data = tpm_malloc(*data_length);
  3.1217 -+  if (*data == NULL) {
  3.1218 -+    filp_close(fp, NULL);
  3.1219 -+    return -1;
  3.1220 -+  }
  3.1221 -+  set_fs(get_ds());
  3.1222 -+  res = fp->f_op->read(fp, *data, *data_length, &fp->f_pos);
  3.1223 -+  set_fs(old_fs);
  3.1224 -+  filp_close(fp, NULL);
  3.1225 -+  if (res != *data_length) {
  3.1226 -+    tpm_free(*data);
  3.1227 -+    return -1;
  3.1228 -+  }
  3.1229 -+  return 0;
  3.1230 -+}
  3.1231 -+
  3.1232 -+#else
  3.1233 -+
  3.1234 -+static int write_to_file(uint8_t *data, size_t data_length)
  3.1235 -+{
  3.1236 -+  info("TPM_STORE_TO_FILE disabled, no data written");
  3.1237 -+  return 0;
  3.1238 -+}
  3.1239 -+
  3.1240 -+static int read_from_file(uint8_t **data, size_t *data_length)
  3.1241 -+{
  3.1242 -+  info("TPM_STORE_TO_FILE disabled, no data read");
  3.1243 -+  return 0;
  3.1244 -+}
  3.1245 -+
  3.1246 -+#endif /* TPM_STORE_TO_FILE */
  3.1247 -+
  3.1248 -+int tpm_store_permanent_data(void)
  3.1249 -+{
  3.1250 -+  uint8_t *buf, *ptr;
  3.1251 -+  UINT32 buf_length, len;
  3.1252 -+
  3.1253 -+  /* marshal data */
  3.1254 -+  buf_length = len = 4 + sizeof_TPM_STCLEAR_FLAGS(tpmData.stclear.flags)
  3.1255 -+    + sizeof_TPM_PERMANENT_FLAGS(tpmData.permanent.flags) 
  3.1256 -+    + sizeof_TPM_STANY_FLAGS(tpmData.stany.flags) + 2
  3.1257 -+    + sizeof_TPM_STCLEAR_DATA(tpmData.stclear.data) 
  3.1258 -+    + sizeof_TPM_PERMANENT_DATA(tpmData.permanent.data)
  3.1259 -+    + sizeof_TPM_STANY_DATA(tpmData.stany.data);
  3.1260 -+  buf = ptr = tpm_malloc(buf_length);
  3.1261 -+  if (buf == NULL
  3.1262 -+      || tpm_marshal_TPM_VERSION(&ptr, &len, &tpmData.permanent.data.version)
  3.1263 -+      || tpm_marshal_TPM_STCLEAR_FLAGS(&ptr, &len, &tpmData.stclear.flags)
  3.1264 -+      || tpm_marshal_TPM_PERMANENT_FLAGS(&ptr, &len, &tpmData.permanent.flags)
  3.1265 -+      || tpm_marshal_TPM_STANY_FLAGS(&ptr, &len, &tpmData.stany.flags)
  3.1266 -+      || tpm_marshal_BOOL(&ptr, &len, tpmData.permanent.flags.selfTestSucceeded)
  3.1267 -+      || tpm_marshal_BOOL(&ptr, &len, tpmData.permanent.flags.owned)
  3.1268 -+      || tpm_marshal_TPM_STCLEAR_DATA(&ptr, &len, &tpmData.stclear.data)
  3.1269 -+      || tpm_marshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)
  3.1270 -+      || tpm_marshal_TPM_STANY_DATA(&ptr, &len, &tpmData.stany.data)) {
  3.1271 -+    tpm_free(buf);
  3.1272 -+    return -1;
  3.1273 -+  }
  3.1274 -+
  3.1275 -+  if (write_to_file(buf, buf_length - len)) {
  3.1276 -+    tpm_free(buf);
  3.1277 -+    return -1; 
  3.1278 -+  }
  3.1279 -+  tpm_free(buf);
  3.1280 -+  return 0;
  3.1281 -+}
  3.1282 -+
  3.1283 -+int tpm_restore_permanent_data(void)
  3.1284 -+{
  3.1285 -+  uint8_t *buf, *ptr;
  3.1286 -+  size_t buf_length;
  3.1287 -+  UINT32 len;
  3.1288 -+  TPM_VERSION ver;
  3.1289 -+
  3.1290 -+  /* read data */
  3.1291 -+  if (read_from_file(&buf, &buf_length)) return -1;
  3.1292 -+  ptr = buf;
  3.1293 -+  len = (uint32_t) buf_length;
  3.1294 -+  /* unmarshal data */
  3.1295 -+  if (tpm_unmarshal_TPM_VERSION(&ptr, &len, &ver)
  3.1296 -+      || memcmp(&ver, &tpmData.permanent.data.version, sizeof(TPM_VERSION))
  3.1297 -+      || tpm_unmarshal_TPM_STCLEAR_FLAGS(&ptr, &len, &tpmData.stclear.flags)
  3.1298 -+      || tpm_unmarshal_TPM_PERMANENT_FLAGS(&ptr, &len, &tpmData.permanent.flags)
  3.1299 -+      || tpm_unmarshal_TPM_STANY_FLAGS(&ptr, &len, &tpmData.stany.flags)
  3.1300 -+      || tpm_unmarshal_BOOL(&ptr, &len, &tpmData.permanent.flags.selfTestSucceeded)
  3.1301 -+      || tpm_unmarshal_BOOL(&ptr, &len, &tpmData.permanent.flags.owned)
  3.1302 -+      || tpm_unmarshal_TPM_STCLEAR_DATA(&ptr, &len, &tpmData.stclear.data)
  3.1303 -+      || tpm_unmarshal_TPM_PERMANENT_DATA(&ptr, &len, &tpmData.permanent.data)
  3.1304 -+      || tpm_unmarshal_TPM_STANY_DATA(&ptr, &len, &tpmData.stany.data)) {
  3.1305 -+    tpm_free(buf);
  3.1306 -+    return -1;
  3.1307 -+  }
  3.1308 -+
  3.1309 -+  tpm_free(buf);
  3.1310 -+  return 0;
  3.1311 -+}
  3.1312 -+
  3.1313 -+int tpm_erase_permanent_data(void)
  3.1314 -+{
  3.1315 -+  int res = write_to_file("", 0);
  3.1316 -+  return res;
  3.1317 -+}
  3.1318 -+
  3.1319 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_deprecated.c tpm_emulator/tpm/tpm_deprecated.c
  3.1320 ---- tpm_emulator-0.3-x86_64/tpm/tpm_deprecated.c	2006-08-29 15:07:21.622298456 -0700
  3.1321 -+++ tpm_emulator/tpm/tpm_deprecated.c	2006-08-29 15:26:17.122676160 -0700
  3.1322 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_deprecated.c tpm_emulator/tpm/tpm_deprecated.c
  3.1323 +--- orig/tpm_emulator-0.4/tpm/tpm_deprecated.c	2006-06-23 03:37:07.000000000 -0700
  3.1324 ++++ tpm_emulator/tpm/tpm_deprecated.c	2006-07-24 14:35:35.000000000 -0700
  3.1325  @@ -1,6 +1,7 @@
  3.1326   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  3.1327    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  3.1328 @@ -1265,9 +991,9 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  3.1329                           authContextSize, &contextBlob);
  3.1330     if (res != TPM_SUCCESS) return res;
  3.1331     len = *authContextSize;
  3.1332 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_emulator.h tpm_emulator/tpm/tpm_emulator.h
  3.1333 ---- tpm_emulator-0.3-x86_64/tpm/tpm_emulator.h	2006-08-29 15:07:21.648294504 -0700
  3.1334 -+++ tpm_emulator/tpm/tpm_emulator.h	2006-08-29 15:26:17.122676160 -0700
  3.1335 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_emulator.h tpm_emulator/tpm/tpm_emulator.h
  3.1336 +--- orig/tpm_emulator-0.4/tpm/tpm_emulator.h	2006-06-23 03:37:07.000000000 -0700
  3.1337 ++++ tpm_emulator/tpm/tpm_emulator.h	2006-07-24 14:35:35.000000000 -0700
  3.1338  @@ -1,5 +1,6 @@
  3.1339   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  3.1340    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  3.1341 @@ -1285,45 +1011,476 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  3.1342   #undef  TPM_GENERATE_SEED_DAA
  3.1343   
  3.1344   #define TPM_MANUFACTURER 0x4554485A /* 'ETHZ' */        
  3.1345 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_integrity.c tpm_emulator/tpm/tpm_integrity.c
  3.1346 ---- tpm_emulator-0.3-x86_64/tpm/tpm_integrity.c	2006-08-29 15:07:21.645294960 -0700
  3.1347 -+++ tpm_emulator/tpm/tpm_integrity.c	2006-08-29 15:26:17.123676008 -0700
  3.1348 -@@ -1,6 +1,7 @@
  3.1349 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  3.1350 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  3.1351 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
  3.1352 -+ * Copyright (C) 2005 INTEL Corp
  3.1353 -  *
  3.1354 -  * This module is free software; you can redistribute it and/or modify
  3.1355 -  * it under the terms of the GNU General Public License as published
  3.1356 -@@ -194,4 +195,3 @@ TPM_RESULT tpm_verify_pcr(TPM_KEY_DATA *
  3.1357 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_marshalling.c tpm_emulator/tpm/tpm_marshalling.c
  3.1358 +--- orig/tpm_emulator-0.4/tpm/tpm_marshalling.c	2006-06-23 03:37:07.000000000 -0700
  3.1359 ++++ tpm_emulator/tpm/tpm_marshalling.c	2006-07-24 14:35:35.000000000 -0700
  3.1360 +@@ -1312,7 +1312,7 @@ int tpm_unmarshal_TPM_STANY_FLAGS(BYTE *
  3.1361 + 
  3.1362 + int tpm_marshal_RSA(BYTE **ptr, UINT32 *length, rsa_private_key_t *v)
  3.1363 + {
  3.1364 +-  UINT32 m_len, e_len, q_len;
  3.1365 ++  size_t m_len, e_len, q_len;
  3.1366 +   if (*length < sizeof_RSA((*v))) return -1;
  3.1367 +   if (v->size > 0) {
  3.1368 +     rsa_export_modulus(v, &(*ptr)[6], &m_len);
  3.1369 +@@ -1460,6 +1460,66 @@ int tpm_unmarshal_TPM_PERMANENT_DATA(BYT
  3.1370 +   return 0;
  3.1371 + }
  3.1372 + 
  3.1373 ++int tpm_marshal_TPM_STCLEAR_DATA(BYTE **ptr, UINT32 *length, TPM_STCLEAR_DATA *v)
  3.1374 ++{
  3.1375 ++  if (tpm_marshal_TPM_STRUCTURE_TAG(ptr, length, v->tag)
  3.1376 ++    || tpm_marshal_TPM_NONCE(ptr, length, &v->contextNonceKey)
  3.1377 ++    || tpm_marshal_TPM_COUNT_ID(ptr, length, v->countID) ) return -1;
  3.1378 ++
  3.1379 ++  return 0;
  3.1380 ++}
  3.1381 ++
  3.1382 ++int tpm_unmarshal_TPM_STCLEAR_DATA(BYTE **ptr, UINT32 *length, TPM_STCLEAR_DATA *v)
  3.1383 ++{
  3.1384 ++  if (tpm_unmarshal_TPM_STRUCTURE_TAG(ptr, length, &v->tag)
  3.1385 ++    || tpm_unmarshal_TPM_NONCE(ptr, length, &v->contextNonceKey)
  3.1386 ++    || tpm_unmarshal_TPM_COUNT_ID(ptr, length, &v->countID) ) return -1;
  3.1387 ++
  3.1388 ++  return 0;
  3.1389 ++}
  3.1390 ++
  3.1391 ++int tpm_marshal_TPM_STANY_DATA(BYTE **ptr, UINT32 *length, TPM_STANY_DATA *v)
  3.1392 ++{
  3.1393 ++  UINT32 i;
  3.1394 ++  if (tpm_marshal_TPM_STRUCTURE_TAG(ptr, length, v->tag)
  3.1395 ++    || tpm_marshal_TPM_NONCE(ptr, length, &v->contextNonceSession)
  3.1396 ++    || tpm_marshal_TPM_DIGEST(ptr, length, &v->auditDigest)
  3.1397 ++    || tpm_marshal_BOOL(ptr, length, v->auditSession)
  3.1398 ++    || tpm_marshal_TPM_CURRENT_TICKS(ptr, length, &v->currentTicks)
  3.1399 ++    || tpm_marshal_UINT32(ptr, length, v->contextCount)
  3.1400 ++    || tpm_marshal_UINT32_ARRAY(ptr, length, v->contextList, TPM_MAX_SESSION_LIST)) return -1;
  3.1401 ++  for (i = 0; i < TPM_MAX_SESSIONS; i++) {
  3.1402 ++    if (tpm_marshal_TPM_SESSION_DATA(ptr, length, &v->sessions[i])) return -1;
  3.1403 ++  }
  3.1404 ++  for (i = 0; i < TPM_MAX_SESSIONS_DAA; i++) {
  3.1405 ++    if (tpm_marshal_TPM_DAA_SESSION_DATA(ptr, length, &v->sessionsDAA[i])) return -1;
  3.1406 ++  }
  3.1407 ++  if (tpm_marshal_TPM_TRANSHANDLE(ptr, length, v->transExclusive)) return -1;
  3.1408 ++
  3.1409 ++  return 0;
  3.1410 ++}
  3.1411 ++
  3.1412 ++int tpm_unmarshal_TPM_STANY_DATA(BYTE **ptr, UINT32 *length, TPM_STANY_DATA *v)
  3.1413 ++{
  3.1414 ++  UINT32 i;
  3.1415 ++  if (tpm_unmarshal_TPM_STRUCTURE_TAG(ptr, length, &v->tag)
  3.1416 ++    || tpm_unmarshal_TPM_NONCE(ptr, length, &v->contextNonceSession)
  3.1417 ++    || tpm_unmarshal_TPM_DIGEST(ptr, length, &v->auditDigest)
  3.1418 ++    || tpm_unmarshal_BOOL(ptr, length, &v->auditSession)
  3.1419 ++    || tpm_unmarshal_TPM_CURRENT_TICKS(ptr, length, &v->currentTicks)
  3.1420 ++    || tpm_unmarshal_UINT32(ptr, length, &v->contextCount)
  3.1421 ++    || tpm_unmarshal_UINT32_ARRAY(ptr, length, v->contextList, TPM_MAX_SESSION_LIST)) return -1;
  3.1422 ++  for (i = 0; i < TPM_MAX_SESSIONS; i++) {
  3.1423 ++    if (tpm_unmarshal_TPM_SESSION_DATA(ptr, length, &v->sessions[i])) return -1;
  3.1424 ++  }
  3.1425 ++  for (i = 0; i < TPM_MAX_SESSIONS_DAA; i++) {
  3.1426 ++    if (tpm_unmarshal_TPM_DAA_SESSION_DATA(ptr, length, &v->sessionsDAA[i])) return -1;
  3.1427 ++  }
  3.1428 ++  if (tpm_unmarshal_TPM_TRANSHANDLE(ptr, length, &v->transExclusive)) return -1;
  3.1429 ++
  3.1430 ++  return 0;
  3.1431 ++}
  3.1432 ++
  3.1433 + int tpm_marshal_TPM_SESSION_DATA(BYTE **ptr, UINT32 *length, TPM_SESSION_DATA *v)
  3.1434 + {
  3.1435 +   if (tpm_marshal_BYTE(ptr, length, v->type)
  3.1436 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_marshalling.h tpm_emulator/tpm/tpm_marshalling.h
  3.1437 +--- orig/tpm_emulator-0.4/tpm/tpm_marshalling.h	2006-06-23 03:37:07.000000000 -0700
  3.1438 ++++ tpm_emulator/tpm/tpm_marshalling.h	2006-07-24 14:35:35.000000000 -0700
  3.1439 +@@ -432,6 +432,12 @@ int tpm_unmarshal_TPM_KEY_DATA(BYTE **pt
  3.1440 + int tpm_marshal_TPM_PERMANENT_DATA(BYTE **ptr, UINT32 *length, TPM_PERMANENT_DATA *);
  3.1441 + int tpm_unmarshal_TPM_PERMANENT_DATA(BYTE **ptr, UINT32 *length, TPM_PERMANENT_DATA *);
  3.1442 + 
  3.1443 ++int tpm_marshal_TPM_STCLEAR_DATA(BYTE **ptr, UINT32 *length, TPM_STCLEAR_DATA *v);
  3.1444 ++int tpm_unmarshal_TPM_STCLEAR_DATA(BYTE **ptr, UINT32 *length, TPM_STCLEAR_DATA *v);
  3.1445 ++
  3.1446 ++int tpm_marshal_TPM_STANY_DATA(BYTE **ptr, UINT32 *length, TPM_STANY_DATA *v);
  3.1447 ++int tpm_unmarshal_TPM_STANY_DATA(BYTE **ptr, UINT32 *length, TPM_STANY_DATA *v);
  3.1448 ++
  3.1449 + int tpm_marshal_TPM_SESSION_DATA(BYTE **ptr, UINT32 *length, TPM_SESSION_DATA *v);
  3.1450 + int tpm_unmarshal_TPM_SESSION_DATA(BYTE **ptr, UINT32 *length, TPM_SESSION_DATA *v);
  3.1451 + 
  3.1452 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_owner.c tpm_emulator/tpm/tpm_owner.c
  3.1453 +--- orig/tpm_emulator-0.4/tpm/tpm_owner.c	2006-06-23 03:37:07.000000000 -0700
  3.1454 ++++ tpm_emulator/tpm/tpm_owner.c	2006-07-24 14:35:35.000000000 -0700
  3.1455 +@@ -108,7 +108,7 @@ TPM_RESULT TPM_TakeOwnership(TPM_PROTOCO
  3.1456 +   TPM_RESULT res;
  3.1457 +   rsa_private_key_t *ek = &tpmData.permanent.data.endorsementKey;
  3.1458 +   TPM_KEY_DATA *srk = &tpmData.permanent.data.srk;
  3.1459 +-  UINT32 buf_size = ek->size >> 3;
  3.1460 ++  size_t buf_size = ek->size >> 3, key_length; 
  3.1461 +   BYTE buf[buf_size];
  3.1462 + 
  3.1463 +   info("TPM_TakeOwnership()");
  3.1464 +@@ -173,7 +173,8 @@ TPM_RESULT TPM_TakeOwnership(TPM_PROTOCO
  3.1465 +     return TPM_FAIL;
  3.1466     }
  3.1467 +   rsa_export_modulus(&srk->key, srkPub->pubKey.key,
  3.1468 +-    &srkPub->pubKey.keyLength);
  3.1469 ++		     &key_length);
  3.1470 ++  srkPub->pubKey.keyLength = (UINT32) key_length;
  3.1471 +   /* setup tpmProof and set state to owned */
  3.1472 +   tpm_get_random_bytes(tpmData.permanent.data.tpmProof.nonce, 
  3.1473 +     sizeof(tpmData.permanent.data.tpmProof.nonce));
  3.1474 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_startup.c tpm_emulator/tpm/tpm_startup.c
  3.1475 +--- orig/tpm_emulator-0.4/tpm/tpm_startup.c	2006-06-23 03:37:07.000000000 -0700
  3.1476 ++++ tpm_emulator/tpm/tpm_startup.c	2006-07-24 14:35:35.000000000 -0700
  3.1477 +@@ -41,26 +41,29 @@ void TPM_Init(TPM_STARTUP_TYPE startupTy
  3.1478 + TPM_RESULT TPM_Startup(TPM_STARTUP_TYPE startupType)
  3.1479 + {
  3.1480 +   int i;
  3.1481 ++  int restore_fail;
  3.1482 +   info("TPM_Startup(%d)", startupType);
  3.1483 +   if (tpmData.stany.flags.postInitialise == FALSE) return TPM_INVALID_POSTINIT;
  3.1484 +-  /* reset STANY_FLAGS */
  3.1485 +-  SET_TO_ZERO(&tpmData.stany.flags);
  3.1486 +-  tpmData.stany.flags.tag = TPM_TAG_STANY_FLAGS;
  3.1487 +-  /* reset STANY_DATA (invalidates ALL sessions) */
  3.1488 +-  SET_TO_ZERO(&tpmData.stany.data);
  3.1489 +-  tpmData.stany.data.tag = TPM_TAG_STANY_DATA;
  3.1490 +-  /* init session-context nonce */
  3.1491 +-  SET_TO_RAND(&tpmData.stany.data.contextNonceSession);
  3.1492 ++
  3.1493 ++  /* try and restore state to get EK, SRK, etc */
  3.1494 ++  restore_fail = tpm_restore_permanent_data();
  3.1495 ++
  3.1496 +   /* set data and flags according to the given startup type */
  3.1497 +   if (startupType == TPM_ST_CLEAR) {
  3.1498 +-    /* if available, restore permanent data */
  3.1499 +-    tpm_restore_permanent_data();
  3.1500 ++    /* reset STANY_FLAGS */
  3.1501 ++    SET_TO_ZERO(&tpmData.stany.flags);
  3.1502 ++    tpmData.stany.flags.tag = TPM_TAG_STANY_FLAGS;
  3.1503 ++    /* reset STANY_DATA (invalidates ALL sessions) */
  3.1504 ++    SET_TO_ZERO(&tpmData.stany.data);
  3.1505 ++    tpmData.stany.data.tag = TPM_TAG_STANY_DATA;
  3.1506 ++    /* init session-context nonce */
  3.1507 ++    SET_TO_RAND(&tpmData.stany.data.contextNonceSession);
  3.1508 +     /* reset PCR values */
  3.1509 +     for (i = 0; i < TPM_NUM_PCR; i++) {
  3.1510 +-      if (tpmData.permanent.data.pcrAttrib[i].pcrReset)
  3.1511 +-        SET_TO_ZERO(tpmData.permanent.data.pcrValue[i].digest);
  3.1512 ++      if (!tpmData.permanent.data.pcrAttrib[i].pcrReset)
  3.1513 ++        SET_TO_ZERO(&tpmData.permanent.data.pcrValue[i].digest);
  3.1514 +       else
  3.1515 +-        SET_TO_0xFF(tpmData.permanent.data.pcrValue[i].digest);
  3.1516 ++        SET_TO_0xFF(&tpmData.permanent.data.pcrValue[i].digest);
  3.1517 +     }
  3.1518 +     /* reset STCLEAR_FLAGS */
  3.1519 +     SET_TO_ZERO(&tpmData.stclear.flags);
  3.1520 +@@ -79,7 +82,8 @@ TPM_RESULT TPM_Startup(TPM_STARTUP_TYPE 
  3.1521 +     /* init key-context nonce */
  3.1522 +     SET_TO_RAND(&tpmData.stclear.data.contextNonceKey);
  3.1523 +   } else if (startupType == TPM_ST_STATE) {
  3.1524 +-    if (tpm_restore_permanent_data()) {
  3.1525 ++    /* restore must have been successful for TPM_ST_STATE */
  3.1526 ++    if (restore_fail) {
  3.1527 +       error("restoring permanent data failed");
  3.1528 +       tpmData.permanent.data.testResult = "tpm_restore_permanent_data() failed";
  3.1529 +       tpmData.permanent.flags.selfTestSucceeded = FALSE;
  3.1530 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_storage.c tpm_emulator/tpm/tpm_storage.c
  3.1531 +--- orig/tpm_emulator-0.4/tpm/tpm_storage.c	2006-06-23 03:37:07.000000000 -0700
  3.1532 ++++ tpm_emulator/tpm/tpm_storage.c	2006-07-24 14:35:35.000000000 -0700
  3.1533 +@@ -58,6 +58,7 @@ int encrypt_sealed_data(TPM_KEY_DATA *ke
  3.1534 +                         BYTE *enc, UINT32 *enc_size)
  3.1535 + {
  3.1536 +   UINT32 len;
  3.1537 ++  size_t enc_size32 = *enc_size;
  3.1538 +   BYTE *buf, *ptr;
  3.1539 +   rsa_public_key_t pub_key;
  3.1540 +   int scheme;
  3.1541 +@@ -72,7 +73,7 @@ int encrypt_sealed_data(TPM_KEY_DATA *ke
  3.1542 +   if (buf == NULL
  3.1543 +       || tpm_marshal_TPM_SEALED_DATA(&ptr, &len, seal)
  3.1544 +       || rsa_encrypt(&pub_key, scheme, buf, sizeof_TPM_SEALED_DATA((*seal)),
  3.1545 +-                     enc, enc_size)) {
  3.1546 ++                     enc, &enc_size32)) {
  3.1547 +     tpm_free(buf);
  3.1548 +     rsa_release_public_key(&pub_key);
  3.1549 +     return -1;
  3.1550 +@@ -85,7 +86,8 @@ int encrypt_sealed_data(TPM_KEY_DATA *ke
  3.1551 + int decrypt_sealed_data(TPM_KEY_DATA *key, BYTE *enc, UINT32 enc_size,
  3.1552 +                         TPM_SEALED_DATA *seal, BYTE **buf) 
  3.1553 + {
  3.1554 +-  UINT32 len;
  3.1555 ++  size_t len;
  3.1556 ++  UINT32 len32;
  3.1557 +   BYTE *ptr;
  3.1558 +   int scheme;
  3.1559 +   switch (key->encScheme) {
  3.1560 +@@ -96,8 +98,12 @@ int decrypt_sealed_data(TPM_KEY_DATA *ke
  3.1561 +   len = enc_size;
  3.1562 +   *buf = ptr = tpm_malloc(len);
  3.1563 +   if (*buf == NULL
  3.1564 +-      || rsa_decrypt(&key->key, scheme, enc, enc_size, *buf, &len)
  3.1565 +-      || tpm_unmarshal_TPM_SEALED_DATA(&ptr, &len, seal)) {
  3.1566 ++      || rsa_decrypt(&key->key, scheme, enc, enc_size, *buf, &len) ){
  3.1567 ++    tpm_free(*buf);
  3.1568 ++    return -1;
  3.1569 ++  }
  3.1570 ++  len32 = len;
  3.1571 ++  if (tpm_unmarshal_TPM_SEALED_DATA(&ptr, &len32, seal)) {
  3.1572 +     tpm_free(*buf);
  3.1573 +     return -1;
  3.1574 +   }
  3.1575 +@@ -240,11 +246,12 @@ TPM_RESULT TPM_Unseal(TPM_KEY_HANDLE par
  3.1576 + 
  3.1577 + TPM_RESULT TPM_UnBind(TPM_KEY_HANDLE keyHandle, UINT32 inDataSize,
  3.1578 +                       BYTE *inData, TPM_AUTH *auth1, 
  3.1579 +-                      UINT32 *outDataSize, BYTE **outData)
  3.1580 ++                      UINT32 *outDataSize32, BYTE **outData)
  3.1581 + {
  3.1582 +   TPM_RESULT res;
  3.1583 +   TPM_KEY_DATA *key;
  3.1584 +   int scheme;
  3.1585 ++  size_t outDataSize;
  3.1586 +   
  3.1587 +   info("TPM_UnBind()");
  3.1588 +   /* get key */
  3.1589 +@@ -262,8 +269,8 @@ TPM_RESULT TPM_UnBind(TPM_KEY_HANDLE key
  3.1590 +   /* the size of the input data muss be greater than zero */
  3.1591 +   if (inDataSize == 0) return TPM_BAD_PARAMETER;
  3.1592 +   /* decrypt data */
  3.1593 +-  *outDataSize = inDataSize;
  3.1594 +-  *outData = tpm_malloc(*outDataSize);
  3.1595 ++  outDataSize = inDataSize;
  3.1596 ++  *outData = tpm_malloc(outDataSize);
  3.1597 +   if (*outData == NULL) return TPM_NOSPACE;
  3.1598 +   switch (key->encScheme) {
  3.1599 +     case TPM_ES_RSAESOAEP_SHA1_MGF1: scheme = RSA_ES_OAEP_SHA1; break;
  3.1600 +@@ -271,20 +278,21 @@ TPM_RESULT TPM_UnBind(TPM_KEY_HANDLE key
  3.1601 +     default: tpm_free(*outData); return TPM_DECRYPT_ERROR;
  3.1602 +   }
  3.1603 +   if (rsa_decrypt(&key->key, scheme, inData, inDataSize, 
  3.1604 +-      *outData, outDataSize)) {
  3.1605 ++      *outData, &outDataSize)) {
  3.1606 +     tpm_free(*outData);
  3.1607 +     return TPM_DECRYPT_ERROR;
  3.1608 +   }
  3.1609 +   /* verify data if it is of type TPM_BOUND_DATA */
  3.1610 +   if (key->encScheme == TPM_ES_RSAESOAEP_SHA1_MGF1 
  3.1611 +       || key->keyUsage != TPM_KEY_LEGACY) {
  3.1612 +-    if (*outDataSize < 5 || memcmp(*outData, "\x01\x01\00\x00\x02", 5) != 0) {
  3.1613 ++    if (outDataSize < 5 || memcmp(*outData, "\x01\x01\00\x00\x02", 5) != 0) {
  3.1614 +       tpm_free(*outData);
  3.1615 +       return TPM_DECRYPT_ERROR;
  3.1616 +     }
  3.1617 +-    *outDataSize -= 5;
  3.1618 +-    memmove(*outData, &(*outData)[5], *outDataSize);
  3.1619 ++    outDataSize -= 5;
  3.1620 ++    memmove(*outData, &(*outData)[5], outDataSize);
  3.1621 +   }
  3.1622 ++  *outDataSize32 = (UINT32) outDataSize;
  3.1623     return TPM_SUCCESS;
  3.1624   }
  3.1625 --
  3.1626 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_structures.h tpm_emulator/tpm/tpm_structures.h
  3.1627 ---- tpm_emulator-0.3-x86_64/tpm/tpm_structures.h	2006-08-29 15:08:20.545340792 -0700
  3.1628 -+++ tpm_emulator/tpm/tpm_structures.h	2006-08-29 15:26:17.125675704 -0700
  3.1629 -@@ -1,6 +1,7 @@
  3.1630 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  3.1631 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  3.1632 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
  3.1633 -+ * Copyright (C) 2005 INTEL Corp
  3.1634 -  *
  3.1635 -  * This module is free software; you can redistribute it and/or modify
  3.1636 -  * it under the terms of the GNU General Public License as published
  3.1637 -@@ -18,7 +19,7 @@
  3.1638 - #ifndef _TPM_STRUCTURES_H_
  3.1639 - #define _TPM_STRUCTURES_H_
  3.1640   
  3.1641 --#include <linux/types.h>
  3.1642 -+//#include <linux/types.h>
  3.1643 - #include "crypto/rsa.h"
  3.1644 +@@ -334,12 +342,13 @@ int compute_pubkey_digest(TPM_PUBKEY *ke
  3.1645 + }
  3.1646 + 
  3.1647 + int encrypt_private_key(TPM_KEY_DATA *key, TPM_STORE_ASYMKEY *store,
  3.1648 +-                        BYTE *enc, UINT32 *enc_size)
  3.1649 ++                        BYTE *enc, UINT32 *enc_size32)
  3.1650 + {
  3.1651 +   UINT32 len;
  3.1652 +   BYTE *buf, *ptr;
  3.1653 +   rsa_public_key_t pub_key;
  3.1654 +   int scheme;
  3.1655 ++  size_t enc_size;
  3.1656 +   switch (key->encScheme) {
  3.1657 +     case TPM_ES_RSAESOAEP_SHA1_MGF1: scheme = RSA_ES_OAEP_SHA1; break;
  3.1658 +     case TPM_ES_RSAESPKCSv15: scheme = RSA_ES_PKCSV15; break;
  3.1659 +@@ -351,11 +360,12 @@ int encrypt_private_key(TPM_KEY_DATA *ke
  3.1660 +   if (buf == NULL
  3.1661 +       || tpm_marshal_TPM_STORE_ASYMKEY(&ptr, &len, store)
  3.1662 +       || rsa_encrypt(&pub_key, scheme, buf, sizeof_TPM_STORE_ASYMKEY((*store)),
  3.1663 +-                     enc, enc_size)) {
  3.1664 ++                     enc, &enc_size)) {
  3.1665 +     tpm_free(buf);
  3.1666 +     rsa_release_public_key(&pub_key);
  3.1667 +     return -1;
  3.1668 +   }
  3.1669 ++  *enc_size32 = (UINT32) enc_size;
  3.1670 +   tpm_free(buf);
  3.1671 +   rsa_release_public_key(&pub_key);
  3.1672 +   return 0;
  3.1673 +@@ -364,7 +374,8 @@ int encrypt_private_key(TPM_KEY_DATA *ke
  3.1674 + int decrypt_private_key(TPM_KEY_DATA *key, BYTE *enc, UINT32 enc_size, 
  3.1675 +                         TPM_STORE_ASYMKEY *store, BYTE **buf) 
  3.1676 + {
  3.1677 +-  UINT32 len;
  3.1678 ++  UINT32 len32;
  3.1679 ++  size_t len;
  3.1680 +   BYTE *ptr;
  3.1681 +   int scheme;
  3.1682 +   switch (key->encScheme) {
  3.1683 +@@ -375,8 +386,12 @@ int decrypt_private_key(TPM_KEY_DATA *ke
  3.1684 +   len = enc_size;
  3.1685 +   *buf = ptr = tpm_malloc(len);
  3.1686 +   if (*buf == NULL
  3.1687 +-      || rsa_decrypt(&key->key, scheme, enc, enc_size, *buf, &len)
  3.1688 +-      || tpm_unmarshal_TPM_STORE_ASYMKEY(&ptr, &len, store)) {
  3.1689 ++      || rsa_decrypt(&key->key, scheme, enc, enc_size, *buf, &len) ) {
  3.1690 ++    tpm_free(*buf);
  3.1691 ++    return -1;
  3.1692 ++  }
  3.1693 ++  len32 = (UINT32) len;
  3.1694 ++  if (tpm_unmarshal_TPM_STORE_ASYMKEY(&ptr, &len32, store)) {  
  3.1695 +     tpm_free(*buf);
  3.1696 +     return -1;
  3.1697 +   }
  3.1698 +@@ -394,7 +409,7 @@ TPM_RESULT TPM_CreateWrapKey(TPM_KEY_HAN
  3.1699 +   TPM_SESSION_DATA *session;
  3.1700 +   TPM_STORE_ASYMKEY store;
  3.1701 +   rsa_private_key_t rsa;
  3.1702 +-  UINT32 key_length;
  3.1703 ++  size_t key_length;
  3.1704 + 
  3.1705 +   info("TPM_CreateWrapKey()");
  3.1706 +   /* get parent key */
  3.1707 +@@ -450,11 +465,11 @@ TPM_RESULT TPM_CreateWrapKey(TPM_KEY_HAN
  3.1708 +     }
  3.1709 +   }
  3.1710 +   /* generate key and store it */
  3.1711 +-  key_length = keyInfo->algorithmParms.parms.rsa.keyLength;
  3.1712 +-  if (rsa_generate_key(&rsa, key_length)) return TPM_FAIL;
  3.1713 +-  wrappedKey->pubKey.keyLength = key_length >> 3;
  3.1714 ++  if (rsa_generate_key(&rsa, keyInfo->algorithmParms.parms.rsa.keyLength)) 
  3.1715 ++    return TPM_FAIL;
  3.1716 ++  wrappedKey->pubKey.keyLength = keyInfo->algorithmParms.parms.rsa.keyLength >> 3;
  3.1717 +   wrappedKey->pubKey.key = tpm_malloc(wrappedKey->pubKey.keyLength);
  3.1718 +-  store.privKey.keyLength = key_length >> 4;
  3.1719 ++  store.privKey.keyLength = keyInfo->algorithmParms.parms.rsa.keyLength >> 4;
  3.1720 +   store.privKey.key = tpm_malloc(store.privKey.keyLength);
  3.1721 +   wrappedKey->encDataSize = parent->key.size >> 3;
  3.1722 +   wrappedKey->encData = tpm_malloc(wrappedKey->encDataSize);
  3.1723 +@@ -466,9 +481,11 @@ TPM_RESULT TPM_CreateWrapKey(TPM_KEY_HAN
  3.1724 +     tpm_free(wrappedKey->encData);
  3.1725 +     return TPM_NOSPACE;
  3.1726 +   }
  3.1727 +-  rsa_export_modulus(&rsa, wrappedKey->pubKey.key, 
  3.1728 +-    &wrappedKey->pubKey.keyLength);
  3.1729 +-  rsa_export_prime1(&rsa, store.privKey.key, &store.privKey.keyLength);
  3.1730 ++  rsa_export_modulus(&rsa, wrappedKey->pubKey.key,
  3.1731 ++		     &key_length);
  3.1732 ++  wrappedKey->pubKey.keyLength = (UINT32) key_length;
  3.1733 ++  rsa_export_prime1(&rsa, store.privKey.key, &key_length);
  3.1734 ++  store.privKey.keyLength = (UINT32) key_length;
  3.1735 +   rsa_release_private_key(&rsa);
  3.1736 +   /* compute the digest of the wrapped key (without encData) */
  3.1737 +   if (compute_key_digest(wrappedKey, &store.pubDataDigest)) {
  3.1738 +@@ -602,6 +619,7 @@ TPM_RESULT TPM_LoadKey2(TPM_KEY_HANDLE p
  3.1739 + 
  3.1740 + int tpm_setup_key_parms(TPM_KEY_DATA *key, TPM_KEY_PARMS *parms)
  3.1741 + {
  3.1742 ++  size_t key_length;
  3.1743 +   parms->algorithmID = TPM_ALG_RSA;
  3.1744 +   parms->encScheme = key->encScheme;
  3.1745 +   parms->sigScheme = key->sigScheme;
  3.1746 +@@ -611,7 +629,8 @@ int tpm_setup_key_parms(TPM_KEY_DATA *ke
  3.1747 +   parms->parms.rsa.exponent = tpm_malloc(parms->parms.rsa.exponentSize);
  3.1748 +   if (parms->parms.rsa.exponent == NULL) return -1;
  3.1749 +   rsa_export_exponent(&key->key, parms->parms.rsa.exponent,
  3.1750 +-    &parms->parms.rsa.exponentSize);
  3.1751 ++    &key_length);
  3.1752 ++  parms->parms.rsa.exponentSize = (UINT32) key_length;
  3.1753 +   parms->parmSize = 12 + parms->parms.rsa.exponentSize;
  3.1754 +   return 0;
  3.1755 + }
  3.1756 +@@ -622,6 +641,7 @@ TPM_RESULT TPM_GetPubKey(TPM_KEY_HANDLE 
  3.1757 +   TPM_RESULT res;
  3.1758 +   TPM_KEY_DATA *key;
  3.1759 +   TPM_DIGEST digest;
  3.1760 ++  size_t key_length;
  3.1761 +   info("TPM_GetPubKey()");
  3.1762 +   /* get key */
  3.1763 +   if (keyHandle == TPM_KH_SRK
  3.1764 +@@ -650,8 +670,8 @@ TPM_RESULT TPM_GetPubKey(TPM_KEY_HANDLE 
  3.1765 +   pubKey->pubKey.keyLength = key->key.size >> 3;
  3.1766 +   pubKey->pubKey.key = tpm_malloc(pubKey->pubKey.keyLength);
  3.1767 +   if (pubKey->pubKey.key == NULL) return TPM_NOSPACE;
  3.1768 +-  rsa_export_modulus(&key->key, pubKey->pubKey.key, 
  3.1769 +-    &pubKey->pubKey.keyLength);
  3.1770 ++  rsa_export_modulus(&key->key, pubKey->pubKey.key, &key_length);
  3.1771 ++  pubKey->pubKey.keyLength = (UINT32) key_length;
  3.1772 +   if (tpm_setup_key_parms(key, &pubKey->algorithmParms) != 0) {
  3.1773 +     error("TPM_GetPubKey(): tpm_setup_key_parms() failed.");
  3.1774 +     tpm_free(pubKey->pubKey.key);
  3.1775 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_structures.h tpm_emulator/tpm/tpm_structures.h
  3.1776 +--- orig/tpm_emulator-0.4/tpm/tpm_structures.h	2006-06-23 03:37:07.000000000 -0700
  3.1777 ++++ tpm_emulator/tpm/tpm_structures.h	2006-07-24 14:35:35.000000000 -0700
  3.1778 +@@ -1958,6 +1958,7 @@ typedef struct tdTPM_DAA_ISSUER {
  3.1779 +   TPM_DIGEST DAA_digest_gamma;
  3.1780 +   BYTE DAA_generic_q[26];
  3.1781 + } TPM_DAA_ISSUER;
  3.1782 ++#define sizeof_TPM_DAA_ISSUER(s) (2 + (20 * 6) + 26 )
  3.1783   
  3.1784   /*
  3.1785 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_testing.c tpm_emulator/tpm/tpm_testing.c
  3.1786 ---- tpm_emulator-0.3-x86_64/tpm/tpm_testing.c	2006-08-29 15:07:21.646294808 -0700
  3.1787 -+++ tpm_emulator/tpm/tpm_testing.c	2006-08-29 15:26:17.127675400 -0700
  3.1788 +  * TPM_DAA_TPM ([TPM_Part2], Section 22.4)
  3.1789 +@@ -1973,6 +1974,7 @@ typedef struct tdTPM_DAA_TPM {
  3.1790 +   TPM_DIGEST DAA_rekey;
  3.1791 +   UINT32 DAA_count;
  3.1792 + } TPM_DAA_TPM;
  3.1793 ++#define sizeof_TPM_DAA_TPM(s) (2 + (4 * 20) + 4)
  3.1794 + 
  3.1795 + /*
  3.1796 +  * TPM_DAA_CONTEXT ([TPM_Part2], Section 22.5)
  3.1797 +@@ -1987,6 +1989,7 @@ typedef struct tdTPM_DAA_CONTEXT {
  3.1798 +   BYTE DAA_scratch[256];
  3.1799 +   BYTE DAA_stage;
  3.1800 + } TPM_DAA_CONTEXT;
  3.1801 ++#define sizeof_TPM_DAA_CONTEXT(s) (2 + (3 * 20) + 256 + 1)
  3.1802 + 
  3.1803 + /*
  3.1804 +  * TPM_DAA_JOINDATA ([TPM_Part2], Section 22.6)
  3.1805 +@@ -1998,6 +2001,7 @@ typedef struct tdTPM_DAA_JOINDATA {
  3.1806 +   BYTE DAA_join_u1[138];
  3.1807 +   TPM_DIGEST DAA_digest_n0;
  3.1808 + } TPM_DAA_JOINDATA;
  3.1809 ++#define sizeof_TPM_DAA_JOINDATA(s) (1 + 1 + 20)
  3.1810 + 
  3.1811 + /*
  3.1812 +  * TPM_DAA_BLOB ([TPM_Part2], Section 22.8)
  3.1813 +@@ -2202,6 +2206,7 @@ typedef struct tdTPM_STCLEAR_DATA {
  3.1814 +   //UINT32 ownerReference;
  3.1815 +   //BOOL disableResetLock;
  3.1816 + } TPM_STCLEAR_DATA;
  3.1817 ++#define sizeof_TPM_STCLEAR_DATA(s) (2 + 20 + 4)
  3.1818 + 
  3.1819 + /*
  3.1820 +  * TPM_SESSION_DATA
  3.1821 +@@ -2238,6 +2243,11 @@ typedef struct tdTPM_DAA_SESSION_DATA {
  3.1822 +   TPM_DAA_JOINDATA DAA_joinSession;
  3.1823 +   TPM_HANDLE handle;
  3.1824 + } TPM_DAA_SESSION_DATA;
  3.1825 ++#define sizeof_TPM_DAA_SESSION_DATA(s) ( 1 \
  3.1826 ++  + sizeof_TPM_DAA_ISSUER(s.DAA_issuerSettings) \
  3.1827 ++  + sizeof_TPM_DAA_TPM(s.DAA_tpmSpecific) \
  3.1828 ++  + sizeof_TPM_DAA_CONTEXT(s.DAA_session) \
  3.1829 ++  + sizeof_TPM_DAA_JOINDATA(s.DAA_joinSession) + 4)
  3.1830 + 
  3.1831 + /*
  3.1832 +  * TPM_STANY_DATA ([TPM_Part2], Section 7.6)
  3.1833 +@@ -2262,6 +2272,11 @@ typedef struct tdTPM_STANY_DATA {
  3.1834 +   TPM_DAAHANDLE currentDAA;
  3.1835 +   TPM_TRANSHANDLE transExclusive;
  3.1836 + } TPM_STANY_DATA;
  3.1837 ++#define sizeof_TPM_STANY_DATA(s) (2 + 20 + 20 + 1 \
  3.1838 ++  + sizeof_TPM_CURRENT_TICKS(s.currentTicks) \
  3.1839 ++  + 4 + (4 * TPM_MAX_SESSION_LIST) \
  3.1840 ++  + (sizeof_TPM_SESSION_DATA(s.sessions[0]) * TPM_MAX_SESSION_LIST) \
  3.1841 ++  + (sizeof_TPM_DAA_SESSION_DATA(s.sessionsDAA[0]) * TPM_MAX_SESSIONS_DAA) + 4)
  3.1842 + 
  3.1843 + /*
  3.1844 +  * TPM_DATA
  3.1845 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_testing.c tpm_emulator/tpm/tpm_testing.c
  3.1846 +--- orig/tpm_emulator-0.4/tpm/tpm_testing.c	2006-06-23 03:37:07.000000000 -0700
  3.1847 ++++ tpm_emulator/tpm/tpm_testing.c	2006-07-24 14:35:35.000000000 -0700
  3.1848  @@ -1,6 +1,7 @@
  3.1849   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  3.1850    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  3.1851 @@ -1439,9 +1596,9 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  3.1852     rsa_private_key_t priv_key;
  3.1853     rsa_public_key_t pub_key;
  3.1854   
  3.1855 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_ticks.c tpm_emulator/tpm/tpm_ticks.c
  3.1856 ---- tpm_emulator-0.3-x86_64/tpm/tpm_ticks.c	2006-08-29 15:07:21.646294808 -0700
  3.1857 -+++ tpm_emulator/tpm/tpm_ticks.c	2006-08-29 15:26:17.128675248 -0700
  3.1858 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_ticks.c tpm_emulator/tpm/tpm_ticks.c
  3.1859 +--- orig/tpm_emulator-0.4/tpm/tpm_ticks.c	2006-06-23 03:37:07.000000000 -0700
  3.1860 ++++ tpm_emulator/tpm/tpm_ticks.c	2006-07-24 14:35:35.000000000 -0700
  3.1861  @@ -1,6 +1,7 @@
  3.1862   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  3.1863    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  3.1864 @@ -1450,7 +1607,7 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  3.1865    *
  3.1866    * This module is free software; you can redistribute it and/or modify
  3.1867    * it under the terms of the GNU General Public License as published
  3.1868 -@@ -37,9 +38,7 @@ TPM_RESULT TPM_SetTickType(TPM_TICKTYPE 
  3.1869 +@@ -39,9 +40,7 @@ TPM_RESULT TPM_SetTickType(TPM_TICKTYPE 
  3.1870   TPM_RESULT TPM_GetTicks(TPM_CURRENT_TICKS *currentTime)
  3.1871   {
  3.1872     info("TPM_GetTicks()");
  3.1873 @@ -1461,7 +1618,7 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  3.1874   }
  3.1875   
  3.1876   TPM_RESULT TPM_TickStampBlob(TPM_KEY_HANDLE keyHandle, TPM_NONCE *antiReplay,
  3.1877 -@@ -47,61 +46,12 @@ TPM_RESULT TPM_TickStampBlob(TPM_KEY_HAN
  3.1878 +@@ -49,64 +48,11 @@ TPM_RESULT TPM_TickStampBlob(TPM_KEY_HAN
  3.1879                                TPM_CURRENT_TICKS *currentTicks, 
  3.1880                                UINT32 *sigSize, BYTE **sig)
  3.1881   {
  3.1882 @@ -1493,7 +1650,7 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  3.1883  -  }
  3.1884  -  memcpy(&info[0], "\x05\x00TSTP", 6);
  3.1885  -  memcpy(&info[6], antiReplay->nonce, 20);
  3.1886 --  *(UINT32*)&info[26] = cpu_to_be32(20
  3.1887 +-  *(UINT32*)&info[26] = CPU_TO_BE32(20
  3.1888  -                        + sizeof_TPM_CURRENT_TICKS(currentTicks));
  3.1889  -  memcpy(&info[30], digestToStamp->digest, sizeof(TPM_DIGEST));
  3.1890  -  p = &info[30 + sizeof(TPM_DIGEST)]; 
  3.1891 @@ -1513,91 +1670,45 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  3.1892  -  if (tpmData.stany.data.currentTicks.tag == 0) {
  3.1893  -    tpmData.stany.data.currentTicks.tag = TPM_TAG_CURRENT_TICKS;
  3.1894  -    tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();
  3.1895 +-/* removed since v1.2 rev 94
  3.1896  -    tpmData.stany.data.currentTicks.tickType = tpmData.permanent.data.tickType;
  3.1897 +-*/
  3.1898  -    tpm_get_random_bytes(tpmData.stany.data.currentTicks.tickNonce.nonce, 
  3.1899  -      sizeof(TPM_NONCE));
  3.1900  -    tpmData.stany.data.currentTicks.tickRate = 1;
  3.1901 +-/* removed since v1.2 rev 94
  3.1902  -    tpmData.stany.data.currentTicks.tickSecurity = TICK_SEC_NO_CHECK;
  3.1903 +-*/
  3.1904  -  } else {
  3.1905  -    tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();   
  3.1906  -  }
  3.1907   }
  3.1908 -   
  3.1909   
  3.1910 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_transport.c tpm_emulator/tpm/tpm_transport.c
  3.1911 ---- tpm_emulator-0.3-x86_64/tpm/tpm_transport.c	2006-08-29 15:07:21.647294656 -0700
  3.1912 -+++ tpm_emulator/tpm/tpm_transport.c	2006-08-29 15:26:17.129675096 -0700
  3.1913 -@@ -59,7 +59,7 @@ static int decrypt_transport_auth(TPM_KE
  3.1914 - static void transport_log_in(TPM_COMMAND_CODE ordinal, BYTE parameters[20],
  3.1915 -                              BYTE pubKeyHash[20], TPM_DIGEST *transDigest)
  3.1916 - {
  3.1917 --  UINT32 tag = cpu_to_be32(TPM_TAG_TRANSPORT_LOG_IN);
  3.1918 -+  UINT32 tag = CPU_TO_BE32(TPM_TAG_TRANSPORT_LOG_IN);
  3.1919 -   BYTE *ptr, buf[sizeof_TPM_TRANSPORT_LOG_IN(x)];
  3.1920 -   UINT32 len = sizeof(buf);
  3.1921 -   sha1_ctx_t sha1;
  3.1922 -@@ -76,7 +76,7 @@ static void transport_log_in(TPM_COMMAND
  3.1923 - static void transport_log_out(TPM_CURRENT_TICKS *currentTicks, BYTE parameters[20],
  3.1924 -                               TPM_MODIFIER_INDICATOR locality, TPM_DIGEST *transDigest)
  3.1925 - {
  3.1926 --  UINT32 tag = cpu_to_be32(TPM_TAG_TRANSPORT_LOG_OUT);
  3.1927 -+  UINT32 tag = CPU_TO_BE32(TPM_TAG_TRANSPORT_LOG_OUT);
  3.1928 -   BYTE *ptr, buf[sizeof_TPM_TRANSPORT_LOG_OUT(x)];
  3.1929 -   UINT32 len = sizeof(buf);
  3.1930 -   sha1_ctx_t sha1;
  3.1931 -@@ -191,7 +191,7 @@ static void decrypt_wrapped_command(BYTE
  3.1932 +diff -uprN orig/tpm_emulator-0.4/tpm/tpm_transport.c tpm_emulator/tpm/tpm_transport.c
  3.1933 +--- orig/tpm_emulator-0.4/tpm/tpm_transport.c	2006-06-23 03:37:07.000000000 -0700
  3.1934 ++++ tpm_emulator/tpm/tpm_transport.c	2006-07-24 14:35:35.000000000 -0700
  3.1935 +@@ -189,7 +189,7 @@ static void decrypt_wrapped_command(BYTE
  3.1936 +     sha1_init(&sha1);
  3.1937 +     sha1_update(&sha1, auth->nonceEven.nonce, sizeof(auth->nonceEven.nonce));
  3.1938       sha1_update(&sha1, auth->nonceOdd.nonce, sizeof(auth->nonceOdd.nonce));
  3.1939 -     sha1_update(&sha1, "in", 2);
  3.1940 -     sha1_update(&sha1, secret, sizeof(TPM_SECRET));
  3.1941 --    j = cpu_to_be32(i);
  3.1942 -+    j = CPU_TO_BE32(i);
  3.1943 -     sha1_update(&sha1, (BYTE*)&j, 4);
  3.1944 -     sha1_final(&sha1, mask);
  3.1945 -     for (j = 0; j < sizeof(mask) && buf_len > 0; j++) { 
  3.1946 -@@ -213,7 +213,7 @@ static void encrypt_wrapped_command(BYTE
  3.1947 -     sha1_update(&sha1, auth->nonceOdd.nonce, sizeof(auth->nonceOdd.nonce));
  3.1948 -     sha1_update(&sha1, "out", 3);
  3.1949 +-    sha1_update(&sha1, "in", 2);
  3.1950 ++    sha1_update(&sha1, (BYTE*)"in", 2);
  3.1951       sha1_update(&sha1, secret, sizeof(TPM_SECRET));
  3.1952 --    j = cpu_to_be32(i);
  3.1953 -+    j = CPU_TO_BE32(i);
  3.1954 +     j = CPU_TO_BE32(i);
  3.1955       sha1_update(&sha1, (BYTE*)&j, 4);
  3.1956 -     sha1_final(&sha1, mask);
  3.1957 -     for (j = 0; j < sizeof(mask) && buf_len > 0; j++) { 
  3.1958 -@@ -253,9 +253,9 @@ TPM_RESULT TPM_ExecuteTransport(UINT32 i
  3.1959 -   /* verify authorization */
  3.1960 -   tpm_compute_in_param_digest(&req);
  3.1961 -   sha1_init(&sha1);
  3.1962 --  res = cpu_to_be32(TPM_ORD_ExecuteTransport);
  3.1963 -+  res = CPU_TO_BE32(TPM_ORD_ExecuteTransport);
  3.1964 -   sha1_update(&sha1, (BYTE*)&res, 4);
  3.1965 --  res = cpu_to_be32(inWrappedCmdSize);
  3.1966 -+  res = CPU_TO_BE32(inWrappedCmdSize);
  3.1967 -   sha1_update(&sha1, (BYTE*)&res, 4);
  3.1968 -   sha1_update(&sha1, req.auth1.digest, sizeof(req.auth1.digest));
  3.1969 -   sha1_final(&sha1, auth1->digest);
  3.1970 -@@ -357,7 +357,7 @@ TPM_RESULT TPM_ReleaseTransportSigned(TP
  3.1971 -   /* setup a TPM_SIGN_INFO structure */
  3.1972 -   memcpy(&buf[0], "\x05\x00TRAN", 6);
  3.1973 -   memcpy(&buf[6], antiReplay->nonce, 20);
  3.1974 --  *(UINT32*)&buf[26] = cpu_to_be32(20);
  3.1975 -+  *(UINT32*)&buf[26] = CPU_TO_BE32(20);
  3.1976 -   memcpy(&buf[30], session->transInternal.transDigest.digest, 20);
  3.1977 -   /* sign info structure */ 
  3.1978 -   res = tpm_sign(key, auth1, TRUE, buf, sizeof(buf), signature, signSize);
  3.1979 -diff -uprN tpm_emulator-0.3-x86_64/tpm_version.h tpm_emulator/tpm_version.h
  3.1980 ---- tpm_emulator-0.3-x86_64/tpm_version.h	2006-08-29 15:07:21.649294352 -0700
  3.1981 -+++ tpm_emulator/tpm_version.h	1969-12-31 16:00:00.000000000 -0800
  3.1982 -@@ -1,6 +0,0 @@
  3.1983 --#ifndef _TPM_VERSION_H_
  3.1984 --#define _TPM_VERSION_H_
  3.1985 --#define VERSION_MAJOR 0
  3.1986 --#define VERSION_MINOR 3
  3.1987 --#define VERSION_BUILD 1136893683
  3.1988 --#endif /* _TPM_VERSION_H_ */
  3.1989 -diff -uprN tpm_emulator-0.3-x86_64/tpmd.c tpm_emulator/tpmd.c
  3.1990 ---- tpm_emulator-0.3-x86_64/tpmd.c	1969-12-31 16:00:00.000000000 -0800
  3.1991 -+++ tpm_emulator/tpmd.c	2006-08-29 15:26:17.130674944 -0700
  3.1992 -@@ -0,0 +1,141 @@
  3.1993 +@@ -211,7 +211,7 @@ static void encrypt_wrapped_command(BYTE
  3.1994 +     sha1_init(&sha1);
  3.1995 +     sha1_update(&sha1, auth->nonceEven.nonce, sizeof(auth->nonceEven.nonce));
  3.1996 +     sha1_update(&sha1, auth->nonceOdd.nonce, sizeof(auth->nonceOdd.nonce));
  3.1997 +-    sha1_update(&sha1, "out", 3);
  3.1998 ++    sha1_update(&sha1, (BYTE*)"out", 3);
  3.1999 +     sha1_update(&sha1, secret, sizeof(TPM_SECRET));
  3.2000 +     j = CPU_TO_BE32(i);
  3.2001 +     sha1_update(&sha1, (BYTE*)&j, 4);
  3.2002 +diff -uprN orig/tpm_emulator-0.4/tpmd.c tpm_emulator/tpmd.c
  3.2003 +--- orig/tpm_emulator-0.4/tpmd.c	1969-12-31 16:00:00.000000000 -0800
  3.2004 ++++ tpm_emulator/tpmd.c	2006-07-24 14:35:35.000000000 -0700
  3.2005 +@@ -0,0 +1,156 @@
  3.2006  +/* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  3.2007  + * Copyright (C) 2005 INTEL Corp
  3.2008  + *
  3.2009 @@ -1624,8 +1735,8 @@ diff -uprN tpm_emulator-0.3-x86_64/tpmd.
  3.2010  +
  3.2011  +#include "tpm_emulator.h"
  3.2012  +
  3.2013 -+#define TPM_RX_FNAME "/tmp/tpm_in.fifo"
  3.2014 -+#define TPM_TX_FNAME "/tmp/tpm_out.fifo"
  3.2015 ++#define TPM_RX_FNAME "/var/tpm/tpm_in.fifo"
  3.2016 ++#define TPM_TX_FNAME "/var/tpm/tpm_out.fifo"
  3.2017  +
  3.2018  +#define BUFFER_SIZE 2048
  3.2019  +
  3.2020 @@ -1656,7 +1767,8 @@ diff -uprN tpm_emulator-0.3-x86_64/tpmd.
  3.2021  +  uint32_t out_size;
  3.2022  +  int in_size, written;
  3.2023  +  int i;
  3.2024 -+ 
  3.2025 ++  struct stat file_info;
  3.2026 ++
  3.2027  +  int tpm_tx_fh=-1, tpm_rx_fh=-1;
  3.2028  +  if (argc < 2) {
  3.2029  +    printf("Usage: tpmd clear|save|deactivated\n" );
  3.2030 @@ -1679,6 +1791,20 @@ diff -uprN tpm_emulator-0.3-x86_64/tpmd.
  3.2031  +    return -1;
  3.2032  +  }
  3.2033  +
  3.2034 ++  if ( stat(TPM_RX_FNAME, &file_info) == -1) {
  3.2035 ++    if ( mkfifo(TPM_RX_FNAME, S_IWUSR | S_IRUSR ) ) {
  3.2036 ++      printf("Failed to create fifo %s.\n", TPM_RX_FNAME);
  3.2037 ++      return -1;
  3.2038 ++    }
  3.2039 ++  }
  3.2040 ++
  3.2041 ++  if ( stat(TPM_TX_FNAME, &file_info) == -1) {
  3.2042 ++    if ( mkfifo(TPM_TX_FNAME, S_IWUSR | S_IRUSR ) ) {
  3.2043 ++      printf("Failed to create fifo %s.\n", TPM_TX_FNAME);
  3.2044 ++      return -1;
  3.2045 ++    }
  3.2046 ++  }
  3.2047 ++
  3.2048  +  while (1) {
  3.2049  +abort_command:
  3.2050  +    if (tpm_rx_fh < 0) {
  3.2051 @@ -1739,3 +1865,14 @@ diff -uprN tpm_emulator-0.3-x86_64/tpmd.
  3.2052  +  close(tpm_rx_fh);
  3.2053  +
  3.2054  +}
  3.2055 +Binary files orig/tpm_emulator-0.4/tpm_emulator and tpm_emulator/tpm_emulator differ
  3.2056 +diff -uprN orig/tpm_emulator-0.4/tpm_version.h tpm_emulator/tpm_version.h
  3.2057 +--- orig/tpm_emulator-0.4/tpm_version.h	2006-06-23 03:37:07.000000000 -0700
  3.2058 ++++ tpm_emulator/tpm_version.h	2006-07-24 14:35:41.000000000 -0700
  3.2059 +@@ -2,5 +2,5 @@
  3.2060 + #define _TPM_VERSION_H_
  3.2061 + #define VERSION_MAJOR 0
  3.2062 + #define VERSION_MINOR 4
  3.2063 +-#define VERSION_BUILD 1151058734
  3.2064 ++#define VERSION_BUILD 1153776940
  3.2065 + #endif /* _TPM_VERSION_H_ */
     4.1 --- a/tools/vtpm/vtpm.patch	Tue Jul 25 11:19:48 2006 +0100
     4.2 +++ b/tools/vtpm/vtpm.patch	Tue Jul 25 11:30:30 2006 +0100
     4.3 @@ -1,478 +1,45 @@
     4.4 -diff -uprN tpm_emulator-0.3-x86_64/AUTHORS vtpm/AUTHORS
     4.5 ---- tpm_emulator-0.3-x86_64/AUTHORS	2006-08-29 15:07:21.618299064 -0700
     4.6 -+++ vtpm/AUTHORS	2006-08-29 15:12:07.184886344 -0700
     4.7 -@@ -1,2 +1,3 @@
     4.8 +diff -uprN tpm_emulator/AUTHORS vtpm/AUTHORS
     4.9 +--- tpm_emulator/AUTHORS	2006-07-24 14:35:35.000000000 -0700
    4.10 ++++ vtpm/AUTHORS	2006-07-24 14:35:35.000000000 -0700
    4.11 +@@ -1,3 +1,3 @@
    4.12   Mario Strasser <mast@gmx.net>
    4.13   Heiko Stamer <stamer@gaos.org> [DAA]
    4.14 +-INTEL Corp <> [Dropped to Ring3]
    4.15  +INTEL Corp <> [VTPM Extensions]
    4.16 -diff -uprN tpm_emulator-0.3-x86_64/ChangeLog vtpm/ChangeLog
    4.17 ---- tpm_emulator-0.3-x86_64/ChangeLog	2006-08-29 15:07:21.618299064 -0700
    4.18 -+++ vtpm/ChangeLog	2006-08-29 15:12:07.185886192 -0700
    4.19 -@@ -1,3 +1,7 @@
    4.20 -+2005-08-16 Intel Corp
    4.21 -+	* Moved module out of kernel to run as a ring 3 app
    4.22 -+	* Modified save_to_file and load_from_file to call a xen backend driver to call a VTPM manager
    4.23 -+
    4.24 - 2005-12-24  Mario Strasser <mast@gmx.net>
    4.25 - 	* tpm_transport.c, tpm_marshalling.c, tpm_structures.h:
    4.26 - 		Transport session functionality added
    4.27 -diff -uprN tpm_emulator-0.3-x86_64/Makefile vtpm/Makefile
    4.28 ---- tpm_emulator-0.3-x86_64/Makefile	2006-08-29 15:08:20.532342768 -0700
    4.29 -+++ vtpm/Makefile	2006-08-29 15:13:53.023796384 -0700
    4.30 -@@ -1,22 +1,31 @@
    4.31 - # Software-Based Trusted Platform Module (TPM) Emulator for Linux
    4.32 - # Copyright (C) 2004 Mario Strasser <mast@gmx.net>
    4.33 -+# Copyright (C) 2006 INTEL Corp.
    4.34 - #
    4.35 - # $Id: Makefile 69 2005-12-13 12:55:52Z mast $
    4.36 - 
    4.37 --# kernel settings
    4.38 --KERNEL_RELEASE := $(shell uname -r)
    4.39 --KERNEL_BUILD   := /lib/modules/$(KERNEL_RELEASE)/build
    4.40 --MOD_SUBDIR     := misc
    4.41 - COMPILE_ARCH    ?= $(shell uname -m | sed -e s/i.86/x86_32/)
    4.42 - 
    4.43 - # module settings
    4.44 --MODULE_NAME    := tpm_emulator
    4.45 -+BIN            := vtpmd
    4.46 - VERSION_MAJOR  := 0
    4.47 - VERSION_MINOR  := 3
    4.48 - VERSION_BUILD  := $(shell date +"%s")
    4.49 - 
    4.50 --# enable/disable DEBUG messages
    4.51 --EXTRA_CFLAGS   += -Wall -DDEBUG -g  
    4.52 -+# Installation program and options
    4.53 -+INSTALL         = install
    4.54 -+INSTALL_PROG    = $(INSTALL) -m0755
    4.55 -+INSTALL_DIR     = $(INSTALL) -d -m0755
    4.56 -+
    4.57 -+# Xen tools installation directory
    4.58 -+TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
    4.59 -+
    4.60 -+CC      := gcc
    4.61 -+CFLAGS  += -g -Wall $(INCLUDE) -DDEBUG
    4.62 -+CFLAGS  += -I. -Itpm -I../../vtpm_manager/manager
    4.63 -+
    4.64 -+# Is the simulator running in it's own vm?
    4.65 -+#CFLAGS += -DVTPM_MULTI_VM
    4.66 - 
    4.67 - ifeq ($(COMPILE_ARCH),x86_64)
    4.68 - LIBDIR = lib64
    4.69 -@@ -34,38 +43,31 @@ DIRS           := . crypto tpm 
    4.70 - SRCS           := $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.c))
    4.71 - OBJS           := $(patsubst %.c, %.o, $(SRCS))
    4.72 - SRCS           += $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.h))
    4.73 --DISTSRC        := ./README ./AUTHORS ./ChangeLog ./Makefile $(SRCS)
    4.74 --DISTDIR        := tpm_emulator-$(VERSION_MAJOR).$(VERSION_MINOR)
    4.75 - 
    4.76 --obj-m               := $(MODULE_NAME).o
    4.77 --$(MODULE_NAME)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
    4.78 -+obj-m               := $(BIN)
    4.79 -+$(BIN)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
    4.80 - 
    4.81 - EXTRA_CFLAGS   += -I$(src) -I$(src)/crypto -I$(src)/tpm 
    4.82 - 
    4.83 - # do not print "Entering directory ..."
    4.84 - MAKEFLAGS      += --no-print-directory
    4.85 - 
    4.86 --all:	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version
    4.87 --	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules
    4.88 -+all: $(BIN)
    4.89 -+
    4.90 -+$(BIN):	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version $(SRCS) $(OBJS)
    4.91 -+	$(CC) $(CFLAGS) $(OBJS) $(src)/crypto/libgmp.a -o $(BIN)
    4.92 -+
    4.93 -+%.o: %.c
    4.94 -+	$(CC) $(CFLAGS) -c $< -o $@
    4.95 - 
    4.96 --install:
    4.97 --	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules_install
    4.98 --	test -d /var/tpm || mkdir /var/tpm
    4.99 --	test -c /dev/tpm || mknod /dev/tpm c 10 224
   4.100 --	chmod 666 /dev/tpm
   4.101 --	depmod -a
   4.102 -+install: $(BIN)
   4.103 -+	$(INSTALL_PROG) $(BIN) $(TOOLS_INSTALL_DIR)
   4.104 - 
   4.105 - clean:
   4.106 --	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) clean
   4.107 --	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a
   4.108 -+	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a $(OBJS)
   4.109 - 
   4.110 --dist:	$(DISTSRC)
   4.111 --	rm -rf $(DISTDIR)
   4.112 --	mkdir $(DISTDIR)
   4.113 --	cp --parents $(DISTSRC) $(DISTDIR)/
   4.114 --	rm -f $(DISTDIR)/crypto/gmp.h 
   4.115 --	tar -chzf $(DISTDIR).tar.gz $(DISTDIR)
   4.116 --	rm -rf $(DISTDIR)
   4.117 -+mrproper: clean
   4.118 -+	rm -f $(BIN) tpm_version.h
   4.119 - 
   4.120 - $(src)/crypto/libgmp.a:
   4.121 - 	test -f $(src)/crypto/libgmp.a || ln -s $(GMP_LIB) $(src)/crypto/libgmp.a
   4.122 -diff -uprN tpm_emulator-0.3-x86_64/README vtpm/README
   4.123 ---- tpm_emulator-0.3-x86_64/README	2006-08-29 15:07:43.530967832 -0700
   4.124 -+++ vtpm/README	2006-08-29 15:12:07.190885432 -0700
   4.125 -@@ -13,7 +13,8 @@ $Id: README 78 2006-01-07 10:45:39Z mast
   4.126 - Copyright
   4.127 - --------------------------------------------------------------------------
   4.128 - Copyright (C) 2004 Mario Strasser <mast@gmx.net> and Swiss Federal 
   4.129 --Institute of Technology (ETH) Zurich.
   4.130 -+                   Institute of Technology (ETH) Zurich.
   4.131 -+Copyright (C) 2005 INTEL Corp 
   4.132 -               
   4.133 - This program is free software; you can redistribute it and/or modify
   4.134 - it under the terms of the GNU General Public License as published by
   4.135 -diff -uprN tpm_emulator-0.3-x86_64/crypto/gmp_kernel_wrapper.c vtpm/crypto/gmp_kernel_wrapper.c
   4.136 ---- tpm_emulator-0.3-x86_64/crypto/gmp_kernel_wrapper.c	2006-08-29 15:07:43.525968592 -0700
   4.137 -+++ vtpm/crypto/gmp_kernel_wrapper.c	2006-08-29 15:12:07.186886040 -0700
   4.138 -@@ -1,5 +1,6 @@
   4.139 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.140 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.141 -+ * Copyright (C) 2005 INTEL Corp
   4.142 -  *
   4.143 -  * This module is free software; you can redistribute it and/or modify
   4.144 -  * it under the terms of the GNU General Public License as published
   4.145 -@@ -24,15 +25,10 @@ int __gmp_junk;
   4.146 - void __attribute__ ((regparm(0))) __gmp_assert_fail(const char *filename, 
   4.147 -   int linenum, const char *expr) 
   4.148 - {
   4.149 --  panic(KERN_CRIT TPM_MODULE_NAME "%s:%d: GNU MP assertion failed: %s\n", 
   4.150 -+  error("%s:%d: GNU MP assertion failed: %s\n", 
   4.151 -     filename, linenum, expr);
   4.152 - }
   4.153 - 
   4.154 --void __attribute__ ((regparm(0))) abort(void)
   4.155 --{
   4.156 --  panic(KERN_CRIT TPM_MODULE_NAME "GNU MP abort() was called\n");
   4.157 --}
   4.158 --
   4.159 - /* overwrite GNU MP random functions (used by mpz/millerrabin.c) */ 
   4.160 - 
   4.161 - void __attribute__ ((regparm(0))) gmp_randinit(gmp_randstate_t rstate, 
   4.162 -@@ -77,20 +73,19 @@ void __attribute__ ((regparm(0))) mpz_ur
   4.163 - 
   4.164 - void __attribute__ ((regparm(0))) *kernel_allocate(size_t size)
   4.165 - {
   4.166 --  void *ret  = (void*)kmalloc(size, GFP_KERNEL);
   4.167 --  if (!ret) panic(KERN_CRIT TPM_MODULE_NAME 
   4.168 --    "GMP: cannot allocate memory (size=%Zu)\n", size);
   4.169 -+  void *ret  = (void*)malloc(size);
   4.170 -+  if (!ret) error("GMP: cannot allocate memory (size=%Zu)\n", size);
   4.171 -   return ret;
   4.172 - }
   4.173 - 
   4.174 - void __attribute__ ((regparm(0))) *kernel_reallocate(void *oldptr, 
   4.175 -   size_t old_size, size_t new_size)
   4.176 - {
   4.177 --  void *ret = (void*)kmalloc(new_size, GFP_KERNEL);
   4.178 --  if (!ret) panic(KERN_CRIT TPM_MODULE_NAME "GMP: Cannot reallocate memory "
   4.179 -+  void *ret = (void*)malloc(new_size);
   4.180 -+  if (!ret) error("GMP: Cannot reallocate memory "
   4.181 -     "(old_size=%Zu new_size=%Zu)\n", old_size, new_size);
   4.182 -   memcpy(ret, oldptr, old_size);
   4.183 --  kfree(oldptr);
   4.184 -+  free(oldptr);
   4.185 -   return ret;
   4.186 - }
   4.187 - 
   4.188 -@@ -99,7 +94,7 @@ void __attribute__ ((regparm(0))) kernel
   4.189 -   /* overwrite used memory */
   4.190 -   if (blk_ptr != NULL) { 
   4.191 -     memset(blk_ptr, 0, blk_size);
   4.192 --    kfree(blk_ptr);
   4.193 -+    free(blk_ptr);
   4.194 -   }
   4.195 - }
   4.196 - 
   4.197 -diff -uprN tpm_emulator-0.3-x86_64/crypto/rsa.c vtpm/crypto/rsa.c
   4.198 ---- tpm_emulator-0.3-x86_64/crypto/rsa.c	2006-08-29 15:07:21.618299064 -0700
   4.199 -+++ vtpm/crypto/rsa.c	2006-08-29 15:12:07.187885888 -0700
   4.200 +diff -uprN tpm_emulator/ChangeLog vtpm/ChangeLog
   4.201 +--- tpm_emulator/ChangeLog	2006-07-24 14:35:35.000000000 -0700
   4.202 ++++ vtpm/ChangeLog	2006-07-24 14:35:35.000000000 -0700
   4.203  @@ -1,5 +1,6 @@
   4.204 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.205 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.206 -+ * Copyright (C) 2005 INTEL Corp
   4.207 -  *
   4.208 -  * This module is free software; you can redistribute it and/or modify
   4.209 -  * it under the terms of the GNU General Public License as published
   4.210 -@@ -381,7 +382,7 @@ static int encode_message(int type, uint
   4.211 -       msg[0] = 0x00;
   4.212 -       get_random_bytes(&msg[1], SHA1_DIGEST_LENGTH);
   4.213 -       sha1_init(&ctx);
   4.214 --      sha1_update(&ctx, "TCPA", 4);
   4.215 -+      sha1_update(&ctx, (uint8_t *) "TCPA", 4);
   4.216 -       sha1_final(&ctx, &msg[1 + SHA1_DIGEST_LENGTH]);
   4.217 -       memset(&msg[1 + 2 * SHA1_DIGEST_LENGTH], 0x00, 
   4.218 -         msg_len - data_len - 2 * SHA1_DIGEST_LENGTH - 2);
   4.219 -@@ -429,7 +430,7 @@ static int decode_message(int type, uint
   4.220 -       mask_generation(&msg[1], SHA1_DIGEST_LENGTH,
   4.221 -         &msg[1 + SHA1_DIGEST_LENGTH], msg_len - SHA1_DIGEST_LENGTH - 1);
   4.222 -       sha1_init(&ctx);
   4.223 --      sha1_update(&ctx, "TCPA", 4);
   4.224 -+      sha1_update(&ctx, (uint8_t *) "TCPA", 4);
   4.225 -       sha1_final(&ctx, &msg[1]);
   4.226 -       if (memcmp(&msg[1], &msg[1 + SHA1_DIGEST_LENGTH], 
   4.227 -           SHA1_DIGEST_LENGTH) != 0) return -1;
   4.228 -diff -uprN tpm_emulator-0.3-x86_64/linux_module.c vtpm/linux_module.c
   4.229 ---- tpm_emulator-0.3-x86_64/linux_module.c	2006-08-29 15:07:43.526968440 -0700
   4.230 -+++ vtpm/linux_module.c	1969-12-31 16:00:00.000000000 -0800
   4.231 -@@ -1,194 +0,0 @@
   4.232 --/* Software-Based Trusted Platform Module (TPM) Emulator for Linux 
   4.233 -- * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.234 -- *
   4.235 -- * This module is free software; you can redistribute it and/or modify 
   4.236 -- * it under the terms of the GNU General Public License as published 
   4.237 -- * by the Free Software Foundation; either version 2 of the License, 
   4.238 -- * or (at your option) any later version.  
   4.239 -- *
   4.240 -- * This module is distributed in the hope that it will be useful, 
   4.241 -- * but WITHOUT ANY WARRANTY; without even the implied warranty of 
   4.242 -- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
   4.243 -- * GNU General Public License for more details.
   4.244 -- *
   4.245 -- * $Id: linux_module.c 76 2006-01-02 22:17:58Z hstamer $
   4.246 -- */
   4.247 --
   4.248 --#include <linux/module.h>
   4.249 --#include <linux/kernel.h>
   4.250 --#include <linux/init.h>
   4.251 --#include <linux/miscdevice.h>
   4.252 --#include <linux/poll.h>
   4.253 --#include "linux_module.h"
   4.254 --#include "tpm/tpm_emulator.h"
   4.255 --
   4.256 --MODULE_LICENSE("GPL");
   4.257 --MODULE_AUTHOR("Mario Strasser <mast@gmx.net>");
   4.258 --MODULE_DESCRIPTION("Trusted Platform Module (TPM) Emulator");
   4.259 --MODULE_SUPPORTED_DEVICE(TPM_DEVICE_NAME);
   4.260 --
   4.261 --/* module startup parameters */
   4.262 --char *startup = "save";
   4.263 --module_param(startup, charp, 0444);
   4.264 --MODULE_PARM_DESC(startup, " Sets the startup mode of the TPM. "
   4.265 --  "Possible values are 'clear', 'save' (default) and 'deactivated.");
   4.266 --char *storage_file = "/var/tpm/tpm_emulator-1.2.0.2";
   4.267 --module_param(storage_file, charp, 0644);
   4.268 --MODULE_PARM_DESC(storage_file, " Sets the persistent-data storage " 
   4.269 --  "file of the TPM.");
   4.270 --
   4.271 --/* TPM lock */
   4.272 --static struct semaphore tpm_mutex;
   4.273 --
   4.274 --/* TPM command response */
   4.275 --static struct {
   4.276 --  uint8_t *data;
   4.277 --  uint32_t size;
   4.278 --} tpm_response;
   4.279 --
   4.280 --/* module state */
   4.281 --#define STATE_IS_OPEN 0
   4.282 --static uint32_t module_state;
   4.283 --
   4.284 --static int tpm_open(struct inode *inode, struct file *file)
   4.285 --{
   4.286 --  debug("%s()", __FUNCTION__);
   4.287 --  if (test_and_set_bit(STATE_IS_OPEN, (void*)&module_state)) return -EBUSY;
   4.288 --  return 0;
   4.289 --}
   4.290 --
   4.291 --static int tpm_release(struct inode *inode, struct file *file)
   4.292 --{
   4.293 --  debug("%s()", __FUNCTION__);
   4.294 --  clear_bit(STATE_IS_OPEN, (void*)&module_state);
   4.295 --  down(&tpm_mutex);
   4.296 --  if (tpm_response.data != NULL) {
   4.297 --    kfree(tpm_response.data);
   4.298 --    tpm_response.data = NULL;
   4.299 --  }
   4.300 --  up(&tpm_mutex);
   4.301 --  return 0;
   4.302 --}
   4.303 --
   4.304 --static ssize_t tpm_read(struct file *file, char *buf, size_t count, loff_t *ppos)
   4.305 --{
   4.306 --  debug("%s(%Zu)", __FUNCTION__, count);
   4.307 --  down(&tpm_mutex);
   4.308 --  if (tpm_response.data != NULL) {
   4.309 --    count = min(count, (size_t)tpm_response.size - (size_t)*ppos);
   4.310 --    count -= copy_to_user(buf, &tpm_response.data[*ppos], count);
   4.311 --    *ppos += count;
   4.312 --    if ((size_t)tpm_response.size == (size_t)*ppos) {
   4.313 --      kfree(tpm_response.data);
   4.314 --      tpm_response.data = NULL;
   4.315 --    }
   4.316 --  } else {
   4.317 --    count = 0;
   4.318 --  }
   4.319 --  up(&tpm_mutex);
   4.320 --  return count;
   4.321 --}
   4.322 --
   4.323 --static ssize_t tpm_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
   4.324 --{
   4.325 --  debug("%s(%Zu)", __FUNCTION__, count);
   4.326 --  down(&tpm_mutex);
   4.327 --  *ppos = 0;
   4.328 --  if (tpm_response.data != NULL) kfree(tpm_response.data);
   4.329 --  if (tpm_handle_command(buf, count, &tpm_response.data, 
   4.330 --                         &tpm_response.size) != 0) { 
   4.331 --    count = -EILSEQ;
   4.332 --    tpm_response.data = NULL;
   4.333 --  }
   4.334 --  up(&tpm_mutex);
   4.335 --  return count;
   4.336 --}
   4.337 --
   4.338 --#define TPMIOC_CANCEL   _IO('T', 0x00)
   4.339 --#define TPMIOC_TRANSMIT _IO('T', 0x01)
   4.340 --
   4.341 --static int tpm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
   4.342 --{
   4.343 --  debug("%s(%d, %p)", __FUNCTION__, cmd, (char*)arg);
   4.344 --  if (cmd == TPMIOC_TRANSMIT) {
   4.345 --    uint32_t count = ntohl(*(uint32_t*)(arg + 2));
   4.346 --    down(&tpm_mutex);
   4.347 --    if (tpm_response.data != NULL) kfree(tpm_response.data);
   4.348 --    if (tpm_handle_command((char*)arg, count, &tpm_response.data,
   4.349 --                           &tpm_response.size) == 0) {
   4.350 --      tpm_response.size -= copy_to_user((char*)arg, tpm_response.data,
   4.351 --                            tpm_response.size);
   4.352 --      kfree(tpm_response.data);
   4.353 --      tpm_response.data = NULL;
   4.354 --    } else {
   4.355 --      tpm_response.size = 0;
   4.356 --      tpm_response.data = NULL;
   4.357 --    }
   4.358 --    up(&tpm_mutex);
   4.359 --    return tpm_response.size;
   4.360 --  }
   4.361 --  return -1;
   4.362 --}
   4.363 --
   4.364 --struct file_operations fops = {
   4.365 --  .owner   = THIS_MODULE,
   4.366 --  .open    = tpm_open,
   4.367 --  .release = tpm_release,
   4.368 --  .read    = tpm_read,
   4.369 --  .write   = tpm_write,
   4.370 --  .ioctl   = tpm_ioctl,
   4.371 --};
   4.372 --
   4.373 --static struct miscdevice tpm_dev = {
   4.374 --  .minor      = TPM_DEVICE_MINOR, 
   4.375 --  .name       = TPM_DEVICE_NAME, 
   4.376 --  .fops       = &fops,
   4.377 --};
   4.378 --
   4.379 --int __init init_tpm_module(void)
   4.380 --{
   4.381 --  int res = misc_register(&tpm_dev);
   4.382 --  if (res != 0) {
   4.383 --    error("misc_register() failed for minor %d\n", TPM_DEVICE_MINOR);
   4.384 --    return res;
   4.385 --  }
   4.386 --  /* initialize variables */
   4.387 --  sema_init(&tpm_mutex, 1);
   4.388 --  module_state = 0;
   4.389 --  tpm_response.data = NULL;    
   4.390 --  /* initialize TPM emulator */
   4.391 --  if (!strcmp(startup, "clear")) {
   4.392 --    tpm_emulator_init(1);
   4.393 --  } else if (!strcmp(startup, "save")) {
   4.394 --    tpm_emulator_init(2);
   4.395 --  } else if (!strcmp(startup, "deactivated")) {
   4.396 --    tpm_emulator_init(3);
   4.397 --  } else {
   4.398 --    error("invalid startup mode '%s'; must be 'clear', "
   4.399 --      "'save' (default) or 'deactivated", startup);
   4.400 --    misc_deregister(&tpm_dev);
   4.401 --    return -EINVAL;
   4.402 --  }
   4.403 --  return 0;
   4.404 --}
   4.405 --
   4.406 --void __exit cleanup_tpm_module(void)
   4.407 --{
   4.408 --  tpm_emulator_shutdown();
   4.409 --  misc_deregister(&tpm_dev);
   4.410 --  if (tpm_response.data != NULL) kfree(tpm_response.data);
   4.411 --}
   4.412 --
   4.413 --module_init(init_tpm_module);
   4.414 --module_exit(cleanup_tpm_module);
   4.415 --
   4.416 --uint64_t tpm_get_ticks(void)
   4.417 --{
   4.418 --  static struct timespec old_time = {0, 0};
   4.419 --  struct timespec new_time = current_kernel_time();
   4.420 --  uint64_t ticks = (uint64_t)(old_time.tv_sec - new_time.tv_sec) * 1000000
   4.421 --                   + (old_time.tv_nsec - new_time.tv_nsec) / 1000;
   4.422 --  old_time = new_time;
   4.423 --  return (ticks > 0) ? ticks : 1;
   4.424 --}
   4.425 --
   4.426 -diff -uprN tpm_emulator-0.3-x86_64/linux_module.h vtpm/linux_module.h
   4.427 ---- tpm_emulator-0.3-x86_64/linux_module.h	2006-08-29 15:07:43.527968288 -0700
   4.428 -+++ vtpm/linux_module.h	2006-08-29 15:12:07.189885584 -0700
   4.429 -@@ -1,5 +1,6 @@
   4.430 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.431 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.432 -+ * Copyright (C) 2005 INTEL Corp
   4.433 -  *
   4.434 -  * This module is free software; you can redistribute it and/or modify
   4.435 -  * it under the terms of the GNU General Public License as published
   4.436 -@@ -17,17 +18,22 @@
   4.437 - #ifndef _LINUX_MODULE_H_
   4.438 - #define _LINUX_MODULE_H_
   4.439 + ????-??-?? Intel Corp
   4.440 + 	* Moved module out of kernel to run as a ring 3 app
   4.441 ++	* Modified save_to_file and load_from_file to call xen VTPM manager
   4.442   
   4.443 --#include <linux/version.h>
   4.444 --#include <linux/kernel.h>
   4.445 --#include <linux/slab.h>
   4.446 -+#include <malloc.h>
   4.447 -+#include <stdint.h>
   4.448 -+#include <stdio.h>
   4.449 -+#include <string.h>
   4.450 - #include <linux/types.h>
   4.451 --#include <linux/string.h>
   4.452 --#include <linux/random.h>
   4.453 --#include <linux/time.h>
   4.454 --#include <asm/byteorder.h>
   4.455 + 2006-06-23  Mario Strasser <mast@gmx.net>
   4.456 + 	* tpm_startup.c: behaviour of ST_CLEAR and storage of
   4.457 +diff -uprN tpm_emulator/linux_module.h vtpm/linux_module.h
   4.458 +--- tpm_emulator/linux_module.h	2006-07-24 14:35:35.000000000 -0700
   4.459 ++++ vtpm/linux_module.h	2006-07-24 14:35:35.000000000 -0700
   4.460 +@@ -44,18 +44,21 @@
   4.461 + #define TPM_DEVICE_NAME   "tpm"
   4.462 + #define TPM_MODULE_NAME   "tpm_emulator"
   4.463   
   4.464 --/* module settings */
   4.465 -+#include <endian.h>
   4.466 -+#define __BYTEORDER_HAS_U64__
   4.467 -+#ifdef LITTLE_ENDIAN
   4.468 -+ #include <linux/byteorder/little_endian.h>
   4.469 -+#else
   4.470 -+ #include <linux/byteorder/big_endian.h>
   4.471 -+#endif
   4.472 - 
   4.473 -+/* module settings */
   4.474 -+#define min(A,B) ((A)<(B)?(A):(B))
   4.475 - #ifndef STR
   4.476 - #define STR(s) __STR__(s)
   4.477 - #define __STR__(s) #s
   4.478 -@@ -39,34 +45,38 @@
   4.479 - #define TPM_MODULE_NAME 	"tpm_emulator"
   4.480 - 
   4.481 - /* debug and log output functions */
   4.482 ++/* debug and log output functions */
   4.483  +extern int dmi_id; 
   4.484 - 
   4.485 ++
   4.486   #ifdef DEBUG
   4.487 --#define debug(fmt, ...) printk(KERN_DEBUG "%s %s:%d: Debug: " fmt "\n", \
   4.488 --                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   4.489 +-#define debug(fmt, ...) printf("TPMD: %s:%d: Debug: " fmt "\n", \
   4.490 +-                        __FILE__, __LINE__, ## __VA_ARGS__)
   4.491  +#define debug(fmt, ...) printf("TPMD[%d]: %s:%d: Debug: " fmt "\n", \
   4.492  +                        dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
   4.493   #else
   4.494   #define debug(fmt, ...) 
   4.495   #endif
   4.496 --#define info(fmt, ...)  printk(KERN_INFO "%s %s:%d: Info: " fmt "\n", \
   4.497 --                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   4.498 --#define error(fmt, ...) printk(KERN_ERR "%s %s:%d: Error: " fmt "\n", \
   4.499 --                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   4.500 --#define alert(fmt, ...) printk(KERN_ALERT "%s %s:%d: Alert: " fmt "\n", \
   4.501 --                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   4.502 +-#define info(fmt, ...)  printf("TPMD: %s:%d: Info: " fmt "\n", \
   4.503 +-                        __FILE__, __LINE__, ## __VA_ARGS__)
   4.504 +-#define error(fmt, ...) printf("TPMD: %s:%d: Error: " fmt "\n", \
   4.505 +-                        __FILE__, __LINE__, ## __VA_ARGS__)
   4.506 +-#define alert(fmt, ...) printf("TPMD: %s:%d: Alert: " fmt "\n", \
   4.507 +-                        __FILE__, __LINE__, ## __VA_ARGS__)
   4.508  +#define info(fmt, ...)  printf("TPMD[%d]: %s:%d: Info: " fmt "\n", \
   4.509  +                        dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
   4.510  +#define error(fmt, ...) printf("TPMD[%d]: %s:%d: Error: " fmt "\n", \
   4.511 @@ -482,393 +49,43 @@ diff -uprN tpm_emulator-0.3-x86_64/linux
   4.512   
   4.513   /* memory allocation */
   4.514   
   4.515 - static inline void *tpm_malloc(size_t size) 
   4.516 - {
   4.517 --  return kmalloc(size, GFP_KERNEL);  
   4.518 -+  return malloc(size);  
   4.519 - }
   4.520 - 
   4.521 - static inline void tpm_free(const void *ptr)
   4.522 - {
   4.523 --  if (ptr != NULL) kfree(ptr);
   4.524 -+  if (ptr != NULL) free( (void *) ptr);
   4.525 - }
   4.526 - 
   4.527 - /* random numbers */
   4.528 - 
   4.529 -+//FIXME;
   4.530 -+void get_random_bytes(void *buf, int nbytes);
   4.531 -+
   4.532 - static inline void tpm_get_random_bytes(void *buf, int nbytes)
   4.533 - {
   4.534 -   get_random_bytes(buf, nbytes);
   4.535 -@@ -86,9 +96,9 @@ uint64_t tpm_get_ticks(void);
   4.536 - #define CPU_TO_LE16(x) __cpu_to_le16(x)
   4.537 - 
   4.538 - #define BE64_TO_CPU(x) __be64_to_cpu(x)
   4.539 --#define LE64_TO_CPU(x) __be64_to_cpu(x)
   4.540 -+#define LE64_TO_CPU(x) __le64_to_cpu(x)
   4.541 - #define BE32_TO_CPU(x) __be32_to_cpu(x)
   4.542 --#define LE32_TO_CPU(x) __be32_to_cpu(x)
   4.543 -+#define LE32_TO_CPU(x) __le32_to_cpu(x)
   4.544 - #define BE16_TO_CPU(x) __be16_to_cpu(x)
   4.545 - #define LE16_TO_CPU(x) __le16_to_cpu(x)
   4.546 - 
   4.547 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_audit.c vtpm/tpm/tpm_audit.c
   4.548 ---- tpm_emulator-0.3-x86_64/tpm/tpm_audit.c	2006-08-29 15:07:21.620298760 -0700
   4.549 -+++ vtpm/tpm/tpm_audit.c	2006-08-29 15:12:07.191885280 -0700
   4.550 -@@ -1,6 +1,7 @@
   4.551 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.552 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.553 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   4.554 -+ * Copyright (C) 2005 INTEL Corp
   4.555 -  *
   4.556 -  * This module is free software; you can redistribute it and/or modify
   4.557 -  * it under the terms of the GNU General Public License as published
   4.558 -@@ -45,14 +46,14 @@ void tpm_audit_request(TPM_COMMAND_CODE 
   4.559 -       tpmData.permanent.data.auditMonotonicCounter++;
   4.560 -     }
   4.561 -     /* update audit digest */
   4.562 --    *((UINT16*)&buf[0])  = cpu_to_be16(TPM_TAG_AUDIT_EVENT_IN);
   4.563 --    *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
   4.564 -+    *((UINT16*)&buf[0])  = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_IN);
   4.565 -+    *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
   4.566 -     sha1_init(&sha1_ctx);
   4.567 -     sha1_update(&sha1_ctx, req->param, req->paramSize);
   4.568 -     sha1_final(&sha1_ctx, &buf[6]);
   4.569 --    *((UINT16*)&buf[26])  = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
   4.570 -+    *((UINT16*)&buf[26])  = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
   4.571 -     memset(&buf[30], 0, 4);
   4.572 --    *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
   4.573 -+    *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
   4.574 -     sha1_init(&sha1_ctx);
   4.575 -     sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest, 
   4.576 -       sizeof(TPM_DIGEST));
   4.577 -@@ -70,15 +71,15 @@ void tpm_audit_response(TPM_COMMAND_CODE
   4.578 -       && (AUDIT_STATUS[ord / 8] & (1 << (ord & 0x07)))) {
   4.579 -     info("tpm_audit_response()");
   4.580 -     /* update audit digest */
   4.581 --    *((UINT16*)&buf[0])  = cpu_to_be16(TPM_TAG_AUDIT_EVENT_OUT);
   4.582 --    *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
   4.583 -+    *((UINT16*)&buf[0])  = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_OUT);
   4.584 -+    *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
   4.585 -     sha1_init(&sha1_ctx);
   4.586 -     sha1_update(&sha1_ctx, rsp->param, rsp->paramSize);
   4.587 -     sha1_final(&sha1_ctx, &buf[6]);
   4.588 --    *((UINT16*)&buf[26])  = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
   4.589 -+    *((UINT16*)&buf[26])  = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
   4.590 -     memset(&buf[30], 0, 4);
   4.591 --    *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
   4.592 --    *((UINT32*)&buf[34]) = cpu_to_be32(rsp->result);
   4.593 -+    *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
   4.594 -+    *((UINT32*)&buf[34]) = CPU_TO_BE32(rsp->result);
   4.595 -     sha1_init(&sha1_ctx);
   4.596 -     sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest, 
   4.597 -       sizeof(TPM_DIGEST));
   4.598 -@@ -158,7 +159,7 @@ TPM_RESULT TPM_GetAuditDigestSigned(TPM_
   4.599 -   }
   4.600 -   memcpy(&buf[0], "\x05\x00ADIG", 6);
   4.601 -   memcpy(&buf[6], antiReplay->nonce, 20);
   4.602 --  *(UINT32*)&buf[26] = cpu_to_be32(buf_size - 30);
   4.603 -+  *(UINT32*)&buf[26] = CPU_TO_BE32(buf_size - 30);
   4.604 -   memcpy(&buf[30], auditDigest->digest, 20);
   4.605 -   ptr = &buf[50];
   4.606 -   len = buf_size - 50;
   4.607 -@@ -198,4 +199,3 @@ TPM_RESULT TPM_SetOrdinalAuditStatus(TPM
   4.608 -   }
   4.609 -   return TPM_SUCCESS;
   4.610 - }
   4.611 --
   4.612 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_authorization.c vtpm/tpm/tpm_authorization.c
   4.613 ---- tpm_emulator-0.3-x86_64/tpm/tpm_authorization.c	2006-08-29 15:07:21.620298760 -0700
   4.614 -+++ vtpm/tpm/tpm_authorization.c	2006-08-29 15:12:07.192885128 -0700
   4.615 -@@ -1,6 +1,7 @@
   4.616 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.617 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.618 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   4.619 -+ * Copyright (C) 2005 INTEL Corp
   4.620 -  *
   4.621 -  * This module is free software; you can redistribute it and/or modify
   4.622 -  * it under the terms of the GNU General Public License as published
   4.623 -@@ -279,7 +280,7 @@ TPM_RESULT tpm_verify_auth(TPM_AUTH *aut
   4.624 - {
   4.625 -   hmac_ctx_t ctx;
   4.626 -   TPM_SESSION_DATA *session;
   4.627 --  UINT32 auth_handle = cpu_to_be32(auth->authHandle);
   4.628 -+  UINT32 auth_handle = CPU_TO_BE32(auth->authHandle);
   4.629 -   
   4.630 -   info("tpm_verify_auth(%08x)", auth->authHandle);
   4.631 -   /* get dedicated authorization or transport session */
   4.632 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_capability.c vtpm/tpm/tpm_capability.c
   4.633 ---- tpm_emulator-0.3-x86_64/tpm/tpm_capability.c	2006-08-29 15:07:21.620298760 -0700
   4.634 -+++ vtpm/tpm/tpm_capability.c	2006-08-29 15:12:07.193884976 -0700
   4.635 -@@ -1,6 +1,7 @@
   4.636 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.637 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.638 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   4.639 -+ * Copyright (C) 2005 INTEL Corp
   4.640 -  *
   4.641 -  * This module is free software; you can redistribute it and/or modify
   4.642 -  * it under the terms of the GNU General Public License as published
   4.643 -@@ -406,7 +407,7 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
   4.644 - 
   4.645 -     case TPM_CAP_KEY_HANDLE:
   4.646 -       debug("[TPM_CAP_KEY_HANDLE]");
   4.647 --      subCapSize = cpu_to_be32(TPM_RT_KEY);
   4.648 -+      subCapSize = CPU_TO_BE32(TPM_RT_KEY);
   4.649 -       return cap_handle(4, (BYTE*)&subCapSize, respSize, resp);
   4.650 - 
   4.651 -     case TPM_CAP_CHECK_LOADED:
   4.652 -@@ -480,4 +481,3 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
   4.653 -       return TPM_BAD_MODE;
   4.654 -   }
   4.655 - }
   4.656 --
   4.657 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_cmd_handler.c vtpm/tpm/tpm_cmd_handler.c
   4.658 ---- tpm_emulator-0.3-x86_64/tpm/tpm_cmd_handler.c	2006-08-29 15:07:21.621298608 -0700
   4.659 -+++ vtpm/tpm/tpm_cmd_handler.c	2006-08-29 15:12:07.197884368 -0700
   4.660 -@@ -1,6 +1,7 @@
   4.661 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.662 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.663 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   4.664 -+ * Copyright (C) 2005 INTEL Corp
   4.665 -  *
   4.666 -  * This module is free software; you can redistribute it and/or modify
   4.667 -  * it under the terms of the GNU General Public License as published
   4.668 -@@ -73,7 +74,7 @@ void tpm_compute_in_param_digest(TPM_REQ
   4.669 - {
   4.670 -   sha1_ctx_t sha1;
   4.671 -   UINT32 offset = tpm_get_param_offset(req->ordinal);
   4.672 --  UINT32 ord = cpu_to_be32(req->ordinal);
   4.673 -+  UINT32 ord = CPU_TO_BE32(req->ordinal);
   4.674 - 
   4.675 -   /* compute SHA1 hash */
   4.676 -   if (offset <= req->paramSize) {
   4.677 -@@ -89,8 +90,8 @@ void tpm_compute_in_param_digest(TPM_REQ
   4.678 - void tpm_compute_out_param_digest(TPM_COMMAND_CODE ordinal, TPM_RESPONSE *rsp)
   4.679 - {
   4.680 -   sha1_ctx_t sha1;
   4.681 --  UINT32 res = cpu_to_be32(rsp->result);
   4.682 --  UINT32 ord = cpu_to_be32(ordinal);
   4.683 -+  UINT32 res = CPU_TO_BE32(rsp->result);
   4.684 -+  UINT32 ord = CPU_TO_BE32(ordinal);
   4.685 +diff -uprN tpm_emulator/Makefile vtpm/Makefile
   4.686 +--- tpm_emulator/Makefile	2006-07-24 14:35:35.000000000 -0700
   4.687 ++++ vtpm/Makefile	2006-07-24 14:35:35.000000000 -0700
   4.688 +@@ -7,7 +7,7 @@
   4.689 + COMPILE_ARCH    ?= $(shell uname -m | sed -e s/i.86/x86_32/)
   4.690   
   4.691 -   /* compute SHA1 hash */
   4.692 -   sha1_init(&sha1);
   4.693 -@@ -3123,7 +3124,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
   4.694 -       hmac_update(&hmac, rsp->auth2->digest, sizeof(rsp->auth2->digest));
   4.695 - #if 0
   4.696 -       if (tpm_get_auth(rsp->auth2->authHandle)->type == TPM_ST_OIAP) {
   4.697 --        UINT32 handle = cpu_to_be32(rsp->auth2->authHandle);
   4.698 -+        UINT32 handle = CPU_TO_BE32(rsp->auth2->authHandle);
   4.699 -         hmac_update(&hmac, (BYTE*)&handle, 4);
   4.700 -       }
   4.701 - #endif
   4.702 -@@ -3138,7 +3139,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
   4.703 -       hmac_update(&hmac, rsp->auth1->digest, sizeof(rsp->auth1->digest));
   4.704 - #if 0
   4.705 -       if (tpm_get_auth(rsp->auth1->authHandle)->type == TPM_ST_OIAP) {
   4.706 --        UINT32 handle = cpu_to_be32(rsp->auth1->authHandle);
   4.707 -+        UINT32 handle = CPU_TO_BE32(rsp->auth1->authHandle);
   4.708 -         hmac_update(&hmac, (BYTE*)&handle, 4);
   4.709 -       }
   4.710 - #endif
   4.711 -@@ -3221,7 +3222,9 @@ extern const char *tpm_error_to_string(T
   4.712 - void tpm_execute_command(TPM_REQUEST *req, TPM_RESPONSE *rsp)
   4.713 - {
   4.714 -   TPM_RESULT res;
   4.715 --  
   4.716 -+
   4.717 -+  req->tag = (BYTE) req->tag;  // FIXME: Why is this here
   4.718 + # module settings
   4.719 +-BIN            := tpm_emulator
   4.720 ++BIN            := vtpmd
   4.721 + VERSION_MAJOR  := 0
   4.722 + VERSION_MINOR  := 4
   4.723 + VERSION_BUILD  := $(shell date +"%s")
   4.724 +@@ -22,7 +22,7 @@ TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
   4.725 + 
   4.726 + CC      := gcc
   4.727 + CFLAGS  += -g -Wall $(INCLUDE) -DDEBUG
   4.728 +-CFLAGS  += -I. -Itpm
   4.729 ++CFLAGS  += -I. -Itpm -I../../vtpm_manager/manager
   4.730 + 
   4.731 + # Is the simulator running in it's own vm?
   4.732 + #CFLAGS += -DVTPM_MULTI_VM
   4.733 +@@ -62,7 +62,6 @@ $(BIN):	$(src)/crypto/gmp.h $(src)/crypt
   4.734 + 
   4.735 + install: $(BIN)
   4.736 + 	$(INSTALL_PROG) $(BIN) $(TOOLS_INSTALL_DIR)
   4.737 +-	@if [ ! -d "/var/tpm" ]; then mkdir /var/tpm; fi
   4.738 + 
   4.739 + clean:
   4.740 + 	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a $(OBJS)
   4.741 +@@ -98,3 +97,4 @@ version:
   4.742 + 	@echo "#endif /* _TPM_VERSION_H_ */" >> $(src)/tpm_version.h
   4.743 + 
   4.744 + .PHONY: all install clean dist gmp version
   4.745  +
   4.746 -   /* setup authorisation as well as response tag and size */
   4.747 -   memset(rsp, 0, sizeof(*rsp));
   4.748 -   switch (req->tag) {
   4.749 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_crypto.c vtpm/tpm/tpm_crypto.c
   4.750 ---- tpm_emulator-0.3-x86_64/tpm/tpm_crypto.c	2006-08-29 15:07:43.531967680 -0700
   4.751 -+++ vtpm/tpm/tpm_crypto.c	2006-08-29 15:12:07.198884216 -0700
   4.752 -@@ -1,6 +1,7 @@
   4.753 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.754 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.755 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
   4.756 -+ * Copyright (C) 2005 INTEL Corp
   4.757 -  *
   4.758 -  * This module is free software; you can redistribute it and/or modify
   4.759 -  * it under the terms of the GNU General Public License as published
   4.760 -@@ -106,7 +107,7 @@ TPM_RESULT tpm_sign(TPM_KEY_DATA *key, T
   4.761 -     /* setup TPM_SIGN_INFO structure */
   4.762 -     memcpy(&buf[0], "\x05\x00SIGN", 6);
   4.763 -     memcpy(&buf[6], auth->nonceOdd.nonce, 20);
   4.764 --    *(UINT32*)&buf[26] = cpu_to_be32(areaToSignSize);
   4.765 -+    *(UINT32*)&buf[26] = CPU_TO_BE32(areaToSignSize);
   4.766 -     memcpy(&buf[30], areaToSign, areaToSignSize);
   4.767 -     if (rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1, 
   4.768 -         buf, areaToSignSize + 30, *sig)) {
   4.769 -@@ -383,4 +384,3 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
   4.770 -   }
   4.771 -   return TPM_SUCCESS;
   4.772 - }
   4.773 --
   4.774 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_daa.c vtpm/tpm/tpm_daa.c
   4.775 ---- tpm_emulator-0.3-x86_64/tpm/tpm_daa.c	2006-08-29 15:07:21.622298456 -0700
   4.776 -+++ vtpm/tpm/tpm_daa.c	2006-08-29 15:12:07.203883456 -0700
   4.777 -@@ -700,14 +700,14 @@ info("tested until here");
   4.778 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.779 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.780 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.781 --      sha1_update(&sha1, "\x00", 1);
   4.782 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   4.783 -       sha1_final(&sha1, scratch);
   4.784 -       sha1_init(&sha1);
   4.785 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   4.786 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.787 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.788 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.789 --      sha1_update(&sha1, "\x01", 1);
   4.790 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.791 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   4.792 -       mpz_init(f), mpz_init(q);
   4.793 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   4.794 -@@ -787,14 +787,14 @@ info("tested until here");
   4.795 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.796 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.797 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.798 --      sha1_update(&sha1, "\x00", 1);
   4.799 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   4.800 -       sha1_final(&sha1, scratch);
   4.801 -       sha1_init(&sha1);
   4.802 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   4.803 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.804 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.805 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.806 --      sha1_update(&sha1, "\x01", 1);
   4.807 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.808 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   4.809 -       mpz_init(f), mpz_init(q);
   4.810 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   4.811 -@@ -1440,14 +1440,14 @@ info("tested until here");
   4.812 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.813 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.814 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.815 --      sha1_update(&sha1, "\x00", 1);
   4.816 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   4.817 -       sha1_final(&sha1, scratch);
   4.818 -       sha1_init(&sha1);
   4.819 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   4.820 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.821 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.822 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.823 --      sha1_update(&sha1, "\x01", 1);
   4.824 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.825 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   4.826 -       mpz_init(f), mpz_init(q);
   4.827 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   4.828 -@@ -1660,14 +1660,14 @@ info("tested until here");
   4.829 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.830 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.831 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.832 --      sha1_update(&sha1, "\x00", 1);
   4.833 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   4.834 -       sha1_final(&sha1, scratch);
   4.835 -       sha1_init(&sha1);
   4.836 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   4.837 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.838 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.839 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.840 --      sha1_update(&sha1, "\x01", 1);
   4.841 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.842 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   4.843 -       mpz_init(f), mpz_init(q);
   4.844 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   4.845 -@@ -1740,14 +1740,14 @@ info("tested until here");
   4.846 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.847 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.848 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.849 --      sha1_update(&sha1, "\x00", 1);
   4.850 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   4.851 -       sha1_final(&sha1, scratch);
   4.852 -       sha1_init(&sha1);
   4.853 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   4.854 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.855 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.856 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.857 --      sha1_update(&sha1, "\x01", 1);
   4.858 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.859 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   4.860 -       mpz_init(f), mpz_init(q);
   4.861 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   4.862 -@@ -2828,14 +2828,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   4.863 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.864 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.865 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.866 --      sha1_update(&sha1, "\x00", 1);
   4.867 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   4.868 -       sha1_final(&sha1, scratch);
   4.869 -       sha1_init(&sha1);
   4.870 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   4.871 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.872 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.873 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.874 --      sha1_update(&sha1, "\x01", 1);
   4.875 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.876 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   4.877 -       mpz_init(f), mpz_init(q);
   4.878 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   4.879 -@@ -3050,7 +3050,7 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   4.880 -         sha1_init(&sha1);
   4.881 -         sha1_update(&sha1, (BYTE*) &session->DAA_session.DAA_digest, 
   4.882 -           sizeof(session->DAA_session.DAA_digest));
   4.883 --        sha1_update(&sha1, "\x01", 1);
   4.884 -+        sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.885 -         sha1_update(&sha1, inputData1, inputSize1);
   4.886 -         sha1_final(&sha1, (BYTE*) &session->DAA_session.DAA_digest);
   4.887 -       }
   4.888 -@@ -3078,7 +3078,7 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   4.889 -         sha1_init(&sha1);
   4.890 -         sha1_update(&sha1, (BYTE*) &session->DAA_session.DAA_digest, 
   4.891 -           sizeof(session->DAA_session.DAA_digest));
   4.892 --        sha1_update(&sha1, "\x01", 1);
   4.893 -+        sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.894 -         rsa_export_modulus(&aikData->key, scratch, &size);
   4.895 -         sha1_update(&sha1, scratch, size);
   4.896 -         sha1_final(&sha1, (BYTE*) &session->DAA_session.DAA_digest);
   4.897 -@@ -3134,14 +3134,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   4.898 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.899 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.900 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.901 --      sha1_update(&sha1, "\x00", 1);
   4.902 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   4.903 -       sha1_final(&sha1, scratch);
   4.904 -       sha1_init(&sha1);
   4.905 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   4.906 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.907 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.908 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.909 --      sha1_update(&sha1, "\x01", 1);
   4.910 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.911 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   4.912 -       mpz_init(f), mpz_init(q);
   4.913 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   4.914 -@@ -3213,14 +3213,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   4.915 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.916 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.917 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.918 --      sha1_update(&sha1, "\x00", 1);
   4.919 -+      sha1_update(&sha1, (BYTE *) "\x00", 1);
   4.920 -       sha1_final(&sha1, scratch);
   4.921 -       sha1_init(&sha1);
   4.922 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   4.923 -           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   4.924 -       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   4.925 -           sizeof(session->DAA_tpmSpecific.DAA_count));
   4.926 --      sha1_update(&sha1, "\x01", 1);
   4.927 -+      sha1_update(&sha1, (BYTE *) "\x01", 1);
   4.928 -       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   4.929 -       mpz_init(f), mpz_init(q);
   4.930 -       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   4.931 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_data.c vtpm/tpm/tpm_data.c
   4.932 ---- tpm_emulator-0.3-x86_64/tpm/tpm_data.c	2006-08-29 15:08:20.535342312 -0700
   4.933 -+++ vtpm/tpm/tpm_data.c	2006-08-29 15:12:07.206883000 -0700
   4.934 +diff -uprN tpm_emulator/tpm/tpm_data.c vtpm/tpm/tpm_data.c
   4.935 +--- tpm_emulator/tpm/tpm_data.c	2006-07-24 14:35:35.000000000 -0700
   4.936 ++++ vtpm/tpm/tpm_data.c	2006-07-24 14:35:35.000000000 -0700
   4.937  @@ -1,6 +1,7 @@
   4.938   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   4.939    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   4.940 @@ -878,7 +95,7 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   4.941    * This module is free software; you can redistribute it and/or modify
   4.942    * it under the terms of the GNU General Public License as published
   4.943  @@ -15,10 +16,15 @@
   4.944 -  * $Id: tpm_data.c 36 2005-10-26 20:31:19Z hstamer $
   4.945 +  * $Id: tpm_data.c 98 2006-05-07 14:16:29Z hstamer $
   4.946    */
   4.947   
   4.948  +#include <sys/types.h>
   4.949 @@ -894,37 +111,15 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   4.950   
   4.951   TPM_DATA tpmData;
   4.952   
   4.953 -@@ -39,6 +45,7 @@ static inline void init_pcr_attr(int pcr
   4.954 - 
   4.955 - void tpm_init_data(void)
   4.956 - {
   4.957 -+#ifndef TPM_GENERATE_EK
   4.958 -   /* endorsement key */
   4.959 -   uint8_t ek_n[] =  "\xa8\xdb\xa9\x42\xa8\xf3\xb8\x06\x85\x90\x76\x93\xad\xf7"
   4.960 -     "\x74\xec\x3f\xd3\x3d\x9d\xe8\x2e\xff\x15\xed\x0e\xce\x5f\x93"
   4.961 -@@ -77,6 +84,8 @@ void tpm_init_data(void)
   4.962 -     "\xd1\xc0\x8b\x5b\xa2\x2e\xa7\x15\xca\x50\x75\x10\x48\x9c\x2b"
   4.963 -     "\x18\xb9\x67\x8f\x5d\x64\xc3\x28\x9f\x2f\x16\x2f\x08\xda\x47"
   4.964 -     "\xec\x86\x43\x0c\x80\x99\x07\x34\x0f";
   4.965 -+#endif
   4.966 -+
   4.967 -   int i;
   4.968 -   /* reset all data to NULL, FALSE or 0 */
   4.969 -   memset(&tpmData, 0, sizeof(tpmData));
   4.970 -@@ -150,49 +159,235 @@ void tpm_release_data(void)
   4.971 - 
   4.972 - #ifdef TPM_STORE_TO_FILE
   4.973 - 
   4.974 --#include <linux/fs.h>
   4.975 --#include <linux/unistd.h>
   4.976 --#include <asm/uaccess.h>
   4.977 -+#include <sys/types.h>
   4.978 -+#include <sys/stat.h>
   4.979 -+#include <fcntl.h>
   4.980 -+
   4.981 -+ static int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
   4.982 +@@ -158,45 +164,232 @@ void tpm_release_data(void)
   4.983 + #include <sys/types.h>
   4.984 + #include <sys/stat.h>
   4.985 + #include <fcntl.h>
   4.986 +-#include <unistd.h>
   4.987   
   4.988  -#define TPM_STORAGE_FILE "/var/tpm/tpm_emulator-1.2." STR(VERSION_MAJOR) "." STR(VERSION_MINOR) 
   4.989 ++ static int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
   4.990 ++
   4.991  +#ifdef VTPM_MUTLI_VM
   4.992  + #define DEV_FE "/dev/tpm"
   4.993  +#else
   4.994 @@ -938,14 +133,10 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
   4.995   static int write_to_file(uint8_t *data, size_t data_length)
   4.996   {
   4.997  -  int res;
   4.998 --  struct file *fp;
   4.999 --  mm_segment_t old_fs = get_fs();
  4.1000 --  fp = filp_open(TPM_STORAGE_FILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
  4.1001 --  if (IS_ERR(fp)) return -1;
  4.1002 --  set_fs(get_ds());
  4.1003 --  res = fp->f_op->write(fp, data, data_length, &fp->f_pos);
  4.1004 --  set_fs(old_fs);
  4.1005 --  filp_close(fp, NULL);
  4.1006 +-  int fp;
  4.1007 +-  fp = open(TPM_STORAGE_FILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
  4.1008 +-  res = write(fp, data, data_length);
  4.1009 +-  close(fp);
  4.1010  -  return (res == data_length) ? 0 : -1;
  4.1011  +  int res, out_data_size, in_header_size;
  4.1012  +  BYTE *ptr, *out_data, *in_header;
  4.1013 @@ -1049,15 +240,14 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  4.1014   static int read_from_file(uint8_t **data, size_t *data_length)
  4.1015   {
  4.1016  -  int res;
  4.1017 --  struct file *fp;
  4.1018 --  mm_segment_t old_fs = get_fs();
  4.1019 --  fp = filp_open(TPM_STORAGE_FILE, O_RDONLY, 0);
  4.1020 --  if (IS_ERR(fp)) return -1;
  4.1021 --  *data_length = (size_t)fp->f_dentry->d_inode->i_size;
  4.1022 --  /* *data_length = i_size_read(fp->f_dentry->d_inode); */
  4.1023 --  *data = tpm_malloc(*data_length);
  4.1024 --  if (*data == NULL) {
  4.1025 --    filp_close(fp, NULL);
  4.1026 +-  int fp, file_status;
  4.1027 +-  struct stat file_info;
  4.1028 +-  fp = open(TPM_STORAGE_FILE, O_RDONLY, 0);
  4.1029 +-  file_status = fstat(fp, &file_info);
  4.1030 +-  if (file_status < 0) {
  4.1031 +-    close(fp);
  4.1032 +-    return -1;
  4.1033 +-  } 
  4.1034  +  int res, out_data_size, in_header_size;
  4.1035  +  uint8_t *ptr, *out_data, *in_header;
  4.1036  +  UINT16 tag = VTPM_TAG_REQ;
  4.1037 @@ -1074,7 +264,11 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  4.1038  +	vtpm_tx_fh = open(VTPM_TX_FIFO, O_WRONLY);
  4.1039  +#endif
  4.1040  +  }
  4.1041 -+
  4.1042 + 
  4.1043 +-  *data_length = file_info.st_size; 
  4.1044 +-  *data = tpm_malloc(*data_length);
  4.1045 +-  if (*data == NULL) {
  4.1046 +-    close(fp);
  4.1047  +  if (vtpm_tx_fh < 0) {
  4.1048  +		return -1;
  4.1049  +  }
  4.1050 @@ -1101,10 +295,8 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  4.1051  +    free(out_data);
  4.1052       return -1;
  4.1053     }
  4.1054 --  set_fs(get_ds());
  4.1055 --  res = fp->f_op->read(fp, *data, *data_length, &fp->f_pos);
  4.1056 --  set_fs(old_fs);
  4.1057 --  filp_close(fp, NULL);
  4.1058 +-  res = read(fp, *data, *data_length);
  4.1059 +-  close(fp);
  4.1060  +
  4.1061  +  printf("\tSending LoadNVM command\n");
  4.1062  +  res = write(vtpm_tx_fh, out_data, out_data_size);
  4.1063 @@ -1178,504 +370,138 @@ diff -uprN tpm_emulator-0.3-x86_64/tpm/t
  4.1064   }
  4.1065   
  4.1066   #else
  4.1067 -@@ -278,7 +473,6 @@ int tpm_restore_permanent_data(void)
  4.1068 - 
  4.1069 - int tpm_erase_permanent_data(void)
  4.1070 - {
  4.1071 --  int res = write_to_file("", 0);
  4.1072 -+  int res = write_to_file((uint8_t*)"", 0);
  4.1073 -   return res;
  4.1074 - }
  4.1075 --
  4.1076 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_deprecated.c vtpm/tpm/tpm_deprecated.c
  4.1077 ---- tpm_emulator-0.3-x86_64/tpm/tpm_deprecated.c	2006-08-29 15:07:21.622298456 -0700
  4.1078 -+++ vtpm/tpm/tpm_deprecated.c	2006-08-29 15:12:07.207882848 -0700
  4.1079 -@@ -1,6 +1,7 @@
  4.1080 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  4.1081 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  4.1082 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
  4.1083 -+ * Copyright (C) 2005 INTEL Corp
  4.1084 -  *
  4.1085 -  * This module is free software; you can redistribute it and/or modify
  4.1086 -  * it under the terms of the GNU General Public License as published
  4.1087 -@@ -50,7 +51,7 @@ TPM_RESULT TPM_SaveKeyContext(TPM_KEY_HA
  4.1088 -   BYTE *ptr;
  4.1089 -   UINT32 len;
  4.1090 -   info("TPM_SaveKeyContext()");
  4.1091 --  res = TPM_SaveContext(keyHandle, TPM_RT_KEY, "SaveKeyContext..", 
  4.1092 -+  res = TPM_SaveContext(keyHandle, TPM_RT_KEY, (BYTE*)"SaveKeyContext..", 
  4.1093 -                         keyContextSize, &contextBlob);
  4.1094 -   if (res != TPM_SUCCESS) return res;
  4.1095 -   len = *keyContextSize;
  4.1096 -@@ -82,7 +83,7 @@ TPM_RESULT TPM_SaveAuthContext(TPM_AUTHH
  4.1097 -   BYTE *ptr;
  4.1098 -   UINT32 len;
  4.1099 -   info("TPM_SaveAuthContext()");
  4.1100 --  res = TPM_SaveContext(authHandle, TPM_RT_KEY, "SaveAuthContext.", 
  4.1101 -+  res = TPM_SaveContext(authHandle, TPM_RT_KEY, (BYTE*)"SaveAuthContext.", 
  4.1102 -                         authContextSize, &contextBlob);
  4.1103 -   if (res != TPM_SUCCESS) return res;
  4.1104 -   len = *authContextSize;
  4.1105 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_emulator.h vtpm/tpm/tpm_emulator.h
  4.1106 ---- tpm_emulator-0.3-x86_64/tpm/tpm_emulator.h	2006-08-29 15:07:21.648294504 -0700
  4.1107 -+++ vtpm/tpm/tpm_emulator.h	2006-08-29 15:12:07.208882696 -0700
  4.1108 -@@ -1,5 +1,6 @@
  4.1109 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  4.1110 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  4.1111 -+ * Copyright (C) 2005 INTEL Corp
  4.1112 -  *
  4.1113 -  * This module is free software; you can redistribute it and/or modify
  4.1114 -  * it under the terms of the GNU General Public License as published
  4.1115 -@@ -22,7 +23,8 @@
  4.1116 - /* TPM configuration */
  4.1117 - #define TPM_STORE_TO_FILE       1
  4.1118 - #undef  TPM_STRONG_PERSISTENCE
  4.1119 --#undef  TPM_GENERATE_EK
  4.1120 -+//#undef  TPM_GENERATE_EK
  4.1121 -+#define  TPM_GENERATE_EK
  4.1122 - #undef  TPM_GENERATE_SEED_DAA
  4.1123 - 
  4.1124 - #define TPM_MANUFACTURER 0x4554485A /* 'ETHZ' */        
  4.1125 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_integrity.c vtpm/tpm/tpm_integrity.c
  4.1126 ---- tpm_emulator-0.3-x86_64/tpm/tpm_integrity.c	2006-08-29 15:07:21.645294960 -0700
  4.1127 -+++ vtpm/tpm/tpm_integrity.c	2006-08-29 15:12:07.208882696 -0700
  4.1128 -@@ -1,6 +1,7 @@
  4.1129 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  4.1130 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  4.1131 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
  4.1132 -+ * Copyright (C) 2005 INTEL Corp
  4.1133 -  *
  4.1134 -  * This module is free software; you can redistribute it and/or modify
  4.1135 -  * it under the terms of the GNU General Public License as published
  4.1136 -@@ -194,4 +195,3 @@ TPM_RESULT tpm_verify_pcr(TPM_KEY_DATA *
  4.1137 -   }
  4.1138 -   return TPM_SUCCESS;
  4.1139 - }
  4.1140 --
  4.1141 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_structures.h vtpm/tpm/tpm_structures.h
  4.1142 ---- tpm_emulator-0.3-x86_64/tpm/tpm_structures.h	2006-08-29 15:08:20.545340792 -0700
  4.1143 -+++ vtpm/tpm/tpm_structures.h	2006-08-29 15:12:07.211882240 -0700
  4.1144 -@@ -1,6 +1,7 @@
  4.1145 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  4.1146 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  4.1147 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
  4.1148 -+ * Copyright (C) 2005 INTEL Corp
  4.1149 -  *
  4.1150 -  * This module is free software; you can redistribute it and/or modify
  4.1151 -  * it under the terms of the GNU General Public License as published
  4.1152 -@@ -18,7 +19,7 @@
  4.1153 - #ifndef _TPM_STRUCTURES_H_
  4.1154 - #define _TPM_STRUCTURES_H_
  4.1155 - 
  4.1156 --#include <linux/types.h>
  4.1157 -+//#include <linux/types.h>
  4.1158 - #include "crypto/rsa.h"
  4.1159 - 
  4.1160 - /*
  4.1161 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_testing.c vtpm/tpm/tpm_testing.c
  4.1162 ---- tpm_emulator-0.3-x86_64/tpm/tpm_testing.c	2006-08-29 15:07:21.646294808 -0700
  4.1163 -+++ vtpm/tpm/tpm_testing.c	2006-08-29 15:12:07.213881936 -0700
  4.1164 -@@ -1,6 +1,7 @@
  4.1165 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  4.1166 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  4.1167 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
  4.1168 -+ * Copyright (C) 2005 INTEL Corp
  4.1169 -  *
  4.1170 -  * This module is free software; you can redistribute it and/or modify
  4.1171 -  * it under the terms of the GNU General Public License as published
  4.1172 -@@ -95,24 +96,24 @@ static int tpm_test_sha1(void)
  4.1173 -   struct {
  4.1174 -     uint8_t *data; uint32_t repetitions; uint8_t *digest;
  4.1175 -   } test_cases[] =  {{
  4.1176 --    "abc", 1,
  4.1177 --    "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
  4.1178 -+	(uint8_t*)"abc", 1,
  4.1179 -+    (uint8_t*)"\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
  4.1180 -   }, {
  4.1181 --    "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
  4.1182 --    "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
  4.1183 -+    (uint8_t*)"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
  4.1184 -+    (uint8_t*)"\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
  4.1185 -   }, {
  4.1186 --    "a", 1000000,
  4.1187 --    "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
  4.1188 -+    (uint8_t*)"a", 1000000,
  4.1189 -+    (uint8_t*)"\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
  4.1190 -   }, {
  4.1191 --    "0123456701234567012345670123456701234567012345670123456701234567", 10,
  4.1192 --    "\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52"
  4.1193 -+    (uint8_t*)"0123456701234567012345670123456701234567012345670123456701234567", 10,
  4.1194 -+    (uint8_t*)"\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52"
  4.1195 -   }};
  4.1196 - 
  4.1197 -   debug("tpm_test_sha1()");
  4.1198 -   for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
  4.1199 -     sha1_init(&ctx);
  4.1200 -     for (j = 0; j < test_cases[i].repetitions; j++)
  4.1201 --      sha1_update(&ctx, test_cases[i].data, strlen(test_cases[i].data));
  4.1202 -+      sha1_update(&ctx, test_cases[i].data, strlen((char*)test_cases[i].data));
  4.1203 -     sha1_final(&ctx, digest);
  4.1204 -     if (memcmp(digest, test_cases[i].digest, SHA1_DIGEST_LENGTH) != 0) return -1;
  4.1205 -   }
  4.1206 -@@ -128,41 +129,41 @@ static int tpm_test_hmac(void)
  4.1207 -   struct {
  4.1208 -     uint8_t *key, key_len, *data, data_len, *digest;
  4.1209 -   } test_cases[] = {{
  4.1210 --    "\x0b", 20, "Hi There", 8,
  4.1211 --    "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
  4.1212 -+    (uint8_t*)"\x0b", 20, (uint8_t*)"Hi There", 8,
  4.1213 -+    (uint8_t*)"\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
  4.1214 -   }, {
  4.1215 --    "Jefe", 4, "what do ya want for nothing?", 28,
  4.1216 --    "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
  4.1217 -+    (uint8_t*)"Jefe", 4, (uint8_t*)"what do ya want for nothing?", 28,
  4.1218 -+    (uint8_t*)"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
  4.1219 -   }, {
  4.1220 --    "\xaa", 20, "\xdd", 50,
  4.1221 --    "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
  4.1222 -+    (uint8_t*)"\xaa", 20, (uint8_t*)"\xdd", 50,
  4.1223 -+    (uint8_t*)"\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
  4.1224 -   }, {
  4.1225 --    "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
  4.1226 --    "\x15\x16\x17\x18\x19", 25, "\xcd", 50,
  4.1227 --    "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
  4.1228 -+    (uint8_t*)"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
  4.1229 -+    "\x15\x16\x17\x18\x19", 25, (uint8_t*)"\xcd", 50,
  4.1230 -+    (uint8_t*)"\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
  4.1231 -   }, {
  4.1232 --    "\x0c", 20, "Test With Truncation", 20,
  4.1233 --    "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
  4.1234 -+    (uint8_t*)"\x0c", 20, (uint8_t*)"Test With Truncation", 20,
  4.1235 -+    (uint8_t*)"\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
  4.1236 -   }, {
  4.1237 --    "\xaa", 80, "Test Using Larger Than Block-Size Key - Hash Key First", 54,
  4.1238 --    "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
  4.1239 -+    (uint8_t*)"\xaa", 80, (uint8_t*)"Test Using Larger Than Block-Size Key - Hash Key First", 54,
  4.1240 -+    (uint8_t*)"\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
  4.1241 -   }, {
  4.1242 --    "\xaa", 80,
  4.1243 --    "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
  4.1244 --    "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
  4.1245 -+    (uint8_t*)"\xaa", 80,
  4.1246 -+    (uint8_t*)"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
  4.1247 -+    (uint8_t*)"\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
  4.1248 -   }};
  4.1249 +diff -uprN tpm_emulator/tpmd.c vtpm/tpmd.c
  4.1250 +--- tpm_emulator/tpmd.c	2006-07-24 14:35:35.000000000 -0700
  4.1251 ++++ vtpm/tpmd.c	2006-07-24 14:35:35.000000000 -0700
  4.1252 +@@ -23,13 +23,27 @@
  4.1253 + #include <sys/time.h>
  4.1254   
  4.1255 -   debug("tpm_test_hmac()");
  4.1256 -   for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
  4.1257 --    if (strlen(test_cases[i].key) < test_cases[i].key_len) {
  4.1258 -+    if (strlen((char*)test_cases[i].key) < test_cases[i].key_len) {
  4.1259 -       uint8_t key[test_cases[i].key_len];
  4.1260 -       memset(key, test_cases[i].key[0], test_cases[i].key_len);
  4.1261 -       hmac_init(&ctx, key, test_cases[i].key_len);
  4.1262 -     } else {
  4.1263 -       hmac_init(&ctx, test_cases[i].key, test_cases[i].key_len);
  4.1264 -     }
  4.1265 --    for (j = 0; j < test_cases[i].data_len; j += strlen(test_cases[i].data)) {
  4.1266 --      hmac_update(&ctx, test_cases[i].data, strlen(test_cases[i].data));
  4.1267 -+    for (j = 0; j < test_cases[i].data_len; j += strlen((char*)test_cases[i].data)) {
  4.1268 -+      hmac_update(&ctx, test_cases[i].data, strlen((char*)test_cases[i].data));
  4.1269 -     }
  4.1270 -     hmac_final(&ctx, digest);
  4.1271 -     if (memcmp(digest, test_cases[i].digest, SHA1_DIGEST_LENGTH) != 0) return -1;
  4.1272 -@@ -173,9 +174,9 @@ static int tpm_test_hmac(void)
  4.1273 - static int tpm_test_rsa_EK(void)
  4.1274 - {
  4.1275 -   int res = 0;
  4.1276 --  char *data = "RSA PKCS #1 v1.5 Test-String";
  4.1277 -+  uint8_t *data = (uint8_t*)"RSA PKCS #1 v1.5 Test-String";
  4.1278 -   uint8_t buf[256];
  4.1279 --  size_t buf_len, data_len = strlen(data);
  4.1280 -+  size_t buf_len, data_len = strlen((char*)data);
  4.1281 -   rsa_private_key_t priv_key;
  4.1282 -   rsa_public_key_t pub_key;
  4.1283 - 
  4.1284 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_ticks.c vtpm/tpm/tpm_ticks.c
  4.1285 ---- tpm_emulator-0.3-x86_64/tpm/tpm_ticks.c	2006-08-29 15:07:21.646294808 -0700
  4.1286 -+++ vtpm/tpm/tpm_ticks.c	2006-08-29 15:12:07.235878592 -0700
  4.1287 -@@ -1,6 +1,7 @@
  4.1288 - /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  4.1289 -  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  4.1290 -  *                    Swiss Federal Institute of Technology (ETH) Zurich
  4.1291 -+ * Copyright (C) 2005 INTEL Corp
  4.1292 -  *
  4.1293 -  * This module is free software; you can redistribute it and/or modify
  4.1294 -  * it under the terms of the GNU General Public License as published
  4.1295 -@@ -37,9 +38,7 @@ TPM_RESULT TPM_SetTickType(TPM_TICKTYPE 
  4.1296 - TPM_RESULT TPM_GetTicks(TPM_CURRENT_TICKS *currentTime)
  4.1297 - {
  4.1298 -   info("TPM_GetTicks()");
  4.1299 --  memcpy(currentTime, &tpmData.stany.data.currentTicks, 
  4.1300 --    sizeof(TPM_CURRENT_TICKS));
  4.1301 --  return TPM_SUCCESS;
  4.1302 -+  return TPM_DISABLED_CMD;
  4.1303 - }
  4.1304 - 
  4.1305 - TPM_RESULT TPM_TickStampBlob(TPM_KEY_HANDLE keyHandle, TPM_NONCE *antiReplay,
  4.1306 -@@ -47,61 +46,12 @@ TPM_RESULT TPM_TickStampBlob(TPM_KEY_HAN
  4.1307 -                              TPM_CURRENT_TICKS *currentTicks, 
  4.1308 -                              UINT32 *sigSize, BYTE **sig)
  4.1309 - {
  4.1310 --  TPM_RESULT res;
  4.1311 --  TPM_KEY_DATA *key;
  4.1312 --  BYTE *info, *p;
  4.1313 --  UINT32 info_length, length;
  4.1314 -   info("TPM_TickStampBlob()");
  4.1315 --  /* get key */
  4.1316 --  key = tpm_get_key(keyHandle);
  4.1317 --  if (key == NULL) return TPM_INVALID_KEYHANDLE;
  4.1318 --  /* verify authorization */ 
  4.1319 --  res = tpm_verify_auth(auth1, key->usageAuth, keyHandle);
  4.1320 --  if (res != TPM_SUCCESS) return res;
  4.1321 --  if (key->keyUsage != TPM_KEY_SIGNING && key->keyUsage != TPM_KEY_LEGACY
  4.1322 --      && key->keyUsage != TPM_KEY_IDENTITY) return TPM_INVALID_KEYUSAGE;
  4.1323 --  /* get current ticks */
  4.1324 --  TPM_GetTicks(currentTicks);
  4.1325 --  /* sign data using signature scheme PKCS1_SHA1 and TPM_SIGN_INFO container */
  4.1326 --  *sigSize = key->key.size >> 3;
  4.1327 --  *sig = tpm_malloc(*sigSize);
  4.1328 --  if (*sig == NULL) return TPM_FAIL; 
  4.1329 --  /* setup TPM_SIGN_INFO structure */
  4.1330 --  info_length = 30 + sizeof(TPM_DIGEST) + sizeof_TPM_CURRENT_TICKS(currentTicks);
  4.1331 --  info = tpm_malloc(info_length);
  4.1332 --  if (info == NULL) {
  4.1333 --    tpm_free(*sig);
  4.1334 --    return TPM_FAIL;
  4.1335 --  }
  4.1336 --  memcpy(&info[0], "\x05\x00TSTP", 6);
  4.1337 --  memcpy(&info[6], antiReplay->nonce, 20);
  4.1338 --  *(UINT32*)&info[26] = cpu_to_be32(20
  4.1339 --                        + sizeof_TPM_CURRENT_TICKS(currentTicks));
  4.1340 --  memcpy(&info[30], digestToStamp->digest, sizeof(TPM_DIGEST));
  4.1341 --  p = &info[30 + sizeof(TPM_DIGEST)]; 
  4.1342 --  length = sizeof_TPM_CURRENT_TICKS(currentTicks);
  4.1343 --  if (tpm_marshal_TPM_CURRENT_TICKS(&p, &length, currentTicks)
  4.1344 --      || rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1, info, info_length, *sig)) {   
  4.1345 --    tpm_free(*sig);
  4.1346 --    tpm_free(info);
  4.1347 --    return TPM_FAIL;
  4.1348 --  } 
  4.1349 --  return TPM_SUCCESS;
  4.1350 -+  return TPM_DISABLED_CMD;
  4.1351 - }
  4.1352 + #include "tpm_emulator.h"
  4.1353 ++#include "vtpm_manager.h"
  4.1354   
  4.1355 - void tpm_update_ticks(void)
  4.1356 - {
  4.1357 --  if (tpmData.stany.data.currentTicks.tag == 0) {
  4.1358 --    tpmData.stany.data.currentTicks.tag = TPM_TAG_CURRENT_TICKS;
  4.1359 --    tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();
  4.1360 --    tpmData.stany.data.currentTicks.tickType = tpmData.permanent.data.tickType;
  4.1361 --    tpm_get_random_bytes(tpmData.stany.data.currentTicks.tickNonce.nonce, 
  4.1362 --      sizeof(TPM_NONCE));
  4.1363 --    tpmData.stany.data.currentTicks.tickRate = 1;
  4.1364 --    tpmData.stany.data.currentTicks.tickSecurity = TICK_SEC_NO_CHECK;
  4.1365 --  } else {
  4.1366 --    tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();   
  4.1367 --  }
  4.1368 - }
  4.1369 -   
  4.1370 - 
  4.1371 -diff -uprN tpm_emulator-0.3-x86_64/tpm/tpm_transport.c vtpm/tpm/tpm_transport.c
  4.1372 ---- tpm_emulator-0.3-x86_64/tpm/tpm_transport.c	2006-08-29 15:07:21.647294656 -0700
  4.1373 -+++ vtpm/tpm/tpm_transport.c	2006-08-29 15:12:07.239877984 -0700
  4.1374 -@@ -59,7 +59,7 @@ static int decrypt_transport_auth(TPM_KE
  4.1375 - static void transport_log_in(TPM_COMMAND_CODE ordinal, BYTE parameters[20],
  4.1376 -                              BYTE pubKeyHash[20], TPM_DIGEST *transDigest)
  4.1377 - {
  4.1378 --  UINT32 tag = cpu_to_be32(TPM_TAG_TRANSPORT_LOG_IN);
  4.1379 -+  UINT32 tag = CPU_TO_BE32(TPM_TAG_TRANSPORT_LOG_IN);
  4.1380 -   BYTE *ptr, buf[sizeof_TPM_TRANSPORT_LOG_IN(x)];
  4.1381 -   UINT32 len = sizeof(buf);
  4.1382 -   sha1_ctx_t sha1;
  4.1383 -@@ -76,7 +76,7 @@ static void transport_log_in(TPM_COMMAND
  4.1384 - static void transport_log_out(TPM_CURRENT_TICKS *currentTicks, BYTE parameters[20],
  4.1385 -                               TPM_MODIFIER_INDICATOR locality, TPM_DIGEST *transDigest)
  4.1386 - {
  4.1387 --  UINT32 tag = cpu_to_be32(TPM_TAG_TRANSPORT_LOG_OUT);
  4.1388 -+  UINT32 tag = CPU_TO_BE32(TPM_TAG_TRANSPORT_LOG_OUT);
  4.1389 -   BYTE *ptr, buf[sizeof_TPM_TRANSPORT_LOG_OUT(x)];
  4.1390 -   UINT32 len = sizeof(buf);
  4.1391 -   sha1_ctx_t sha1;
  4.1392 -@@ -191,7 +191,7 @@ static void decrypt_wrapped_command(BYTE
  4.1393 -     sha1_update(&sha1, auth->nonceOdd.nonce, sizeof(auth->nonceOdd.nonce));
  4.1394 -     sha1_update(&sha1, "in", 2);
  4.1395 -     sha1_update(&sha1, secret, sizeof(TPM_SECRET));
  4.1396 --    j = cpu_to_be32(i);
  4.1397 -+    j = CPU_TO_BE32(i);
  4.1398 -     sha1_update(&sha1, (BYTE*)&j, 4);
  4.1399 -     sha1_final(&sha1, mask);
  4.1400 -     for (j = 0; j < sizeof(mask) && buf_len > 0; j++) { 
  4.1401 -@@ -213,7 +213,7 @@ static void encrypt_wrapped_command(BYTE
  4.1402 -     sha1_update(&sha1, auth->nonceOdd.nonce, sizeof(auth->nonceOdd.nonce));
  4.1403 -     sha1_update(&sha1, "out", 3);
  4.1404 -     sha1_update(&sha1, secret, sizeof(TPM_SECRET));
  4.1405 --    j = cpu_to_be32(i);
  4.1406 -+    j = CPU_TO_BE32(i);
  4.1407 -     sha1_update(&sha1, (BYTE*)&j, 4);
  4.1408 -     sha1_final(&sha1, mask);
  4.1409 -     for (j = 0; j < sizeof(mask) && buf_len > 0; j++) { 
  4.1410 -@@ -253,9 +253,9 @@ TPM_RESULT TPM_ExecuteTransport(UINT32 i
  4.1411 -   /* verify authorization */
  4.1412 -   tpm_compute_in_param_digest(&req);
  4.1413 -   sha1_init(&sha1);
  4.1414 --  res = cpu_to_be32(TPM_ORD_ExecuteTransport);
  4.1415 -+  res = CPU_TO_BE32(TPM_ORD_ExecuteTransport);
  4.1416 -   sha1_update(&sha1, (BYTE*)&res, 4);
  4.1417 --  res = cpu_to_be32(inWrappedCmdSize);
  4.1418 -+  res = CPU_TO_BE32(inWrappedCmdSize);
  4.1419 -   sha1_update(&sha1, (BYTE*)&res, 4);
  4.1420 -   sha1_update(&sha1, req.auth1.digest, sizeof(req.auth1.digest));
  4.1421 -   sha1_final(&sha1, auth1->digest);
  4.1422 -@@ -357,7 +357,7 @@ TPM_RESULT TPM_ReleaseTransportSigned(TP
  4.1423 -   /* setup a TPM_SIGN_INFO structure */
  4.1424 -   memcpy(&buf[0], "\x05\x00TRAN", 6);
  4.1425 -   memcpy(&buf[6], antiReplay->nonce, 20);
  4.1426 --  *(UINT32*)&buf[26] = cpu_to_be32(20);
  4.1427 -+  *(UINT32*)&buf[26] = CPU_TO_BE32(20);
  4.1428 -   memcpy(&buf[30], session->transInternal.transDigest.digest, 20);
  4.1429 -   /* sign info structure */ 
  4.1430 -   res = tpm_sign(key, auth1, TRUE, buf, sizeof(buf), signature, signSize);
  4.1431 -diff -uprN tpm_emulator-0.3-x86_64/tpmd.c vtpm/tpmd.c
  4.1432 ---- tpm_emulator-0.3-x86_64/tpmd.c	1969-12-31 16:00:00.000000000 -0800
  4.1433 -+++ vtpm/tpmd.c	2006-08-29 15:12:07.240877832 -0700
  4.1434 -@@ -0,0 +1,207 @@
  4.1435 -+/* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  4.1436 -+ * Copyright (C) 2005 INTEL Corp
  4.1437 -+ *
  4.1438 -+ * This module is free software; you can redistribute it and/or modify
  4.1439 -+ * it under the terms of the GNU General Public License as published
  4.1440 -+ * by the Free Software Foundation; either version 2 of the License,
  4.1441 -+ * or (at your option) any later version.
  4.1442 -+ *
  4.1443 -+ * This module is distributed in the hope that it will be useful,
  4.1444 -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4.1445 -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  4.1446 -+ * GNU General Public License for more details.
  4.1447 -+ *
  4.1448 -+ */
  4.1449 -+
  4.1450 -+#include <stdio.h>
  4.1451 -+#include <stdlib.h>
  4.1452 -+#include <unistd.h>
  4.1453 -+#include <string.h>
  4.1454 -+#include <sys/types.h>
  4.1455 -+#include <sys/stat.h>
  4.1456 -+#include <fcntl.h>
  4.1457 -+#include <sys/time.h>
  4.1458 -+
  4.1459 -+#include "tpm_emulator.h"
  4.1460 -+#include "vtpm_manager.h"
  4.1461 -+
  4.1462 +-#define TPM_RX_FNAME "/var/tpm/tpm_in.fifo"
  4.1463 +-#define TPM_TX_FNAME "/var/tpm/tpm_out.fifo"
  4.1464  +#ifdef VTPM_MULTI_VM
  4.1465  + #define DEV_BE "/dev/vtpm"
  4.1466  +#else
  4.1467  + #define GUEST_RX_FIFO_D "/var/vtpm/fifos/tpm_cmd_to_%d.fifo"
  4.1468  + #define GUEST_TX_FIFO "/var/vtpm/fifos/tpm_rsp_from_all.fifo"
  4.1469  +#endif
  4.1470 -+
  4.1471 + 
  4.1472  + int dmi_id;
  4.1473  +						
  4.1474 -+#define BUFFER_SIZE 2048
  4.1475 -+
  4.1476 + #define BUFFER_SIZE 2048
  4.1477 + 
  4.1478  +static uint8_t ctrl_msg[] = { 0, 0, 0, 0,   // destination
  4.1479  +                              1, 193,       // VTPM_TAG
  4.1480  +                              0, 0, 0, 10,  // Size
  4.1481  +                              0, 0, 0, 0};  // TPM_SUCCESS
  4.1482  +                            
  4.1483  +
  4.1484 -+static int devurandom=0;
  4.1485 -+
  4.1486 -+	  
  4.1487 -+void get_random_bytes(void *buf, int nbytes) {
  4.1488 -+  
  4.1489 -+  if (devurandom == 0) {
  4.1490 -+    devurandom = open("/dev/urandom", O_RDONLY);
  4.1491 -+  }
  4.1492 + static int devurandom=0;
  4.1493  +
  4.1494 -+  if (read(devurandom, buf, nbytes) != nbytes) {
  4.1495 -+      printf("Can't get random number.\n");
  4.1496 -+      exit(-1);
  4.1497 -+  }
  4.1498 -+}
  4.1499 -+
  4.1500 -+uint64_t tpm_get_ticks(void)
  4.1501 -+{
  4.1502 -+  //struct timeval tv;
  4.1503 -+  //int gettimeofday(&tv, struct timezone *tz);
  4.1504 -+  return 0;
  4.1505 -+}
  4.1506 -+
  4.1507 -+int main(int argc, char **argv)
  4.1508 -+{
  4.1509 + 	  
  4.1510 + void get_random_bytes(void *buf, int nbytes) {
  4.1511 +   
  4.1512 +@@ -52,18 +66,26 @@ uint64_t tpm_get_ticks(void)
  4.1513 + 
  4.1514 + int main(int argc, char **argv)
  4.1515 + {
  4.1516 +-  uint8_t in[BUFFER_SIZE], *out;
  4.1517  +  uint8_t in[BUFFER_SIZE], *out, *addressed_out;
  4.1518 -+  uint32_t out_size;
  4.1519 -+  int in_size, written;
  4.1520 +   uint32_t out_size;
  4.1521 +   int in_size, written;
  4.1522 +-  int i;
  4.1523 +-  struct stat file_info;
  4.1524 +-
  4.1525 +-  int tpm_tx_fh=-1, tpm_rx_fh=-1;
  4.1526  +  int i, guest_id=-1;
  4.1527  + 
  4.1528  +  int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
  4.1529  +#ifdef VTPM_MULTI_VM
  4.1530 -+  if (argc < 2) {
  4.1531 -+    printf("Usage: tpmd clear|save|deactivated\n" );
  4.1532 +   if (argc < 2) {
  4.1533 +     printf("Usage: tpmd clear|save|deactivated\n" );
  4.1534  +#else
  4.1535  +  if (argc < 3) {
  4.1536  +    printf("Usage: tpmd clear|save|deactivated vtpmid\n" );
  4.1537  +#endif
  4.1538 -+	  return -1;
  4.1539 -+  }
  4.1540 -+
  4.1541 + 	  return -1;
  4.1542 +   }
  4.1543 + 
  4.1544  +#ifndef VTPM_MULTI_VM
  4.1545  +  dmi_id = atoi(argv[2]);
  4.1546  +#endif
  4.1547  +
  4.1548 -+  /* initialize TPM emulator */
  4.1549 -+  if (!strcmp(argv[1], "clear")) {
  4.1550 -+    printf("Initializing tpm: %s\n", argv[1]);
  4.1551 -+    tpm_emulator_init(1);
  4.1552 -+  } else if (!strcmp(argv[1], "save")) { 
  4.1553 -+    printf("Initializing tpm: %s\n", argv[1]);
  4.1554 -+    tpm_emulator_init(2);
  4.1555 -+  } else if (!strcmp(argv[1], "deactivated")) {
  4.1556 -+    printf("Initializing tpm: %s\n", argv[1]);
  4.1557 -+    tpm_emulator_init(3);
  4.1558 -+  } else {
  4.1559 -+    printf("invalid startup mode '%s'; must be 'clear', "
  4.1560 -+      "'save' (default) or 'deactivated", argv[1]);
  4.1561 -+    return -1;
  4.1562 -+  }
  4.1563 -+
  4.1564 +   /* initialize TPM emulator */
  4.1565 +   if (!strcmp(argv[1], "clear")) {
  4.1566 +     printf("Initializing tpm: %s\n", argv[1]);
  4.1567 +@@ -80,46 +102,30 @@ int main(int argc, char **argv)
  4.1568 +     return -1;
  4.1569 +   }
  4.1570 + 
  4.1571 +-  if ( stat(TPM_RX_FNAME, &file_info) == -1) {
  4.1572 +-    if ( mkfifo(TPM_RX_FNAME, S_IWUSR | S_IRUSR ) ) {
  4.1573 +-      printf("Failed to create fifo %s.\n", TPM_RX_FNAME);
  4.1574 +-      return -1;
  4.1575 +-    }
  4.1576 +-  }
  4.1577 +-
  4.1578 +-  if ( stat(TPM_TX_FNAME, &file_info) == -1) {
  4.1579 +-    if ( mkfifo(TPM_TX_FNAME, S_IWUSR | S_IRUSR ) ) {
  4.1580 +-      printf("Failed to create fifo %s.\n", TPM_TX_FNAME);
  4.1581 +-      return -1;
  4.1582 +-    }
  4.1583 +-  }
  4.1584 +-
  4.1585  +  char *guest_rx_file = malloc(10 + strlen(GUEST_RX_FIFO_D));
  4.1586  +  sprintf(guest_rx_file, GUEST_RX_FIFO_D, (uint32_t) dmi_id);
  4.1587  +  
  4.1588 -+  while (1) {
  4.1589 -+abort_command:
  4.1590 +   while (1) {
  4.1591 + abort_command:
  4.1592 +-    if (tpm_rx_fh < 0) {
  4.1593 +-      tpm_rx_fh = open(TPM_RX_FNAME, O_RDONLY);
  4.1594  +    if (vtpm_rx_fh < 0) {
  4.1595  +#ifdef VTPM_MUTLI_VM
  4.1596  +	  vtpm_rx_fh = open(DEV_BE, O_RDWR);
  4.1597  +#else
  4.1598  +      vtpm_rx_fh = open(guest_rx_file, O_RDONLY);
  4.1599  +#endif
  4.1600 -+    }
  4.1601 -+    
  4.1602 +     }
  4.1603 +     
  4.1604 +-    if (tpm_rx_fh < 0) {
  4.1605  +    if (vtpm_rx_fh < 0) {
  4.1606 -+      printf("ERROR: failed to open devices to listen to guest.\n");
  4.1607 -+      return -1;
  4.1608 -+    }
  4.1609 -+    
  4.1610 +       printf("ERROR: failed to open devices to listen to guest.\n");
  4.1611 +       return -1;
  4.1612 +     }
  4.1613 +     
  4.1614 +-    if (tpm_tx_fh < 0) {
  4.1615 +-      tpm_tx_fh = open(TPM_TX_FNAME, O_WRONLY);
  4.1616 +-    }
  4.1617 +-
  4.1618 +-    if (tpm_tx_fh < 0) {
  4.1619 +-      printf("ERROR: failed to open devices to respond to guest.\n");
  4.1620 +-      return -1;
  4.1621 +-    }
  4.1622 +-
  4.1623 +-    in_size = read(tpm_rx_fh, in, BUFFER_SIZE);
  4.1624  +    in_size = read(vtpm_rx_fh, in, BUFFER_SIZE);
  4.1625 -+    if (in_size < 6) { // Magic size of minium TPM command
  4.1626 -+      printf("Recv[%d] to small: 0x", in_size);
  4.1627 -+      if (in_size <= 0) {
  4.1628 +     if (in_size < 6) { // Magic size of minium TPM command
  4.1629 +       printf("Recv[%d] to small: 0x", in_size);
  4.1630 +       if (in_size <= 0) {
  4.1631 +-          close(tpm_rx_fh);
  4.1632 +-          tpm_rx_fh = -1;
  4.1633  +          close(vtpm_rx_fh);
  4.1634  +          vtpm_rx_fh = -1;
  4.1635 -+          goto abort_command;
  4.1636 -+      }
  4.1637 -+    } else { 
  4.1638 -+      printf("Recv[%d]: 0x", in_size);
  4.1639 -+      for (i=0; i< in_size; i++) 
  4.1640 -+        printf("%x ", in[i]);
  4.1641 -+      printf("\n");
  4.1642 -+    }
  4.1643 -+
  4.1644 +           goto abort_command;
  4.1645 +       }
  4.1646 +     } else { 
  4.1647 +@@ -129,28 +135,73 @@ abort_command:
  4.1648 +       printf("\n");
  4.1649 +     }
  4.1650 + 
  4.1651 +-    
  4.1652 +-    if (tpm_handle_command(in, in_size, &out, &out_size) != 0) { 
  4.1653 +-        printf("ERROR: Handler Failed.\n");
  4.1654  +    if (guest_id == -1) {
  4.1655  +        guest_id = *((uint32_t *) in);
  4.1656  +        *((uint32_t *) ctrl_msg) = *((uint32_t *) in);
  4.1657 @@ -1683,8 +509,9 @@ diff -uprN tpm_emulator-0.3-x86_64/tpmd.
  4.1658  +        if (guest_id != *((uint32_t *) in) ) {
  4.1659  +            printf("WARNING: More than one guest attached\n");
  4.1660  +        }
  4.1661 -+    }
  4.1662 -+
  4.1663 +     }
  4.1664 + 
  4.1665 +-    written = write(tpm_tx_fh, out, out_size);
  4.1666  +    if (vtpm_tx_fh < 0) {
  4.1667  +#ifdef VTPM_MUTLI_VM
  4.1668  +	  vtpm_tx_fh = open(DEV_BE, O_RDWR);
  4.1669 @@ -1693,7 +520,11 @@ diff -uprN tpm_emulator-0.3-x86_64/tpmd.
  4.1670  +      vtpm_tx_fh = open(GUEST_TX_FIFO, O_WRONLY);
  4.1671  +#endif
  4.1672  +    }
  4.1673 -+
  4.1674 + 
  4.1675 +-    if (written != out_size ) {
  4.1676 +-      printf("ERROR: Part of response not written %d/%d.\nAttempt: ", written, out_size);
  4.1677 +-    } else {
  4.1678 +-      printf("Sent[%Zu]: ", out_size);
  4.1679  +    if (vtpm_tx_fh < 0) {
  4.1680  +      printf("ERROR: failed to open devices to respond to guest.\n");
  4.1681  +      return -1;
  4.1682 @@ -1733,16 +564,34 @@ diff -uprN tpm_emulator-0.3-x86_64/tpmd.
  4.1683  +      }
  4.1684  +      tpm_free(out);
  4.1685  +      tpm_free(addressed_out);
  4.1686 -+    }
  4.1687 -+
  4.1688 -+  } // loop
  4.1689 -+
  4.1690 -+  tpm_emulator_shutdown();
  4.1691 -+
  4.1692 +     }
  4.1693 +-    for (i=0; i< out_size; i++)
  4.1694 +-      printf("%x ", out[i]);
  4.1695 +-    printf("\n");
  4.1696 +-    tpm_free(out);
  4.1697 + 
  4.1698 +   } // loop
  4.1699 + 
  4.1700 +   tpm_emulator_shutdown();
  4.1701 + 
  4.1702 +-  close(tpm_tx_fh);
  4.1703 +-  close(tpm_rx_fh);
  4.1704  +  close(vtpm_tx_fh);
  4.1705  +#ifndef VTPM_MUTLI_VM
  4.1706  +  close(vtpm_rx_fh);
  4.1707  +  free (guest_rx_file);
  4.1708  +#endif
  4.1709 -+
  4.1710 -+}
  4.1711 + 
  4.1712 + }
  4.1713 +Binary files tpm_emulator/tpm_emulator and vtpm/tpm_emulator differ
  4.1714 +diff -uprN tpm_emulator/tpm_version.h vtpm/tpm_version.h
  4.1715 +--- tpm_emulator/tpm_version.h	2006-07-24 14:35:41.000000000 -0700
  4.1716 ++++ vtpm/tpm_version.h	2006-07-24 14:35:35.000000000 -0700
  4.1717 +@@ -2,5 +2,5 @@
  4.1718 + #define _TPM_VERSION_H_
  4.1719 + #define VERSION_MAJOR 0
  4.1720 + #define VERSION_MINOR 4
  4.1721 +-#define VERSION_BUILD 1153776940
  4.1722 ++#define VERSION_BUILD 1153776935
  4.1723 + #endif /* _TPM_VERSION_H_ */
  4.1724 +Binary files tpm_emulator/vtpmd and vtpm/vtpmd differ
     5.1 --- a/tools/vtpm_manager/manager/dmictl.c	Tue Jul 25 11:19:48 2006 +0100
     5.2 +++ b/tools/vtpm_manager/manager/dmictl.c	Tue Jul 25 11:30:30 2006 +0100
     5.3 @@ -105,6 +105,9 @@ TPM_RESULT close_dmi(VTPM_DMI_RESOURCE *
     5.4    if (dmi_res == NULL) 
     5.5      return TPM_SUCCESS;
     5.6  
     5.7 +  if (dmi_res->dmi_id == VTPM_CTL_DM) 
     5.8 +    return(TPM_BAD_PARAMETER);
     5.9 +
    5.10    TCS_CloseContext(dmi_res->TCSContext);
    5.11    dmi_res->connected = FALSE;
    5.12  
     6.1 --- a/tools/vtpm_manager/manager/vtpm_manager_handler.c	Tue Jul 25 11:19:48 2006 +0100
     6.2 +++ b/tools/vtpm_manager/manager/vtpm_manager_handler.c	Tue Jul 25 11:30:30 2006 +0100
     6.3 @@ -166,7 +166,8 @@ TPM_RESULT VTPM_Manager_Handler( vtpm_ip
     6.4           (!dmi_res->connected) ) {
     6.5        vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempted access to non-existent or disconnected DMI %d. Aborting...\n", dmi);
     6.6        status = TPM_BAD_PARAMETER;
     6.7 -      goto abort_with_error;
     6.8 +      // We have no one to reply to, they don't exist.
     6.9 +      goto abort_command;
    6.10      }
    6.11  
    6.12      if (tag == VTPM_TAG_REQ) { 
     7.1 --- a/tools/vtpm_manager/manager/vtpmd.c	Tue Jul 25 11:19:48 2006 +0100
     7.2 +++ b/tools/vtpm_manager/manager/vtpmd.c	Tue Jul 25 11:30:30 2006 +0100
     7.3 @@ -172,7 +172,7 @@ TPM_RESULT VTPM_New_DMI_Extra(VTPM_DMI_R
     7.4      } else if (pid == 0) {
     7.5        switch (startup_mode) {
     7.6        case TPM_ST_CLEAR:
     7.7 -        execl (TPM_EMULATOR_PATH, "vtmpd", "clear", dmi_id_str, NULL);
     7.8 +        execl (TPM_EMULATOR_PATH, "vtpmd", "clear", dmi_id_str, NULL);
     7.9          break;
    7.10        case TPM_ST_STATE:
    7.11          execl (TPM_EMULATOR_PATH, "vtpmd", "save", dmi_id_str, NULL);
     8.1 --- a/tools/vtpm_manager/tcs/tcs.c	Tue Jul 25 11:19:48 2006 +0100
     8.2 +++ b/tools/vtpm_manager/tcs/tcs.c	Tue Jul 25 11:30:30 2006 +0100
     8.3 @@ -162,8 +162,7 @@ TPM_RESULT TCS_OpenContext(TCS_CONTEXT_H
     8.4    
     8.5    vtpmloginfo(VTPM_LOG_TCS, "Calling TCS_OpenContext:\n");
     8.6    
     8.7 -  // hContext must point to a null memory context handle
     8.8 -  if(*hContext == HANDLE_NULL) {
     8.9 +  if (hContext) {
    8.10      CONTEXT_HANDLE* pContextHandle = (CONTEXT_HANDLE *) malloc(sizeof(CONTEXT_HANDLE));
    8.11      if (pContextHandle == NULL) 
    8.12        return TPM_SIZE;
     9.1 --- a/tools/vtpm_manager/tcs/transmit.c	Tue Jul 25 11:19:48 2006 +0100
     9.2 +++ b/tools/vtpm_manager/tcs/transmit.c	Tue Jul 25 11:30:30 2006 +0100
     9.3 @@ -49,8 +49,8 @@ static int g_tx_fd = -1;              //
     9.4   #define TPM_TX_FNAME "/dev/tpm0"
     9.5   static int *g_rx_fdp = &g_tx_fd;
     9.6  #else
     9.7 - #define TPM_TX_FNAME "/tmp/tpm_in.fifo"
     9.8 - #define TPM_RX_FNAME "/tmp/tpm_out.fifo"
     9.9 + #define TPM_TX_FNAME "/var/tpm/tpm_in.fifo"
    9.10 + #define TPM_RX_FNAME "/var/tpm/tpm_out.fifo"
    9.11   static int g_rx_fd = -1;
    9.12   static int *g_rx_fdp = &g_rx_fd;              // the fd to the TPM
    9.13  #endif