Commit d4248be2 authored by Jonathan Rudenberg's avatar Jonathan Rudenberg

Add method to get Cipher from CipherState

Signed-off-by: default avatarJonathan Rudenberg <jonathan@titanous.com>
parent 9a626dbd
......@@ -10,20 +10,39 @@ type CipherState struct {
c Cipher
k [32]byte
n uint64
invalid bool
}
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)
s.n++
return out
}
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)
s.n++
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 {
CipherState
hasK bool
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment