1#!/usr/bin/env bash
2set -x
3set -e
4set -o pipefail
5
6USAGE="epd_encryption_test.sh <path_to_encryptcli> <tested_file>"
7if [ $# -lt 2 ]
8then
9    echo $USAGE
10    exit 1
11fi
12
13ENCRYPT_CLI="$1"
14FILE="$2"
15TMP_DIR="./tmp"
16PASSPHRASE_FILE="passphrase.txt"
17KEY_FILE="x2_key/test_x2_key.key"
18
19mkdir -p $TMP_DIR
20
21RESULT=0
22
23############
24# Passphrase only
25############
26echo "Passphrase only"
27
28cat $PASSPHRASE_FILE | $ENCRYPT_CLI -e --pwd-fd 0 -o $TMP_DIR/out_file.gpg $FILE
29if ! [ -r $TMP_DIR/out_file.gpg ]
30then
31    echo encrypted file does not exist
32    exit -1;
33fi
34$GPG -d --batch --passphrase-file $PASSPHRASE_FILE -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.gpg
35
36if diff $FILE $TMP_DIR/out_file.txt
37then
38    echo OK
39else
40    RESULT=1
41    echo FAIL
42fi
43rm $TMP_DIR/out_file.gpg
44rm $TMP_DIR/out_file.txt
45
46############
47# Key only
48############
49echo "Key only"
50
51$ENCRYPT_CLI -e --key-only --plain-text-key -k $KEY_FILE -o $TMP_DIR/out_file.epd $FILE
52$ENCRYPT_CLI -d --key-only --plain-text-key -k $KEY_FILE -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.epd
53
54if diff $FILE $TMP_DIR/out_file.txt
55then
56    echo OK
57else
58    RESULT=1
59    echo FAIL
60fi
61rm $TMP_DIR/out_file.epd
62rm $TMP_DIR/out_file.txt
63
64############
65# Key only when encrypting a .gpg file
66############
67echo "Key only when encrypting a .gpg file"
68$ENCRYPT_CLI -e --key-only --plain-text-key -k $KEY_FILE -o $TMP_DIR/out_file.gpg $FILE
69$GPG -d --batch --batch --passphrase-file $KEY_FILE -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.gpg
70
71if diff $FILE $TMP_DIR/out_file.txt
72then
73    echo OK
74else
75    RESULT=1
76    echo FAIL
77fi
78rm $TMP_DIR/out_file.gpg
79rm $TMP_DIR/out_file.txt
80
81############
82# Key only when decrypting a .gpg file
83############
84echo "Key only when decrytping a .gpg file"
85$ENCRYPT_CLI -e --key-only --plain-text-key -k $KEY_FILE -o $TMP_DIR/out_file.gpg $FILE
86$ENCRYPT_CLI -d --key-only --plain-text-key -k $KEY_FILE -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.gpg
87
88if diff $FILE $TMP_DIR/out_file.txt
89then
90    echo OK
91else
92    RESULT=1
93    echo FAIL
94fi
95rm $TMP_DIR/out_file.gpg
96rm $TMP_DIR/out_file.txt
97
98############
99# Key only (persisted)
100############
101echo "Key only (persisted)"
102
103$ENCRYPT_CLI -e --key-only --plain-text-key --persist-key -k $KEY_FILE -o $TMP_DIR/out_file.epd $FILE
104$ENCRYPT_CLI -d --key-only --plain-text-key -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.epd
105
106if diff $FILE $TMP_DIR/out_file.txt
107then
108    echo OK
109else
110    RESULT=1
111    echo FAIL
112fi
113rm $TMP_DIR/out_file.epd
114rm $TMP_DIR/out_file.txt
115
116############
117# Key and passphrase
118############
119echo "Key and passphrase"
120
121cat $PASSPHRASE_FILE | $ENCRYPT_CLI -e -k $KEY_FILE --plain-text-key --pwd-fd 0 -o $TMP_DIR/out_file.epd $FILE
122cat $PASSPHRASE_FILE | $ENCRYPT_CLI -d -k $KEY_FILE --plain-text-key --pwd-fd 0 -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.epd
123
124if diff $FILE $TMP_DIR/out_file.txt
125then
126    echo OK
127else
128    RESULT=1
129    echo FAIL
130fi
131rm $TMP_DIR/out_file.epd
132rm $TMP_DIR/out_file.txt
133
134############
135# Key (persisted) and passphrase
136############
137echo "Key (persisted) and passphrase"
138
139cat $PASSPHRASE_FILE | $ENCRYPT_CLI -e --persist-key --plain-text-key -k $KEY_FILE --pwd-fd 0 -o $TMP_DIR/out_file.epd $FILE
140cat $PASSPHRASE_FILE | $ENCRYPT_CLI -d --pwd-fd 0 --plain-text-key -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.epd
141
142if diff $FILE $TMP_DIR/out_file.txt
143then
144    echo OK
145else
146    RESULT=1
147    echo FAIL
148fi
149rm $TMP_DIR/out_file.epd
150rm $TMP_DIR/out_file.txt
151
152############
153# Encrypted key file generated by EncryptPad and decrypted by GPG
154############
155echo "Encrypted key file generated by EncryptPad and decrypted by GPG"
156
157cat $PASSPHRASE_FILE | $ENCRYPT_CLI --generate-key --key-pwd-fd 0 $TMP_DIR/encrypted_key.key
158$GPG -d --batch --passphrase-file $PASSPHRASE_FILE -o $TMP_DIR/out_file.txt $TMP_DIR/encrypted_key.key
159
160if [ "$?" = "0" ]
161then
162    echo OK
163else
164    RESULT=1
165    echo FAIL
166fi
167
168rm $TMP_DIR/encrypted_key.key
169rm $TMP_DIR/out_file.txt
170
171############
172# EncryptPad encrypts with an encrypted key file
173############
174echo "EncryptPad encrypts with an encrypted key file"
175
176cat $PASSPHRASE_FILE | $ENCRYPT_CLI --generate-key --key-pwd-fd 0 $TMP_DIR/encrypted_key.key
177cat $PASSPHRASE_FILE | $ENCRYPT_CLI -e --key-only --key-pwd-fd 0 -k $TMP_DIR/encrypted_key.key -o $TMP_DIR/out_file.gpg $FILE
178
179$GPG -d --batch --passphrase-file $PASSPHRASE_FILE -o $TMP_DIR/unencrypted_key.key $TMP_DIR/encrypted_key.key
180$GPG -d --batch --passphrase-file $TMP_DIR/unencrypted_key.key -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.gpg
181
182if diff $FILE $TMP_DIR/out_file.txt
183then
184    echo OK
185else
186    RESULT=1
187    echo FAIL
188fi
189
190cat $PASSPHRASE_FILE | $ENCRYPT_CLI -d --key-only --key-pwd-fd 0 -k $TMP_DIR/encrypted_key.key -o $TMP_DIR/epad_out_file.txt $TMP_DIR/out_file.gpg
191
192if diff $FILE $TMP_DIR/epad_out_file.txt
193then
194    echo OK
195else
196    RESULT=1
197    echo FAIL
198fi
199
200rm $TMP_DIR/encrypted_key.key
201rm $TMP_DIR/out_file.gpg
202rm $TMP_DIR/unencrypted_key.key
203rm $TMP_DIR/out_file.txt
204rm $TMP_DIR/epad_out_file.txt
205
206###########
207# EncryptPad encrypts and decrypts with an encrypted key in double protection mode
208###########
209echo "EncryptPad encrypts and decrypts with an encrypted key in double protection mode"
210
211cat $PASSPHRASE_FILE | $ENCRYPT_CLI --generate-key --key-pwd-fd 0 $TMP_DIR/encrypted_key.key
212cat $PASSPHRASE_FILE | $ENCRYPT_CLI -e -k $TMP_DIR/encrypted_key.key --key-pwd-file $PASSPHRASE_FILE --pwd-fd 0 -o $TMP_DIR/out_file.epd $FILE
213cat $PASSPHRASE_FILE | $ENCRYPT_CLI -d -k $TMP_DIR/encrypted_key.key --key-pwd-file $PASSPHRASE_FILE --pwd-fd 0 -o $TMP_DIR/out_file.txt $TMP_DIR/out_file.epd
214
215if diff $FILE $TMP_DIR/out_file.txt
216then
217    echo OK
218else
219    RESULT=1
220    echo FAIL
221fi
222
223rm $TMP_DIR/out_file.epd
224rm $TMP_DIR/out_file.txt
225rm $TMP_DIR/encrypted_key.key
226
227###########
228# Conclusion
229###########
230
231exit $RESULT
232