#
# Change the CFLAGS and MANDIR and BINDIR to suit your machine.
#
# There are commented compilation lines for various machines.
# Just look for the first set that matches your machine, and uncomment them.
#
# Type "make" to build and test.
# Type "make -n install" to see how to install the code
#
# Link without shared libraries because they make it possible to create trojan
# horse library calls at run-time.
#
# You don't need to use -D__BSD for Berkley systems unless you have
# a non ANSI/POSIX compliant system (pretty rare these days: 28-Mar-94)
#
# Tested using vendor's "cc" on the following machines:  (03-Apr-94)
#    HP-UX  B.09.00 B 9000/370
#    HP-UX  A.09.01 A 9000/735
#    ULTRIX 4.3 1     RISC
#    ULTRIX 4.3 0     RISC
#    OSF1   V2.0 240  alpha
#    SunOS  4.1.1 1   sun4c
#    SunOS  4.1.3 1   sun4c
#    hermes 4.0 3.0   i386 386/AT
#    Mach   3.0       Next (MC68040/25Mhz)
#    ULTRIX 4.0 0     VAX
#
# Thanks to Greg Pratt(glp5941@ultb.isc.rit.edu) for testing on several of 
# the machines above.  Thanks to John Sechrest (sechrest@cs.orst.EDU) for
# testing on the Next.
#
# The MD5 checksum is available for the tar file of this distribution.  
# Send mail to the author. (barrett@asgard.cs.Colorado.EDU)
#
# Copyright 1994 by David A. Barrett
#
#    This program is not to be distributed for profit or included in such 
#    software without written permission from the author (this means 
#    you, TVAnswer).  No permission is required for non-profit use.
#
# Don't change the next 5 lines unless you are running MS-DOS.
O=o
LN=ln
RM=rm
EXE=
LD=$(CC) $(LDFLAGS) -o $@
# space past -o; SunOS and DEC require it, MSDOS prohibits it.

# uncomment the next few lines if you are running MS-DOS
#
# begin Turbo C++ 3.1 portion.  Worked. (also atob and btoa) D.A.B. (9-Sep-93)
# Note: Pipe commands fail to work correctly on MSDOS ignore those errors
#       Also, 286 instructions cause a subroutine call for 32-bit shift.
#O=obj
#EXE=.exe
#CC=bcc
#LN=copy
#RM=del
#CFLAGS=-O2 -I. -D__msdos -3 -a
#GETOPT=getopt.$(O)
#LD=$(CC) $(LDFLAGS) -o$@ 
# -a = align words
# -2 = generate 286 instructions (which still suck!, need 386 code generation)
# -3 = generate 386 instructions (doubles execution speed)
# end of Turbo C++ 3.1 portion

CMP=./cmp$(EXE)

#default C compiler
CC=cc
CFLAGS=-O
LDFLAGS=-O
#CFLAGS=-g
#LDFLAGS=-g

#SPARC SunOs cc compiler
#CC=cc
#CFLAGS=-Bstatic -O2
#LDFLAGS=-Bstatic -O2

#SPARC SunOs Wspro C++ compiler
#CC=CC
#CFLAGS=-Bstatic -O4
#LDFLAGS=-Bstatic -O4

#DEC OSF1 alpha C compiler (also works for Ultrix on DEC MIPS)
#CC=cc
#CFLAGS=-non_shared -O2 -std
#LDFLAGS=-non_shared -O2 -std

#DEC OSF1 alpha C++ compiler 
#CC=cxx
#CFLAGS=-non_shared -D__LANGUAGE_C__ -O2
#LDFLAGS=-non_shared -O2

#HP-UX ANSI C compiler
#CC=cc
#CFLAGS=-O -Aa
#LDFLAGS=$(CFLAGS) -Wl,-a,archive

#HP-UX C++ compiler
#CC=CC
#CFLAGS=-O -Aa -D_HPUX_SOURCE		# _HPUX_SOURCE only for getkey.c 
#LDFLAGS=$(CFLAGS) -Wl,-a,archive

#gnu C compiler (make sure your version doesn't use shared libraries)
# Use the more strict compliation options only of you want to see all
# warnings.  The code works either way.
#CC=gcc 		# -ansi -pedantic	
#CFLAGS=-O2 		# -Wall
#LDFLAGS=-O2

ROOT=/usr/local
MANDIR=$(ROOT)/man/man1
BINDIR=$(ROOT)/bin
#BINDIR=$(ROOT)/`arch`/bin

