Skip to content

Rust wrapper: copy aead keys directly into return struct - #11380

Open
holtrop-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
holtrop-wolfssl:f-11286
Open

Rust wrapper: copy aead keys directly into return struct#11380
holtrop-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
holtrop-wolfssl:f-11286

Conversation

@holtrop-wolfssl

@holtrop-wolfssl holtrop-wolfssl commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Description

Rust wrapper: copy aead keys directly into return struct

Fixes F-11286.

Testing

How did you test?

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@holtrop-wolfssl holtrop-wolfssl self-assigned this Sep 4, 2026
Copilot AI lite review requested due to automatic review settings September 4, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new code still introduces an avoidable extra stack copy of the key via a temporary buffer, and it can be simplified to copy directly into the struct field to better align with the stated security goal.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates the Rust wolfcrypt wrapper’s AEAD KeyInit::new() implementations to reduce residual key material on the stack by explicitly zeroizing the temporary key buffer after initializing the AEAD struct (Fixes F-11286).

Changes:

  • Zeroize the temporary [u8; N] key buffer (k) after constructing AEAD key-holding structs.
  • Apply the same pattern across ChaCha20-Poly1305/XChaCha20-Poly1305 and AES-{CCM,GCM} AEAD wrappers.
File summaries
File Description
wrapper/rust/wolfssl-wolfcrypt/src/chacha20_poly1305.rs Zeroizes temporary AEAD key buffer during KeyInit::new() for ChaCha20-Poly1305 and XChaCha20-Poly1305 wrappers.
wrapper/rust/wolfssl-wolfcrypt/src/aes.rs Zeroizes temporary AEAD key buffer during KeyInit::new() for AES-CCM and AES-GCM wrappers.
Review details

Suppressed comments (6)

wrapper/rust/wolfssl-wolfcrypt/src/chacha20_poly1305.rs:536

  • The temporary stack buffer k holds a full copy of the AEAD key; even though it’s zeroized, you can avoid creating the extra key copy entirely by writing directly into the struct field.
    fn new(key: &aead::Key<Self>) -> Self {
        let mut k = [0u8; 32];
        k.copy_from_slice(key.as_ref());
        let out = XChaCha20Poly1305Aead { key: k };
        k.zeroize();
        out
    }

wrapper/rust/wolfssl-wolfcrypt/src/aes.rs:586

  • This creates a second stack copy of the key in k. You can avoid the extra copy (and the need to zeroize it) by copying directly into the struct’s key field.
    fn new(key: &aead::Key<Self>) -> Self {
        let mut k = [0u8; 24];
        k.copy_from_slice(key.as_ref());
        let out = Aes192Ccm { key: k };
        k.zeroize();
        out
    }

wrapper/rust/wolfssl-wolfcrypt/src/aes.rs:640

  • This creates a second stack copy of the key in k. You can avoid the extra copy (and the need to zeroize it) by copying directly into the struct’s key field.
    fn new(key: &aead::Key<Self>) -> Self {
        let mut k = [0u8; 32];
        k.copy_from_slice(key.as_ref());
        let out = Aes256Ccm { key: k };
        k.zeroize();
        out
    }

wrapper/rust/wolfssl-wolfcrypt/src/aes.rs:1862

  • This creates a second stack copy of the key in k. You can avoid the extra copy (and the need to zeroize it) by copying directly into the struct’s key field.
    fn new(key: &aead::Key<Self>) -> Self {
        let mut k = [0u8; 32];
        k.copy_from_slice(key.as_ref());
        let out = Aes256Gcm { key: k };
        k.zeroize();
        out
    }

wrapper/rust/wolfssl-wolfcrypt/src/aes.rs:1754

  • This creates a second stack copy of the key in k. You can avoid the extra copy (and the need to zeroize it) by copying directly into the struct’s key field.
    fn new(key: &aead::Key<Self>) -> Self {
        let mut k = [0u8; 16];
        k.copy_from_slice(key.as_ref());
        let out = Aes128Gcm { key: k };
        k.zeroize();
        out
    }

wrapper/rust/wolfssl-wolfcrypt/src/aes.rs:1808

  • This creates a second stack copy of the key in k. You can avoid the extra copy (and the need to zeroize it) by copying directly into the struct’s key field.
    fn new(key: &aead::Key<Self>) -> Self {
        let mut k = [0u8; 24];
        k.copy_from_slice(key.as_ref());
        let out = Aes192Gcm { key: k };
        k.zeroize();
        out
    }
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread wrapper/rust/wolfssl-wolfcrypt/src/aes.rs
Comment thread wrapper/rust/wolfssl-wolfcrypt/src/chacha20_poly1305.rs
Avoid local variable to store them.

Fixes F-11286.
@holtrop-wolfssl holtrop-wolfssl changed the title Rust wrapper: zeroize aead keys on stack Rust wrapper: copy aead keys directly into return struct Sep 4, 2026
@holtrop-wolfssl

Copy link
Copy Markdown
Contributor Author

retest this please (ERROR: PRB-fips-ready-config #14766 was deleted)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants