Skip to content

Commit 7b5f4b4

Browse files
committed
fix(udp-tracker-core): align cipher with blowfish update
1 parent b90e091 commit 7b5f4b4

5 files changed

Lines changed: 14 additions & 37 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/udp-tracker-core/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ bittorrent-tracker-core = { version = "3.0.0-develop", path = "../tracker-core"
2020
bittorrent-udp-tracker-protocol = { version = "3.0.0-develop", path = "../udp-protocol" }
2121
bloom = "0.3.2"
2222
blowfish = "0"
23-
cipher = "0.4"
23+
cipher = "0.5"
2424
criterion = { version = "0.5.1", features = [ "async_tokio" ] }
2525
futures = "0"
26-
generic-array = "0"
2726
lazy_static = "1"
2827
rand = "0"
2928
serde = "1.0.219"

packages/udp-tracker-core/src/connection_cookie.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ use tracing::instrument;
8484
use zerocopy::AsBytes;
8585

8686
use crate::crypto::keys::CipherArrayBlowfish;
87-
8887
/// Error returned when there was an error with the connection cookie.
8988
#[derive(Error, Debug, Clone, PartialEq)]
9089
pub enum ConnectionCookieError {
@@ -140,8 +139,8 @@ use std::ops::Range;
140139
pub fn check(cookie: &Cookie, fingerprint: u64, valid_range: Range<f64>) -> Result<f64, ConnectionCookieError> {
141140
assert!(valid_range.start <= valid_range.end, "range start is larger than range end");
142141

143-
let cookie_bytes = CipherArrayBlowfish::from_slice(cookie.0.as_bytes());
144-
let cookie_bytes = decode(*cookie_bytes);
142+
let cookie_bytes = CipherArrayBlowfish::try_from(cookie.0.as_bytes()).expect("it should be the same size");
143+
let cookie_bytes = decode(cookie_bytes);
145144

146145
let issue_time = disassemble(fingerprint, cookie_bytes);
147146

@@ -176,7 +175,7 @@ pub fn gen_remote_fingerprint(remote_addr: &SocketAddr) -> u64 {
176175
}
177176

178177
mod cookie_builder {
179-
use cipher::{BlockDecrypt, BlockEncrypt};
178+
use cipher::{BlockCipherDecrypt, BlockCipherEncrypt};
180179
use tracing::instrument;
181180
use zerocopy::{byteorder, AsBytes as _, NativeEndian};
182181

@@ -196,7 +195,7 @@ mod cookie_builder {
196195
let cookie: byteorder::I64<NativeEndian> =
197196
*zerocopy::FromBytes::ref_from(&cookie.to_ne_bytes()).expect("it should be aligned");
198197

199-
*CipherArrayBlowfish::from_slice(cookie.as_bytes())
198+
CipherArrayBlowfish::try_from(cookie.as_bytes()).expect("it should be the same size")
200199
}
201200

202201
#[instrument()]

packages/udp-tracker-core/src/crypto/ephemeral_instance_keys.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
//! application starts and are not persisted anywhere.
55
66
use blowfish::BlowfishLE;
7-
use cipher::{BlockSizeUser, KeyInit};
8-
use generic_array::GenericArray;
7+
use cipher::{Block, KeyInit};
98
use rand::rngs::ThreadRng;
109
use rand::RngExt;
1110

1211
pub type Seed = [u8; 32];
1312
pub type CipherBlowfish = BlowfishLE;
14-
pub type CipherArrayBlowfish = GenericArray<u8, <CipherBlowfish as BlockSizeUser>::BlockSize>;
13+
pub type CipherArrayBlowfish = Block<CipherBlowfish>;
1514

1615
lazy_static! {
1716
/// The random static seed.

packages/udp-tracker-core/src/crypto/keys.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//!
66
//! It also provides the logic for the cipher for encryption and decryption.
77
8-
use cipher::{BlockDecrypt, BlockEncrypt};
8+
use cipher::{BlockCipherDecrypt, BlockCipherEncrypt};
99

1010
use self::detail_cipher::CURRENT_CIPHER;
1111
use self::detail_seed::CURRENT_SEED;
@@ -15,7 +15,7 @@ use crate::crypto::ephemeral_instance_keys::{CipherBlowfish, Seed, RANDOM_CIPHER
1515
/// This trait is for structures that can keep and provide a seed.
1616
pub trait Keeper {
1717
type Seed: Sized + Default + AsMut<[u8]>;
18-
type Cipher: BlockEncrypt + BlockDecrypt;
18+
type Cipher: BlockCipherEncrypt + BlockCipherDecrypt;
1919

2020
/// It returns a reference to the seed that is keeping.
2121
fn get_seed() -> &'static Self::Seed;
@@ -137,14 +137,14 @@ mod detail_cipher {
137137

138138
#[cfg(test)]
139139
mod tests {
140-
use cipher::BlockEncrypt;
140+
use cipher::BlockCipherEncrypt;
141141

142142
use crate::crypto::ephemeral_instance_keys::{CipherArrayBlowfish, ZEROED_TEST_CIPHER_BLOWFISH};
143143
use crate::crypto::keys::detail_cipher::CURRENT_CIPHER;
144144

145145
#[test]
146146
fn it_should_default_to_zeroed_seed_when_testing() {
147-
let mut data: cipher::generic_array::GenericArray<u8, _> = CipherArrayBlowfish::from([0u8; 8]);
147+
let mut data = CipherArrayBlowfish::from([0u8; 8]);
148148
let mut data_2 = CipherArrayBlowfish::from([0u8; 8]);
149149

150150
CURRENT_CIPHER.encrypt_block(&mut data);

0 commit comments

Comments
 (0)