# no changes needed below this line

# atob and btoa are modified copies from the compress4.0 distribution
AUXBINS=atob$(EXE) btoa$(EXE)

# need getopt.c cmp.c and unistd.h only for MSDOS
AUXSRC=atob.c btoa.1 btoa.c getopt.c cmp.c unistd.h
BINSRC=testdata.des testin.cfb testout.cfb testin.bta testout.bta testin testout
HDR=cipher.h des.h desdefs.h
SRC=README README.linux Makefile $(HDR) $(BINSRC) \
	cipher.c descfb.c deskey.c desblock.c sboxp.c getkey.c \
	destest.c desvalid.c testcfb.c \
	cipher.1 cmail\
	$(AUXSRC)

DESOBJS=desblock.$(O) sboxp.$(O)
BINS=cipher$(EXE)
PCBINS=atob.exe btoa.exe cipher.exe
TESTBINS=destest$(EXE) desvalid$(EXE) cipher$(EXE) decipher$(EXE) testcfb$(EXE) $(CMP)

all:		$(BINS) test $(AUXBINS)

cipher$(EXE):	cipher.$(O) getkey.$(O) deskey.$(O) $(DESOBJS) descfb.$(O) $(GETOPT)
#		$(LD) cipher.c getkey.c deskey.c $(DESOBJS) descfb.c
#		pixie cipher
#		./cipher.pixie -k test <cipher >/dev/null
#		prof -feedback cipher.feedback -pixie cipher >prof.cipher1
#		mv cipher cipher.O3
#		$(LD) -feedback cipher.feedback cipher.c getkey.c deskey.c $(DESOBJS) descfb.c
#		pixie cipher
#		./cipher.pixie -k test </tmp/x >/dev/null
#		prof -pixie cipher >prof.feedback
		$(LD) cipher.$(O) getkey.$(O) deskey.$(O) \
		$(DESOBJS) descfb.$(O) $(GETOPT)

decipher$(EXE):	cipher$(EXE)
		-$(RM) decipher$(EXE)
		$(LN) cipher$(EXE) decipher$(EXE)

desvalid$(EXE):	desvalid.$(O) $(DESOBJS) $(GETOPT)
		$(LD) desvalid.$(O) $(DESOBJS) $(GETOPT)

destest$(EXE):	destest.$(O) $(DESOBJS) $(GETOPT)
		$(LD) destest.$(O) $(DESOBJS) $(GETOPT)

testcfb$(EXE):	testcfb.$(O) descfb.$(O) $(DESOBJS) $(GETOPT)
		$(LD) testcfb.$(O) descfb.$(O) $(DESOBJS) $(GETOPT)

atob$(EXE):	atob.$(O)
		$(LD) atob.$(O)

btoa$(EXE):	btoa.$(O)
		$(LD) btoa.$(O)

despass$(EXE):	despass.$(O) desblock.$(O) sboxp.$(O)
		$(LD) desbass.$(O) desblock.$(O) sboxp.$(O)

uxpass$(EXE):	uxpass.$(O)
		$(LD) uxpass.$(O)

cmp$(EXE):	cmp.$(O)
		$(LD) cmp.$(O)


cipher.$(O):	cipher.h des.h

cipher.man:	cipher.1
		nroff -man <cipher.1 | col -b >cipher.man

despass.$(O):	cipher.h des.h
descfb.$(O):	des.h
deskey.$(O):	des.h
sboxp.$(O):	desdefs.h

desblock.$(O):	desblock.c
		$(CC) $(CFLAGS) -c desblock.c

time:		cipher$(EXE)
		time cipher -i00 -x00 <testin3 >x

gprof: 		time
		gprof -b cipher >gprof.out

