Skip to content

Commit 9ec1985

Browse files
Refactor: Replace ConstDefault with Default for various structures and clean up dependencies
1 parent 9a1e6b4 commit 9ec1985

File tree

15 files changed

+112
-80
lines changed

15 files changed

+112
-80
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ getrandom = { version = "0.3", default-features = false }
5252
pki-types = { package = "rustls-pki-types", version = "1.12.0", default-features = false }
5353
preinterpret = "0.2.0"
5454
rand_core = { version = "0.9.3", default-features = false, features = [
55-
"os_rng"
55+
"os_rng",
5656
], optional = true }
5757
rustls = { version = "0.23.31", default-features = false }
5858
webpki = { package = "rustls-webpki", version = "0.103.4", default-features = false, optional = true }
59-
paste = "1.0.15"
6059

6160

6261
[dev-dependencies]
@@ -65,9 +64,11 @@ itertools = { version = "0.14.0", default-features = false }
6564
rsa_098 = { package = "rsa", version = "0.9.8", features = ["sha2"] }
6665
signature_220 = { package = "signature", version = "2.2.0" }
6766
rustls = { version = "0.23.31", default-features = false, features = ["std"] }
68-
spki = { version = "0.8.0-rc.4", default-features = false, features = ["alloc"] }
67+
spki = { version = "0.8.0-rc.4", default-features = false, features = [
68+
"alloc",
69+
] }
6970
x509-cert = { version = "0.2.5", default-features = false, features = [
70-
"builder"
71+
"builder",
7172
] }
7273
rand_core_064 = { package = "rand_core", version = "0.6.4" }
7374
p256_0132 = { package = "p256", version = "0.13.2" }
@@ -226,7 +227,11 @@ aes-ccm = ["aes", "ccm"]
226227
aes-gcm = ["dep:aes-gcm", "aes", "gcm"]
227228
ccm = ["dep:ccm"]
228229
chacha20poly1305 = ["dep:chacha20poly1305"]
229-
elliptic-curve = ["dep:elliptic-curve", "elliptic-curve/ecdh", "elliptic-curve/sec1"]
230+
elliptic-curve = [
231+
"dep:elliptic-curve",
232+
"elliptic-curve/ecdh",
233+
"elliptic-curve/sec1",
234+
]
230235
gcm = []
231236
rand = ["dep:rand_core", "signature?/rand_core", "x25519-dalek?/os_rng"]
232237
signature = ["dep:signature"]

src/hash.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[cfg(feature = "alloc")]
22
use alloc::boxed::Box;
3-
use const_default::ConstDefault;
43

54
use core::marker::PhantomData;
65
use digest::{Digest, OutputSizeUser};
@@ -12,11 +11,17 @@ pub trait HashAlgorithm {
1211
}
1312

1413
// Generic hash implementation
15-
#[derive(ConstDefault)]
14+
#[derive(Default)]
1615
pub struct GenericHash<H> {
1716
_phantom: PhantomData<H>,
1817
}
1918

19+
impl<H> GenericHash<H> {
20+
pub const DEFAULT: Self = Self {
21+
_phantom: PhantomData,
22+
};
23+
}
24+
2025
impl<H> hash::Hash for GenericHash<H>
2126
where
2227
H: Digest + OutputSizeUser + Clone + Send + Sync + 'static + HashAlgorithm,

src/kx/nist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ where
6969
.map_err(|_| Error::General("Failed to generate private key".into()))?;
7070

7171
Ok(Box::new(NistKeyExchange::<C> {
72-
pub_key: priv_key.public_key().to_sec1_bytes().into(),
72+
pub_key: priv_key.public_key().to_sec1_bytes(),
7373
priv_key,
7474
}))
7575
}

