Skip to content

Commit a15ffef

Browse files
committed
Added Xchacha20 stream cipher
1 parent 80d7277 commit a15ffef

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

core/cipher.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var streamList = map[string]struct {
5050
"AES-192-CFB": {24, shadowstream.AESCFB},
5151
"AES-256-CFB": {32, shadowstream.AESCFB},
5252
"CHACHA20-IETF": {32, shadowstream.Chacha20IETF},
53+
"XCHACHA20": {32, shadowstream.Xchacha20},
5354
}
5455

5556
// ListCipher returns a list of available cipher names sorted alphabetically.

shadowstream/cipher.go

+19
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,22 @@ func Chacha20IETF(key []byte) (Cipher, error) {
7070
}
7171
return chacha20ietfkey(key), nil
7272
}
73+
74+
type xchacha20key []byte
75+
76+
func (k xchacha20key) IVSize() int { return chacha20.XNonceSize }
77+
func (k xchacha20key) Decrypter(iv []byte) cipher.Stream { return k.Encrypter(iv) }
78+
func (k xchacha20key) Encrypter(iv []byte) cipher.Stream {
79+
ciph, err := chacha20.NewCipher(k, iv)
80+
if err != nil {
81+
panic(err) // should never happen
82+
}
83+
return ciph
84+
}
85+
86+
func Xchacha20(key []byte) (Cipher, error) {
87+
if len(key) != chacha20.KeySize {
88+
return nil, KeySizeError(chacha20.KeySize)
89+
}
90+
return xchacha20key(key), nil
91+
}

0 commit comments

Comments
 (0)