Skip to content

Commit

Permalink
Updates to generation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptojedi committed Aug 7, 2024
1 parent 57f2545 commit 70c90a7
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 1 deletion.
127 changes: 127 additions & 0 deletions build-common/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# -*- Makefile -*-

# --------------------------------------------------------------------
AS ?= as
CC ?= clang
CFLAGS ?= -O3 -Wall -Wextra -Wpedantic -Wvla -Werror -std=c99 \
-Wundef -Wshadow -Wcast-align -Wpointer-arith -Wmissing-prototypes \
-fstrict-aliasing -fno-common -pipe

JASMIN ?= jasminc
JASMIN_CT ?= jasmin-ct

# --------------------------------------------------------------------
CI ?= 0
export CI
CICL ?= "1"

# --------------------------------------------------------------------
LOGS := libjade-logs-src.tar.gz

EXCLUDE ?=
SRC := .

FILTER ?= $(SRC)/crypto_%
JAZZ ?= $(filter $(FILTER), $(filter-out $(addprefix ./,$(EXCLUDE)), $(sort $(dir $(shell find $(SRC) -name '*.jazz')))))
SAFETY ?= $(addsuffix safety, $(JAZZ))
SCT ?= $(addsuffix check_sct, $(JAZZ))

SOURCES ?= $(filter-out ./, $(sort $(dir $(shell find $(SRC) -name 'Makefile'))))
ASM := $(shell find $(SRC) -name '*.s')
API := $(addsuffix include/api.h, $(dir $(ASM)))
OBJ := $(ASM:%.s=%.o)

FAIL_ON_ERROR ?= 0

# --------------------------------------------------------------------
ifeq ($(CI),1)
.PHONY: backward_compatibility
backward_compatibility:
$(MAKE) default
$(MAKE) reporter
mv $(LOGS) check.tar.gz
$(MAKE) err
endif

# --------------------------------------------------------------------
.PHONY: libjade.a libjade.h $(JAZZ) clean distclean $(LOGS)
.INTERMEDIATE: $(OBJ)

default: libjade.a libjade.h

all: $(JAZZ)

libjade.a: $(JAZZ)
$(MAKE) __libjade

__libjade: $(OBJ)
ar -rc libjade.a $(OBJ)
ranlib libjade.a
echo "" | cat - $(API) > libjade.h

$(JAZZ):
$(MAKE) -C $@ || !(($(FAIL_ON_ERROR)))

# --------------------------------------------------------------------

SAFETY_TIMEOUT ?= 1440m
export SAFETY_TIMEOUT

.PHONY: safety
safety: $(SAFETY)

$(SAFETY):
$(MAKE) -C $(@D) $(@F) || true

# --------------------------------------------------------------------
.PHONY: check_sct
check_sct: $(SCT)

$(SCT):
$(MAKE) -C $(@D) $(@F) || true

# --------------------------------------------------------------------
ifeq ($(CI),1)

reporter:
$(MAKE) reporter_s
$(MAKE) $(LOGS)

reporter_s:
./../scripts/ci/reporter/jlog "Compilation status" src/ *.s $(CICL)
$(MAKE) $(LOGS)

reporter_safety:
./../scripts/ci/reporter/jlog "Safety status" src/ *.safety $(CICL)
$(MAKE) $(LOGS)

reporter_sct:
./../scripts/ci/reporter/jlog "Speculative constant-time status" src/ *.sct $(CICL)

ERR := $(shell find $(BIN) -name '*.error')
CIR := $(shell find $(BIN) -name '*.log') $(ERR)

$(LOGS):
@$(JASMIN) -version > notes
ifeq ($(words $(CIR)),0)
@echo "good job." >> notes
@tar -zcvf $@ notes
else
@tar -zcvf $@ notes $(CIR)
endif
@rm notes

err:
ifneq ($(words $(ERR)),0)
$(error $(ERR))
endif

endif

# --------------------------------------------------------------------
clean:
rm -f libjade.a libjade.h $(LOGS) check.tar.gz

