@@ -70,6 +70,9 @@ pub trait AsyncStreamCipher: Sized {
7070 }
7171
7272 /// Encrypt data from buffer to buffer.
73+ ///
74+ /// # Errors
75+ /// Returns [`NotEqualError`] if provided `in_buf` and `out_buf` have different lengths.
7376 fn encrypt_b2b ( self , in_buf : & [ u8 ] , out_buf : & mut [ u8 ] ) -> Result < ( ) , NotEqualError >
7477 where
7578 Self : BlockModeEncrypt ,
@@ -78,6 +81,9 @@ pub trait AsyncStreamCipher: Sized {
7881 }
7982
8083 /// Decrypt data from buffer to buffer.
84+ ///
85+ /// # Errors
86+ /// Returns [`NotEqualError`] if provided `in_buf` and `out_buf` have different lengths.
8187 fn decrypt_b2b ( self , in_buf : & [ u8 ] , out_buf : & mut [ u8 ] ) -> Result < ( ) , NotEqualError >
8288 where
8389 Self : BlockModeDecrypt ,
@@ -110,42 +116,58 @@ pub trait AsyncStreamCipher: Sized {
110116/// and the seeking methods defined in the [`StreamCipherSeek`] trait.
111117pub trait StreamCipher {
112118 /// Check that the cipher can generate a keystream with a length of `data_len` bytes.
119+ ///
120+ /// # Errors
121+ /// Returns [`StreamCipherError`] in the event it cannot.
113122 fn check_remaining ( & self , data_len : usize ) -> Result < ( ) , StreamCipherError > ;
114123
115124 /// Apply keystream to `inout` without checking for keystream repetition.
116125 ///
117- /// # WARNING
126+ /// <div><class = "warning">
127+ /// <b>WARNING<b>
128+ ///
118129 /// This method should be used with extreme caution! Triggering keystream repetition can expose
119130 /// the stream cipher to chosen plaintext attacks.
131+ /// </div>
120132 fn unchecked_apply_keystream_inout ( & mut self , buf : InOutBuf < ' _ , ' _ , u8 > ) ;
121133
122134 /// Apply keystream to `buf` without checking for keystream repetition.
123135 ///
124- /// # WARNING
136+ /// <div><class = "warning">
137+ /// <b>WARNING<b>
138+ ///
125139 /// This method should be used with extreme caution! Triggering keystream repetition can expose
126140 /// the stream cipher to chosen plaintext attacks.
141+ /// </div>
127142 fn unchecked_write_keystream ( & mut self , buf : & mut [ u8 ] ) ;
128143
129144 /// Apply keystream to data behind `buf` without checking for keystream repetition.
130145 ///
131- /// # WARNING
146+ /// <div><class = "warning">
147+ /// <b>WARNING<b>
148+ ///
132149 /// This method should be used with extreme caution! Triggering keystream repetition can expose
133150 /// the stream cipher to chosen plaintext attacks.
151+ /// </div>
134152 #[ inline]
135153 fn unchecked_apply_keystream ( & mut self , buf : & mut [ u8 ] ) {
136- self . unchecked_apply_keystream_inout ( buf. into ( ) )
154+ self . unchecked_apply_keystream_inout ( buf. into ( ) ) ;
137155 }
138156
139157 /// Apply keystream to data buffer-to-buffer without checking for keystream repetition.
140158 ///
141159 /// It will XOR generated keystream with data from the `input` buffer
142160 /// and will write result to the `output` buffer.
143161 ///
162+ /// # Errors
144163 /// Returns [`NotEqualError`] if the `input` and `output` buffers have different lengths.
145164 ///
146- /// # WARNING
165+ /// <div><class = "warning">
166+ /// <b>WARNING<b>
167+ ///
147168 /// This method should be used with extreme caution! Triggering keystream repetition can expose
148169 /// the stream cipher to chosen plaintext attacks.
170+ /// </div>
149171 #[ inline]
150172 fn unchecked_apply_keystream_b2b (
151173 & mut self ,
@@ -159,6 +181,7 @@ pub trait StreamCipher {
159181
160182 /// Apply keystream to `inout` data.
161183 ///
184+ /// # Errors
162185 /// If the end of the keystream is reached with the given buffer length,
163186 /// the method will return [`StreamCipherError`] without modifying `buf`.
164187 fn try_apply_keystream_inout (
@@ -172,6 +195,7 @@ pub trait StreamCipher {
172195
173196 /// Apply keystream to data behind `buf`.
174197 ///
198+ /// # Errors
175199 /// If the end of the keystream is reached with the given buffer length,
176200 /// the method will return [`StreamCipherError`] without modifying `buf`.
177201 #[ inline]
@@ -184,6 +208,7 @@ pub trait StreamCipher {
184208 /// It will XOR generated keystream with data from the `input` buffer
185209 /// and will write result to the `output` buffer.
186210 ///
211+ /// # Errors
187212 /// Returns [`StreamCipherError`] without modifying the buffers if the `input` and `output`
188213 /// buffers have different lengths, or if the end of the keystream is reached with
189214 /// the given data length.
@@ -200,6 +225,7 @@ pub trait StreamCipher {
200225
201226 /// Write keystream to `buf`.
202227 ///
228+ /// # Errors
203229 /// If the end of the keystream is reached with the given buffer length,
204230 /// the method will return [`StreamCipherError`] without modifying `buf`.
205231 #[ inline]
@@ -218,7 +244,8 @@ pub trait StreamCipher {
218244 /// If the end of the keystream is reached with the given buffer length.
219245 #[ inline]
220246 fn apply_keystream_inout ( & mut self , buf : InOutBuf < ' _ , ' _ , u8 > ) {
221- self . try_apply_keystream_inout ( buf) . unwrap ( ) ;
247+ self . try_apply_keystream_inout ( buf)
248+ . expect ( "end of keystream reached" ) ;
222249 }
223250
224251 /// Apply keystream to data in-place.
@@ -230,7 +257,8 @@ pub trait StreamCipher {
230257 /// If the end of the keystream is reached with the given buffer length.
231258 #[ inline]
232259 fn apply_keystream ( & mut self , buf : & mut [ u8 ] ) {
233- self . try_apply_keystream ( buf) . unwrap ( ) ;
260+ self . try_apply_keystream ( buf)
261+ . expect ( "end of keystream reached" ) ;
234262 }
235263
236264 /// Apply keystream to data buffer-to-buffer.
@@ -246,7 +274,7 @@ pub trait StreamCipher {
246274 let Ok ( buf) = InOutBuf :: new ( input, output) else {
247275 panic ! ( "Lengths of input and output buffers are not equal to each other!" ) ;
248276 } ;
249- self . apply_keystream_inout ( buf)
277+ self . apply_keystream_inout ( buf) ;
250278 }
251279
252280 /// Write keystream to `buf`.
@@ -255,7 +283,8 @@ pub trait StreamCipher {
255283 /// If the end of the keystream is reached with the given buffer length.
256284 #[ inline]
257285 fn write_keystream ( & mut self , buf : & mut [ u8 ] ) {
258- self . try_write_keystream ( buf) . unwrap ( ) ;
286+ self . try_write_keystream ( buf)
287+ . expect ( "end of keystream reached" ) ;
259288 }
260289}
261290
@@ -266,11 +295,13 @@ pub trait StreamCipher {
266295pub trait StreamCipherSeek {
267296 /// Try to get current keystream position in bytes.
268297 ///
298+ /// # Errors
269299 /// Returns [`OverflowError`] if the position value can not be represented by type `T`.
270300 fn try_current_pos < T : SeekNum > ( & self ) -> Result < T , OverflowError > ;
271301
272302 /// Try to seek to the provided position in bytes.
273303 ///
304+ /// # Errors
274305 /// Returns [`StreamCipherError`] if the position value is bigger than keystream length.
275306 fn try_seek < T : SeekNum > ( & mut self , pos : T ) -> Result < ( ) , StreamCipherError > ;
276307
@@ -279,15 +310,17 @@ pub trait StreamCipherSeek {
279310 /// # Panics
280311 /// If the position value can not be represented by type `T`.
281312 fn current_pos < T : SeekNum > ( & self ) -> T {
282- self . try_current_pos ( ) . unwrap ( )
313+ self . try_current_pos ( )
314+ . expect ( "position cannot be represented by `T`" )
283315 }
284316
285317 /// Seek to the provided keystream position in bytes.
286318 ///
287319 /// # Panics
288320 /// If the position value is bigger than keystream length.
289321 fn seek < T : SeekNum > ( & mut self , pos : T ) {
290- self . try_seek ( pos) . unwrap ( )
322+ self . try_seek ( pos)
323+ . expect ( "position value bigger than keystream length" ) ;
291324 }
292325}
293326
@@ -299,12 +332,12 @@ impl<C: StreamCipher> StreamCipher for &mut C {
299332
300333 #[ inline]
301334 fn unchecked_apply_keystream_inout ( & mut self , buf : InOutBuf < ' _ , ' _ , u8 > ) {
302- C :: unchecked_apply_keystream_inout ( self , buf)
335+ C :: unchecked_apply_keystream_inout ( self , buf) ;
303336 }
304337
305338 #[ inline]
306339 fn unchecked_write_keystream ( & mut self , buf : & mut [ u8 ] ) {
307- C :: unchecked_write_keystream ( self , buf)
340+ C :: unchecked_write_keystream ( self , buf) ;
308341 }
309342}
310343
@@ -316,13 +349,19 @@ impl<C: StreamCipher> StreamCipher for &mut C {
316349pub trait SeekNum : Sized {
317350 /// Try to get position for block number `block`, byte position inside
318351 /// block `byte`, and block size `bs`.
352+ ///
353+ /// # Errors
354+ /// Returns [`OverflowError`] in the event of a counter overflow.
319355 fn from_block_byte < T : StreamCipherCounter > (
320356 block : T ,
321357 byte : u8 ,
322358 bs : u8 ,
323359 ) -> Result < Self , OverflowError > ;
324360
325361 /// Try to get block number and bytes position for given block size `bs`.
362+ ///
363+ /// # Errors
364+ /// Returns [`OverflowError`] in the event of a counter overflow.
326365 fn into_block_byte < T : StreamCipherCounter > ( self , bs : u8 ) -> Result < ( T , u8 ) , OverflowError > ;
327366}
328367
0 commit comments