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.
 
 
 
 
 

326 lines
12 KiB

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