RustCrypto/block-ciphers

After upgrading from `aes=0.8` to `0.9` the existing code no longer work.

liweinan opened this issue · 6 comments

After upgrading from aes=0.8 to 0.9 the following code no longer work:

    use aes::Aes128;
    use block_modes::block_padding::Pkcs7;
    use block_modes::{BlockMode, Cbc};

    let key = base64::decode(session_key).unwrap();
    let iv = base64::decode(params.iv.clone().unwrap()).unwrap();
    type Aes128Cbc = Cbc<Aes128, Pkcs7>;

    let cipher = Aes128Cbc::new_from_slices(&key, &iv).unwrap();
    let decrypted_data = cipher.decrypt(&mut decoded_encrypted_data).unwrap();

It's hard to find guide on upgrade migration. Can anyone provide some example how to do the migration here?

Have you seen the notice about block-modes deprecation? I think documentation of the cbc crate should be sufficient in your case.

@newpavlov Thanks for the info. I'll check it to see how to resolve the problem.

I meet the the same problem, when i change aes from '0.7.0' to '0.8.0'

Only change the Cargo.toml file , no code part

OS: MacOS
rustup 1.24.3
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active rustc version is rustc 1.60.0-nightly

   Compiling cipher v0.4.0
error[E0310]: the parameter type `BS` may not live long enough
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/cipher-0.4.0/src/block.rs:535:22
    |
535 |     type BlockSize = BS;
    |                      ^^
    |
    = help: consider adding an explicit lifetime bound `BS: 'static`...
    = note: ...so that the type `BS` will meet its required lifetime bounds...
note: ...that is required by this bound
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/crypto-common-0.1.2/src/lib.rs:39:39
    |
39  |     type BlockSize: ArrayLength<u8> + 'static;
    |                                       ^^^^^^^

error[E0310]: the parameter type `BS` may not live long enough
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/cipher-0.4.0/src/block.rs:551:22
    |
551 |     type BlockSize = BS;
    |                      ^^
    |
    = help: consider adding an explicit lifetime bound `BS: 'static`...
    = note: ...so that the type `BS` will meet its required lifetime bounds...
note: ...that is required by this bound
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/crypto-common-0.1.2/src/lib.rs:39:39
    |
39  |     type BlockSize: ArrayLength<u8> + 'static;
    |                                       ^^^^^^^

error[E0310]: the parameter type `BS` may not live long enough
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/cipher-0.4.0/src/stream_core.rs:218:22
    |
218 |     type BlockSize = BS;
    |                      ^^
    |
    = help: consider adding an explicit lifetime bound `BS: 'static`...
    = note: ...so that the type `BS` will meet its required lifetime bounds...
note: ...that is required by this bound
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/crypto-common-0.1.2/src/lib.rs:39:39
    |
39  |     type BlockSize: ArrayLength<u8> + 'static;
    |                                       ^^^^^^^

error[E0310]: the parameter type `BS` may not live long enough
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/cipher-0.4.0/src/stream_core.rs:231:22
    |
231 |     type BlockSize = BS;
    |                      ^^
    |
    = help: consider adding an explicit lifetime bound `BS: 'static`...
    = note: ...so that the type `BS` will meet its required lifetime bounds...
note: ...that is required by this bound
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/crypto-common-0.1.2/src/lib.rs:39:39
    |
39  |     type BlockSize: ArrayLength<u8> + 'static;
    |                                       ^^^^^^^

error[E0310]: the parameter type `BS` may not live long enough
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/cipher-0.4.0/src/stream_core.rs:255:22
    |
255 |     type BlockSize = BS;
    |                      ^^
    |
    = help: consider adding an explicit lifetime bound `BS: 'static`...
    = note: ...so that the type `BS` will meet its required lifetime bounds...
note: ...that is required by this bound
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/crypto-common-0.1.2/src/lib.rs:39:39
    |
39  |     type BlockSize: ArrayLength<u8> + 'static;
    |                                       ^^^^^^^

error[E0310]: the parameter type `BS` may not live long enough
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/cipher-0.4.0/src/stream_core.rs:272:22
    |
272 |     type BlockSize = BS;
    |                      ^^
    |
    = help: consider adding an explicit lifetime bound `BS: 'static`...
    = note: ...so that the type `BS` will meet its required lifetime bounds...
note: ...that is required by this bound
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/crypto-common-0.1.2/src/lib.rs:39:39
    |
39  |     type BlockSize: ArrayLength<u8> + 'static;
    |                                       ^^^^^^^

@xiaolitongxue666
What do you expect? For pre-1.0 crates minor version bump usually means breaking changes. See my previous comment about migration from block-modes. To re-iterate: block-modes is deprecated in favor of the crates from RustCrypto/block-modes repository, see their documentation if you want to migrate to cipher v0.4 crates (such as aes v0.8).

@xiaolitongxue666 RustCrypto/traits#937 might be related to your problem

This block of code works.

[dependencies]
cbc = "0.1.0"
aes = "0.8.0"
block-modes = "0.9.0"
hex-literal = "0.3.4"
fn main() {

        use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, KeyIvInit};
        use cbc::{Decryptor};
        use hex_literal::hex;

        type Aes128CbcDec = 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 = vec![0u8; 48];

        // let pt_len = plaintext.len();
        // buf[..pt_len].copy_from_slice(&plaintext[..]);

        let ct_len = ciphertext.len();
        buf[..ct_len].copy_from_slice(&ciphertext[..]);

        //Decrypt
        let pt = Aes128CbcDec::new(&key.into(), &iv.into())
            .decrypt_padded_mut::<Pkcs7>(&mut buf)
            .unwrap();
        assert_eq!(pt, &plaintext[..]);

}