mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-29 13:35:20 -05:00
implementation for Shadowsocks AEAD
This commit is contained in:
@@ -29,6 +29,26 @@ func (v StaticBytesGenerator) Next() []byte {
|
||||
return v.Content
|
||||
}
|
||||
|
||||
type IncreasingAEADNonceGenerator struct {
|
||||
nonce []byte
|
||||
}
|
||||
|
||||
func NewIncreasingAEADNonceGenerator() *IncreasingAEADNonceGenerator {
|
||||
return &IncreasingAEADNonceGenerator{
|
||||
nonce: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *IncreasingAEADNonceGenerator) Next() []byte {
|
||||
for i := range g.nonce {
|
||||
g.nonce[i]++
|
||||
if g.nonce[i] != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return g.nonce
|
||||
}
|
||||
|
||||
type Authenticator interface {
|
||||
NonceSize() int
|
||||
Overhead() int
|
||||
|
||||
@@ -34,6 +34,29 @@ func (PlainChunkSizeParser) Decode(b []byte) (uint16, error) {
|
||||
return serial.BytesToUint16(b), nil
|
||||
}
|
||||
|
||||
type AEADChunkSizeParser struct {
|
||||
Auth *AEADAuthenticator
|
||||
}
|
||||
|
||||
func (p *AEADChunkSizeParser) SizeBytes() int {
|
||||
return 2 + p.Auth.Overhead()
|
||||
}
|
||||
|
||||
func (p *AEADChunkSizeParser) Encode(size uint16, b []byte) []byte {
|
||||
b = serial.Uint16ToBytes(size, b)
|
||||
b, err := p.Auth.Seal(b[:0], b)
|
||||
common.Must(err)
|
||||
return b
|
||||
}
|
||||
|
||||
func (p *AEADChunkSizeParser) Decode(b []byte) (uint16, error) {
|
||||
b, err := p.Auth.Open(b[:0], b)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return serial.BytesToUint16(b), nil
|
||||
}
|
||||
|
||||
type ChunkStreamReader struct {
|
||||
sizeDecoder ChunkSizeDecoder
|
||||
reader buf.Reader
|
||||
|
||||
Reference in New Issue
Block a user