chore: checkpoint before Python removal

This commit is contained in:
2026-03-26 22:33:59 +00:00
parent 683cec9307
commit e568ddf82a
29972 changed files with 11269302 additions and 2 deletions

195
vendor/cbc/src/decrypt.rs vendored Normal file
View File

@@ -0,0 +1,195 @@
use crate::xor;
use cipher::{
crypto_common::{InnerUser, IvSizeUser},
generic_array::{ArrayLength, GenericArray},
inout::InOut,
AlgorithmName, Block, BlockBackend, BlockCipher, BlockClosure, BlockDecryptMut, BlockSizeUser,
InnerIvInit, Iv, IvState, ParBlocks, ParBlocksSizeUser,
};
use core::fmt;
#[cfg(feature = "zeroize")]
use cipher::zeroize::{Zeroize, ZeroizeOnDrop};
/// CBC mode decryptor.
#[derive(Clone)]
pub struct Decryptor<C>
where
C: BlockDecryptMut + BlockCipher,
{
cipher: C,
iv: Block<C>,
}
impl<C> BlockSizeUser for Decryptor<C>
where
C: BlockDecryptMut + BlockCipher,
{
type BlockSize = C::BlockSize;
}
impl<C> BlockDecryptMut for Decryptor<C>
where
C: BlockDecryptMut + BlockCipher,
{
fn decrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
let Self { cipher, iv } = self;
cipher.decrypt_with_backend_mut(Closure { iv, f })
}
}
impl<C> InnerUser for Decryptor<C>
where
C: BlockDecryptMut + BlockCipher,
{
type Inner = C;
}
impl<C> IvSizeUser for Decryptor<C>
where
C: BlockDecryptMut + BlockCipher,
{
type IvSize = C::BlockSize;
}
impl<C> InnerIvInit for Decryptor<C>
where
C: BlockDecryptMut + BlockCipher,
{
#[inline]
fn inner_iv_init(cipher: C, iv: &Iv<Self>) -> Self {
Self {
cipher,
iv: iv.clone(),
}
}
}
impl<C> IvState for Decryptor<C>
where
C: BlockDecryptMut + BlockCipher,
{
#[inline]
fn iv_state(&self) -> Iv<Self> {
self.iv.clone()
}
}
impl<C> AlgorithmName for Decryptor<C>
where
C: BlockDecryptMut + BlockCipher + AlgorithmName,
{
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("cbc::Decryptor<")?;
<C as AlgorithmName>::write_alg_name(f)?;
f.write_str(">")
}
}
impl<C> fmt::Debug for Decryptor<C>
where
C: BlockDecryptMut + BlockCipher + AlgorithmName,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("cbc::Decryptor<")?;
<C as AlgorithmName>::write_alg_name(f)?;
f.write_str("> { ... }")
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl<C: BlockDecryptMut + BlockCipher> Drop for Decryptor<C> {
fn drop(&mut self) {
self.iv.zeroize();
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl<C: BlockDecryptMut + BlockCipher + ZeroizeOnDrop> ZeroizeOnDrop for Decryptor<C> {}
struct Closure<'a, BS, BC>
where
BS: ArrayLength<u8>,
BC: BlockClosure<BlockSize = BS>,
{
iv: &'a mut GenericArray<u8, BS>,
f: BC,
}
impl<'a, BS, BC> BlockSizeUser for Closure<'a, BS, BC>
where
BS: ArrayLength<u8>,
BC: BlockClosure<BlockSize = BS>,
{
type BlockSize = BS;
}
impl<'a, BS, BC> BlockClosure for Closure<'a, BS, BC>
where
BS: ArrayLength<u8>,
BC: BlockClosure<BlockSize = BS>,
{
#[inline(always)]
fn call<B: BlockBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B) {
let Self { iv, f } = self;
f.call(&mut Backend { iv, backend });
}
}
struct Backend<'a, BS, BK>
where
BS: ArrayLength<u8>,
BK: BlockBackend<BlockSize = BS>,
{
iv: &'a mut GenericArray<u8, BS>,
backend: &'a mut BK,
}
impl<'a, BS, BK> BlockSizeUser for Backend<'a, BS, BK>
where
BS: ArrayLength<u8>,
BK: BlockBackend<BlockSize = BS>,
{
type BlockSize = BS;
}
impl<'a, BS, BK> ParBlocksSizeUser for Backend<'a, BS, BK>
where
BS: ArrayLength<u8>,
BK: BlockBackend<BlockSize = BS>,
{
type ParBlocksSize = BK::ParBlocksSize;
}
impl<'a, BS, BK> BlockBackend for Backend<'a, BS, BK>
where
BS: ArrayLength<u8>,
BK: BlockBackend<BlockSize = BS>,
{
#[inline(always)]
fn proc_block(&mut self, mut block: InOut<'_, '_, Block<Self>>) {
let in_block = block.clone_in();
let mut t = block.clone_in();
self.backend.proc_block((&mut t).into());
xor(&mut t, self.iv);
*block.get_out() = t;
*self.iv = in_block;
}
#[inline(always)]
fn proc_par_blocks(&mut self, mut blocks: InOut<'_, '_, ParBlocks<Self>>) {
let in_blocks = blocks.clone_in();
let mut t = blocks.clone_in();
self.backend.proc_par_blocks((&mut t).into());
let n = t.len();
xor(&mut t[0], self.iv);
for i in 1..n {
xor(&mut t[i], &in_blocks[i - 1])
}
*blocks.get_out() = t;
*self.iv = in_blocks[n - 1].clone();
}
}

180
vendor/cbc/src/encrypt.rs vendored Normal file
View File

@@ -0,0 +1,180 @@
use crate::xor;
use cipher::{
consts::U1,
crypto_common::{InnerUser, IvSizeUser},
generic_array::{ArrayLength, GenericArray},
inout::InOut,
AlgorithmName, Block, BlockBackend, BlockCipher, BlockClosure, BlockEncryptMut, BlockSizeUser,
InnerIvInit, Iv, IvState, ParBlocksSizeUser,
};
use core::fmt;
#[cfg(feature = "zeroize")]
use cipher::zeroize::{Zeroize, ZeroizeOnDrop};
/// CBC mode encryptor.
#[derive(Clone)]
pub struct Encryptor<C>
where
C: BlockEncryptMut + BlockCipher,
{
cipher: C,
iv: Block<C>,
}
impl<C> BlockSizeUser for Encryptor<C>
where
C: BlockEncryptMut + BlockCipher,
{
type BlockSize = C::BlockSize;
}
impl<C> BlockEncryptMut for Encryptor<C>
where
C: BlockEncryptMut + BlockCipher,
{
fn encrypt_with_backend_mut(&mut self, f: impl BlockClosure<BlockSize = Self::BlockSize>) {
let Self { cipher, iv } = self;
cipher.encrypt_with_backend_mut(Closure { iv, f })
}
}
impl<C> InnerUser for Encryptor<C>
where
C: BlockEncryptMut + BlockCipher,
{
type Inner = C;
}
impl<C> IvSizeUser for Encryptor<C>
where
C: BlockEncryptMut + BlockCipher,
{
type IvSize = C::BlockSize;
}
impl<C> InnerIvInit for Encryptor<C>
where
C: BlockEncryptMut + BlockCipher,
{
#[inline]
fn inner_iv_init(cipher: C, iv: &Iv<Self>) -> Self {
Self {
cipher,
iv: iv.clone(),
}
}
}
impl<C> IvState for Encryptor<C>
where
C: BlockEncryptMut + BlockCipher,
{
#[inline]
fn iv_state(&self) -> Iv<Self> {
self.iv.clone()
}
}
impl<C> AlgorithmName for Encryptor<C>
where
C: BlockEncryptMut + BlockCipher + AlgorithmName,
{
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("cbc::Encryptor<")?;
<C as AlgorithmName>::write_alg_name(f)?;
f.write_str(">")
}
}
impl<C> fmt::Debug for Encryptor<C>
where
C: BlockEncryptMut + BlockCipher + AlgorithmName,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("cbc::Encryptor<")?;
<C as AlgorithmName>::write_alg_name(f)?;
f.write_str("> { ... }")
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl<C: BlockEncryptMut + BlockCipher> Drop for Encryptor<C> {
fn drop(&mut self) {
self.iv.zeroize();
}
}
#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl<C: BlockEncryptMut + BlockCipher + ZeroizeOnDrop> ZeroizeOnDrop for Encryptor<C> {}
struct Closure<'a, BS, BC>
where
BS: ArrayLength<u8>,
BC: BlockClosure<BlockSize = BS>,
{
iv: &'a mut GenericArray<u8, BS>,
f: BC,
}
impl<'a, BS, BC> BlockSizeUser for Closure<'a, BS, BC>
where
BS: ArrayLength<u8>,
BC: BlockClosure<BlockSize = BS>,
{
type BlockSize = BS;
}
impl<'a, BS, BC> BlockClosure for Closure<'a, BS, BC>
where
BS: ArrayLength<u8>,
BC: BlockClosure<BlockSize = BS>,
{
#[inline(always)]
fn call<B: BlockBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B) {
let Self { iv, f } = self;
f.call(&mut Backend { iv, backend });
}
}
struct Backend<'a, BS, BK>
where
BS: ArrayLength<u8>,
BK: BlockBackend<BlockSize = BS>,
{
iv: &'a mut GenericArray<u8, BS>,
backend: &'a mut BK,
}
impl<'a, BS, BK> BlockSizeUser for Backend<'a, BS, BK>
where
BS: ArrayLength<u8>,
BK: BlockBackend<BlockSize = BS>,
{
type BlockSize = BS;
}
impl<'a, BS, BK> ParBlocksSizeUser for Backend<'a, BS, BK>
where
BS: ArrayLength<u8>,
BK: BlockBackend<BlockSize = BS>,
{
type ParBlocksSize = U1;
}
impl<'a, BS, BK> BlockBackend for Backend<'a, BS, BK>
where
BS: ArrayLength<u8>,
BK: BlockBackend<BlockSize = BS>,
{
#[inline(always)]
fn proc_block(&mut self, mut block: InOut<'_, '_, Block<Self>>) {
let mut t = block.clone_in();
xor(&mut t, self.iv);
self.backend.proc_block((&mut t).into());
*self.iv = t.clone();
*block.get_out() = t;
}
}

