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.
 
 
 
 
 

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