Skip to content

Commit

Permalink
Migrate from paste
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Mar 7, 2025
1 parent 337a85d commit 690f25f
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 74 deletions.
2 changes: 1 addition & 1 deletion aws-lc-fips-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bindgen = { version = "0.69.5", optional = true }
bindgen = { version = "0.69.5" }

[dependencies]
paste = "1.0.11"
concat-idents = "1.1.5"

[dev-dependencies]
# Pinned dependency to preserve MSRV: 1.60.0 <= rust-version < 1.65.0
Expand Down
25 changes: 11 additions & 14 deletions aws-lc-fips-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@
#![cfg_attr(not(clippy), allow(unexpected_cfgs))]
#![cfg_attr(not(clippy), allow(unknown_lints))]

use paste::paste;
use concat_idents::concat_idents;
use std::os::raw::{c_char, c_long, c_void};

#[allow(unused_macros)]
macro_rules! use_bindings {
($bindings:ident) => {
mod $bindings;
pub use $bindings::*;
};
}

macro_rules! platform_binding {
($platform:ident) => {
paste! {
concat_idents!(bindings_name = $platform, _, crypto {
#[cfg(all($platform, not(feature = "ssl"), not(use_bindgen_generated)))]
use_bindings!([< $platform _crypto >]);

mod bindings_name;
#[cfg(all($platform, not(feature = "ssl"), not(use_bindgen_generated)))]
pub use bindings_name::*;
});
concat_idents!(bindings_name = $platform, _, crypto, _, ssl {
#[cfg(all($platform, feature = "ssl", not(use_bindgen_generated)))]
mod bindings_name;
#[cfg(all($platform, feature = "ssl", not(use_bindgen_generated)))]
use_bindings!([< $platform _crypto_ssl >]);
}
pub use bindings_name::*;
});
};
}

Expand Down
2 changes: 1 addition & 1 deletion aws-lc-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ untrusted = { version = "0.7.1", optional = true }
aws-lc-sys = { version = "0.27.0", path = "../aws-lc-sys", optional = true }
aws-lc-fips-sys = { version = "0.13.1", path = "../aws-lc-fips-sys", optional = true }
zeroize = "1.7"
paste = "1.0.11"
concat-idents = "1.1.5"

[dev-dependencies]
lazy_static = "1.4.0"
Expand Down
34 changes: 21 additions & 13 deletions aws-lc-rs/src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,9 @@
use crate::error::Unspecified;
use crate::{derive_debug_via_id, hkdf};
use aead_ctx::AeadCtx;
use concat_idents::concat_idents;
use core::fmt::Debug;
use core::ops::RangeFrom;
use core::stringify;
use paste::paste;

mod aead_ctx;
mod aes_gcm;
Expand Down Expand Up @@ -530,14 +529,14 @@ impl<N: NonceSequence> SealingKey<N> {

macro_rules! nonce_seq_key_op_mut {
($name:ident) => {
paste! {
concat_idents!( prepared_nonce_name = $name, PreparedNonce {
/// A key operation with a precomputed nonce from a key's associated `NonceSequence`.
pub struct [<$name PreparedNonce>]<'a, N: NonceSequence> {
pub struct prepared_nonce_name<'a, N: NonceSequence> {
key: &'a mut $name<N>,
nonce: Nonce,
}

impl<'a, N: NonceSequence> [<$name PreparedNonce>]<'a, N> {
impl<'a, N: NonceSequence> prepared_nonce_name<'a, N> {
fn new(key: &'a mut $name<N>) -> Result<Self, Unspecified> {
let nonce = key.nonce_sequence.advance()?;
Ok(Self {
Expand All @@ -547,26 +546,35 @@ macro_rules! nonce_seq_key_op_mut {
}
}

impl<N: NonceSequence> [<$name PreparedNonce>]<'_, N> {
impl<N: NonceSequence> prepared_nonce_name<'_, N> {
/// Returns the prepared Nonce that is used for key methods invoked on [Self].
#[must_use]
pub fn nonce(&self) -> &Nonce {
&self.nonce
}
}

impl<N: NonceSequence> Debug for [<$name PreparedNonce>]<'_, N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
f.debug_struct(stringify!([<$name PreparedNonce>])).finish_non_exhaustive()
}
}
}
};
});
}
}

nonce_seq_key_op_mut!(OpeningKey);
nonce_seq_key_op_mut!(SealingKey);

