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.
 
 
 
 
 

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