-
Notifications
You must be signed in to change notification settings - Fork 11
/
encryption.go
46 lines (41 loc) · 1.24 KB
/
encryption.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
43
44
45
46
package tuna
import (
"crypto/sha256"
"fmt"
"net"
stream "github.com/nknorg/encrypted-stream"
"github.com/nknorg/tuna/pb"
)
const (
connNonceSize = 32
sharedKeySize = 32
encryptKeySize = 32
)
func computeEncryptKey(connNonce []byte, sharedKey []byte) *[encryptKeySize]byte {
encryptKey := sha256.Sum256(append(connNonce, sharedKey...))
return &encryptKey
}
func encryptConn(conn net.Conn, encryptKey *[encryptKeySize]byte, encryptionAlgo pb.EncryptionAlgo, initiator bool) (net.Conn, error) {
var cipher stream.Cipher
var err error
switch encryptionAlgo {
case pb.EncryptionAlgo_ENCRYPTION_NONE:
return conn, nil
case pb.EncryptionAlgo_ENCRYPTION_XSALSA20_POLY1305:
cipher = stream.NewXSalsa20Poly1305Cipher(encryptKey)
case pb.EncryptionAlgo_ENCRYPTION_AES_GCM:
cipher, err = stream.NewAESGCMCipher(encryptKey[:])
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported encryption algo %v", encryptionAlgo)
}
config := &stream.Config{
Cipher: cipher,
Initiator: initiator,
SequentialNonce: true,
DisableNonceVerification: true, // for compatibility with old version, will be removed in the future
}
return stream.NewEncryptedStream(conn, config)
}