distclean: clean
for i in $(SOURCES); do $(MAKE) -C $$i clean; done

38 changes: 38 additions & 0 deletions build-common/Makefile.checksafety
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Notes:
# - this file defines fine-grained targets that allow checking the safety of individual exported
# functions
# - it is meant to be included by Makefile.common, right before the 'generic' safety targets

ifneq ($(OP),)

SAFETY_FLAGS ?=
SAFETY_TIMEOUT ?= 4320m
SAFETY_DIR := .safety
CHECK_SAFETY_S = (time timeout -v $(SAFETY_TIMEOUT) $(JASMINC) -slice $* -checksafety $(SAFETY_FLAGS) $(shell cat $(SAFETY_DIR)/$*.safetyparam) $< 2> $@) $(CIT)
CHECK_SAFETY = (time timeout -v $(SAFETY_TIMEOUT) $(JASMINC) -checksafety $(SAFETY_FLAGS) $(shell cat $(SAFETY_DIR)/$(OP).safetyparam) $< 2> $@) $(CIT)

SAFETY_TARGETS = $(addsuffix .safety, $(FUNCTIONS))

checksafety-all: $(SAFETY_TARGETS)

$(OP).safety : $(OP).jazz $(SAFETY_DIR)/$(OP).safetyparam $(DEPS_DIR)/$(OP).safety.d | $(SAFETY_DIR) $(DEPS_DIR) $(CI_DIR)
$(DEPS)
$(CHECK_SAFETY)

$(SAFETY_TARGETS):
%.safety : $(OP).jazz $(SAFETY_DIR)/$(OP).safetyparam $(DEPS_DIR)/%.safety.d | $(SAFETY_DIR) $(DEPS_DIR) $(CI_DIR)
$(DEPS)
$(CHECK_SAFETY_S)

DEPFILES := \
$(DEPFILES) \
$(addprefix $(DEPS_DIR)/, $(addsuffix .safety.d, $(FUNCTIONS) $(OP)))

$(SAFETY_DIR)/$(OP).safetyparam: $(SAFETY_DIR)
$(MAKE) -C $(TEST) bin/$(RDIR)/safetyparams
(cd $(SAFETY_DIR) && $(TDIR)/safetyparams)

$(SAFETY_DIR): ; @mkdir -p $@

endif

47 changes: 47 additions & 0 deletions build-common/Makefile.checksct
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Notes:
# - this file defines fine-grained targets that allow checking the speculative constant-time of individual exported
# functions
# - it is meant to be included by Makefile.common

# JASMIN_CT belongs here (and not Makefile.common): some options differ from jasminc
JASMIN_CT ?= jasmin-ct

ifneq ($(OP),)

# TODO: remove --infer and annotate exported functions
SCT_FLAGS ?= --infer

CHECK_SCT_SLICE = (JASMINPATH="Jade=$(SRC)" $(JASMIN_CT) --slice $* --sct $(SCT_FLAGS) $< > $@ 2>&1) $(CIT)
CHECK_SCT_SLICE_STDOUT = (JASMINPATH="Jade=$(SRC)" $(JASMIN_CT) --slice $* --sct $(SCT_FLAGS) $< ) $(CIT)

CHECK_SCT = (JASMINPATH="Jade=$(SRC)" $(JASMIN_CT) --sct $(SCT_FLAGS) $< > $@ 2>&1) $(CIT)
CHECK_SCT_STDOUT = (JASMINPATH="Jade=$(SRC)" $(JASMIN_CT) --sct $(SCT_FLAGS) $< ) $(CIT)

SCT_TARGETS = $(addsuffix .sct, $(FUNCTIONS))
SCT_TARGETS_STDOUT = $(addsuffix .stdout, $(SCT_TARGETS))

check_sct: $(SCT_TARGETS)

$(OP).sct : $(OP).jazz $(DEPS_DIR)/$(OP).sct.d | $(DEPS_DIR) $(CI_DIR)
$(DEPS)
$(CHECK_SCT)

