xref: /freebsd/crypto/libecc/scripts/test_ec_utils.sh (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1#/*
2# *  Copyright (C) 2021 - This file is part of libecc project
3# *
4# *  Authors:
5# *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6# *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7# *
8# *  This software is licensed under a dual BSD and GPL v2 license.
9# *  See LICENSE file at the root folder of the project.
10# */
11#!/bin/bash
12
13BASEDIR=$(dirname "$0")
14EC_UTILS=$BASEDIR/../build/ec_utils
15
16# trap ctrl-c and call ctrl_c()
17trap ctrl_c INT
18
19function ctrl_c() {
20    echo "** Trapped CTRL-C, cleaning ..."
21    rm -f test_key_public_key.bin test_key_private_key.bin test_key_private_key.h test_key_public_key.h signed_file.bin.signed
22    exit
23}
24
25# Test ec_utils cases
26curves=("FRP256V1" "SECP192R1" "SECP224R1" "SECP256R1" "SECP384R1" "SECP521R1" "BRAINPOOLP192R1" "BRAINPOOLP224R1" "BRAINPOOLP256R1" "BRAINPOOLP384R1" "BRAINPOOLP512R1" "GOST256" "GOST512" "SM2P256TEST" "SM2P256V1" "WEI25519" "WEI448" "GOST_R3410_2012_256_PARAMSETA" "SECP256K1")
27signatures=("ECDSA" "ECKCDSA" "ECSDSA" "ECOSDSA" "ECFSDSA" "ECGDSA" "ECRDSA" "SM2" "EDDSA25519" "EDDSA25519CTX" "EDDSA25519PH" "EDDSA448" "EDDSA448PH" "DECDSA")
28hashes=("SHA224" "SHA256" "SHA384" "SHA512" "SHA512_224" "SHA512_256" "SHA3_224" "SHA3_256" "SHA3_384" "SHA3_512" "SM3" "SHAKE256" "STREEBOG256" "STREEBOG512")
29
30for c in "${!curves[@]}"
31do
32    for s in "${!signatures[@]}"
33    do
34        # Generate keys
35        # NOTE: EDDSA family only accepts WEI curves
36        if [[ "${signatures[s]}" == "EDDSA25519" || "${signatures[s]}" == "EDDSA25519CTX" || "${signatures[s]}" == "EDDSA25519PH" ]]
37        then
38            if [[ "${curves[c]}" != "WEI25519" ]]
39            then
40                continue
41            fi
42        fi
43        if [[ "${signatures[s]}" == "EDDSA448" || "${signatures[s]}" == "EDDSA448PH" ]]
44        then
45            if [[ "${curves[c]}" != "WEI448" ]]
46            then
47                continue
48            fi
49        fi
50        echo "===== ${curves[c]} ${signatures[s]}"
51        $EC_UTILS gen_keys ${curves[c]} ${signatures[s]} test_key || exit 0
52        for h in "${!hashes[@]}"
53        do
54            if [[ "${signatures[s]}" == "EDDSA25519" || "${signatures[s]}" == "EDDSA25519CTX" || "${signatures[s]}" == "EDDSA25519PH" ]]
55            then
56                if [[ "${hashes[h]}" != "SHA512" ]]
57                then
58                    continue
59                fi
60            fi
61            if [[ "${signatures[s]}" == "EDDSA448" || "${signatures[s]}" == "EDDSA448PH" ]]
62            then
63                if [[ "${hashes[h]}" != "SHAKE256" ]]
64                then
65                    continue
66                fi
67            fi
68            echo "========= TESTING ${curves[c]} ${signatures[s]} ${hashes[h]}"
69            # Try to sign
70            $EC_UTILS sign ${curves[c]} ${signatures[s]} ${hashes[h]} $EC_UTILS test_key_private_key.bin signed_file.bin.signed "ANCILLARY" || exit 0
71            # Try to verify
72            $EC_UTILS verify ${curves[c]} ${signatures[s]} ${hashes[h]} $EC_UTILS test_key_public_key.bin signed_file.bin.signed "ANCILLARY"  || exit 0
73            rm -f signed_file.bin.signed
74            # Try to "struct" sign
75            $EC_UTILS struct_sign ${curves[c]} ${signatures[s]} ${hashes[h]} $EC_UTILS test_key_private_key.bin signed_file.bin.signed IMAGE_TYPE0 1337 "ANCILLARY"  || exit 0
76            # Try to "struct" verify
77            $EC_UTILS struct_verify ${curves[c]} ${signatures[s]} ${hashes[h]} signed_file.bin.signed test_key_public_key.bin "ANCILLARY"  || exit 0
78            rm -f signed_file.bin.signed
79        done
80        rm -f test_key_public_key.bin test_key_private_key.bin test_key_private_key.h test_key_public_key.h
81    done
82done
83