Imported Upstream version 2.5.1
[debian/amanda] / server-src / amcrypt-ossl.sh.in
1 #!@SHELL@
2 #
3 # amcrypt-ossl.sh - crypto helper using OpenSSL
4 # Usage: amcrypt-ossl.sh [-d]
5 #
6
7 # change these as needed
8 OPENSSL=                        # whatever's in $PATH
9 CIPHER=aes-256-cbc              # see `openssl help` for more ciphers
10 AMANDA_HOME=~amandabackup
11 RANDFILE=$AMANDA_HOME/.rnd
12 export RANDFILE
13 PASSPHRASE=$AMANDA_HOME/.am_passphrase  # required
14
15 # where might openssl be?
16 PATH=/bin:/usr/bin:/usr/local/bin:/usr/ssl/bin:/usr/local/ssl/bin
17 export PATH
18 ME=`basename "$0"`
19
20 if [ -z "${OPENSSL:=`which openssl`}" ]; then
21         echo "${ME}: openssl not found" >&2
22         exit 1
23 elif [ ! -x "${OPENSSL}" ]; then
24         echo "${ME}: can't execute openssl (${OPENSSL})" >&2
25         exit 1
26 fi
27
28 # we'll need to pad the datastream to a multiple of the cipher block size prior
29 # to encryption. 96 bytes (= 768 bits) should be good for any cipher.
30 pad() {
31         perl -pe 'BEGIN { $bs = 96; $/ = \8192 } $nbytes = ($nbytes + length) % $bs; END { print "\0" x ($bs - $nbytes) }'
32 }
33
34 if [ "$1" = -d ]; then
35         # decrypt
36         "${OPENSSL}" enc -d "-${CIPHER}" -nopad -salt -pass fd:3 3< "${PASSPHRASE}"
37 else
38         # encrypt
39         pad | "${OPENSSL}" enc -e "-${CIPHER}" -nopad -salt -pass fd:3 3< "${PASSPHRASE}"
40 fi
41