cederom commented on PR #4113:
URL: https://github.com/apache/nuttx-apps/pull/4113#issuecomment-7116399840

   Thanks, the bug is real: in `crypto/cryptosoft.c` OFB/CFB8/CFB128 have a 
`reinit` hook, so `swcr_encdec()` never writes the IV back to `crp_iv`, and 
every call restarted from the caller's initial IV.
   
   I checked the new IV derivation against a reference AES implementation 
(random messages split into random chunks, chained via the derived IV, compared 
with a single call; whole-block chunks for CFB128/OFB, arbitrary chunks for 
CFB8, encrypt and decrypt): no mismatches. In-place operation is also fine 
since the input is saved before the call.
   
   A few points before merge:
   
   **1. Partial blocks still reuse the keystream silently (CFB128/OFB)**
   
   After a call whose length is not a multiple of 16, `iv[]` is left unchanged 
and `*iv_off` becomes non-zero. The next call ignores the incoming `*iv_off` 
and starts again from the old `iv[]`, so for OFB (and CFB128) encryption the 
same keystream is reused. With OFB, XOR of the two ciphertexts gives XOR of the 
two plaintexts. This is pre-existing, but now the functions look like they 
support chaining while still giving wrong (and insecure) output for a common 
case: `mbedtls_cipher_update()` passes `&ctx->unprocessed_len` as `iv_off`, so 
any multipart use with odd lengths reaches it.
   
   Also, `*iv_off = length % 16` doesn't follow mbedtls's meaning of `iv_off` 
(it should be `(*iv_off + length) % 16`).
   
   Could you either:
   - reject `*iv_off != 0` on entry with `MBEDTLS_ERR_AES_BAD_INPUT_DATA`, so a 
caller gets an error instead of a reused keystream, or
   - handle the partial head/tail in software the way mbedtls does (keep 
`E(iv)` in `iv[]` and XOR the bytes up to the block boundary; 
`mbedtls_aes_crypt_ecb()` is available here), and pass only the whole blocks to 
the driver?
   
   The first is a small change and enough for this PR, in my opinion.
   
   **2. The `ivlen` change is incomplete for virtio and leaks into other modes**
   
   - `virtio_crypto_fill_dataq_request_cipher()` also takes `dst_data_len` from 
`crp_olen`, i.e. `cop->olen`, which `aes_alt.c` never sets (the driver tests in 
`testing/drivers/crypto` set both `olen` and `ivlen`). So setting `ivlen` alone 
probably doesn't make these modes work on virtio, and the commit says it wasn't 
run there.
   - `ctx->dev.crypt` lives in the context and is only cleared in 
`cryptodev_init()`, so after a CFB/OFB call `ivlen = 16` stays set for later 
CBC/ECB/XTS/CTR calls on the same context. For CTR, `crypt.iv` is 
`nonce_counter + 4`, so a virtio device would read 16 bytes from a 12-byte 
remainder, 4 bytes past the caller's array.
   - CBC/ECB/XTS/CTR don't set `ivlen` either, so they have the same virtio 
issue.
   
   I'd suggest either dropping the `ivlen` lines from this PR and fixing virtio 
separately (setting `ivlen`/`olen` explicitly in every function), or doing that 
for all modes here and testing on a virtio-crypto device (e.g. QEMU).
   
   **3. Minor**
   
   - `mbedtls_aes_crypt_cfb8()` with `length == 0`: `input + length - n` and 
`memcpy(..., 0)` on a possibly NULL `input` is undefined behaviour; an early 
`if (length == 0) return 0;` avoids it.
   - In CFB128 the `memset(temp, ...)` is only needed for decryption; harmless.
   
   **4. Testing**
   
   Build-only testing doesn't exercise the change. Could you add (or at least 
run and paste) a multi-call case, e.g. encrypt a message in one call and in 
several whole-block calls, and check the outputs are the same for 
CFB128/CFB8/OFB? mbedtls's own multipart cipher tests (`test_suite_cipher`) 
would also catch both the fixed case and point 1.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to