Skip to content

Commit 8834f33

Browse files
authored
Workspace-level lint configuration (#194)
Enables and fixes the same workspace-level lint configuration we originally implemented in RustCrypto/utils#1411, but for this repo. This primarily includes `clippy` lints, which have replaced the ones configured in individual lib.rs files. Since every crate in this repo previously declared `forbid(unsafe_code)` this has also been moved to the workspace-level.
1 parent b802abc commit 8834f33

26 files changed

Lines changed: 184 additions & 32 deletions

File tree

.clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allow-unwrap-in-consts = true
2+
allow-unwrap-in-tests = true

Cargo.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,43 @@ members = [
1010

1111
[profile.dev]
1212
opt-level = 2
13+
14+
[workspace.lints.clippy]
15+
borrow_as_ptr = "warn"
16+
cast_lossless = "warn"
17+
cast_possible_truncation = "warn"
18+
cast_possible_wrap = "warn"
19+
cast_precision_loss = "warn"
20+
cast_sign_loss = "warn"
21+
checked_conversions = "warn"
22+
doc_markdown = "warn"
23+
from_iter_instead_of_collect = "warn"
24+
implicit_saturating_sub = "warn"
25+
manual_assert = "warn"
26+
map_unwrap_or = "warn"
27+
missing_errors_doc = "warn"
28+
missing_panics_doc = "warn"
29+
mod_module_files = "warn"
30+
must_use_candidate = "warn"
31+
needless_range_loop = "allow"
32+
ptr_as_ptr = "warn"
33+
redundant_closure_for_method_calls = "warn"
34+
ref_as_ptr = "warn"
35+
return_self_not_must_use = "warn"
36+
semicolon_if_nothing_returned = "warn"
37+
trivially_copy_pass_by_ref = "warn"
38+
std_instead_of_alloc = "warn"
39+
std_instead_of_core = "warn"
40+
undocumented_unsafe_blocks = "warn"
41+
unnecessary_safety_comment = "warn"
42+
unwrap_used = "warn"
43+
44+
[workspace.lints.rust]
45+
missing_copy_implementations = "warn"
46+
missing_debug_implementations = "warn"
47+
missing_docs = "warn"
48+
trivial_casts = "warn"
49+
trivial_numeric_casts = "warn"
50+
unsafe_code = "forbid"
51+
unused_lifetimes = "warn"
52+
unused_qualifications = "warn"

ansi-x963-kdf/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ digest = "0.11"
1818
[dev-dependencies]
1919
hex-literal = "1"
2020
sha2 = { version = "0.11", default-features = false }
21+
22+
[lints]
23+
workspace = true

ansi-x963-kdf/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RustCrypto: ANSI X9.63 Key Derivation Function
1+
# [RustCrypto]: ANSI X9.63 Key Derivation Function
22

33
[![crate][crate-image]][crate-link]
44
[![Docs][docs-image]][docs-link]
@@ -39,6 +39,8 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
3939
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
4040
dual licensed as above, without any additional terms or conditions.
4141

42+
[//]: # (badges)
43+
4244
[crate-image]: https://img.shields.io/crates/v/ansi-x963-kdf.svg?logo=rust
4345
[crate-link]: https://crates.io/crates/ansi-x963-kdf
4446
[docs-image]: https://docs.rs/ansi-x963-kdf/badge.svg
@@ -49,3 +51,7 @@ dual licensed as above, without any additional terms or conditions.
4951
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg
5052
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
5153
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260043-KDFs
54+
55+
[//]: # (links)
56+
57+
[RustCrypto]: https://github.com/RustCrypto

ansi-x963-kdf/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
66
)]
77
#![cfg_attr(docsrs, feature(doc_cfg))]
8-
#![forbid(unsafe_code)]
9-
#![warn(missing_docs)]
108

119
use core::fmt;
1210
use digest::{Digest, FixedOutputReset, array::typenum::Unsigned};
@@ -22,6 +20,10 @@ use digest::{Digest, FixedOutputReset, array::typenum::Unsigned};
2220
/// ansi_x963_kdf::derive_key_into::<Sha256>(b"secret", b"shared-info", &mut key).unwrap();
2321
/// assert_eq!(key, hex!("8dbb1d50bcc7fc782abc9db5c64a2826"));
2422
/// ```
23+
///
24+
/// # Errors
25+
/// - Returns [`Error::InputOverflow`] if too much input is provided
26+
/// - Returns [`Error::CounterOverflow`] if `key` is too long
2527
#[inline]
2628
pub fn derive_key_into<D>(secret: &[u8], shared_info: &[u8], key: &mut [u8]) -> Result<(), Error>
2729
where
@@ -38,13 +40,14 @@ where
3840
// 1. Check that |Z| + |SharedInfo| + 4 < hashmaxlen
3941
// where "hashmaxlen denote the maximum length in octets of messages that can be hashed using Hash".
4042
// N.B.: `D::OutputSize::U64 * (u32::MAX as u64)`` is currently used as an approximation of hashmaxlen.
41-
if secret.len() as u64 + shared_info.len() as u64 + 4 >= D::OutputSize::U64 * (u32::MAX as u64)
43+
if secret.len() as u64 + shared_info.len() as u64 + 4
44+
>= D::OutputSize::U64 * u64::from(u32::MAX)
4245
{
4346
return Err(Error::InputOverflow);
4447
}
4548

4649
// 2. Check that keydatalen < hashlen × (2^32 − 1)
47-
if key.len() as u64 >= D::OutputSize::U64 * (u32::MAX as u64) {
50+
if key.len() as u64 >= D::OutputSize::U64 * u64::from(u32::MAX) {
4851
return Err(Error::CounterOverflow);
4952
}
5053

ansi-x963-kdf/tests/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! KDF2 implementation [KDF2BytesGenerator][1]
55
//!
66
//! [1]: https://downloads.bouncycastle.org/java/docs/bcprov-jdk18on-javadoc/
7+
#![allow(clippy::unwrap_used, reason = "tests")]
8+
79
use digest::{Digest, FixedOutputReset};
810
use hex_literal::hex;
911
use sha2::{Sha224, Sha256, Sha512};

bake-kdf/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ belt-hash = { version = "0.2.0-rc.5", default-features = false }
1818
[dev-dependencies]
1919
hex-literal = "1"
2020

21+
[lints]
22+
workspace = true
23+
2124
[package.metadata.docs.rs]
2225
all-features = true

bake-kdf/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RustCrypto: bake-kdf
1+
# [RustCrypto]: bake-kdf
22

33
[![crate][crate-image]][crate-link]
44
[![Docs][docs-image]][docs-link]
@@ -53,3 +53,7 @@ dual licensed as above, without any additional terms or conditions.
5353
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg
5454
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
5555
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260043-KDFs
56+
57+
[//]: # (links)
58+
59+
[RustCrypto]: https://github.com/RustCrypto

bake-kdf/src/lib.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
66
)]
77
#![cfg_attr(docsrs, feature(doc_cfg))]
8-
#![forbid(unsafe_code)]
9-
#![warn(missing_docs)]
8+
#![allow(clippy::unwrap_used, reason = "todo")]
109

1110
use belt_hash::digest::FixedOutput;
1211
use belt_hash::{BeltHash, Digest, block_api::belt_compress};
@@ -15,9 +14,13 @@ use belt_hash::{BeltHash, Digest, block_api::belt_compress};
1514
///
1615
/// # Panics
1716
/// If `N` is not equal to 16, 24, or 32.
18-
// TODO: use compile-time checks for `N`
1917
#[inline]
18+
#[must_use]
2019
pub fn belt_keyexpand<const N: usize>(k: &[u8; N]) -> [u32; 8] {
20+
const {
21+
assert!(matches!(N, 16 | 24 | 32), "N must be 16, 24, or 32");
22+
}
23+
2124
let mut t = [0u32; 8];
2225
// TODO: move this conversion into `belt_keyrep` when we will be able
2326
// to use generic parameters as `[u32; N / 4]`.
@@ -44,15 +47,30 @@ pub fn belt_keyexpand<const N: usize>(k: &[u8; N]) -> [u32; 8] {
4447
/// `belt-keyrep` key repetition algorithm described in STB 34.101.31-2020 8.1.3.
4548
///
4649
/// # Panics
47-
/// If `(N, M)` is not equal to `(16, 16)`, `(24, 16)`, `(24, 24)`,
48-
/// `(32, 16)`, `(32, 24)`, or `(32, 32)`.
49-
// TODO: use compile-time check for `N` and `M`
50+
/// If `(N, M)` is not equal to one of:
51+
/// - `(16, 16)`
52+
/// - `(24, 16)`
53+
/// - `(24, 24)`
54+
/// - `(32, 16)`
55+
/// - `(32, 24)`
56+
/// - `(32, 32)`
5057
#[inline]
58+
#[must_use]
5159
pub fn belt_keyrep<const N: usize, const M: usize>(
5260
x: &[u8; N],
5361
d: &[u8; 12],
5462
i: &[u8; 16],
5563
) -> [u8; M] {
64+
const {
65+
assert!(
66+
matches!(
67+
(N, M),
68+
(16, 16) | (24, 16) | (24, 24) | (32, 16) | (32, 24) | (32, 32)
69+
),
70+
"invalid N/M values"
71+
);
72+
}
73+
5674
let r: u32 = match (N, M) {
5775
(16, 16) => 0xC8BA94B1,
5876
(24, 16) => 0x12D6E35B,
@@ -89,6 +107,7 @@ pub fn belt_keyrep<const N: usize, const M: usize>(
89107

90108
/// `bake-kdf` key derivation algorithm described in STB 34.101.66-2014 8.1.4.
91109
#[inline]
110+
#[must_use]
92111
pub fn bake_kdf(x: &[u8], s: &[u8], c: u128) -> [u8; 32] {
93112
let mut hasher = BeltHash::default();
94113
hasher.update(x);

bake-kdf/tests/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Test vectors
2+
13
use bake_kdf::{bake_kdf, belt_keyexpand, belt_keyrep};
24
use hex_literal::hex;
35

0 commit comments

Comments
 (0)