# $Id$
#
# Makefile for low-level crypto library

CC=@CC@
CPP=@CPP@
RANLIB=@RANLIB@
AR=ar

# Reset VPATH
SRCDIR=@srcdir@
VPATH=$(SRCDIR):$(SRCDIR)/../include

CRYPTO_INCLUDES=$(SRCDIR)/../include

# Additional C preprocessor flags
PREFLAGS=$(EXTRA_CPP_FLAGS) -I$(CRYPTO_INCLUDES) -I. -I$(SRCDIR) $(DEFINES)

# The flags to generate a shared library
CFLAGS=$(PREFLAGS) $(OTHERFLAGS) $(EXTRA_CFLAGS)
LDFLAGS=@LDFLAGS@

default: algorithms.a

### Magic Makefile for descore

# Interesting defines are sparc, mc68000, vax and i386
# Rely on gcc defining them appropriately.
# CPPFLAGS=	-Dsparc		# use 6+8 general regs
# CPPFLAGS=	-Dmc68000	# use 3+4 addr (1+4 live), and 3+3 data regs
# CPPFLAGS=	-Dvax		# use 6+0 general regs
# CPPFLAGS=	-Di386		# use 3+0 regs, and 3+0 normal variables

CODEGEN.c=	$(CC) $(CFLAGS) $(CPPFLAGS) -S
COMPILE.c=	$(CC) $(CFLAGS) $(CPPFLAGS) -c
LINK.c=		$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)

# hand-entered files that go into the library
SC=		desKerb.c desUtil.c desQuick.c
SO=		desKerb.o desUtil.o desQuick.o

# special generated files
GH=		parity.h rotors.h keymap.h
GC=		desSmallFips.c desSmallCore.c desQuickFips.c desQuickCore.c
GI=		desSmallFips.i desSmallCore.i desQuickFips.i desQuickCore.i
GO=		desSmallFips.o desSmallCore.o desQuickFips.o desQuickCore.o

O=		$(SO) $(GO)

# prefer compilation from .i if .i exists
#.SUFFIXES:
#.SUFFIXES:	.i .o .c $(SUFFIXES)

desTest:	desCore.a desTest.o
		$(LINK.c) -o $@ desTest.o desCore.a
		./desTest

# test all performance enhancement flags
sure:
		make clean ; make desTest 'CPPFLAGS=-Di386    -Umc68000 -Usparc'
		make clean ; make desTest 'CPPFLAGS=-Dvax     -Umc68000 -Usparc'
		make clean ; make desTest 'CPPFLAGS=-Dmc68000 -Usparc'
		make clean ; make desTest 'CPPFLAGS=-Dsparc   -Umc68000'
		make clean ; make desTest

clean:
	-rm -f *.o *.i *.x *.a ./desTest
	-rm -f $(GC) $(GH) desdata

depend:

desCore.a:	$O
		$(AR) cru $@ $O
		$(RANLIB) $@

desdata.o:	desinfo.h $(SRCDIR)/../include/des.h
desUtil.o:	$(GH)
$(SO):		$(SRCDIR)/../include/des.h
$(GI):		desCode.h $(SRCDIR)/../include/des.h Makefile
desTest.o:	$(SRCDIR)/../include/des.h

$(GH):		desdata
		./desdata $@ > $@

desdata:	desdata.o
		$(LINK.c) -o $@ desdata.o

# new rules  (note: tr|sed|tr is NOT necessary,  just there so .i is readable)
.c.i:
	$(CPP) $(CFLAGS) $(CPPFLAGS) $< > $*.x
	@tr ';'\\012 \\012';' < $*.x |			\
	 sed	-e 's/[ 	][ 	]*/ /g'		\
		-e 's/^ //'				\
		-e 's/ $$//'				\
		-e '/^$$/d'				\
		-e '/^[^;]/s/^/;/'			\
		-e 's/#[^;]*;//g'			\
		-e 's/\([){]\) *\(register\)/\1;\2/g'	\
		-e 's/\([[(]\) /\1/g'			\
		-e 's/ \([])]\)/\1/g'			\
		-e 's/\([^]+0123 ]\) =/\1  =/g'		\
		-e 's/}/};;/g'				\
		-e 's/ *; */;/g'			\
		-e 's/;;;*/;;/g'			\
		-e '1s/^;*//' |				\
	 tr ';'\\012 \\012';' > $@
	@echo ""  >> $@
#	@echo "}" >> $@			# last definition must be a procedure

#		-e 's/\(;[kmxyz][0-9]*\)\([^;]*=\)/\1  \2/g'

.i.o:
		$(CODEGEN.c) $<
		$(COMPILE.c) $*.s

# slowest to quickest
desSmallFips.c:
		@echo '#include "desCode.h"' > $@
		@echo \
'ENCRYPT(DesSmallFipsEncrypt,TEMPSMALL,LOADFIPS,KEYMAPSMALL,SAVEFIPS)' >> $@
		@echo \
'DECRYPT(DesSmallFipsDecrypt,TEMPSMALL,LOADFIPS,KEYMAPSMALL,SAVEFIPS)' >> $@
desSmallCore.c:
		@echo '#include "desCode.h"' > $@
		@echo \
'ENCRYPT(DesSmallCoreEncrypt,TEMPSMALL,LOADCORE,KEYMAPSMALL,SAVECORE)' >> $@
		@echo \
'DECRYPT(DesSmallCoreDecrypt,TEMPSMALL,LOADCORE,KEYMAPSMALL,SAVECORE)' >> $@
desQuickFips.c:
		@echo '#include "desCode.h"' > $@
		@echo \
'ENCRYPT(DesQuickFipsEncrypt,TEMPQUICK,LOADFIPS,KEYMAPQUICK,SAVEFIPS)' >> $@
		@echo \
'DECRYPT(DesQuickFipsDecrypt,TEMPQUICK,LOADFIPS,KEYMAPQUICK,SAVEFIPS)' >> $@
desQuickCore.c:
		@echo '#include "desCode.h"' > $@
		@echo \
'ENCRYPT(DesQuickCoreEncrypt,TEMPQUICK,LOADCORE,KEYMAPQUICK,SAVECORE)' >> $@
		@echo \
'DECRYPT(DesQuickCoreDecrypt,TEMPQUICK,LOADCORE,KEYMAPQUICK,SAVECORE)' >> $@

### End of rules for desCore

# BEGIN NATIONAL SECURITY
MASS_DESTRUCTION_OBJS = idea.o rc4.o cast.o $(O)
# END NATIONAL SECURITY

OBJS = $(MASS_DESTRUCTION_OBJS) sha.o md5.o

algorithms.a: $(OBJS)
	rm -f algorithms.a
	$(AR) cru algorithms.a $(OBJS)
	$(RANLIB) algorithms.a

Makefile: $(SRCDIR)/Makefile.in config.status
	CONFIG_FILES=Makefile CONFIG_HEADERS="" ./config.status
	@echo "Run make again"
	@exit 1

