#
# The Initial Developer of the Original Code is International
# Business Machines Corporation. Portions created by IBM
# Corporation are Copyright (C) 2007 International Business
# Machines Corporation. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the Common Public License as published by
# IBM Corporation; either version 1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# Common Public License for more details.
#
# You should have received a copy of the Common Public License
# along with this program; if not, a copy can be viewed at
# http://www.opensource.org/licenses/cpl1.0.php.
#

OBJ=tpm_createkey tpm_quote tpm_pcrread tpm_extend tpm_unsealdata iml

all: $(OBJ)


hex2bin.o: hex2bin.c
	gcc -Wall -c hex2bin.c

tpm_createkey:tpm_createkey.c hex2bin.o 
	gcc -Wall -o tpm_createkey hex2bin.o -ltspi tpm_createkey.c

tpm_quote:tpm_quote.c hex2bin.o
	gcc -Wall -o tpm_quote hex2bin.o -ltspi tpm_quote.c

tpm_pcrread:tpm_pcrread.c hex2bin.o
	gcc -Wall -o tpm_pcrread hex2bin.o -ltspi tpm_pcrread.c

tpm_extend:tpm_extend.c hex2bin.o
	gcc -Wall -o tpm_extend hex2bin.o -ltspi tpm_extend.c

tpm_unsealdata: tpm_unsealdata.c
	gcc -Wall -o tpm_unsealdata  -ltspi -ltpm_unseal tpm_unsealdata.c

iml: iml.c
	gcc -Wall -o iml  -ltspi iml.c

install:
	mkdir -p $(DESTDIR)/usr/bin
	install tpm_createkey 	$(DESTDIR)/usr/bin/tpm_createkey 	
	install tpm_quote 	    $(DESTDIR)/usr/bin/tpm_quote 	
	install tpm_pcrread 	$(DESTDIR)/usr/bin/tpm_pcrread 	
	install -m 4755 tpm_extend 		$(DESTDIR)/usr/bin/tpm_extend 	
	install tpm_unsealdata  $(DESTDIR)/usr/bin/tpm_unsealdata 	
	install -m 4755 iml 	$(DESTDIR)/usr/bin/iml 	

clean:
	rm -f $(OBJ)
	rm -f *.o
	rm -f *~

#
# TEST
#

UUID1=00000000000000000000000000000078
UUID2=1448c61c6af7770fc4a3b413c1094e35
UUID3=00000000000000000000000000000555
UUID4=00000000000000000000000000009999
NONCE1=83c5cacb20a590e5b48980598cc69f18068495de

PASSWORD=hogehoge

test001:
	./tpm_pcrread -p 0

test101:
	@echo "Create Key without auth"
	@echo " update -> test111)"
	@echo " quote  -> test201)"
	./tpm_createkey --noauth -u $(UUID1)

test102:
	@echo "Create Key with auth(popup)"
	@echo "   try this password $(PASSWORD)"
	@echo " update -> test112)"
	@echo " quote  -> test202)"
	@echo " quote  -> test202b)"
	./tpm_createkey -u $(UUID2)

test103:
	@echo "Create Key with auth, popup (test112,test202)"
	@echo "   password $(PASSWORD)"
	@echo " update -> test113)"
	@echo " quote  -> test203)"
	@echo " quote  -> test203b)"
	./tpm_createkey --password $(PASSWORD)-u $(UUID3)

test104:
	@echo "Create Key with auth, cmdline"
	@echo "   password $(PASSWORD)"
	@echo " update -> test114)"
	@echo " quote  -> test204)"
	@echo " quote  -> test204b)"
	./tpm_createkey -u $(UUID4)


test111:
	@echo "Create Key without auth, UPDATE"
	./tpm_createkey --noauth --force -u $(UUID1)

test112:
	@echo "Create Key with auth(popup), UPDATE"
	@echo "   try this password $(PASSWORD)"
	./tpm_createkey --popup --force -u $(UUID2)

test113:
	@echo "Create Key with auth, UPDATE"
	@echo "   password $(PASSWORD)"
	./tpm_createkey --auth $(PASSWORD) --force -u $(UUID3)

test114:
	@echo "Create Key with auth, cmdline, UPDATE"
	@echo "   password $(PASSWORD)"
	@echo " quote  -> test204)"
	@echo " quote  -> test204b)"
	./tpm_createkey -u $(UUID4)


test120:
	@echo "List Keys"
	./tpm_createkey --list


test201:
	@echo "Quote without key auth"
	./tpm_quote --noauth -u $(UUID1) -p 0 -p 2 -p 4 -p 8 -n $(NONCE1)

test202:
	@echo "Quote with key auth"
	@echo "   password $(PASSWORD)"
	./tpm_quote --popup -u $(UUID2) -p 0 -p 2 -p 4 -p 8 -n $(NONCE1)

test202b:
	@echo "Quote with key auth $(UUID3)"
	@echo "   password $(PASSWORD)"
	./tpm_quote --popup -u $(UUID3) -p 0 -p 2 -p 4 -p 8 -n $(NONCE1)

test203:
	@echo "Quote with key auth"
	@echo "   password $(PASSWORD)"
	./tpm_quote --auth  $(PASSWORD) -u $(UUID3) -p 0 -p 2 -p 4 -p 8 -n $(NONCE1)

test203b:
	@echo "Quote with key auth, $(UUID2)"
	@echo "   password $(PASSWORD)"
	./tpm_quote --auth  $(PASSWORD) -u $(UUID2) -p 0 -p 2 -p 4 -p 8 -n $(NONCE1)

test204:
	@echo "Quote with key auth"
	@echo "   password $(PASSWORD)"
	./tpm_quote  -u $(UUID4) -p 0 -p 2 -p 4 -p 8 -n $(NONCE1)

test204b:
	@echo "Quote with key auth, $(UUID2)"
	@echo "   password $(PASSWORD)"
	./tpm_quote  -u $(UUID2) -p 0 -p 2 -p 4 -p 8 -n $(NONCE1)

#test202:
#	./tpm_quote -u $(UUID1) -p 0 -n $(NONCE1) > quote0.properties
#
#test203:
#	./tpm_quote -u $(UUID1) -p 0 -p 2 -p 4 -p 8 -p 10 -n $(NONCE1) > quote16.properties
#
#test204:
#	./tpm_quote -u $(UUID1) -p 0 -p 2 -p 4 -p 8 -p 10 -p 17 -p 18 -n $(NONCE1) > quote24.properties

test301:
	@echo "Seal/Unseal README"
	tpm_sealdata -i README -o README.sealed
	./tpm_unsealdata -i README.sealed -o README.plain
	diff README README.plain

test302:
	@echo "Seal/Unseal w/ PCR10  README"
	./tpm_pcrread -p 10
	tpm_sealdata -p 10 -i README -o README.sealed
	./tpm_unsealdata -i README.sealed -o README.plain
	diff README README.plain

test303:
	@echo "run some new command"
	./tpm_unsealdata -i README.sealed -o README.plain
	diff README README.plain

#
# test4**, test for IML
#
test401:
	./iml -l -o test401out.txt
test402:
	./iml -b -o test402out.bin




	 
# EOF