-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdh.go
41 lines (33 loc) · 945 Bytes
/
ecdh.go
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
package minileap
import "crypto/ecdh"
// ECDH derives a shared symmetric key using the private half of your
// Curve25519 keypair and the account ID (miniLock ID) of another user
// you are encrypting a miniLeap message/message/blob to.
func ECDH(keyPairPrivate []byte, theirAccountID string) (*[ValidKeyLength]byte, error) {
if len(keyPairPrivate) != ValidKeyLength {
return nil, ErrInvalidKey
}
curve := ecdh.X25519()
theirPubkey, err := AccountIDToCurve25519(theirAccountID)
if err != nil {
return nil, err
}
theirPubX25519, err := curve.NewPublicKey(theirPubkey)
if err != nil {
return nil, err
}
myPrivX25519, err := curve.NewPrivateKey(keyPairPrivate)
if err != nil {
return nil, err
}
// Magic :-D
sharedSecret, err := myPrivX25519.ECDH(theirPubX25519)
if err != nil {
return nil, err
}
sharedSecret32, err := ConvertKey(sharedSecret)
if err != nil {
return nil, err
}
return sharedSecret32, nil
}