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.
 
 
 
 
 

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