test:		$(BINS) $(TESTBINS) $(AUXBINS) testdata.des testin.cfb $(CMP)
		@echo des subroutine Tests
		./desvalid -v < testdata.des
		./destest -v
		@echo desCFB subroutine Tests with varying block sizes
		./testcfb -s 1 <testin.cfb >x 
		$(CMP) x testout.cfb
		./testcfb -s 2 <testin.cfb >x 
		$(CMP) x testout.cfb
		./testcfb -s 3 <testin.cfb >x 
		$(CMP) x testout.cfb
		./testcfb -s 4 <testin.cfb >x 
		$(CMP) x testout.cfb
		./testcfb -s 5 <testin.cfb >x 
		$(CMP) x testout.cfb
		./testcfb -s 9 <testin.cfb >x 
		$(CMP) x testout.cfb
		./testcfb -s 15 <testin.cfb >x 
		$(CMP) x testout.cfb
		./testcfb -s 17 <testin.cfb >x 
		$(CMP) x testout.cfb
		@$(RM) x
		@echo cipher program Tests -- single encipherment
		./cipher -x0123456789abcdef -i1234567890abcdef <testin.cfb >x
		$(CMP) testout.cfb x
		./cipher -d -x0123456789abcdef -i1234567890abcdef <x >y
		$(CMP) testin.cfb y
		./cipher -k bigtestkey <testin >x 
		$(CMP) testout x
		./cipher -d -k bigtestkey <x >y 
		$(CMP) testin y
		@echo Multiple encipherment: if MS-DOS ignore pipes failing
		./decipher -k test <testin | ./cipher -k test >x 
		$(CMP) x testin
		./cipher -k "phartles_and" -k "jaymes_beer" <testin >x
		./cipher -d -k "jaymes_beer" -d -k "phartles_and" <x >y
		$(CMP) testin y
		./decipher -e -k "phartles_and" <testin | ./cipher -k "jaymes_beer" >z
		$(CMP) x z
		@echo Mixed triple encryption
		./cipher -e -k k1 -d -k k2 -e -k k3 <testin >x
		./cipher -d -k k3 -e -k k2 -d -k k1 <x >y 
		$(CMP) testin y
		./decipher -k k3 <x | ./cipher -k k2 | ./decipher -k k1 >z
		$(CMP) testin z
		@echo "Expect 16 weak key warnings:"
		@./cipher -xe001e001f101f101 <testin >x
		@./cipher -x01e001e001f101f1 <testin >x
		@./cipher -xfe1ffe1ffe0efe0e <testin >x
		@./cipher -x1ffe1ffe0efe0efe <testin >x
		@./cipher -xe01fe01ff10ef10e <testin >x
		@./cipher -x1fe01fe00ef10ef1 <testin >x
		@./cipher -x01fe01fe01fe01fe <testin >x
		@./cipher -xfe01fe01fe01fe01 <testin >x
		@./cipher -x011f011f010e010e <testin >x
		@./cipher -x1f011f010e010e01 <testin >x
		@./cipher -xe0fee0fef1fef1fe <testin >x
		@./cipher -xfee0fee0fef1fef1 <testin >x
		@./cipher -x0101010101010101 <testin >x
		@./cipher -xfefefefefefefefe <testin >x
		@./cipher -xe0e0e0e0f1f1f1f1 <testin >x
		@./cipher -x1f1f1f1f0e0e0e0e <testin >x
		./btoa <testin.bta >x
		$(CMP) testout.bta x
		./atob <x >y
		$(CMP) testin.bta y
		@$(RM) x 
		@$(RM) y 
		@$(RM) z

install:	$(BINS) cipher.1 $(AUXBINS)
		cp cipher$(EXE) $(BINDIR)
		-$(LN) $(BINDIR)/cipher$(EXE) $(BINDIR)/decipher$(EXE)
		cp cipher.1 $(MANDIR)
		cp btoa$(EXE) atob$(EXE) $(BINDIR)
		cp btoa.1 $(MANDIR)
		-$(LN) $(MANDIR)/btoa.1 $(MANDIR)/atob.1

tar:		$(SRC) $(PCBINS)
		tar -cvf - $(SRC) $(PCBINS) | compress >cipher.tar.Z

shar:		$(SRC)
		shar $(SRC) >cipher.shar

arc:		cipher.lzh

cipher.lzh:	$(SRC)
		lharc c cipher.lzh $(SRC)

clean:
	rm -f cipher.$(O) desvalid.$(O) destest.$(O) testcfb.$(O) 
	rm -f desblock.s $(DESOBJS) getkey.$(O) descfb.$(O) deskey.$(O) 
	rm -f cmp.$(O) $(GETOPT)
	rm -f btoa.$(O) atob.$(O)
	rm -f despass.$(O) uxpass.$(O)
	rm -f x y z
	rm -f $(CMP)

clobber: 	clean
	rm -f $(BINS) $(TESTBINS) $(AUXBINS)
	rm -f despass$(EXE) uxpass$(EXE)
