Add method to get Cipher from CipherState

Signed-off-by: Jonathan Rudenberg <jonathan@titanous.com>
This commit is contained in:
Jonathan Rudenberg 2016-07-12 21:32:38 -04:00
parent 9a626dbd0b
commit d4248be25d
1 changed files with 19 additions and 0 deletions

View File

@ -10,20 +10,39 @@ type CipherState struct {
c Cipher c Cipher
k [32]byte k [32]byte
n uint64 n uint64
invalid bool
} }
func (s *CipherState) Encrypt(out, ad, plaintext []byte) []byte { func (s *CipherState) Encrypt(out, ad, plaintext []byte) []byte {
if s.invalid {
panic("noise: CipherSuite has been copied, state is invalid")
}
out = s.c.Encrypt(out, s.n, ad, plaintext) out = s.c.Encrypt(out, s.n, ad, plaintext)
s.n++ s.n++
return out return out
} }
func (s *CipherState) Decrypt(out, ad, ciphertext []byte) ([]byte, error) { func (s *CipherState) Decrypt(out, ad, ciphertext []byte) ([]byte, error) {
if s.invalid {
panic("noise: CipherSuite has been copied, state is invalid")
}
out, err := s.c.Decrypt(out, s.n, ad, ciphertext) out, err := s.c.Decrypt(out, s.n, ad, ciphertext)
s.n++ s.n++
return out, err return out, err
} }
// Cipher returns the low-level symmetric encryption primitive. It should only
// be used if nonces need to be managed manually, for example with a network
// protocol that can deliver out-of-order messages. This is dangerous, users
// must ensure that they are incrementing a nonce after every encrypt operation.
// After calling this method, it is an error to call Encrypt/Decrypt on the
// CipherState.
func (s *CipherState) Cipher() Cipher {
s.invalid = true
return s.c
}
type symmetricState struct { type symmetricState struct {
CipherState CipherState
hasK bool hasK bool