summaryrefslogtreecommitdiff
path: root/test/check-implementation.sh
blob: 5f46606c5182b661a18facf1f3dc25be7b34d6ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash

set -eu
shopt -s extglob

# Run NIST's genkat_aead.c against the reference implementation as
# well as another one, and compare vectors.

TEST_DIR=$(dirname $0)
ROOT_DIR=${TEST_DIR}/..

implem=$1


list-implementation-files ()
{
    local mode=$1
    local key_length=$2
    local src_dir=${ROOT_DIR}/src

    # src/${implem} can contain arbitrary files; we need to copy
    # everything save for the unused AE mode.

    for f in ${src_dir}/${implem}/!(lilliput-i|lilliput-ii).[ch]
    do
        echo ${f}
    done

    echo ${src_dir}/${implem}/lilliput-${mode}.c
    echo ${src_dir}/${mode}-${key_length}/parameters.h
}

run-genkat ()
{
    local tmp_dir=$1
    local implem=$2
    local mode=$3
    local keylen=$4
    echo -e "    ${mode}-${keylen}…"

    local src_dir=${ROOT_DIR}/src
    local vectors_dir=${tmp_dir}/vectors/${implem}
    local genkat_dir=${tmp_dir}/genkat/${implem}/${mode}-${keylen}
    local genkat=${genkat_dir}/genkat

    local source_files=(
        cipher.{c,h}
        constants.h
        lilliput-ae{.h,-utils.h}
        lilliput-${mode}.c
        tweakey.{c,h}
    )

    mkdir -p ${genkat_dir}  # "-p" allows comparing ref against ref.

    list-implementation-files ${mode} ${keylen} | xargs cp -t ${genkat_dir}

    cp ${ROOT_DIR}/nist/{api.h,encrypt.c} ${genkat_dir}
    cp ${ROOT_DIR}/nist/TestVectorGen/* ${genkat_dir}

    local nist_flags=(-std=c99 -Wall -Wextra -Wshadow -fsanitize=address,undefined -O2)

    gcc ${nist_flags[@]} -I${genkat_dir} ${genkat_dir}/*.c -o ${genkat}

    ${genkat}
    mv LWC_AEAD_KAT*.txt ${vectors_dir}/${mode}-${keylen}
}


test-implem ()
{
    local tmp_dir=$1
    local implem=$2
    echo "Generating vectors for implementation ${implem}…"

    mkdir -p ${tmp_dir}/{genkat,vectors}/${implem}

    local mode
    local keylen

    for mode in i ii
    do
        for keylen in 128 192 256
        do
            run-genkat ${tmp_dir} ${implem} ${mode} ${keylen}
        done
    done
}


tmp_dir=$(mktemp -d)

test-implem ${tmp_dir} ref
test-implem ${tmp_dir} ${implem}

diff --brief --new-file --recursive ${tmp_dir}/vectors/ref ${tmp_dir}/vectors/${implem}