-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcbc_decrypt.sh
executable file
·53 lines (40 loc) · 1.19 KB
/
cbc_decrypt.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
#!/bin/bash
#
# Decrypt data using AES-128-CBC mode.
#
set -u # stop if any variable is undefined
############################################
# Check parameters
############################################
if [ $# -ne 1 ]
then
>&2 echo "Decrypt data using AES-128-CBC mode"
>&2 echo ""
>&2 echo "Usage $0 KeyID [IV]"
>&2 echo "Where:"
>&2 echo " KeyID: is the key slot (or TEMPKEY)"
>&2 echo " IV: is the initialization vector (16 hex bytes) (0 is the default)"
>&2 echo ""
>&2 echo "Ex.: cat LICENSE.aes | ./cbc_decrypt.sh 3"
exit 1
fi
SLOT=$1
IV=${2:-"00000000000000000000000000000000"}
############################################
# Main loop
############################################
last_encrypted_block=$IV
for current_encrypted_block in $(xxd -a -p -c 16)
do
# Decrypt
current_clear_block=$(./aes_decrypt $SLOT $current_encrypted_block)
# Check for error
if [ $? -ne 0 ]; then
>&2 echo "Device error."
exit 1
fi
# XOR (for CBC mode)
current_clear_block=$(./xor $current_clear_block $last_encrypted_block)
echo $current_clear_block | xxd -r -p
last_encrypted_block=$current_encrypted_block
done