$(OP).sct.stdout : $(OP).jazz | $(CI_DIR)
$(CHECK_SCT_STDOUT)

$(SCT_TARGETS):
%.sct : $(OP).jazz $(DEPS_DIR)/%.sct.d | $(DEPS_DIR) $(CI_DIR)
$(DEPS)
$(CHECK_SCT_SLICE)

$(SCT_TARGETS_STDOUT):
%.sct.stdout : $(OP).jazz | $(CI_DIR)
$(CHECK_SCT_SLICE_STDOUT)

DEPFILES := \
$(DEPFILES) \
$(addprefix $(DEPS_DIR)/, $(addsuffix .sct.d, $(FUNCTIONS) $(OP)))

$(SCT_DIR): ; @mkdir -p $@

endif
134 changes: 134 additions & 0 deletions build-common/Makefile.common
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@

# --------------------------------------------------------------------
TOP = $(abspath $(dir $(filter %Makefile.common,$(MAKEFILE_LIST)))../)
SRC = $(TOP)/src/
BC = $(TOP)/build-common/
TEST = $(TOP)/test/
PROOF = $(TOP)/proof/

CDIR = $(abspath $(dir $(abspath $(firstword $(MAKEFILE_LIST)))))
RDIR = $(subst $(SRC),,$(CDIR))
TDIR = $(TEST)bin/$(RDIR)
PDIR = $(PROOF)$(RDIR)

# OPERATION: crypto_kem, crypto_hash, ..., common; OP is a shorthand for OPERATION
OPERATION = $(word 1, $(subst /, ,$(RDIR)))
OP = $(word 2, $(subst _, , $(OPERATION)_))
NAMESPACE = $(subst crypto_,jade_, $(subst -,_,$(subst /,_,$(RDIR))))

# --------------------------------------------------------------------

include $(BC)/Makefile.functions

# --------------------------------------------------------------------
CI_DIR =
CIT =
ifeq ($(CI),1)
CI_DIR := .ci
CIT = 2> $(CI_DIR)/$(@F).log && rm -f $(CI_DIR)/$(@F).error || \
(echo $$? > $(CI_DIR)/$(@F).error && \
cat $(CI_DIR)/$(@F).log >> $(CI_DIR)/$(@F).error && \
rm $(CI_DIR)/$(@F).log && \
exit 127)
endif

# --------------------------------------------------------------------
JEXT ?= jazz
override JFLAGS += -noinsertarraycopy $(addprefix -slice ,$(FUNCTIONS))
JINCLUDE = -I Jade:$(SRC)

JASMIN ?= jasminc
JASMINC := $(JASMIN) $(JFLAGS) $(JINCLUDE)
COMPILE = ($(JASMINC) -o $@ $<) $(CIT)

# --------------------------------------------------------------------
CHECKS_DIR := checks

# --------------------------------------------------------------------
DEPS_DIR := .deps
DEPS = ((printf "$@: "; printf "$< "; $(JASMINC) -print-dependencies $<) > $(DEPS_DIR)/$(@F).d) $(CIT)
DEPFILES :=

# --------------------------------------------------------------------
compile: $(SRCS:%.$(JEXT)=%.s)
@true

# --------------------------------------------------------------------
# Note: by using a single-file extraction, only one Jasmin file can be processed (the first from SRCS)

extract: $(EC_DIR)/extracted_s.ec $(EC_DIR)/extracted_ct.ec
@true

$(EC_DIR)/extracted_s.ec : $(SRCS) $(DEPS_DIR)/extracted_s.ec.d | $(DEPS_DIR) $(EC_DIR) $(CI_DIR)
$(DEPS)
$(EXTRACT_S)

$(EC_DIR)/extracted_ct.ec : $(SRCS) $(DEPS_DIR)/extracted_ct.ec.d | $(DEPS_DIR) $(EC_DIR) $(CI_DIR)
$(DEPS)
$(EXTRACT_CT)

