#! /bin/sh
#
# FakeMAC server-side example in Bourne shell (or maybe bash)
# Copyright (C) 2021 Jean Zee (a.k.a. CmpZ)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# Documentation is at http://cybertiggyr.com/fakemac.html
#

# I've packaged this FakeMAC server-side code into a test.
# On the command line, you give it the FakeMAC code & the name
# of a file that contains the message.  It tells you whether
# the message is authenticate.  If it is, you must supply
# code that decodes the message & acts on it.

Secret=$1
Message=$2
Received=$3

echo Secret is \"$Secret\"
echo Message is \"$Message\"
echo Received is \"$Received\"

O_PAD=`./sha1.sh "${Secret}ooo"`
I_PAD=`./sha1.sh "${Secret}iii"`

echo "Received:" $Received
#
# Start with I_PAD + Message
Actual=${I_PAD}$Message
# SHA1 of that
Actual=`./sha1.sh "$Actual"`
# From this point on, we know that Actual is concatenation of
# strings containing only hex values, so there is no need to
# quote it when calling SHA1.
# Concat O_PAD with previous
Actual=${O_PAD}$Actual
# Finally, hash of that
Actual=`./sha1.sh $Actual`
echo "Actual:  " $Actual

if test "$Actual" = "$Received"
then
    echo good
    exit 0
else
    echo bad
    exit 1
fi
