ia64/xen-unstable

view tools/vtpm/vtpm.patch @ 9488:0a6f5527ca4b

[IA64] set itv handoff as masked and enable reading irr[0-3]

Set initial vcpu itv handoff state to mask the timer vector.
This seems to match hardware and makes logical sense from a
spurious interrupt perspective. Enable vcpu_get_irr[0-3]
functions as they seem to work and have the proper backing.
This enables the check_sal_cache_flush() in arch/ia64/kernel.sal.c
to work unmodified, allowing us to remove the Xen changes from
the file (and thus the file from the sparse tree).

Signed-off-by: Alex Williamson <alex.williamson@hp.com>
author awilliam@xenbuild.aw
date Tue Apr 04 09:39:45 2006 -0600 (2006-04-04)
parents 06d84bf87159
children 72ef14e79cc0
line source
1 diff -uprN orig/tpm_emulator-0.2-x86_64/AUTHORS vtpm/AUTHORS
2 --- orig/tpm_emulator-0.2-x86_64/AUTHORS 2005-08-15 00:58:57.000000000 -0700
3 +++ vtpm/AUTHORS 2005-09-14 20:27:22.000000000 -0700
4 @@ -1 +1,2 @@
5 Mario Strasser <mast@gmx.net>
6 +INTEL Corp <>
7 diff -uprN orig/tpm_emulator-0.2-x86_64/ChangeLog vtpm/ChangeLog
8 --- orig/tpm_emulator-0.2-x86_64/ChangeLog 2005-08-15 00:58:57.000000000 -0700
9 +++ vtpm/ChangeLog 2005-09-14 20:27:22.000000000 -0700
10 @@ -1,3 +1,7 @@
11 +2005-08-16 Intel Corp
12 + Moved module out of kernel to run as a ring 3 app
13 + Modified save_to_file and load_from_file to call a xen backend driver to call a VTPM manager
14 +
15 2005-08-15 Mario Strasser <mast@gmx.net>
16 * all: some typos corrected
17 * tpm_integrity.c: bug in TPM_Extend fixed
18 diff -uprN orig/tpm_emulator-0.2-x86_64/crypto/gmp_kernel_wrapper.c vtpm/crypto/gmp_kernel_wrapper.c
19 --- orig/tpm_emulator-0.2-x86_64/crypto/gmp_kernel_wrapper.c 2005-09-15 19:21:42.508873032 -0700
20 +++ vtpm/crypto/gmp_kernel_wrapper.c 2005-09-15 19:25:37.319176440 -0700
21 @@ -1,5 +1,6 @@
22 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
23 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
24 + * Copyright (C) 2005 INTEL Corp
25 *
26 * This module is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published
28 @@ -24,15 +25,10 @@ int __gmp_junk;
29 void __attribute__ ((regparm(0))) __gmp_assert_fail(const char *filename,
30 int linenum, const char *expr)
31 {
32 - panic(KERN_CRIT TPM_MODULE_NAME "%s:%d: GNU MP assertion failed: %s\n",
33 + error("%s:%d: GNU MP assertion failed: %s\n",
34 filename, linenum, expr);
35 }
37 -void __attribute__ ((regparm(0))) abort(void)
38 -{
39 - panic(KERN_CRIT TPM_MODULE_NAME "GNU MP abort() was called\n");
40 -}
41 -
42 /* overwrite GNU MP random functions (used by mpz/millerrabin.c) */
44 void __attribute__ ((regparm(0))) gmp_randinit(gmp_randstate_t rstate,
45 @@ -77,20 +73,19 @@ void __attribute__ ((regparm(0))) mpz_ur
47 void __attribute__ ((regparm(0))) *kernel_allocate(size_t size)
48 {
49 - void *ret = (void*)kmalloc(size, GFP_KERNEL);
50 - if (!ret) panic(KERN_CRIT TPM_MODULE_NAME
51 - "GMP: cannot allocate memory (size=%Zu)\n", size);
52 + void *ret = (void*)malloc(size);
53 + if (!ret) error("GMP: cannot allocate memory (size=%Zu)\n", size);
54 return ret;
55 }
57 void __attribute__ ((regparm(0))) *kernel_reallocate(void *oldptr,
58 size_t old_size, size_t new_size)
59 {
60 - void *ret = (void*)kmalloc(new_size, GFP_KERNEL);
61 - if (!ret) panic(KERN_CRIT TPM_MODULE_NAME "GMP: Cannot reallocate memory "
62 - "(old_size=%Zu new_size=%Zu)\n", old_size, new_size);
63 + void *ret = (void*)malloc(new_size);
64 + if (!ret) error("GMP: Cannot reallocate memory "
65 + "(old_size=%Zu new_size=%Zu)\n", old_size, new_size);
66 memcpy(ret, oldptr, old_size);
67 - kfree(oldptr);
68 + free(oldptr);
69 return ret;
70 }
72 @@ -99,7 +94,7 @@ void __attribute__ ((regparm(0))) kernel
73 /* overwrite used memory */
74 if (blk_ptr != NULL) {
75 memset(blk_ptr, 0, blk_size);
76 - kfree(blk_ptr);
77 + free(blk_ptr);
78 }
79 }
81 diff -uprN orig/tpm_emulator-0.2-x86_64/crypto/rsa.c vtpm/crypto/rsa.c
82 --- orig/tpm_emulator-0.2-x86_64/crypto/rsa.c 2005-08-15 00:58:57.000000000 -0700
83 +++ vtpm/crypto/rsa.c 2005-09-14 20:27:22.000000000 -0700
84 @@ -1,5 +1,6 @@
85 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
86 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
87 + * Copyright (C) 2005 INTEL Corp
88 *
89 * This module is free software; you can redistribute it and/or modify
90 * it under the terms of the GNU General Public License as published
91 @@ -363,7 +364,7 @@ static int encode_message(int type, uint
92 msg[0] = 0x00;
93 get_random_bytes(&msg[1], SHA1_DIGEST_LENGTH);
94 sha1_init(&ctx);
95 - sha1_update(&ctx, "TCPA", 4);
96 + sha1_update(&ctx, (uint8_t *) "TCPA", 4);
97 sha1_final(&ctx, &msg[1 + SHA1_DIGEST_LENGTH]);
98 memset(&msg[1 + 2 * SHA1_DIGEST_LENGTH], 0x00,
99 msg_len - data_len - 2 * SHA1_DIGEST_LENGTH - 2);
100 @@ -411,7 +412,7 @@ static int decode_message(int type, uint
101 mask_generation(&msg[1], SHA1_DIGEST_LENGTH,
102 &msg[1 + SHA1_DIGEST_LENGTH], msg_len - SHA1_DIGEST_LENGTH - 1);
103 sha1_init(&ctx);
104 - sha1_update(&ctx, "TCPA", 4);
105 + sha1_update(&ctx, (uint8_t *) "TCPA", 4);
106 sha1_final(&ctx, &msg[1]);
107 if (memcmp(&msg[1], &msg[1 + SHA1_DIGEST_LENGTH],
108 SHA1_DIGEST_LENGTH) != 0) return -1;
109 diff -uprN orig/tpm_emulator-0.2-x86_64/linux_module.c vtpm/linux_module.c
110 --- orig/tpm_emulator-0.2-x86_64/linux_module.c 2005-09-15 19:22:40.343080896 -0700
111 +++ vtpm/linux_module.c 1969-12-31 16:00:00.000000000 -0800
112 @@ -1,163 +0,0 @@
113 -/* Software-Based Trusted Platform Module (TPM) Emulator for Linux
114 - * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
115 - *
116 - * This module is free software; you can redistribute it and/or modify
117 - * it under the terms of the GNU General Public License as published
118 - * by the Free Software Foundation; either version 2 of the License,
119 - * or (at your option) any later version.
120 - *
121 - * This module is distributed in the hope that it will be useful,
122 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
123 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124 - * GNU General Public License for more details.
125 - *
126 - * $Id: linux_module.c 19 2005-05-18 08:29:37Z mast $
127 - */
128 -
129 -#include <linux/module.h>
130 -#include <linux/kernel.h>
131 -#include <linux/init.h>
132 -#include <linux/miscdevice.h>
133 -#include <linux/poll.h>
134 -#include "linux_module.h"
135 -#include "tpm/tpm_emulator.h"
136 -
137 -MODULE_LICENSE("GPL");
138 -MODULE_AUTHOR("Mario Strasser <mast@gmx.net>");
139 -MODULE_DESCRIPTION("Trusted Platform Module (TPM) Emulator");
140 -MODULE_SUPPORTED_DEVICE(TPM_DEVICE_NAME);
141 -
142 -/* module startup parameters */
143 -char *startup = "save";
144 -MODULE_PARM(startup, "s");
145 -MODULE_PARM_DESC(startup, " Sets the startup mode of the TPM. "
146 - "Possible values are 'clear', 'save' (default) and 'deactivated.");
147 -char *storage_file = "/var/tpm/tpm_emulator-1.2.0.1";
148 -MODULE_PARM(storage_file, "s");
149 -MODULE_PARM_DESC(storage_file, " Sets the persistent-data storage "
150 - "file of the TPM.");
151 -
152 -/* TPM lock */
153 -static struct semaphore tpm_mutex;
154 -
155 -/* TPM command response */
156 -static struct {
157 - uint8_t *data;
158 - uint32_t size;
159 -} tpm_response;
160 -
161 -/* module state */
162 -#define STATE_IS_OPEN 0
163 -static uint32_t module_state;
164 -
165 -static int tpm_open(struct inode *inode, struct file *file)
166 -{
167 - debug("%s()", __FUNCTION__);
168 - if (test_and_set_bit(STATE_IS_OPEN, (void*)&module_state)) return -EBUSY;
169 - return 0;
170 -}
171 -
172 -static int tpm_release(struct inode *inode, struct file *file)
173 -{
174 - debug("%s()", __FUNCTION__);
175 - clear_bit(STATE_IS_OPEN, (void*)&module_state);
176 - return 0;
177 -}
178 -
179 -static ssize_t tpm_read(struct file *file, char *buf, size_t count, loff_t *ppos)
180 -{
181 - debug("%s(%Zu)", __FUNCTION__, count);
182 - down(&tpm_mutex);
183 - if (tpm_response.data != NULL) {
184 - count = min(count, (size_t)tpm_response.size - (size_t)*ppos);
185 - count -= copy_to_user(buf, &tpm_response.data[*ppos], count);
186 - *ppos += count;
187 - } else {
188 - count = 0;
189 - }
190 - up(&tpm_mutex);
191 - return count;
192 -}
193 -
194 -static ssize_t tpm_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
195 -{
196 - debug("%s(%Zu)", __FUNCTION__, count);
197 - down(&tpm_mutex);
198 - *ppos = 0;
199 - if (tpm_response.data != NULL) kfree(tpm_response.data);
200 - if (tpm_handle_command(buf, count, &tpm_response.data,
201 - &tpm_response.size) != 0) {
202 - count = -EILSEQ;
203 - tpm_response.data = NULL;
204 - }
205 - up(&tpm_mutex);
206 - return count;
207 -}
208 -
209 -static int tpm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
210 -{
211 - debug("%s(%d, %ld)", __FUNCTION__, cmd, arg);
212 - return -1;
213 -}
214 -
215 -struct file_operations fops = {
216 - .owner = THIS_MODULE,
217 - .open = tpm_open,
218 - .release = tpm_release,
219 - .read = tpm_read,
220 - .write = tpm_write,
221 - .ioctl = tpm_ioctl,
222 -};
223 -
224 -static struct miscdevice tpm_dev = {
225 - .minor = TPM_DEVICE_MINOR,
226 - .name = TPM_DEVICE_NAME,
227 - .fops = &fops,
228 -};
229 -
230 -int __init init_tpm_module(void)
231 -{
232 - int res = misc_register(&tpm_dev);
233 - if (res != 0) {
234 - error("misc_register() failed for minor %d\n", TPM_DEVICE_MINOR);
235 - return res;
236 - }
237 - /* initialize variables */
238 - sema_init(&tpm_mutex, 1);
239 - module_state = 0;
240 - tpm_response.data = NULL;
241 - /* initialize TPM emulator */
242 - if (!strcmp(startup, "clear")) {
243 - tpm_emulator_init(1);
244 - } else if (!strcmp(startup, "save")) {
245 - tpm_emulator_init(2);
246 - } else if (!strcmp(startup, "deactivated")) {
247 - tpm_emulator_init(3);
248 - } else {
249 - error("invalid startup mode '%s'; must be 'clear', "
250 - "'save' (default) or 'deactivated", startup);
251 - misc_deregister(&tpm_dev);
252 - return -EINVAL;
253 - }
254 - return 0;
255 -}
256 -
257 -void __exit cleanup_tpm_module(void)
258 -{
259 - tpm_emulator_shutdown();
260 - misc_deregister(&tpm_dev);
261 -}
262 -
263 -module_init(init_tpm_module);
264 -module_exit(cleanup_tpm_module);
265 -
266 -uint64_t tpm_get_ticks(void)
267 -{
268 - static struct timespec old_time = {0, 0};
269 - struct timespec new_time = current_kernel_time();
270 - uint64_t ticks = (uint64_t)(old_time.tv_sec - new_time.tv_sec) * 1000000
271 - + (old_time.tv_nsec - new_time.tv_nsec) / 1000;
272 - old_time = new_time;
273 - return (ticks > 0) ? ticks : 1;
274 -}
275 -
276 diff -uprN orig/tpm_emulator-0.2-x86_64/linux_module.h vtpm/linux_module.h
277 --- orig/tpm_emulator-0.2-x86_64/linux_module.h 2005-09-15 19:21:14.844078720 -0700
278 +++ vtpm/linux_module.h 2005-09-14 20:27:22.000000000 -0700
279 @@ -1,5 +1,6 @@
280 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
281 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
282 + * Copyright (C) 2005 INTEL Corp
283 *
284 * This module is free software; you can redistribute it and/or modify
285 * it under the terms of the GNU General Public License as published
286 @@ -17,17 +18,22 @@
287 #ifndef _LINUX_MODULE_H_
288 #define _LINUX_MODULE_H_
290 -#include <linux/version.h>
291 -#include <linux/kernel.h>
292 -#include <linux/slab.h>
293 +#include <malloc.h>
294 +#include <stdint.h>
295 +#include <stdio.h>
296 +#include <string.h>
297 #include <linux/types.h>
298 -#include <linux/string.h>
299 -#include <linux/random.h>
300 -#include <linux/time.h>
301 -#include <asm/byteorder.h>
303 -/* module settings */
304 +#include <endian.h>
305 +#define __BYTEORDER_HAS_U64__
306 +#ifdef LITTLE_ENDIAN
307 + #include <linux/byteorder/little_endian.h>
308 +#else
309 + #include <linux/byteorder/big_endian.h>
310 +#endif
312 +/* module settings */
313 +#define min(A,B) ((A)<(B)?(A):(B))
314 #ifndef STR
315 #define STR(s) __STR__(s)
316 #define __STR__(s) #s
317 @@ -39,34 +45,38 @@
318 #define TPM_MODULE_NAME "tpm_emulator"
320 /* debug and log output functions */
321 +extern int dmi_id;
323 #ifdef DEBUG
324 -#define debug(fmt, ...) printk(KERN_DEBUG "%s %s:%d: Debug: " fmt "\n", \
325 - TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
326 +#define debug(fmt, ...) printf("TPMD[%d]: %s:%d: Debug: " fmt "\n", \
327 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
328 #else
329 #define debug(fmt, ...)
330 #endif
331 -#define info(fmt, ...) printk(KERN_INFO "%s %s:%d: Info: " fmt "\n", \
332 - TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
333 -#define error(fmt, ...) printk(KERN_ERR "%s %s:%d: Error: " fmt "\n", \
334 - TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
335 -#define alert(fmt, ...) printk(KERN_ALERT "%s %s:%d: Alert: " fmt "\n", \
336 - TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
337 +#define info(fmt, ...) printf("TPMD[%d]: %s:%d: Info: " fmt "\n", \
338 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
339 +#define error(fmt, ...) printf("TPMD[%d]: %s:%d: Error: " fmt "\n", \
340 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
341 +#define alert(fmt, ...) printf("TPMD[%d]: %s:%d: Alert: " fmt "\n", \
342 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
344 /* memory allocation */
346 static inline void *tpm_malloc(size_t size)
347 {
348 - return kmalloc(size, GFP_KERNEL);
349 + return malloc(size);
350 }
352 static inline void tpm_free(const void *ptr)
353 {
354 - if (ptr != NULL) kfree(ptr);
355 + if (ptr != NULL) free( (void *) ptr);
356 }
358 /* random numbers */
360 +//FIXME;
361 +void get_random_bytes(void *buf, int nbytes);
362 +
363 static inline void tpm_get_random_bytes(void *buf, int nbytes)
364 {
365 get_random_bytes(buf, nbytes);
366 @@ -86,9 +96,9 @@ uint64_t tpm_get_ticks(void);
367 #define CPU_TO_LE16(x) __cpu_to_le16(x)
369 #define BE64_TO_CPU(x) __be64_to_cpu(x)
370 -#define LE64_TO_CPU(x) __be64_to_cpu(x)
371 +#define LE64_TO_CPU(x) __le64_to_cpu(x)
372 #define BE32_TO_CPU(x) __be32_to_cpu(x)
373 -#define LE32_TO_CPU(x) __be32_to_cpu(x)
374 +#define LE32_TO_CPU(x) __le32_to_cpu(x)
375 #define BE16_TO_CPU(x) __be16_to_cpu(x)
376 #define LE16_TO_CPU(x) __le16_to_cpu(x)
378 diff -uprN orig/tpm_emulator-0.2-x86_64/Makefile vtpm/Makefile
379 --- orig/tpm_emulator-0.2-x86_64/Makefile 2005-09-15 19:21:14.845078568 -0700
380 +++ vtpm/Makefile 2005-09-14 20:27:22.000000000 -0700
381 @@ -1,22 +1,31 @@
382 # Software-Based Trusted Platform Module (TPM) Emulator for Linux
383 # Copyright (C) 2004 Mario Strasser <mast@gmx.net>
384 +# Copyright (C) 2005 INTEL Corp.
385 #
386 # $Id: Makefile 10 2005-04-26 20:59:50Z mast $
388 -# kernel settings
389 -KERNEL_RELEASE := $(shell uname -r)
390 -KERNEL_BUILD := /lib/modules/$(KERNEL_RELEASE)/build
391 -MOD_SUBDIR := misc
392 COMPILE_ARCH ?= $(shell uname -m | sed -e s/i.86/x86_32/)
394 # module settings
395 -MODULE_NAME := tpm_emulator
396 +BIN := vtpmd
397 VERSION_MAJOR := 0
398 VERSION_MINOR := 2
399 VERSION_BUILD := $(shell date +"%s")
401 -# enable/disable DEBUG messages
402 -EXTRA_CFLAGS += -DDEBUG -g
403 +# Installation program and options
404 +INSTALL = install
405 +INSTALL_PROG = $(INSTALL) -m0755
406 +INSTALL_DIR = $(INSTALL) -d -m0755
407 +
408 +# Xen tools installation directory
409 +TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
410 +
411 +CC := gcc
412 +CFLAGS += -g -Wall $(INCLUDE) -DDEBUG
413 +CFLAGS += -I. -Itpm
414 +
415 +# Is the simulator running in it's own vm?
416 +#CFLAGS += -DVTPM_MULTI_VM
418 ifeq ($(COMPILE_ARCH),x86_64)
419 LIBDIR = lib64
420 @@ -34,38 +43,31 @@ DIRS := . crypto tpm
421 SRCS := $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.c))
422 OBJS := $(patsubst %.c, %.o, $(SRCS))
423 SRCS += $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.h))
424 -DISTSRC := ./README ./AUTHORS ./ChangeLog ./Makefile $(SRCS)
425 -DISTDIR := tpm_emulator-$(VERSION_MAJOR).$(VERSION_MINOR)
427 -obj-m := $(MODULE_NAME).o
428 -$(MODULE_NAME)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
429 +obj-m := $(BIN)
430 +$(BIN)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
432 EXTRA_CFLAGS += -I$(src) -I$(src)/crypto -I$(src)/tpm
434 # do not print "Entering directory ..."
435 MAKEFLAGS += --no-print-directory
437 -all: $(src)/crypto/gmp.h $(src)/crypto/libgmp.a version
438 - @$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules
439 +all: $(BIN)
440 +
441 +$(BIN): $(src)/crypto/gmp.h $(src)/crypto/libgmp.a version $(SRCS) $(OBJS)
442 + $(CC) $(CFLAGS) $(OBJS) $(src)/crypto/libgmp.a -o $(BIN)
443 +
444 +%.o: %.c
445 + $(CC) $(CFLAGS) -c $< -o $@
447 install:
448 - @$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules_install
449 - test -d /var/tpm || mkdir /var/tpm
450 - test -c /dev/tpm || mknod /dev/tpm c 10 224
451 - chmod 666 /dev/tpm
452 - depmod -a
453 + $(INSTALL_PROG) $(BIN) $(TOOLS_INSTALL_DIR)
455 clean:
456 - @$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) clean
457 - rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a
458 + rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a $(OBJS)
460 -dist: $(DISTSRC)
461 - rm -rf $(DISTDIR)
462 - mkdir $(DISTDIR)
463 - cp --parents $(DISTSRC) $(DISTDIR)/
464 - rm -f $(DISTDIR)/crypto/gmp.h
465 - tar -chzf $(DISTDIR).tar.gz $(DISTDIR)
466 - rm -rf $(DISTDIR)
467 +mrproper: clean
468 + rm -f $(BIN) tpm_version.h
470 $(src)/crypto/libgmp.a:
471 test -f $(src)/crypto/libgmp.a || ln -s $(GMP_LIB) $(src)/crypto/libgmp.a
472 diff -uprN orig/tpm_emulator-0.2-x86_64/README vtpm/README
473 --- orig/tpm_emulator-0.2-x86_64/README 2005-08-15 00:58:57.000000000 -0700
474 +++ vtpm/README 2005-09-14 20:27:22.000000000 -0700
475 @@ -13,7 +13,8 @@ $Id: README 8 2005-01-25 21:11:45Z jmoli
476 Copyright
477 --------------------------------------------------------------------------
478 Copyright (C) 2004 Mario Strasser <mast@gmx.net> and Swiss Federal
479 -Institute of Technology (ETH) Zurich.
480 + Institute of Technology (ETH) Zurich.
481 +Copyright (C) 2005 INTEL Corp
483 This program is free software; you can redistribute it and/or modify
484 it under the terms of the GNU General Public License as published by
485 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_audit.c vtpm/tpm/tpm_audit.c
486 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_audit.c 2005-08-15 00:58:57.000000000 -0700
487 +++ vtpm/tpm/tpm_audit.c 2005-09-14 20:27:22.000000000 -0700
488 @@ -1,6 +1,7 @@
489 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
490 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
491 * Swiss Federal Institute of Technology (ETH) Zurich
492 + * Copyright (C) 2005 INTEL Corp
493 *
494 * This module is free software; you can redistribute it and/or modify
495 * it under the terms of the GNU General Public License as published
496 @@ -45,14 +46,14 @@ void tpm_audit_request(TPM_COMMAND_CODE
497 tpmData.permanent.data.auditMonotonicCounter++;
498 }
499 /* update audit digest */
500 - *((UINT16*)&buf[0]) = cpu_to_be16(TPM_TAG_AUDIT_EVENT_IN);
501 - *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
502 + *((UINT16*)&buf[0]) = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_IN);
503 + *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
504 sha1_init(&sha1_ctx);
505 sha1_update(&sha1_ctx, req->param, req->paramSize);
506 sha1_final(&sha1_ctx, &buf[6]);
507 - *((UINT16*)&buf[26]) = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
508 + *((UINT16*)&buf[26]) = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
509 memset(&buf[30], 0, 4);
510 - *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
511 + *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
512 sha1_init(&sha1_ctx);
513 sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest,
514 sizeof(TPM_DIGEST));
515 @@ -70,15 +71,15 @@ void tpm_audit_response(TPM_COMMAND_CODE
516 && (AUDIT_STATUS[ord / 8] & (1 << (ord & 0x07)))) {
517 info("tpm_audit_response()");
518 /* update audit digest */
519 - *((UINT16*)&buf[0]) = cpu_to_be16(TPM_TAG_AUDIT_EVENT_OUT);
520 - *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
521 + *((UINT16*)&buf[0]) = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_OUT);
522 + *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
523 sha1_init(&sha1_ctx);
524 sha1_update(&sha1_ctx, rsp->param, rsp->paramSize);
525 sha1_final(&sha1_ctx, &buf[6]);
526 - *((UINT16*)&buf[26]) = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
527 + *((UINT16*)&buf[26]) = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
528 memset(&buf[30], 0, 4);
529 - *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
530 - *((UINT32*)&buf[34]) = cpu_to_be32(rsp->result);
531 + *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
532 + *((UINT32*)&buf[34]) = CPU_TO_BE32(rsp->result);
533 sha1_init(&sha1_ctx);
534 sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest,
535 sizeof(TPM_DIGEST));
536 @@ -158,7 +159,7 @@ TPM_RESULT TPM_GetAuditDigestSigned(TPM_
537 }
538 memcpy(&buf[0], "\x05\x00ADIG", 6);
539 memcpy(&buf[6], antiReplay->nonce, 20);
540 - *(UINT32*)&buf[26] = cpu_to_be32(buf_size - 30);
541 + *(UINT32*)&buf[26] = CPU_TO_BE32(buf_size - 30);
542 memcpy(&buf[30], auditDigest->digest, 20);
543 ptr = &buf[50];
544 len = buf_size - 50;
545 @@ -198,4 +199,3 @@ TPM_RESULT TPM_SetOrdinalAuditStatus(TPM
546 }
547 return TPM_SUCCESS;
548 }
549 -
550 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_authorization.c vtpm/tpm/tpm_authorization.c
551 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_authorization.c 2005-08-15 00:58:57.000000000 -0700
552 +++ vtpm/tpm/tpm_authorization.c 2005-09-14 20:27:22.000000000 -0700
553 @@ -1,6 +1,7 @@
554 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
555 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
556 * Swiss Federal Institute of Technology (ETH) Zurich
557 + * Copyright (C) 2005 INTEL Corp
558 *
559 * This module is free software; you can redistribute it and/or modify
560 * it under the terms of the GNU General Public License as published
561 @@ -268,7 +269,7 @@ TPM_RESULT tpm_verify_auth(TPM_AUTH *aut
562 {
563 hmac_ctx_t ctx;
564 TPM_SESSION_DATA *session;
565 - UINT32 auth_handle = cpu_to_be32(auth->authHandle);
566 + UINT32 auth_handle = CPU_TO_BE32(auth->authHandle);
568 info("tpm_verify_auth(%08x)", auth->authHandle);
569 /* get dedicated authorization session */
570 @@ -316,5 +317,3 @@ void tpm_decrypt_auth_secret(TPM_ENCAUTH
571 for (i = 0; i < sizeof(TPM_SECRET); i++)
572 plainAuth[i] ^= encAuth[i];
573 }
574 -
575 -
576 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_capability.c vtpm/tpm/tpm_capability.c
577 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_capability.c 2005-08-15 00:58:57.000000000 -0700
578 +++ vtpm/tpm/tpm_capability.c 2005-09-14 20:27:22.000000000 -0700
579 @@ -1,6 +1,7 @@
580 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
581 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
582 * Swiss Federal Institute of Technology (ETH) Zurich
583 + * Copyright (C) 2005 INTEL Corp
584 *
585 * This module is free software; you can redistribute it and/or modify
586 * it under the terms of the GNU General Public License as published
587 @@ -398,7 +399,7 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
589 case TPM_CAP_KEY_HANDLE:
590 debug("[TPM_CAP_KEY_HANDLE]");
591 - subCapSize = cpu_to_be32(TPM_RT_KEY);
592 + subCapSize = CPU_TO_BE32(TPM_RT_KEY);
593 return cap_handle(4, (BYTE*)&subCapSize, respSize, resp);
595 case TPM_CAP_CHECK_LOADED:
596 @@ -472,4 +473,3 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
597 return TPM_BAD_MODE;
598 }
599 }
600 -
601 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_cmd_handler.c vtpm/tpm/tpm_cmd_handler.c
602 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_cmd_handler.c 2005-08-15 00:58:57.000000000 -0700
603 +++ vtpm/tpm/tpm_cmd_handler.c 2005-09-14 20:27:22.000000000 -0700
604 @@ -1,6 +1,7 @@
605 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
606 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
607 * Swiss Federal Institute of Technology (ETH) Zurich
608 + * Copyright (C) 2005 INTEL Corp
609 *
610 * This module is free software; you can redistribute it and/or modify
611 * it under the terms of the GNU General Public License as published
612 @@ -26,7 +27,7 @@ static void tpm_compute_in_param_digest(
613 {
614 sha1_ctx_t sha1;
615 UINT32 offset;
616 - UINT32 ord = cpu_to_be32(req->ordinal);
617 + UINT32 ord = CPU_TO_BE32(req->ordinal);
619 /* skip all key-handles at the beginning */
620 switch (req->ordinal) {
621 @@ -82,8 +83,8 @@ static void tpm_compute_in_param_digest(
622 static void tpm_compute_out_param_digest(TPM_COMMAND_CODE ordinal, TPM_RESPONSE *rsp)
623 {
624 sha1_ctx_t sha1;
625 - UINT32 res = cpu_to_be32(rsp->result);
626 - UINT32 ord = cpu_to_be32(ordinal);
627 + UINT32 res = CPU_TO_BE32(rsp->result);
628 + UINT32 ord = CPU_TO_BE32(ordinal);
630 /* compute SHA1 hash */
631 sha1_init(&sha1);
632 @@ -3081,7 +3082,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
633 hmac_update(&hmac, rsp->auth2->digest, sizeof(rsp->auth2->digest));
634 #if 0
635 if (tpm_get_auth(rsp->auth2->authHandle)->type == TPM_ST_OIAP) {
636 - UINT32 handle = cpu_to_be32(rsp->auth2->authHandle);
637 + UINT32 handle = CPU_TO_BE32(rsp->auth2->authHandle);
638 hmac_update(&hmac, (BYTE*)&handle, 4);
639 }
640 #endif
641 @@ -3096,7 +3097,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
642 hmac_update(&hmac, rsp->auth1->digest, sizeof(rsp->auth1->digest));
643 #if 0
644 if (tpm_get_auth(rsp->auth1->authHandle)->type == TPM_ST_OIAP) {
645 - UINT32 handle = cpu_to_be32(rsp->auth1->authHandle);
646 + UINT32 handle = CPU_TO_BE32(rsp->auth1->authHandle);
647 hmac_update(&hmac, (BYTE*)&handle, 4);
648 }
649 #endif
650 @@ -3179,7 +3180,9 @@ extern const char *tpm_error_to_string(T
651 static void tpm_execute_command(TPM_REQUEST *req, TPM_RESPONSE *rsp)
652 {
653 TPM_RESULT res;
654 -
655 +
656 + req->tag = (BYTE) req->tag; // VIN HACK!!!
657 +
658 /* setup authorisation as well as response tag and size */
659 memset(rsp, 0, sizeof(*rsp));
660 switch (req->tag) {
661 @@ -3878,4 +3881,3 @@ int tpm_handle_command(const uint8_t *in
662 tpm_free(rsp.param);
663 return 0;
664 }
665 -
666 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_crypto.c vtpm/tpm/tpm_crypto.c
667 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_crypto.c 2005-09-15 19:21:14.846078416 -0700
668 +++ vtpm/tpm/tpm_crypto.c 2005-09-14 20:27:22.000000000 -0700
669 @@ -1,6 +1,7 @@
670 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
671 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
672 * Swiss Federal Institute of Technology (ETH) Zurich
673 + * Copyright (C) 2005 INTEL Corp
674 *
675 * This module is free software; you can redistribute it and/or modify
676 * it under the terms of the GNU General Public License as published
677 @@ -106,7 +107,7 @@ TPM_RESULT tpm_sign(TPM_KEY_DATA *key, T
678 /* setup TPM_SIGN_INFO structure */
679 memcpy(&buf[0], "\x05\x00SIGN", 6);
680 memcpy(&buf[6], auth->nonceOdd.nonce, 20);
681 - *(UINT32*)&buf[26] = cpu_to_be32(areaToSignSize);
682 + *(UINT32*)&buf[26] = CPU_TO_BE32(areaToSignSize);
683 memcpy(&buf[30], areaToSign, areaToSignSize);
684 if (rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1,
685 buf, areaToSignSize + 30, *sig)) {
686 @@ -383,4 +384,3 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
687 }
688 return TPM_SUCCESS;
689 }
690 -
691 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_data.c vtpm/tpm/tpm_data.c
692 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_data.c 2005-09-15 19:21:14.847078264 -0700
693 +++ vtpm/tpm/tpm_data.c 2005-09-14 20:27:22.000000000 -0700
694 @@ -1,6 +1,7 @@
695 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
696 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
697 * Swiss Federal Institute of Technology (ETH) Zurich
698 + * Copyright (C) 2005 INTEL Corp
699 *
700 * This module is free software; you can redistribute it and/or modify
701 * it under the terms of the GNU General Public License as published
702 @@ -15,9 +16,15 @@
703 * $Id: tpm_data.c 9 2005-04-26 18:15:31Z mast $
704 */
706 +#include <sys/types.h>
707 +#include <sys/stat.h>
708 +#include <fcntl.h>
709 +#include <unistd.h>
710 +
711 #include "tpm_emulator.h"
712 #include "tpm_structures.h"
713 #include "tpm_marshalling.h"
714 +#include "vtpm_manager.h"
716 TPM_DATA tpmData;
718 @@ -28,6 +35,7 @@ BOOL tpm_get_physical_presence(void)
720 void tpm_init_data(void)
721 {
722 +#ifndef TPM_GENERATE_EK
723 /* endorsement key */
724 uint8_t ek_n[] = "\xa8\xdb\xa9\x42\xa8\xf3\xb8\x06\x85\x90\x76\x93\xad\xf7"
725 "\x74\xec\x3f\xd3\x3d\x9d\xe8\x2e\xff\x15\xed\x0e\xce\x5f\x93"
726 @@ -66,6 +74,8 @@ void tpm_init_data(void)
727 "\xd1\xc0\x8b\x5b\xa2\x2e\xa7\x15\xca\x50\x75\x10\x48\x9c\x2b"
728 "\x18\xb9\x67\x8f\x5d\x64\xc3\x28\x9f\x2f\x16\x2f\x08\xda\x47"
729 "\xec\x86\x43\x0c\x80\x99\x07\x34\x0f";
730 +#endif
731 +
732 int i;
733 /* reset all data to NULL, FALSE or 0 */
734 memset(&tpmData, 0, sizeof(tpmData));
735 @@ -85,6 +95,10 @@ void tpm_init_data(void)
736 tpmData.permanent.data.version.revMinor = VERSION_MINOR;
737 /* setup PCR attributes */
738 for (i = 0; i < TPM_NUM_PCR; i++) {
739 + int j;
740 + for (j=0; j < TPM_NUM_LOCALITY; j++) {
741 + tpmData.permanent.data.pcrAttrib[i].pcrExtendLocal[j] = TRUE;
742 + }
743 tpmData.permanent.data.pcrAttrib[i].pcrReset = TRUE;
744 }
745 /* set tick type */
746 @@ -115,49 +129,235 @@ void tpm_release_data(void)
748 #ifdef TPM_STORE_TO_FILE
750 -#include <linux/fs.h>
751 -#include <linux/unistd.h>
752 -#include <asm/uaccess.h>
753 +#include <sys/types.h>
754 +#include <sys/stat.h>
755 +#include <fcntl.h>
756 +
757 + static int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
759 -#define TPM_STORAGE_FILE "/var/tpm/tpm_emulator-1.2." STR(VERSION_MAJOR) "." STR(VERSION_MINOR)
760 +#ifdef VTPM_MUTLI_VM
761 + #define DEV_FE "/dev/tpm"
762 +#else
763 + #define VTPM_RX_FIFO_D "/var/vtpm/fifos/vtpm-to-%d.fifo"
764 + #define VTPM_TX_FIFO "/var/vtpm/fifos/vtpm-from-all.fifo"
765 +
766 + extern int dmi_id;
767 + static char *vtpm_rx_name=NULL;
768 +#endif
770 static int write_to_file(uint8_t *data, size_t data_length)
771 {
772 - int res;
773 - struct file *fp;
774 - mm_segment_t old_fs = get_fs();
775 - fp = filp_open(TPM_STORAGE_FILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
776 - if (IS_ERR(fp)) return -1;
777 - set_fs(get_ds());
778 - res = fp->f_op->write(fp, data, data_length, &fp->f_pos);
779 - set_fs(old_fs);
780 - filp_close(fp, NULL);
781 - return (res == data_length) ? 0 : -1;
782 + int res, out_data_size, in_header_size;
783 + BYTE *ptr, *out_data, *in_header;
784 + UINT32 result, len, in_rsp_size;
785 + UINT16 tag = VTPM_TAG_REQ;
786 +
787 + printf("Saving NVM\n");
788 + if (vtpm_tx_fh < 0) {
789 +#ifdef VTPM_MUTLI_VM
790 + vtpm_tx_fh = open(DEV_FE, O_RDWR);
791 +#else
792 + vtpm_tx_fh = open(VTPM_TX_FIFO, O_WRONLY);
793 +#endif
794 + }
795 +
796 + if (vtpm_tx_fh < 0) {
797 + return -1;
798 + }
799 +
800 + // Send request to VTPM Manager to encrypt data
801 +#ifdef VTPM_MUTLI_VM
802 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_CLT + data_length;
803 +#else
804 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_SRV + data_length;
805 +#endif
806 +
807 + out_data = ptr = (BYTE *) malloc(len);
808 +
809 + if (ptr == NULL
810 +#ifndef VTPM_MUTLI_VM
811 + || tpm_marshal_UINT32(&ptr, &len, dmi_id)
812 +#endif
813 + || tpm_marshal_UINT16(&ptr, &len, tag)
814 +#ifdef VTPM_MUTLI_VM
815 + || tpm_marshal_UINT32(&ptr, &len, out_data_size)
816 +#else
817 + || tpm_marshal_UINT32(&ptr, &len, out_data_size - sizeof(uint32_t))
818 +#endif
819 + || tpm_marshal_UINT32(&ptr, &len, VTPM_ORD_SAVENVM)
820 + || tpm_marshal_BYTE_ARRAY(&ptr, &len, data, data_length)) {
821 + free(out_data);
822 + return -1;
823 + }
824 +
825 + printf("\tSending SaveNVM Command.\n");
826 + res = write(vtpm_tx_fh, out_data, out_data_size);
827 + free(out_data);
828 + if (res != out_data_size) return -1;
829 +
830 + if (vtpm_rx_fh < 0) {
831 +#ifdef VTPM_MUTLI_VM
832 + vtpm_rx_fh = vtpm_tx_fh
833 +#else
834 + if (vtpm_rx_name == NULL) {
835 + vtpm_rx_name = malloc(10 + strlen(VTPM_RX_FIFO_D));
836 + sprintf(vtpm_rx_name, VTPM_RX_FIFO_D, (uint32_t) dmi_id);
837 + }
838 + vtpm_rx_fh = open(vtpm_rx_name, O_RDONLY);
839 +#endif
840 + }
841 +
842 + if (vtpm_rx_fh < 0) {
843 + return -1;
844 + }
845 +
846 + // Read Header of response so we can get the size & status
847 +#ifdef VTPM_MUTLI_VM
848 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
849 +#else
850 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
851 +#endif
852 + in_header = ptr = malloc(in_header_size);
853 +
854 + printf("\tReading SaveNVM header.\n");
855 + res = read(vtpm_rx_fh, in_header, in_header_size);
856 +
857 + if ( (res != in_header_size)
858 +#ifndef VTPM_MUTLI_VM
859 + || tpm_unmarshal_UINT32(&ptr, &len, (UINT32*)&dmi_id)
860 +#endif
861 + || tpm_unmarshal_UINT16(&ptr, &len, &tag)
862 + || tpm_unmarshal_UINT32(&ptr, &len, &in_rsp_size)
863 + || tpm_unmarshal_UINT32(&ptr, &len, &result) ) {
864 + free(in_header);
865 + return -1;
866 + }
867 + free(in_header);
868 +
869 + if (result != VTPM_SUCCESS) {
870 + return -1;
871 + }
872 +
873 +#ifdef VTPM_MUTLI_VM
874 + close(vtpm_tx_fh); close(vtpm_rx_fh);
875 +#endif
876 +
877 + printf("\tFinishing up SaveNVM\n");
878 + return (0);
879 }
881 static int read_from_file(uint8_t **data, size_t *data_length)
882 {
883 - int res;
884 - struct file *fp;
885 - mm_segment_t old_fs = get_fs();
886 - fp = filp_open(TPM_STORAGE_FILE, O_RDONLY, 0);
887 - if (IS_ERR(fp)) return -1;
888 - *data_length = (size_t)fp->f_dentry->d_inode->i_size;
889 - /* *data_length = i_size_read(fp->f_dentry->d_inode); */
890 - *data = tpm_malloc(*data_length);
891 - if (*data == NULL) {
892 - filp_close(fp, NULL);
893 + int res, out_data_size, in_header_size;
894 + uint8_t *ptr, *out_data, *in_header;
895 + UINT16 tag = VTPM_TAG_REQ;
896 + UINT32 len, in_rsp_size, result;
897 +#ifdef VTPM_MUTLI_VM
898 + int vtpm_rx_fh, vtpm_tx_fh;
899 +#endif
900 +
901 + printf("Loading NVM.\n");
902 + if (vtpm_tx_fh < 0) {
903 +#ifdef VTPM_MUTLI_VM
904 + vtpm_tx_fh = open(DEV_FE, O_RDWR);
905 +#else
906 + vtpm_tx_fh = open(VTPM_TX_FIFO, O_WRONLY);
907 +#endif
908 + }
909 +
910 + if (vtpm_tx_fh < 0) {
911 + return -1;
912 + }
913 +
914 + // Send request to VTPM Manager to encrypt data
915 +#ifdef VTPM_MUTLI_VM
916 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
917 +#else
918 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
919 +#endif
920 + out_data = ptr = (BYTE *) malloc(len);
921 +
922 + if (ptr == NULL
923 +#ifndef VTPM_MUTLI_VM
924 + || tpm_marshal_UINT32(&ptr, &len, dmi_id)
925 +#endif
926 + || tpm_marshal_UINT16(&ptr, &len, tag)
927 +#ifdef VTPM_MUTLI_VM
928 + || tpm_marshal_UINT32(&ptr, &len, out_data_size)
929 +#else
930 + || tpm_marshal_UINT32(&ptr, &len, out_data_size - sizeof(uint32_t))
931 +#endif
932 + || tpm_marshal_UINT32(&ptr, &len, VTPM_ORD_LOADNVM)) {
933 + free(out_data);
934 return -1;
935 }
936 - set_fs(get_ds());
937 - res = fp->f_op->read(fp, *data, *data_length, &fp->f_pos);
938 - set_fs(old_fs);
939 - filp_close(fp, NULL);
940 +
941 + printf("\tSending LoadNVM command\n");
942 + res = write(vtpm_tx_fh, out_data, out_data_size);
943 + free(out_data);
944 + if (res != out_data_size) return -1;
945 +
946 + if (vtpm_rx_fh < 0) {
947 +#ifdef VTPM_MUTLI_VM
948 + vtpm_rx_fh = vtpm_tx_fh;
949 +#else
950 + if (vtpm_rx_name == NULL) {
951 + vtpm_rx_name = malloc(10 + strlen(VTPM_RX_FIFO_D));
952 + sprintf(vtpm_rx_name, VTPM_RX_FIFO_D, (uint32_t) dmi_id);
953 + }
954 + vtpm_rx_fh = open(vtpm_rx_name, O_RDONLY);
955 +#endif
956 + }
957 +
958 + if (vtpm_rx_fh < 0) {
959 + return -1;
960 + }
961 +
962 + // Read Header of response so we can get the size & status
963 +#ifdef VTPM_MUTLI_VM
964 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
965 +#else
966 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
967 +#endif
968 + in_header = ptr = malloc(in_header_size);
969 +
970 + printf("\tReading LoadNVM header\n");
971 + res = read(vtpm_rx_fh, in_header, in_header_size);
972 +
973 + if ( (res != in_header_size)
974 +#ifndef VTPM_MUTLI_VM
975 + || tpm_unmarshal_UINT32(&ptr, &len, (UINT32*)&dmi_id)
976 +#endif
977 + || tpm_unmarshal_UINT16(&ptr, &len, &tag)
978 + || tpm_unmarshal_UINT32(&ptr, &len, &in_rsp_size)
979 + || tpm_unmarshal_UINT32(&ptr, &len, &result) ) {
980 + free(in_header);
981 + return -1;
982 + }
983 + free(in_header);
984 +
985 + if (result != VTPM_SUCCESS) {
986 + return -1;
987 + }
988 +
989 + // Read Encrypted data from VTPM Manager
990 + *data_length = in_rsp_size - VTPM_COMMAND_HEADER_SIZE_CLT;
991 + *data = (uint8_t *) malloc(*data_length);
992 +
993 + printf("\tReading clear data from LoadNVM.\n");
994 + res = read(vtpm_rx_fh, *data, *data_length);
995 +#ifdef VTPM_MUTLI_VM
996 + close(vtpm_rx_fh);close(vtpm_tx_fh);
997 +#endif
998 +
999 + printf("\tReturing from loading NVM\n");
1000 if (res != *data_length) {
1001 - tpm_free(*data);
1002 - return -1;
1003 + free(*data);
1004 + return -1;
1005 + } else {
1006 + return 0;
1008 - return 0;
1012 #else
1013 @@ -232,7 +432,6 @@ int tpm_restore_permanent_data(void)
1015 int tpm_erase_permanent_data(void)
1017 - int res = write_to_file("", 0);
1018 + int res = write_to_file((uint8_t*)"", 0);
1019 return res;
1022 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_deprecated.c vtpm/tpm/tpm_deprecated.c
1023 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_deprecated.c 2005-08-15 00:58:57.000000000 -0700
1024 +++ vtpm/tpm/tpm_deprecated.c 2005-09-14 20:27:22.000000000 -0700
1025 @@ -1,6 +1,7 @@
1026 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
1027 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
1028 * Swiss Federal Institute of Technology (ETH) Zurich
1029 + * Copyright (C) 2005 INTEL Corp
1031 * This module is free software; you can redistribute it and/or modify
1032 * it under the terms of the GNU General Public License as published
1033 @@ -50,7 +51,7 @@ TPM_RESULT TPM_SaveKeyContext(TPM_KEY_HA
1034 BYTE *ptr;
1035 UINT32 len;
1036 info("TPM_SaveKeyContext()");
1037 - res = TPM_SaveContext(keyHandle, TPM_RT_KEY, "SaveKeyContext..",
1038 + res = TPM_SaveContext(keyHandle, TPM_RT_KEY, (BYTE*)"SaveKeyContext..",
1039 keyContextSize, &contextBlob);
1040 if (res != TPM_SUCCESS) return res;
1041 len = *keyContextSize;
1042 @@ -82,7 +83,7 @@ TPM_RESULT TPM_SaveAuthContext(TPM_AUTHH
1043 BYTE *ptr;
1044 UINT32 len;
1045 info("TPM_SaveAuthContext()");
1046 - res = TPM_SaveContext(authHandle, TPM_RT_KEY, "SaveAuthContext.",
1047 + res = TPM_SaveContext(authHandle, TPM_RT_KEY, (BYTE*)"SaveAuthContext.",
1048 authContextSize, &contextBlob);
1049 if (res != TPM_SUCCESS) return res;
1050 len = *authContextSize;
1051 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_emulator.h vtpm/tpm/tpm_emulator.h
1052 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_emulator.h 2005-08-15 00:58:57.000000000 -0700
1053 +++ vtpm/tpm/tpm_emulator.h 2005-09-14 20:27:22.000000000 -0700
1054 @@ -1,5 +1,6 @@
1055 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
1056 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
1057 + * Copyright (C) 2005 INTEL Corp
1059 * This module is free software; you can redistribute it and/or modify
1060 * it under the terms of the GNU General Public License as published
1061 @@ -22,7 +23,8 @@
1062 /* TPM configuration */
1063 #define TPM_STORE_TO_FILE 1
1064 #undef TPM_STRONG_PERSISTENCE
1065 -#undef TPM_GENERATE_EK
1066 +//#undef TPM_GENERATE_EK
1067 +#define TPM_GENERATE_EK
1069 /**
1070 * tpm_emulator_init - initialises and starts the TPM emulator
1071 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_integrity.c vtpm/tpm/tpm_integrity.c
1072 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_integrity.c 2005-08-15 00:58:57.000000000 -0700
1073 +++ vtpm/tpm/tpm_integrity.c 2005-09-14 20:27:22.000000000 -0700
1074 @@ -1,6 +1,7 @@
1075 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
1076 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
1077 * Swiss Federal Institute of Technology (ETH) Zurich
1078 + * Copyright (C) 2005 INTEL Corp
1080 * This module is free software; you can redistribute it and/or modify
1081 * it under the terms of the GNU General Public License as published
1082 @@ -194,4 +195,3 @@ TPM_RESULT tpm_verify_pcr(TPM_KEY_DATA *
1084 return TPM_SUCCESS;
1087 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_structures.h vtpm/tpm/tpm_structures.h
1088 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_structures.h 2005-08-15 00:58:57.000000000 -0700
1089 +++ vtpm/tpm/tpm_structures.h 2005-09-14 20:27:22.000000000 -0700
1090 @@ -1,6 +1,7 @@
1091 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
1092 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
1093 * Swiss Federal Institute of Technology (ETH) Zurich
1094 + * Copyright (C) 2005 INTEL Corp
1096 * This module is free software; you can redistribute it and/or modify
1097 * it under the terms of the GNU General Public License as published
1098 @@ -18,7 +19,7 @@
1099 #ifndef _TPM_STRUCTURES_H_
1100 #define _TPM_STRUCTURES_H_
1102 -#include <linux/types.h>
1103 +//#include <linux/types.h>
1104 #include "crypto/rsa.h"
1106 /*
1107 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_testing.c vtpm/tpm/tpm_testing.c
1108 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_testing.c 2005-08-15 00:58:57.000000000 -0700
1109 +++ vtpm/tpm/tpm_testing.c 2005-09-14 20:27:22.000000000 -0700
1110 @@ -1,6 +1,7 @@
1111 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
1112 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
1113 * Swiss Federal Institute of Technology (ETH) Zurich
1114 + * Copyright (C) 2005 INTEL Corp
1116 * This module is free software; you can redistribute it and/or modify
1117 * it under the terms of the GNU General Public License as published
1118 @@ -95,24 +96,24 @@ static int tpm_test_sha1(void)
1119 struct {
1120 uint8_t *data; uint32_t repetitions; uint8_t *digest;
1121 } test_cases[] = {{
1122 - "abc", 1,
1123 - "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
1124 + (uint8_t*)"abc", 1,
1125 + (uint8_t*)"\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
1126 }, {
1127 - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
1128 - "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
1129 + (uint8_t*)"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
1130 + (uint8_t*)"\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
1131 }, {
1132 - "a", 1000000,
1133 - "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
1134 + (uint8_t*)"a", 1000000,
1135 + (uint8_t*)"\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
1136 }, {
1137 - "0123456701234567012345670123456701234567012345670123456701234567", 10,
1138 - "\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52"
1139 + (uint8_t*)"0123456701234567012345670123456701234567012345670123456701234567", 10,
1140 + (uint8_t*)"\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52"
1141 }};
1143 debug("tpm_test_sha1()");
1144 for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
1145 sha1_init(&ctx);
1146 for (j = 0; j < test_cases[i].repetitions; j++)
1147 - sha1_update(&ctx, test_cases[i].data, strlen(test_cases[i].data));
1148 + sha1_update(&ctx, test_cases[i].data, strlen((char*)test_cases[i].data));
1149 sha1_final(&ctx, digest);
1150 if (memcmp(digest, test_cases[i].digest, SHA1_DIGEST_LENGTH) != 0) return -1;
1152 @@ -128,41 +129,41 @@ static int tpm_test_hmac(void)
1153 struct {
1154 uint8_t *key, key_len, *data, data_len, *digest;
1155 } test_cases[] = {{
1156 - "\x0b", 20, "Hi There", 8,
1157 - "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
1158 + (uint8_t*)"\x0b", 20, (uint8_t*)"Hi There", 8,
1159 + (uint8_t*)"\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
1160 }, {
1161 - "Jefe", 4, "what do ya want for nothing?", 28,
1162 - "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
1163 + (uint8_t*)"Jefe", 4, (uint8_t*)"what do ya want for nothing?", 28,
1164 + (uint8_t*)"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
1165 }, {
1166 - "\xaa", 20, "\xdd", 50,
1167 - "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
1168 + (uint8_t*)"\xaa", 20, (uint8_t*)"\xdd", 50,
1169 + (uint8_t*)"\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
1170 }, {
1171 - "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
1172 - "\x15\x16\x17\x18\x19", 25, "\xcd", 50,
1173 - "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
1174 + (uint8_t*)"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
1175 + "\x15\x16\x17\x18\x19", 25, (uint8_t*)"\xcd", 50,
1176 + (uint8_t*)"\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
1177 }, {
1178 - "\x0c", 20, "Test With Truncation", 20,
1179 - "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
1180 + (uint8_t*)"\x0c", 20, (uint8_t*)"Test With Truncation", 20,
1181 + (uint8_t*)"\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
1182 }, {
1183 - "\xaa", 80, "Test Using Larger Than Block-Size Key - Hash Key First", 54,
1184 - "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
1185 + (uint8_t*)"\xaa", 80, (uint8_t*)"Test Using Larger Than Block-Size Key - Hash Key First", 54,
1186 + (uint8_t*)"\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
1187 }, {
1188 - "\xaa", 80,
1189 - "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
1190 - "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
1191 + (uint8_t*)"\xaa", 80,
1192 + (uint8_t*)"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
1193 + (uint8_t*)"\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
1194 }};
1196 debug("tpm_test_hmac()");
1197 for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
1198 - if (strlen(test_cases[i].key) < test_cases[i].key_len) {
1199 + if (strlen((char*)test_cases[i].key) < test_cases[i].key_len) {
1200 uint8_t key[test_cases[i].key_len];
1201 memset(key, test_cases[i].key[0], test_cases[i].key_len);
1202 hmac_init(&ctx, key, test_cases[i].key_len);
1203 } else {
1204 hmac_init(&ctx, test_cases[i].key, test_cases[i].key_len);
1206 - for (j = 0; j < test_cases[i].data_len; j += strlen(test_cases[i].data)) {
1207 - hmac_update(&ctx, test_cases[i].data, strlen(test_cases[i].data));
1208 + for (j = 0; j < test_cases[i].data_len; j += strlen((char*)test_cases[i].data)) {
1209 + hmac_update(&ctx, test_cases[i].data, strlen((char*)test_cases[i].data));
1211 hmac_final(&ctx, digest);
1212 if (memcmp(digest, test_cases[i].digest, SHA1_DIGEST_LENGTH) != 0) return -1;
1213 @@ -173,9 +174,9 @@ static int tpm_test_hmac(void)
1214 static int tpm_test_rsa_EK(void)
1216 int res = 0;
1217 - char *data = "RSA PKCS #1 v1.5 Test-String";
1218 + uint8_t *data = (uint8_t*)"RSA PKCS #1 v1.5 Test-String";
1219 uint8_t buf[256];
1220 - size_t buf_len, data_len = strlen(data);
1221 + size_t buf_len, data_len = strlen((char*)data);
1222 rsa_private_key_t priv_key;
1223 rsa_public_key_t pub_key;
1225 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/tpm_ticks.c vtpm/tpm/tpm_ticks.c
1226 --- orig/tpm_emulator-0.2-x86_64/tpm/tpm_ticks.c 2005-08-15 00:58:57.000000000 -0700
1227 +++ vtpm/tpm/tpm_ticks.c 2005-09-14 20:27:22.000000000 -0700
1228 @@ -1,6 +1,7 @@
1229 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
1230 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
1231 * Swiss Federal Institute of Technology (ETH) Zurich
1232 + * Copyright (C) 2005 INTEL Corp
1234 * This module is free software; you can redistribute it and/or modify
1235 * it under the terms of the GNU General Public License as published
1236 @@ -37,9 +38,7 @@ TPM_RESULT TPM_SetTickType(TPM_TICKTYPE
1237 TPM_RESULT TPM_GetTicks(TPM_CURRENT_TICKS *currentTime)
1239 info("TPM_GetTicks()");
1240 - memcpy(currentTime, &tpmData.stany.data.currentTicks,
1241 - sizeof(TPM_CURRENT_TICKS));
1242 - return TPM_SUCCESS;
1243 + return TPM_DISABLED_CMD;
1246 TPM_RESULT TPM_TickStampBlob(TPM_KEY_HANDLE keyHandle, TPM_NONCE *antiReplay,
1247 @@ -47,61 +46,12 @@ TPM_RESULT TPM_TickStampBlob(TPM_KEY_HAN
1248 TPM_CURRENT_TICKS *currentTicks,
1249 UINT32 *sigSize, BYTE **sig)
1251 - TPM_RESULT res;
1252 - TPM_KEY_DATA *key;
1253 - BYTE *info, *p;
1254 - UINT32 info_length, length;
1255 info("TPM_TickStampBlob()");
1256 - /* get key */
1257 - key = tpm_get_key(keyHandle);
1258 - if (key == NULL) return TPM_INVALID_KEYHANDLE;
1259 - /* verify authorization */
1260 - res = tpm_verify_auth(auth1, key->usageAuth, keyHandle);
1261 - if (res != TPM_SUCCESS) return res;
1262 - if (key->keyUsage != TPM_KEY_SIGNING && key->keyUsage != TPM_KEY_LEGACY
1263 - && key->keyUsage != TPM_KEY_IDENTITY) return TPM_INVALID_KEYUSAGE;
1264 - /* get current ticks */
1265 - TPM_GetTicks(currentTicks);
1266 - /* sign data using signature scheme PKCS1_SHA1 and TPM_SIGN_INFO container */
1267 - *sigSize = key->key.size >> 3;
1268 - *sig = tpm_malloc(*sigSize);
1269 - if (*sig == NULL) return TPM_FAIL;
1270 - /* setup TPM_SIGN_INFO structure */
1271 - info_length = 30 + sizeof(TPM_DIGEST) + sizeof_TPM_CURRENT_TICKS(currentTicks);
1272 - info = tpm_malloc(info_length);
1273 - if (info == NULL) {
1274 - tpm_free(*sig);
1275 - return TPM_FAIL;
1276 - }
1277 - memcpy(&info[0], "\x05\x00TSTP", 6);
1278 - memcpy(&info[6], antiReplay->nonce, 20);
1279 - *(UINT32*)&info[26] = cpu_to_be32(20
1280 - + sizeof_TPM_CURRENT_TICKS(currentTicks));
1281 - memcpy(&info[30], digestToStamp->digest, sizeof(TPM_DIGEST));
1282 - p = &info[30 + sizeof(TPM_DIGEST)];
1283 - length = sizeof_TPM_CURRENT_TICKS(currentTicks);
1284 - if (tpm_marshal_TPM_CURRENT_TICKS(&p, &length, currentTicks)
1285 - || rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1, info, info_length, *sig)) {
1286 - tpm_free(*sig);
1287 - tpm_free(info);
1288 - return TPM_FAIL;
1289 - }
1290 - return TPM_SUCCESS;
1291 + return TPM_DISABLED_CMD;
1294 void tpm_update_ticks(void)
1296 - if (tpmData.stany.data.currentTicks.tag == 0) {
1297 - tpmData.stany.data.currentTicks.tag = TPM_TAG_CURRENT_TICKS;
1298 - tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();
1299 - tpmData.stany.data.currentTicks.tickType = tpmData.permanent.data.tickType;
1300 - tpm_get_random_bytes(tpmData.stany.data.currentTicks.tickNonce.nonce,
1301 - sizeof(TPM_NONCE));
1302 - tpmData.stany.data.currentTicks.tickRate = 1;
1303 - tpmData.stany.data.currentTicks.tickSecurity = TICK_SEC_NO_CHECK;
1304 - } else {
1305 - tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();
1306 - }
1310 diff -uprN orig/tpm_emulator-0.2-x86_64/tpm/vtpm_manager.h vtpm/tpm/vtpm_manager.h
1311 --- orig/tpm_emulator-0.2-x86_64/tpm/vtpm_manager.h 1969-12-31 16:00:00.000000000 -0800
1312 +++ vtpm/tpm/vtpm_manager.h 2005-09-14 20:27:22.000000000 -0700
1313 @@ -0,0 +1,126 @@
1314 +// ===================================================================
1315 +//
1316 +// Copyright (c) 2005, Intel Corp.
1317 +// All rights reserved.
1318 +//
1319 +// Redistribution and use in source and binary forms, with or without
1320 +// modification, are permitted provided that the following conditions
1321 +// are met:
1322 +//
1323 +// * Redistributions of source code must retain the above copyright
1324 +// notice, this list of conditions and the following disclaimer.
1325 +// * Redistributions in binary form must reproduce the above
1326 +// copyright notice, this list of conditions and the following
1327 +// disclaimer in the documentation and/or other materials provided
1328 +// with the distribution.
1329 +// * Neither the name of Intel Corporation nor the names of its
1330 +// contributors may be used to endorse or promote products derived
1331 +// from this software without specific prior written permission.
1332 +//
1333 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1334 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1335 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1336 +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
1337 +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
1338 +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1339 +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1340 +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1341 +// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1342 +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1343 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1344 +// OF THE POSSIBILITY OF SUCH DAMAGE.
1345 +// ===================================================================
1346 +//
1347 +// vtpm_manager.h
1348 +//
1349 +// Public Interface header for VTPM Manager
1350 +//
1351 +// ==================================================================
1353 +#ifndef __VTPM_MANAGER_H__
1354 +#define __VTPM_MANAGER_H__
1356 +#define VTPM_TAG_REQ 0x01c1
1357 +#define VTPM_TAG_RSP 0x01c4
1358 +#define COMMAND_BUFFER_SIZE 4096
1360 +// Header sizes. Note Header MAY include the DMI
1361 +#define VTPM_COMMAND_HEADER_SIZE_SRV ( sizeof(UINT32) + sizeof(TPM_TAG) + sizeof(UINT32) + sizeof(TPM_COMMAND_CODE))
1362 +#define VTPM_COMMAND_HEADER_SIZE_CLT ( sizeof(TPM_TAG) + sizeof(UINT32) + sizeof(TPM_COMMAND_CODE))
1364 +//************************ Command Codes ****************************
1365 +#define VTPM_ORD_OPEN 1 // ULM Creates New DMI
1366 +#define VTPM_ORD_CLOSE 2 // ULM Closes a DMI
1367 +#define VTPM_ORD_DELETE 3 // ULM Permemently Deletes DMI
1368 +#define VTPM_ORD_SAVENVM 4 // DMI requests Secrets Unseal
1369 +#define VTPM_ORD_LOADNVM 5 // DMI requests Secrets Saved
1370 +#define VTPM_ORD_TPMCOMMAND 6 // DMI issues HW TPM Command
1372 +//************************ Return Codes ****************************
1373 +#define VTPM_SUCCESS 0
1374 +#define VTPM_FAIL 1
1375 +#define VTPM_UNSUPPORTED 2
1376 +#define VTPM_FORBIDDEN 3
1377 +#define VTPM_RESTORE_CONTEXT_FAILED 4
1378 +#define VTPM_INVALID_REQUEST 5
1380 +/******************* Command Parameter API *************************
1382 +VTPM Command Format
1383 + dmi: 4 bytes // Source of message.
1384 + // WARNING: This is prepended by the channel.
1385 + // Thus it is received by VTPM Manager,
1386 + // but not sent by DMI
1387 + tpm tag: 2 bytes
1388 + command size: 4 bytes // Size of command including header but not DMI
1389 + ord: 4 bytes // Command ordinal above
1390 + parameters: size - 10 bytes // Command Parameter
1392 +VTPM Response Format
1393 + tpm tag: 2 bytes
1394 + response_size: 4 bytes
1395 + status: 4 bytes
1396 + parameters: size - 10 bytes
1399 +VTPM_Open:
1400 + Input Parameters:
1401 + Domain_type: 1 byte
1402 + domain_id: 4 bytes
1403 + instance_id: 4 bytes
1404 + Output Parameters:
1405 + None
1407 +VTPM_Close
1408 + Input Parameters:
1409 + instance_id: 4 bytes
1410 + Output Parameters:
1411 + None
1413 +VTPM_Delete
1414 + Input Parameters:
1415 + instance_id: 4 bytes
1416 + Output Parameters:
1417 + None
1419 +VTPM_SaveNVM
1420 + Input Parameters:
1421 + data: n bytes (Header indicates size of data)
1422 + Output Parameters:
1423 + None
1425 +VTPM_LoadNVM
1426 + Input Parameters:
1427 + None
1428 + Output Parameters:
1429 + data: n bytes (Header indicates size of data)
1431 +VTPM_TPMCommand
1432 + Input Parameters:
1433 + TPM Command Byte Stream: n bytes
1434 + Output Parameters:
1435 + TPM Reponse Byte Stream: n bytes
1437 +*********************************************************************/
1439 +#endif //_VTPM_MANAGER_H_
1440 diff -uprN orig/tpm_emulator-0.2-x86_64/tpmd.c vtpm/tpmd.c
1441 --- orig/tpm_emulator-0.2-x86_64/tpmd.c 1969-12-31 16:00:00.000000000 -0800
1442 +++ vtpm/tpmd.c 2005-09-15 19:28:55.783005352 -0700
1443 @@ -0,0 +1,207 @@
1444 +/* Software-Based Trusted Platform Module (TPM) Emulator for Linux
1445 + * Copyright (C) 2005 INTEL Corp
1446 + *
1447 + * This module is free software; you can redistribute it and/or modify
1448 + * it under the terms of the GNU General Public License as published
1449 + * by the Free Software Foundation; either version 2 of the License,
1450 + * or (at your option) any later version.
1451 + *
1452 + * This module is distributed in the hope that it will be useful,
1453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1455 + * GNU General Public License for more details.
1456 + *
1457 + */
1459 +#include <stdio.h>
1460 +#include <stdlib.h>
1461 +#include <unistd.h>
1462 +#include <string.h>
1463 +#include <sys/types.h>
1464 +#include <sys/stat.h>
1465 +#include <fcntl.h>
1466 +#include <sys/time.h>
1468 +#include "tpm_emulator.h"
1469 +#include "vtpm_manager.h"
1471 +#ifdef VTPM_MULTI_VM
1472 + #define DEV_BE "/dev/vtpm"
1473 +#else
1474 + #define GUEST_RX_FIFO_D "/var/vtpm/fifos/guest-to-%d.fifo"
1475 + #define GUEST_TX_FIFO "/var/vtpm/fifos/guest-from-all.fifo"
1476 +#endif
1478 + int dmi_id;
1480 +#define BUFFER_SIZE 2048
1482 +static uint8_t ctrl_msg[] = { 0, 0, 0, 0, // destination
1483 + 1, 193, // VTPM_TAG
1484 + 0, 0, 0, 10, // Size
1485 + 0, 0, 0, 0}; // TPM_SUCCESS
1488 +static int devurandom=0;
1491 +void get_random_bytes(void *buf, int nbytes) {
1493 + if (devurandom == 0) {
1494 + devurandom = open("/dev/urandom", O_RDONLY);
1495 + }
1497 + if (read(devurandom, buf, nbytes) != nbytes) {
1498 + printf("Can't get random number.\n");
1499 + exit(-1);
1500 + }
1501 +}
1503 +uint64_t tpm_get_ticks(void)
1504 +{
1505 + //struct timeval tv;
1506 + //int gettimeofday(&tv, struct timezone *tz);
1507 + return 0;
1508 +}
1510 +int main(int argc, char **argv)
1511 +{
1512 + uint8_t in[BUFFER_SIZE], *out, *addressed_out;
1513 + uint32_t out_size;
1514 + int in_size, written;
1515 + int i, guest_id=-1;
1517 + int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
1518 +#ifdef VTPM_MULTI_VM
1519 + if (argc < 2) {
1520 + printf("Usage: tpmd clear|save|deactivated\n" );
1521 +#else
1522 + if (argc < 3) {
1523 + printf("Usage: tpmd clear|save|deactivated vtpmid\n" );
1524 +#endif
1525 + return -1;
1526 + }
1528 +#ifndef VTPM_MULTI_VM
1529 + dmi_id = atoi(argv[2]);
1530 +#endif
1532 + /* initialize TPM emulator */
1533 + if (!strcmp(argv[1], "clear")) {
1534 + printf("Initializing tpm: %s\n", argv[1]);
1535 + tpm_emulator_init(1);
1536 + } else if (!strcmp(argv[1], "save")) {
1537 + printf("Initializing tpm: %s\n", argv[1]);
1538 + tpm_emulator_init(2);
1539 + } else if (!strcmp(argv[1], "deactivated")) {
1540 + printf("Initializing tpm: %s\n", argv[1]);
1541 + tpm_emulator_init(3);
1542 + } else {
1543 + printf("invalid startup mode '%s'; must be 'clear', "
1544 + "'save' (default) or 'deactivated", argv[1]);
1545 + return -1;
1546 + }
1548 + char *guest_rx_file = malloc(10 + strlen(GUEST_RX_FIFO_D));
1549 + sprintf(guest_rx_file, GUEST_RX_FIFO_D, (uint32_t) dmi_id);
1551 + while (1) {
1552 +abort_command:
1553 + if (vtpm_rx_fh < 0) {
1554 +#ifdef VTPM_MUTLI_VM
1555 + vtpm_rx_fh = open(DEV_BE, O_RDWR);
1556 +#else
1557 + vtpm_rx_fh = open(guest_rx_file, O_RDONLY);
1558 +#endif
1559 + }
1561 + if (vtpm_rx_fh < 0) {
1562 + printf("ERROR: failed to open devices to listen to guest.\n");
1563 + return -1;
1564 + }
1566 + in_size = read(vtpm_rx_fh, in, BUFFER_SIZE);
1567 + if (in_size < 6) { // Magic size of minium TPM command
1568 + printf("Recv[%d] to small: 0x", in_size);
1569 + if (in_size <= 0) {
1570 + close(vtpm_rx_fh);
1571 + vtpm_rx_fh = -1;
1572 + goto abort_command;
1573 + }
1574 + } else {
1575 + printf("Recv[%d]: 0x", in_size);
1576 + for (i=0; i< in_size; i++)
1577 + printf("%x ", in[i]);
1578 + printf("\n");
1579 + }
1581 + if (guest_id == -1) {
1582 + guest_id = *((uint32_t *) in);
1583 + *((uint32_t *) ctrl_msg) = *((uint32_t *) in);
1584 + } else {
1585 + if (guest_id != *((uint32_t *) in) ) {
1586 + printf("WARNING: More than one guest attached\n");
1587 + }
1588 + }
1590 + if (vtpm_tx_fh < 0) {
1591 +#ifdef VTPM_MUTLI_VM
1592 + vtpm_tx_fh = open(DEV_BE, O_RDWR);
1593 + vtpm_rx_fh = vtpm_tx_fh;
1594 +#else
1595 + vtpm_tx_fh = open(GUEST_TX_FIFO, O_WRONLY);
1596 +#endif
1597 + }
1599 + if (vtpm_tx_fh < 0) {
1600 + printf("ERROR: failed to open devices to respond to guest.\n");
1601 + return -1;
1602 + }
1604 + // Handle command, but we need to skip the identifier
1605 + if ( BE16_TO_CPU( ((uint16_t *) in)[2] ) == VTPM_TAG_REQ ) { // Control message from xend
1606 + // This DM doesn't really care about ctrl messages. Just ACK the message
1607 + written = write(vtpm_tx_fh, ctrl_msg, sizeof(ctrl_msg));
1609 + if (written != sizeof(ctrl_msg)) {
1610 + printf("ERROR: Part of response not written %d/%Zu.\n", written, sizeof(ctrl_msg));
1611 + } else {
1612 + printf("Send Ctrl Message confermation\n");
1613 + }
1614 + } else { // Message from Guest
1615 + if (tpm_handle_command(in + sizeof(uint32_t), in_size - sizeof(uint32_t), &out, &out_size) != 0) {
1616 + printf("ERROR: Handler Failed.\n");
1617 + }
1619 + addressed_out = (uint8_t *) tpm_malloc(sizeof(uint32_t) + out_size);
1620 + *(uint32_t *) addressed_out = *(uint32_t *) in;
1621 + memcpy(addressed_out + sizeof(uint32_t), out, out_size);
1623 + written = write(vtpm_tx_fh, addressed_out, out_size + sizeof(uint32_t));
1625 + if (written != out_size + sizeof(uint32_t)) {
1626 + printf("ERROR: Part of response not written %d/%d.\n", written, out_size);
1627 + for (i=0; i< out_size+ sizeof(uint32_t); i++)
1628 + printf("%x ", addressed_out[i]);
1629 + printf("\n");
1630 + } else {
1631 + printf("Sent[%Zu]: ", out_size + sizeof(uint32_t));
1632 + for (i=0; i< out_size+ sizeof(uint32_t); i++)
1633 + printf("%x ", addressed_out[i]);
1634 + printf("\n");
1635 + }
1636 + tpm_free(out);
1637 + tpm_free(addressed_out);
1638 + }
1640 + } // loop
1642 + tpm_emulator_shutdown();
1644 + close(vtpm_tx_fh);
1645 +#ifndef VTPM_MUTLI_VM
1646 + close(vtpm_rx_fh);
1647 + free (guest_rx_file);
1648 +#endif
1650 +}