interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / crypto / Makefile

92 lines · 4164 bytes Raw Blame History
1# AIENOS in-house crypto in C: AES-256, POLYVAL, AES-256-GCM-SIV (RFC 8452),
2# HMAC-SHA-256. Port of crates/aienos-crypto. No outside library: SHA-256 is
3# the in-house copy in ../argus/sha256.c. Library objects import no heap,
4# clock or I/O; the test uses hosted libc.
5#
6# make build out/libaienos_crypto.a
7# make test known-answer + negative tests (host, not QEMU)
8# make sanitize the same test under ASan+UBSan (Linux)
9# make mutants remove each GUARD-tagged rule in gcm_siv.c; tests must fail
10# make clean
11
12CC ?= cc
13AR ?= ar
14CFLAGS ?= -std=gnu11 -O2 -Wall -Wextra -Werror -fstack-protector-strong
15OUT ?= out
16
17SRCS = aes.c polyval.c gcm_siv.c hmac.c ct.c
18OBJS = $(SRCS:%.c=$(OUT)/%.o)
19LIB = $(OUT)/libaienos_crypto.a
20TEST = $(OUT)/crypto_test
21HDRS = aienos_crypto.h crypto_internal.h ../argus/sha256.h
22SANFLAGS = -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=all -g
23
24all: $(LIB)
25
26$(OUT):
27 mkdir -p $(OUT)
28
29$(OUT)/%.o: %.c $(HDRS) | $(OUT)
30 $(CC) $(CFLAGS) -c -o $@ $<
31
32$(OUT)/sha256.o: ../argus/sha256.c ../argus/sha256.h | $(OUT)
33 $(CC) $(CFLAGS) -c -o $@ ../argus/sha256.c
34
35$(LIB): $(OBJS) $(OUT)/sha256.o
36 $(AR) rcs $@ $^
37
38$(TEST): tests/crypto_test.c $(LIB) $(HDRS)
39 $(CC) $(CFLAGS) -o $@ tests/crypto_test.c $(LIB)
40
41test: $(TEST) lib-checks
42 ./$(TEST)
43
44# The library objects must not allocate, read a clock, or do their own I/O.
45lib-checks: $(OBJS) $(OUT)/sha256.o
46 @bad=$$(nm -u $(OBJS) $(OUT)/sha256.o | awk 'NF {print $$NF}' | sed 's/^_//' | grep -xE 'malloc|calloc|realloc|free|aligned_alloc|posix_memalign|mmap|clock_gettime|time|gettimeofday|open|openat|read|write|pread|pwrite|fopen|fread|fwrite|printf|fprintf|puts|getrandom'); \
47 if [ -n "$$bad" ]; then echo "lib-checks FAIL: library imports $$bad"; exit 1; fi
48 @echo "lib-checks PASS (no heap, clock or direct I/O in libaienos_crypto objects)"
49
50sanitize: | $(OUT)
51ifeq ($(shell uname -s),Linux)
52 $(CC) $(CFLAGS) $(SANFLAGS) -o $(OUT)/crypto_test_asan tests/crypto_test.c $(SRCS) ../argus/sha256.c
53 ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/crypto_test_asan
54else
55 @echo "sanitize: Linux only"
56endif
57
58# Mutants: delete each GUARD-tagged line of gcm_siv.c in turn. The tests must
59# fail (or crash) for every mutant, or that rule is not tested.
60GUARDS = tag-msb ctr-msb pt-max aad-max-seal ct-max aad-max-open tag-check wipe-on-fail
61
62$(OUT)/t_crypto_test.o: tests/crypto_test.c $(HDRS) | $(OUT)
63 $(CC) $(CFLAGS) -Wno-error -I. -c -o $@ $<
64
65mutants: $(OBJS) $(OUT)/sha256.o $(OUT)/t_crypto_test.o | $(OUT)
66 @ulimit -c 0; fail=0; \
67 dups=$$(printf '%s\n' $(GUARDS) | sort | uniq -d); \
68 [ -z "$$dups" ] || { echo "duplicate guard names (parallel runs would collide):" $$dups; echo "AIENOS_CRYPTO_MUTANTS: FAIL"; exit 1; }; \
69 for g in $(GUARDS); do \
70 rm -f $(OUT)/mut_$$g.msg $(OUT)/mut_$$g.fail $(OUT)/mut_$$g.killed; \
71 ( grep -q "GUARD:$$g \*/" gcm_siv.c || { echo "mutant $$g: guard tag missing" > $(OUT)/mut_$$g.msg; echo 1 > $(OUT)/mut_$$g.fail; exit 0; }; \
72 sed "/GUARD:$$g \*\//d" gcm_siv.c > $(OUT)/mut_$$g.c; \
73 $(CC) $(CFLAGS) -Wno-error -I. -c -o $(OUT)/mut_$$g.o $(OUT)/mut_$$g.c \
74 && $(CC) $(CFLAGS) -Wno-error -o $(OUT)/mut_$$g $(OUT)/t_crypto_test.o $(OUT)/mut_$$g.o \
75 $(OUT)/aes.o $(OUT)/polyval.o $(OUT)/hmac.o $(OUT)/ct.o $(OUT)/sha256.o \
76 || { echo "mutant $$g: does not compile" > $(OUT)/mut_$$g.msg; echo 1 > $(OUT)/mut_$$g.fail; exit 0; }; \
77 if ./$(OUT)/mut_$$g > $(OUT)/mut_$$g.log 2>&1; then \
78 echo "mutant $$g: SURVIVED (FAIL)" > $(OUT)/mut_$$g.msg; echo 1 > $(OUT)/mut_$$g.fail; \
79 else echo "mutant $$g: killed" > $(OUT)/mut_$$g.msg; : > $(OUT)/mut_$$g.killed; fi \
80 ) & \
81 done; \
82 wait; \
83 for g in $(GUARDS); do \
84 if [ -f $(OUT)/mut_$$g.msg ]; then cat $(OUT)/mut_$$g.msg; else echo "mutant $$g: no result recorded (FAIL)"; fi; \
85 [ -f $(OUT)/mut_$$g.killed ] && [ ! -f $(OUT)/mut_$$g.fail ] || fail=1; \
86 done; \
87 if [ $$fail -eq 0 ]; then echo "AIENOS_CRYPTO_MUTANTS: PASS"; else echo "AIENOS_CRYPTO_MUTANTS: FAIL"; exit 1; fi
88
89clean:
90 rm -rf $(OUT)
91
92.PHONY: all test lib-checks sanitize mutants clean