src/misc.rs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
1-
#[macro_export]
2-
macro_rules! const_concat_slices {
3-
($ty:ty, $a:expr, $b:expr $(,)*) => {{
4-
const A: &[$ty] = $a;
5-
const B: &[$ty] = $b;
6-
const __LEN: usize = A.len() + B.len();
7-
const __CONCATENATED: &[$ty; __LEN] = &{
8-
let mut out: [$ty; __LEN] = if __LEN == 0 {
9-
unsafe { core::mem::transmute([0u8; core::mem::size_of::<$ty>() * __LEN]) }
10-
} else if A.len() == 0 {
11-
[B[0]; __LEN]
12-
} else {
13-
[A[0]; __LEN]
14-
};
15-
let mut i = 0;
16-
while i < A.len() {
17-
out[i] = A[i];
18-
i += 1;
19-
}
20-
i = 0;
21-
while i < B.len() {
22-
out[i + A.len()] = B[i];
23-
i += 1;
24-
}
25-
out
26-
};
27-
28-
__CONCATENATED
29-
}};
30-
($ty:ty, $a:expr, $b:expr, $($c:expr), + $(,)* ) => {{
31-
const CON: &[$ty] = const_concat_slices!($ty, $a, $b);
32-
const_concat_slices!($ty, CON, $($c), +)
33-
}}
34-
}
35-
36-
pub(crate) use const_concat_slices;
1+
#[macro_export]
2+
macro_rules! const_concat_slices {
3+
($ty:ty, $a:expr, $b:expr $(,)*) => {
4+
const {
5+
const A: &[$ty] = $a;
6+
const B: &[$ty] = $b;
7+
const __LEN: usize = A.len() + B.len();
8+
const __CONCATENATED: &[$ty; __LEN] = &const {
9+
let mut out: [$ty; __LEN] = if __LEN == 0 {
10+
unsafe { core::mem::transmute::<[u8; {core::mem::size_of::<$ty>() * __LEN}], [$ty; __LEN]>([0u8; core::mem::size_of::<$ty>() * __LEN]) }
11+
} else if A.len() == 0 {
12+
[B[0]; __LEN]
13+
} else {
14+
[A[0]; __LEN]
15+
};
16+
let mut i = 0;
17+
while i < A.len() {
18+
out[i] = A[i];
19+
i += 1;
20+
}
21+
i = 0;
22+
while i < B.len() {
23+
out[i + A.len()] = B[i];
24+
i += 1;
25+
}
26+
out
27+
};
28+
29+
__CONCATENATED
30+
}
31+
};
32+
($ty:ty, $a:expr, $b:expr, $($c:expr), + $(,)* ) => {
33+
const {
34+
const CON: &[$ty] = const_concat_slices!($ty, $a, $b);
35+
const_concat_slices!($ty, CON, $($c), +)
36+
}
37+
}
38+
}
39+
40+
pub(crate) use const_concat_slices;

