#
# Makefile for fast DES and UNIX password implementation
#
# entry points:
# too numerous to mention, and most of them are internal anyway; the
# more useful ones are:
#	make install
#		to make and then install everything
#	make lib
#		to make the library
#	make clean
#		to delete .o files
#	make clobber
#		to delete .o files, libraries, and executables
#
# Author:
#	Matt Bishop
#	Research Institute for Advanced Computer Science
#	NASA Ames Research Center
#	Moffett Field, CA  94035
#
#	mab@riacs.edu, ...!{ames,decvax!decwrl}!riacs!mab
#
# Copyright:
#	(c) 1988 by the Research Institute for Advanced Computer
#		Science and Matt Bishop
#
# parameters; these will change from system to system
# ********** WARNING: if made from the top level make, these
# **********          will be reset to the values in that Makefile
# **********          or as dictated by that command line
# LIBDIR	is where the library "libdes.a" goes
# INCDIR	is where the header file "des.h" goes
# DES, CRY, SHC	are the code numbers of the encryption and password
#		algorithms; the first two digits indicate the number
#		of bits passed to the combined spe function (06 and
#		12 are known; YOU MUST PUT THE ZERO BEFORE THE 6!)
#		and the third indicates the number of permutations
#		(1 or 3) to use to construct the key.
LIBDIR	= ../lib
INCDIR	= ../include


# programs invoked here
# AR		library archiver
# CP		copy file
# CPP		C preprocessor
# ECHO		print arguments
# LORDER	print order of loader files
# M4		macro preprocessor
# MV		move file
# RANLIB	make entry point table for library
# RM		remove file
# SED		stream editor
# TSORT		topological sort
AR	= ar
CP	= cp
CPP	= /lib/cpp
ECHO	= echo
LORDER	= lorder
M4	= m4
MV	= mv
RANLIB	= ranlib
RM	= rm
SED	= sed
TSORT	= tsort
# makefile variables; these control how make works
# SHELL		the shell used to run the subcommands
SHELL	= /bin/sh
# flags and files
# CFLAGS	options to compile anything
# C0FLAGS       options to compile the straight DES routines
# C1FLAGS	options to compile the fast UNIX password encryption routine
# C2FLAGS	options to compile the fast password encryption routine
COPTS	= 
CFLAGS	= ${COPTS} -I../include 
C0FLAGS = ${CFLAGS} -DDES -O
C1FLAGS = ${CFLAGS} -DCRYPT -O -h vmesg_1
C2FLAGS	= ${CFLAGS} -DSHORTCRYPT -O -h vmesg_1 -h ivdep

SYSTYPE = SYSV

STDCRYPTLIB=
TCFLAGS = -O -D$(SYSTYPE) -I../include ${COPTS}

# header, source, and object files
# BASEINC	header files all mapping configurations include
# BOXES		labels to generate boxes for the desired configuration
# HDRBOX	sources for the lookup tables
# MAPS		objects for the maps for the desired configuration
# OBJBOX	objects for the lookup tables
# OBJECT	object files for the UNIX wrapping programs
# OBJMAP	object files for the encryption programs
# SRCBOX	sources for the lookup tables
BASEINC	= des.h ../include/mach.h box_ipe.h box_eipinv.h
BOXES	= box${SHC}
HDRBOX	= box_key.h box_pc1.h box_lsh.h box_pc2.h \
		box_ipe.h box_spe6.h box_spe12.h box_eipinv.h
MAPS	= sdes${SHC}.o
# MAPS	= sdes${SHC}.o
OBJBOX	= box_key.o box_pc1.o box_lsh.o box_pc2.o \
		box_ipe.o box_spe6.o box_spe12.o box_eipinv.o
OBJECT	= shortcrypt.o 
OBJMAP	= sdes061.o 
SRCBOX	= box_key.c box_pc1.c box_lsh.c box_pc2.c \
		box_ipe.c box_spe12.c box_eipinv.c

# here come the dependencies
box061:	box_key.o box_ipe.o box_spe6.o box_eipinv.o
sdes061.o:	${BASEINC} box_key.h box_spe6.h

${OBJBOX}:	genbox des.h ../include/mach.h

# install the cookies
install:	lib
	${MV} libdes.a ${LIBDIR}/libdes.a
	-${RANLIB} ${LIBDIR}/libdes.a
	${CP} des.h ../include/mach.h ${INCDIR}
	
# bake the cookies
lib:	${MAPS} ${OBJECT} ${BOXES} libvers.o
	${RM} -f libdes.a
	${AR} rcv libdes.a `${LORDER} ${MAPS} ${OBJECT} box_*.o libvers.o | ${TSORT}`
	-${RANLIB} libdes.a


# make the mappings with the desired configurations

sdes${SHC}.o:
	PFL=`${ECHO} ${SHC} | ${SED} 's/\(..\)\(.\)/-DKEY=\2 -DPATH=\1/'`; \
		${CPP} -P ${CFLAGS} $$PFL -DWHICH=shc < csdes.m4 | \
					${M4} - > sdes.c; \
		${CC} $$PFL ${C2FLAGS} -c sdes.c; \
		${MV} sdes.o sdes${SHC}.o; \
		${RM} -f sdes.c

libvers.o: libvers.m4 ../include/mach.h
	${CPP} -P -I../include libvers.m4 | ${M4} - > libvers.c
	${CC} -c libvers.c
	${RM} -f libvers.c

# make the program to make the tables
genbox: genbox.c ../include/mach.h des.h
	PFL=`${ECHO} ${DES} | ${SED} 's/\(..\)\(.\)/-DKEY=\2 -DPATH=\1/'`; \
		${CC} $$PFL ${CFLAGS} -O genbox.c -o genbox

# make the tables
${HDRBOX} ${SRCBOX}:	genbox
	./genbox $@

box_spe6.c: genbox
	genbox box_spe6.c
	${CC} -c -I../include box_spe6.c

genpwd: genpwd.o
	$(CC) $(TCFLAGS) -o genpwd genpwd.o $(STDCRYPTLIB)

timecrypt: timecrypt.o $(LIB)
	$(CC) $(TCFLAGS) -o timecrypt timecrypt.o $(LIB)

timecrypt.o: timecrypt.c
	${CC} ${TCFLAGS} -c timecrypt.c


# clean up
clean:
	${RM} -f ${OBJECT} ${OBJBOX} ${OBJMAP}

clobber:
	${RM} -f ${OBJECT} ${OBJBOX} ${SRCBOX} ${HDRBOX} ${OBJMAP} ${SPEBOX} \
		${SPEOBJ} a.out core genbox genbox.o libvers.o *.a *~
