sfinx

sphincs+ inspired signing utility
git clone git://git.finwo.net/app/sfinx
Log | Files | Refs | README | LICENSE

test_embed.sh (6337B)


      1 #!/bin/sh
      2 # Reads export.mk for the library source list, compiles a program that includes
      3 # only sfinx.h against that list plus keccak-fast, then exercises the crypto and
      4 # the key format API. No CLI, no util, no argparse is reachable.
      5 set -eu
      6 HERE=$(cd "$(dirname "$0")" && pwd)
      7 ROOT=$(cd "${HERE}/.." && pwd)
      8 . "${HERE}/_tap.sh"
      9 
     10 CC=${CC:-cc}
     11 HASH_FIRST="${ROOT}/lib/finwo/keccak-fast/src/keccak-fast.c"
     12 HASH="${HASH_FIRST} ${ROOT}/lib/finwo/keccak-fast/src/backend/scalar.c ${ROOT}/lib/finwo/keccak-fast/src/backend/scalar_bmi.c ${ROOT}/lib/finwo/keccak-fast/src/backend/avx2.c ${ROOT}/lib/finwo/keccak-fast/src/backend/avx512.c"
     13 
     14 tap_begin "embed: library stands alone"
     15 
     16 if [ -f "${ROOT}/src/sfinx.h" ] && [ -f "${ROOT}/src/sfinx.c" ]; then
     17   tap 1 "sources present" ""
     18 else
     19   tap 0 "sources present" "missing src/sfinx.h or src/sfinx.c"
     20 fi
     21 
     22 bad=$(grep -E '^[[:space:]]*#include' "${ROOT}/src/sfinx.h" |
     23   grep -vE '^[[:space:]]*#include[[:space:]]+<(stddef|stdint|sys/types)\.h>' || true)
     24 if [ -z "${bad}" ]; then
     25   tap 1 "sfinx.h includes only stddef, stdint and sys/types" ""
     26 else
     27   tap 0 "sfinx.h includes only stddef, stdint and sys/types" "${bad}"
     28 fi
     29 
     30 if [ ! -f "${HASH_FIRST}" ]; then
     31   (cd "${ROOT}" && dep install) >/dev/null 2>&1 || true
     32 fi
     33 if [ -f "${HASH_FIRST}" ] && [ -f "${ROOT}/lib/.dep/include/finwo/keccak-fast.h" ]; then
     34   tap 1 "root dep installed" ""
     35 else
     36   tap 0 "root dep installed" "dep install did not provide keccak-fast"
     37 fi
     38 
     39 TMP=$(mktemp -d)
     40 trap 'rm -rf "${TMP}"' EXIT
     41 
     42 # export.mk, templated the way dep does it, expanded the way make does it
     43 sed "s|{{module.dirname}}|${ROOT}|g" "${ROOT}/export.mk" >"${TMP}/export.mk"
     44 printf 'include %s\nprobe:\n\t@echo $(SRC)\n' "${TMP}/export.mk" >"${TMP}/probe.mk"
     45 LIB_SRCS=$(make -s -f "${TMP}/probe.mk" probe 2>/dev/null || true)
     46 if [ -n "${LIB_SRCS}" ]; then
     47   tap 1 "export.mk yields library sources" ""
     48 else
     49   tap 0 "export.mk yields library sources" "probe produced nothing"
     50 fi
     51 
     52 missing=""
     53 for f in ${LIB_SRCS}; do
     54   [ -f "$f" ] || missing="${missing} ${f}"
     55 done
     56 if [ -z "${missing}" ]; then
     57   tap 1 "library sources exist" ""
     58 else
     59   tap 0 "library sources exist" "missing:${missing}"
     60 fi
     61 
     62 bad=""
     63 for f in ${LIB_SRCS}; do
     64   hit=$(grep -E '^[[:space:]]*#include' "$f" | grep -E 'cli/|util/|argparse|log\.h' || true)
     65   [ -z "${hit}" ] || bad="${bad}${f} ${hit}; "
     66 done
     67 if [ -z "${bad}" ]; then
     68   tap 1 "library sources have no util includes" ""
     69 else
     70   tap 0 "library sources have no util includes" "${bad}"
     71 fi
     72 
     73 cat >"${TMP}/embed.c" <<'EOF'
     74 #include "sfinx.h"
     75 
     76 #include <stdio.h>
     77 #include <string.h>
     78 
     79 static int fail(const char *stage) {
     80   fprintf(stderr, "embed: %s\n", stage);
     81   return 1;
     82 }
     83 
     84 int main(void) {
     85   static const uint8_t seed[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
     86                                    0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
     87                                    0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
     88   static const uint8_t path[2] = {0x12, 0x34};
     89   static const uint8_t msg[]   = "sfinx embed round trip";
     90   uint8_t              pub[SFINX_HASH_LEN_MAX];
     91   uint8_t              sig[4096];
     92   uint8_t              tamper[4096];
     93   uint8_t              hdr[4096];
     94   uint8_t              sigtext[8192];
     95   uint8_t              sigback[4096];
     96   size_t               pub_len     = 0;
     97   size_t               sig_len     = 0;
     98   size_t               hdr_len     = 0;
     99   size_t               sigtext_len = 0;
    100   size_t               sigback_len = 0;
    101   size_t               msg_len     = sizeof(msg) - 1;
    102   sfinx_key            key;
    103   sfinx_key            reread;
    104   sfinx_status         status;
    105 
    106   if (sfinx_tree_public_key(SFINX_HASH_256, seed, sizeof(seed), sizeof(path), pub, sizeof(pub), &pub_len) != SFINX_OK) {
    107     return fail("tree_public_key");
    108   }
    109   if (sfinx_tree_sign(SFINX_HASH_256, seed, sizeof(seed), path, sizeof(path), msg, msg_len, sig, sizeof(sig),
    110                       &sig_len) != SFINX_OK) {
    111     return fail("tree_sign");
    112   }
    113   if (sfinx_verify(pub, pub_len, msg, msg_len, sig, sig_len) != SFINX_OK) {
    114     return fail("verify round trip");
    115   }
    116 
    117   memcpy(tamper, sig, sig_len);
    118   tamper[1] ^= 0x01;
    119   if (sfinx_verify(pub, pub_len, msg, msg_len, tamper, sig_len) == SFINX_OK) {
    120     return fail("tampered path accepted");
    121   }
    122 
    123   status = sfinx_key_decode(seed, sizeof(seed), "raw", &key);
    124   if (status != SFINX_OK) {
    125     return fail("decode raw");
    126   }
    127   key.path_len = sizeof(path);
    128   if (sfinx_key_derive(&key) != SFINX_OK) {
    129     return fail("derive");
    130   }
    131   if (key.pub_len != pub_len || memcmp(key.pub, pub, pub_len) != 0) {
    132     return fail("derived pub mismatch");
    133   }
    134   if (sfinx_key_encode_len(&key, "hdr") == 0) {
    135     return fail("encode_len hdr");
    136   }
    137   if (sfinx_key_encode(&key, "hdr", hdr, sizeof(hdr), &hdr_len) != SFINX_OK) {
    138     return fail("encode hdr");
    139   }
    140   status = sfinx_key_decode(hdr, hdr_len, NULL, &reread);
    141   if (status != SFINX_OK) {
    142     return fail("decode hdr auto");
    143   }
    144   if (reread.pub_len != pub_len || memcmp(reread.pub, pub, pub_len) != 0) {
    145     return fail("hdr pub mismatch");
    146   }
    147   sigtext_len = sfinx_sig_encode_len(sig, sig_len, "hdr");
    148   if (sigtext_len == 0 || sigtext_len > sizeof(sigtext)) {
    149     return fail("sig encode_len");
    150   }
    151   if (sfinx_sig_encode(sig, sig_len, "hdr", sigtext, sizeof(sigtext), &sigtext_len) != SFINX_OK) {
    152     return fail("sig encode hdr");
    153   }
    154   if (sfinx_sig_decode(sigtext, sigtext_len, NULL, sigback, sizeof(sigback), &sigback_len) != SFINX_OK) {
    155     return fail("sig decode auto");
    156   }
    157   if (sigback_len != sig_len || memcmp(sigback, sig, sig_len) != 0) {
    158     return fail("sig round trip");
    159   }
    160   sfinx_key_free(&key);
    161   sfinx_key_free(&reread);
    162   return 0;
    163 }
    164 EOF
    165 
    166 if "${CC}" -Wall -Wextra -O2 -I"${ROOT}/src" -I"${ROOT}/lib/.dep/include" "${TMP}/embed.c" ${LIB_SRCS} \
    167   ${HASH} -o "${TMP}/embed" >"${TMP}/build.log" 2>&1; then
    168   tap 1 "embed program builds against the library only" ""
    169 else
    170   tap 0 "embed program builds against the library only" "$(cat "${TMP}/build.log")"
    171 fi
    172 
    173 rc=0
    174 if [ -x "${TMP}/embed" ]; then
    175   "${TMP}/embed" >"${TMP}/run.log" 2>&1 || rc=$?
    176 else
    177   rc=127
    178 fi
    179 if [ "${rc}" = 0 ]; then
    180   tap 1 "embed crypto and format round trip" ""
    181 else
    182   tap 0 "embed crypto and format round trip" "exit ${rc}: $(cat "${TMP}/run.log" 2>/dev/null || true)"
    183 fi
    184 
    185 tap_plan