Skip to content

Commit c7e45f5

Browse files
committed
chore: Enable the unreachable_pub Rust lint
1 parent 93cae42 commit c7e45f5

16 files changed

Lines changed: 122 additions & 86 deletions

File tree

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ unsafe_op_in_unsafe_fn = "deny"
2727
unused_extern_crates = "deny"
2828
unused_import_braces = "deny"
2929
unused_qualifications = "deny"
30+
unreachable_pub = "deny"
3031
rust_2018_idioms = "deny"
3132

3233
[lints.clippy]

‎src/cipher/key.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ pub(crate) struct CipherKeys {
7070
}
7171

7272
impl CipherKeys {
73-
pub fn new(message_key: &[u8; 32]) -> Self {
73+
pub(crate) fn new(message_key: &[u8; 32]) -> Self {
7474
let expanded_keys = ExpandedKeys::new(message_key);
7575

7676
Self::from_expanded_keys(expanded_keys)
7777
}
7878

79-
pub fn new_megolm(message_key: &[u8; 128]) -> Self {
79+
pub(crate) fn new_megolm(message_key: &[u8; 128]) -> Self {
8080
let expanded_keys = ExpandedKeys::new_megolm(message_key);
8181

8282
Self::from_expanded_keys(expanded_keys)
8383
}
8484

85-
pub fn new_pickle(pickle_key: &[u8]) -> Self {
85+
pub(crate) fn new_pickle(pickle_key: &[u8]) -> Self {
8686
let expanded_keys = ExpandedKeys::new_pickle(pickle_key);
8787

8888
Self::from_expanded_keys(expanded_keys)
@@ -100,16 +100,16 @@ impl CipherKeys {
100100
Self { aes_key, aes_iv, mac_key }
101101
}
102102

103-
pub fn aes_key(&self) -> &Aes256Key {
103+
pub(crate) fn aes_key(&self) -> &Aes256Key {
104104
#[allow(deprecated)]
105105
Aes256Key::from_slice(self.aes_key.as_slice())
106106
}
107107

108-
pub const fn mac_key(&self) -> &HmacSha256Key {
108+
pub(crate) const fn mac_key(&self) -> &HmacSha256Key {
109109
&self.mac_key
110110
}
111111

112-
pub fn iv(&self) -> &Aes256Iv {
112+
pub(crate) fn iv(&self) -> &Aes256Iv {
113113
#[allow(deprecated)]
114114
Aes256Iv::from_slice(self.aes_iv.as_slice())
115115
}

‎src/cipher/mod.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,20 @@ pub(crate) type HmacSha256 = Hmac<Sha256>;
3333

3434
/// The message authentication code of a ciphertext.
3535
#[derive(Debug, Clone, PartialEq, Eq)]
36+
#[allow(unreachable_pub)]
3637
pub struct Mac(pub(crate) [u8; Self::LENGTH]);
3738

3839
impl Mac {
3940
/// The expected length of the message authentication code (MAC).
41+
#[allow(unreachable_pub)]
4042
pub const LENGTH: usize = 32;
4143
/// The expected length of the message authentication code (MAC) if
4244
/// truncation is applied.
45+
#[allow(unreachable_pub)]
4346
pub const TRUNCATED_LEN: usize = 8;
4447

4548
/// Truncates and converts the [`Mac`] into a byte array.
49+
#[allow(unreachable_pub)]
4650
pub fn truncate(&self) -> [u8; Self::TRUNCATED_LEN] {
4751
let mut truncated = [0u8; Self::TRUNCATED_LEN];
4852
truncated.copy_from_slice(&self.0[0..Self::TRUNCATED_LEN]);
@@ -51,6 +55,7 @@ impl Mac {
5155
}
5256

5357
/// Return the [`Mac`] as a byte slice.
58+
#[allow(unreachable_pub)]
5459
pub fn as_bytes(&self) -> &[u8] {
5560
self.0.as_ref()
5661
}
@@ -63,7 +68,7 @@ pub(crate) enum MessageMac {
6368
}
6469

6570
impl MessageMac {
66-
pub fn as_bytes(&self) -> &[u8] {
71+
pub(crate) fn as_bytes(&self) -> &[u8] {
6772
match self {
6873
MessageMac::Truncated(m) => m.as_ref(),
6974
MessageMac::Full(m) => m.as_bytes(),
@@ -94,6 +99,7 @@ pub enum DecryptionError {
9499
}
95100

96101
/// A cipher used for encrypting and decrypting messages.
102+
#[allow(unreachable_pub)]
97103
pub struct Cipher {
98104
keys: CipherKeys,
99105
}
@@ -107,6 +113,7 @@ impl Cipher {
107113
///
108114
/// This key derivation format is typically used for generating individual
109115
/// message keys in the Olm double ratchet.
116+
#[allow(unreachable_pub)]
110117
pub fn new(key: &[u8; 32]) -> Self {
111118
let keys = CipherKeys::new(key);
112119

@@ -122,6 +129,7 @@ impl Cipher {
122129
///
123130
/// This key derivation format is typically used for generating individual
124131
/// message keys in the Megolm ratchet.
132+
#[allow(unreachable_pub)]
125133
pub fn new_megolm(&key: &[u8; 128]) -> Self {
126134
let keys = CipherKeys::new_megolm(&key);
127135

@@ -138,6 +146,7 @@ impl Cipher {
138146
///
139147
/// This key derivation format is typically used for libolm-compatible
140148
/// encrypted pickle formats.
149+
#[allow(unreachable_pub)]
141150
pub fn new_pickle(key: &[u8]) -> Self {
142151
let keys = CipherKeys::new_pickle(key);
143152

@@ -160,6 +169,7 @@ impl Cipher {
160169
/// **Warning**: This is a low-level function and does not provide
161170
/// authentication for the ciphertext. You must call [`Cipher::mac()`]
162171
/// separately to generate the message authentication code (MAC).
172+
#[allow(unreachable_pub)]
163173
pub fn encrypt(&self, plaintext: &[u8]) -> Vec<u8> {
164174
let cipher = Aes256CbcEnc::new(self.keys.aes_key(), self.keys.iv());
165175
cipher.encrypt_padded_vec::<Pkcs7>(plaintext)
@@ -170,6 +180,7 @@ impl Cipher {
170180
/// **Warning**: This is a low-level function and must be called after the
171181
/// [`Cipher::encrypt`] method. The ciphertext produced by
172182
/// [`Cipher::encrypt`] must be passed as the argument to this method.
183+
#[allow(unreachable_pub)]
173184
pub fn mac(&self, message: &[u8]) -> Mac {
174185
let mut hmac = self.get_hmac();
175186
hmac.update(message);
@@ -187,6 +198,7 @@ impl Cipher {
187198
/// **Warning**: This is a low-level function. Before calling this, you must
188199
/// call [`Cipher::verify_mac()`] or [`Cipher::verify_truncated_mac()`]
189200
/// to ensure the integrity of the ciphertext.
201+
#[allow(unreachable_pub)]
190202
pub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>, UnpadError> {
191203
let cipher = Aes256CbcDec::new(self.keys.aes_key(), self.keys.iv());
192204
cipher.decrypt_padded_vec::<Pkcs7>(ciphertext)
@@ -198,6 +210,7 @@ impl Cipher {
198210
/// **Warning**: This is a low-level function and must be called before
199211
/// invoking the [`Cipher::decrypt()`] method.
200212
#[cfg(all(not(fuzzing), feature = "experimental-session-config"))]
213+
#[allow(unreachable_pub)]
201214
pub fn verify_mac(&self, message: &[u8], tag: &Mac) -> Result<(), MacError> {
202215
let mut hmac = self.get_hmac();
203216

@@ -211,6 +224,7 @@ impl Cipher {
211224
/// **Warning**: This is a low-level function and must be called before
212225
/// invoking the [`Cipher::decrypt()`] method.
213226
#[cfg(not(fuzzing))]
227+
#[allow(unreachable_pub)]
214228
pub fn verify_truncated_mac(&self, message: &[u8], tag: &[u8]) -> Result<(), MacError> {
215229
let mut hmac = self.get_hmac();
216230

@@ -251,6 +265,7 @@ impl Cipher {
251265
/// message authentication tag to it.
252266
///
253267
/// This follows the encryption method used by the libolm pickle format.
268+
#[allow(unreachable_pub)]
254269
pub fn encrypt_pickle(&self, plaintext: &[u8]) -> Vec<u8> {
255270
let mut ciphertext = self.encrypt(plaintext);
256271
let mac = self.mac(&ciphertext);
@@ -267,6 +282,7 @@ impl Cipher {
267282
/// MAC before decrypting the ciphertext.
268283
///
269284
/// This follows the encryption method used by the libolm pickle format.
285+
#[allow(unreachable_pub)]
270286
pub fn decrypt_pickle(&self, ciphertext: &[u8]) -> Result<Vec<u8>, DecryptionError> {
271287
if ciphertext.len() < Mac::TRUNCATED_LEN + 1 {
272288
Err(DecryptionError::MacMissing)

‎src/megolm/ratchet.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ impl<'a> RatchetParts<'a> {
134134
}
135135

136136
impl Ratchet {
137-
pub const RATCHET_LENGTH: usize = 128;
137+
pub(super) const RATCHET_LENGTH: usize = 128;
138138
const RATCHET_PART_COUNT: usize = 4;
139139
const LAST_RATCHET_INDEX: usize = Self::RATCHET_PART_COUNT - 1;
140140

141-
pub fn new() -> Self {
141+
pub(super) fn new() -> Self {
142142
let mut rng = rng();
143143

144144
let mut ratchet =
@@ -149,20 +149,20 @@ impl Ratchet {
149149
ratchet
150150
}
151151

152-
pub const fn from_bytes(bytes: Box<[u8; Self::RATCHET_LENGTH]>, counter: u32) -> Self {
152+
pub(super) const fn from_bytes(bytes: Box<[u8; Self::RATCHET_LENGTH]>, counter: u32) -> Self {
153153
Self { inner: RatchetBytes(bytes), counter }
154154
}
155155

156-
pub const fn index(&self) -> u32 {
156+
pub(super) const fn index(&self) -> u32 {
157157
self.counter
158158
}
159159

160-
pub const fn as_bytes(&self) -> &[u8; Self::RATCHET_LENGTH] {
160+
pub(super) const fn as_bytes(&self) -> &[u8; Self::RATCHET_LENGTH] {
161161
&self.inner.0
162162
}
163163

164164
#[cfg(test)]
165-
pub const fn ratchet_bytes_pointer(&self) -> *const [u8; 128] {
165+
pub(super) const fn ratchet_bytes_pointer(&self) -> *const [u8; 128] {
166166
&*self.inner.0
167167
}
168168

@@ -180,7 +180,7 @@ impl Ratchet {
180180
RatchetParts { r_0, r_1, r_2, r_3 }
181181
}
182182

183-
pub fn advance(&mut self) {
183+
pub(super) fn advance(&mut self) {
184184
let mut mask: u32 = 0x00FFFFFF;
185185

186186
// The index of the "slowest" part of the ratchet that needs to be
@@ -208,7 +208,7 @@ impl Ratchet {
208208
}
209209
}
210210

211-
pub fn advance_to(&mut self, advance_to: u32) {
211+
pub(super) fn advance_to(&mut self, advance_to: u32) {
212212
for j in 0..Self::RATCHET_PART_COUNT {
213213
let shift = (Self::LAST_RATCHET_INDEX - j) * 8;
214214
let mask: u32 = !0u32 << shift;

‎src/olm/account/fallback_keys.rs‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ impl FallbackKey {
3333
Self { key_id, key, published: false }
3434
}
3535

36-
pub fn public_key(&self) -> Curve25519PublicKey {
36+
pub(super) fn public_key(&self) -> Curve25519PublicKey {
3737
Curve25519PublicKey::from(&self.key)
3838
}
3939

40-
pub const fn secret_key(&self) -> &Curve25519SecretKey {
40+
pub(super) const fn secret_key(&self) -> &Curve25519SecretKey {
4141
&self.key
4242
}
4343

44-
pub const fn key_id(&self) -> KeyId {
44+
pub(super) const fn key_id(&self) -> KeyId {
4545
self.key_id
4646
}
4747

48-
pub fn mark_as_published(&mut self) {
48+
pub(super) fn mark_as_published(&mut self) {
4949
self.published = true;
5050
}
5151

52-
pub const fn published(&self) -> bool {
52+
pub(super) const fn published(&self) -> bool {
5353
self.published
5454
}
5555
}
@@ -62,17 +62,17 @@ pub(super) struct FallbackKeys {
6262
}
6363

6464
impl FallbackKeys {
65-
pub const fn new() -> Self {
65+
pub(super) const fn new() -> Self {
6666
Self { key_id: 0, fallback_key: None, previous_fallback_key: None }
6767
}
6868

69-
pub fn mark_as_published(&mut self) {
69+
pub(super) fn mark_as_published(&mut self) {
7070
if let Some(f) = self.fallback_key.as_mut() {
7171
f.mark_as_published()
7272
}
7373
}
7474

75-
pub fn generate_fallback_key(&mut self) -> Option<Curve25519PublicKey> {
75+
pub(super) fn generate_fallback_key(&mut self) -> Option<Curve25519PublicKey> {
7676
let key_id = KeyId(self.key_id);
7777
self.key_id += 1;
7878

@@ -84,7 +84,10 @@ impl FallbackKeys {
8484
ret
8585
}
8686

87-
pub fn get_secret_key(&self, public_key: &Curve25519PublicKey) -> Option<&Curve25519SecretKey> {
87+
pub(super) fn get_secret_key(
88+
&self,
89+
public_key: &Curve25519PublicKey,
90+
) -> Option<&Curve25519SecretKey> {
8891
self.fallback_key
8992
.as_ref()
9093
.filter(|f| f.public_key() == *public_key)
@@ -94,11 +97,11 @@ impl FallbackKeys {
9497
.map(|f| f.secret_key())
9598
}
9699

97-
pub fn forget_previous_fallback_key(&mut self) -> Option<FallbackKey> {
100+
pub(super) fn forget_previous_fallback_key(&mut self) -> Option<FallbackKey> {
98101
self.previous_fallback_key.take()
99102
}
100103

101-
pub fn unpublished_fallback_key(&self) -> Option<&FallbackKey> {
104+
pub(super) fn unpublished_fallback_key(&self) -> Option<&FallbackKey> {
102105
self.fallback_key.as_ref().filter(|f| !f.published())
103106
}
104107
}

‎src/olm/account/one_time_keys.rs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct OneTimeKeyGenerationResult {
4444
impl OneTimeKeys {
4545
const MAX_ONE_TIME_KEYS: usize = 100 * PUBLIC_MAX_ONE_TIME_KEYS;
4646

47-
pub fn new() -> Self {
47+
pub(super) fn new() -> Self {
4848
Self {
4949
next_key_id: 0,
5050
unpublished_public_keys: Default::default(),
@@ -53,15 +53,18 @@ impl OneTimeKeys {
5353
}
5454
}
5555

56-
pub fn mark_as_published(&mut self) {
56+
pub(super) fn mark_as_published(&mut self) {
5757
self.unpublished_public_keys.clear();
5858
}
5959

60-
pub fn get_secret_key(&self, public_key: &Curve25519PublicKey) -> Option<&Curve25519SecretKey> {
60+
pub(super) fn get_secret_key(
61+
&self,
62+
public_key: &Curve25519PublicKey,
63+
) -> Option<&Curve25519SecretKey> {
6164
self.key_ids_by_key.get(public_key).and_then(|key_id| self.private_keys.get(key_id))
6265
}
6366

64-
pub fn remove_secret_key(
67+
pub(super) fn remove_secret_key(
6568
&mut self,
6669
public_key: &Curve25519PublicKey,
6770
) -> Option<Curve25519SecretKey> {
@@ -127,7 +130,7 @@ impl OneTimeKeys {
127130
!self.unpublished_public_keys.contains_key(key_id)
128131
}
129132

130-
pub fn generate(&mut self, count: usize) -> OneTimeKeyGenerationResult {
133+
pub(super) fn generate(&mut self, count: usize) -> OneTimeKeyGenerationResult {
131134
let mut removed_keys = Vec::new();
132135
let mut created_keys = Vec::new();
133136

0 commit comments

Comments
 (0)