-
Notifications
You must be signed in to change notification settings - Fork 2
/
read_encrypted.sh
executable file
·82 lines (61 loc) · 1.95 KB
/
read_encrypted.sh
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
#!/bin/bash
#
# Read encrypted data from a slot.
#
set -u # stop if any variable is undefined
set -e # stop if any command fails
############################################
# Check parameters
############################################
if [ $# -ne 3 ]
then
echo "Read encrypted data from a slot"
echo "Usage $0 SlotID KeyID Key"
echo "Ex.: $0 2 7 0000000000000000000000000000000000000000000000000000000000000000"
exit 1
fi
nonce_in=0000000000000000000000000000000000000000
dataslot=$1
keyslot=$2
key=$3
echo "Reading data from slot: $dataslot"
echo "Read key in slot: $keyslot"
echo
############################################
# Get serial number
############################################
sn=`./serial`
sn01=${sn:0:4}
sn23=${sn:4:4}
sn47=${sn:8:8}
sn8=${sn:16:2}
#echo "Serial: $sn"
############################################
# Get random nonce, data and device MAC
############################################
nonce_out=`./nonce_rand $nonce_in` # create nonce
out=`./gendig $keyslot` # incorporate key Digest to TempKey
slotdata=`./read_slot $dataslot` # Get encrypted data
#echo "Nonce: $nonce_out"
############################################
# Calculate local TempKey
############################################
opcode=16
mode=00
param2=00
tempkey_msg=$nonce_out$nonce_in$opcode$mode$param2
tempkey=$(echo $tempkey_msg | xxd -r -p | sha256sum | cut -d ' ' -f 1)
############################################
# Update Tempkey using slot data
############################################
opcode=15
param1=02
param2=$(printf "%02x00" $keyslot)
zeros=00000000000000000000000000000000000000000000000000
tempkey_msg=$key$opcode$param1$param2$sn8$sn01$zeros$tempkey
tempkey=$(echo $tempkey_msg | xxd -r -p | sha256sum | cut -d ' ' -f 1)
############################################
# Decrypt data using calculated TempKey
############################################
cleartext=`./xor $tempkey $slotdata`
echo "Data: $cleartext"