direct-io.hg

changeset 10491:533b0709a68f

[VTPM_TOOLS] Moved the developement tpm_emulator down to ring 3, which
should make debugging easier as well as fix the emulator for x86_64

Signed-off-by: Vincent Scarlata <vincent.r.scarlata@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Tue Jun 27 11:13:24 2006 +0100 (2006-06-27)
parents e236794915cc
children 18587d3e94c8
files tools/vtpm/Rules.mk tools/vtpm/tpm_emulator.patch tools/vtpm_manager/Rules.mk tools/vtpm_manager/manager/vtpm_manager.c tools/vtpm_manager/tcs/transmit.c
line diff
     1.1 --- a/tools/vtpm/Rules.mk	Tue Jun 27 11:05:39 2006 +0100
     1.2 +++ b/tools/vtpm/Rules.mk	Tue Jun 27 11:13:24 2006 +0100
     1.3 @@ -33,7 +33,6 @@ OBJS	= $(patsubst %.c,%.o,$(SRCS))
     1.4  
     1.5  -include $(DEP_FILES)
     1.6  
     1.7 -# Emulator does not work on 64-bit systems, and may be broken on 32 right now
     1.8  BUILD_EMULATOR = n
     1.9  
    1.10  # Make sure these are just rules
     2.1 --- a/tools/vtpm/tpm_emulator.patch	Tue Jun 27 11:05:39 2006 +0100
     2.2 +++ b/tools/vtpm/tpm_emulator.patch	Tue Jun 27 11:13:24 2006 +0100
     2.3 @@ -1,68 +1,453 @@
     2.4 -diff -uprN orig/tpm_emulator-0.2-x86_64/AUTHORS tpm_emulator/AUTHORS
     2.5 ---- orig/tpm_emulator-0.2-x86_64/AUTHORS	2005-08-15 00:58:57.000000000 -0700
     2.6 -+++ tpm_emulator/AUTHORS	2005-09-14 20:27:22.000000000 -0700
     2.7 -@@ -1 +1,2 @@
     2.8 +diff -uprN orig/tpm_emulator-0.3-x86_64/AUTHORS tpm_emulator/AUTHORS
     2.9 +--- orig/tpm_emulator-0.3-x86_64/AUTHORS	2006-01-10 04:21:45.000000000 -0800
    2.10 ++++ tpm_emulator/AUTHORS	2006-06-02 13:36:03.000000000 -0700
    2.11 +@@ -1,2 +1,3 @@
    2.12   Mario Strasser <mast@gmx.net>
    2.13 -+INTEL Corp <>
    2.14 -diff -uprN orig/tpm_emulator-0.2-x86_64/ChangeLog tpm_emulator/ChangeLog
    2.15 ---- orig/tpm_emulator-0.2-x86_64/ChangeLog	2005-08-15 00:58:57.000000000 -0700
    2.16 -+++ tpm_emulator/ChangeLog	2005-09-14 20:27:22.000000000 -0700
    2.17 -@@ -1,3 +1,7 @@
    2.18 -+2005-08-16: INTEL Corp
    2.19 -+	* Set default permissions to PCRs
    2.20 -+	* Changed device to /dev/tpm0
    2.21 + Heiko Stamer <stamer@gaos.org> [DAA]
    2.22 ++INTEL Corp <> [Dropped to Ring3]
    2.23 +diff -uprN orig/tpm_emulator-0.3-x86_64/ChangeLog tpm_emulator/ChangeLog
    2.24 +--- orig/tpm_emulator-0.3-x86_64/ChangeLog	2006-01-10 04:21:45.000000000 -0800
    2.25 ++++ tpm_emulator/ChangeLog	2006-06-02 13:36:03.000000000 -0700
    2.26 +@@ -1,3 +1,6 @@
    2.27 ++2005-08-16 Intel Corp
    2.28 ++	* Moved module out of kernel to run as a ring 3 app
    2.29  +
    2.30 - 2005-08-15  Mario Strasser <mast@gmx.net>
    2.31 - 	* all: some typos corrected
    2.32 - 	* tpm_integrity.c: bug in TPM_Extend fixed
    2.33 -diff -uprN orig/tpm_emulator-0.2-x86_64/linux_module.h tpm_emulator/linux_module.h
    2.34 ---- orig/tpm_emulator-0.2-x86_64/linux_module.h	2005-09-15 19:21:14.844078720 -0700
    2.35 -+++ tpm_emulator/linux_module.h	2005-09-14 20:27:22.000000000 -0700
    2.36 + 2005-12-24  Mario Strasser <mast@gmx.net>
    2.37 + 	* tpm_transport.c, tpm_marshalling.c, tpm_structures.h:
    2.38 + 		Transport session functionality added
    2.39 +diff -uprN orig/tpm_emulator-0.3-x86_64/crypto/gmp_kernel_wrapper.c tpm_emulator/crypto/gmp_kernel_wrapper.c
    2.40 +--- orig/tpm_emulator-0.3-x86_64/crypto/gmp_kernel_wrapper.c	2006-06-05 09:54:21.000000000 -0700
    2.41 ++++ tpm_emulator/crypto/gmp_kernel_wrapper.c	2006-06-02 13:36:03.000000000 -0700
    2.42  @@ -1,5 +1,6 @@
    2.43   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
    2.44    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
    2.45 -+ * Copyright (C) 2005 INTEL Corp.
    2.46 ++ * Copyright (C) 2005 INTEL Corp
    2.47 +  *
    2.48 +  * This module is free software; you can redistribute it and/or modify
    2.49 +  * it under the terms of the GNU General Public License as published
    2.50 +@@ -24,15 +25,10 @@ int __gmp_junk;
    2.51 + void __attribute__ ((regparm(0))) __gmp_assert_fail(const char *filename, 
    2.52 +   int linenum, const char *expr) 
    2.53 + {
    2.54 +-  panic(KERN_CRIT TPM_MODULE_NAME "%s:%d: GNU MP assertion failed: %s\n", 
    2.55 ++  error("%s:%d: GNU MP assertion failed: %s\n", 
    2.56 +     filename, linenum, expr);
    2.57 + }
    2.58 + 
    2.59 +-void __attribute__ ((regparm(0))) abort(void)
    2.60 +-{
    2.61 +-  panic(KERN_CRIT TPM_MODULE_NAME "GNU MP abort() was called\n");
    2.62 +-}
    2.63 +-
    2.64 + /* overwrite GNU MP random functions (used by mpz/millerrabin.c) */ 
    2.65 + 
    2.66 + void __attribute__ ((regparm(0))) gmp_randinit(gmp_randstate_t rstate, 
    2.67 +@@ -77,20 +73,19 @@ void __attribute__ ((regparm(0))) mpz_ur
    2.68 + 
    2.69 + void __attribute__ ((regparm(0))) *kernel_allocate(size_t size)
    2.70 + {
    2.71 +-  void *ret  = (void*)kmalloc(size, GFP_KERNEL);
    2.72 +-  if (!ret) panic(KERN_CRIT TPM_MODULE_NAME 
    2.73 +-    "GMP: cannot allocate memory (size=%Zu)\n", size);
    2.74 ++  void *ret  = (void*)malloc(size);
    2.75 ++  if (!ret) error("GMP: cannot allocate memory (size=%Zu)\n", size);
    2.76 +   return ret;
    2.77 + }
    2.78 + 
    2.79 + void __attribute__ ((regparm(0))) *kernel_reallocate(void *oldptr, 
    2.80 +   size_t old_size, size_t new_size)
    2.81 + {
    2.82 +-  void *ret = (void*)kmalloc(new_size, GFP_KERNEL);
    2.83 +-  if (!ret) panic(KERN_CRIT TPM_MODULE_NAME "GMP: Cannot reallocate memory "
    2.84 ++  void *ret = (void*)malloc(new_size);
    2.85 ++  if (!ret) error("GMP: Cannot reallocate memory "
    2.86 +     "(old_size=%Zu new_size=%Zu)\n", old_size, new_size);
    2.87 +   memcpy(ret, oldptr, old_size);
    2.88 +-  kfree(oldptr);
    2.89 ++  free(oldptr);
    2.90 +   return ret;
    2.91 + }
    2.92 + 
    2.93 +@@ -99,7 +94,7 @@ void __attribute__ ((regparm(0))) kernel
    2.94 +   /* overwrite used memory */
    2.95 +   if (blk_ptr != NULL) { 
    2.96 +     memset(blk_ptr, 0, blk_size);
    2.97 +-    kfree(blk_ptr);
    2.98 ++    free(blk_ptr);
    2.99 +   }
   2.100 + }
   2.101 + 
   2.102 +diff -uprN orig/tpm_emulator-0.3-x86_64/crypto/rsa.c tpm_emulator/crypto/rsa.c
   2.103 +--- orig/tpm_emulator-0.3-x86_64/crypto/rsa.c	2006-01-10 04:21:45.000000000 -0800
   2.104 ++++ tpm_emulator/crypto/rsa.c	2006-06-02 13:36:03.000000000 -0700
   2.105 +@@ -1,5 +1,6 @@
   2.106 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   2.107 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   2.108 ++ * Copyright (C) 2005 INTEL Corp
   2.109    *
   2.110    * This module is free software; you can redistribute it and/or modify
   2.111    * it under the terms of the GNU General Public License as published
   2.112 -@@ -35,7 +36,7 @@
   2.113 - #include "tpm_version.h"
   2.114 +@@ -381,7 +382,7 @@ static int encode_message(int type, uint
   2.115 +       msg[0] = 0x00;
   2.116 +       get_random_bytes(&msg[1], SHA1_DIGEST_LENGTH);
   2.117 +       sha1_init(&ctx);
   2.118 +-      sha1_update(&ctx, "TCPA", 4);
   2.119 ++      sha1_update(&ctx, (uint8_t *) "TCPA", 4);
   2.120 +       sha1_final(&ctx, &msg[1 + SHA1_DIGEST_LENGTH]);
   2.121 +       memset(&msg[1 + 2 * SHA1_DIGEST_LENGTH], 0x00, 
   2.122 +         msg_len - data_len - 2 * SHA1_DIGEST_LENGTH - 2);
   2.123 +@@ -429,7 +430,7 @@ static int decode_message(int type, uint
   2.124 +       mask_generation(&msg[1], SHA1_DIGEST_LENGTH,
   2.125 +         &msg[1 + SHA1_DIGEST_LENGTH], msg_len - SHA1_DIGEST_LENGTH - 1);
   2.126 +       sha1_init(&ctx);
   2.127 +-      sha1_update(&ctx, "TCPA", 4);
   2.128 ++      sha1_update(&ctx, (uint8_t *) "TCPA", 4);
   2.129 +       sha1_final(&ctx, &msg[1]);
   2.130 +       if (memcmp(&msg[1], &msg[1 + SHA1_DIGEST_LENGTH], 
   2.131 +           SHA1_DIGEST_LENGTH) != 0) return -1;
   2.132 +diff -uprN orig/tpm_emulator-0.3-x86_64/linux_module.c tpm_emulator/linux_module.c
   2.133 +--- orig/tpm_emulator-0.3-x86_64/linux_module.c	2006-06-05 09:54:21.000000000 -0700
   2.134 ++++ tpm_emulator/linux_module.c	1969-12-31 16:00:00.000000000 -0800
   2.135 +@@ -1,194 +0,0 @@
   2.136 +-/* Software-Based Trusted Platform Module (TPM) Emulator for Linux 
   2.137 +- * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   2.138 +- *
   2.139 +- * This module is free software; you can redistribute it and/or modify 
   2.140 +- * it under the terms of the GNU General Public License as published 
   2.141 +- * by the Free Software Foundation; either version 2 of the License, 
   2.142 +- * or (at your option) any later version.  
   2.143 +- *
   2.144 +- * This module is distributed in the hope that it will be useful, 
   2.145 +- * but WITHOUT ANY WARRANTY; without even the implied warranty of 
   2.146 +- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
   2.147 +- * GNU General Public License for more details.
   2.148 +- *
   2.149 +- * $Id: linux_module.c 76 2006-01-02 22:17:58Z hstamer $
   2.150 +- */
   2.151 +-
   2.152 +-#include <linux/module.h>
   2.153 +-#include <linux/kernel.h>
   2.154 +-#include <linux/init.h>
   2.155 +-#include <linux/miscdevice.h>
   2.156 +-#include <linux/poll.h>
   2.157 +-#include "linux_module.h"
   2.158 +-#include "tpm/tpm_emulator.h"
   2.159 +-
   2.160 +-MODULE_LICENSE("GPL");
   2.161 +-MODULE_AUTHOR("Mario Strasser <mast@gmx.net>");
   2.162 +-MODULE_DESCRIPTION("Trusted Platform Module (TPM) Emulator");
   2.163 +-MODULE_SUPPORTED_DEVICE(TPM_DEVICE_NAME);
   2.164 +-
   2.165 +-/* module startup parameters */
   2.166 +-char *startup = "save";
   2.167 +-module_param(startup, charp, 0444);
   2.168 +-MODULE_PARM_DESC(startup, " Sets the startup mode of the TPM. "
   2.169 +-  "Possible values are 'clear', 'save' (default) and 'deactivated.");
   2.170 +-char *storage_file = "/var/tpm/tpm_emulator-1.2.0.2";
   2.171 +-module_param(storage_file, charp, 0644);
   2.172 +-MODULE_PARM_DESC(storage_file, " Sets the persistent-data storage " 
   2.173 +-  "file of the TPM.");
   2.174 +-
   2.175 +-/* TPM lock */
   2.176 +-static struct semaphore tpm_mutex;
   2.177 +-
   2.178 +-/* TPM command response */
   2.179 +-static struct {
   2.180 +-  uint8_t *data;
   2.181 +-  uint32_t size;
   2.182 +-} tpm_response;
   2.183 +-
   2.184 +-/* module state */
   2.185 +-#define STATE_IS_OPEN 0
   2.186 +-static uint32_t module_state;
   2.187 +-
   2.188 +-static int tpm_open(struct inode *inode, struct file *file)
   2.189 +-{
   2.190 +-  debug("%s()", __FUNCTION__);
   2.191 +-  if (test_and_set_bit(STATE_IS_OPEN, (void*)&module_state)) return -EBUSY;
   2.192 +-  return 0;
   2.193 +-}
   2.194 +-
   2.195 +-static int tpm_release(struct inode *inode, struct file *file)
   2.196 +-{
   2.197 +-  debug("%s()", __FUNCTION__);
   2.198 +-  clear_bit(STATE_IS_OPEN, (void*)&module_state);
   2.199 +-  down(&tpm_mutex);
   2.200 +-  if (tpm_response.data != NULL) {
   2.201 +-    kfree(tpm_response.data);
   2.202 +-    tpm_response.data = NULL;
   2.203 +-  }
   2.204 +-  up(&tpm_mutex);
   2.205 +-  return 0;
   2.206 +-}
   2.207 +-
   2.208 +-static ssize_t tpm_read(struct file *file, char *buf, size_t count, loff_t *ppos)
   2.209 +-{
   2.210 +-  debug("%s(%Zu)", __FUNCTION__, count);
   2.211 +-  down(&tpm_mutex);
   2.212 +-  if (tpm_response.data != NULL) {
   2.213 +-    count = min(count, (size_t)tpm_response.size - (size_t)*ppos);
   2.214 +-    count -= copy_to_user(buf, &tpm_response.data[*ppos], count);
   2.215 +-    *ppos += count;
   2.216 +-    if ((size_t)tpm_response.size == (size_t)*ppos) {
   2.217 +-      kfree(tpm_response.data);
   2.218 +-      tpm_response.data = NULL;
   2.219 +-    }
   2.220 +-  } else {
   2.221 +-    count = 0;
   2.222 +-  }
   2.223 +-  up(&tpm_mutex);
   2.224 +-  return count;
   2.225 +-}
   2.226 +-
   2.227 +-static ssize_t tpm_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
   2.228 +-{
   2.229 +-  debug("%s(%Zu)", __FUNCTION__, count);
   2.230 +-  down(&tpm_mutex);
   2.231 +-  *ppos = 0;
   2.232 +-  if (tpm_response.data != NULL) kfree(tpm_response.data);
   2.233 +-  if (tpm_handle_command(buf, count, &tpm_response.data, 
   2.234 +-                         &tpm_response.size) != 0) { 
   2.235 +-    count = -EILSEQ;
   2.236 +-    tpm_response.data = NULL;
   2.237 +-  }
   2.238 +-  up(&tpm_mutex);
   2.239 +-  return count;
   2.240 +-}
   2.241 +-
   2.242 +-#define TPMIOC_CANCEL   _IO('T', 0x00)
   2.243 +-#define TPMIOC_TRANSMIT _IO('T', 0x01)
   2.244 +-
   2.245 +-static int tpm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
   2.246 +-{
   2.247 +-  debug("%s(%d, %p)", __FUNCTION__, cmd, (char*)arg);
   2.248 +-  if (cmd == TPMIOC_TRANSMIT) {
   2.249 +-    uint32_t count = ntohl(*(uint32_t*)(arg + 2));
   2.250 +-    down(&tpm_mutex);
   2.251 +-    if (tpm_response.data != NULL) kfree(tpm_response.data);
   2.252 +-    if (tpm_handle_command((char*)arg, count, &tpm_response.data,
   2.253 +-                           &tpm_response.size) == 0) {
   2.254 +-      tpm_response.size -= copy_to_user((char*)arg, tpm_response.data,
   2.255 +-                            tpm_response.size);
   2.256 +-      kfree(tpm_response.data);
   2.257 +-      tpm_response.data = NULL;
   2.258 +-    } else {
   2.259 +-      tpm_response.size = 0;
   2.260 +-      tpm_response.data = NULL;
   2.261 +-    }
   2.262 +-    up(&tpm_mutex);
   2.263 +-    return tpm_response.size;
   2.264 +-  }
   2.265 +-  return -1;
   2.266 +-}
   2.267 +-
   2.268 +-struct file_operations fops = {
   2.269 +-  .owner   = THIS_MODULE,
   2.270 +-  .open    = tpm_open,
   2.271 +-  .release = tpm_release,
   2.272 +-  .read    = tpm_read,
   2.273 +-  .write   = tpm_write,
   2.274 +-  .ioctl   = tpm_ioctl,
   2.275 +-};
   2.276 +-
   2.277 +-static struct miscdevice tpm_dev = {
   2.278 +-  .minor      = TPM_DEVICE_MINOR, 
   2.279 +-  .name       = TPM_DEVICE_NAME, 
   2.280 +-  .fops       = &fops,
   2.281 +-};
   2.282 +-
   2.283 +-int __init init_tpm_module(void)
   2.284 +-{
   2.285 +-  int res = misc_register(&tpm_dev);
   2.286 +-  if (res != 0) {
   2.287 +-    error("misc_register() failed for minor %d\n", TPM_DEVICE_MINOR);
   2.288 +-    return res;
   2.289 +-  }
   2.290 +-  /* initialize variables */
   2.291 +-  sema_init(&tpm_mutex, 1);
   2.292 +-  module_state = 0;
   2.293 +-  tpm_response.data = NULL;    
   2.294 +-  /* initialize TPM emulator */
   2.295 +-  if (!strcmp(startup, "clear")) {
   2.296 +-    tpm_emulator_init(1);
   2.297 +-  } else if (!strcmp(startup, "save")) {
   2.298 +-    tpm_emulator_init(2);
   2.299 +-  } else if (!strcmp(startup, "deactivated")) {
   2.300 +-    tpm_emulator_init(3);
   2.301 +-  } else {
   2.302 +-    error("invalid startup mode '%s'; must be 'clear', "
   2.303 +-      "'save' (default) or 'deactivated", startup);
   2.304 +-    misc_deregister(&tpm_dev);
   2.305 +-    return -EINVAL;
   2.306 +-  }
   2.307 +-  return 0;
   2.308 +-}
   2.309 +-
   2.310 +-void __exit cleanup_tpm_module(void)
   2.311 +-{
   2.312 +-  tpm_emulator_shutdown();
   2.313 +-  misc_deregister(&tpm_dev);
   2.314 +-  if (tpm_response.data != NULL) kfree(tpm_response.data);
   2.315 +-}
   2.316 +-
   2.317 +-module_init(init_tpm_module);
   2.318 +-module_exit(cleanup_tpm_module);
   2.319 +-
   2.320 +-uint64_t tpm_get_ticks(void)
   2.321 +-{
   2.322 +-  static struct timespec old_time = {0, 0};
   2.323 +-  struct timespec new_time = current_kernel_time();
   2.324 +-  uint64_t ticks = (uint64_t)(old_time.tv_sec - new_time.tv_sec) * 1000000
   2.325 +-                   + (old_time.tv_nsec - new_time.tv_nsec) / 1000;
   2.326 +-  old_time = new_time;
   2.327 +-  return (ticks > 0) ? ticks : 1;
   2.328 +-}
   2.329 +-
   2.330 +diff -uprN orig/tpm_emulator-0.3-x86_64/linux_module.h tpm_emulator/linux_module.h
   2.331 +--- orig/tpm_emulator-0.3-x86_64/linux_module.h	2006-06-05 09:54:21.000000000 -0700
   2.332 ++++ tpm_emulator/linux_module.h	2006-06-02 13:36:03.000000000 -0700
   2.333 +@@ -1,5 +1,6 @@
   2.334 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   2.335 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   2.336 ++ * Copyright (C) 2005 INTEL Corp
   2.337 +  *
   2.338 +  * This module is free software; you can redistribute it and/or modify
   2.339 +  * it under the terms of the GNU General Public License as published
   2.340 +@@ -17,17 +18,22 @@
   2.341 + #ifndef _LINUX_MODULE_H_
   2.342 + #define _LINUX_MODULE_H_
   2.343   
   2.344 - #define TPM_DEVICE_MINOR	224
   2.345 --#define TPM_DEVICE_NAME         "tpm"
   2.346 -+#define TPM_DEVICE_NAME         "tpm0"
   2.347 +-#include <linux/version.h>
   2.348 +-#include <linux/kernel.h>
   2.349 +-#include <linux/slab.h>
   2.350 ++#include <malloc.h>
   2.351 ++#include <stdint.h>
   2.352 ++#include <stdio.h>
   2.353 ++#include <string.h>
   2.354 + #include <linux/types.h>
   2.355 +-#include <linux/string.h>
   2.356 +-#include <linux/random.h>
   2.357 +-#include <linux/time.h>
   2.358 +-#include <asm/byteorder.h>
   2.359 + 
   2.360 +-/* module settings */
   2.361 ++#include <endian.h>
   2.362 ++#define __BYTEORDER_HAS_U64__
   2.363 ++#ifdef LITTLE_ENDIAN
   2.364 ++ #include <linux/byteorder/little_endian.h>
   2.365 ++#else
   2.366 ++ #include <linux/byteorder/big_endian.h>
   2.367 ++#endif
   2.368 + 
   2.369 ++/* module settings */
   2.370 ++#define min(A,B) ((A)<(B)?(A):(B))
   2.371 + #ifndef STR
   2.372 + #define STR(s) __STR__(s)
   2.373 + #define __STR__(s) #s
   2.374 +@@ -38,35 +44,36 @@
   2.375 + #define TPM_DEVICE_NAME         "tpm"
   2.376   #define TPM_MODULE_NAME 	"tpm_emulator"
   2.377   
   2.378 - /* debug and log output functions */
   2.379 -diff -uprN orig/tpm_emulator-0.2-x86_64/Makefile tpm_emulator/Makefile
   2.380 ---- orig/tpm_emulator-0.2-x86_64/Makefile	2005-09-15 19:21:14.845078568 -0700
   2.381 -+++ tpm_emulator/Makefile	2005-09-14 20:27:22.000000000 -0700
   2.382 -@@ -1,16 +1,22 @@
   2.383 +-/* debug and log output functions */
   2.384 +-
   2.385 + #ifdef DEBUG
   2.386 +-#define debug(fmt, ...) printk(KERN_DEBUG "%s %s:%d: Debug: " fmt "\n", \
   2.387 +-                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   2.388 ++#define debug(fmt, ...) printf("TPMD: %s:%d: Debug: " fmt "\n", \
   2.389 ++                        __FILE__, __LINE__, ## __VA_ARGS__)
   2.390 + #else
   2.391 + #define debug(fmt, ...) 
   2.392 + #endif
   2.393 +-#define info(fmt, ...)  printk(KERN_INFO "%s %s:%d: Info: " fmt "\n", \
   2.394 +-                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   2.395 +-#define error(fmt, ...) printk(KERN_ERR "%s %s:%d: Error: " fmt "\n", \
   2.396 +-                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   2.397 +-#define alert(fmt, ...) printk(KERN_ALERT "%s %s:%d: Alert: " fmt "\n", \
   2.398 +-                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   2.399 ++#define info(fmt, ...)  printf("TPMD: %s:%d: Info: " fmt "\n", \
   2.400 ++                        __FILE__, __LINE__, ## __VA_ARGS__)
   2.401 ++#define error(fmt, ...) printf("TPMD: %s:%d: Error: " fmt "\n", \
   2.402 ++                        __FILE__, __LINE__, ## __VA_ARGS__)
   2.403 ++#define alert(fmt, ...) printf("TPMD: %s:%d: Alert: " fmt "\n", \
   2.404 ++                        __FILE__, __LINE__, ## __VA_ARGS__)
   2.405 + 
   2.406 + /* memory allocation */
   2.407 + 
   2.408 + static inline void *tpm_malloc(size_t size) 
   2.409 + {
   2.410 +-  return kmalloc(size, GFP_KERNEL);  
   2.411 ++  return malloc(size);  
   2.412 + }
   2.413 + 
   2.414 + static inline void tpm_free(const void *ptr)
   2.415 + {
   2.416 +-  if (ptr != NULL) kfree(ptr);
   2.417 ++  if (ptr != NULL) free( (void *) ptr);
   2.418 + }
   2.419 + 
   2.420 + /* random numbers */
   2.421 + 
   2.422 ++//FIXME;
   2.423 ++void get_random_bytes(void *buf, int nbytes);
   2.424 ++
   2.425 + static inline void tpm_get_random_bytes(void *buf, int nbytes)
   2.426 + {
   2.427 +   get_random_bytes(buf, nbytes);
   2.428 +@@ -86,9 +93,9 @@ uint64_t tpm_get_ticks(void);
   2.429 + #define CPU_TO_LE16(x) __cpu_to_le16(x)
   2.430 + 
   2.431 + #define BE64_TO_CPU(x) __be64_to_cpu(x)
   2.432 +-#define LE64_TO_CPU(x) __be64_to_cpu(x)
   2.433 ++#define LE64_TO_CPU(x) __le64_to_cpu(x)
   2.434 + #define BE32_TO_CPU(x) __be32_to_cpu(x)
   2.435 +-#define LE32_TO_CPU(x) __be32_to_cpu(x)
   2.436 ++#define LE32_TO_CPU(x) __le32_to_cpu(x)
   2.437 + #define BE16_TO_CPU(x) __be16_to_cpu(x)
   2.438 + #define LE16_TO_CPU(x) __le16_to_cpu(x)
   2.439 + 
   2.440 +diff -uprN orig/tpm_emulator-0.3-x86_64/Makefile tpm_emulator/Makefile
   2.441 +--- orig/tpm_emulator-0.3-x86_64/Makefile	2006-06-05 09:54:21.000000000 -0700
   2.442 ++++ tpm_emulator/Makefile	2006-06-02 13:36:03.000000000 -0700
   2.443 +@@ -1,22 +1,31 @@
   2.444   # Software-Based Trusted Platform Module (TPM) Emulator for Linux
   2.445   # Copyright (C) 2004 Mario Strasser <mast@gmx.net>
   2.446 -+# Copyright (C) 2005 INTEL Corp.
   2.447 ++# Copyright (C) 2006 INTEL Corp.
   2.448   #
   2.449 - # $Id: Makefile 10 2005-04-26 20:59:50Z mast $
   2.450 + # $Id: Makefile 69 2005-12-13 12:55:52Z mast $
   2.451   
   2.452 -+XEN_ROOT       := ../../..
   2.453 -+EUID           := $(shell id -u)
   2.454 -+
   2.455 - # kernel settings
   2.456 - KERNEL_RELEASE := $(shell uname -r)
   2.457 +-# kernel settings
   2.458 +-KERNEL_RELEASE := $(shell uname -r)
   2.459  -KERNEL_BUILD   := /lib/modules/$(KERNEL_RELEASE)/build
   2.460 -+CUR_DIR        := $(shell pwd)
   2.461 -+LINUX_VERSION  := $(shell cat $(CUR_DIR)/$(XEN_ROOT)/buildconfigs/mk.linux-2.6-xen | grep "LINUX_VER" | grep "2.6" | gawk '{ print $$3 }' )
   2.462 -+KERNEL_BUILD   := $(XEN_ROOT)/linux-$(LINUX_VERSION)-xen
   2.463 - MOD_SUBDIR     := misc
   2.464 +-MOD_SUBDIR     := misc
   2.465   COMPILE_ARCH    ?= $(shell uname -m | sed -e s/i.86/x86_32/)
   2.466   
   2.467   # module settings
   2.468  -MODULE_NAME    := tpm_emulator
   2.469  +BIN            := tpm_emulator
   2.470   VERSION_MAJOR  := 0
   2.471 - VERSION_MINOR  := 2
   2.472 + VERSION_MINOR  := 3
   2.473   VERSION_BUILD  := $(shell date +"%s")
   2.474 -@@ -34,11 +38,9 @@ DIRS           := . crypto tpm 
   2.475 + 
   2.476 +-# enable/disable DEBUG messages
   2.477 +-EXTRA_CFLAGS   += -Wall -DDEBUG -g  
   2.478 ++# Installation program and options
   2.479 ++INSTALL         = install
   2.480 ++INSTALL_PROG    = $(INSTALL) -m0755
   2.481 ++INSTALL_DIR     = $(INSTALL) -d -m0755
   2.482 ++
   2.483 ++# Xen tools installation directory
   2.484 ++TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
   2.485 ++
   2.486 ++CC      := gcc
   2.487 ++CFLAGS  += -g -Wall $(INCLUDE) -DDEBUG
   2.488 ++CFLAGS  += -I. -Itpm
   2.489 ++
   2.490 ++# Is the simulator running in it's own vm?
   2.491 ++#CFLAGS += -DVTPM_MULTI_VM
   2.492 + 
   2.493 + ifeq ($(COMPILE_ARCH),x86_64)
   2.494 + LIBDIR = lib64
   2.495 +@@ -34,38 +43,31 @@ DIRS           := . crypto tpm 
   2.496   SRCS           := $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.c))
   2.497   OBJS           := $(patsubst %.c, %.o, $(SRCS))
   2.498   SRCS           += $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.h))
   2.499 @@ -71,13 +456,23 @@ diff -uprN orig/tpm_emulator-0.2-x86_64/
   2.500   
   2.501  -obj-m               := $(MODULE_NAME).o
   2.502  -$(MODULE_NAME)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
   2.503 -+obj-m               := $(BIN).o
   2.504 ++obj-m               := $(BIN)
   2.505  +$(BIN)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
   2.506   
   2.507   EXTRA_CFLAGS   += -I$(src) -I$(src)/crypto -I$(src)/tpm 
   2.508   
   2.509 -@@ -49,23 +51,17 @@ all:	$(src)/crypto/gmp.h $(src)/crypto/l
   2.510 - 	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules
   2.511 + # do not print "Entering directory ..."
   2.512 + MAKEFLAGS      += --no-print-directory
   2.513 + 
   2.514 +-all:	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version
   2.515 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules
   2.516 ++all: $(BIN)
   2.517 ++
   2.518 ++$(BIN):	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version $(SRCS) $(OBJS)
   2.519 ++	$(CC) $(CFLAGS) $(OBJS) $(src)/crypto/libgmp.a -o $(BIN)
   2.520 ++
   2.521 ++%.o: %.c
   2.522 ++	$(CC) $(CFLAGS) -c $< -o $@
   2.523   
   2.524   install:
   2.525  -	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules_install
   2.526 @@ -85,15 +480,12 @@ diff -uprN orig/tpm_emulator-0.2-x86_64/
   2.527  -	test -c /dev/tpm || mknod /dev/tpm c 10 224
   2.528  -	chmod 666 /dev/tpm
   2.529  -	depmod -a
   2.530 -+	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) INSTALL_MOD_PATH=$(DESTDIR) modules_install
   2.531 -+	test -d $(DESTDIR)/var/tpm || mkdir $(DESTDIR)/var/tpm
   2.532 -+	test -d $(DESTDIR)/dev || mkdir $(DESTDIR)/dev
   2.533 -+	test -c $(DESTDIR)/dev/tpm0 || [ $(EUID) -ne 0 ] || mknod $(DESTDIR)/dev/tpm0 c 10 224
   2.534 -+	[ $(EUID) -ne 0 ] || chmod 666 $(DESTDIR)/dev/tpm0
   2.535 ++	$(INSTALL_PROG) $(BIN) $(TOOLS_INSTALL_DIR)
   2.536   
   2.537   clean:
   2.538 - 	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) clean
   2.539 - 	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a
   2.540 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) clean
   2.541 +-	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a
   2.542 ++	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a $(OBJS)
   2.543   
   2.544  -dist:	$(DISTSRC)
   2.545  -	rm -rf $(DISTDIR)
   2.546 @@ -103,25 +495,461 @@ diff -uprN orig/tpm_emulator-0.2-x86_64/
   2.547  -	tar -chzf $(DISTDIR).tar.gz $(DISTDIR)
   2.548  -	rm -rf $(DISTDIR)
   2.549  +mrproper: clean
   2.550 ++	rm -f $(BIN) tpm_version.h
   2.551   
   2.552   $(src)/crypto/libgmp.a:
   2.553   	test -f $(src)/crypto/libgmp.a || ln -s $(GMP_LIB) $(src)/crypto/libgmp.a
   2.554 -diff -uprN orig/tpm_emulator-0.2-x86_64/README tpm_emulator/README
   2.555 ---- orig/tpm_emulator-0.2-x86_64/README	2005-08-15 00:58:57.000000000 -0700
   2.556 -+++ tpm_emulator/README	2005-09-14 20:27:22.000000000 -0700
   2.557 -@@ -13,7 +13,8 @@ $Id: README 8 2005-01-25 21:11:45Z jmoli
   2.558 +diff -uprN orig/tpm_emulator-0.3-x86_64/README tpm_emulator/README
   2.559 +--- orig/tpm_emulator-0.3-x86_64/README	2006-06-05 09:54:21.000000000 -0700
   2.560 ++++ tpm_emulator/README	2006-06-02 13:36:03.000000000 -0700
   2.561 +@@ -13,7 +13,8 @@ $Id: README 78 2006-01-07 10:45:39Z mast
   2.562   Copyright
   2.563   --------------------------------------------------------------------------
   2.564   Copyright (C) 2004 Mario Strasser <mast@gmx.net> and Swiss Federal 
   2.565  -Institute of Technology (ETH) Zurich.
   2.566  +                   Institute of Technology (ETH) Zurich.
   2.567 -+Copyright (C) 2005 
   2.568 ++Copyright (C) 2005 INTEL Corp 
   2.569                 
   2.570   This program is free software; you can redistribute it and/or modify
   2.571   it under the terms of the GNU General Public License as published by
   2.572 -diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_data.c tpm_emulator/tpm/tpm_data.c
   2.573 ---- orig/tpm_emulator-0.2-x86_64/tpm/tpm_data.c	2005-09-15 19:21:14.847078264 -0700
   2.574 -+++ tpm_emulator/tpm/tpm_data.c	2005-09-14 20:27:22.000000000 -0700
   2.575 +diff -uprN orig/tpm_emulator-0.3-x86_64/README.1st tpm_emulator/README.1st
   2.576 +--- orig/tpm_emulator-0.3-x86_64/README.1st	1969-12-31 16:00:00.000000000 -0800
   2.577 ++++ tpm_emulator/README.1st	2006-06-02 13:37:48.000000000 -0700
   2.578 +@@ -0,0 +1 @@
   2.579 ++Note that you must manually create /tmp/tpm_in.fifo and /tmp/tpm_out.fifo for this emulator to work.
   2.580 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_audit.c tpm_emulator/tpm/tpm_audit.c
   2.581 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_audit.c	2006-01-10 04:21:45.000000000 -0800
   2.582 ++++ tpm_emulator/tpm/tpm_audit.c	2006-06-02 13:36:03.000000000 -0700
   2.583 +@@ -1,6 +1,7 @@
   2.584 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   2.585 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   2.586 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   2.587 ++ * Copyright (C) 2005 INTEL Corp
   2.588 +  *
   2.589 +  * This module is free software; you can redistribute it and/or modify
   2.590 +  * it under the terms of the GNU General Public License as published
   2.591 +@@ -45,14 +46,14 @@ void tpm_audit_request(TPM_COMMAND_CODE 
   2.592 +       tpmData.permanent.data.auditMonotonicCounter++;
   2.593 +     }
   2.594 +     /* update audit digest */
   2.595 +-    *((UINT16*)&buf[0])  = cpu_to_be16(TPM_TAG_AUDIT_EVENT_IN);
   2.596 +-    *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
   2.597 ++    *((UINT16*)&buf[0])  = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_IN);
   2.598 ++    *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
   2.599 +     sha1_init(&sha1_ctx);
   2.600 +     sha1_update(&sha1_ctx, req->param, req->paramSize);
   2.601 +     sha1_final(&sha1_ctx, &buf[6]);
   2.602 +-    *((UINT16*)&buf[26])  = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
   2.603 ++    *((UINT16*)&buf[26])  = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
   2.604 +     memset(&buf[30], 0, 4);
   2.605 +-    *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
   2.606 ++    *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
   2.607 +     sha1_init(&sha1_ctx);
   2.608 +     sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest, 
   2.609 +       sizeof(TPM_DIGEST));
   2.610 +@@ -70,15 +71,15 @@ void tpm_audit_response(TPM_COMMAND_CODE
   2.611 +       && (AUDIT_STATUS[ord / 8] & (1 << (ord & 0x07)))) {
   2.612 +     info("tpm_audit_response()");
   2.613 +     /* update audit digest */
   2.614 +-    *((UINT16*)&buf[0])  = cpu_to_be16(TPM_TAG_AUDIT_EVENT_OUT);
   2.615 +-    *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
   2.616 ++    *((UINT16*)&buf[0])  = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_OUT);
   2.617 ++    *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
   2.618 +     sha1_init(&sha1_ctx);
   2.619 +     sha1_update(&sha1_ctx, rsp->param, rsp->paramSize);
   2.620 +     sha1_final(&sha1_ctx, &buf[6]);
   2.621 +-    *((UINT16*)&buf[26])  = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
   2.622 ++    *((UINT16*)&buf[26])  = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
   2.623 +     memset(&buf[30], 0, 4);
   2.624 +-    *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
   2.625 +-    *((UINT32*)&buf[34]) = cpu_to_be32(rsp->result);
   2.626 ++    *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
   2.627 ++    *((UINT32*)&buf[34]) = CPU_TO_BE32(rsp->result);
   2.628 +     sha1_init(&sha1_ctx);
   2.629 +     sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest, 
   2.630 +       sizeof(TPM_DIGEST));
   2.631 +@@ -158,7 +159,7 @@ TPM_RESULT TPM_GetAuditDigestSigned(TPM_
   2.632 +   }
   2.633 +   memcpy(&buf[0], "\x05\x00ADIG", 6);
   2.634 +   memcpy(&buf[6], antiReplay->nonce, 20);
   2.635 +-  *(UINT32*)&buf[26] = cpu_to_be32(buf_size - 30);
   2.636 ++  *(UINT32*)&buf[26] = CPU_TO_BE32(buf_size - 30);
   2.637 +   memcpy(&buf[30], auditDigest->digest, 20);
   2.638 +   ptr = &buf[50];
   2.639 +   len = buf_size - 50;
   2.640 +@@ -198,4 +199,3 @@ TPM_RESULT TPM_SetOrdinalAuditStatus(TPM
   2.641 +   }
   2.642 +   return TPM_SUCCESS;
   2.643 + }
   2.644 +-
   2.645 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_authorization.c tpm_emulator/tpm/tpm_authorization.c
   2.646 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_authorization.c	2006-01-10 04:21:45.000000000 -0800
   2.647 ++++ tpm_emulator/tpm/tpm_authorization.c	2006-06-02 13:36:03.000000000 -0700
   2.648 +@@ -1,6 +1,7 @@
   2.649 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   2.650 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   2.651 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   2.652 ++ * Copyright (C) 2005 INTEL Corp
   2.653 +  *
   2.654 +  * This module is free software; you can redistribute it and/or modify
   2.655 +  * it under the terms of the GNU General Public License as published
   2.656 +@@ -279,7 +280,7 @@ TPM_RESULT tpm_verify_auth(TPM_AUTH *aut
   2.657 + {
   2.658 +   hmac_ctx_t ctx;
   2.659 +   TPM_SESSION_DATA *session;
   2.660 +-  UINT32 auth_handle = cpu_to_be32(auth->authHandle);
   2.661 ++  UINT32 auth_handle = CPU_TO_BE32(auth->authHandle);
   2.662 +   
   2.663 +   info("tpm_verify_auth(%08x)", auth->authHandle);
   2.664 +   /* get dedicated authorization or transport session */
   2.665 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_capability.c tpm_emulator/tpm/tpm_capability.c
   2.666 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_capability.c	2006-01-10 04:21:45.000000000 -0800
   2.667 ++++ tpm_emulator/tpm/tpm_capability.c	2006-06-02 13:36:03.000000000 -0700
   2.668 +@@ -1,6 +1,7 @@
   2.669 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   2.670 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   2.671 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   2.672 ++ * Copyright (C) 2005 INTEL Corp
   2.673 +  *
   2.674 +  * This module is free software; you can redistribute it and/or modify
   2.675 +  * it under the terms of the GNU General Public License as published
   2.676 +@@ -406,7 +407,7 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
   2.677 + 
   2.678 +     case TPM_CAP_KEY_HANDLE:
   2.679 +       debug("[TPM_CAP_KEY_HANDLE]");
   2.680 +-      subCapSize = cpu_to_be32(TPM_RT_KEY);
   2.681 ++      subCapSize = CPU_TO_BE32(TPM_RT_KEY);
   2.682 +       return cap_handle(4, (BYTE*)&subCapSize, respSize, resp);
   2.683 + 
   2.684 +     case TPM_CAP_CHECK_LOADED:
   2.685 +@@ -480,4 +481,3 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
   2.686 +       return TPM_BAD_MODE;
   2.687 +   }
   2.688 + }
   2.689 +-
   2.690 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_cmd_handler.c tpm_emulator/tpm/tpm_cmd_handler.c
   2.691 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_cmd_handler.c	2006-01-10 04:21:45.000000000 -0800
   2.692 ++++ tpm_emulator/tpm/tpm_cmd_handler.c	2006-06-02 13:36:03.000000000 -0700
   2.693 +@@ -1,6 +1,7 @@
   2.694 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   2.695 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   2.696 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   2.697 ++ * Copyright (C) 2005 INTEL Corp
   2.698 +  *
   2.699 +  * This module is free software; you can redistribute it and/or modify
   2.700 +  * it under the terms of the GNU General Public License as published
   2.701 +@@ -73,7 +74,7 @@ void tpm_compute_in_param_digest(TPM_REQ
   2.702 + {
   2.703 +   sha1_ctx_t sha1;
   2.704 +   UINT32 offset = tpm_get_param_offset(req->ordinal);
   2.705 +-  UINT32 ord = cpu_to_be32(req->ordinal);
   2.706 ++  UINT32 ord = CPU_TO_BE32(req->ordinal);
   2.707 + 
   2.708 +   /* compute SHA1 hash */
   2.709 +   if (offset <= req->paramSize) {
   2.710 +@@ -89,8 +90,8 @@ void tpm_compute_in_param_digest(TPM_REQ
   2.711 + void tpm_compute_out_param_digest(TPM_COMMAND_CODE ordinal, TPM_RESPONSE *rsp)
   2.712 + {
   2.713 +   sha1_ctx_t sha1;
   2.714 +-  UINT32 res = cpu_to_be32(rsp->result);
   2.715 +-  UINT32 ord = cpu_to_be32(ordinal);
   2.716 ++  UINT32 res = CPU_TO_BE32(rsp->result);
   2.717 ++  UINT32 ord = CPU_TO_BE32(ordinal);
   2.718 + 
   2.719 +   /* compute SHA1 hash */
   2.720 +   sha1_init(&sha1);
   2.721 +@@ -3123,7 +3124,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
   2.722 +       hmac_update(&hmac, rsp->auth2->digest, sizeof(rsp->auth2->digest));
   2.723 + #if 0
   2.724 +       if (tpm_get_auth(rsp->auth2->authHandle)->type == TPM_ST_OIAP) {
   2.725 +-        UINT32 handle = cpu_to_be32(rsp->auth2->authHandle);
   2.726 ++        UINT32 handle = CPU_TO_BE32(rsp->auth2->authHandle);
   2.727 +         hmac_update(&hmac, (BYTE*)&handle, 4);
   2.728 +       }
   2.729 + #endif
   2.730 +@@ -3138,7 +3139,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
   2.731 +       hmac_update(&hmac, rsp->auth1->digest, sizeof(rsp->auth1->digest));
   2.732 + #if 0
   2.733 +       if (tpm_get_auth(rsp->auth1->authHandle)->type == TPM_ST_OIAP) {
   2.734 +-        UINT32 handle = cpu_to_be32(rsp->auth1->authHandle);
   2.735 ++        UINT32 handle = CPU_TO_BE32(rsp->auth1->authHandle);
   2.736 +         hmac_update(&hmac, (BYTE*)&handle, 4);
   2.737 +       }
   2.738 + #endif
   2.739 +@@ -3221,7 +3222,9 @@ extern const char *tpm_error_to_string(T
   2.740 + void tpm_execute_command(TPM_REQUEST *req, TPM_RESPONSE *rsp)
   2.741 + {
   2.742 +   TPM_RESULT res;
   2.743 +-  
   2.744 ++
   2.745 ++  req->tag = (BYTE) req->tag;  // FIXME: Why is this here
   2.746 ++
   2.747 +   /* setup authorisation as well as response tag and size */
   2.748 +   memset(rsp, 0, sizeof(*rsp));
   2.749 +   switch (req->tag) {
   2.750 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_crypto.c tpm_emulator/tpm/tpm_crypto.c
   2.751 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_crypto.c	2006-06-05 09:54:21.000000000 -0700
   2.752 ++++ tpm_emulator/tpm/tpm_crypto.c	2006-06-02 13:36:03.000000000 -0700
   2.753 +@@ -1,6 +1,7 @@
   2.754 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   2.755 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   2.756 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   2.757 ++ * Copyright (C) 2005 INTEL Corp
   2.758 +  *
   2.759 +  * This module is free software; you can redistribute it and/or modify
   2.760 +  * it under the terms of the GNU General Public License as published
   2.761 +@@ -106,7 +107,7 @@ TPM_RESULT tpm_sign(TPM_KEY_DATA *key, T
   2.762 +     /* setup TPM_SIGN_INFO structure */
   2.763 +     memcpy(&buf[0], "\x05\x00SIGN", 6);
   2.764 +     memcpy(&buf[6], auth->nonceOdd.nonce, 20);
   2.765 +-    *(UINT32*)&buf[26] = cpu_to_be32(areaToSignSize);
   2.766 ++    *(UINT32*)&buf[26] = CPU_TO_BE32(areaToSignSize);
   2.767 +     memcpy(&buf[30], areaToSign, areaToSignSize);
   2.768 +     if (rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1, 
   2.769 +         buf, areaToSignSize + 30, *sig)) {
   2.770 +@@ -383,4 +384,3 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
   2.771 +   }
   2.772 +   return TPM_SUCCESS;
   2.773 + }
   2.774 +-
   2.775 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_daa.c tpm_emulator/tpm/tpm_daa.c
   2.776 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_daa.c	2006-01-10 04:21:45.000000000 -0800
   2.777 ++++ tpm_emulator/tpm/tpm_daa.c	2006-06-02 13:36:03.000000000 -0700
   2.778 +@@ -700,14 +700,14 @@ info("tested until here");
   2.779 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.780 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.781 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.782 +-      sha1_update(&sha1, "\x00", 1);
   2.783 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   2.784 +       sha1_final(&sha1, scratch);
   2.785 +       sha1_init(&sha1);
   2.786 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   2.787 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.788 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.789 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.790 +-      sha1_update(&sha1, "\x01", 1);
   2.791 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.792 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   2.793 +       mpz_init(f), mpz_init(q);
   2.794 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   2.795 +@@ -787,14 +787,14 @@ info("tested until here");
   2.796 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.797 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.798 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.799 +-      sha1_update(&sha1, "\x00", 1);
   2.800 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   2.801 +       sha1_final(&sha1, scratch);
   2.802 +       sha1_init(&sha1);
   2.803 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   2.804 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.805 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.806 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.807 +-      sha1_update(&sha1, "\x01", 1);
   2.808 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.809 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   2.810 +       mpz_init(f), mpz_init(q);
   2.811 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   2.812 +@@ -1440,14 +1440,14 @@ info("tested until here");
   2.813 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.814 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.815 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.816 +-      sha1_update(&sha1, "\x00", 1);
   2.817 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   2.818 +       sha1_final(&sha1, scratch);
   2.819 +       sha1_init(&sha1);
   2.820 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   2.821 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.822 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.823 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.824 +-      sha1_update(&sha1, "\x01", 1);
   2.825 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.826 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   2.827 +       mpz_init(f), mpz_init(q);
   2.828 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   2.829 +@@ -1660,14 +1660,14 @@ info("tested until here");
   2.830 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.831 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.832 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.833 +-      sha1_update(&sha1, "\x00", 1);
   2.834 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   2.835 +       sha1_final(&sha1, scratch);
   2.836 +       sha1_init(&sha1);
   2.837 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   2.838 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.839 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.840 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.841 +-      sha1_update(&sha1, "\x01", 1);
   2.842 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.843 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   2.844 +       mpz_init(f), mpz_init(q);
   2.845 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   2.846 +@@ -1740,14 +1740,14 @@ info("tested until here");
   2.847 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.848 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.849 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.850 +-      sha1_update(&sha1, "\x00", 1);
   2.851 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   2.852 +       sha1_final(&sha1, scratch);
   2.853 +       sha1_init(&sha1);
   2.854 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   2.855 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.856 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.857 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.858 +-      sha1_update(&sha1, "\x01", 1);
   2.859 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.860 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   2.861 +       mpz_init(f), mpz_init(q);
   2.862 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   2.863 +@@ -2828,14 +2828,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   2.864 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.865 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.866 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.867 +-      sha1_update(&sha1, "\x00", 1);
   2.868 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   2.869 +       sha1_final(&sha1, scratch);
   2.870 +       sha1_init(&sha1);
   2.871 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   2.872 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.873 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.874 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.875 +-      sha1_update(&sha1, "\x01", 1);
   2.876 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.877 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   2.878 +       mpz_init(f), mpz_init(q);
   2.879 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   2.880 +@@ -3050,7 +3050,7 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   2.881 +         sha1_init(&sha1);
   2.882 +         sha1_update(&sha1, (BYTE*) &session->DAA_session.DAA_digest, 
   2.883 +           sizeof(session->DAA_session.DAA_digest));
   2.884 +-        sha1_update(&sha1, "\x01", 1);
   2.885 ++        sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.886 +         sha1_update(&sha1, inputData1, inputSize1);
   2.887 +         sha1_final(&sha1, (BYTE*) &session->DAA_session.DAA_digest);
   2.888 +       }
   2.889 +@@ -3078,7 +3078,7 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   2.890 +         sha1_init(&sha1);
   2.891 +         sha1_update(&sha1, (BYTE*) &session->DAA_session.DAA_digest, 
   2.892 +           sizeof(session->DAA_session.DAA_digest));
   2.893 +-        sha1_update(&sha1, "\x01", 1);
   2.894 ++        sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.895 +         rsa_export_modulus(&aikData->key, scratch, &size);
   2.896 +         sha1_update(&sha1, scratch, size);
   2.897 +         sha1_final(&sha1, (BYTE*) &session->DAA_session.DAA_digest);
   2.898 +@@ -3134,14 +3134,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   2.899 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.900 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.901 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.902 +-      sha1_update(&sha1, "\x00", 1);
   2.903 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   2.904 +       sha1_final(&sha1, scratch);
   2.905 +       sha1_init(&sha1);
   2.906 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   2.907 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.908 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.909 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.910 +-      sha1_update(&sha1, "\x01", 1);
   2.911 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.912 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   2.913 +       mpz_init(f), mpz_init(q);
   2.914 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   2.915 +@@ -3213,14 +3213,14 @@ TPM_RESULT TPM_DAA_Sign(TPM_HANDLE handl
   2.916 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.917 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.918 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.919 +-      sha1_update(&sha1, "\x00", 1);
   2.920 ++      sha1_update(&sha1, (BYTE *) "\x00", 1);
   2.921 +       sha1_final(&sha1, scratch);
   2.922 +       sha1_init(&sha1);
   2.923 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_rekey, 
   2.924 +           sizeof(session->DAA_tpmSpecific.DAA_rekey));
   2.925 +       sha1_update(&sha1, (BYTE*) &session->DAA_tpmSpecific.DAA_count, 
   2.926 +           sizeof(session->DAA_tpmSpecific.DAA_count));
   2.927 +-      sha1_update(&sha1, "\x01", 1);
   2.928 ++      sha1_update(&sha1, (BYTE *) "\x01", 1);
   2.929 +       sha1_final(&sha1, scratch + SHA1_DIGEST_LENGTH);
   2.930 +       mpz_init(f), mpz_init(q);
   2.931 +       mpz_import(f, 2 * SHA1_DIGEST_LENGTH, 1, 1, 0, 0, scratch);
   2.932 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_data.c tpm_emulator/tpm/tpm_data.c
   2.933 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_data.c	2006-06-05 09:54:21.000000000 -0700
   2.934 ++++ tpm_emulator/tpm/tpm_data.c	2006-06-02 13:36:03.000000000 -0700
   2.935 +@@ -150,44 +150,43 @@ void tpm_release_data(void)
   2.936 + 
   2.937 + #ifdef TPM_STORE_TO_FILE
   2.938 + 
   2.939 +-#include <linux/fs.h>
   2.940 +-#include <linux/unistd.h>
   2.941 +-#include <asm/uaccess.h>
   2.942 ++#include <sys/types.h>
   2.943 ++#include <sys/stat.h>
   2.944 ++#include <fcntl.h>
   2.945 ++#include <unistd.h>
   2.946 + 
   2.947 + #define TPM_STORAGE_FILE "/var/tpm/tpm_emulator-1.2." STR(VERSION_MAJOR) "." STR(VERSION_MINOR) 
   2.948 + 
   2.949 + static int write_to_file(uint8_t *data, size_t data_length)
   2.950 + {
   2.951 +   int res;
   2.952 +-  struct file *fp;
   2.953 +-  mm_segment_t old_fs = get_fs();
   2.954 +-  fp = filp_open(TPM_STORAGE_FILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
   2.955 +-  if (IS_ERR(fp)) return -1;
   2.956 +-  set_fs(get_ds());
   2.957 +-  res = fp->f_op->write(fp, data, data_length, &fp->f_pos);
   2.958 +-  set_fs(old_fs);
   2.959 +-  filp_close(fp, NULL);
   2.960 ++  int fp;
   2.961 ++  fp = open(TPM_STORAGE_FILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
   2.962 ++  res = write(fp, data, data_length);
   2.963 ++  close(fp);
   2.964 +   return (res == data_length) ? 0 : -1;
   2.965 + }
   2.966 + 
   2.967 + static int read_from_file(uint8_t **data, size_t *data_length)
   2.968 + {
   2.969 +   int res;
   2.970 +-  struct file *fp;
   2.971 +-  mm_segment_t old_fs = get_fs();
   2.972 +-  fp = filp_open(TPM_STORAGE_FILE, O_RDONLY, 0);
   2.973 +-  if (IS_ERR(fp)) return -1;
   2.974 +-  *data_length = (size_t)fp->f_dentry->d_inode->i_size;
   2.975 +-  /* *data_length = i_size_read(fp->f_dentry->d_inode); */
   2.976 ++  int fp, file_status;
   2.977 ++  struct stat file_info;
   2.978 ++  fp = open(TPM_STORAGE_FILE, O_RDONLY, 0);
   2.979 ++  file_status = fstat(fp, &file_info);
   2.980 ++  if (file_status < 0) {
   2.981 ++    close(fp);
   2.982 ++    return -1;
   2.983 ++  } 
   2.984 ++
   2.985 ++  *data_length = file_info.st_size; 
   2.986 +   *data = tpm_malloc(*data_length);
   2.987 +   if (*data == NULL) {
   2.988 +-    filp_close(fp, NULL);
   2.989 ++    close(fp);
   2.990 +     return -1;
   2.991 +   }
   2.992 +-  set_fs(get_ds());
   2.993 +-  res = fp->f_op->read(fp, *data, *data_length, &fp->f_pos);
   2.994 +-  set_fs(old_fs);
   2.995 +-  filp_close(fp, NULL);
   2.996 ++  res = read(fp, *data, *data_length);
   2.997 ++  close(fp);
   2.998 +   if (res != *data_length) {
   2.999 +     tpm_free(*data);
  2.1000 +     return -1;
  2.1001 +@@ -267,7 +266,7 @@ int tpm_restore_permanent_data(void)
  2.1002 + 
  2.1003 + int tpm_erase_permanent_data(void)
  2.1004 + {
  2.1005 +-  int res = write_to_file("", 0);
  2.1006 ++  int res = write_to_file((uint8_t *) "", 0);
  2.1007 +   return res;
  2.1008 + }
  2.1009 + 
  2.1010 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_deprecated.c tpm_emulator/tpm/tpm_deprecated.c
  2.1011 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_deprecated.c	2006-01-10 04:21:45.000000000 -0800
  2.1012 ++++ tpm_emulator/tpm/tpm_deprecated.c	2006-06-02 13:36:03.000000000 -0700
  2.1013  @@ -1,6 +1,7 @@
  2.1014   /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  2.1015    * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  2.1016 @@ -130,15 +958,495 @@ diff -uprN orig/tpm_emulator-0.2-x86_64/
  2.1017    *
  2.1018    * This module is free software; you can redistribute it and/or modify
  2.1019    * it under the terms of the GNU General Public License as published
  2.1020 -@@ -85,6 +86,11 @@ void tpm_init_data(void)
  2.1021 -   tpmData.permanent.data.version.revMinor = VERSION_MINOR;
  2.1022 -   /* setup PCR attributes */
  2.1023 -   for (i = 0; i < TPM_NUM_PCR; i++) {
  2.1024 -+    int j;
  2.1025 -+    for (j=0; j < TPM_NUM_LOCALITY; j++) {
  2.1026 -+      tpmData.permanent.data.pcrAttrib[i].pcrExtendLocal[j] = TRUE;
  2.1027 +@@ -50,7 +51,7 @@ TPM_RESULT TPM_SaveKeyContext(TPM_KEY_HA
  2.1028 +   BYTE *ptr;
  2.1029 +   UINT32 len;
  2.1030 +   info("TPM_SaveKeyContext()");
  2.1031 +-  res = TPM_SaveContext(keyHandle, TPM_RT_KEY, "SaveKeyContext..", 
  2.1032 ++  res = TPM_SaveContext(keyHandle, TPM_RT_KEY, (BYTE*)"SaveKeyContext..", 
  2.1033 +                         keyContextSize, &contextBlob);
  2.1034 +   if (res != TPM_SUCCESS) return res;
  2.1035 +   len = *keyContextSize;
  2.1036 +@@ -82,7 +83,7 @@ TPM_RESULT TPM_SaveAuthContext(TPM_AUTHH
  2.1037 +   BYTE *ptr;
  2.1038 +   UINT32 len;
  2.1039 +   info("TPM_SaveAuthContext()");
  2.1040 +-  res = TPM_SaveContext(authHandle, TPM_RT_KEY, "SaveAuthContext.", 
  2.1041 ++  res = TPM_SaveContext(authHandle, TPM_RT_KEY, (BYTE*)"SaveAuthContext.", 
  2.1042 +                         authContextSize, &contextBlob);
  2.1043 +   if (res != TPM_SUCCESS) return res;
  2.1044 +   len = *authContextSize;
  2.1045 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_emulator.h tpm_emulator/tpm/tpm_emulator.h
  2.1046 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_emulator.h	2006-01-10 04:21:45.000000000 -0800
  2.1047 ++++ tpm_emulator/tpm/tpm_emulator.h	2006-06-02 13:36:03.000000000 -0700
  2.1048 +@@ -1,5 +1,6 @@
  2.1049 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  2.1050 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  2.1051 ++ * Copyright (C) 2005 INTEL Corp
  2.1052 +  *
  2.1053 +  * This module is free software; you can redistribute it and/or modify
  2.1054 +  * it under the terms of the GNU General Public License as published
  2.1055 +@@ -22,7 +23,8 @@
  2.1056 + /* TPM configuration */
  2.1057 + #define TPM_STORE_TO_FILE       1
  2.1058 + #undef  TPM_STRONG_PERSISTENCE
  2.1059 +-#undef  TPM_GENERATE_EK
  2.1060 ++//#undef  TPM_GENERATE_EK
  2.1061 ++#define  TPM_GENERATE_EK
  2.1062 + #undef  TPM_GENERATE_SEED_DAA
  2.1063 + 
  2.1064 + #define TPM_MANUFACTURER 0x4554485A /* 'ETHZ' */        
  2.1065 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_integrity.c tpm_emulator/tpm/tpm_integrity.c
  2.1066 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_integrity.c	2006-01-10 04:21:45.000000000 -0800
  2.1067 ++++ tpm_emulator/tpm/tpm_integrity.c	2006-06-02 13:36:03.000000000 -0700
  2.1068 +@@ -1,6 +1,7 @@
  2.1069 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  2.1070 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  2.1071 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  2.1072 ++ * Copyright (C) 2005 INTEL Corp
  2.1073 +  *
  2.1074 +  * This module is free software; you can redistribute it and/or modify
  2.1075 +  * it under the terms of the GNU General Public License as published
  2.1076 +@@ -194,4 +195,3 @@ TPM_RESULT tpm_verify_pcr(TPM_KEY_DATA *
  2.1077 +   }
  2.1078 +   return TPM_SUCCESS;
  2.1079 + }
  2.1080 +-
  2.1081 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_structures.h tpm_emulator/tpm/tpm_structures.h
  2.1082 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_structures.h	2006-01-10 04:21:45.000000000 -0800
  2.1083 ++++ tpm_emulator/tpm/tpm_structures.h	2006-06-02 13:36:03.000000000 -0700
  2.1084 +@@ -1,6 +1,7 @@
  2.1085 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  2.1086 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  2.1087 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  2.1088 ++ * Copyright (C) 2005 INTEL Corp
  2.1089 +  *
  2.1090 +  * This module is free software; you can redistribute it and/or modify
  2.1091 +  * it under the terms of the GNU General Public License as published
  2.1092 +@@ -18,7 +19,7 @@
  2.1093 + #ifndef _TPM_STRUCTURES_H_
  2.1094 + #define _TPM_STRUCTURES_H_
  2.1095 + 
  2.1096 +-#include <linux/types.h>
  2.1097 ++//#include <linux/types.h>
  2.1098 + #include "crypto/rsa.h"
  2.1099 + 
  2.1100 + /*
  2.1101 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_testing.c tpm_emulator/tpm/tpm_testing.c
  2.1102 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_testing.c	2006-01-10 04:21:45.000000000 -0800
  2.1103 ++++ tpm_emulator/tpm/tpm_testing.c	2006-06-02 13:36:03.000000000 -0700
  2.1104 +@@ -1,6 +1,7 @@
  2.1105 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  2.1106 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  2.1107 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  2.1108 ++ * Copyright (C) 2005 INTEL Corp
  2.1109 +  *
  2.1110 +  * This module is free software; you can redistribute it and/or modify
  2.1111 +  * it under the terms of the GNU General Public License as published
  2.1112 +@@ -95,24 +96,24 @@ static int tpm_test_sha1(void)
  2.1113 +   struct {
  2.1114 +     uint8_t *data; uint32_t repetitions; uint8_t *digest;
  2.1115 +   } test_cases[] =  {{
  2.1116 +-    "abc", 1,
  2.1117 +-    "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
  2.1118 ++	(uint8_t*)"abc", 1,
  2.1119 ++    (uint8_t*)"\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
  2.1120 +   }, {
  2.1121 +-    "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
  2.1122 +-    "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
  2.1123 ++    (uint8_t*)"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
  2.1124 ++    (uint8_t*)"\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
  2.1125 +   }, {
  2.1126 +-    "a", 1000000,
  2.1127 +-    "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
  2.1128 ++    (uint8_t*)"a", 1000000,
  2.1129 ++    (uint8_t*)"\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
  2.1130 +   }, {
  2.1131 +-    "0123456701234567012345670123456701234567012345670123456701234567", 10,
  2.1132 +-    "\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52"
  2.1133 ++    (uint8_t*)"0123456701234567012345670123456701234567012345670123456701234567", 10,
  2.1134 ++    (uint8_t*)"\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52"
  2.1135 +   }};
  2.1136 + 
  2.1137 +   debug("tpm_test_sha1()");
  2.1138 +   for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
  2.1139 +     sha1_init(&ctx);
  2.1140 +     for (j = 0; j < test_cases[i].repetitions; j++)
  2.1141 +-      sha1_update(&ctx, test_cases[i].data, strlen(test_cases[i].data));
  2.1142 ++      sha1_update(&ctx, test_cases[i].data, strlen((char*)test_cases[i].data));
  2.1143 +     sha1_final(&ctx, digest);
  2.1144 +     if (memcmp(digest, test_cases[i].digest, SHA1_DIGEST_LENGTH) != 0) return -1;
  2.1145 +   }
  2.1146 +@@ -128,41 +129,41 @@ static int tpm_test_hmac(void)
  2.1147 +   struct {
  2.1148 +     uint8_t *key, key_len, *data, data_len, *digest;
  2.1149 +   } test_cases[] = {{
  2.1150 +-    "\x0b", 20, "Hi There", 8,
  2.1151 +-    "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
  2.1152 ++    (uint8_t*)"\x0b", 20, (uint8_t*)"Hi There", 8,
  2.1153 ++    (uint8_t*)"\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
  2.1154 +   }, {
  2.1155 +-    "Jefe", 4, "what do ya want for nothing?", 28,
  2.1156 +-    "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
  2.1157 ++    (uint8_t*)"Jefe", 4, (uint8_t*)"what do ya want for nothing?", 28,
  2.1158 ++    (uint8_t*)"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
  2.1159 +   }, {
  2.1160 +-    "\xaa", 20, "\xdd", 50,
  2.1161 +-    "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
  2.1162 ++    (uint8_t*)"\xaa", 20, (uint8_t*)"\xdd", 50,
  2.1163 ++    (uint8_t*)"\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
  2.1164 +   }, {
  2.1165 +-    "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
  2.1166 +-    "\x15\x16\x17\x18\x19", 25, "\xcd", 50,
  2.1167 +-    "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
  2.1168 ++    (uint8_t*)"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
  2.1169 ++    "\x15\x16\x17\x18\x19", 25, (uint8_t*)"\xcd", 50,
  2.1170 ++    (uint8_t*)"\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
  2.1171 +   }, {
  2.1172 +-    "\x0c", 20, "Test With Truncation", 20,
  2.1173 +-    "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
  2.1174 ++    (uint8_t*)"\x0c", 20, (uint8_t*)"Test With Truncation", 20,
  2.1175 ++    (uint8_t*)"\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
  2.1176 +   }, {
  2.1177 +-    "\xaa", 80, "Test Using Larger Than Block-Size Key - Hash Key First", 54,
  2.1178 +-    "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
  2.1179 ++    (uint8_t*)"\xaa", 80, (uint8_t*)"Test Using Larger Than Block-Size Key - Hash Key First", 54,
  2.1180 ++    (uint8_t*)"\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
  2.1181 +   }, {
  2.1182 +-    "\xaa", 80,
  2.1183 +-    "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
  2.1184 +-    "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
  2.1185 ++    (uint8_t*)"\xaa", 80,
  2.1186 ++    (uint8_t*)"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
  2.1187 ++    (uint8_t*)"\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
  2.1188 +   }};
  2.1189 + 
  2.1190 +   debug("tpm_test_hmac()");
  2.1191 +   for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
  2.1192 +-    if (strlen(test_cases[i].key) < test_cases[i].key_len) {
  2.1193 ++    if (strlen((char*)test_cases[i].key) < test_cases[i].key_len) {
  2.1194 +       uint8_t key[test_cases[i].key_len];
  2.1195 +       memset(key, test_cases[i].key[0], test_cases[i].key_len);
  2.1196 +       hmac_init(&ctx, key, test_cases[i].key_len);
  2.1197 +     } else {
  2.1198 +       hmac_init(&ctx, test_cases[i].key, test_cases[i].key_len);
  2.1199 +     }
  2.1200 +-    for (j = 0; j < test_cases[i].data_len; j += strlen(test_cases[i].data)) {
  2.1201 +-      hmac_update(&ctx, test_cases[i].data, strlen(test_cases[i].data));
  2.1202 ++    for (j = 0; j < test_cases[i].data_len; j += strlen((char*)test_cases[i].data)) {
  2.1203 ++      hmac_update(&ctx, test_cases[i].data, strlen((char*)test_cases[i].data));
  2.1204 +     }
  2.1205 +     hmac_final(&ctx, digest);
  2.1206 +     if (memcmp(digest, test_cases[i].digest, SHA1_DIGEST_LENGTH) != 0) return -1;
  2.1207 +@@ -173,9 +174,9 @@ static int tpm_test_hmac(void)
  2.1208 + static int tpm_test_rsa_EK(void)
  2.1209 + {
  2.1210 +   int res = 0;
  2.1211 +-  char *data = "RSA PKCS #1 v1.5 Test-String";
  2.1212 ++  uint8_t *data = (uint8_t*)"RSA PKCS #1 v1.5 Test-String";
  2.1213 +   uint8_t buf[256];
  2.1214 +-  size_t buf_len, data_len = strlen(data);
  2.1215 ++  size_t buf_len, data_len = strlen((char*)data);
  2.1216 +   rsa_private_key_t priv_key;
  2.1217 +   rsa_public_key_t pub_key;
  2.1218 + 
  2.1219 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_ticks.c tpm_emulator/tpm/tpm_ticks.c
  2.1220 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_ticks.c	2006-01-10 04:21:45.000000000 -0800
  2.1221 ++++ tpm_emulator/tpm/tpm_ticks.c	2006-06-02 13:36:03.000000000 -0700
  2.1222 +@@ -1,6 +1,7 @@
  2.1223 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  2.1224 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  2.1225 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  2.1226 ++ * Copyright (C) 2005 INTEL Corp
  2.1227 +  *
  2.1228 +  * This module is free software; you can redistribute it and/or modify
  2.1229 +  * it under the terms of the GNU General Public License as published
  2.1230 +@@ -37,9 +38,7 @@ TPM_RESULT TPM_SetTickType(TPM_TICKTYPE 
  2.1231 + TPM_RESULT TPM_GetTicks(TPM_CURRENT_TICKS *currentTime)
  2.1232 + {
  2.1233 +   info("TPM_GetTicks()");
  2.1234 +-  memcpy(currentTime, &tpmData.stany.data.currentTicks, 
  2.1235 +-    sizeof(TPM_CURRENT_TICKS));
  2.1236 +-  return TPM_SUCCESS;
  2.1237 ++  return TPM_DISABLED_CMD;
  2.1238 + }
  2.1239 + 
  2.1240 + TPM_RESULT TPM_TickStampBlob(TPM_KEY_HANDLE keyHandle, TPM_NONCE *antiReplay,
  2.1241 +@@ -47,61 +46,12 @@ TPM_RESULT TPM_TickStampBlob(TPM_KEY_HAN
  2.1242 +                              TPM_CURRENT_TICKS *currentTicks, 
  2.1243 +                              UINT32 *sigSize, BYTE **sig)
  2.1244 + {
  2.1245 +-  TPM_RESULT res;
  2.1246 +-  TPM_KEY_DATA *key;
  2.1247 +-  BYTE *info, *p;
  2.1248 +-  UINT32 info_length, length;
  2.1249 +   info("TPM_TickStampBlob()");
  2.1250 +-  /* get key */
  2.1251 +-  key = tpm_get_key(keyHandle);
  2.1252 +-  if (key == NULL) return TPM_INVALID_KEYHANDLE;
  2.1253 +-  /* verify authorization */ 
  2.1254 +-  res = tpm_verify_auth(auth1, key->usageAuth, keyHandle);
  2.1255 +-  if (res != TPM_SUCCESS) return res;
  2.1256 +-  if (key->keyUsage != TPM_KEY_SIGNING && key->keyUsage != TPM_KEY_LEGACY
  2.1257 +-      && key->keyUsage != TPM_KEY_IDENTITY) return TPM_INVALID_KEYUSAGE;
  2.1258 +-  /* get current ticks */
  2.1259 +-  TPM_GetTicks(currentTicks);
  2.1260 +-  /* sign data using signature scheme PKCS1_SHA1 and TPM_SIGN_INFO container */
  2.1261 +-  *sigSize = key->key.size >> 3;
  2.1262 +-  *sig = tpm_malloc(*sigSize);
  2.1263 +-  if (*sig == NULL) return TPM_FAIL; 
  2.1264 +-  /* setup TPM_SIGN_INFO structure */
  2.1265 +-  info_length = 30 + sizeof(TPM_DIGEST) + sizeof_TPM_CURRENT_TICKS(currentTicks);
  2.1266 +-  info = tpm_malloc(info_length);
  2.1267 +-  if (info == NULL) {
  2.1268 +-    tpm_free(*sig);
  2.1269 +-    return TPM_FAIL;
  2.1270 +-  }
  2.1271 +-  memcpy(&info[0], "\x05\x00TSTP", 6);
  2.1272 +-  memcpy(&info[6], antiReplay->nonce, 20);
  2.1273 +-  *(UINT32*)&info[26] = cpu_to_be32(20
  2.1274 +-                        + sizeof_TPM_CURRENT_TICKS(currentTicks));
  2.1275 +-  memcpy(&info[30], digestToStamp->digest, sizeof(TPM_DIGEST));
  2.1276 +-  p = &info[30 + sizeof(TPM_DIGEST)]; 
  2.1277 +-  length = sizeof_TPM_CURRENT_TICKS(currentTicks);
  2.1278 +-  if (tpm_marshal_TPM_CURRENT_TICKS(&p, &length, currentTicks)
  2.1279 +-      || rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1, info, info_length, *sig)) {   
  2.1280 +-    tpm_free(*sig);
  2.1281 +-    tpm_free(info);
  2.1282 +-    return TPM_FAIL;
  2.1283 +-  } 
  2.1284 +-  return TPM_SUCCESS;
  2.1285 ++  return TPM_DISABLED_CMD;
  2.1286 + }
  2.1287 + 
  2.1288 + void tpm_update_ticks(void)
  2.1289 + {
  2.1290 +-  if (tpmData.stany.data.currentTicks.tag == 0) {
  2.1291 +-    tpmData.stany.data.currentTicks.tag = TPM_TAG_CURRENT_TICKS;
  2.1292 +-    tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();
  2.1293 +-    tpmData.stany.data.currentTicks.tickType = tpmData.permanent.data.tickType;
  2.1294 +-    tpm_get_random_bytes(tpmData.stany.data.currentTicks.tickNonce.nonce, 
  2.1295 +-      sizeof(TPM_NONCE));
  2.1296 +-    tpmData.stany.data.currentTicks.tickRate = 1;
  2.1297 +-    tpmData.stany.data.currentTicks.tickSecurity = TICK_SEC_NO_CHECK;
  2.1298 +-  } else {
  2.1299 +-    tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();   
  2.1300 +-  }
  2.1301 + }
  2.1302 +   
  2.1303 + 
  2.1304 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm/tpm_transport.c tpm_emulator/tpm/tpm_transport.c
  2.1305 +--- orig/tpm_emulator-0.3-x86_64/tpm/tpm_transport.c	2006-01-10 04:21:45.000000000 -0800
  2.1306 ++++ tpm_emulator/tpm/tpm_transport.c	2006-06-02 13:36:03.000000000 -0700
  2.1307 +@@ -59,7 +59,7 @@ static int decrypt_transport_auth(TPM_KE
  2.1308 + static void transport_log_in(TPM_COMMAND_CODE ordinal, BYTE parameters[20],
  2.1309 +                              BYTE pubKeyHash[20], TPM_DIGEST *transDigest)
  2.1310 + {
  2.1311 +-  UINT32 tag = cpu_to_be32(TPM_TAG_TRANSPORT_LOG_IN);
  2.1312 ++  UINT32 tag = CPU_TO_BE32(TPM_TAG_TRANSPORT_LOG_IN);
  2.1313 +   BYTE *ptr, buf[sizeof_TPM_TRANSPORT_LOG_IN(x)];
  2.1314 +   UINT32 len = sizeof(buf);
  2.1315 +   sha1_ctx_t sha1;
  2.1316 +@@ -76,7 +76,7 @@ static void transport_log_in(TPM_COMMAND
  2.1317 + static void transport_log_out(TPM_CURRENT_TICKS *currentTicks, BYTE parameters[20],
  2.1318 +                               TPM_MODIFIER_INDICATOR locality, TPM_DIGEST *transDigest)
  2.1319 + {
  2.1320 +-  UINT32 tag = cpu_to_be32(TPM_TAG_TRANSPORT_LOG_OUT);
  2.1321 ++  UINT32 tag = CPU_TO_BE32(TPM_TAG_TRANSPORT_LOG_OUT);
  2.1322 +   BYTE *ptr, buf[sizeof_TPM_TRANSPORT_LOG_OUT(x)];
  2.1323 +   UINT32 len = sizeof(buf);
  2.1324 +   sha1_ctx_t sha1;
  2.1325 +@@ -191,7 +191,7 @@ static void decrypt_wrapped_command(BYTE
  2.1326 +     sha1_update(&sha1, auth->nonceOdd.nonce, sizeof(auth->nonceOdd.nonce));
  2.1327 +     sha1_update(&sha1, "in", 2);
  2.1328 +     sha1_update(&sha1, secret, sizeof(TPM_SECRET));
  2.1329 +-    j = cpu_to_be32(i);
  2.1330 ++    j = CPU_TO_BE32(i);
  2.1331 +     sha1_update(&sha1, (BYTE*)&j, 4);
  2.1332 +     sha1_final(&sha1, mask);
  2.1333 +     for (j = 0; j < sizeof(mask) && buf_len > 0; j++) { 
  2.1334 +@@ -213,7 +213,7 @@ static void encrypt_wrapped_command(BYTE
  2.1335 +     sha1_update(&sha1, auth->nonceOdd.nonce, sizeof(auth->nonceOdd.nonce));
  2.1336 +     sha1_update(&sha1, "out", 3);
  2.1337 +     sha1_update(&sha1, secret, sizeof(TPM_SECRET));
  2.1338 +-    j = cpu_to_be32(i);
  2.1339 ++    j = CPU_TO_BE32(i);
  2.1340 +     sha1_update(&sha1, (BYTE*)&j, 4);
  2.1341 +     sha1_final(&sha1, mask);
  2.1342 +     for (j = 0; j < sizeof(mask) && buf_len > 0; j++) { 
  2.1343 +@@ -253,9 +253,9 @@ TPM_RESULT TPM_ExecuteTransport(UINT32 i
  2.1344 +   /* verify authorization */
  2.1345 +   tpm_compute_in_param_digest(&req);
  2.1346 +   sha1_init(&sha1);
  2.1347 +-  res = cpu_to_be32(TPM_ORD_ExecuteTransport);
  2.1348 ++  res = CPU_TO_BE32(TPM_ORD_ExecuteTransport);
  2.1349 +   sha1_update(&sha1, (BYTE*)&res, 4);
  2.1350 +-  res = cpu_to_be32(inWrappedCmdSize);
  2.1351 ++  res = CPU_TO_BE32(inWrappedCmdSize);
  2.1352 +   sha1_update(&sha1, (BYTE*)&res, 4);
  2.1353 +   sha1_update(&sha1, req.auth1.digest, sizeof(req.auth1.digest));
  2.1354 +   sha1_final(&sha1, auth1->digest);
  2.1355 +@@ -357,7 +357,7 @@ TPM_RESULT TPM_ReleaseTransportSigned(TP
  2.1356 +   /* setup a TPM_SIGN_INFO structure */
  2.1357 +   memcpy(&buf[0], "\x05\x00TRAN", 6);
  2.1358 +   memcpy(&buf[6], antiReplay->nonce, 20);
  2.1359 +-  *(UINT32*)&buf[26] = cpu_to_be32(20);
  2.1360 ++  *(UINT32*)&buf[26] = CPU_TO_BE32(20);
  2.1361 +   memcpy(&buf[30], session->transInternal.transDigest.digest, 20);
  2.1362 +   /* sign info structure */ 
  2.1363 +   res = tpm_sign(key, auth1, TRUE, buf, sizeof(buf), signature, signSize);
  2.1364 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpmd.c tpm_emulator/tpmd.c
  2.1365 +--- orig/tpm_emulator-0.3-x86_64/tpmd.c	1969-12-31 16:00:00.000000000 -0800
  2.1366 ++++ tpm_emulator/tpmd.c	2006-06-02 13:36:03.000000000 -0700
  2.1367 +@@ -0,0 +1,141 @@
  2.1368 ++/* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  2.1369 ++ * Copyright (C) 2005 INTEL Corp
  2.1370 ++ *
  2.1371 ++ * This module is free software; you can redistribute it and/or modify
  2.1372 ++ * it under the terms of the GNU General Public License as published
  2.1373 ++ * by the Free Software Foundation; either version 2 of the License,
  2.1374 ++ * or (at your option) any later version.
  2.1375 ++ *
  2.1376 ++ * This module is distributed in the hope that it will be useful,
  2.1377 ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2.1378 ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  2.1379 ++ * GNU General Public License for more details.
  2.1380 ++ *
  2.1381 ++ */
  2.1382 ++
  2.1383 ++#include <stdio.h>
  2.1384 ++#include <stdlib.h>
  2.1385 ++#include <unistd.h>
  2.1386 ++#include <string.h>
  2.1387 ++#include <sys/types.h>
  2.1388 ++#include <sys/stat.h>
  2.1389 ++#include <fcntl.h>
  2.1390 ++#include <sys/time.h>
  2.1391 ++
  2.1392 ++#include "tpm_emulator.h"
  2.1393 ++
  2.1394 ++#define TPM_RX_FNAME "/tmp/tpm_in.fifo"
  2.1395 ++#define TPM_TX_FNAME "/tmp/tpm_out.fifo"
  2.1396 ++
  2.1397 ++#define BUFFER_SIZE 2048
  2.1398 ++
  2.1399 ++static int devurandom=0;
  2.1400 ++	  
  2.1401 ++void get_random_bytes(void *buf, int nbytes) {
  2.1402 ++  
  2.1403 ++  if (devurandom == 0) {
  2.1404 ++    devurandom = open("/dev/urandom", O_RDONLY);
  2.1405 ++  }
  2.1406 ++
  2.1407 ++  if (read(devurandom, buf, nbytes) != nbytes) {
  2.1408 ++      printf("Can't get random number.\n");
  2.1409 ++      exit(-1);
  2.1410 ++  }
  2.1411 ++}
  2.1412 ++
  2.1413 ++uint64_t tpm_get_ticks(void)
  2.1414 ++{
  2.1415 ++  //struct timeval tv;
  2.1416 ++  //int gettimeofday(&tv, struct timezone *tz);
  2.1417 ++  return 0;
  2.1418 ++}
  2.1419 ++
  2.1420 ++int main(int argc, char **argv)
  2.1421 ++{
  2.1422 ++  uint8_t in[BUFFER_SIZE], *out;
  2.1423 ++  uint32_t out_size;
  2.1424 ++  int in_size, written;
  2.1425 ++  int i;
  2.1426 ++ 
  2.1427 ++  int tpm_tx_fh=-1, tpm_rx_fh=-1;
  2.1428 ++  if (argc < 2) {
  2.1429 ++    printf("Usage: tpmd clear|save|deactivated\n" );
  2.1430 ++	  return -1;
  2.1431 ++  }
  2.1432 ++
  2.1433 ++  /* initialize TPM emulator */
  2.1434 ++  if (!strcmp(argv[1], "clear")) {
  2.1435 ++    printf("Initializing tpm: %s\n", argv[1]);
  2.1436 ++    tpm_emulator_init(1);
  2.1437 ++  } else if (!strcmp(argv[1], "save")) { 
  2.1438 ++    printf("Initializing tpm: %s\n", argv[1]);
  2.1439 ++    tpm_emulator_init(2);
  2.1440 ++  } else if (!strcmp(argv[1], "deactivated")) {
  2.1441 ++    printf("Initializing tpm: %s\n", argv[1]);
  2.1442 ++    tpm_emulator_init(3);
  2.1443 ++  } else {
  2.1444 ++    printf("invalid startup mode '%s'; must be 'clear', "
  2.1445 ++      "'save' (default) or 'deactivated", argv[1]);
  2.1446 ++    return -1;
  2.1447 ++  }
  2.1448 ++
  2.1449 ++  while (1) {
  2.1450 ++abort_command:
  2.1451 ++    if (tpm_rx_fh < 0) {
  2.1452 ++      tpm_rx_fh = open(TPM_RX_FNAME, O_RDONLY);
  2.1453 ++    }
  2.1454 ++    
  2.1455 ++    if (tpm_rx_fh < 0) {
  2.1456 ++      printf("ERROR: failed to open devices to listen to guest.\n");
  2.1457 ++      return -1;
  2.1458 ++    }
  2.1459 ++    
  2.1460 ++    if (tpm_tx_fh < 0) {
  2.1461 ++      tpm_tx_fh = open(TPM_TX_FNAME, O_WRONLY);
  2.1462  +    }
  2.1463  +
  2.1464 -     tpmData.permanent.data.pcrAttrib[i].pcrReset = TRUE;
  2.1465 -   }
  2.1466 -   /* set tick type */
  2.1467 ++    if (tpm_tx_fh < 0) {
  2.1468 ++      printf("ERROR: failed to open devices to respond to guest.\n");
  2.1469 ++      return -1;
  2.1470 ++    }
  2.1471 ++
  2.1472 ++    in_size = read(tpm_rx_fh, in, BUFFER_SIZE);
  2.1473 ++    if (in_size < 6) { // Magic size of minium TPM command
  2.1474 ++      printf("Recv[%d] to small: 0x", in_size);
  2.1475 ++      if (in_size <= 0) {
  2.1476 ++          close(tpm_rx_fh);
  2.1477 ++          tpm_rx_fh = -1;
  2.1478 ++          goto abort_command;
  2.1479 ++      }
  2.1480 ++    } else { 
  2.1481 ++      printf("Recv[%d]: 0x", in_size);
  2.1482 ++      for (i=0; i< in_size; i++) 
  2.1483 ++        printf("%x ", in[i]);
  2.1484 ++      printf("\n");
  2.1485 ++    }
  2.1486 ++
  2.1487 ++    
  2.1488 ++    if (tpm_handle_command(in, in_size, &out, &out_size) != 0) { 
  2.1489 ++        printf("ERROR: Handler Failed.\n");
  2.1490 ++    }
  2.1491 ++
  2.1492 ++    written = write(tpm_tx_fh, out, out_size);
  2.1493 ++
  2.1494 ++    if (written != out_size ) {
  2.1495 ++      printf("ERROR: Part of response not written %d/%d.\nAttempt: ", written, out_size);
  2.1496 ++    } else {
  2.1497 ++      printf("Sent[%Zu]: ", out_size);
  2.1498 ++    }
  2.1499 ++    for (i=0; i< out_size; i++)
  2.1500 ++      printf("%x ", out[i]);
  2.1501 ++    printf("\n");
  2.1502 ++    tpm_free(out);
  2.1503 ++
  2.1504 ++  } // loop
  2.1505 ++
  2.1506 ++  tpm_emulator_shutdown();
  2.1507 ++
  2.1508 ++  close(tpm_tx_fh);
  2.1509 ++  close(tpm_rx_fh);
  2.1510 ++
  2.1511 ++}
  2.1512 +diff -uprN orig/tpm_emulator-0.3-x86_64/tpm_version.h tpm_emulator/tpm_version.h
  2.1513 +--- orig/tpm_emulator-0.3-x86_64/tpm_version.h	2006-01-10 04:21:45.000000000 -0800
  2.1514 ++++ tpm_emulator/tpm_version.h	1969-12-31 16:00:00.000000000 -0800
  2.1515 +@@ -1,6 +0,0 @@
  2.1516 +-#ifndef _TPM_VERSION_H_
  2.1517 +-#define _TPM_VERSION_H_
  2.1518 +-#define VERSION_MAJOR 0
  2.1519 +-#define VERSION_MINOR 3
  2.1520 +-#define VERSION_BUILD 1136893683
  2.1521 +-#endif /* _TPM_VERSION_H_ */
     3.1 --- a/tools/vtpm_manager/Rules.mk	Tue Jun 27 11:05:39 2006 +0100
     3.2 +++ b/tools/vtpm_manager/Rules.mk	Tue Jun 27 11:13:24 2006 +0100
     3.3 @@ -56,6 +56,9 @@ CFLAGS += -DLOGGING_MODULES="(BITMASK(VT
     3.4  # vtpm_manager listens on fifo's rather than backend
     3.5  #CFLAGS += -DDUMMY_BACKEND
     3.6  
     3.7 +# TCS talks to fifo's rather than /dev/tpm. TPM Emulator assumed on fifos
     3.8 +#CFLAGS += -DDUMMY_TPM
     3.9 +
    3.10  # Do not have manager launch DMs.
    3.11  #CFLAGS += -DMANUAL_DM_LAUNCH
    3.12  
     4.1 --- a/tools/vtpm_manager/manager/vtpm_manager.c	Tue Jun 27 11:05:39 2006 +0100
     4.2 +++ b/tools/vtpm_manager/manager/vtpm_manager.c	Tue Jun 27 11:13:24 2006 +0100
     4.3 @@ -92,8 +92,9 @@ TPM_RESULT VTPM_Create_Manager(){
     4.4    status = VTSP_ReadPubek(vtpm_globals->manager_tcs_handle, &ek_cryptoInfo);
     4.5    
     4.6    // If we can read PubEK then there is no owner and we should take it.
     4.7 +  // We use the abilty to read the pubEK to flag that the TPM is owned.
     4.8 +  // FIXME: Change to just trying to take ownership and react to the status
     4.9    if (status == TPM_SUCCESS) { 
    4.10 -    vtpmloginfo(VTPM_LOG_VTPM, "Failed to readEK meaning TPM has an owner. Creating Keys off existing SRK.\n");
    4.11      TPMTRYRETURN(VTSP_TakeOwnership(vtpm_globals->manager_tcs_handle,
    4.12  				    (const TPM_AUTHDATA*)&vtpm_globals->owner_usage_auth, 
    4.13  				    &SRK_AUTH,
    4.14 @@ -103,6 +104,8 @@ TPM_RESULT VTPM_Create_Manager(){
    4.15      TPMTRYRETURN(VTSP_DisablePubekRead(vtpm_globals->manager_tcs_handle,
    4.16                                         (const TPM_AUTHDATA*)&vtpm_globals->owner_usage_auth,  
    4.17                                         &vtpm_globals->keyAuth));     
    4.18 +  } else {
    4.19 +    vtpmloginfo(VTPM_LOG_VTPM, "Failed to readEK meaning TPM has an owner. Creating Keys off existing SRK.\n");
    4.20    }
    4.21    
    4.22    // Generate storage key's auth
    4.23 @@ -165,7 +168,6 @@ TPM_RESULT VTPM_Create_Manager(){
    4.24                                &vtpm_globals->bootKey,
    4.25                                TRUE ) );
    4.26  
    4.27 -  printf("***************************** FIXME: SAVE NEW STATE *******\n");
    4.28    goto egress;
    4.29    
    4.30   abort_egress:
    4.31 @@ -181,7 +183,7 @@ TPM_RESULT VTPM_Create_Manager(){
    4.32  TPM_RESULT VTPM_Init_Manager() {
    4.33    TPM_RESULT status = TPM_FAIL, serviceStatus;   
    4.34    BYTE *randomsead;
    4.35 -  UINT32 randomsize;
    4.36 +  UINT32 randomsize=256;
    4.37  
    4.38    if ((vtpm_globals = (VTPM_GLOBALS *) malloc(sizeof(VTPM_GLOBALS))) == NULL){
    4.39      status = TPM_FAIL;
     5.1 --- a/tools/vtpm_manager/tcs/transmit.c	Tue Jun 27 11:05:39 2006 +0100
     5.2 +++ b/tools/vtpm_manager/tcs/transmit.c	Tue Jun 27 11:13:24 2006 +0100
     5.3 @@ -43,7 +43,17 @@
     5.4  
     5.5  // flag to track whether TDDL has been opened
     5.6  static int g_TDDL_open = 0;
     5.7 -static int g_fd = -1;              // the fd to the TPM
     5.8 +static int g_tx_fd = -1;              // the fd to the TPM
     5.9 +
    5.10 +#ifndef DUMMY_TPM
    5.11 + #define TPM_TX_FNAME "/dev/tpm0"
    5.12 + static int *g_rx_fdp = &g_tx_fd;
    5.13 +#else
    5.14 + #define TPM_TX_FNAME "/tmp/tpm_in.fifo"
    5.15 + #define TPM_RX_FNAME "/tmp/tpm_out.fifo"
    5.16 + static int g_rx_fd = -1;
    5.17 + static int *g_rx_fdp = &g_rx_fd;              // the fd to the TPM
    5.18 +#endif
    5.19  
    5.20  TPM_RESULT
    5.21  TDDL_TransmitData( TDDL_BYTE* in,
    5.22 @@ -60,10 +70,9 @@ TDDL_TransmitData( TDDL_BYTE* in,
    5.23    vtpmloginfomore(VTPM_LOG_TXDATA, "\n");
    5.24    
    5.25    ssize_t size = 0;
    5.26 -  int fd = g_fd;
    5.27    
    5.28    // send the request
    5.29 -  size = write (fd, in, insize);
    5.30 +  size = write (g_tx_fd, in, insize);
    5.31    if (size < 0) {
    5.32      vtpmlogerror(VTPM_LOG_TXDATA, "write() failed");
    5.33      ERRORDIE (TPM_IOERROR);
    5.34 @@ -74,7 +83,7 @@ TDDL_TransmitData( TDDL_BYTE* in,
    5.35    }
    5.36  
    5.37    // read the response
    5.38 -  size = read (fd, out, TCPA_MAX_BUFFER_LENGTH);
    5.39 +  size = read (*g_rx_fdp, out, TCPA_MAX_BUFFER_LENGTH);
    5.40    if (size < 0) {
    5.41      vtpmlogerror(VTPM_LOG_TXDATA, "read() failed");
    5.42      ERRORDIE (TPM_IOERROR);
    5.43 @@ -98,18 +107,20 @@ TDDL_TransmitData( TDDL_BYTE* in,
    5.44  TPM_RESULT TDDL_Open() {
    5.45    
    5.46    TDDL_RESULT status = TDDL_SUCCESS;
    5.47 -  int fd = -1;
    5.48    
    5.49    if (g_TDDL_open)
    5.50      return TPM_FAIL;
    5.51 -  
    5.52 -  fd = open ("/dev/tpm0", O_RDWR);
    5.53 -  if (fd < 0) {
    5.54 +
    5.55 +#ifdef DUMMY_TPM  
    5.56 +  *g_rx_fdp = open (TPM_RX_FNAME, O_RDWR);
    5.57 +#endif
    5.58 +
    5.59 +  g_tx_fd = open (TPM_TX_FNAME, O_RDWR);
    5.60 +  if (g_tx_fd < 0) {
    5.61      vtpmlogerror(VTPM_LOG_TXDATA, "TPM open failed");
    5.62      return TPM_IOERROR;
    5.63    }
    5.64    
    5.65 -  g_fd = fd;
    5.66    g_TDDL_open = 1;
    5.67    
    5.68    return status;
    5.69 @@ -119,13 +130,18 @@ void TDDL_Close() {
    5.70    if (! g_TDDL_open)
    5.71          return;
    5.72  
    5.73 -  if (g_fd>= 0) {
    5.74 -    if (close(g_fd) < 0) 
    5.75 +  if (g_tx_fd>= 0) {
    5.76 +    if (close(g_tx_fd) < 0) 
    5.77        vtpmlogerror(VTPM_LOG_TXDATA, "closeing tpm failed");
    5.78 -    
    5.79 -    g_fd = -1;
    5.80 +    g_tx_fd = -1;
    5.81    }
    5.82      
    5.83 +  if (*g_rx_fdp>= 0) {
    5.84 +    if (close(*g_rx_fdp) < 0) 
    5.85 +      vtpmlogerror(VTPM_LOG_TXDATA, "closeing tpm failed");
    5.86 +    *g_rx_fdp = -1;
    5.87 +  }
    5.88 +
    5.89    g_TDDL_open = 0;
    5.90    
    5.91  }