Skip to content

Commit 8181ac7

Browse files
committed
cipher: use workspace level clippy config
Enables the workspace-level config added in #2270 and fixes violations
1 parent bbfe1af commit 8181ac7

File tree

11 files changed

+93
-32
lines changed

11 files changed

+93
-32
lines changed

cipher/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ hex-literal = "1"
2828
alloc = []
2929
block-padding = ["inout/block-padding"]
3030
stream-wrapper = ["block-buffer"]
31-
# Enable random key and IV generation methods
3231
getrandom = ["common/getrandom"]
3332
rand_core = ["common/rand_core"]
3433
dev = ["blobby"]
3534
zeroize = ["dep:zeroize", "common/zeroize", "block-buffer?/zeroize"]
3635

36+
[lints]
37+
workspace = true
38+
3739
[package.metadata.docs.rs]
3840
all-features = true

cipher/src/block.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub trait BlockCipherEncrypt: BlockSizeUser + Sized {
7070

7171
/// Encrypt blocks buffer-to-buffer.
7272
///
73+
/// # Errors
7374
/// Returns [`NotEqualError`] if provided `in_blocks` and `out_blocks`
7475
/// have different lengths.
7576
#[inline]
@@ -123,6 +124,7 @@ pub trait BlockCipherDecrypt: BlockSizeUser {
123124

124125
/// Decrypt blocks buffer-to-buffer.
125126
///
127+
/// # Errors
126128
/// Returns [`NotEqualError`] if provided `in_blocks` and `out_blocks`
127129
/// have different lengths.
128130
#[inline]
@@ -192,6 +194,7 @@ pub trait BlockModeEncrypt: BlockSizeUser + Sized {
192194

193195
/// Encrypt blocks buffer-to-buffer.
194196
///
197+
/// # Errors
195198
/// Returns [`NotEqualError`] if provided `in_blocks` and `out_blocks`
196199
/// have different lengths.
197200
#[inline]
@@ -206,6 +209,7 @@ pub trait BlockModeEncrypt: BlockSizeUser + Sized {
206209

207210
/// Pad input and encrypt. Returns resulting ciphertext slice.
208211
///
212+
/// # Errors
209213
/// Returns [`PadError`] if length of output buffer is not sufficient.
210214
#[cfg(feature = "block-padding")]
211215
#[inline]
@@ -223,6 +227,7 @@ pub trait BlockModeEncrypt: BlockSizeUser + Sized {
223227

224228
/// Pad input and encrypt in-place. Returns resulting ciphertext slice.
225229
///
230+
/// # Errors
226231
/// Returns [`PadError`] if length of output buffer is not sufficient.
227232
#[cfg(feature = "block-padding")]
228233
#[inline]
@@ -233,6 +238,7 @@ pub trait BlockModeEncrypt: BlockSizeUser + Sized {
233238

234239
/// Pad input and encrypt buffer-to-buffer. Returns resulting ciphertext slice.
235240
///
241+
/// # Errors
236242
/// Returns [`PadError`] if length of output buffer is not sufficient.
237243
#[cfg(feature = "block-padding")]
238244
#[inline]
@@ -262,12 +268,11 @@ pub trait BlockModeEncrypt: BlockSizeUser + Sized {
262268

263269
let pad_type_id = TypeId::of::<P>();
264270
let buf_blocks_len = if pad_type_id == TypeId::of::<NoPadding>() {
265-
if msg_len % bs != 0 {
266-
panic!(
267-
"NoPadding is used with a {msg_len}‑byte message,
268-
which is not a multiple of the {bs}‑byte cipher block size"
269-
);
270-
}
271+
assert!(
272+
msg_len % bs == 0,
273+
"NoPadding is used with a {msg_len}‑byte message,
274+
which is not a multiple of the {bs}‑byte cipher block size"
275+
);
271276
msg_len / bs
272277
} else if pad_type_id == TypeId::of::<ZeroPadding>() {
273278
msg_len.div_ceil(bs)
@@ -329,6 +334,7 @@ pub trait BlockModeDecrypt: BlockSizeUser + Sized {
329334

330335
/// Decrypt blocks buffer-to-buffer.
331336
///
337+
/// # Errors
332338
/// Returns [`NotEqualError`] if provided `in_blocks` and `out_blocks`
333339
/// have different lengths.
334340
#[inline]
@@ -343,6 +349,7 @@ pub trait BlockModeDecrypt: BlockSizeUser + Sized {
343349

344350
/// Decrypt input and unpad it. Returns resulting plaintext slice.
345351
///
352+
/// # Errors
346353
/// Returns [`block_padding::Error`] if padding is malformed or if input length is
347354
/// not multiple of `Self::BlockSize`.
348355
#[cfg(feature = "block-padding")]
@@ -361,6 +368,7 @@ pub trait BlockModeDecrypt: BlockSizeUser + Sized {
361368

362369
/// Decrypt input and unpad it in-place. Returns resulting plaintext slice.
363370
///
371+
/// # Errors
364372
/// Returns [`block_padding::Error`] if padding is malformed or if input length is
365373
/// not multiple of `Self::BlockSize`.
366374
#[cfg(feature = "block-padding")]
@@ -372,6 +380,7 @@ pub trait BlockModeDecrypt: BlockSizeUser + Sized {
372380
/// Decrypt input and unpad it buffer-to-buffer. Returns resulting
373381
/// plaintext slice.
374382
///
383+
/// # Errors
375384
/// Returns [`block_padding::Error`] if padding is malformed or if input length is
376385
/// not multiple of `Self::BlockSize`.
377386
#[cfg(feature = "block-padding")]
@@ -393,6 +402,7 @@ pub trait BlockModeDecrypt: BlockSizeUser + Sized {
393402
/// Decrypt input and unpad it in a newly allocated Vec. Returns resulting
394403
/// plaintext `Vec`.
395404
///
405+
/// # Errors
396406
/// Returns [`block_padding::Error`] if padding is malformed or if input length is
397407
/// not multiple of `Self::BlockSize`.
398408
#[cfg(all(feature = "block-padding", feature = "alloc"))]

cipher/src/dev/block_cipher.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Development-related functionality for block ciphers
22
3+
#![allow(clippy::missing_errors_doc)]
4+
35
use crate::{Block, BlockCipherDecrypt, BlockCipherEncrypt, KeyInit};
46

57
/// Block cipher test vector
@@ -24,6 +26,7 @@ fn encrypt_test_inner<C: BlockCipherEncrypt>(
2426
return Err("single block encryption failure");
2527
}
2628

29+
#[allow(clippy::cast_possible_truncation)]
2730
let mut blocks1: [Block<C>; 101] = core::array::from_fn(|i| {
2831
let mut block = pt.clone();
2932
block[0] ^= i as u8;
@@ -54,6 +57,7 @@ fn decrypt_test_inner<C: BlockCipherDecrypt>(
5457
return Err("single block decryption failure");
5558
}
5659

60+
#[allow(clippy::cast_possible_truncation)]
5761
let mut blocks1: [Block<C>; 101] = core::array::from_fn(|i| {
5862
let mut block = ct.clone();
5963
block[0] ^= i as u8;

cipher/src/dev/block_mode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Development-related functionality for block modes
22
3+
#![allow(clippy::missing_errors_doc)]
4+
35
use crate::{BlockModeDecrypt, BlockModeEncrypt, KeyIvInit, inout::InOutBuf};
46

57
const MAX_MSG_LEN: usize = 1 << 12;

cipher/src/dev/stream.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
//! Development-related functionality for stream ciphers
2+
3+
#![allow(clippy::missing_errors_doc)]
4+
25
use crate::{KeyIvInit, StreamCipher};
36

47
/// Stream cipher test vector

cipher/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
66
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
77
)]
8-
#![warn(
9-
missing_docs,
10-
rust_2018_idioms,
11-
unused_lifetimes,
12-
missing_debug_implementations
13-
)]
148
#![forbid(unsafe_code)]
159

1610
#[cfg(feature = "alloc")]

cipher/src/stream.rs

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
111117
pub 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 {
266295
pub 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 {
316349
pub 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

Comments
 (0)