/
cipher.go
43 lines (38 loc) · 935 Bytes
/
cipher.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
package noise
import (
"github.com/pkg/errors"
)
type cipherState struct {
*Handshake
}
func newCipherState(handshake *Handshake) cipherState {
return cipherState{
handshake,
}
}
// Encrypt to out.
func (n cipherState) Encrypt(out, ad, plaintext []byte) ([]byte, error) {
if n.initiator {
if n.csI0 == nil {
return nil, errors.Errorf("no cipher for encrypt (I)")
}
return n.csI0.Encrypt(out, ad, plaintext), nil
}
if n.csR1 == nil {
return nil, errors.Errorf("no cipher for encrypt (R)")
}
return n.csR1.Encrypt(out, ad, plaintext), nil
}
// Decrypt to out.
func (n cipherState) Decrypt(out, ad, ciphertext []byte) ([]byte, error) {
if n.initiator {
if n.csI1 == nil {
return nil, errors.Errorf("no cipher for decrypt (I)")
}
return n.csI1.Decrypt(out, ad, ciphertext)
}
if n.csR0 == nil {
return nil, errors.Errorf("no cipher for decrypt (R)")
}
return n.csR0.Decrypt(out, ad, ciphertext)
}