Skip to content

RustCrypto in TockEnv#625

Merged
kaczmarczyck merged 3 commits intogoogle:developfrom
kaczmarczyck:rust-crypto-tock
May 5, 2023
Merged

RustCrypto in TockEnv#625
kaczmarczyck merged 3 commits intogoogle:developfrom
kaczmarczyck:rust-crypto-tock

Conversation

@kaczmarczyck
Copy link
Copy Markdown
Collaborator

Adds a feature flag for RustCrypto (see #1).

As mentioned in #620, binary size increases significantly with the new flag. Our crypto_bench example also sees that speed decreases. Non-embedded new environments might still find it interesting.

The benchmarks and cargo bloat allow a more detailed analysis: AES doesn't seem to hurt binary size or speed, so we could decide to move to the RustCrypto implementation there, and keep ours for ECDSA for now.

@kaczmarczyck kaczmarczyck requested a review from ia0 May 5, 2023 08:06
@coveralls
Copy link
Copy Markdown

coveralls commented May 5, 2023

Coverage Status

Coverage: 96.196% (-0.1%) from 96.34% when pulling 937652f on kaczmarczyck:rust-crypto-tock into f25cdd6 on google:develop.

ia0
ia0 previously approved these changes May 5, 2023
Copy link
Copy Markdown
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

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

Wait, I've just seen the performance comment, let me write something down to try to fix it.

Comment thread Cargo.toml
with_nfc = ["libtock_drivers/with_nfc"]
vendor_hid = ["opensk/vendor_hid"]
ed25519 = ["ed25519-compact", "opensk/ed25519"]
rust_crypto = ["opensk/rust_crypto"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

At the end of this file, you can add the following for crates you want to optimize. This will trade with binary size, so only select those that matter. Might not work though, so needs testing.

[profile.release.package]
aes = { opt-level = 3 }
p256 = { opt-level = 3 }
sha2 = { opt-level = 3 }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good idea! I'll keep the speed settings, as RustCrypto is only an option for boards with enough binary size anyway.

Speed in ms / iter (-Oz -> -O3 (our implementation)):

AES and SHA now beats our implementation, but ECDSA is still slower. I wonder why key generation is so slow though.

Our impl -Oz -O3
Aes256::encrypt_block 0.21 0.55 0.37
Aes256::decrypt_block 1.28 0.55 0.40
Sha256::digest(32 bytes) 0.18 0.24 0.15
Ecdsa::SecretKey::random 3.51 356.84 221.50
Ecdsa::SecretKey::public_key 114.62 0.03 0.03
Ecdsa::SecretKey::sign 194.89 402.95 240.11

Binary size

  • aes: +2.1 kB
  • sha2: +3.7 kB
  • p256: +7.0 kB

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

First time I try this, so I'm learning. Apparently this is not recursive, so you have to specify the relevant dependencies too. I think we need to add also those crates to the list of opt-level = 3:

  • ecdsa
  • elliptic-curve
  • crypto-bigint
  • maybe primeorder
  • maybe rand_core
  • maybe subtle

Or maybe a subset of those is enough, or maybe I'm still missing some that are important.

I don't think it's worth trying to optimize too much though, so I would not try more than once.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

1% faster for Ecdsa::SecretKey::sign, no effect on anything else, but we lose more binary size. So I suggest we keep it as is!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sounds good

Comment thread Cargo.toml
with_nfc = ["libtock_drivers/with_nfc"]
vendor_hid = ["opensk/vendor_hid"]
ed25519 = ["ed25519-compact", "opensk/ed25519"]
rust_crypto = ["opensk/rust_crypto"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sounds good

@kaczmarczyck kaczmarczyck merged commit 99f81ad into google:develop May 5, 2023
@kaczmarczyck kaczmarczyck deleted the rust-crypto-tock branch May 6, 2023 07:33
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