Coder Social home page Coder Social logo

crypto_api_chachapoly's Issues

AeadCipher::seal_to implementation copies plaintext into output buffer before error checks

The AeadCipher::seal API mutates the provided plaintext buffer to encrypt in-place, after performing various error checks. If the caller ignores the return value (and thus any errors), the plaintext will end up being used unencrypted. This is inherent in the design of the API, so it's a risk that the caller is clearly signing up to.

The AeadCipher::seal_to API provides an interface for interacting with an AEAD that ostensibly encrypts from one buffer into another, according to its documentation:

AEAD-seals plaintext into buf together with ad using key and nonce and returns the ciphertext length

Thus a caller could reasonably assume that if they ignored the return value, the output buffer would be unmodified, and the plaintext would not leak. However, the implementation of AeadCipher::seal_to for ChachaPolyIetf copies the plaintext into the output buffer and then calls AeadCipher::seal internally, breaking this assumption.

This could be addressed by copying the logic from seal into seal_to, so that all error checks are performed first, and the encryption operation is guaranteed to succeed at the time the plaintext is copied into the output buffer.

Originally raised by @daira here.

User of new_sec_key might assume it fills the entire buffer

I can imagine someone misinterpreting new_sec_key's thinking that it fills the entire buffer with random bytes, but it actually only fills the first 32-bytes. To avoid accidental mistakes, I'd recommend changing this line to buf.len() == 32 instead of buf.len() < 32.

(BTW I compared the ChaCha20 and Poly1305 implementations to RFC 8439. I didn't look at the tests. This issue and #2 are the only problems I found).

CHACHAPOLY_MAX is too large by 1 block

In the AEAD construction, block counter = 0 is used to generate the poly1305 key, and encryption of data blocks starts with block counter = 1, so at most 2^32 - 1 blocks can be encrypted. However, the value of CHACHAPOLY_MAX is 2^32 blocks. I believe this means that the code will let you encrypt 2^32*64 bytes, and the last block will be XORed with the poly1305 key, because of n overflowing (at least in release builds).

In addition to fixing the constant I'd recommend adding a check (that works even in release builds) for n overflowing in the xor function.

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.