calculateAverage.sh:
#!/bin/bash ### Loop Count Loop=10000 Step1Total=0 Step2Total=0 Step3Total=0 Step4Total=0 Step5Total=0 for a in `seq 1 "$Loop"` do Results=`./calculateKey_4.sh | grep "Elapsed Time"` Step1=`echo "$Results" | grep "Step 1" | awk '{print $9}'` Step1Total=$((Step1Total + Step1)) Step2=`echo "$Results" | grep "Step 2" | awk '{print $9}'` Step2Total=$((Step2Total + Step2)) Step3=`echo "$Results" | grep "Step 3" | awk '{print $9}'` Step3Total=$((Step3Total + Step3)) Step4=`echo "$Results" | grep "Step 4" | awk '{print $9}'` Step4Total=$((Step4Total + Step4)) Step5=`echo "$Results" | grep "Step 5" | awk '{print $9}'` Step5Total=$((Step5Total + Step5)) done echo "Step 1 average for testing $Loop times :$((Step1Total/Loop))" echo "Step 2 average for testing $Loop times :$((Step2Total/Loop))" echo "Step 3 average for testing $Loop times :$((Step3Total/Loop))" echo "Step 4 average for testing $Loop times :$((Step4Total/Loop))" echo "Step 5 average for testing $Loop times :$((Step5Total/Loop))"calculateKey.sh:
#!/bin/bash # CALCULATE AES CMAC DIVERSIFIED KEY FOR IOT SYSTEMS # Cagri POLAT & Kadir YILDIZ # cagripolat@erasistem.net && kadirryildiz@yahoo.com # ### SET OUTPUT VARILABLES red=`tput setaf 1` green=`tput setaf 2` reset=`tput sgr0` ### SET INPUT VARIABLES FOR SYSTEM FOR THE FIRST EXAMPLE #KEY="0123456789ABCDEF0123456789ABCDEF" #iv="00000000000000000000000000000000" #const_Zero="00000000000000000000000000000000" #const_Rb="00000000000000000000000000000087" #LinkUID="01020304050607" #AppID="3042F5" #SystemID="497A6D6972696D4B617274" ### TEST VARIABLLES FOR NXP AN10922.PDF #KEY="00112233445566778899AABBCCDDEEFF" #iv="00000000000000000000000000000000" #const_Zero="00000000000000000000000000000000" #const_Rb="00000000000000000000000000000087" #LinkUID="04782E21801D80" #AppID="3042F5" #SystemID="4E585020416275" ### SET INPUTS FOR OUR NEW DESIGN KEY="0022446688AACCEE1133557799BBDDFF" iv="00000000000000000000000000000000" const_Zero="00000000000000000000000000000000" const_Rb="00000000000000000000000000000087" LinkUID="A1A2A3A4A5A6B1B2B3B4B5B6" AppID="1D1D1D" SystemID="444555436f6d7075746572536369656e636573" ### THE MASTER KEY IS echo "${red}Master Key : $KEY${reset}" ### STEP 1. CALCULATE AES128 CMAC SUBKEYS; K0, K1, K2 echo "========== STEP 1. CALCULATE AES128 CMAC SUBKEYS; K0, K1, K2 ===========" echo "========== STARTING TO CALCULATION STEP 1 ==========" ST=`date '+%s.%N'` ### CALCULATE K0 echo "==== Calculating K0 =====" K0=`echo 0: 00000000000000000000000000000000 | xxd -r | \ openssl enc -aes-128-ecb -nopad \ -K "$KEY" | \ hexdump -v -e '/1 "%02X"'` echo "${red}K0: $K0${reset}" ### CALCULATE K1 echo "==== Calculating K1 =====" K0Low=`echo $K0 | cut -c17-32` K0High=`echo $K0 | cut -c1-16` #echo $K0High-$K0Low ##CALCULATE MSB OF K0 MSByteofK0=`echo $K0 | cut -c-2` MSBitofK0=`printf "\x$MSByteofK0" | xxd -b | cut -d' ' -f2 | cut -c1` echo "MSByteofK0: $MSByteofK0, MSBitofK0: $MSBitofK0" ##CALCULATE MSB OF K1LOW WHICH WILL BE USED IN SHIFTING MSByteofK1TempLow=`echo "$K0Low" | cut -c-2` MSBitofK1TempLow=`printf "\x$MSByteofK1TempLow" | xxd -b | cut -d' ' -f2 | cut -c1` echo "MSByteofK1TempLow=$MSByteofK1TempLow, MSBitofK1TempLow=$MSBitofK1TempLow" K1TempLow=`echo $((0x$K0Low<<1))` K1Low=`printf "%#X\n" "$K1TempLow" | cut -c3-19` K1LowwithXORHex=$((K1TempLow^0x0000000000000087)) K1LowwithXOR=`printf "%#X\n" "$K1LowwithXORHex" | cut -c3-19` K1TempTempHigh=`echo $((0x$K0High<<1))` K1TempHigh=$((K1TempTempHigh+MSBitofK1TempLow)) K1High=`printf "%#X\n" "$K1TempHigh" | cut -c3-19` if [ $MSBitofK0 -eq 1 ] then K1="$K1High$K1LowwithXOR" else K1="$K1High$K1Low" fi echo "K1 without XOR: $K1High$K1Low" echo "${red}K1: $K1${reset}" ### CALCULATE K2 echo "==== Calculating K2 =====" K1Low=`echo $K1 | cut -c17-32` K1High=`echo $K1 | cut -c1-16` ##CALCULATE MSB OF K1 MSByteofK1=`echo $K1 | cut -c-2` MSBitofK1=`printf "\x$MSByteofK1" | xxd -b | cut -d' ' -f2 | cut -c1` echo "MSByteofK1: $MSByteofK1, MSBitofK1: $MSBitofK1" ##CALCULATE MSB OF K2LOW WHICH WILL BE USED IN SHIFTING MSByteofK2TempLow=`echo "$K1Low" | cut -c-2` MSBitofK2TempLow=`printf "\x$MSByteofK2TempLow" | xxd -b | cut -d' ' -f2 | cut -c1` echo "MSByteofK2TempLow=$MSByteofK2TempLow, MSBitofK2TempLow=$MSBitofK2TempLow" K2TempLow=`echo $((0x$K1Low<<1))` K2Low=`printf "%#X\n" "$K2TempLow" | cut -c3-19` K2LowwithXORHex=$((K2TempLow^0x0000000000000087)) K2LowwithXOR=`printf "%#X\n" "$K2LowwithXORHex" | cut -c3-19` K2TempTempHigh=`echo $((0x$K1High<<1))` K2TempHigh=$((K2TempTempHigh+MSBitofK2TempLow)) K2High=`printf "%#X\n" "$K2TempHigh" | cut -c3-19` if [ $MSBitofK1 -eq 1 ] then K2="$K2High$K2LowwithXOR" else K2="$K2High$K2Low" fi echo "K2 without XOR :$K2High$K2Low" echo "${red}K2: $K2${reset}" FT=`date '+%s.%N'` echo "========== FINISHED TO CALCULATION STEP 1 ==========" STSecond=`echo $ST | cut -d'.' -f 1 | sed 's/^0*//'` FTSecond=`echo $FT | cut -d'.' -f 1 | sed 's/^0*//'` STNanoSe=`echo $ST | cut -d'.' -f 2 | sed 's/^0*//'` FTNanoSe=`echo $FT | cut -d'.' -f 2 | sed 's/^0*//'` if [ ${STSecond} -eq ${FTSecond} ] then STep1ElapsedTime="$((FTSecond-STSecond)) Seconds and $((FTNanoSe-STNanoSe)) Nanoseconds" else STep1ElapsedTime="$((FTSecond-STSecond-1)) Seconds and $((1000000-FTNanoSe+STNanoSe)) Nanoseconds" fi ### STEP 2. CALCULATING DIVERSIFICATION INPUT M echo "========== STEP 2. CALCULATING DIVERSIFICATION INPUT M ===========" echo "========== STARTING TO CALCULATION STEP 2 ==========" ST=`date '+%s.%N'` M="$LinkUID$AppID$SystemID" echo "${red}M: $M${reset}" FT=`date '+%s.%N'` echo "========== FINISHED TO CALCULATION STEP 2 ==========" STSecond=`echo $ST | cut -d'.' -f 1 | sed 's/^0*//'` FTSecond=`echo $FT | cut -d'.' -f 1 | sed 's/^0*//'` STNanoSe=`echo $ST | cut -d'.' -f 2 | sed 's/^0*//'` FTNanoSe=`echo $FT | cut -d'.' -f 2 | sed 's/^0*//'` if [ ${STSecond} -eq ${FTSecond} ] then STep2ElapsedTime="$((FTSecond-STSecond)) Seconds and $((FTNanoSe-STNanoSe)) Nanoseconds" else echo "We are here" STep2ElapsedTime="$((FTSecond-STSecond-1)) Seconds and $((1000000-FTNanoSe+STNanoSe)) Nanoseconds" fi ### STEP 3. CALCULATING CMAC INPUT D echo "========== STEP 3. CALCULATING CMAC INPUT ==========" echo "========== STARTING TO CALCULATION STEP 3 ==========" ST=`date '+%s.%N'` D="01$M" #PADDING Lenght=${#D} echo "Lenght of D: $((Lenght/2))" Padding=$((32-Lenght%32)) Pad="80" if [ $Padding -eq 0 ] then echo "No need to padding" else echo "Calculating padding" ZeroCount=$((Padding-2)) echo "ZeroCount :$ZeroCount, as bytes :$((ZeroCount/2)), with first byte (80) :$((ZeroCount/2+1))" for a in `seq 1 $ZeroCount` do Pad="$Pad""0" done echo "Pad: $Pad" fi D="$D$Pad" DLenght=$((${#D})) DLenghtByte=$((DLenght/2)) echo "${red}D: $D, Lenght of D: $((DLenghtByte))${reset}" FT=`date '+%s.%N'` echo "========== FINISHED TO CALCULATION STEP 3 ==========" STSecond=`echo $ST | cut -d'.' -f 1 | sed 's/^0*//'` FTSecond=`echo $FT | cut -d'.' -f 1 | sed 's/^0*//'` STNanoSe=`echo $ST | cut -d'.' -f 2 | sed 's/^0*//'` FTNanoSe=`echo $FT | cut -d'.' -f 2 | sed 's/^0*//'` if [ ${STSecond} == ${FTSecond} ] then STep3ElapsedTime="$((FTSecond-STSecond)) Seconds and $((FTNanoSe-STNanoSe)) Nanoseconds" else STep3ElapsedTime="$((FTSecond-STSecond-1)) Seconds and $((1000000-FTNanoSe+STNanoSe)) Nanoseconds" fi ### STEP 4. CALCULATING ACTUAL D AFTER PADDING echo "========== STEP 4. CALCULATING ACTUAL D AFTER PADDING ==========" echo "========== STARTING TO CALCULATION STEP 1 ==========" ST=`date '+%s.%N'` DHigh=`echo $D | cut -c-$((DLenght-32))` DLow=`echo $D | cut -c$((DLenghtByte+1))-$((DLenght))` DLowLow=`echo $D | cut -c$((DLenght-15))-$((DLenght))` DLowHigh=`echo $D | cut -c$((DLenght-31))-$((DLenght-16))` K2Low=`echo $K2 | cut -c17-32` K2High=`echo $K2 | cut -c-16` K1Low=`echo $K1 | cut -c17-32` K1High=`echo $K1 | cut -c-16` DLowLowHex="0x$DLowLow" DLowHighHex="0x$DLowHigh" K2LowHex="0x$K2Low" K2HighHex="0x$K2High" K1LowHex="0x$K1Low" K1LowHex="0x$K1High" echo "DHigh :$DHigh" echo "DLow :$DLow" echo "DLowHigh-DLowLow: $DLowHigh-$DLowLow" echo "K2High -K2Low : $K2High-$K2Low" ##CALCULATE MSB OF DLOWLOW WHICH WILL BE USED IN SHIFTING MSByteofDLowLow=`echo "$DLowLow" | cut -c-2` MSBitofDLowLow=`printf "\x$MSByteofDLowLow" | xxd -b | cut -d' ' -f2 | cut -c1` echo "MSByteofDLowLow=$MSByteofDLowLow, MSBitofDLowLow=$MSBitofDLowLow" if [ $Padding -eq 0 ] then DLowLowXORedTmp=$(( DLowLowHex ^ K1LowHex)) DLowLowXORed=`printf "%#X\n" "$DLowLowXORedTmp" | cut -c3-` DLowHighXORedTmp=$(( DLowHighHex ^ K1HighHex )) DLowHighXORed=`printf "%#X\n" "$DLowHighXORedTmp" | cut -c3-` DLowXORed="$DLowHighXORed$DLowLowXORed" # echo $DLowXORed else DLowLowXORedTmp=$(( DLowLowHex ^ K2LowHex)) DLowLowXORed=`printf "%#X\n" "$DLowLowXORedTmp" | cut -c3-` DLowHighXORedTmp=$(( DLowHighHex ^ K2HighHex )) DLowHighXORed=`printf "%#X\n" "$DLowHighXORedTmp" | cut -c3-` DLowXORed="$DLowHighXORed$DLowLowXORed" # echo $DLowXORed fi DActual="$DHigh$DLowXORed" echo "${red}DActual: $DActual${reset}" FT=`date '+%s.%N'` echo "========== FINISHED TO CALCULATION STEP 4 ==========" STSecond=`echo $ST | cut -d'.' -f 1 | sed 's/^0*//'` FTSecond=`echo $FT | cut -d'.' -f 1 | sed 's/^0*//'` STNanoSe=`echo $ST | cut -d'.' -f 2 | sed 's/^0*//'` FTNanoSe=`echo $FT | cut -d'.' -f 2 | sed 's/^0*//'` if [ ${STSecond} -eq ${FTSecond} ] then STep4ElapsedTime="$((FTSecond-STSecond)) Seconds and $((FTNanoSe-STNanoSe)) Nanoseconds" else STep4ElapsedTime="$((FTSecond-STSecond-1)) Seconds and $((1000000-FTNanoSe+STNanoSe)) Nanoseconds" fi ### STEP 5. CALCULATE DIVERSIFIED KEY echo "========== STEP 5. CALCULATE DIVERSIFIED KEY ==========" echo "========== STARTING TO CALCULATION STEP 5 ==========" ST=`date '+%s.%N'` DiversifiedCipherText=`echo 0: $DActual | xxd -r -p | \ openssl enc -aes-128-cbc -nopad \ -K "$KEY" \ -iv "$iv" | \ hexdump -v -e '/1 "%02X"'` echo "DiversifiedCipherText :$DiversifiedCipherText" echo "Diversified Key is Last 16-byte block (CMAC) of diversified cipher text." DiversifiedCipherTextLenght=${#DiversifiedCipherText} echo "${red}Diversified Key :`echo $DiversifiedCipherText | cut -c$((DiversifiedCipherTextLenght-31))-$((DiversifiedCipherTextLenght))`${reset}" FT=`date '+%s.%N'` echo "========== FINISHED TO CALCULATION STEP 5 ==========" STSecond=`echo $ST | cut -d'.' -f 1 | sed 's/^0*//'` FTSecond=`echo $FT | cut -d'.' -f 1 | sed 's/^0*//'` STNanoSe=`echo $ST | cut -d'.' -f 2 | sed 's/^0*//'` FTNanoSe=`echo $FT | cut -d'.' -f 2 | sed 's/^0*//'` if [ ${STSecond} -eq ${FTSecond} ] then STep5ElapsedTime="$((FTSecond-STSecond)) Seconds and $((FTNanoSe-STNanoSe)) Nanoseconds" else STep5ElapsedTime="$((FTSecond-STSecond-1)) Seconds and $((1000000-FTNanoSe+STNanoSe)) Nanoseconds" fi echo "Step 1 Elapsed Time : $STep1ElapsedTime" echo "Step 2 Elapsed Time : $STep2ElapsedTime" echo "Step 3 Elapsed Time : $STep3ElapsedTime" echo "Step 4 Elapsed Time : $STep4ElapsedTime" echo "Step 5 Elapsed Time : $STep5ElapsedTime"