impl<N: NonceSequence> Debug for OpeningKeyPreparedNonce<'_, N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
f.debug_struct("OpeningKeyPreparedNonce")
.finish_non_exhaustive()
}
}

impl<N: NonceSequence> Debug for SealingKeyPreparedNonce<'_, N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
f.debug_struct("SealingKeyPreparedNonce")
.finish_non_exhaustive()
}
}

impl<N: NonceSequence> OpeningKeyPreparedNonce<'_, N> {
/// Authenticates and decrypts (“opens”) data in place.
///
Expand Down
15 changes: 8 additions & 7 deletions aws-lc-rs/src/aead/rand_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod tests {
use super::{Aad, RandomizedNonceKey};
use crate::aead::{AES_128_GCM, AES_256_GCM, CHACHA20_POLY1305};
use crate::test::from_hex;
use paste::paste;
use concat_idents::concat_idents;

const TEST_128_BIT_KEY: &[u8] = &[
0xb0, 0x37, 0x9f, 0xf8, 0xfb, 0x8e, 0xa6, 0x31, 0xf4, 0x1c, 0xe6, 0x3e, 0xb5, 0xc5, 0x20,
Expand All @@ -166,17 +166,18 @@ mod tests {

macro_rules! test_randnonce {
($name:ident, $alg:expr, $key:expr) => {
paste! {
concat_idents!(test_name_unsupported = test_, $name, _randnonce_unsupported {
#[test]
fn [<test_ $name _randnonce_unsupported>]() {
fn test_name_unsupported() {
assert!(RandomizedNonceKey::new($alg, $key).is_err());
}
}
});
};

($name:ident, $alg:expr, $key:expr, $expect_tag_len:expr, $expect_nonce_len:expr) => {
paste! {
concat_idents!(test_name = test_, $name, _randnonce {
#[test]
fn [<test_ $name _randnonce>]() {
fn test_name() {
let plaintext = from_hex("00112233445566778899aabbccddeeff").unwrap();
let rand_nonce_key =
RandomizedNonceKey::new($alg, $key).unwrap();
Expand Down Expand Up @@ -215,7 +216,7 @@ mod tests {

assert_eq!(plaintext, in_out[..plaintext.len()]);
}
}
});
};
}

Expand Down
14 changes: 7 additions & 7 deletions aws-lc-rs/src/aead/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ mod tests {
use super::{TlsProtocolId, TlsRecordOpeningKey, TlsRecordSealingKey};
use crate::aead::{Aad, Nonce, AES_128_GCM, AES_256_GCM, CHACHA20_POLY1305};
use crate::test::from_hex;
use paste::paste;
use concat_idents::concat_idents;

const TEST_128_BIT_KEY: &[u8] = &[
0xb0, 0x37, 0x9f, 0xf8, 0xfb, 0x8e, 0xa6, 0x31, 0xf4, 0x1c, 0xe6, 0x3e, 0xb5, 0xc5, 0x20,
Expand Down Expand Up @@ -333,18 +333,18 @@ mod tests {

macro_rules! test_tls_aead {
($name:ident, $alg:expr, $proto:expr, $key:expr) => {
paste! {
concat_idents!( test_name = test_, $name, _tls_aead_unsupported {
#[test]
fn [<test_ $name _tls_aead_unsupported>]() {
fn test_name() {
assert!(TlsRecordSealingKey::new($alg, $proto, $key).is_err());
assert!(TlsRecordOpeningKey::new($alg, $proto, $key).is_err());
}
}
});
};
($name:ident, $alg:expr, $proto:expr, $key:expr, $expect_tag_len:expr, $expect_nonce_len:expr) => {
paste! {
concat_idents!( test_name = test_, $name {
#[test]
fn [<test_ $name>]() {
fn test_name() {
let mut sealing_key =
TlsRecordSealingKey::new($alg, $proto, $key).unwrap();

Expand Down Expand Up @@ -403,7 +403,7 @@ mod tests {
assert_eq!(plaintext, offset_cipher_text[..plaintext.len()]);
}
}
}
});
};
}

Expand Down
8 changes: 4 additions & 4 deletions aws-lc-rs/src/cipher/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ mod tests {
use crate::iv::{FixedLength, IV_LEN_128_BIT};
use crate::rand::{SecureRandom, SystemRandom};
use crate::test::from_hex;
use paste::*;
use concat_idents::concat_idents;

fn step_encrypt(
mut encrypting_key: StreamingEncryptingKey,
Expand Down Expand Up @@ -608,8 +608,8 @@ mod tests {

macro_rules! helper_stream_step_encrypt_test {
($mode:ident) => {
paste! {
fn [<helper_test_ $mode _stream_encrypt_step_n_bytes>](
concat_idents!(test_name = helper_test_, $mode, _stream_encrypt_step_n_bytes {
fn test_name(
encrypting_key_creator: impl Fn() -> StreamingEncryptingKey,
decrypting_key_creator: impl Fn(DecryptionContext) -> StreamingDecryptingKey,
n: usize,
Expand All @@ -629,7 +629,7 @@ mod tests {

assert_eq!(input.as_slice(), &*plaintext);
}
}
});
};
}

Expand Down
30 changes: 18 additions & 12 deletions aws-lc-rs/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@
//! Serialization formats
use crate::buffer::Buffer;
use paste::paste;

macro_rules! generated_encodings {
($($name:ident),*) => { paste! {
($($name:ident),*) => {

use core::fmt::{Debug, Error, Formatter};
use core::ops::Deref;
mod buffer_type {
$(
pub struct [<$name Type>] {
_priv: (),
}
)*
$(
concat_idents::concat_idents!( name_type = $name, Type {
pub struct name_type {
_priv: (),
}
});
)*
}

$(
concat_idents::concat_idents!( name_type = $name, Type {
/// Serialized bytes
pub struct $name<'a>(Buffer<'a, buffer_type::[<$name Type>]>);
pub struct $name<'a>(Buffer<'a, buffer_type::name_type>);

impl<'a> Deref for $name<'a> {
type Target = Buffer<'a, buffer_type::[<$name Type>]>;
type Target = Buffer<'a, buffer_type::name_type>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -46,13 +50,15 @@ macro_rules! generated_encodings {
}
}

impl<'a> From<Buffer<'a, buffer_type::[<$name Type>]>> for $name<'a> {
fn from(value: Buffer<'a, buffer_type::[<$name Type>]>) -> Self {
impl<'a> From<Buffer<'a, buffer_type::name_type>> for $name<'a> {
fn from(value: Buffer<'a, buffer_type::name_type>) -> Self {
Self(value)
}
}

});
)*
}}
}
}
pub(crate) use generated_encodings;
generated_encodings!(
Expand Down
2 changes: 0 additions & 2 deletions aws-lc-rs/src/kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ where
}
}

use paste::paste;

generated_encodings!(EncapsulationKeyBytes);

/// A serializable encapsulation key usable with KEM algorithms. Constructed
Expand Down
2 changes: 1 addition & 1 deletion aws-lc-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bindgen = { version = "0.69.5", optional = true }
bindgen = { version = "0.69.5" }

[dependencies]
paste = "1.0.11"
concat-idents = "1.1.5"

[package.metadata.aws-lc-sys]
commit-hash = "d0356099f6b668697cdb381dfb09f9a694a6c9c2"
24 changes: 12 additions & 12 deletions aws-lc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
#![cfg_attr(not(clippy), allow(unexpected_cfgs))]
#![cfg_attr(not(clippy), allow(unknown_lints))]

use paste::paste;
use concat_idents::concat_idents;
use std::os::raw::{c_char, c_long, c_void};

#[allow(unused_macros)]
macro_rules! use_bindings {
($bindings:ident) => {
mod $bindings;
pub use $bindings::*;
};
}

macro_rules! platform_binding {
($platform:ident) => {
paste! {
concat_idents!(bindings_name = $platform, _, crypto {
#[cfg(all($platform, not(feature = "ssl"), not(use_bindgen_generated)))]
mod bindings_name;
#[cfg(all($platform, not(feature = "ssl"), not(use_bindgen_generated)))]
use_bindings!([< $platform _crypto >]);
}
pub use bindings_name::*;
});
concat_idents!(bindings_name = $platform, _, crypto, _, ssl {
#[cfg(all($platform, feature = "ssl", not(use_bindgen_generated)))]
mod bindings_name;
#[cfg(all($platform, feature = "ssl", not(use_bindgen_generated)))]
pub use bindings_name::*;
});
};
}

Expand Down

0 comments on commit 690f25f

Please sign in to comment.