Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aes: implement Zeroize for aes keys #310

Merged
merged 1 commit into from Mar 18, 2022
Merged

aes: implement Zeroize for aes keys #310

merged 1 commit into from Mar 18, 2022

Conversation

aticu
Copy link
Contributor

@aticu aticu commented Mar 16, 2022

The crate feature zeroize was previously present, but unused.

This implements zeroization on drop for all aes types in the software and aesni implementations, if the zeroize feature is enabled.

This PR does not include support for the armv8 implementation.
I do not have access to hardware to test it and a clean implementation is also likely blocked on the zeroize crate implementing Zeroize for the inner uint8x16_t type used.

Still I thought this might be a useful patch even without armv8 support.
If you'd prefer, I can also add some documentation describing this limitation to users.

@tarcieri
Copy link
Member

I could potentially add a feature to zeroize to make zeroization on ARMv8 possible.

The necessary intrinsics were stabilized in Rust 1.59.

@aticu
Copy link
Contributor Author

aticu commented Mar 16, 2022

In that case I could add the code required for that, but I wouldn't be able to test it.

@tarcieri
Copy link
Member

I have an M1 Mac Mini I can test on locally

@aticu
Copy link
Contributor Author

aticu commented Mar 16, 2022

Great! I've updated the PR to include an implementation for that as well. Once zeroize is updated, only a version bump should be necessary for you to test it locally, assuming I didn't make any mistakes.

@tarcieri
Copy link
Member

Here's a PR to add support to zeroize: RustCrypto/utils#749

Turns out I was wrong: only floating point NEON support was stabilized in Rust 1.59. So that feature is nightly-only, but that's fine, because the armv8 backend for the aes crate is also nightly-only.

aes/src/armv8.rs Outdated Show resolved Hide resolved
@tarcieri
Copy link
Member

tarcieri commented Mar 17, 2022

I released zeroize v1.5.4 with support for zeroizing SIMD registers on aarch64: RustCrypto/utils#750

You'll need to enable the zeroize/aarch64 crate feature when the armv8 feature of aes is enabled.

Pinning to it will be a bit tricky since it's a transitive dependency of cipher, but if you can get things to build with v1.5.4 in Cargo.lock we can figure something out (e.g. pinning it in cipher and then pinning to a new release of cipher)

Also note that there's a lot changing on nightly around aarch64 right now due to the stabilization of aarch64_target_feature. We might be in for a bumpy ride for the next few days.

I pinned to nightly-2022-03-01 in zeroize and that seems to be working.

@aticu
Copy link
Contributor Author

aticu commented Mar 17, 2022

Alright, I've updated the code with the different approach to implementing Drop. You're right, I also think this approach is better.

I tried to at least test compiling for armv8, but I wasn't sure how to enable the aarch64 feature in zeroize, since it isn't a direct dependency. Should I maybe add it as a direct dependency instead of using the version in cipher? Otherwise the only option I can think of is to add a feature for that in cipher that can then be enabled in aes.
Do you know of a way to enable features in transitive dependencies?

@tarcieri
Copy link
Member

Aah yeah, you'll probably want to use a direct dependency

@aticu
Copy link
Contributor Author

aticu commented Mar 17, 2022

Alright, I did that. I guess this is ready for review then. It at least passes cargo check for me for armv8 targets.

@tarcieri
Copy link
Member

tarcieri commented Mar 17, 2022

Hmm, weird, the CI workflow isn't running properly due to a syntax error: https://github.com/RustCrypto/block-ciphers/actions/runs/1998929396/workflow#L42

@aticu
Copy link
Contributor Author

aticu commented Mar 17, 2022

The other file that failed was kuznyechik.yml. They both have in common that the use the

      - run: |
        cargo build --target ${{ matrix.target }}
        cargo build --target ${{ matrix.target }} --features hazmat

syntax.
The other files in the .github/workflows directory don't use that syntax.
Should I change that to use the

      - run: cargo build --target ${{ matrix.target }}
      - run: cargo build --target ${{ matrix.target }} --features hazmat

syntax instead? And should I do it in this PR or open a new one?
Or is it something else entirely? Honestly I don't know too much about GitHub workflows.

@tarcieri
Copy link
Member

Already fixed it: #311

Can you rebase?

@aticu
Copy link
Contributor Author

aticu commented Mar 17, 2022

Done

aes/src/lib.rs Outdated Show resolved Hide resolved
@aticu
Copy link
Contributor Author

aticu commented Mar 17, 2022

The tests all pass now.

@tarcieri tarcieri requested a review from newpavlov March 17, 2022 18:06
@@ -172,6 +175,16 @@ macro_rules! define_aes_impl {
}
}

impl Drop for $name_enc {
fn drop(&mut self) {
Copy link
Member

@newpavlov newpavlov Mar 18, 2022

Choose a reason for hiding this comment

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

It could be worth to add #[inline] attribute here and in other Drop impls. Otherwise, without LTO Rust will not be able to inline it.

Also, are we sure that Rust always handles empty Drop impls in the same way as unimplemented Drop? I vaguely remember that Rust has a special handling of Drop-free types.

Personally, I think that having a Drop impl gated on zeroize is fine (I've used such approach in other crates), especially if we will use doc_cfg to expose it in docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I'm aware the only difference between implementing Drop and not implementing it is being able to implement Copy. The reference also does not seem to say much more about that.

The only other differences I could think of are performance issues, such as not having to track drop flags and not running an empty destructor (though inlining should prevent that). I added the inline attribute to all drop functions now.

In general I agree with @tarcieri that conditionally implementing Drop (or any trait) based on a not obviously related feature could lead to problems.

Copy link
Member

@tarcieri tarcieri Mar 18, 2022

Choose a reason for hiding this comment

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

If nothing else the bounds are different, although whether Drop bounds should be used/allowed at all is a matter of debate. Nevertheless, they do.

The other major difference is it's not possible to move fields out of a struct which impls Drop, although that's not happening in the current implementation so it's not a major issue.

I think Drop is magical enough conditionally dropping based on whether or not features are impl'd is a bit weird.

@tarcieri tarcieri merged commit ca6a657 into RustCrypto:master Mar 18, 2022
@aticu
Copy link
Contributor Author

aticu commented Mar 18, 2022

Thank you for the responses and help in getting this merged so quickly!

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.

None yet

3 participants