src/sign/ecdsa/nist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ where
4242
// #[cfg(feature = "sec1")]
4343
// PrivateKeyDer::Sec1(sec1) => SK::from_sec1_der(sec1.secret_sec1_der())
4444
// .map_err(|e| format!("failed to decrypt private key: {e}")),
45-
PrivateKeyDer::Pkcs1(_) => Err(format!("ECDSA does not support PKCS#1 key")),
45+
PrivateKeyDer::Pkcs1(_) => Err("ECDSA does not support PKCS#1 key".into()),
4646
_ => Err("not supported".into()),
4747
};
4848
pkey.map(|kp| Self {

src/tls12/aead/ccm.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use const_default::ConstDefault;
2-
31
use super::common::Tls12AeadAlgorithmWithExplicitNonce;
42

53
pub const AES_128_CCM: &Tls12AeadAlgorithmWithExplicitNonce<crate::aead::aes::Aes128Ccm> =
6-
&ConstDefault::DEFAULT;
4+
&Tls12AeadAlgorithmWithExplicitNonce::DEFAULT;
75
pub const AES_256_CCM: &Tls12AeadAlgorithmWithExplicitNonce<crate::aead::aes::Aes256Ccm> =
8-
&ConstDefault::DEFAULT;
6+
&Tls12AeadAlgorithmWithExplicitNonce::DEFAULT;
97
pub const AES_128_CCM_8: &Tls12AeadAlgorithmWithExplicitNonce<crate::aead::aes::Aes128Ccm8> =
10-
&ConstDefault::DEFAULT;
8+
&Tls12AeadAlgorithmWithExplicitNonce::DEFAULT;
119
pub const AES_256_CCM_8: &Tls12AeadAlgorithmWithExplicitNonce<crate::aead::aes::Aes256Ccm8> =
12-
&ConstDefault::DEFAULT;
10+
&Tls12AeadAlgorithmWithExplicitNonce::DEFAULT;

src/tls12/aead/common.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use alloc::boxed::Box;
55

66
use ::aead::{AeadInOut, Nonce, Tag};
77
use ::crypto_common::KeyInit;
8-
use const_default::ConstDefault;
98
use rustls::ConnectionTrafficSecrets;
109
use rustls::crypto::cipher::{
1110
self, AeadKey, InboundOpaqueMessage, InboundPlainMessage, KeyBlockShape, MessageDecrypter,
@@ -127,12 +126,19 @@ pub trait Extractor {
127126

128127
impl Extractor for () {}
129128

130-
#[derive(ConstDefault)]
129+
#[derive(Default)]
131130
pub struct Tls12AeadAlgorithmWithExplicitNonce<A, E = ()> {
132131
_aead: PhantomData<A>,
133132
_extractor: PhantomData<E>,
134133
}
135134

135+
impl<A, E> Tls12AeadAlgorithmWithExplicitNonce<A, E> {
136+
pub const DEFAULT: Self = Self {
137+
_aead: PhantomData,
138+
_extractor: PhantomData,
139+
};
140+
}
141+
136142
impl<A, E> Tls12AeadAlgorithm for Tls12AeadAlgorithmWithExplicitNonce<A, E>
137143
where
138144
A: KeyInit + AeadInOut + Send + Sync + 'static,

src/tls12/aead/gcm.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use const_default::ConstDefault;
21
use rustls::ConnectionTrafficSecrets;
32
use rustls::crypto::cipher::{self, AeadKey, Iv};
43

@@ -35,8 +34,8 @@ impl Extractor for Aes256Extractor {
3534
pub const AES_128_GCM: &Tls12AeadAlgorithmWithExplicitNonce<
3635
crate::aead::aes::Aes128Gcm,
3736
Aes128Extractor,
38-
> = &ConstDefault::DEFAULT;
37+
> = &Tls12AeadAlgorithmWithExplicitNonce::DEFAULT;
3938
pub const AES_256_GCM: &Tls12AeadAlgorithmWithExplicitNonce<
4039
crate::aead::aes::Aes256Gcm,
4140
Aes256Extractor,
42-
> = &ConstDefault::DEFAULT;
41+
> = &Tls12AeadAlgorithmWithExplicitNonce::DEFAULT;

src/tls13/aead/ccm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use const_default::ConstDefault;
2-
31
use super::common::Tls13AeadAlgorithmCommon;
42
use crate::aead;
53

6-
pub const AES_128_CCM: &Tls13AeadAlgorithmCommon<aead::aes::Aes128Ccm> = &ConstDefault::DEFAULT;
7-
pub const AES_128_CCM_8: &Tls13AeadAlgorithmCommon<aead::aes::Aes128Ccm8> = &ConstDefault::DEFAULT;
4+
pub const AES_128_CCM: &Tls13AeadAlgorithmCommon<aead::aes::Aes128Ccm> =
5+
&Tls13AeadAlgorithmCommon::DEFAULT;
6+
pub const AES_128_CCM_8: &Tls13AeadAlgorithmCommon<aead::aes::Aes128Ccm8> =
7+
&Tls13AeadAlgorithmCommon::DEFAULT;

0 commit comments

Comments
 (0)