#!/bin/sh

set -e

GEN_DUMP="debian/tests/helpers/gen_rsa_dump.sh"

# Defines the RSA private key-material, used to generate dump
# and to serve as target values
PRIV_EXP="17 e6 c5 42 14 a8 dc dc 75 85 ca 88 34 aa f7 96 a7 97 49 66 07 aa 76 60 15 38 59 d0 2b a3 99 a5"
PRIME_1="00 d0 98 97 8c de 12 b9 e1 01 b5 19 d4 2c a5 4e 7d"
PRIME_2="00 a8 b6 9b 77 f5 93 ca 8a 28 0d cd 7d 75 7d 95 97" 
PUB_EXP="01 00 01"

# Combination of the parameters to feed into the script
KEYSPEC="\
PRIV_EXP=${PRIV_EXP}
PRIME_1=${PRIME_1}
PRIME_2=${PRIME_2}
PUB_EXP=${PUB_EXP}
"

# Memory dump to process
DMP="${AUTOPKGTEST_TMP}/rsa-256.dmp"

# Dump memory of process, which generates key material
sh ${GEN_DUMP} "$KEYSPEC" "$DMP"

# Performs key extraction
ACTUAL=$(rsakeyfind "${DMP}" | tr -d "\n" )

# Checks, compare actual result with target values
echo "${ACTUAL}" | grep -q "${PRIV_EXP}"
echo "${ACTUAL}" | grep -q "${PRIME_1}"
echo "${ACTUAL}" | grep -q "${PRIME_2}"
echo "${ACTUAL}" | grep -q "${PUB_EXP}"

# Clean up
rm ${DMP}

exit 0
