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.
 
 
 
 
 

337 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. ifeq ($(UNAME),Darwin)
  17. CC = clang
  18. CXX = clang++
  19. else
  20. CC = gcc
  21. CXX = g++
  22. endif
  23. LD = $(CC)
  24. LDXX = $(CXX)
  25. ASM ?= $(CC)
  26. PYTHON ?= python
  27. WARNFLAGS = -pedantic -Wall -Wextra -Werror -Wunreachable-code \
  28. -Wmissing-declarations -Wunused-function -Wno-overlength-strings $(EXWARN)
  29. INCFLAGS = -Isrc/include -I$(BUILD_INC) -I$(BUILD_H)
  30. PUB_INCFLAGS = -I$(BUILD_INC)
  31. LANGFLAGS = -std=c99 -fno-strict-aliasing
  32. LANGXXFLAGS = -fno-strict-aliasing
  33. GENFLAGS = -ffunction-sections -fdata-sections -fvisibility=hidden -fomit-frame-pointer -fPIC
  34. OFLAGS ?= -O2
  35. MACOSX_VERSION_MIN ?= 10.9
  36. ifeq ($(UNAME),Darwin)
  37. GENFLAGS += -mmacosx-version-min=$(MACOSX_VERSION_MIN)
  38. endif
  39. TODAY = $(shell date "+%Y-%m-%d")
  40. ARCHFLAGS ?= -march=native
  41. ifeq ($(CC),clang)
  42. WARNFLAGS_C += -Wgcc-compat
  43. endif
  44. ifeq ($(CXX),clang++)
  45. WARNFLAGS_CXX += -Wgcc-compat
  46. endif
  47. ARCHFLAGS += $(XARCHFLAGS)
  48. CFLAGS = $(LANGFLAGS) $(WARNFLAGS) $(WARNFLAGS_C) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
  49. PUB_CFLAGS = $(LANGFLAGS) $(WARNFLAGS) $(WARNFLAGS_C) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
  50. CXXFLAGS = $(LANGXXFLAGS) $(WARNFLAGS) $(WARNFLAGS_CXX) $(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 gen_code_static
  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. $(BUILD_INC)/%: src/public_include/% $(BUILD_OBJ)/timestamp
  102. cp -f $< $@
  103. $(BUILD_INC)/%.h: src/public_include/%.tmpl.h src/generator/*
  104. $(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
  105. $(BUILD_C)/%.h: src/include/%.tmpl.h src/generator/*
  106. $(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $<
  107. $(BUILD_INC)/%.hxx: src/public_include/%.tmpl.hxx src/generator/*
  108. $(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
  109. $(BUILD_C)/%.hxx: src/include/%.tmpl.hxx src/generator/*
  110. $(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $<
  111. ################################################################
  112. # Per-field code: call with field, arch
  113. ################################################################
  114. PER_FIELD_C = $(wildcard src/per_field/*.tmpl.c)
  115. PER_FIELD_H = $(wildcard src/per_field/*.tmpl.h*)
  116. define define_field
  117. ARCH_FOR_$(1) ?= $(2)
  118. COMPONENTS_OF_$(1) = $$(BUILD_OBJ)/$(1)/f_impl.o $$(BUILD_OBJ)/$(1)/f_arithmetic.o $$(BUILD_OBJ)/$(1)/f_generic.o
  119. HEADERS_OF_$(1) = $(HEADERS) $$(BUILD_H)/$(1)/f_field.h
  120. LIBCOMPONENTS += $$(COMPONENTS_OF_$(1))
  121. GEN_CODE_FOR_$(1) = $$(patsubst src/per_field/%,$(BUILD_C)/$(1)/%,$(patsubst %.tmpl.c,%.c,$(PER_FIELD_C)))
  122. GEN_CODE_FOR_$(1) += $$(patsubst src/per_field/%,$(BUILD_H)/$(1)/%,$(patsubst %.tmpl.h,%.h,$(PER_FIELD_H)))
  123. GEN_CODE += $$(GEN_CODE_FOR_$(1))
  124. PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
  125. $$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/generator/* Makefile
  126. $(PYTHON) -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
  127. $$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/generator/* Makefile
  128. $(PYTHON) -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
  129. $$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(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)/%.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. $$(BUILD_OBJ)/$(1)/%.o: src/$(1)/$$(ARCH_FOR_$(1))/%.c $$(HEADERS_OF_$(1))
  138. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
  139. -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
  140. -c -o $$@ $$<
  141. endef
  142. ################################################################
  143. # Per-field, per-curve code: call with curve, field
  144. ################################################################
  145. PER_CURVE_C = $(wildcard src/per_curve/*.tmpl.c)
  146. define define_curve
  147. LIBCOMPONENTS += $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/elligator.o $$(BUILD_OBJ)/$(1)/scalar.o \
  148. $$(BUILD_OBJ)/$(1)/eddsa.o $$(BUILD_OBJ)/$(1)/decaf_tables.o
  149. PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
  150. GLOBAL_HEADERS_OF_$(1) = $(BUILD_INC)/decaf/point_$(3).h $(BUILD_INC)/decaf/point_$(3).hxx \
  151. $(BUILD_INC)/decaf/ed$(3).h $(BUILD_INC)/decaf/ed$(3).hxx
  152. HEADERS_OF_$(1) = $$(HEADERS_OF_$(2)) $$(GLOBAL_HEADERS_OF_$(1))
  153. HEADERS += $$(GLOBAL_HEADERS_OF_$(1))
  154. GEN_CODE_FOR_$(1) = $$(patsubst src/per_curve/%,$(BUILD_C)/$(1)/%,$(patsubst %.tmpl.c,%.c,$(PER_CURVE_C)))
  155. GEN_CODE_FOR_$(1) += $$(GLOBAL_HEADERS_OF_$(1))
  156. GEN_CODE_P2 += $(BUILD_C)/$(1)/decaf_tables.c
  157. GEN_CODE += $$(GEN_CODE_FOR_$(1))
  158. $$(BUILD_C)/$(1)/%.c: src/per_curve/%.tmpl.c src/generator/* Makefile
  159. $(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
  160. $$(BUILD_H)/$(1)/%.h: src/per_curve/%.tmpl.h src/generator/* Makefile
  161. $(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
  162. $$(BUILD_INC)/decaf/point_$(3).%: src/per_curve/point.tmpl.% src/generator/* Makefile
  163. $(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  164. $$(BUILD_INC)/decaf/ed$(3).%: src/per_curve/eddsa.tmpl.% src/generator/* Makefile
  165. $(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  166. $$(BUILD_INC)/decaf/elligator_$(3).%: src/per_curve/elligator.tmpl.% src/generator/* Makefile
  167. $(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  168. $$(BUILD_INC)/decaf/scalar_$(3).%: src/per_curve/scalar.tmpl.% src/generator/* Makefile
  169. $(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
  170. $$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \
  171. $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/scalar.o $$(BUILD_OBJ)/utils.o \
  172. $$(COMPONENTS_OF_$(2))
  173. $$(LD) $$(LDFLAGS) -o $$@ $$^
  174. $$(BUILD_C)/$(1)/decaf_tables.c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
  175. ./$$< > $$@ || (rm $$@; exit 1)
  176. $$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
  177. $$(CC) $$(CFLAGS) -c -o $$@ $$< \
  178. -I build/obj/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
  179. -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2))
  180. $$(BUILD_OBJ)/decaf_gen_tables_$(1).o: src/decaf_gen_tables.c $$(HEADERS_OF_$(1))
  181. $$(CC) $$(CFLAGS) \
  182. -I build/obj/curve_$(1) -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
  183. -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \
  184. -c -o $$@ $$<
  185. endef
  186. ################################################################
  187. # call code above to generate curves and fields
  188. $(eval $(call define_field,p25519,arch_x86_64))
  189. $(eval $(call define_curve,curve25519,p25519,255))
  190. $(eval $(call define_field,p448,arch_x86_64))
  191. $(eval $(call define_curve,ed448goldilocks,p448,448))
  192. # The shakesum utility is in the public bin directory.
  193. $(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/utils.o
  194. $(LD) $(LDFLAGS) -o $@ $^
  195. # The main decaf library, and its symlinks.
  196. lib: $(BUILD_LIB)/libdecaf.so
  197. $(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
  198. ln -sf `basename $^` $@
  199. $(BUILD_LIB)/libdecaf.so.1: $(LIBCOMPONENTS)
  200. rm -f $@
  201. ifeq ($(UNAME),Darwin)
  202. libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
  203. $(LIBCOMPONENTS)
  204. else ifeq ($(UNAME),SunOS)
  205. $(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -o $@ $(LIBCOMPONENTS)
  206. strip --discard-all $@
  207. else
  208. $(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
  209. strip --discard-all $@
  210. endif
  211. $(BUILD_OBJ)/%.o: src/%.c $(HEADERS)
  212. $(CC) $(CFLAGS) -c -o $@ $<
  213. $(BUILD_OBJ)/%.o: test/%.c $(HEADERS)
  214. $(CC) $(PUB_CFLAGS) -c -o $@ $<
  215. $(BUILD_OBJ)/%.o: test/%.cxx $(HEADERS)
  216. $(CXX) $(CXXFLAGS) -c -o $@ $<
  217. # The sage test scripts
  218. sage: $(BUILDPYS)
  219. sagetest: sage lib
  220. $(SAGE) $(BUILD_PY)/test_decaf.sage
  221. $(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
  222. cp -f $(SAGES) $(BUILD_PY)/
  223. $(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
  224. # some sage versions compile to .sage.py
  225. for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
  226. if [ -e $$f.sage.py ]; then \
  227. mv $$f.sage.py $$f.py; \
  228. fi; \
  229. done
  230. # The documentation files
  231. $(BUILD_DOC)/timestamp:
  232. mkdir -p `dirname $@`
  233. touch $@
  234. #
  235. doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS)
  236. doxygen > /dev/null
  237. gen_code_static: $(GEN_CODE)
  238. gen_code: gen_code_static $(GEN_CODE_P2)
  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/*.c src/include src/p* src/generator 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. # NB: you must compile with XCFLAGS=-DNDEBUG or you will get lots of extra warnings due to assert(thing that is always true).
  261. valgrind ./$<
  262. microbench: $(BUILD_IBIN)/bench
  263. ./$< --micro
  264. clean:
  265. rm -fr build
  266. clean_generated: clean
  267. rm -fr $(BUILD_C)/* $(BUILD_H)/* $(BUILD_INC)/*