113
vendor/cbc/src/lib.rs vendored Normal file
View File

@@ -0,0 +1,113 @@
//! [Cipher Block Chaining][1] (CBC) mode.
//!
//! <img src="https://raw.githubusercontent.com/RustCrypto/media/26acc39f/img/block-modes/cbc_enc.svg" width="49%" />
//! <img src="https://raw.githubusercontent.com/RustCrypto/media/26acc39f/img/block-modes/cbc_dec.svg" width="49%"/>
//!
//! Mode functionality is accessed using traits from re-exported [`cipher`] crate.
//!
//! # ⚠️ Security Warning: Hazmat!
//!
//! This crate does not ensure ciphertexts are authentic! Thus ciphertext integrity
//! is not verified, which can lead to serious vulnerabilities!
//!
//! # Example
//! ```
//! # #[cfg(feature = "block-padding")] {
//! use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, KeyIvInit};
//! use hex_literal::hex;
//!
//! type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
//! type Aes128CbcDec = cbc::Decryptor<aes::Aes128>;
//!
//! let key = [0x42; 16];
//! let iv = [0x24; 16];
//! let plaintext = *b"hello world! this is my plaintext.";
//! let ciphertext = hex!(
//! "c7fe247ef97b21f07cbdd26cb5d346bf"
//! "d27867cb00d9486723e159978fb9a5f9"
//! "14cfb228a710de4171e396e7b6cf859e"
//! );
//!
//! // encrypt/decrypt in-place
//! // buffer must be big enough for padded plaintext
//! let mut buf = [0u8; 48];
//! let pt_len = plaintext.len();
//! buf[..pt_len].copy_from_slice(&plaintext);
//! let ct = Aes128CbcEnc::new(&key.into(), &iv.into())
//! .encrypt_padded_mut::<Pkcs7>(&mut buf, pt_len)
//! .unwrap();
//! assert_eq!(ct, &ciphertext[..]);
//!
//! let pt = Aes128CbcDec::new(&key.into(), &iv.into())
//! .decrypt_padded_mut::<Pkcs7>(&mut buf)
//! .unwrap();
//! assert_eq!(pt, &plaintext);
//!
//! // encrypt/decrypt from buffer to buffer
//! let mut buf = [0u8; 48];
//! let ct = Aes128CbcEnc::new(&key.into(), &iv.into())
//! .encrypt_padded_b2b_mut::<Pkcs7>(&plaintext, &mut buf)
//! .unwrap();
//! assert_eq!(ct, &ciphertext[..]);
//!
//! let mut buf = [0u8; 48];
//! let pt = Aes128CbcDec::new(&key.into(), &iv.into())
//! .decrypt_padded_b2b_mut::<Pkcs7>(&ct, &mut buf)
//! .unwrap();
//! assert_eq!(pt, &plaintext);
//! # }
//! ```
//!
//! With enabled `alloc` (or `std`) feature you also can use allocating
//! convinience methods:
//! ```
//! # #[cfg(all(feature = "alloc", feature = "block-padding"))] {
//! # use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, KeyIvInit};
//! # use hex_literal::hex;
//! # type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
//! # type Aes128CbcDec = cbc::Decryptor<aes::Aes128>;
//! # let key = [0x42; 16];
//! # let iv = [0x24; 16];
//! # let plaintext = *b"hello world! this is my plaintext.";
//! # let ciphertext = hex!(
//! # "c7fe247ef97b21f07cbdd26cb5d346bf"
//! # "d27867cb00d9486723e159978fb9a5f9"
//! # "14cfb228a710de4171e396e7b6cf859e"
//! # );
//! let res = Aes128CbcEnc::new(&key.into(), &iv.into())
//! .encrypt_padded_vec_mut::<Pkcs7>(&plaintext);
//! assert_eq!(res[..], ciphertext[..]);
//! let res = Aes128CbcDec::new(&key.into(), &iv.into())
//! .decrypt_padded_vec_mut::<Pkcs7>(&res)
//! .unwrap();
//! assert_eq!(res[..], plaintext[..]);
//! # }
//! ```
//!
//! [1]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CBC
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg",
html_root_url = "https://docs.rs/cbc/0.1.2"
)]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs, rust_2018_idioms)]
mod decrypt;
mod encrypt;
pub use cipher;
pub use decrypt::Decryptor;
pub use encrypt::Encryptor;
use cipher::generic_array::{ArrayLength, GenericArray};
#[inline(always)]
fn xor<N: ArrayLength<u8>>(out: &mut GenericArray<u8, N>, buf: &GenericArray<u8, N>) {
for (a, b) in out.iter_mut().zip(buf) {
*a ^= *b;
}
}