packages feed

crypton-2.3.1 / cbits / tests / ct / run.sh

#!/bin/sh# Does the code that handles a secret run in time independent of it?## memcheck already follows undefined bytes through arithmetic and reports the# moment one decides a branch or an address.  That is the same question, so# each driver declares its secret undefined and runs under valgrind: every# report names a place where the secret reached a branch or an index.# The technique is Adam Langley's ctgrind.## Without valgrind the drivers are still built and run, which says only that# the plumbing is right -- it checks nothing about timing, and says so.## Usage: cbits/tests/ct/run.sh [build-dir]set -euroot=$(CDPATH= cd -- "$(dirname -- "$0")/../../.." && pwd)out=${1:-$(mktemp -d)}cc=${CC:-cc}mkdir -p "$out"cd "$root"D=cbits/decafdecaf_src="$D/ed448goldilocks/decaf_all.c $D/ed448goldilocks/eddsa.c           $D/ed448goldilocks/scalar.c $D/p448/f_arithmetic.c           $D/p448/f_generic.c $D/utils.c $D/p448/arch_ref64/f_impl.c           cbits/crypton_sha3.c"decaf_inc="-DCRYPTON_DECAF_WORD_BITS=64 -I$D/include -I$D/p448           -I$D/include/arch_ref64 -I$D/p448/arch_ref64"# The portable C, not whatever the machine happens to offer, and the BearSSL# it is written on.  All three builds below have to be silent, so what tells# them apart is which implementation each one's dispatch chose -- which the# driver prints and run_one checks, since otherwise a build that quietly took# the wrong path would pass by saying nothing.aes_src="cbits/crypton_aes.c cbits/aes/generic.c cbits/aes/gf.c         cbits/bearssl/aes_ct64.c cbits/bearssl/aes_ct64_enc.c         cbits/bearssl/aes_ct64_dec.c cbits/bearssl/ghash_ctmul64.c         cbits/bearssl/dec32le.c"# The same driver again against the instructions, on each architecture that# has them.armv8_src="$aes_src cbits/aes/armv8.c cbits/crypton_cpu.c"armv8_inc="-DWITH_ARMV8_CRYPTO -march=armv8-a+crypto -Icbits/aes"x86ni_src="$aes_src cbits/aes/x86ni.c cbits/crypton_cpu.c           cbits/aes/gcm_vaes_x86.c cbits/aes/gcm_vaes512_x86.c"x86ni_inc="-DWITH_AESNI -DWITH_PCLMUL -maes -mpclmul -mssse3 -msse4.1 -Icbits/aes"status=0have_valgrind=noct_define=if command -v valgrind > /dev/null 2>&1; then	have_valgrind=yes	ct_define=-DCRYPTON_CT_VALGRINDfi# MLK_CONFIG_CT_TESTING_ENABLED turns on mlkem-native's own secret and# declassify annotations, which are in the source at the points the algorithm# says a value becomes public.  The important one is the public seed: it is# derived from the key generation seed, so it starts out secret, and the# matrix it then seeds is generated by rejection sampling, which branches on# the bytes it draws.  Upstream declassifies the seed first -- rho travels in# the encapsulation key and is not secret -- and without that the driver# reports a leak that is not one.  Those points are maintained by the people# whose proofs cover this code, so they are a better answer than marking by# hand from outside.## Only with valgrind, since the macros include valgrind/memcheck.h.mlkem_src="cbits/mlkem/crypton_mlkem.c"mlkem_inc="-Icbits/mlkem${ct_define:+ -DMLK_CONFIG_CT_TESTING_ENABLED}"# And the hand-written backend, where the architecture has one.  Both builds# have to be silent: unlike the AES pair below there is no variable-time# implementation here for one of them to expose.## crypton_cpu.c because the backend asks it whether this processor has the# instructions before using them; without it the link fails on the# capability function in cbits/mlkem/crypton_mlkem.h.  cpu_stub.c is the one# symbol that file needs from the AES build, which is not wanted here.mlkem_native_src="$mlkem_src cbits/mlkem/crypton_mlkem_asm.S cbits/crypton_cpu.c \                  cbits/tests/ct/cpu_stub.c"mlkem_native_inc="$mlkem_inc -DCRYPTON_MLKEM_NATIVE_BACKEND"# Does this processor have AES instructions at all?## Asked of the system rather than of crypton, because the question being# settled is whether crypton found what is there -- and a witness that comes# from the code under test cannot answer that.  "Cannot tell" counts as# having them, so an unfamiliar system turns into a loud failure rather than# a silent skip.## CRYPTON_CT_CPUINFO is for testing this function itself; nothing else sets# it.machine_has_aes() {	cpuinfo=${CRYPTON_CT_CPUINFO:-/proc/cpuinfo}	if [ -r "$cpuinfo" ]; then		# "aes" in Features on ARM, in flags on x86; -w so that a model		# name containing the letters does not answer for the flags		grep -qw aes "$cpuinfo"		return	fi	if [ "$(uname -s)" = Darwin ]; then		[ "$(sysctl -n hw.optional.arm.FEAT_AES 2>/dev/null)" = 1 ] && return 0		[ "$(sysctl -n hw.optional.aes 2>/dev/null)" = 1 ] && return 0		return 1	fi	return 0}# run_one <name> <sources> <includes> [driver]## The driver defaults to ct_<name>.c.  It is given separately where one# driver is built twice -- the same question asked of the portable C and of# the hand-written backend -- rather than copying the file to a second name.run_one() {	name=$1; srcs=$2; inc=$3; drv=${4:-$1}	# shellcheck disable=SC2086	$cc -O2 -g $ct_define -Icbits -Icbits/include64 $inc \		-o "$out/$name" "cbits/tests/ct/ct_$drv.c" $srcs 2> "$out/$name.cc" || {		echo "FAIL $name did not build"; sed -n '1,12p' "$out/$name.cc"; status=1; return	}	# Which implementation did this build's dispatch actually take?  The	# driver says, and the three AES builds want different answers.  This	# used to be inferred: the portable build was required to report,	# because the tables leaked, and silence meant it had taken an	# accelerated path instead.  The tables are gone and all three are	# silent now, so the question is asked outright rather than read off a	# leak that no longer happens.	case $name in	aes | aes_armv8 | aes_x86ni)		want=0		[ "$name" = aes ] || want=1		got=$("$out/$name" 2>/dev/null | sed -n 's/^dispatch aes=\([01]\).*/\1/p')		if [ "$got" != "$want" ]; then			# A processor with no AES instructions is not a failure of			# this driver; there is simply nothing here to measure, and			# the portable one above has already measured what does run.			# Boards in this position are common -- the Raspberry Pi 2,			# 3 and 4 among them, on either word size.			if [ "$want" = 1 ] && [ "$got" = 0 ] && ! machine_has_aes; then				echo "skip $name: this processor has no AES instructions,"				echo "     so there is nothing here to measure"				return			fi			echo "FAIL $name: dispatch took aes=$got, wanted aes=$want --"			echo "     this build is not running the implementation it is named for"			status=1			return		fi		;;	powm_s2n_*)		got=$("$out/$name" 2>/dev/null | sed -n 's/^dispatch powm_s2n=\([0-9]*\).*/\1/p')		if [ -z "$got" ]; then			echo "FAIL $name: it did not run to the end on this processor"			status=1			return		elif [ "$got" -eq 0 ]; then			echo "FAIL $name: the s2n-bignum reduction ran 0 times --"			echo "     this build checked the C again, not the path it is named for"			status=1			return		fi		;;	esac	if [ "$have_valgrind" = no ]; then		"$out/$name" > /dev/null 2>&1 && echo "built $name (no valgrind here; nothing checked)" \			|| { echo "FAIL $name did not run"; status=1; }		return	fi	# in a subshell, so that a driver valgrind kills is not announced by	# this shell as well -- the log says what happened	( valgrind --error-exitcode=0 --track-origins=yes --num-callers=20 \		--log-file="$out/$name.log" "$out/$name" > /dev/null 2>&1 ) \		2> /dev/null || true	# A driver valgrind could not run to the end reports nothing, and nothing	# used to read as a pass.  An instruction valgrind cannot decode is the	# likely one: the vendored ADX assembly needs a valgrind that knows it.	if grep -q "unhandled instruction\|Illegal instruction\|SIGILL" "$out/$name.log"; then		case $name in		powm_s2n_*)			echo "skip $name: this valgrind cannot decode the vendored assembly's"			echo "     instructions, so it cannot run the path this build is for"			;;		*)			echo "FAIL $name: valgrind could not run it to the end"			grep -m3 "unhandled instruction\|Illegal instruction\|SIGILL" "$out/$name.log" | sed 's/^/    /'			status=1			;;		esac		return	fi	n=$(grep -c "^==[0-9]*== \(Conditional jump\|Use of uninitialised\)" "$out/$name.log" || true)	# Which places did it name?  Only the frame the report is against -- the	# first "at" line under the complaint -- is the place; the "by" lines	# below it are how the code got there and are not themselves branching	# on anything.  Nor is the "at" line under "Uninitialised value was	# created by", which --track-origins prints to say where the value came	# from: that frame is a stack allocation, not a branch, and taking it	# for one put a function's opening brace on the list.	#	# A site is kept as "function@file:line", and known.txt may name any of	# the three widths: that exact line, the function, or the whole file.	# The function is there because the line is the compiler's choice and	# not the code's: gcc 14 names no line of addM's loop at all, gcc 11.5	# on x86-64 names three, and clang on AArch64 named two of those three.	# What the disassembly establishes is a property of the function, so	# that is what an entry should be able to say.	sites=$(awk '		/^==[0-9]*== (Conditional jump|Use of uninitialised)/ { want = 1; next }		want && /^==[0-9]*==    at 0x/ { print; want = 0 }	' "$out/$name.log" |		sed -n 's/^==[0-9]*==    at 0x[0-9A-Fa-f]*: \([A-Za-z_0-9.]*\) (\([^)]*\))$/\1@\2/p' |		grep -v '@ct_' | sort -u)	unknown=	for site in $sites; do		funcfile=${site%%:*}		fileline=${site#*@}		file=${fileline%%:*}		if grep -q "^$fileline[[:space:]]" cbits/tests/ct/known.txt ||		   grep -q "^$funcfile[[:space:]]" cbits/tests/ct/known.txt ||		   grep -q "^$file[[:space:]]" cbits/tests/ct/known.txt; then			continue		fi		unknown="$unknown $site"	done	case $name in	canary)		# the calibration: silence here would mean the marking never reached		# the code, and every other zero in this run would be worthless		if [ "$n" -eq 0 ]; then			echo "FAIL canary: the deliberately leaky driver reported nothing,"			echo "     so the marking is not reaching the code and nothing below counts"			status=1		else			echo "ok   canary: reported $n, so the marking works"		fi		;;	aes | aes_armv8 | aes_x86ni)		# Nothing here looks anything up: the portable AES and GHASH are		# bitsliced, and the two instruction sets branch on nothing.  So		# anything at all is a finding and known.txt does not apply --		# until the tables went this entry read the other way round, with		# the portable build required to report.		if [ "$n" -eq 0 ]; then			echo "ok   $name: the secret decided nothing"		else			echo "FAIL $name: $n report(s) from AES or GHASH,"			echo "     which should branch on nothing:"			for site in $sites; do echo "         $site"; done			sed -n '/Conditional jump\|Use of uninitialised/,/^==[0-9]*== $/p' \				"$out/$name.log" | head -30 | sed 's/^/    /'			status=1		fi		;;	*)		if [ "$n" -eq 0 ]; then			echo "ok   $name: the secret decided nothing"		elif [ -z "$unknown" ]; then			echo "ok   $name: $n report(s), all known -- $(echo "$sites" | tr '\n' ' ')"		else			echo "REPORT $name: the secret decided a branch or an address"			echo "       somewhere not listed in cbits/tests/ct/known.txt:"			for site in $unknown; do echo "         $site"; done			sed -n '/Conditional jump\|Use of uninitialised/,/^==[0-9]*== $/p' \				"$out/$name.log" | head -30 | sed 's/^/    /'			status=1		fi		;;	esac}# The calibration first: it must report, or nothing below means anything.run_one canary  "" ""run_one powm    "cbits/crypton_powm.c" ""# The same exponentiation through s2n-bignum, which is what RSA signing runs# on x86-64 with ADX.  The build above is the C: it does not define# CRYPTON_S2N_BIGNUM, so from the release the assembly went in until this# was added, the path that actually runs was never put through this# harness.  Each size is a different routine -- Karatsuba at 1024 and 2048,# a padded operand through the 2048-bit one at 1536, schoolbook at 3072 and# 4096 -- so each is built.## ADX is assumed by CRYPTON_CPU_TESTING_ADX, and only where the processor# reports it: under valgrind the processor's answers are valgrind's, and# what this is for is the assembly, not valgrind's opinion of the# processor.  The driver counts the reductions it did through the assembly,# and a count of nothing fails the build rather than passing it.case $(uname -m) inx86_64 | amd64)	if grep -qw adx "${CRYPTON_CT_CPUINFO:-/proc/cpuinfo}" 2>/dev/null; then		s2n=cbits/s2n/x86_att		powm_s2n_src="cbits/crypton_powm.c cbits/crypton_cpu.c cbits/tests/ct/cpu_stub.c			$s2n/bignum_montifier.S $s2n/bignum_emontredc_8n.S			$s2n/bignum_kmul_16_32.S $s2n/bignum_ksqr_16_32.S			$s2n/bignum_kmul_32_64.S $s2n/bignum_ksqr_32_64.S			$s2n/bignum_sqr_8_16.S $s2n/bignum_mul_8_16.S			$s2n/bignum_mul.S $s2n/bignum_sqr.S"		powm_s2n_inc="-Icbits/s2n/include -DCRYPTON_S2N_BIGNUM			-DCRYPTON_CPU_TESTING_ADX -DCT_POWM_COUNT_S2N			-Wl,--wrap=bignum_emontredc_8n"		for bytes in 128 192 256 384 512; do			run_one "powm_s2n_$((bytes * 8))" "$powm_s2n_src" \				"$powm_s2n_inc -DCT_POWM_BYTES=$bytes" powm		done	else		echo "skip powm_s2n: this processor reports no ADX, so the vendored"		echo "     multiplication cannot run here"	fi	;;aarch64 | arm64)	# the generated squaring is separate code at each size; 2048 is above	for bytes in 128 192; do		run_one "powm_$((bytes * 8))" "cbits/crypton_powm.c" \			"-DCT_POWM_BYTES=$bytes" powm	done	;;esacrun_one p256    "cbits/p256/p256.c cbits/p256/p256_ec.c" ""run_one x25519  "cbits/curve25519/curve25519-donna-c64.c" ""run_one ed25519 "cbits/ed25519/ed25519.c cbits/crypton_sha512.c" "-Icbits/ed25519"run_one decaf   "$decaf_src" "$decaf_inc"run_one chapoly "cbits/crypton_chacha.c cbits/crypton_poly1305.c" ""run_one mlkem   "$mlkem_src" "$mlkem_inc"run_one aes     "$aes_src" ""# Only where the instructions exist.  Elsewhere there is nothing to measure# and the build would not even compile.case $(uname -m) inaarch64 | arm64)	run_one aes_armv8 "$armv8_src" "$armv8_inc" aes	;;x86_64 | amd64)	run_one aes_x86ni "$x86ni_src" "$x86ni_inc" aes	;;esac# The ML-KEM backend, on both architectures that have one.  x86-64 wants the# flags its own build system passes, or the AVX2 sources do not assemble.## The backend now asks the processor before it runs, so on x86-64 without# AVX2 this build measures the portable C under a name that says otherwise.# Say so and skip, rather than pass while checking the thing the plain# mlkem build above has already checked.case $(uname -m) inaarch64 | arm64)	run_one mlkem_native "$mlkem_native_src" "$mlkem_native_inc" mlkem	;;x86_64 | amd64)	if grep -qw avx2 "${CRYPTON_CT_CPUINFO:-/proc/cpuinfo}" 2>/dev/null; then		run_one mlkem_native "$mlkem_native_src" "$mlkem_native_inc -mavx2 -mbmi2" mlkem	else		echo "skip mlkem_native: this processor has no AVX2, so the backend"		echo "     would fall back to the C the mlkem build above measured"	fi	;;esacif [ "$have_valgrind" = no ]; then	echo "skip no valgrind here, so none of the above was checked"	exit 0fiexit $status