You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

331 lines
13 KiB

  1. # Copyright (c) 2014 Cryptography Research, Inc.
  2. # Released under the MIT License. See LICENSE.txt for license information.
  3. UNAME := $(shell uname)
  4. MACHINE := $(shell uname -m)
  5. # Subdirectories for objects etc.
  6. # Many of them are mapped to build/obj right now, but could be split later.
  7. # The non-build/obj directories are the public interface.
  8. BUILD_OBJ = build/obj
  9. BUILD_C = build/c
  10. BUILD_H = build/c
  11. BUILD_PY = build/obj
  12. BUILD_LIB = build/lib
  13. BUILD_INC = build/include
  14. BUILD_BIN = build/bin
  15. BUILD_IBIN = build/obj/bin
  16. BATBASE=ed448goldilocks_decaf_bats_$(TODAY)
  17. BATNAME=build/$(BATBASE)
  18. ifeq ($(UNAME),Darwin)
  19. CC = clang
  20. CXX = clang++
  21. else
  22. CC = gcc
  23. CXX = g++
  24. endif
  25. LD = $(CC)
  26. LDXX = $(CXX)
  27. ASM ?= $(CC)
  28. WARNFLAGS = -pedantic -Wall -Wextra -Werror -Wunreachable-code \
  29. -Wmissing-declarations -Wunused-function -Wno-overlength-strings $(EXWARN)
  30. INCFLAGS = -Isrc/include -I$(BUILD_INC) -I$(BUILD_H)
  31. PUB_INCFLAGS = -I$(BUILD_INC)
  32. LANGFLAGS = -std=c99 -fno-strict-aliasing
  33. LANGXXFLAGS = -fno-strict-aliasing
  34. GENFLAGS = -ffunction-sections -fdata-sections -fvisibility=hidden -fomit-frame-pointer -fPIC
  35. OFLAGS ?= -O2
  36. MACOSX_VERSION_MIN ?= 10.9
  37. ifeq ($(UNAME),Darwin)
  38. GENFLAGS += -mmacosx-version-min=$(MACOSX_VERSION_MIN)
  39. endif
  40. TODAY = $(shell date "+%Y-%m-%d")
  41. #FIXME ARCHFLAGS
  42. ARCHFLAGS ?= -maes -mavx2 -mbmi2 #TODO
  43. ifeq ($(CC),clang)
  44. WARNFLAGS += -Wgcc-compat
  45. endif
  46. ARCHFLAGS += $(XARCHFLAGS)
  47. CFLAGS = $(LANGFLAGS) $(WARNFLAGS) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
  48. PUB_CFLAGS = $(LANGFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
  49. CXXFLAGS = $(LANGXXFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCXXFLAGS)
  50. LDFLAGS = $(XLDFLAGS)
  51. ASFLAGS = $(ARCHFLAGS) $(XASFLAGS)
  52. SAGE ?= sage
  53. SAGES= $(shell ls test/*.sage)
  54. BUILDPYS= $(SAGES:test/%.sage=$(BUILD_PY)/%.py)
  55. .PHONY: clean all test test_ct bench todo doc lib bat sage sagetest gen_headers
  56. .PRECIOUS: $(BUILD_C)/*/%.c $(BUILD_H)/*/%.h $(BUILD_IBIN)/%
  57. HEADER_SRCS= $(shell find src/public_include -name "*.h*")
  58. GEN_HEADERS_0= $(HEADER_SRCS:src/public_include/%=$(BUILD_INC)/%)
  59. GEN_HEADERS_1= $(GEN_HEADERS_0:%.tmpl.h=%.h)
  60. GEN_HEADERS= $(GEN_HEADERS_1:%.tmpl.hxx=%.hxx)
  61. HEADERS= Makefile $(shell find src test -name "*.h") $(BUILD_OBJ)/timestamp $(GEN_HEADERS)
  62. # components needed by the lib
  63. LIBCOMPONENTS = $(BUILD_OBJ)/utils.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o # and per-field components
  64. BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o
  65. all: lib $(BUILD_IBIN)/test $(BUILD_IBIN)/bench $(BUILD_BIN)/shakesum
  66. scan: clean
  67. scan-build --use-analyzer=`which clang` \
  68. -enable-checker deadcode -enable-checker llvm \
  69. -enable-checker osx -enable-checker security -enable-checker unix \
  70. make all
  71. # Internal test programs, which are not part of the final build/bin directory.
  72. $(BUILD_IBIN)/test: $(BUILD_OBJ)/test_decaf.o lib
  73. ifeq ($(UNAME),Darwin)
  74. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  75. else
  76. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  77. endif
  78. # Internal test programs, which are not part of the final build/bin directory.
  79. $(BUILD_IBIN)/test_ct: $(BUILD_OBJ)/test_ct.o lib
  80. ifeq ($(UNAME),Darwin)
  81. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  82. else
  83. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  84. endif
  85. $(BUILD_IBIN)/bench: $(BUILD_OBJ)/bench_decaf.o lib
  86. ifeq ($(UNAME),Darwin)
  87. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  88. else
  89. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  90. endif
  91. # Create all the build subdirectories
  92. $(BUILD_OBJ)/timestamp:
  93. mkdir -p $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \
  94. $(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_H) $(BUILD_INC)/decaf \
  95. $(PER_OBJ_DIRS)
  96. touch $@
  97. gen_headers: $(GEN_HEADERS)
  98. $(BUILD_INC)/%: src/public_include/% $(BUILD_OBJ)/timestamp
  99. cp -f $< $@
  100. $(BUILD_INC)/%.h: src/public_include/%.tmpl.h src/gen_headers/*
  101. python -B src/gen_headers/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
  102. $(BUILD_INC)/%.hxx: src/public_include/%.tmpl.hxx src/gen_headers/*
  103. python -B src/gen_headers/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
  104. ################################################################
  105. # Per-field code: call with field, arch
  106. ################################################################
  107. define define_field
  108. ARCH_FOR_$(1) ?= $(2)
  109. COMPONENTS_OF_$(1) = $$(BUILD_OBJ)/$(1)/f_impl.o $$(BUILD_OBJ)/$(1)/f_arithmetic.o $$(BUILD_OBJ)/$(1)/f_generic.o
  110. HEADERS_OF_$(1) = $(HEADERS) $$(BUILD_H)/$(1)/f_field.h
  111. LIBCOMPONENTS += $$(COMPONENTS_OF_$(1))
  112. PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
  113. $$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/gen_headers/* $(HEADERS)
  114. python -B src/gen_headers/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
  115. $$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/gen_headers/* $(HEADERS)
  116. python -B src/gen_headers/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
  117. $$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
  118. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
  119. -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
  120. -c -o $$@ $$<
  121. $$(BUILD_OBJ)/$(1)/%.o: src/$(1)/%.c $$(HEADERS_OF_$(1))
  122. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
  123. -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
  124. -c -o $$@ $$<
  125. $$(BUILD_OBJ)/$(1)/%.o: src/$(1)/$$(ARCH_FOR_$(1))/%.c $$(HEADERS_OF_$(1))
  126. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
  127. -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
  128. -c -o $$@ $$<
  129. endef
  130. ################################################################
  131. # Per-field, per-curve code: call with curve, field
  132. ################################################################
  133. define define_curve
  134. LIBCOMPONENTS += $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/elligator.o $$(BUILD_OBJ)/$(1)/scalar.o \
  135. $$(BUILD_OBJ)/$(1)/crypto.o $$(BUILD_OBJ)/$(1)/eddsa.o $$(BUILD_OBJ)/$(1)/decaf_tables.o
  136. PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
  137. GLOBAL_HEADERS_OF_$(1) = $(BUILD_INC)/decaf/decaf_$(3).h $(BUILD_INC)/decaf/decaf_$(3).hxx \
  138. $(BUILD_INC)/decaf/crypto_$(3).h $(BUILD_INC)/decaf/crypto_$(3).hxx \
  139. $(BUILD_INC)/decaf/eddsa_$(3).h $(BUILD_INC)/decaf/eddsa_$(3).hxx
  140. HEADERS_OF_$(1) = $$(HEADERS_OF_$(2)) $$(GLOBAL_HEADERS_OF_$(1))
  141. HEADERS += $$(GLOBAL_HEADERS_OF_$(1))
  142. $$(BUILD_C)/$(1)/%.c: src/per_curve/%.tmpl.c src/gen_headers/* $$(HEADERS_OF_$(2))
  143. python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
  144. $$(BUILD_H)/$(1)/%.h: src/per_curve/%.tmpl.h src/gen_headers/* $$(HEADERS_OF_$(2))
  145. python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
  146. $$(BUILD_INC)/decaf/decaf_$(3).%: src/per_curve/decaf.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
  147. python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  148. $$(BUILD_INC)/decaf/eddsa_$(3).%: src/per_curve/eddsa.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
  149. python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  150. $$(BUILD_INC)/decaf/elligator_$(3).%: src/per_curve/elligator.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
  151. python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  152. $$(BUILD_INC)/decaf/scalar_$(3).%: src/per_curve/scalar.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
  153. python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  154. $$(BUILD_INC)/decaf/crypto_$(3).%: src/per_curve/crypto.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
  155. python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  156. $$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \
  157. $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/scalar.o $$(BUILD_OBJ)/utils.o \
  158. $$(COMPONENTS_OF_$(2))
  159. $$(LD) $$(LDFLAGS) -o $$@ $$^
  160. $$(BUILD_C)/$(1)/decaf_tables.c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
  161. ./$$< > $$@ || (rm $$@; exit 1)
  162. $$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
  163. $$(CC) $$(CFLAGS) -c -o $$@ $$< \
  164. -I build/obj/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
  165. -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2))
  166. $$(BUILD_OBJ)/decaf_gen_tables_$(1).o: src/decaf_gen_tables.c $$(HEADERS_OF_$(1))
  167. $$(CC) $$(CFLAGS) \
  168. -I build/obj/curve_$(1) -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
  169. -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \
  170. -c -o $$@ $$<
  171. endef
  172. ################################################################
  173. # call code above to generate curves and fields
  174. $(eval $(call define_field,p25519,arch_x86_64))
  175. $(eval $(call define_curve,curve25519,p25519,255))
  176. $(eval $(call define_field,p448,arch_x86_64))
  177. $(eval $(call define_curve,ed448goldilocks,p448,448))
  178. # The shakesum utility is in the public bin directory.
  179. $(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/utils.o
  180. $(LD) $(LDFLAGS) -o $@ $^
  181. # The main decaf library, and its symlinks.
  182. lib: $(BUILD_LIB)/libdecaf.so
  183. $(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
  184. ln -sf `basename $^` $@
  185. $(BUILD_LIB)/libdecaf.so.1: $(LIBCOMPONENTS)
  186. rm -f $@
  187. ifeq ($(UNAME),Darwin)
  188. libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
  189. $(LIBCOMPONENTS)
  190. else
  191. $(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
  192. strip --discard-all $@
  193. endif
  194. $(BUILD_OBJ)/%.o: src/%.c $(HEADERS)
  195. $(CC) $(CFLAGS) -c -o $@ $<
  196. $(BUILD_OBJ)/%.o: test/%.c $(HEADERS)
  197. $(CC) $(PUB_CFLAGS) -c -o $@ $<
  198. $(BUILD_OBJ)/%.o: test/%.cxx $(HEADERS)
  199. $(CXX) $(CXXFLAGS) -c -o $@ $<
  200. # The sage test scripts
  201. sage: $(BUILDPYS)
  202. sagetest: sage lib
  203. $(SAGE) $(BUILD_PY)/test_decaf.sage
  204. $(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
  205. cp -f $(SAGES) $(BUILD_PY)/
  206. $(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
  207. # some sage versions compile to .sage.py
  208. for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
  209. if [ -e $$f.sage.py ]; then \
  210. mv $$f.sage.py $$f.py; \
  211. fi; \
  212. done
  213. # The documentation files
  214. $(BUILD_DOC)/timestamp:
  215. mkdir -p `dirname $@`
  216. touch $@
  217. #
  218. doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS)
  219. doxygen > /dev/null
  220. # # The eBATS benchmarking script
  221. # bat: $(BATNAME)
  222. #
  223. # $(BATNAME): include/* src/* src/*/* test/batarch.map $(BUILD_C)/decaf_tables.c # TODO tables some other way
  224. # rm -fr $@
  225. # for prim in dh sign; do \
  226. # targ="$@/crypto_$$prim/ed448goldilocks_decaf"; \
  227. # (while read arch where; do \
  228. # mkdir -p $$targ/`basename $$arch`; \
  229. # cp include/*.h $(BUILD_C)/decaf_tables.c src/decaf.c src/decaf_crypto.c src/shake.c src/include/*.h src/bat/$$prim.c src/p448/$$where/*.c src/p448/$$where/*.h src/p448/*.c src/p448/*.h $$targ/`basename $$arch`; \
  230. # cp src/bat/api_$$prim.h $$targ/`basename $$arch`/api.h; \
  231. # perl -p -i -e 's/SYSNAME/'`basename $(BATNAME)`_`basename $$arch`'/g' $$targ/`basename $$arch`/api.h; \
  232. # perl -p -i -e 's/__TODAY__/'$(TODAY)'/g' $$targ/`basename $$arch`/api.h; \
  233. # done \
  234. # ) < test/batarch.map; \
  235. # echo 'Mike Hamburg' > $$targ/designers; \
  236. # echo 'Ed448-Goldilocks Decaf sign and dh' > $$targ/description; \
  237. # done
  238. # (cd $(BATNAME)/.. && tar czf $(BATBASE).tgz $(BATBASE) )
  239. # Finds todo items in .h and .c files
  240. TODO_TYPES ?= HACK TODO @todo FIXME BUG XXX PERF FUTURE REMOVE MAGIC UNIFY
  241. TODO_LOCATIONS ?= src test Makefile Doxyfile
  242. todo::
  243. @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep --color=auto -w \
  244. `echo $(TODO_TYPES) | tr ' ' '|'`
  245. @echo '============================='
  246. @(for i in $(TODO_TYPES); do \
  247. (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i > /dev/null || continue; \
  248. /bin/echo -n $$i' ' | head -c 10; \
  249. (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i| wc -l; \
  250. done)
  251. @echo '============================='
  252. @echo -n 'Total '
  253. @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w \
  254. `echo $(TODO_TYPES) | tr ' ' '|'` | wc -l
  255. bench: $(BUILD_IBIN)/bench
  256. ./$<
  257. test: $(BUILD_IBIN)/test
  258. ./$<
  259. test_ct: $(BUILD_IBIN)/test_ct
  260. valgrind ./$<
  261. microbench: $(BUILD_IBIN)/bench
  262. ./$< --micro
  263. clean:
  264. rm -fr build