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.
 
 
 
 
 

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