#!/bin/bash

CIPHER="$1"	# cipher to use
KATFILE="$2"	# file with known-answer-test values in NIST's
		# ecb_tbl.txt format should be given on stdin.


[ "$#" -ne 2 ] && cat <<EOF && exit 1
usage: aes-test ciphername ecb_tbl.txt

    where ciphername is a cipher known to the cryptoapi and
    ecb_tbl.txt is a file with known-answer test values in the
    format that NIST required from all of the AES submitters:

    It consists of a series of four-line blocks:

    I=...    (test number)
    KEY=...  (key in ascii hex)
    PT=...   (plain text in ascci hex)
    CT=...   (ciphertext in ascii text)

    (in this order or at least with CT= as the last one of them),
    delimited by blank lines. A line of the form

    KEYSIZE=number

    is used to indicate the key length in bits, but is ignored
    by this program.
EOF

KEYSIZE=0
I=0
KEY=""
PT=""
CT=""
oCT=""

grep -E '^I=|^KEY=|^KEYSIZE=|^PT=|^CT=' "$KATFILE" | \
while read; do
    eval "$REPLY"
    if [ "$CT" != "$oCT" ]; then
	echo -n "Test ${I} @ ${KEYSIZE} bits: "
	(./testcip -c "$CIPHER" -k "$KEY" -p "$PT" -e "$CT" \
				    && echo OK || echo FAILED) | \
	grep -vE '^Registered|^MD5'
	oCT="$CT"
    fi
done
