| 1 |
package noiseutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/flynn/noise" |
| 7 |
) |
| 8 |
|
| 9 |
// CipherState is the post-handshake AEAD cipher used for the data plane. |
| 10 |
// Each supported cipher has its own concrete implementation in this package with the nonce endianness hardcoded, |
| 11 |
// so the encrypt/decrypt fast path avoids interface dispatch on the byte order. |
| 12 |
type CipherState interface { |
| 13 |
// EncryptDanger encrypts and authenticates a given payload. |
| 14 |
// |
| 15 |
// out is a destination slice to hold the output of the EncryptDanger operation. |
| 16 |
// - ad is additional data, which will be authenticated and appended to out, but not encrypted. |
| 17 |
// - plaintext is encrypted, authenticated and appended to out. |
| 18 |
// - n is a nonce value which must never be re-used with this key. |
| 19 |
// - nb is a scratch buffer used to assemble the nonce. |
| 20 |
EncryptDanger(out, ad, plaintext []byte, n uint64, nb []byte) ([]byte, error) |
| 21 |
|
| 22 |
// DecryptDanger authenticates and decrypts a given payload, with the same argument shape as EncryptDanger. |
| 23 |
DecryptDanger(out, ad, ciphertext []byte, n uint64, nb []byte) ([]byte, error) |
| 24 |
|
| 25 |
// Overhead returns the AEAD tag size, or 0 if the receiver is nil. |
| 26 |
Overhead() int |
| 27 |
} |
| 28 |
|
| 29 |
// NewCipherState wraps the post-handshake noise.CipherState in the per-cipher type that matches cipherFunc. |
| 30 |
// cipherFunc must be the same cipher used to build the noise CipherSuite that produced s. |
| 31 |
func NewCipherState(s *noise.CipherState, cipherFunc noise.CipherFunc) CipherState { |
| 32 |
switch cipherFunc.CipherName() { |
| 33 |
case CipherAESGCM.CipherName(): |
| 34 |
return NewCipherStateAESGCM(s) |
| 35 |
case noise.CipherChaChaPoly.CipherName(): |
| 36 |
return NewCipherStateChaChaPoly(s) |
| 37 |
default: |
| 38 |
panic(fmt.Sprintf("noiseutil: unsupported cipher %q", cipherFunc.CipherName())) |
| 39 |
} |
| 40 |
} |