#![cfg_attr(
feature = "sha2",
doc = r#"
# Quick Start
```rust
use rscrypto::{Digest, Sha256};
let digest = Sha256::digest(b"hello world");
let mut h = Sha256::new();
h.update(b"hello ");
h.update(b"world");
assert_eq!(h.finalize(), digest);
```
"#
)]
#![cfg_attr(
feature = "chacha20poly1305",
doc = r#"
# AEAD
```rust
# #[cfg(feature = "getrandom")]
# {
use rscrypto::{Aead, ChaCha20Poly1305, ChaCha20Poly1305Key};
let key = ChaCha20Poly1305Key::from_bytes([0x11; 32]);
let cipher = ChaCha20Poly1305::new(&key);
let mut sealed = [0u8; 4 + ChaCha20Poly1305::TAG_SIZE];
let nonce = cipher.seal_random(b"aad", b"data", &mut sealed)?;
let mut opened = [0u8; 4];
cipher.decrypt(&nonce, b"aad", &sealed, &mut opened)?;
assert_eq!(&opened, b"data");
# }
# Ok::<(), Box<dyn std::error::Error>>(())
```
"#
)]
#![cfg_attr(
all(feature = "password-hashing", feature = "getrandom"),
doc = r#"
# Password Hashing
```rust
use rscrypto::Argon2idPassword;
let passwords = Argon2idPassword::default();
let encoded = passwords.hash_password(b"correct horse battery staple")?;
assert!(
passwords
.verify_password(b"correct horse battery staple", &encoded)
.is_ok()
);
# Ok::<(), Box<dyn std::error::Error>>(())
```
"#
)]
#![cfg_attr(not(test), deny(clippy::unwrap_used))]
#![cfg_attr(not(test), deny(clippy::expect_used))]
#![cfg_attr(not(test), deny(clippy::indexing_slicing))]
#![cfg_attr(
all(
target_arch = "powerpc64",
any(
feature = "crc16",
feature = "crc24",
feature = "crc32",
feature = "crc64",
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "aegis256",
feature = "blake3",
feature = "xxh3",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "argon2"
)
),
feature(portable_simd, powerpc_target_feature)
)]
#![cfg_attr(target_arch = "s390x", feature(asm_experimental_reg))]
#![cfg_attr(
all(
target_arch = "s390x",
any(
feature = "crc16",
feature = "crc24",
feature = "crc32",
feature = "crc64",
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "aegis256",
feature = "blake3",
feature = "xxh3",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "ml-kem",
feature = "argon2"
)
),
feature(portable_simd)
)]
#![cfg_attr(
all(
target_arch = "riscv64",
any(
feature = "crc16",
feature = "crc24",
feature = "crc32",
feature = "crc64",
feature = "blake2b",
feature = "blake2s",
feature = "blake3",
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "aegis256",
feature = "argon2"
)
),
feature(riscv_target_feature)
)]
#![cfg_attr(
all(
target_arch = "riscv64",
any(
feature = "crc16",
feature = "crc24",
feature = "crc32",
feature = "crc64",
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "aegis256"
)
),
feature(asm_experimental_reg)
)]
#![cfg_attr(
all(
target_arch = "riscv64",
any(feature = "sha2", feature = "aes-gcm", feature = "aes-gcm-siv", feature = "aegis256")
),
feature(riscv_ext_intrinsics)
)]
#![cfg_attr(
all(
target_arch = "riscv64",
any(feature = "blake3", feature = "chacha20poly1305", feature = "xchacha20poly1305")
),
feature(portable_simd)
)]
#![cfg_attr(
all(
target_arch = "riscv32",
any(feature = "sha2", feature = "aes-gcm", feature = "aes-gcm-siv", feature = "aegis256")
),
feature(riscv_ext_intrinsics)
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(all(test, not(feature = "alloc")))]
extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;
#[macro_use]
mod macros;
#[cfg(any(
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "aegis256",
feature = "ascon-aead",
feature = "ecdsa-p256",
feature = "ecdsa-p384",
feature = "ed25519",
feature = "x25519",
feature = "ml-kem",
feature = "blake3"
))]
#[macro_use]
mod hex;
#[cfg(any(
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "aegis256",
feature = "ascon-aead"
))]
pub mod aead;
#[cfg(any(
feature = "hmac",
feature = "hmac-sha3",
feature = "hkdf",
feature = "kmac",
feature = "poly1305",
feature = "ecdsa-p256",
feature = "ecdsa-p384",
feature = "ed25519",
feature = "ml-kem",
feature = "rsa",
feature = "x25519",
feature = "phc-strings",
feature = "argon2",
feature = "scrypt"
))]
pub mod auth;
#[doc(hidden)]
mod backend;
pub mod platform;
pub mod traits;
#[cfg(any(feature = "crc16", feature = "crc24", feature = "crc32", feature = "crc64"))]
pub mod checksum;
#[cfg(any(feature = "sha3", feature = "blake3", feature = "ascon-hash"))]
macro_rules! impl_xof_read {
($type:ty) => {
#[cfg(feature = "std")]
impl std::io::Read for $type {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.squeeze(buf);
Ok(buf.len())
}
}
};
}
mod secret;
#[cfg(any(
feature = "sha2",
feature = "sha3",
feature = "blake2b",
feature = "blake2s",
feature = "blake3",
feature = "ascon-hash",
feature = "xxh3",
feature = "rapidhash"
))]
pub mod hashes;
#[cfg_attr(
not(any(feature = "kmac", feature = "ascon-hash", feature = "sha3")),
allow(dead_code)
)]
#[inline]
#[track_caller]
pub(crate) fn bytes_to_bits(len: usize) -> u64 {
let Ok(bytes) = u64::try_from(len) else {
panic!("byte length exceeds u64");
};
let Some(bits) = bytes.checked_mul(8) else {
panic!("byte length bit count exceeds u64");
};
bits
}
#[cfg(feature = "aead")]
pub use aead::{AeadBufferError, OpenError};
#[cfg(feature = "aegis256")]
pub use aead::{Aegis256, Aegis256Key, Aegis256Tag};
#[cfg(feature = "aes-gcm")]
pub use aead::{Aes128Gcm, Aes128GcmKey, Aes128GcmTag};
#[cfg(feature = "aes-gcm-siv")]
pub use aead::{Aes128GcmSiv, Aes128GcmSivKey, Aes128GcmSivTag};
#[cfg(feature = "aes-gcm")]
pub use aead::{Aes256Gcm, Aes256GcmKey, Aes256GcmTag};
#[cfg(feature = "aes-gcm-siv")]
pub use aead::{Aes256GcmSiv, Aes256GcmSivKey, Aes256GcmSivTag};
#[cfg(feature = "ascon-aead")]
pub use aead::{AsconAead128, AsconAead128Key, AsconAead128Tag};
#[cfg(feature = "chacha20poly1305")]
pub use aead::{ChaCha20Poly1305, ChaCha20Poly1305Key, ChaCha20Poly1305Tag};
#[cfg(feature = "xchacha20poly1305")]
pub use aead::{XChaCha20Poly1305, XChaCha20Poly1305Key, XChaCha20Poly1305Tag};
#[cfg(feature = "hkdf")]
pub use auth::HkdfOutputLengthError;
#[cfg(all(feature = "phc-strings", any(feature = "argon2", feature = "scrypt")))]
pub use auth::PasswordStatus;
#[cfg(feature = "argon2")]
pub use auth::{Argon2Context, Argon2Error, Argon2Params, Argon2d, Argon2i, Argon2id};
#[cfg(all(feature = "argon2", feature = "phc-strings"))]
pub use auth::{Argon2VerificationLimits, Argon2idPassword};
#[cfg(any(feature = "ecdsa-p256", feature = "ecdsa-p384"))]
pub use auth::{EcdsaError, EcdsaKeyGenerationError};
#[cfg(feature = "ecdsa-p256")]
pub use auth::{EcdsaP256Keypair, EcdsaP256PublicKey, EcdsaP256SecretKey, EcdsaP256Signature};
#[cfg(feature = "ecdsa-p384")]
pub use auth::{EcdsaP384Keypair, EcdsaP384PublicKey, EcdsaP384SecretKey, EcdsaP384Signature};
#[cfg(feature = "ed25519")]
pub use auth::{Ed25519Keypair, Ed25519PublicKey, Ed25519SecretKey, Ed25519Signature};
#[cfg(feature = "hkdf")]
pub use auth::{HkdfSha256, HkdfSha384, HkdfSha512};
#[cfg(feature = "hmac-sha3")]
pub use auth::{
HmacSha3_224, HmacSha3_224Tag, HmacSha3_256, HmacSha3_256Tag, HmacSha3_384, HmacSha3_384Tag, HmacSha3_512,
HmacSha3_512Tag,
};
#[cfg(feature = "hmac")]
pub use auth::{HmacSha256, HmacSha256Tag, HmacSha384, HmacSha384Tag, HmacSha512, HmacSha512Tag};
#[cfg(feature = "kmac")]
pub use auth::{Kmac128, Kmac256};
#[cfg(feature = "ml-kem")]
pub use auth::{
MlKem512, MlKem512Ciphertext, MlKem512DecapsulationKey, MlKem512EncapsulationKey, MlKem512PreparedDecapsulationKey,
MlKem512PreparedEncapsulationKey, MlKem512SharedSecret, MlKem768, MlKem768Ciphertext, MlKem768DecapsulationKey,
MlKem768EncapsulationKey, MlKem768PreparedDecapsulationKey, MlKem768PreparedEncapsulationKey, MlKem768SharedSecret,
MlKem1024, MlKem1024Ciphertext, MlKem1024DecapsulationKey, MlKem1024EncapsulationKey,
MlKem1024PreparedDecapsulationKey, MlKem1024PreparedEncapsulationKey, MlKem1024SharedSecret, MlKemError,
};
#[cfg(feature = "pbkdf2")]
pub use auth::{Pbkdf2Error, Pbkdf2Params, Pbkdf2Sha256, Pbkdf2Sha512, Pbkdf2VerifyPolicy};
#[cfg(feature = "poly1305")]
pub use auth::{Poly1305, Poly1305OneTimeKey, Poly1305Tag};
#[cfg(feature = "rsa")]
pub use auth::{
RsaEncryptionError, RsaJwtAlgorithm, RsaJwtVerifier, RsaKeyError, RsaKeyGenerationContract, RsaKeyGenerationError,
RsaOaepProfile, RsaPkcs1v15Profile, RsaPrivateKey, RsaPrivateKeyParts, RsaPrivateOpError, RsaPrivateScratch,
RsaProtocolAlgorithmError, RsaPssProfile, RsaPublicExponent, RsaPublicExponentPolicy, RsaPublicKey,
RsaPublicKeyPolicy, RsaPublicOpError, RsaPublicScratch, RsaSignatureProfile, RsaSignatureSigner,
RsaSignatureVerifier, RsaTlsSignatureSchemes, RsaX509PublicKey, RsaX509PublicKeyAlgorithm,
};
#[cfg(feature = "scrypt")]
pub use auth::{Scrypt, ScryptError, ScryptParams};
#[cfg(all(feature = "scrypt", feature = "phc-strings"))]
pub use auth::{ScryptPassword, ScryptVerificationLimits};
#[cfg(feature = "x25519")]
pub use auth::{X25519Error, X25519PublicKey, X25519SecretKey, X25519SharedSecret};
#[cfg(feature = "crc24")]
pub use checksum::Crc24OpenPgp;
#[cfg(feature = "crc16")]
pub use checksum::{Crc16Ccitt, Crc16Ibm};
#[cfg(feature = "crc32")]
pub use checksum::{Crc32, Crc32C};
#[cfg(feature = "crc64")]
pub use checksum::{Crc64, Crc64Nvme};
#[cfg(any(feature = "blake2b", feature = "blake2s"))]
pub use hashes::crypto::Blake2Error;
#[cfg(feature = "ascon-hash")]
pub use hashes::crypto::ascon::AsconCxofCustomizationError;
#[cfg(feature = "ascon-hash")]
pub use hashes::crypto::{AsconCxof128, AsconCxof128Reader, AsconHash256, AsconXof, AsconXofReader};
#[cfg(feature = "blake2b")]
pub use hashes::crypto::{Blake2b, Blake2b256, Blake2b512, Blake2bKey, Blake2bParams};
#[cfg(feature = "blake2s")]
pub use hashes::crypto::{Blake2s128, Blake2s256, Blake2sKey, Blake2sParams};
#[cfg(feature = "blake3")]
pub use hashes::crypto::{Blake3, Blake3KeyedHash, Blake3XofReader};
#[cfg(feature = "sha3")]
pub use hashes::crypto::{
Cshake128, Cshake128XofReader, Cshake256, Cshake256XofReader, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Shake128,
Shake128XofReader, Shake256, Shake256XofReader,
};
#[cfg(feature = "sha2")]
pub use hashes::crypto::{Sha224, Sha256, Sha384, Sha512, Sha512_256};
#[cfg(feature = "rapidhash")]
pub use hashes::fast::{RapidHash64, RapidHasher, RapidRandomState, RapidSeededState, RapidStreamHasher};
#[cfg(feature = "xxh3")]
pub use hashes::fast::{Xxh3, Xxh3_128};
#[cfg(feature = "xxh3")]
pub use hashes::fast::{Xxh3_128Hasher, Xxh3BuildHasher, Xxh3Hasher};
#[cfg(any(
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "aegis256",
feature = "ascon-aead",
feature = "ecdsa-p256",
feature = "ecdsa-p384",
feature = "ed25519",
feature = "ml-kem",
feature = "x25519"
))]
pub use hex::InvalidHexError;
pub use secret::SecretBytes;
#[cfg(feature = "alloc")]
pub use secret::SecretVec;
#[cfg(any(
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "aegis256",
feature = "ascon-aead"
))]
pub use traits::Aead;
pub use traits::{Checksum, ChecksumCombine, Kem, Mac, TrySigner, TrySignerInto, VerificationError, Verifier, ct};
#[cfg(any(
feature = "sha2",
feature = "sha3",
feature = "blake2b",
feature = "blake2s",
feature = "blake3",
feature = "ascon-hash",
feature = "xxh3",
feature = "rapidhash"
))]
pub use traits::{Digest, FastHash, Xof};
pub mod expert {
#[cfg(any(
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "aegis256",
feature = "ascon-aead",
feature = "ecdsa-p256",
feature = "ecdsa-p384",
feature = "ed25519",
feature = "ml-kem",
feature = "x25519"
))]
pub use crate::hex::DisplaySecret;
}
pub mod prelude {
#[cfg(any(
feature = "aes-gcm",
feature = "aes-gcm-siv",
feature = "chacha20poly1305",
feature = "xchacha20poly1305",
feature = "aegis256",
feature = "ascon-aead"
))]
pub use crate::traits::Aead;
pub use crate::traits::{
Checksum, ChecksumCombine, Digest, FastHash, Kem, Mac, TrySigner, TrySignerInto, VerificationError, Verifier, Xof,
};
}
#[cfg(all(doctest, feature = "full", feature = "getrandom"))]
#[doc = include_str!("../README.md")]
pub struct ReadmeDoctests;
#[cfg(all(doctest, feature = "full", feature = "diag"))]
#[doc(hidden)]
#[doc = r#"
```compile_fail
use rscrypto::Crc32Config;
```
```compile_fail
use rscrypto::DispatchInfo;
```
```compile_fail
use rscrypto::kernel_for;
```
```compile_fail
use rscrypto::backend_for;
```
```compile_fail
use rscrypto::backend;
```
```compile_fail
use rscrypto::Crc32Ieee;
```
```compile_fail
use rscrypto::Crc32Castagnoli;
```
```compile_fail
use rscrypto::Crc64Xz;
```
```compile_fail
use rscrypto::AsconXof128;
```
```compile_fail
use rscrypto::AsconXof128Reader;
```
```compile_fail
use rscrypto::BufferedCrc32C;
```
```compile_fail
use rscrypto::Xxh3_64;
```
```compile_fail
use rscrypto::RapidHash;
```
```compile_fail
use rscrypto::checksum::BufferedCrc32C;
```
```compile_fail
use rscrypto::platform_describe;
```
```compile_fail
use rscrypto::DigestReader;
```
```compile_fail
use rscrypto::diag_hmac_sha256_verify_portable;
```
```rust
use rscrypto::checksum::config::Crc32Config;
use rscrypto::checksum::buffered::BufferedCrc32C;
use rscrypto::checksum::introspect::{DispatchInfo, kernel_for};
use rscrypto::checksum::{Crc32Castagnoli, Crc32Ieee, Crc64Xz};
use rscrypto::hashes::fast::{RapidHash64, Xxh3_64};
use rscrypto::hashes::introspect::{KernelIntrospect, kernel_for as hash_kernel_for};
use rscrypto::hashes::DigestReader;
use rscrypto::{AsconXof, AsconXofReader, Xxh3};
fn assert_hash_introspect<T: KernelIntrospect>() {}
let _ = rscrypto::platform::describe();
let _: Crc32Config = rscrypto::Crc32::config();
let _ = kernel_for::<rscrypto::Crc32>(64);
let _ = DispatchInfo::current();
let _ = hash_kernel_for::<rscrypto::Sha256>(1024);
assert_hash_introspect::<rscrypto::Sha256>();
let _ = (core::any::TypeId::of::<Crc32Ieee>(), core::any::TypeId::of::<Crc32Castagnoli>(), core::any::TypeId::of::<Crc64Xz>());
let _ = (core::any::TypeId::of::<AsconXof>(), core::any::TypeId::of::<AsconXofReader>());
let _ = core::any::TypeId::of::<BufferedCrc32C>();
let _ = (core::any::TypeId::of::<Xxh3>(), core::any::TypeId::of::<Xxh3_64>());
let _ = core::any::TypeId::of::<RapidHash64>();
```
"#]
pub struct __RootSurfaceAudit;
#[cfg(all(doctest, feature = "full", feature = "getrandom"))]
#[doc(hidden)]
#[doc = r#"
```compile_fail
use rscrypto::DisplaySecret;
```
```compile_fail
use rscrypto::platform::OverrideError;
```
```compile_fail
use rscrypto::platform::try_set_override;
```
```compile_fail
use rscrypto::aead::{ChaCha20Poly1305Key, Nonce96};
let _ = ChaCha20Poly1305Key::random();
let _ = Nonce96::random();
```
```compile_fail
use rscrypto::{Aead, ChaCha20Poly1305, ChaCha20Poly1305Key, aead::Nonce96};
let cipher = ChaCha20Poly1305::new(&ChaCha20Poly1305Key::from_bytes([0u8; 32]));
let nonce = Nonce96::from_bytes([0u8; 12]);
let mut out = [0u8; 16];
cipher.encrypt(&nonce, b"", b"", &mut out)?;
```
```compile_fail
let _ = rscrypto::aead::__SealToken(());
```
```rust
use rscrypto::{
Aead, ChaCha20Poly1305, ChaCha20Poly1305Key,
aead::{Nonce96, expert::AeadWithNonce},
};
let cipher = ChaCha20Poly1305::new(&ChaCha20Poly1305Key::from_bytes([0u8; 32]));
let nonce = Nonce96::from_bytes([0u8; 12]);
let mut out = [0u8; 16];
cipher.encrypt(&nonce, b"", b"", &mut out)?;
let display_key = ChaCha20Poly1305Key::from_bytes([0u8; 32]);
let _: rscrypto::expert::DisplaySecret<'_> = display_key.display_secret();
let _: Option<rscrypto::platform::expert::OverrideError> = None;
# Ok::<(), rscrypto::aead::SealError>(())
```
"#]
pub struct __MisuseResistantSurfaceAudit;
#[cfg(all(doctest, feature = "full"))]
#[doc(hidden)]
#[doc = r#"
```rust
use rscrypto::{
Blake3, Digest, Sha224, Sha256, Sha384, Sha512, Sha512_256, Sha3_224, Sha3_256, Sha3_384, Sha3_512,
};
fn assert_digest_api<D>()
where
D: Digest,
D::Output: PartialEq + core::fmt::Debug,
{
let mut h = D::new();
h.update(b"abc");
let expected = h.finalize();
h.reset();
h.update(b"abc");
assert_eq!(h.finalize(), expected);
}
assert_digest_api::<Sha224>();
assert_digest_api::<Sha256>();
assert_digest_api::<Sha384>();
assert_digest_api::<Sha512>();
assert_digest_api::<Sha512_256>();
assert_digest_api::<Sha3_224>();
assert_digest_api::<Sha3_256>();
assert_digest_api::<Sha3_384>();
assert_digest_api::<Sha3_512>();
assert_digest_api::<Blake3>();
```
```rust
use rscrypto::{AsconXof, Blake3, Digest, Shake128, Shake256, Xof};
fn squeeze_32(mut reader: impl Xof) -> [u8; 32] {
let mut out = [0u8; 32];
reader.squeeze(&mut out);
out
}
macro_rules! assert_xof_api {
($ty:ty) => {{
let data = b"abc";
let mut h = <$ty>::new();
h.update(data);
let streaming = squeeze_32(h.clone().finalize_xof());
h.reset();
let oneshot = squeeze_32(<$ty>::xof(data));
assert_eq!(streaming, oneshot);
}};
}
assert_xof_api!(Shake128);
assert_xof_api!(Shake256);
assert_xof_api!(Blake3);
assert_xof_api!(AsconXof);
```
```rust
use std::io::{Cursor, Read, Write};
use rscrypto::{Checksum as _, Crc32C};
let mut reader = Crc32C::reader(Cursor::new(b"abc".to_vec()));
std::io::copy(&mut reader, &mut std::io::sink())?;
assert_eq!(reader.checksum(), Crc32C::checksum(b"abc"));
let mut writer = Crc32C::writer(Vec::new());
writer.write_all(b"abc")?;
assert_eq!(writer.checksum(), Crc32C::checksum(b"abc"));
# Ok::<(), std::io::Error>(())
```
```compile_fail
use std::io::Cursor;
use rscrypto::{Checksum as _, Crc32C};
let reader = Crc32C::reader(Cursor::new(b"abc".to_vec()));
let _ = reader.crc();
```
```compile_fail
use rscrypto::{Checksum as _, Crc32C};
let writer = Crc32C::writer(Vec::<u8>::new());
let _ = writer.crc();
```
"#]
pub struct __ApiPatternAudit;
#[cfg(all(doctest, feature = "full"))]
#[doc(hidden)]
#[doc = r#"
```compile_fail
use rscrypto::ConstantTimeEq;
```
```compile_fail
let left = [0u8; 32];
let right = [0u8; 32];
let _ = left.ct_eq(&right);
```
```compile_fail
let left = [0u8; 32];
let right = [0u8; 32];
let _ = rscrypto::ct::constant_time_eq(&left, &right);
```
```compile_fail
use rscrypto::SecretBytes;
let left = SecretBytes::new([0u8; 32]);
let right = SecretBytes::new([0u8; 32]);
let _ = left == right;
```
```compile_fail
use rscrypto::SecretVec;
fn compare(left: &SecretVec, right: &SecretVec) -> bool {
left == right
}
```
```compile_fail
use rscrypto::HmacSha256Tag;
let tag = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let _ = tag == [0u8; HmacSha256Tag::LENGTH];
```
```compile_fail
use rscrypto::HmacSha256Tag;
let left = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let right = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let _ = left == right;
```
```compile_fail
use rscrypto::HmacSha256Tag;
let left = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let right = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let _: bool = left.ct_eq(&right);
```
```compile_fail
use rscrypto::HmacSha256Tag;
let left = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let right = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let _: bool = left.ct_eq(&right).into();
```
```compile_fail
use rscrypto::HmacSha256Tag;
let left = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let right = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
if left.ct_eq(&right) {}
```
```compile_fail
let _ = rscrypto::ct::CtDecision { mask: u8::MAX };
```
```compile_fail
use rscrypto::HmacSha256Tag;
let left = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let right = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let decision = left.ct_eq(&right);
let _ = decision.declassify();
let _ = decision.declassify();
```
```compile_fail
use rscrypto::HmacSha256Tag;
let first = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let second = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let third = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let _ = first.ct_eq(&second) == second.ct_eq(&third);
```
```compile_fail
use rscrypto::HmacSha256Tag;
let left = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let right = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let _ = format!("{:?}", left.ct_eq(&right));
```
```rust
use rscrypto::HmacSha256Tag;
let left = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
let right = HmacSha256Tag::from_bytes([0u8; HmacSha256Tag::LENGTH]);
assert!(left.ct_eq(&right).declassify());
```
"#]
pub struct __OwnerEqualityBoundaryAudit;
#[cfg(all(doctest, feature = "full"))]
#[doc(hidden)]
#[doc = r#"
The secret-bearing owners below must not acquire a generic `Clone` capability.
```compile_fail,E0277
use rscrypto::HmacSha256;
fn require_clone<T: Clone>() {}
require_clone::<HmacSha256>();
```
```compile_fail,E0277
use rscrypto::HmacSha384;
fn require_clone<T: Clone>() {}
require_clone::<HmacSha384>();
```
```compile_fail,E0277
use rscrypto::HmacSha512;
fn require_clone<T: Clone>() {}
require_clone::<HmacSha512>();
```
```compile_fail,E0277
use rscrypto::HmacSha3_224;
fn require_clone<T: Clone>() {}
require_clone::<HmacSha3_224>();
```
```compile_fail,E0277
use rscrypto::HmacSha3_256;
fn require_clone<T: Clone>() {}
require_clone::<HmacSha3_256>();
```
```compile_fail,E0277
use rscrypto::HmacSha3_384;
fn require_clone<T: Clone>() {}
require_clone::<HmacSha3_384>();
```
```compile_fail,E0277
use rscrypto::HmacSha3_512;
fn require_clone<T: Clone>() {}
require_clone::<HmacSha3_512>();
```
```compile_fail,E0277
use rscrypto::HkdfSha256;
fn require_clone<T: Clone>() {}
require_clone::<HkdfSha256>();
```
```compile_fail,E0277
use rscrypto::HkdfSha384;
fn require_clone<T: Clone>() {}
require_clone::<HkdfSha384>();
```
```compile_fail,E0277
use rscrypto::HkdfSha512;
fn require_clone<T: Clone>() {}
require_clone::<HkdfSha512>();
```
```compile_fail,E0277
use rscrypto::Kmac128;
fn require_clone<T: Clone>() {}
require_clone::<Kmac128>();
```
```compile_fail,E0277
use rscrypto::Kmac256;
fn require_clone<T: Clone>() {}
require_clone::<Kmac256>();
```
```compile_fail,E0277
use rscrypto::Pbkdf2Sha256;
fn require_clone<T: Clone>() {}
require_clone::<Pbkdf2Sha256>();
```
```compile_fail,E0277
use rscrypto::Pbkdf2Sha512;
fn require_clone<T: Clone>() {}
require_clone::<Pbkdf2Sha512>();
```
```compile_fail,E0277
use rscrypto::Blake2bParams;
fn require_clone<T: Clone>() {}
require_clone::<Blake2bParams>();
```
```compile_fail,E0277
use rscrypto::Blake2sParams;
fn require_clone<T: Clone>() {}
require_clone::<Blake2sParams>();
```
```compile_fail,E0277
use rscrypto::Blake2b;
fn require_clone<T: Clone>() {}
require_clone::<Blake2b>();
```
```compile_fail,E0277
use rscrypto::Poly1305OneTimeKey;
fn require_clone<T: Clone>() {}
require_clone::<Poly1305OneTimeKey>();
```
```compile_fail,E0277
use rscrypto::Poly1305;
fn require_clone<T: Clone>() {}
require_clone::<Poly1305>();
```
```compile_fail,E0277
use rscrypto::EcdsaP256SecretKey;
fn require_clone<T: Clone>() {}
require_clone::<EcdsaP256SecretKey>();
```
```compile_fail,E0277
use rscrypto::EcdsaP256Keypair;
fn require_clone<T: Clone>() {}
require_clone::<EcdsaP256Keypair>();
```
```compile_fail,E0277
use rscrypto::EcdsaP384SecretKey;
fn require_clone<T: Clone>() {}
require_clone::<EcdsaP384SecretKey>();
```
```compile_fail,E0277
use rscrypto::EcdsaP384Keypair;
fn require_clone<T: Clone>() {}
require_clone::<EcdsaP384Keypair>();
```
```compile_fail,E0277
use rscrypto::Ed25519SecretKey;
fn require_clone<T: Clone>() {}
require_clone::<Ed25519SecretKey>();
```
```compile_fail,E0277
use rscrypto::Ed25519Keypair;
fn require_clone<T: Clone>() {}
require_clone::<Ed25519Keypair>();
```
```compile_fail,E0277
use rscrypto::X25519SecretKey;
fn require_clone<T: Clone>() {}
require_clone::<X25519SecretKey>();
```
```compile_fail,E0277
use rscrypto::MlKem512DecapsulationKey;
fn require_clone<T: Clone>() {}
require_clone::<MlKem512DecapsulationKey>();
```
```compile_fail,E0277
use rscrypto::MlKem768DecapsulationKey;
fn require_clone<T: Clone>() {}
require_clone::<MlKem768DecapsulationKey>();
```
```compile_fail,E0277
use rscrypto::MlKem1024DecapsulationKey;
fn require_clone<T: Clone>() {}
require_clone::<MlKem1024DecapsulationKey>();
```
```compile_fail,E0277
use rscrypto::MlKem512PreparedDecapsulationKey;
fn require_clone<T: Clone>() {}
require_clone::<MlKem512PreparedDecapsulationKey>();
```
```compile_fail,E0277
use rscrypto::MlKem768PreparedDecapsulationKey;
fn require_clone<T: Clone>() {}
require_clone::<MlKem768PreparedDecapsulationKey>();
```
```compile_fail,E0277
use rscrypto::MlKem1024PreparedDecapsulationKey;
fn require_clone<T: Clone>() {}
require_clone::<MlKem1024PreparedDecapsulationKey>();
```
```compile_fail,E0277
use rscrypto::RsaPrivateKey;
fn require_clone<T: Clone>() {}
require_clone::<RsaPrivateKey>();
```
```compile_fail,E0277
use rscrypto::RsaPrivateScratch;
fn require_clone<T: Clone>() {}
require_clone::<RsaPrivateScratch>();
```
"#]
pub struct __SecretCloneBoundaryAudit;
#[cfg(all(test, miri))]
mod miri_shadow_tests;
#[cfg(test)]
mod length_framing_tests {
#[test]
fn bytes_to_bits_accepts_max_encodable_len() {
assert_eq!(super::bytes_to_bits((u64::MAX / 8) as usize), u64::MAX - 7);
}
#[test]
#[cfg(target_pointer_width = "64")]
#[should_panic(expected = "byte length bit count exceeds u64")]
fn bytes_to_bits_rejects_bit_count_overflow() {
let _ = super::bytes_to_bits((u64::MAX / 8).strict_add(1) as usize);
}
}
#[cfg(all(test, feature = "std", feature = "sha2", feature = "crc32"))]
mod direct_io_write_tests {
use std::io::{IoSlice, Write};
use super::{Crc32C, Sha256};
use crate::traits::Checksum as _;
#[test]
fn digest_state_accepts_direct_io_write() {
let mut digest = Sha256::new();
digest.write_all(b"hello ").unwrap();
digest.write_all(b"world").unwrap();
assert_eq!(digest.finalize(), Sha256::digest(b"hello world"));
}
#[test]
fn checksum_state_accepts_direct_io_write() {
let mut checksum = Crc32C::new();
checksum.write_all(b"hello ").unwrap();
checksum.write_all(b"world").unwrap();
assert_eq!(checksum.finalize(), Crc32C::checksum(b"hello world"));
}
#[test]
fn digest_vectored_write_consumes_all_buffers() {
let mut digest = Sha256::new();
let bufs = [IoSlice::new(b"hello "), IoSlice::new(b"world")];
let written = digest.write_vectored(&bufs).unwrap();
assert_eq!(written, b"hello world".len());
assert_eq!(digest.finalize(), Sha256::digest(b"hello world"));
}
}
#[cfg(test)]
mod send_sync_assertions {
#![allow(unused_imports)]
use super::*;
fn assert_send_sync<T: Send + Sync>() {}
fn assert_clone<T: Clone>() {}
fn assert_debug<T: core::fmt::Debug>() {}
#[test]
fn public_types_are_send_and_sync() {
assert_send_sync::<traits::error::VerificationError>();
assert_send_sync::<platform::Caps>();
assert_send_sync::<platform::Arch>();
assert_send_sync::<platform::Detected>();
assert_send_sync::<platform::expert::OverrideError>();
assert_send_sync::<platform::Description>();
}
#[test]
#[cfg(feature = "checksums")]
fn checksum_types_are_send_and_sync() {
assert_send_sync::<Crc16Ccitt>();
assert_send_sync::<Crc16Ibm>();
assert_send_sync::<checksum::config::Crc16Force>();
assert_send_sync::<checksum::config::Crc16Config>();
assert_send_sync::<Crc24OpenPgp>();
assert_send_sync::<checksum::config::Crc24Force>();
assert_send_sync::<checksum::config::Crc24Config>();
assert_send_sync::<Crc32>();
assert_send_sync::<Crc32C>();
assert_send_sync::<checksum::config::Crc32Force>();
assert_send_sync::<checksum::config::Crc32Config>();
assert_send_sync::<Crc64>();
assert_send_sync::<Crc64Nvme>();
assert_send_sync::<checksum::config::Crc64Force>();
assert_send_sync::<checksum::config::Crc64Config>();
#[cfg(feature = "diag")]
{
assert_send_sync::<checksum::introspect::DispatchInfo>();
assert_send_sync::<checksum::diag::SelectionReason>();
assert_send_sync::<checksum::diag::Crc32Polynomial>();
assert_send_sync::<checksum::diag::Crc64Polynomial>();
assert_send_sync::<checksum::diag::Crc32SelectionDiag>();
assert_send_sync::<checksum::diag::Crc64SelectionDiag>();
}
}
#[test]
#[cfg(all(feature = "checksums", feature = "alloc"))]
fn buffered_checksum_types_are_send_and_sync() {
assert_send_sync::<checksum::buffered::BufferedCrc16Ccitt>();
assert_send_sync::<checksum::buffered::BufferedCrc16Ibm>();
assert_send_sync::<checksum::buffered::BufferedCrc24OpenPgp>();
assert_send_sync::<checksum::buffered::BufferedCrc32>();
assert_send_sync::<checksum::buffered::BufferedCrc32C>();
assert_send_sync::<checksum::buffered::BufferedCrc64>();
assert_send_sync::<checksum::buffered::BufferedCrc64Nvme>();
}
#[test]
#[cfg(feature = "hashes")]
fn hash_types_are_send_and_sync() {
assert_send_sync::<Sha256>();
assert_send_sync::<Sha224>();
assert_send_sync::<Sha512>();
assert_send_sync::<Sha384>();
assert_send_sync::<Sha512_256>();
assert_send_sync::<Sha3_256>();
assert_send_sync::<Sha3_224>();
assert_send_sync::<Sha3_512>();
assert_send_sync::<Sha3_384>();
assert_send_sync::<Shake128>();
assert_send_sync::<Shake256>();
assert_send_sync::<Shake128XofReader>();
assert_send_sync::<Shake256XofReader>();
assert_send_sync::<Cshake128>();
assert_send_sync::<Cshake256>();
assert_send_sync::<Cshake128XofReader>();
assert_send_sync::<Cshake256XofReader>();
assert_send_sync::<AsconHash256>();
assert_send_sync::<AsconXof>();
assert_send_sync::<AsconXofReader>();
assert_send_sync::<AsconCxof128>();
assert_send_sync::<AsconCxof128Reader>();
assert_send_sync::<Blake3>();
assert_send_sync::<Blake3XofReader>();
assert_send_sync::<Xxh3>();
assert_send_sync::<Xxh3_128>();
assert_send_sync::<RapidHash64>();
assert_send_sync::<Xxh3BuildHasher>();
assert_send_sync::<Xxh3Hasher>();
assert_send_sync::<Xxh3_128Hasher>();
assert_send_sync::<RapidSeededState>();
assert_send_sync::<RapidRandomState>();
assert_send_sync::<RapidHasher>();
assert_send_sync::<RapidStreamHasher>();
}
#[test]
#[cfg(all(feature = "checksums", feature = "std"))]
fn io_adapter_types_are_send_and_sync() {
assert_send_sync::<traits::io::ChecksumReader<std::io::Cursor<Vec<u8>>, Crc32C>>();
assert_send_sync::<traits::io::ChecksumWriter<Vec<u8>, Crc32C>>();
}
#[test]
#[cfg(all(feature = "hashes", feature = "std"))]
fn digest_io_adapter_types_are_send_and_sync() {
assert_send_sync::<hashes::DigestReader<std::io::Cursor<Vec<u8>>, Sha256>>();
assert_send_sync::<hashes::DigestWriter<Vec<u8>, Sha256>>();
}
#[test]
fn platform_types_are_clone_and_debug() {
assert_clone::<platform::Caps>();
assert_clone::<platform::Arch>();
assert_clone::<platform::Detected>();
assert_clone::<platform::expert::OverrideError>();
assert_clone::<platform::Description>();
assert_clone::<traits::error::VerificationError>();
assert_debug::<platform::Caps>();
assert_debug::<platform::Arch>();
assert_debug::<platform::Detected>();
assert_debug::<platform::expert::OverrideError>();
assert_debug::<platform::Description>();
assert_debug::<traits::error::VerificationError>();
}
#[test]
#[cfg(feature = "checksums")]
fn checksum_types_are_clone_and_debug() {
assert_clone::<Crc16Ccitt>();
assert_clone::<Crc16Ibm>();
assert_clone::<Crc24OpenPgp>();
assert_clone::<Crc32>();
assert_clone::<Crc32C>();
assert_clone::<Crc64>();
assert_clone::<Crc64Nvme>();
assert_clone::<checksum::config::Crc16Force>();
assert_clone::<checksum::config::Crc16Config>();
assert_clone::<checksum::config::Crc24Force>();
assert_clone::<checksum::config::Crc24Config>();
assert_clone::<checksum::config::Crc32Force>();
assert_clone::<checksum::config::Crc32Config>();
assert_clone::<checksum::config::Crc64Force>();
assert_clone::<checksum::config::Crc64Config>();
assert_debug::<Crc16Ccitt>();
assert_debug::<Crc16Ibm>();
assert_debug::<Crc24OpenPgp>();
assert_debug::<Crc32>();
assert_debug::<Crc32C>();
assert_debug::<Crc64>();
assert_debug::<Crc64Nvme>();
assert_debug::<checksum::config::Crc16Force>();
assert_debug::<checksum::config::Crc16Config>();
assert_debug::<checksum::config::Crc24Force>();
assert_debug::<checksum::config::Crc24Config>();
assert_debug::<checksum::config::Crc32Force>();
assert_debug::<checksum::config::Crc32Config>();
assert_debug::<checksum::config::Crc64Force>();
assert_debug::<checksum::config::Crc64Config>();
#[cfg(feature = "diag")]
{
assert_clone::<checksum::introspect::DispatchInfo>();
assert_debug::<checksum::introspect::DispatchInfo>();
}
}
#[test]
#[cfg(all(feature = "checksums", feature = "alloc"))]
fn buffered_checksum_types_are_clone_and_debug() {
assert_debug::<checksum::buffered::BufferedCrc16Ccitt>();
assert_debug::<checksum::buffered::BufferedCrc16Ibm>();
assert_debug::<checksum::buffered::BufferedCrc24OpenPgp>();
assert_debug::<checksum::buffered::BufferedCrc32>();
assert_debug::<checksum::buffered::BufferedCrc32C>();
assert_debug::<checksum::buffered::BufferedCrc64>();
assert_debug::<checksum::buffered::BufferedCrc64Nvme>();
}
#[test]
#[cfg(feature = "hashes")]
fn hash_types_are_clone_and_debug() {
assert_clone::<Sha256>();
assert_clone::<Sha224>();
assert_clone::<Sha512>();
assert_clone::<Sha384>();
assert_clone::<Sha512_256>();
assert_clone::<Sha3_256>();
assert_clone::<Sha3_224>();
assert_clone::<Sha3_512>();
assert_clone::<Sha3_384>();
assert_clone::<Shake128>();
assert_clone::<Shake256>();
assert_clone::<Shake128XofReader>();
assert_clone::<Shake256XofReader>();
assert_clone::<Cshake128>();
assert_clone::<Cshake256>();
assert_clone::<Cshake128XofReader>();
assert_clone::<Cshake256XofReader>();
assert_clone::<AsconHash256>();
assert_clone::<AsconXof>();
assert_clone::<AsconXofReader>();
assert_clone::<AsconCxof128>();
assert_clone::<AsconCxof128Reader>();
assert_clone::<Blake3>();
assert_clone::<Blake3XofReader>();
assert_clone::<Xxh3>();
assert_clone::<Xxh3_128>();
assert_clone::<RapidHash64>();
assert_debug::<Sha256>();
assert_debug::<Sha224>();
assert_debug::<Sha512>();
assert_debug::<Sha384>();
assert_debug::<Sha512_256>();
assert_debug::<Sha3_256>();
assert_debug::<Sha3_224>();
assert_debug::<Sha3_512>();
assert_debug::<Sha3_384>();
assert_debug::<Shake128>();
assert_debug::<Shake256>();
assert_debug::<Shake128XofReader>();
assert_debug::<Shake256XofReader>();
assert_debug::<Cshake128>();
assert_debug::<Cshake256>();
assert_debug::<Cshake128XofReader>();
assert_debug::<Cshake256XofReader>();
assert_debug::<AsconHash256>();
assert_debug::<AsconXof>();
assert_debug::<AsconXofReader>();
assert_debug::<AsconCxof128>();
assert_debug::<AsconCxof128Reader>();
assert_debug::<Blake3>();
assert_debug::<Blake3XofReader>();
assert_debug::<Xxh3>();
assert_debug::<Xxh3_128>();
assert_debug::<RapidHash64>();
assert_clone::<Xxh3BuildHasher>();
assert_clone::<RapidSeededState>();
assert_clone::<RapidRandomState>();
assert_clone::<RapidStreamHasher>();
assert_debug::<Xxh3BuildHasher>();
assert_debug::<Xxh3Hasher>();
assert_debug::<Xxh3_128Hasher>();
assert_debug::<RapidSeededState>();
assert_debug::<RapidRandomState>();
assert_debug::<RapidHasher>();
assert_debug::<RapidStreamHasher>();
}
#[test]
#[cfg(all(feature = "checksums", feature = "std"))]
fn io_adapter_types_are_debug() {
assert_debug::<traits::io::ChecksumReader<std::io::Cursor<Vec<u8>>, Crc32C>>();
assert_debug::<traits::io::ChecksumWriter<Vec<u8>, Crc32C>>();
}
#[test]
#[cfg(all(feature = "hashes", feature = "std"))]
fn digest_io_adapter_types_are_debug() {
assert_debug::<hashes::DigestReader<std::io::Cursor<Vec<u8>>, Sha256>>();
assert_debug::<hashes::DigestWriter<Vec<u8>, Sha256>>();
}
}