#!/bin/sh

test_description="Check signing"
. $SHARNESS_TEST_SRCDIR/sharness.sh

echo "gost3410-512C
gost3410-256A
ed25519-blake2b
slh-dsa-shake-256s" | while read keyalgo ; do

sub="-sub what=ever"
typ="some-different-type"
test_expect_success "$keyalgo: pub generation" "cmkeytool \
    -algo $keyalgo -ku sig $sub \
    5>sign.$keyalgo.pub 9>sign.$keyalgo.prv"
dd if=/dev/urandom of=sign.$keyalgo.data bs=300K count=1 2>/dev/null
encTo=$(dd if=/dev/urandom bs=32 count=1 2>/dev/null | xxd -c 0 -p)
badEncTo=$(dd if=/dev/urandom bs=32 count=1 2>/dev/null | xxd -c 0 -p)
encTo="-encrypted-to $encTo"
badEncTo="-encrypted-to $badEncTo"

for merkle in "" "-merkle" ; do

algo=${keyalgo}${merkle}
test_expect_success "$algo: signing" "
    cmsigtool $merkle -t $typ $encTo \
    4<sign.$keyalgo.pub 8<sign.$keyalgo.prv \
    <sign.$keyalgo.data >sign.$algo.sig"
test_expect_success "$algo: verifying" "
    cmsigtool -v -t $typ 4<sign.$keyalgo.pub \
    <sign.$algo.sig >sign.data.got"
test_expect_success "$algo: comparing" \
    "test_cmp sign.$keyalgo.data sign.data.got"
test_expect_success "$algo: differing type" "
    ! cmsigtool -v 4<sign.$keyalgo.pub <sign.$algo.sig >/dev/null"
test_expect_success "$algo: good encTo" "
    ! cmsigtool -v $encTo 4<sign.$keyalgo.pub <sign.$algo.sig >/dev/null"
test_expect_success "$algo: bad encTo" "
    ! cmsigtool -v $badEncTo 4<sign.$keyalgo.pub <sign.$algo.sig >/dev/null"

test_expect_success "$algo: detached signing" "
    cmsigtool -d $merkle -t $typ \
    4<sign.$keyalgo.pub 8<sign.$keyalgo.prv \
    <sign.$keyalgo.data >sign.$algo.detached.sig"
test_expect_success "$algo: detached verifying" "
    cat sign.$algo.detached.sig sign.$keyalgo.data |
    cmsigtool -d -v -t $typ 4<sign.$keyalgo.pub"
test_expect_success "$algo: differing type" "
    ! cmsigtool -d -v 4<sign.$keyalgo.pub <sign.$algo.detached.sig >/dev/null"
test_expect_success "$algo: good encTo" "! cmsigtool -d \
    -v $encTo 4<sign.$keyalgo.pub <sign.$algo.detached.sig >/dev/null"
test_expect_success "$algo: bad encTo" "! cmsigtool -d \
    -v $badEncTo 4<sign.$keyalgo.pub <sign.$algo.detached.sig >/dev/null"

done

done

test_done
