Add xchacha20-ietf-poly1305 for Shadowsocks

This commit is contained in:
maskedeken 2021-05-25 23:45:48 +08:00 committed by 世界
parent 3fe61ed4a2
commit 238bd5d050
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 52 additions and 23 deletions

View file

@ -77,6 +77,12 @@ func createChaCha20Poly1305(key []byte) cipher.AEAD {
return ChaChaPoly1305
}
func createXChaCha20Poly1305(key []byte) cipher.AEAD {
XChaChaPoly1305, err := chacha20poly1305.NewX(key)
common.Must(err)
return XChaChaPoly1305
}
func (a *Account) getCipher() (Cipher, error) {
switch a.CipherType {
case CipherType_AES_128_GCM:
@ -97,6 +103,12 @@ func (a *Account) getCipher() (Cipher, error) {
IVBytes: 32,
AEADAuthCreator: createChaCha20Poly1305,
}, nil
case CipherType_XCHACHA20_POLY1305:
return &AEADCipher{
KeyBytes: 32,
IVBytes: 32,
AEADAuthCreator: createXChaCha20Poly1305,
}, nil
case CipherType_NONE:
return NoneCipher{}, nil
default:
@ -152,11 +164,12 @@ func (c *AEADCipher) IVSize() int32 {
}
func (c *AEADCipher) createAuthenticator(key []byte, iv []byte) *crypto.AEADAuthenticator {
nonce := crypto.GenerateInitialAEADNonce()
subkey := make([]byte, c.KeyBytes)
hkdfSHA1(key, iv, subkey)
aead := c.AEADAuthCreator(subkey)
nonce := crypto.GenerateAEADNonceWithSize(aead.NonceSize())
return &crypto.AEADAuthenticator{
AEAD: c.AEADAuthCreator(subkey),
AEAD: aead,
NonceGenerator: nonce,
}
}