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.
 
 
 
 
 

322 lines
12 KiB

  1. # Copyright (c) 2014-2017 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 = src/GENERATED/c
  10. BUILD_H = src/GENERATED/c
  11. BUILD_PY = build/obj
  12. BUILD_LIB = build/lib
  13. BUILD_INC = src/GENERATED/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) $(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_code
  56. .PRECIOUS: $(BUILD_C)/*/%.c $(BUILD_H)/*/%.h $(BUILD_H)/%.h $(BUILD_H)/%.hxx $(BUILD_H)/*/%.hxx $(BUILD_IBIN)/%
  57. HEADER_SRCS= $(shell find src/public_include -name "*.h*")
  58. HEADER_PRIVATE_SRCS= $(shell find src/include -name "*.tmpl.h*")
  59. GEN_CODE_0= $(HEADER_SRCS:src/public_include/%=$(BUILD_INC)/%)
  60. GEN_CODE_0+= $(HEADER_PRIVATE_SRCS:src/include/%=$(BUILD_C)/%)
  61. GEN_CODE_1= $(GEN_CODE_0:%.tmpl.h=%.h)
  62. GEN_CODE= $(GEN_CODE_1:%.tmpl.hxx=%.hxx)
  63. HEADERS= Makefile $(shell find src test -name "*.h") $(BUILD_OBJ)/timestamp $(GEN_CODE)
  64. # components needed by the lib
  65. LIBCOMPONENTS = $(BUILD_OBJ)/utils.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/spongerng.o # and per-field components
  66. BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o
  67. all: lib $(BUILD_IBIN)/test $(BUILD_IBIN)/bench $(BUILD_BIN)/shakesum
  68. scan: clean
  69. scan-build --use-analyzer=`which clang` \
  70. -enable-checker deadcode -enable-checker llvm \
  71. -enable-checker osx -enable-checker security -enable-checker unix \
  72. make all
  73. # Internal test programs, which are not part of the final build/bin directory.
  74. $(BUILD_IBIN)/test: $(BUILD_OBJ)/test_decaf.o lib
  75. ifeq ($(UNAME),Darwin)
  76. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  77. else
  78. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  79. endif
  80. # Internal test programs, which are not part of the final build/bin directory.
  81. $(BUILD_IBIN)/test_ct: $(BUILD_OBJ)/test_ct.o lib
  82. ifeq ($(UNAME),Darwin)
  83. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  84. else
  85. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  86. endif
  87. $(BUILD_IBIN)/bench: $(BUILD_OBJ)/bench_decaf.o lib
  88. ifeq ($(UNAME),Darwin)
  89. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  90. else
  91. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  92. endif
  93. # Create all the build subdirectories
  94. $(BUILD_OBJ)/timestamp:
  95. mkdir -p $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \
  96. $(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_H) $(BUILD_INC)/decaf \
  97. $(PER_OBJ_DIRS) $(BUILD_C)/decaf
  98. touch $@
  99. gen_code: $(GEN_CODE)
  100. $(BUILD_INC)/%: src/public_include/% $(BUILD_OBJ)/timestamp
  101. cp -f $< $@
  102. $(BUILD_INC)/%.h: src/public_include/%.tmpl.h src/generator/*
  103. python -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
  104. $(BUILD_C)/%.h: src/include/%.tmpl.h src/generator/*
  105. python -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $<
  106. $(BUILD_INC)/%.hxx: src/public_include/%.tmpl.hxx src/generator/*
  107. python -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
  108. $(BUILD_C)/%.hxx: src/include/%.tmpl.hxx src/generator/*
  109. python -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $<
  110. ################################################################
  111. # Per-field code: call with field, arch
  112. ################################################################
  113. define define_field
  114. ARCH_FOR_$(1) ?= $(2)
  115. COMPONENTS_OF_$(1) = $$(BUILD_OBJ)/$(1)/f_impl.o $$(BUILD_OBJ)/$(1)/f_arithmetic.o $$(BUILD_OBJ)/$(1)/f_generic.o
  116. HEADERS_OF_$(1) = $(HEADERS) $$(BUILD_H)/$(1)/f_field.h
  117. LIBCOMPONENTS += $$(COMPONENTS_OF_$(1))
  118. PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
  119. $$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/generator/* Makefile
  120. python -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
  121. $$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/generator/* Makefile
  122. python -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
  123. $$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
  124. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
  125. -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
  126. -c -o $$@ $$<
  127. $$(BUILD_OBJ)/$(1)/%.o: src/$(1)/%.c $$(HEADERS_OF_$(1))
  128. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
  129. -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
  130. -c -o $$@ $$<
  131. $$(BUILD_OBJ)/$(1)/%.o: src/$(1)/$$(ARCH_FOR_$(1))/%.c $$(HEADERS_OF_$(1))
  132. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
  133. -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
  134. -c -o $$@ $$<
  135. endef
  136. ################################################################
  137. # Per-field, per-curve code: call with curve, field
  138. ################################################################
  139. define define_curve
  140. LIBCOMPONENTS += $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/elligator.o $$(BUILD_OBJ)/$(1)/scalar.o \
  141. $$(BUILD_OBJ)/$(1)/eddsa.o $$(BUILD_OBJ)/$(1)/decaf_tables.o
  142. PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
  143. GLOBAL_HEADERS_OF_$(1) = $(BUILD_INC)/decaf/point_$(3).h $(BUILD_INC)/decaf/point_$(3).hxx \
  144. $(BUILD_INC)/decaf/ed$(3).h $(BUILD_INC)/decaf/ed$(3).hxx
  145. HEADERS_OF_$(1) = $$(HEADERS_OF_$(2)) $$(GLOBAL_HEADERS_OF_$(1))
  146. HEADERS += $$(GLOBAL_HEADERS_OF_$(1))
  147. $$(BUILD_C)/$(1)/%.c: src/per_curve/%.tmpl.c src/generator/* Makefile
  148. python -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
  149. $$(BUILD_H)/$(1)/%.h: src/per_curve/%.tmpl.h src/generator/* Makefile
  150. python -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
  151. $$(BUILD_INC)/decaf/point_$(3).%: src/per_curve/point.tmpl.% src/generator/* Makefile
  152. python -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  153. $$(BUILD_INC)/decaf/ed$(3).%: src/per_curve/eddsa.tmpl.% src/generator/* Makefile
  154. python -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  155. $$(BUILD_INC)/decaf/elligator_$(3).%: src/per_curve/elligator.tmpl.% src/generator/* Makefile
  156. python -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  157. $$(BUILD_INC)/decaf/scalar_$(3).%: src/per_curve/scalar.tmpl.% src/generator/* Makefile
  158. python -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  159. $$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \
  160. $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/scalar.o $$(BUILD_OBJ)/utils.o \
  161. $$(COMPONENTS_OF_$(2))
  162. $$(LD) $$(LDFLAGS) -o $$@ $$^
  163. $$(BUILD_C)/$(1)/decaf_tables.c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
  164. ./$$< > $$@ || (rm $$@; exit 1)
  165. $$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
  166. $$(CC) $$(CFLAGS) -c -o $$@ $$< \
  167. -I build/obj/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
  168. -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2))
  169. $$(BUILD_OBJ)/decaf_gen_tables_$(1).o: src/decaf_gen_tables.c $$(HEADERS_OF_$(1))
  170. $$(CC) $$(CFLAGS) \
  171. -I build/obj/curve_$(1) -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
  172. -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \
  173. -c -o $$@ $$<
  174. endef
  175. ################################################################
  176. # call code above to generate curves and fields
  177. $(eval $(call define_field,p25519,arch_x86_64))
  178. $(eval $(call define_curve,curve25519,p25519,255))
  179. $(eval $(call define_field,p448,arch_x86_64))
  180. $(eval $(call define_curve,ed448goldilocks,p448,448))
  181. # The shakesum utility is in the public bin directory.
  182. $(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/utils.o
  183. $(LD) $(LDFLAGS) -o $@ $^
  184. # The main decaf library, and its symlinks.
  185. lib: $(BUILD_LIB)/libdecaf.so
  186. $(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
  187. ln -sf `basename $^` $@
  188. $(BUILD_LIB)/libdecaf.so.1: $(LIBCOMPONENTS)
  189. rm -f $@
  190. ifeq ($(UNAME),Darwin)
  191. libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
  192. $(LIBCOMPONENTS)
  193. else ifeq ($(UNAME),SunOS)
  194. $(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -o $@ $(LIBCOMPONENTS)
  195. strip --discard-all $@
  196. else
  197. $(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
  198. strip --discard-all $@
  199. endif
  200. $(BUILD_OBJ)/%.o: src/%.c $(HEADERS)
  201. $(CC) $(CFLAGS) -c -o $@ $<
  202. $(BUILD_OBJ)/%.o: test/%.c $(HEADERS)
  203. $(CC) $(PUB_CFLAGS) -c -o $@ $<
  204. $(BUILD_OBJ)/%.o: test/%.cxx $(HEADERS)
  205. $(CXX) $(CXXFLAGS) -c -o $@ $<
  206. # The sage test scripts
  207. sage: $(BUILDPYS)
  208. sagetest: sage lib
  209. $(SAGE) $(BUILD_PY)/test_decaf.sage
  210. $(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
  211. cp -f $(SAGES) $(BUILD_PY)/
  212. $(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
  213. # some sage versions compile to .sage.py
  214. for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
  215. if [ -e $$f.sage.py ]; then \
  216. mv $$f.sage.py $$f.py; \
  217. fi; \
  218. done
  219. # The documentation files
  220. $(BUILD_DOC)/timestamp:
  221. mkdir -p `dirname $@`
  222. touch $@
  223. #
  224. doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS)
  225. doxygen > /dev/null
  226. # Finds todo items in .h and .c files
  227. TODO_TYPES ?= HACK TODO @todo FIXME BUG XXX PERF FUTURE REMOVE MAGIC UNIFY
  228. TODO_LOCATIONS ?= src/*.c src/include src/p* src/generator test Makefile Doxyfile
  229. todo::
  230. @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep --color=auto -w \
  231. `echo $(TODO_TYPES) | tr ' ' '|'`
  232. @echo '============================='
  233. @(for i in $(TODO_TYPES); do \
  234. (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i > /dev/null || continue; \
  235. /bin/echo -n $$i' ' | head -c 10; \
  236. (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i| wc -l; \
  237. done)
  238. @echo '============================='
  239. @echo -n 'Total '
  240. @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w \
  241. `echo $(TODO_TYPES) | tr ' ' '|'` | wc -l
  242. bench: $(BUILD_IBIN)/bench
  243. ./$<
  244. test: $(BUILD_IBIN)/test
  245. ./$<
  246. test_ct: $(BUILD_IBIN)/test_ct
  247. # NB: you must compile with XCFLAGS=-DNDEBUG or you will get lots of extra warnings due to assert(thing that is always true).
  248. valgrind ./$<
  249. microbench: $(BUILD_IBIN)/bench
  250. ./$< --micro
  251. clean:
  252. rm -fr build
  253. clean_generated: clean
  254. rm -fr $(BUILD_C)/* $(BUILD_H)/* $(BUILD_INC)/*