# --------------------------------------------------------------------

extract0: extract_s extract_ct
@true

extract_s: $(SRCS:%.$(JEXT)=$(EC_DIR)/%_s.ec)
@true

extract_ct: $(SRCS:%.$(JEXT)=$(EC_DIR)/%_ct.ec)
@true

%.s : %.$(JEXT) $(DEPS_DIR)/%.s.d | $(DEPS_DIR) $(CI_DIR)
$(DEPS)
$(COMPILE)

$(EC_DIR)/%_s.ec : %.$(JEXT) $(DEPS_DIR)/%_s.ec.d | $(DEPS_DIR) $(EC_DIR) $(CI_DIR)
$(DEPS)
$(EXTRACT_S)

$(EC_DIR)/%_ct.ec : %.$(JEXT) $(DEPS_DIR)/%_ct.ec.d | $(DEPS_DIR) $(EC_DIR) $(CI_DIR)
$(DEPS)
$(EXTRACT_CT)

# --------------------------------------------------------------------

include $(BC)/Makefile.checksafety

# --------------------------------------------------------------------

include $(BC)/Makefile.checksct

# --------------------------------------------------------------------
$(CHECKSDIR): ; @mkdir -p $@
$(DEPS_DIR): ; @mkdir -p $@
$(EC_DIR): ; @mkdir -p $@; touch $@/.gitkeep
ifeq ($(CI),1)
$(CI_DIR): ; @mkdir -p $@
endif

DEPFILES := \
$(DEPFILES) \
$(SRCS:%.$(JEXT)=$(DEPS_DIR)/%.s.d) \
$(SRCS:%.$(JEXT)=$(DEPS_DIR)/%_s.ec.d) \
$(SRCS:%.$(JEXT)=$(DEPS_DIR)/%_ct.ec.d) \
$(DEPS_DIR)/extracted_s.ec.d \
$(DEPS_DIR)/extracted_ct.ec.d

$(DEPFILES):

include $(wildcard $(DEPFILES))

# --------------------------------------------------------------------
.PHONY: .jflags
.jflags:
@echo -n "$(JFLAGS)" > .jflags

# --------------------------------------------------------------------
.PHONY: clean

clean:
@rm -fr $(DEPS_DIR) $(CHECKS_DIR) $(SAFETY_DIR) *.s *.safety* *.sct* *.o *.a .jflags *.out
ifeq ($(CI),1)
@rm -fr $(CI_DIR)
endif

41 changes: 41 additions & 0 deletions build-common/Makefile.functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
ifeq ($(OP),kem)
ifeq ($(SRCS),kem.jazz)
FUNCTIONS = $(addprefix $(NAMESPACE)_, keypair_derand keypair enc_derand enc dec)
endif
endif

ifeq ($(OP),hash)
ifeq ($(SRCS),hash.jazz)
FUNCTIONS = $(NAMESPACE)
endif
endif

ifeq ($(OP),onetimeauth)
ifeq ($(SRCS),onetimeauth.jazz)
FUNCTIONS = $(NAMESPACE) $(NAMESPACE)_verify
endif
endif

ifeq ($(OP),scalarmult)
ifeq ($(SRCS),scalarmult.jazz)
FUNCTIONS = $(NAMESPACE) $(NAMESPACE)_base
endif
endif

ifeq ($(OP),secretbox)
ifeq ($(SRCS),secretbox.jazz)
FUNCTIONS = $(NAMESPACE) $(NAMESPACE)_open
endif
endif

ifeq ($(OP),sign)
ifeq ($(SRCS),sign.jazz)
FUNCTIONS = $(NAMESPACE)_keypair $(NAMESPACE) $(NAMESPACE)_open
endif
endif

ifeq ($(OP),xof)
ifeq ($(SRCS),xof.jazz)
FUNCTIONS = $(NAMESPACE)
endif
endif
Loading

0 comments on commit 70c90a7

Please sign in to comment.