Skip to content

Commit

Permalink
impl IvState for cfb-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Feb 6, 2022
1 parent 102b20d commit ff23e5c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
25 changes: 14 additions & 11 deletions cfb-mode/src/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use cipher::{
crypto_common::{InnerUser, IvSizeUser},
generic_array::{ArrayLength, GenericArray},
inout::InOut,
AlgorithmName, AsyncStreamCipher, Block, BlockBackend, BlockCipher, BlockClosure,
BlockDecryptMut, BlockEncryptMut, BlockSizeUser, InnerIvInit, Iv, ParBlocks, ParBlocksSizeUser,
AlgorithmName, AsyncStreamCipher, Block, BlockBackend, BlockCipher, BlockClosure, BlockDecrypt,
BlockDecryptMut, BlockEncryptMut, BlockSizeUser, InnerIvInit, Iv, IvState, ParBlocks,
ParBlocksSizeUser,
};
use core::fmt;

Expand Down Expand Up @@ -65,15 +66,17 @@ where
}
}

// impl<C> IvState for Decryptor<C>
// where
// C: BlockEncryptMut + BlockDecryptMut + BlockCipher,
// {
// #[inline]
// fn iv_state(&self) -> Iv<Self> {
// self.iv.clone()
// }
// }
impl<C> IvState for Decryptor<C>
where
C: BlockEncryptMut + BlockDecrypt + BlockCipher,
{
#[inline]
fn iv_state(&self) -> Iv<Self> {
let mut res = self.iv.clone();
self.cipher.decrypt_block(&mut res);
res
}
}

impl<C> AlgorithmName for Decryptor<C>
where
Expand Down
24 changes: 13 additions & 11 deletions cfb-mode/src/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use cipher::{
crypto_common::{InnerUser, IvSizeUser},
generic_array::{ArrayLength, GenericArray},
inout::InOut,
AlgorithmName, AsyncStreamCipher, Block, BlockBackend, BlockCipher, BlockClosure,
BlockEncryptMut, BlockSizeUser, InnerIvInit, Iv, ParBlocksSizeUser,
AlgorithmName, AsyncStreamCipher, Block, BlockBackend, BlockCipher, BlockClosure, BlockDecrypt,
BlockEncryptMut, BlockSizeUser, InnerIvInit, Iv, IvState, ParBlocksSizeUser,
};
use core::fmt;

Expand Down Expand Up @@ -66,15 +66,17 @@ where
}
}

// impl<C> IvState for Encryptor<C>
// where
// C: BlockEncryptMut + BlockCipher,
// {
// #[inline]
// fn iv_state(&self) -> Iv<Self> {
// self.iv.clone()
// }
// }
impl<C> IvState for Encryptor<C>
where
C: BlockEncryptMut + BlockDecrypt + BlockCipher,
{
#[inline]
fn iv_state(&self) -> Iv<Self> {
let mut res = self.iv.clone();
self.cipher.decrypt_block(&mut res);
res
}
}

impl<C> AlgorithmName for Encryptor<C>
where
Expand Down
4 changes: 1 addition & 3 deletions cfb-mode/tests/aes.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use aes::*;
use cfb_mode::{Decryptor, Encryptor};
use cipher::{block_mode_dec_test, block_mode_enc_test};
use cipher::{block_mode_dec_test, block_mode_enc_test, iv_state_test};

/*
iv_state_test!(aes128_cfb_enc_iv_state, Encryptor<Aes128>, encrypt);
iv_state_test!(aes128_cfb_dec_iv_state, Decryptor<Aes128>, decrypt);
iv_state_test!(aes192_cfb_enc_iv_state, Encryptor<Aes192>, encrypt);
iv_state_test!(aes192_cfb_dec_iv_state, Decryptor<Aes192>, decrypt);
iv_state_test!(aes256_cfb_enc_iv_state, Encryptor<Aes256>, encrypt);
iv_state_test!(aes256_cfb_dec_iv_state, Decryptor<Aes256>, decrypt);
*/

// Test vectors from CVAP "AES Multiblock Message Test (MMT) Sample Vectors":
// <https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Block-Ciphers>
Expand Down

0 comments on commit ff23e5c

Please sign in to comment.