-
Notifications
You must be signed in to change notification settings - Fork 0
/
ossl-ecdh.sh
55 lines (51 loc) · 1 KB
/
ossl-ecdh.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
#!/usr/bin/env bash
help_message() {
printf "Usage:
$0 <arguments>
Arguments:
[ -h ] Show this help
[ -l ] List all supported curves
[ -c CURVE,KEYNAME ] Generate an ECDH key pair
[ -d PRIVKEY,PUBKEY ] Generate a shared secret between a private and public key
"
}
ARGS=$(getopt -n openssl-ecdh -o c:d:lh -- "$@")
eval set -- "$ARGS"
while :
do case "$1" in
'-c')
# https://jameshfisher.com/2017/04/14/openssl-ecc/
# https://unix.stackexchange.com/a/164260
IFS=','
eccArgs=($2)
curve=${eccArgs[0]}
keyname=${eccArgs[1]}
unset IFS
openssl ecparam -name "$curve" -genkey -out "${keyname}.priv"
openssl ec -in "${keyname}.priv" -pubout -out "${keyname}.pub"
shift 2
;;
'-d')
IFS=','
keyArgs=($2)
privkey=${keyArgs[0]}
pubkey=${keyArgs[1]}
unset IFS
openssl pkeyutl -derive -inkey "$privkey" -peerkey "$pubkey" \
| openssl enc -base64 -A \
&& printf '\n'
shift 2
;;
'-l')
openssl ecparam -list_curves
exit
;;
'-h')
help_message
exit
;;
--)
shift
break
;;
esac; done