Giter Site home page Giter Site logo

Comments (5)

kitanic avatar kitanic commented on August 10, 2024

I found one more erroneous behavior about PKCS#7.

The decrypted result with padding should always be smaller than the ciphertext, but the storage buffer requires the size of the ciphertext + 16bytes.
The following code will result in error.

fn decode_test(ciphertext: &[u8], key: &[u8], iv: &[u8]) {
  let mut plain = Vec::new();

  plain.resize(ciphertext.len(), 0); // MUST: plain < cipher

  let mut cipher = Cipher::<Decryption, Traditional>::new(CipherId::Aes, CipherMode::CBC, (key.len() as u32) * 8)?;
  cipher.set_padding(CipherPadding::Pkcs7)?;
  let cipher = cipher.set_key_iv(key, iv)?;

  cipher.decrypt(ciphertext, &mut plain)?; // An error occurred here.
}

from rust-mbedtls.

jethrogb avatar jethrogb commented on August 10, 2024

This requirement comes from MbedTLS

output The buffer for the output data. This must be able to hold at least ilen + block_size. This must not be the same buffer as input.

https://tls.mbed.org/api/cipher_8h.html#a1c249f6ee1a0d2c906927c7790c41dc5

from rust-mbedtls.

kitanic avatar kitanic commented on August 10, 2024

No. It is incorrect.

In practice, it don't need more buffer than necessary, and it can encrypt without any problem if you have enough space for just blocksize boundary.
Perhaps the mbedtls documentation is wrong(!).

This is verification code.
check.c.txt

from rust-mbedtls.

jethrogb avatar jethrogb commented on August 10, 2024

I understand that logically in some cases not the full output buffer is accessed, but for memory safety we must follow the documented API. We can relax the requirements in the bindings if MbedTLS does.

from rust-mbedtls.

kitanic avatar kitanic commented on August 10, 2024

I agree it.
So my suggestion is to not do a prior buffer size check in the rust side implementation. mbedtls should receive olen during encryption/decryption to check for buffer size and return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if necessary. Therefore, there is no risk of crashing if you pass an undersized buffer.

Wrappers should not limit native behavior, and should follow its error codes.

from rust-mbedtls.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.