4141/// AES-256 in Galois/Counter Mode (GCM).
4242const AES256_GCM : & str =
"[email protected] " ; 4343
44+ /// ChaCha20-Poly1305
45+ const CHACHA20_POLY1305 : & str =
"[email protected] " ; 46+
47+ /// Triple-DES in block chaining (CBC) mode
48+ const TDES_CBC : & str = "3des-cbc" ;
49+
4450/// Nonces for AEAD modes.
4551#[ cfg( feature = "aes-gcm" ) ]
4652type AeadNonce = [ u8 ; 12 ] ;
@@ -84,6 +90,12 @@ pub enum Cipher {
8490
8591 /// AES-256 in Galois/Counter Mode (GCM).
8692 Aes256Gcm ,
93+
94+ /// ChaCha20-Poly1305
95+ ChaCha20Poly1305 ,
96+
97+ /// TripleDES in block chaining (CBC) mode
98+ TDesCbc ,
8799}
88100
89101impl Cipher {
@@ -102,6 +114,8 @@ impl Cipher {
102114 AES256_CTR => Ok ( Self :: Aes256Ctr ) ,
103115 AES128_GCM => Ok ( Self :: Aes128Gcm ) ,
104116 AES256_GCM => Ok ( Self :: Aes256Gcm ) ,
117+ CHACHA20_POLY1305 => Ok ( Self :: ChaCha20Poly1305 ) ,
118+ TDES_CBC => Ok ( Self :: TDesCbc ) ,
105119 _ => Err ( Error :: AlgorithmUnknown ) ,
106120 }
107121 }
@@ -118,6 +132,8 @@ impl Cipher {
118132 Self :: Aes256Ctr => AES256_CTR ,
119133 Self :: Aes128Gcm => AES128_GCM ,
120134 Self :: Aes256Gcm => AES256_GCM ,
135+ Self :: ChaCha20Poly1305 => CHACHA20_POLY1305 ,
136+ Self :: TDesCbc => TDES_CBC ,
121137 }
122138 }
123139
@@ -133,13 +149,15 @@ impl Cipher {
133149 Self :: Aes256Ctr => Some ( ( 32 , 16 ) ) ,
134150 Self :: Aes128Gcm => Some ( ( 16 , 12 ) ) ,
135151 Self :: Aes256Gcm => Some ( ( 32 , 12 ) ) ,
152+ Self :: ChaCha20Poly1305 => Some ( ( 64 , 0 ) ) ,
153+ Self :: TDesCbc => Some ( ( 24 , 8 ) ) ,
136154 }
137155 }
138156
139157 /// Get the block size for this cipher in bytes.
140158 pub fn block_size ( self ) -> usize {
141159 match self {
142- Self :: None => 8 ,
160+ Self :: None | Self :: ChaCha20Poly1305 | Self :: TDesCbc => 8 ,
143161 Self :: Aes128Cbc
144162 | Self :: Aes192Cbc
145163 | Self :: Aes256Cbc
@@ -163,7 +181,10 @@ impl Cipher {
163181
164182 /// Does this cipher have an authentication tag? (i.e. is it an AEAD mode?)
165183 pub fn has_tag ( self ) -> bool {
166- matches ! ( self , Self :: Aes128Gcm | Self :: Aes256Gcm )
184+ matches ! (
185+ self ,
186+ Self :: Aes128Gcm | Self :: Aes256Gcm | Self :: ChaCha20Poly1305
187+ )
167188 }
168189
169190 /// Is this cipher `none`?
@@ -347,10 +368,9 @@ where
347368#[ cfg( feature = "encryption" ) ]
348369fn ctr_encrypt < C > ( key : & [ u8 ] , iv : & [ u8 ] , buffer : & mut [ u8 ] ) -> Result < ( ) >
349370where
350- C : StreamCipherCore + KeyIvInit
371+ C : StreamCipherCore + KeyIvInit ,
351372{
352- let cipher = C :: new_from_slices ( key, iv)
353- . map_err ( |_| Error :: Crypto ) ?;
373+ let cipher = C :: new_from_slices ( key, iv) . map_err ( |_| Error :: Crypto ) ?;
354374
355375 cipher
356376 . try_apply_keystream_partial ( buffer. into ( ) )
0 commit comments