-
Notifications
You must be signed in to change notification settings - Fork 211
/
crypto.go
42 lines (31 loc) · 1.05 KB
/
crypto.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
42
package ecdh
import (
"crypto/sha512"
"github.com/perlin-network/noise/internal/edwards25519"
)
func computeSharedKey(nodePrivateKey edwards25519.PrivateKey, remotePublicKey edwards25519.PublicKey) []byte {
var nodeSecretKeyBuf, remotePublicKeyBuf, sharedKeyBuf [32]byte
copy(nodeSecretKeyBuf[:], deriveSecretKey(nodePrivateKey))
copy(remotePublicKeyBuf[:], remotePublicKey[:])
var sharedKeyElement, publicKeyElement edwards25519.ExtendedGroupElement
publicKeyElement.FromBytes(&remotePublicKeyBuf)
edwards25519.GeScalarMult(&sharedKeyElement, &nodeSecretKeyBuf, &publicKeyElement)
sharedKeyElement.ToBytes(&sharedKeyBuf)
return sharedKeyBuf[:]
}
func deriveSecretKey(privateKey edwards25519.PrivateKey) []byte {
digest := sha512.Sum512(privateKey[:32])
digest[0] &= 248
digest[31] &= 127
digest[31] |= 64
return digest[:32]
}
func isEd25519GroupElement(buf []byte) bool {
if len(buf) != edwards25519.PublicKeySize {
return false
}
var buff [32]byte
copy(buff[:], buf)
var A edwards25519.ExtendedGroupElement
return A.FromBytes(&buff)
}